tutorial.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <html>
  2. <head>
  3. <title>libigl developer tutorial</title>
  4. <link href="./style.css" rel="stylesheet" type="text/css">
  5. </head>
  6. <body class=article_body>
  7. <div class=article>
  8. <a href=.><img src=libigl-logo.jpg alt="igl logo" class=center></a>
  9. <h1>Using the libigl library</h1>
  10. <p>
  11. The <a href=.>libigl</a> library is a collection of useful/reusable/sharable C++ functions
  12. with very few external <a href="#dependencies">dependencies</a>. The
  13. library may be used as a <a href="#header_library">"headers only"
  14. library</a> or a <a href=#static_library>statically linked library</a>.
  15. </p>
  16. <p>
  17. This duality is illustrated in the example
  18. <code>examples/example_fun</code>. When built this example compiles two
  19. binaries, one using the <code>example_fun</code> routine of the igl
  20. library, either as a headers-only library or linking against the igl
  21. static library.
  22. </p>
  23. <h2 id=header_library>Headers (.h) only library</h2>
  24. <p>
  25. All classes and functions in the libigl library are written in a way in
  26. which the entire library may be compiled <i>just-in-time</i>,
  27. effectively behaiving as if it were a "headers only" library (like e.g.
  28. Eigen). This is achieved by careful organization of each pair of .h and
  29. .cpp files. To take advantage of this one must only include the path to
  30. <code>libigl</code> directory in one's project's include path and
  31. define the preprocessor macro <code>IGL_HEADER_ONLY</code>.
  32. </p>
  33. <p>
  34. Defining <code>IGL_HEADER_ONLY</code> may be done at the project level,
  35. prescribing that all included igl headers be treated as code that
  36. should be inlined. Consequently all templated functions will be derived
  37. at compile time if need be.
  38. </p>
  39. <p>
  40. One may also define <code>IGL_HEADER_ONLY</code> not only on a per-file
  41. basis, but a <i>per-include</i> basis. For example it may be useful for a
  42. project to use the static library for most functionality from IGL, but then
  43. include a certain IGL function as an inlined function. This may be achieved
  44. by surrounding the relevant include with a define and undefine of the
  45. <code>IGL_HEADER_ONLY</code> macro. Here's an example of the little
  46. widget:
  47. </p>
  48. <pre><code>
  49. ...
  50. #include &lt;igl/some_other_igl_function.h&gt;
  51. #ifndef IGL_HEADER_ONLY
  52. # define IGL_HEADER_ONLY
  53. # define IGL_HEADER_ONLY_WAS_NOT_DEFINED
  54. #endif
  55. #include &lt;igl/igl_function_to_inline.h&gt;
  56. #ifdef IGL_HEADER_ONLY_WAS_NOT_DEFINED
  57. # undef IGL_HEADER_ONLY
  58. #endif
  59. #include &lt;igl/yet_another_igl_function.h&gt;
  60. ...
  61. </code></pre>
  62. <span class=todo>example <code>examples/XXX</code> also highlights this
  63. feature</span>
  64. <div class=note>This practice is not recommended outside of debugging
  65. purposes.</div>
  66. <h3>Benefits of headers-only library</h3>
  67. <ul>
  68. <li><strong>Easy templates:</strong> When using the libigl library as a
  69. headers-only library no special care need be taken when using templated
  70. functions.</li>
  71. </ul>
  72. <h3>Drawbacks of headers-only library</h3>
  73. <ul>
  74. <li><strong>Inlining not guaranteed:</strong> Most compilers do not
  75. guarantee that functions will get inlined even if explicitly told to do
  76. so. Though we have not yet encountered this problem, it is always a
  77. risk.</li>
  78. <li><strong>Long compile, large binary:</strong>As a headers-only
  79. library we depend on the compiler to properly inline each function
  80. call. This means compile time is high and binary size can be quite
  81. large.</li>
  82. </ul>
  83. <h2 id=static_library>Statically linked library</h2>
  84. <h3 id=explicit_specialization_of_templated_functions>Explicit
  85. specialization of templated functions</h3>
  86. <p>
  87. Special care must be taken by the developers of each function and
  88. class in the libigl library that uses C++ templates. If this function
  89. is intended to be compiled into the statically linked libigl library
  90. then function is only compiled for each <i>explicitly</i> specialized
  91. declaration. These should be added at the bottom of the corresponding
  92. .cpp file surrounded by a <code>#ifndef IGL_HEADER_ONLY</code>.
  93. </p>
  94. <p>
  95. Of course, a developer may not know ahead of time which
  96. specializations should be explicitly included in the igl static lib.
  97. One way to find out is to add one explicit specialization for each
  98. call in one's own project. This only ever needs to be done once for
  99. each template.
  100. </p>
  101. <p>
  102. The process is somewhat mechanical using a linker with reasonable error
  103. output.
  104. </p>
  105. <p>
  106. Supposed for example we have compiled the igl static lib, including the
  107. cat.h and cat.cpp functions, without any explicit instanciation. Say
  108. using the makefile in the <code>libigl</code> directory:
  109. </p>
  110. <pre><code>
  111. cd $LIBIGL
  112. make
  113. </code></pre>
  114. <p>
  115. Now if we try to compile a project and link against it we may get
  116. an error like:
  117. </p>
  118. <pre><code>
  119. Undefined symbols for architecture x86_64:
  120. "<span class=highlight>Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; igl::cat&lt;Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; &gt;(int, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&)</span>", referenced from:
  121. uniform_sample(Eigen::Matrix&lt;double, -1, -1, 0, -1, -1&gt; const&, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&, int, double, Eigen::Matrix&lt;double, -1, -1, 0, -1, -1&gt;&)in Skinning.o
  122. "Eigen::SparseMatrix&lt;double, 0, int&gt; igl::cat&lt;Eigen::SparseMatrix&lt;double, 0, int&gt; &gt;(int, Eigen::SparseMatrix&lt;double, 0, int&gt; const&, Eigen::SparseMatrix&lt;double, 0, int&gt; const&)", referenced from:
  123. covariance_scatter_matrix(Eigen::Matrix&lt;double, -1, -1, 0, -1, -1&gt; const&, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&, ArapEnergy, Eigen::SparseMatrix&lt;double, 0, int&gt;&)in arap_dof.o
  124. arap_rhs(Eigen::Matrix&lt;double, -1, -1, 0, -1, -1&gt; const&, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&, ArapEnergy, Eigen::SparseMatrix&lt;double, 0, int&gt;&)in arap_dof.o
  125. </code></pre>
  126. <p>
  127. This looks like a mess, but luckily we don't really need to read it
  128. all. Just copy the first highlighted part in quotes, then append it
  129. to the list of explicit templat specializations at the end of
  130. <code>cat.cpp</code> after the word
  131. <strong><code>template</code></strong> and followed by a semi-colon.
  132. Like this:
  133. </p>
  134. <pre><code>
  135. ...
  136. #ifndef IGL_HEADER_ONLY
  137. // Explicit template specialization
  138. <strong>template</strong> <span class=highlight>Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; igl::cat&lt;Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; &gt;(int, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&, Eigen::Matrix&lt;int, -1, -1, 0, -1, -1&gt; const&)</span>;
  139. #endif
  140. </code></pre>
  141. <p>
  142. Then you must recompile the IGL static library.
  143. </p>
  144. <pre><code>
  145. cd $LIBIGL
  146. make
  147. </code></pre>
  148. <p>
  149. And try to compile your project again, potentially repeating this
  150. process until no more symbols are undefined.
  151. </p>
  152. <div class=note>It may be useful to check that you code compiles with
  153. no errors first using the <a href=#header_library>headers-only
  154. version</a> to be sure that all errors are from missing template
  155. specializations.</div>
  156. <p>
  157. If you're using make then the following command will
  158. reveal each missing symbol on its own line:
  159. </p>
  160. <pre><code>
  161. make 2&gt;&1 | grep "referenced from" | sed -e "s/, referenced from.*//"
  162. </code></pre>
  163. <p>
  164. Alternatively you can use the <code>autoexplicit.sh</code> function
  165. which (for well organized .h/.cpp pairs in libigl) automatically
  166. create explicit instanciations from your compiler's error messages.
  167. Repeat this process until convergence:
  168. </p>
  169. <pre><code>
  170. cd /to/your/project
  171. make 2&gt;$LIBIGL/make.err
  172. cd $LIBIGL
  173. cat make.err | ./autoexplicit.sh
  174. make clean
  175. make
  176. </code></pre>
  177. <h3>Benefits of static library</h3>
  178. <ul>
  179. <li><strong>Faster compile time:</strong> Because the libigl library
  180. is already compiled, only the new code in ones project must be
  181. compiled and then linked to IGL. This means compile times are
  182. generally faster.</li>
  183. <li><strong>Debug <i>or</i> optimized:</strong> The IGL static
  184. library may be compiled in debug mode or optimized release mode
  185. regardless of whether one's project is being optimized or
  186. debugged.</li>
  187. </ul>
  188. <h3>Drawbacks of static library</h3>
  189. <ul>
  190. <li><strong>Hard to use templates:</strong> <a
  191. href="#explicit_specialization_of_templated_functions">Special
  192. care</a> (by the developers of the library) needs to be taken when
  193. exposing templated functions.</li>
  194. </ul>
  195. <h2 id="dependencies">Dependencies</h2>
  196. <p>
  197. By design the libigl library has very few external dependencies.
  198. </p>
  199. <h3>Mandatory dependencies</h3>
  200. <p>
  201. Besides the standard C++ library, there are a few dependencies,
  202. without which the main library will not compile or work properly.
  203. These are:
  204. </p>
  205. <ul>
  206. <li><a href=http://eigen.tuxfamily.org/>Eigen3</a></li>
  207. <li>OpenGL <span class=todo>implement IGL_NO_OPENGL compiler option</span></li>
  208. <li>GLUT <span class=todo>implement IGL_NO_GLUT compiler option</span></li>
  209. </ul>
  210. <h3>Optional dependencies</h3>
  211. <p>
  212. Certain functions and classes included the libigl library have
  213. external dependencies by construction (e.g. the matlab_interface
  214. routines are only useful when matlab is present anyway). These are
  215. <strong>never</strong> compiled by default into the static igl
  216. library <span class=todo>and are only exposed through compiler
  217. options</span>. These are:
  218. </p>
  219. <ul>
  220. <li>MATLAB</li>
  221. <li><a href="http://www.antisphere.com/Wiki/tools:anttweakbar">AntTweakBar</a></li>
  222. </ul>
  223. <h1>Converting matlab code to C++ using IGL and Eigen</h1>
  224. <p>
  225. Eigen's matrix API often makes it fairly to translate to and from
  226. matlab code (Eigen provides a <a
  227. href=http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt>
  228. translation table</a>). We have implemented a few additional
  229. matlab-esque functions to make the translation even easier. <a
  230. href="matlab-to-eigen.html">Our own translation table</a> shows a list
  231. of common matlab functions and their igl-eigen equivalents.
  232. </p>
  233. <p>See also: <a href=style_guidelines.html>style guidlines</a>, <a
  234. href=doc.html>auto-documentation</a>, <a
  235. href=file-formats/index.html>file formats</a></p>
  236. <h3>Including OpenGL</h3>
  237. <p>
  238. A standard include for the OpenGL headers should be placed in the .cpp file if possible. To ensure compilability on Mac OS X, Windows and Linux, use:
  239. <pre><code>
  240. #if __APPLE__
  241. # include &lt;OpenGL/gl.h&gt;
  242. #elif defined(_WIN32)
  243. # define NOMINMAX
  244. # include &lt;Windows.h&gt;
  245. # undef NOMINMAX
  246. # include &lt;GL/glew.h&gt;
  247. # include &lt;GL/gl.h&gt;
  248. #else
  249. # define GL_GLEXT_PROTOTYPES
  250. # include &lt;GL/gl.h&gt;
  251. # include &lt;GL/glext.h&gt;
  252. #endif
  253. </code></pre>
  254. </p>
  255. <p><span class=todo>This should be encapsulated in a single utility header</span></p>
  256. </div>
  257. </body>
  258. </html>