tutorial.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 IGL 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>igl_lib</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 feature</span>
  63. <div class=note>This practice is not recommended outside of debugging purposes.</div>
  64. <h3>Benefits of headers-only library</h3>
  65. <ul>
  66. <li><strong>Easy templates:</strong> When using the IGL library as a
  67. headers-only library no special care need be taken when using templated
  68. functions.</li>
  69. </ul>
  70. <h3>Drawbacks of headers-only library</h3>
  71. <ul>
  72. <li><strong>Inlining not guaranteed:</strong> Most compilers do not
  73. guarantee that functions will get inlined even if explicitly told to do
  74. so. Though we have not yet encountered this problem, it is always a
  75. risk.</li>
  76. <li><strong>Long compile, large binary:</strong>As a headers-only
  77. library we depend on the compiler to properly inline each function
  78. call. This means compile time is high and binary size can be quite
  79. large.</li>
  80. </ul>
  81. <h2 id=static_library>Statically linked library</h2>
  82. <h3 id=explicit_specialization_of_templated_functions>Explicit
  83. specialization of templated functions</h3>
  84. <p>
  85. Special care must be taken by the developers of each function and
  86. class in the IGL library that uses C++ templates. If this function is
  87. intended to be compiled into the statically linked igl library then
  88. function is only compiled for each <i>explicitly</i> specialized
  89. declaration. These should be added at the bottom of the corresponding
  90. .cpp file surrounded by a <code>#ifndef IGL_HEADER_ONLY</code>.
  91. </p>
  92. <p>
  93. Of course, a developer may not know ahead of time which
  94. specializations should be explicitly included in the igl static lib.
  95. One way to find out is to add one explicit specialization for each
  96. call in one's own project. This only ever needs to be done once for
  97. each template.
  98. </p>
  99. <p>
  100. The process is somewhat mechanical using a linker with reasonable error
  101. output.
  102. </p>
  103. <p>
  104. Supposed for example we have compiled the igl static lib, including the
  105. cat.h and cat.cpp functions, without any explicit instanciation. Say
  106. using the makefile in the <code>igl_lib</code> directory:
  107. </p>
  108. <pre><code>
  109. cd $IGL_LIB
  110. make
  111. </code></pre>
  112. <p>
  113. Now if we try to compile a project and link against it we may get
  114. an error like:
  115. </p>
  116. <pre><code>
  117. Undefined symbols for architecture x86_64:
  118. "<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:
  119. 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
  120. "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:
  121. 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
  122. 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
  123. </code></pre>
  124. <p>
  125. This looks like a mess, but luckily we don't really need to read it
  126. all. Just copy the first highlighted part in quotes, then append it
  127. to the list of explicit templat specializations at the end of
  128. <code>cat.cpp</code> after the word
  129. <strong><code>template</code></strong> and followed by a semi-colon.
  130. Like this:
  131. </p>
  132. <pre><code>
  133. ...
  134. #ifndef IGL_HEADER_ONLY
  135. // Explicit template specialization
  136. <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>;
  137. #endif
  138. </code></pre>
  139. <p>
  140. Then you must recompile the IGL static library.
  141. </p>
  142. <pre><code>
  143. cd $IGL_LIB
  144. make
  145. </code></pre>
  146. <p>
  147. And try to compile your project again, potentially repeating this
  148. process until no more symbols are undefined.
  149. </p>
  150. <div class=note>It may be useful to check that you code compiles with
  151. no errors first using the <a href=#header_library>headers-only
  152. version</a> to be sure that all errors are from missing template
  153. specializations.</div>
  154. <p>
  155. If you're using make then the following command will
  156. reveal each missing symbol on its own line:
  157. </p>
  158. <pre><code>
  159. make 2&gt;&1 | grep "referenced from" | sed -e "s/, referenced from.*//"
  160. </code></pre>
  161. <p>
  162. Alternatively you can use the <code>autoexplicit.sh</code> function
  163. which (for well organized .cpp files in the igl_lib) automatically
  164. create explicit instanciations from your compiler's error messages.
  165. Repeat this process until convergence:
  166. </p>
  167. <pre><code>
  168. cd /to/your/project
  169. make 2&gt;$IGL_LIB/make.err
  170. cd $IGL_LIB
  171. cat make.err | ./autoexplicit.sh
  172. make clean
  173. make
  174. </code></pre>
  175. <h3>Benefits of static library</h3>
  176. <ul>
  177. <li><strong>Faster compile time:</strong> Because the igl library is
  178. already compiled, only the new code in ones project must be compiled
  179. and then linked to IGL. This means compile times are generally
  180. faster.</li>
  181. <li><strong>Debug <i>or</i> optimized:</strong> The IGL static
  182. library may be compiled in debug mode or optimized release mode
  183. regardless of whether one's project is being optimized or
  184. debugged.</li>
  185. </ul>
  186. <h3>Drawbacks of static library</h3>
  187. <ul>
  188. <li><strong>Hard to use templates:</strong> <a
  189. href="#explicit_specialization_of_templated_functions">Special
  190. care</a> (by the developers of the library) needs to be taken when
  191. exposing templated functions.</li>
  192. </ul>
  193. <h2 id="dependencies">Dependencies</h2>
  194. <p>
  195. By design the IGL library has very few external dependencies.
  196. </p>
  197. <h3>Mandatory dependencies</h3>
  198. <p>
  199. Besides the standard C++ library, there are a few dependencies,
  200. without which the main library will not compile or work properly.
  201. These are:
  202. </p>
  203. <ul>
  204. <li><a href=http://eigen.tuxfamily.org/>Eigen3</a></li>
  205. <li>OpenGL <span class=todo>implement IGL_NO_OPENGL compiler option</span></li>
  206. <li>GLUT <span class=todo>implement IGL_NO_GLUT compiler option</span></li>
  207. </ul>
  208. <h3>Optional dependencies</h3>
  209. <p>
  210. Certain functions and classes included the IGL library have external
  211. dependencies by construction (e.g. the matlab_interface routines are
  212. only useful when matlab is present anyway). These are
  213. <strong>never</strong> compiled by default into the static igl
  214. library <span class=todo>and are only exposed through compiler
  215. options</span>. These are:
  216. </p>
  217. <ul>
  218. <li>MATLAB</li>
  219. <li><a href="http://www.antisphere.com/Wiki/tools:anttweakbar">AntTweakBar</a></li>
  220. </ul>
  221. <h1>Converting matlab code to C++ using IGL and Eigen</h1>
  222. <p>
  223. Eigen's matrix API often makes it fairly to translate to and from
  224. matlab code (Eigen provides a <a
  225. href=http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt>
  226. translation table</a>). We have implemented a few additional
  227. matlab-esque functions to make the translation even easier. <a
  228. href="matlab-to-eigen.html">Our own translation table</a> shows a list
  229. of common matlab functions and their igl-eigen equivalents.
  230. </p>
  231. <p>See also: <a href=style_guidelines.html>style guidlines</a>, <a href=doc.html>auto-documentation</a></p>
  232. </div>
  233. </body>
  234. </html>