tutorial.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #ifndef IGL_HEADER_ONLY
  115. # define IGL_HEADER_ONLY
  116. # define IGL_HEADER_ONLY_WAS_NOT_DEFINED
  117. #endif
  118. #include &lt;igl_function_to_inline.h&gt;
  119. #ifdef IGL_HEADER_ONLY_WAS_NOT_DEFINED
  120. # undef IGL_HEADER_ONLY
  121. #endif
  122. #include &lt;yet_another_igl_function.h&gt;
  123. ...
  124. </code></pre>
  125. <span class=todo>example <code>examples/XXX</code> also highlights this feature</span>
  126. <div class=note>This practice is not recommended outside of debugging purposes.</div>
  127. <h3>Benefits of headers-only library</h3>
  128. <ul>
  129. <li><strong>Easy templates:</strong> When using the IGL library as a
  130. headers-only library no special care need be taken when using templated
  131. functions.</li>
  132. </ul>
  133. <h3>Drawbacks of headers-only library</h3>
  134. <ul>
  135. <li><strong>Inlining not guaranteed:</strong> Most compilers do not
  136. guarantee that functions will get inlined even if explicitly told to do
  137. so. Though we have not yet encountered this problem, it is always a
  138. risk.</li>
  139. <li><strong>Long compile, large binary:</strong>As a headers-only
  140. library we depend on the compiler to properly inline each function
  141. call. This means compile time is high and binary size can be quite
  142. large.</li>
  143. </ul>
  144. <h2 id=static_library>Statically linked library</h2>
  145. <h3 id=explicit_specialization_of_templated_functions>Explicit
  146. specialization of templated functions</h3>
  147. <p>
  148. Special care must be taken by the developers of each function and
  149. class in the IGL library that uses C++ templates. If this function is
  150. intended to be compiled into the statically linked igl library then
  151. function is only compiled for each <i>explicitly</i> specialized
  152. declaration. These should be added at the bottom of the corresponding
  153. .cpp file surrounded by a <code>#ifndef IGL_HEADER_ONLY</code>.
  154. </p>
  155. <p>
  156. Of course, a developer may not know ahead of time which
  157. specializations should be explicitly included in the igl static lib.
  158. One way to find out is to add one explicit specialization for each
  159. call in one's own project. This only ever needs to be done once for
  160. each template.
  161. </p>
  162. <p>
  163. The process is somewhat mechanical using a linker with reasonable error
  164. output.
  165. </p>
  166. <p>
  167. Supposed for example we have compiled the igl static lib, including the
  168. cat.h and cat.cpp functions, without any explicit instanciation. Say
  169. using the makefile in the <code>igl_lib</code> directory:
  170. </p>
  171. <pre><code>
  172. cd $IGL_LIB
  173. make
  174. </code></pre>
  175. <p>
  176. Now if we try to compile a project and link against it we may get
  177. an error like:
  178. </p>
  179. <pre><code>
  180. Undefined symbols for architecture x86_64:
  181. "<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:
  182. 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
  183. "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:
  184. 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
  185. 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
  186. </code></pre>
  187. <p>
  188. This looks like a mess, but luckily we don't really need to read it
  189. all. Just copy the first highlighted part in quotes, then append it
  190. to the list of explicit templat specializations at the end of
  191. <code>cat.cpp</code> after the word
  192. <strong><code>template</code></strong> and followed by a semi-colon.
  193. Like this:
  194. </p>
  195. <pre><code>
  196. ...
  197. #ifndef IGL_HEADER_ONLY
  198. // Explicit template specialization
  199. <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>;
  200. #endif
  201. </code></pre>
  202. <p>
  203. Then you must recompile the IGL static library.
  204. </p>
  205. <pre><code>
  206. cd $IGL_LIB
  207. make
  208. </code></pre>
  209. <p>
  210. And try to compile your project again, potentially repeating this
  211. process until no more symbols are undefined.
  212. </p>
  213. <div class=note>It may be useful to check that you code compiles with
  214. no errors first using the <a href=#header_library>headers-only
  215. version</a> to be sure that all errors are from missing template
  216. specializations.</div>
  217. <p>
  218. If you're using make then the following command will
  219. reveal each missing symbol on its own line:
  220. </p>
  221. <pre><code>
  222. make 2&gt;&1 | grep "referenced from" | sed -e "s/, referenced from.*//"
  223. </code></pre>
  224. <p>
  225. Alternatively you can use the <code>autoexplicit.sh</code> function
  226. which (for well organized .cpp files in the igl_lib) automatically
  227. create explicit instanciations from your compiler's error messages.
  228. Repeat this process until convergence:
  229. </p>
  230. <pre><code>
  231. cd /to/your/project
  232. make 2&gt;$IGL_LIB/make.err
  233. cd $IGL_LIB
  234. cat make.err | ./autoexplicit.sh
  235. make clean
  236. make
  237. </code></pre>
  238. <h3>Benefits of static library</h3>
  239. <ul>
  240. <li><strong>Faster compile time:</strong> Because the igl library is
  241. already compiled, only the new code in ones project must be compiled
  242. and then linked to IGL. This means compile times are generally
  243. faster.</li>
  244. <li><strong>Debug <i>or</i> optimized:</strong> The IGL static
  245. library may be compiled in debug mode or optimized release mode
  246. regardless of whether one's project is being optimized or
  247. debugged.</li>
  248. </ul>
  249. <h3>Drawbacks of static library</h3>
  250. <ul>
  251. <li><strong>Hard to use templates:</strong> <a
  252. href="#explicit_specialization_of_templated_functions">Special
  253. care</a> (by the developers of the library) needs to be taken when
  254. exposing templated functions.</li>
  255. </ul>
  256. <h2 id="dependencies">Dependencies</h2>
  257. <p>
  258. By design the IGL library has very few external dependencies.
  259. </p>
  260. <h3>Mandatory dependencies</h3>
  261. <p>
  262. Besides the standard C++ library, there are a few dependencies,
  263. without which the main library will not compile or work properly.
  264. These are:
  265. </p>
  266. <ul>
  267. <li><a href=http://eigen.tuxfamily.org/>Eigen3</a></li>
  268. <li>OpenGL <span class=todo>implement IGL_NO_OPENGL compiler option</span></li>
  269. <li>GLUT <span class=todo>implement IGL_NO_GLUT compiler option</span></li>
  270. </ul>
  271. <h3>Optional dependencies</h3>
  272. <p>
  273. Certain functions and classes included the IGL library have external
  274. dependencies by construction (e.g. the matlab_interface routines are
  275. only useful when matlab is present anyway). These are
  276. <strong>never</strong> compiled by default into the static igl
  277. library <span class=todo>and are only exposed through compiler
  278. options</span>. These are:
  279. </p>
  280. <ul>
  281. <li>MATLAB</li>
  282. <li><a href="http://www.antisphere.com/Wiki/tools:anttweakbar">AntTweakBar</a></li>
  283. </ul>
  284. <h1>Converting matlab code to C++ using IGL and Eigen</h1>
  285. <p>
  286. Eigen's matrix API often makes it fairly to translate to and from
  287. matlab code (Eigen provides a <a
  288. href=http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt>
  289. translation table</a>). We have implemented a few additional
  290. matlab-esque functions to make the translation even easier. <a
  291. href="matlab-to-eigen.html">Our own translation table</a> shows a list
  292. of common matlab functions and their igl-eigen equivalents.
  293. </p>
  294. </body>
  295. </html>