tutorial.html 11 KB

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