tutorial.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <html>
  2. <head>
  3. <title>libigl developer tutorial</title>
  4. <link href="./style.css" rel="stylesheet" type="text/css">
  5. </head>
  6. <body>
  7. <div id=fixed_sidebar>
  8. <ul>
  9. <li>
  10. <a href=#header_library>Headers (.h) only library</a>
  11. <ul>
  12. <li><a href=#widget>IGL_HEADER_ONLY widget</a></li>
  13. <li><a href=#header_benefits_drawbacks>Benefits and drawbacks</a></li>
  14. </ul>
  15. </li>
  16. <li>
  17. <a href=#static_library>Statically linked library (libigl.a)</a>
  18. <ul>
  19. <li><a href=#static_benefits_drawbacks>Benefits and drawbacks</a></li>
  20. </ul>
  21. </li>
  22. <li>
  23. <a href=#compress>Compressed igl.h/igl.cpp pair</a>
  24. <ul>
  25. <li><a href=#compress_benefits_drawbacks>Benefits and drawbacks</a></li>
  26. </ul>
  27. </li>
  28. <li>
  29. <a href=#dependencies>Dependencies</a>
  30. </li>
  31. <li><a href=#matlab>Conversion from matlab</a>
  32. <ul>
  33. </div>
  34. <div id=container>
  35. <div class=article_outer>
  36. <div class=article_inner>
  37. <a href=.><img src=libigl-logo.jpg alt="igl logo" class=center></a>
  38. <h1>Using the libigl library</h1>
  39. <p>
  40. The <a href=.>libigl</a> library is a collection of useful/reusable/sharable C++ functions
  41. with very few external <a href="#dependencies">dependencies</a>. The
  42. library may be used as a <a href="#header_library">"headers only"
  43. library</a>, a <a href=#static_library>statically linked library</a>, or as a compressed <a href="#compress">.h/.cpp pair</a>.
  44. </p>
  45. <p>
  46. This duality between statically compiled and header-only is illustrated
  47. in the example <code>examples/example_fun</code>. When built, this example
  48. compiles two binaries, one using the <code>example_fun</code> routine of
  49. the igl library, either as a headers-only library or linking against the
  50. igl static library.
  51. </p>
  52. <h2 id=header_library>Headers (.h) only library</h2>
  53. <p>
  54. All classes and functions in the libigl library are written in a way in
  55. which the entire library may be compiled <i>just-in-time</i>,
  56. effectively behaiving as if it were a "headers only" library (like e.g.
  57. Eigen). This is achieved by careful organization of each pair of .h and
  58. .cpp files. To take advantage of this one must only include the path to
  59. <code>libigl</code> directory in one's project's include path and
  60. define the preprocessor macro <code>IGL_HEADER_ONLY</code>.
  61. </p>
  62. <p>
  63. Defining <code>IGL_HEADER_ONLY</code> may be done at the project level,
  64. prescribing that all included igl headers be treated as code that
  65. should be inlined. Consequently all templated functions will be derived
  66. at compile time if need be.
  67. </p>
  68. <p id=widget>
  69. One may also define <code>IGL_HEADER_ONLY</code> not only on a per-file
  70. basis, but a <i>per-include</i> basis. For example it may be useful for a
  71. project to use the static library for most functionality from IGL, but then
  72. include a certain IGL function as an inlined function. This may be achieved
  73. by surrounding the relevant include with a define and undefine of the
  74. <code>IGL_HEADER_ONLY</code> macro. Here's an example of the little
  75. widget:
  76. </p>
  77. <pre><code>
  78. ...
  79. #include &lt;igl/some_other_igl_function.h&gt;
  80. #ifndef IGL_HEADER_ONLY
  81. # define IGL_HEADER_ONLY
  82. # define IGL_HEADER_ONLY_WAS_NOT_DEFINED
  83. #endif
  84. #include &lt;igl/igl_function_to_inline.h&gt;
  85. #ifdef IGL_HEADER_ONLY_WAS_NOT_DEFINED
  86. # undef IGL_HEADER_ONLY
  87. #endif
  88. #include &lt;igl/yet_another_igl_function.h&gt;
  89. ...
  90. </code></pre>
  91. <span class=todo>example <code>examples/XXX</code> also highlights this
  92. feature</span>
  93. <div class=note>This practice is not recommended outside of debugging
  94. purposes.</div>
  95. <h3 id=header_benefits_drawbacks>Benefits of headers-only library</h3>
  96. <ul>
  97. <li><strong>Easy templates:</strong> When using the libigl library as a
  98. headers-only library no special care need be taken when using templated
  99. functions.</li>
  100. </ul>
  101. <h3>Drawbacks of headers-only library</h3>
  102. <ul>
  103. <li><strong>Inlining not guaranteed:</strong> Most compilers do not
  104. guarantee that functions will get inlined even if explicitly told to do
  105. so. Though we have not yet encountered this problem, it is always a
  106. risk.</li>
  107. <li><strong>Long compile, large binary:</strong>As a headers-only
  108. library we depend on the compiler to properly inline each function
  109. call. This means compile time is high and binary size can be quite
  110. large.</li>
  111. </ul>
  112. <h2 id=static_library>Statically linked library</h2>
  113. <h3 id=explicit_specialization_of_templated_functions>Explicit
  114. specialization of templated functions</h3>
  115. <p>
  116. Special care must be taken by the developers of each function and
  117. class in the libigl library that uses C++ templates. If this function
  118. is intended to be compiled into the statically linked libigl library
  119. then function is only compiled for each <i>explicitly</i> specialized
  120. declaration. These should be added at the bottom of the corresponding
  121. .cpp file surrounded by a <code>#ifndef IGL_HEADER_ONLY</code>.
  122. </p>
  123. <p>
  124. Of course, a developer may not know ahead of time which
  125. specializations should be explicitly included in the igl static lib.
  126. One way to find out is to add one explicit specialization for each
  127. call in one's own project. This only ever needs to be done once for
  128. each template.
  129. </p>
  130. <p>
  131. The process is somewhat mechanical using a linker with reasonable error
  132. output.
  133. </p>
  134. <p>
  135. Supposed for example we have compiled the igl static lib, including the
  136. cat.h and cat.cpp functions, without any explicit instanciation. Say
  137. using the makefile in the <code>libigl</code> directory:
  138. </p>
  139. <pre><code>
  140. cd $LIBIGL
  141. make
  142. </code></pre>
  143. <p>
  144. Now if we try to compile a project and link against it we may get
  145. an error like:
  146. </p>
  147. <pre><code>
  148. Undefined symbols for architecture x86_64:
  149. "<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:
  150. 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
  151. "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:
  152. 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
  153. 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
  154. </code></pre>
  155. <p>
  156. This looks like a mess, but luckily we don't really need to read it
  157. all. Just copy the first highlighted part in quotes, then append it
  158. to the list of explicit templat specializations at the end of
  159. <code>cat.cpp</code> after the word
  160. <strong><code>template</code></strong> and followed by a semi-colon.
  161. Like this:
  162. </p>
  163. <pre><code>
  164. ...
  165. #ifndef IGL_HEADER_ONLY
  166. // Explicit template specialization
  167. <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>;
  168. #endif
  169. </code></pre>
  170. <p>
  171. Then you must recompile the IGL static library.
  172. </p>
  173. <pre><code>
  174. cd $LIBIGL
  175. make
  176. </code></pre>
  177. <p>
  178. And try to compile your project again, potentially repeating this
  179. process until no more symbols are undefined.
  180. </p>
  181. <div class=note>It may be useful to check that you code compiles with
  182. no errors first using the <a href=#header_library>headers-only
  183. version</a> to be sure that all errors are from missing template
  184. specializations.</div>
  185. <p>
  186. If you're using make then the following command will
  187. reveal each missing symbol on its own line:
  188. </p>
  189. <pre><code>
  190. make 2&gt;&1 | grep "referenced from" | sed -e "s/, referenced from.*//"
  191. </code></pre>
  192. <p>
  193. Alternatively you can use the <code>autoexplicit.sh</code> function
  194. which (for well organized .h/.cpp pairs in libigl) automatically
  195. create explicit instanciations from your compiler's error messages.
  196. Repeat this process until convergence:
  197. </p>
  198. <pre><code>
  199. cd /to/your/project
  200. make 2&gt;$LIBIGL/make.err
  201. cd $LIBIGL
  202. cat make.err | ./autoexplicit.sh
  203. make clean
  204. make
  205. </code></pre>
  206. <h3 id=static_benefits_drawbacks>Benefits of static library</h3>
  207. <ul>
  208. <li><strong>Faster compile time:</strong> Because the libigl library
  209. is already compiled, only the new code in ones project must be
  210. compiled and then linked to IGL. This means compile times are
  211. generally faster.</li>
  212. <li><strong>Debug <i>or</i> optimized:</strong> The IGL static
  213. library may be compiled in debug mode or optimized release mode
  214. regardless of whether one's project is being optimized or
  215. debugged.</li>
  216. </ul>
  217. <h3>Drawbacks of static library</h3>
  218. <ul>
  219. <li><strong>Hard to use templates:</strong> <a
  220. href="#explicit_specialization_of_templated_functions">Special
  221. care</a> (by the developers of the library) needs to be taken when
  222. exposing templated functions.</li>
  223. </ul>
  224. <h2 id=compress>Compressed .h/.cpp pair</h2>
  225. <p>Calling the script:</p>
  226. <pre><code>scripts/compress.sh igl.h igl.cpp</code></pre>
  227. <p>will create a single header
  228. <code>igl.h</code> and a single cpp file <code>igl.cpp</code>.
  229. <p>Alternatively, you can also compress everything into a single header file (analagous to IGL_HEADER_ONLY):</p>
  230. <pre><code>scripts/compress.sh igl.h</code></pre>
  231. <h3 id=compress_benefits_drawbacks>Benefits of compressed .h/.cpp pair</h3>
  232. <ul>
  233. <li><strong>Easy incorporation:</strong> This can be easily incorporated
  234. into external projects.</li>
  235. </ul>
  236. <h3>Drawbacks of compressed .h/.cpp pair</h3>
  237. <ul>
  238. <li><strong>Hard to debug/edit:</strong> The compressed files are
  239. automatically generated. They're huge and should not be edited. Thus
  240. debugging and editting are near impossible.</li>
  241. <li><strong>Compounded dependencies:</strong>
  242. An immediate disadvantage of this
  243. seems to be that even to use a single function (e.g.
  244. <code>cotmatrix</code>), compiling and linking against
  245. <code>igl.cpp</code> will require linking to all of <code>libigl</code>'s
  246. dependencies (<code>OpenGL</code>, <code>GLUT</code>,
  247. <code>AntTweakBar</code>, <code>BLAS</code>). However, because all
  248. depencies other than Eigen should be encapsulated between
  249. <code>#ifndef</code> guards (e.g. <code>#ifndef IGL_NO_OPENGL</code>, it
  250. is possible to ignore certain functions that have such dependencies.</li>
  251. <li><strong>Long compile:</strong> Compiling <code>igl.cpp</code> takes a long time and isn't easily parallelized (no <code>make -j12</code> equivalent).</li>
  252. </ul>
  253. <p>Here's a tiny test example using <code>igl.h</code> and <code>igl.cpp</code>. Save the following in <code>test.cpp</code>:</p>
  254. <pre><code>
  255. #include &lt;igl.h&gt;
  256. #include &lt;Eigen/Core&gt;
  257. int main(int argc, char * argv[])
  258. {
  259. Eigen::MatrixXd V;
  260. Eigen::MatrixXi F;
  261. return (argc&gt;=2 &amp;&amp; igl::read(argv[1],V,F)?0:1);
  262. }
  263. </code></pre>
  264. <p>Then compile <code>igl.cpp</code> with:
  265. <pre><code>
  266. g++ -o igl.o -c igl.cpp -I/opt/local/include/eigen3 -DIGL_NO_OPENGL -DIGL_NO_ANTTWEAKBAR
  267. </code></pre>
  268. <p>Notice that we're using <code>-DIGL_NO_OPENGL -DIGL_NO_ANTTWEAKBAR</code> to disable any libigl dependencies on OpenGL and AntTweakBar.</p>
  269. <p>Now compile <code>test.cpp</code> with:
  270. <pre><code>
  271. g++ -g -I/opt/local/include/eigen3/ -I/usr/local/igl/libigl/ -L/usr/local/igl/libigl/ -ligl -DIGL_NO_OPENGL -DIGL_NO_ANTTWEAKBAR -o test
  272. </code></pre>
  273. <p>Try running it with:</p>
  274. <pre><code>
  275. ./test path/to/mesh.obj
  276. </code></pre>
  277. <h2 id="dependencies">Dependencies</h2>
  278. <p>
  279. By design the libigl library has very few external dependencies.
  280. </p>
  281. <h3>Mandatory dependencies</h3>
  282. <p> <a href=http://eigen.tuxfamily.org/>Eigen3</a> and the Standard
  283. Template Library (STL) are the only truly mandatory dependencies.
  284. Without them libigl will not compile or work properly.</p>
  285. <p> OpenGL is an <i>assumed</i> dependency, but is in fact also
  286. optional. All OpenGL-dependent functions may be disabled by defining
  287. the <code>IGL_NO_OPENGL</code> preprocessor macro.</p>
  288. </p>
  289. <p> Likewise,
  290. <a href="http://www.antisphere.com/Wiki/tools:anttweakbar">AntTweakBar</a>
  291. is an
  292. <i>assumed</i> dependency, similarly diabled by defining
  293. the <code>IGL_NO_ANTTWEAKBAR</code> preprocessor macro.</p>
  294. <p><span class=todo>Each IGL_NO_XXX should just be replaced by a libiglXXX.a extra</span></p>
  295. <h3>Optional dependencies</h3>
  296. <p>
  297. Certain <i>extra</i> functions and classes included the libigl
  298. library have external dependencies by construction (e.g. the
  299. matlab_interface routines are only useful when matlab is present
  300. anyway). These are <strong>never</strong> compiled by default into
  301. the static igl library, rather they are compiled as "extras" (e.g.
  302. libiglmatlab.a contains the MATLAB-dependent functions).
  303. </p>
  304. <p>Currently these include:<p>
  305. <table>
  306. <tr class=header><th>Extra</th><th>Dependency</th></tr>
  307. <tr class=d0><td>libiglmatlab.a</td><td>MATLAB</td></tr>
  308. <tr class=d1><td>libiglmosek.a</td><td>Mosek</td></tr>
  309. <tr class=d0><td>libigltetgen.a</td><td>TetGen</td></tr>
  310. <tr class=d1><td>libiglxml.a</td><td>TinyXml2</td></tr>
  311. <tr class=d0><td>libiglpng.a</td><td>LibPNG</td></tr>
  312. <tr class=d1><td>libiglembree.a</td><td>Embree</td></tr>
  313. </table>
  314. <p>Some of our examples (<code>libigl/examples</code>) also depend on GLUT.</p>
  315. <h2 id=matlab>Converting matlab code to C++ using IGL and Eigen</h2>
  316. <p>
  317. Eigen's matrix API often makes it fairly to translate to and from
  318. matlab code (Eigen provides a <a
  319. href=http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt>
  320. translation table</a>). We have implemented a few additional
  321. matlab-esque functions to make the translation even easier. <a
  322. href="matlab-to-eigen.html">Our own translation table</a> shows a list
  323. of common matlab functions and their igl-eigen equivalents.
  324. </p>
  325. <h3>Including OpenGL</h3>
  326. <p>Just include the convenience header, which takes care of system dependent paths, <code>glew</code>, etc.:</p>
  327. <pre><code>
  328. #include "OpenGL_convenience.h"
  329. </code></pre>
  330. <!--<p>Likewise for GLUT:</p>
  331. <pre><code>
  332. #include "GLUT_convenience.h"
  333. </code></pre>-->
  334. <del>
  335. <p>
  336. 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:
  337. <pre><code>
  338. #if __APPLE__
  339. # include &lt;OpenGL/gl.h&gt;
  340. #elif defined(_WIN32)
  341. # define NOMINMAX
  342. # include &lt;Windows.h&gt;
  343. # undef NOMINMAX
  344. # include &lt;GL/glew.h&gt;
  345. # include &lt;GL/gl.h&gt;
  346. #else
  347. # define GL_GLEXT_PROTOTYPES
  348. # include &lt;GL/gl.h&gt;
  349. # include &lt;GL/glext.h&gt;
  350. #endif
  351. </code></pre>
  352. </p>
  353. </del>
  354. <h3>Including headers to other igl functions</h3>
  355. <p>
  356. Source files in the main igl directory should include other libigl headers using the name of the file in quotation marks:
  357. </p>
  358. <pre><code>
  359. #include "other_function.h"
  360. </code></pre>
  361. <p>
  362. Source files in your project and in libigl <i>extras</i> should include libigl headers using the igl directory and name file in angle brackets:
  363. </p>
  364. <pre><code>
  365. #include &lt;igl/some_function.h&gt;
  366. </code></pre>
  367. <p>
  368. libigl headers of extras can then be included using:
  369. </p>
  370. <pre><code>
  371. #include &lt;igl/extra/some_extra_function.h&gt;
  372. </code></pre>
  373. <hr>
  374. <p>See also: <a href=style_guidelines.html>style guidlines</a>, <a
  375. href=doc.html>auto-documentation</a>, <a
  376. href=file-formats/index.html>file formats</a></p>
  377. </div>
  378. </div>
  379. </div>
  380. </body>
  381. </html>