tutorial.html 11 KB

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