style-guidelines.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>libigl</title>
  6. <meta name="author" content="Alec Jacobson and Daniele Panozzo and others"/>
  7. <link type="text/css" rel="stylesheet" href="tutorial/style.css"/>
  8. <script type='text/javascript' src='http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script> <link rel='stylesheet' href='http://yandex.st/highlightjs/7.3/styles/default.min.css'> <script src='http://yandex.st/highlightjs/7.3/highlight.min.js'></script> <script>hljs.initHighlightingOnLoad();</script>
  9. </head>
  10. <body>
  11. <h1 id="libiglstyleguidelines">Libigl Style Guidelines</h1>
  12. <p>Libigl is used and developed by many people. This document highlights some
  13. style guidelines for <em>developers</em> of the library, but also acts as
  14. best-practices for users.</p>
  15. <h2 id="filefunction">One function, one .h/.cpp pair </h2>
  16. <p>The structure of libigl is very flat and function-based. For every
  17. function/sub-routine, create a single .h and .cpp file. For example, if you have
  18. a function that determines connected components from a face list <code>F</code> you would
  19. create the header <code>connected_components.h</code> and <code>connected_components.cpp</code> and the only
  20. function defined should be <code>void connected_components(const ... F, ... C)</code>. If the
  21. implementation of <code>connected_components</code> requires a subroutine to compute an
  22. adjacency matrix then <em>create another pair</em> <code>adjacency_matrix.h</code> and
  23. <code>adjacency_matrix.cpp</code> with a single function <code>void adjacency_matrix(const ... F, ... A)</code>.</p>
  24. <h3 id="example">Example</h3>
  25. <p>Here is an example function that would be defined in
  26. <code>include/igl/example_fun.h</code> and implemented in <code>include/igl/example_fun.cpp</code>.</p>
  27. <h4 id="example_fun.h"><code>example_fun.h</code></h4>
  28. <pre><code class="cpp">// This file is part of libigl, a simple c++ geometry processing library.
  29. //
  30. // Copyright (C) 2015 [Your Name] [your email address]
  31. //
  32. // This Source Code Form is subject to the terms of the Mozilla Public License
  33. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  34. // obtain one at http://mozilla.org/MPL/2.0/
  35. #ifndef IGL_EXAMPLE_FUN_H
  36. #define IGL_EXAMPLE_FUN_H
  37. #include &quot;igl_inline.h&quot;
  38. namespace igl
  39. {
  40. // This is an example of a function, it takes a templated parameter and
  41. // shovels it into cout
  42. //
  43. // Input:
  44. // input some input of a Printable type
  45. // Returns true for the sake of returning something
  46. template &lt;typename Printable&gt;
  47. IGL_INLINE bool example_fun(const Printable &amp; input);
  48. }
  49. #ifndef IGL_STATIC_LIBRARY
  50. #include &quot;example_fun.cpp&quot;
  51. #endif
  52. #endif
  53. </code></pre>
  54. <h4 id="example_fun.cpp"><code>example_fun.cpp</code></h4>
  55. <pre><code>// This file is part of libigl, a simple c++ geometry processing library.
  56. //
  57. // Copyright (C) 2015 [Your Name] [your email address]
  58. //
  59. // This Source Code Form is subject to the terms of the Mozilla Public License
  60. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  61. // obtain one at http://mozilla.org/MPL/2.0/
  62. #include &quot;igl/example_fun.h&quot;
  63. #include &lt;iostream&gt;
  64. template &lt;typename Printable&gt;
  65. IGL_INLINE bool igl::example_fun(const Printable &amp; input)
  66. {
  67. using namespace std;
  68. cout&lt;&lt;&quot;example_fun: &quot;&lt;&lt;input&lt;&lt;endl;
  69. return true;
  70. }
  71. #ifdef IGL_STATIC_LIBRARY
  72. template bool igl::example_fun&lt;double&gt;(const double&amp; input);
  73. template bool igl::example_fun&lt;int&gt;(const int&amp; input);
  74. #endif
  75. </code></pre>
  76. <h3 id="avoidstatichelperfunctions">Avoid static &#8220;helper&#8221; functions</h3>
  77. <p>Strive to encapsulate sub-functions that could possibly be useful outside of
  78. the implementation of your current function. This might mean abstracting the
  79. interface a bit. If it doesn&#8217;t dramatically effect performance then create a
  80. new pair of .h/.cpp files with this sub-function.</p>
  81. <h4 id="lambdafunctions">Lambda functions</h4>
  82. <p>If encapsulation in a separate file is not possible or does not make sense,
  83. then avoid crowding the namespace by creating lambda functions within the
  84. function implementation.</p>
  85. <p>These lambda functions must still be documented with clear <a href="#headerdocumentation">input and output
  86. arguments</a>. Avoid using full capturing of all automatic
  87. variables: do not use <code>[&amp;]</code> or <code>[=]</code>. Rather specify each captured variable
  88. individually.</p>
  89. <h3 id="avoidhelperclasses">Avoid &#8220;helper&#8221; classes</h3>
  90. <p>Libigl is built around the high-performance paradigm of &#8220;struct of arrays&#8221;
  91. rather than &#8220;array of structs&#8221;. The way we achieve this is to avoid classes and
  92. pass &#8220;basic types&#8221; directly. The price we pay is long function interfaces, but
  93. this increases code reuse dramatically. A &#8220;basic type&#8221; in our context is a
  94. Eigen type, stl type, or basic C type.</p>
  95. <h2 id="headerdocumentation">Header Documentation</h2>
  96. <p>Each function prototype should be well documented in its corresponding .h
  97. header file. A typical documentation consists of four parts:</p>
  98. <pre><code class="cpp">// [A human readable description of what the function does.]
  99. //
  100. // Inputs:
  101. // [variable name of first (const) input] [dimensions and description of
  102. // this input variable]
  103. // [variable name of second (const) input] [dimensions and description of
  104. // this input variable]
  105. // ...
  106. // Outputs:
  107. // [variable name of first output ] [dimensions and description of this
  108. // output variable]
  109. // [variable name of second output ] [dimensions and description of this
  110. // output variable]
  111. // ...
  112. // Returns [description of return value]
  113. </code></pre>
  114. <h3 id="example">Example</h3>
  115. <p>For example the header <code>barycenter.h</code></p>
  116. <pre><code>// Computes the barycenter of every simplex
  117. //
  118. // Inputs:
  119. // V #V by dim matrix of vertex coordinates
  120. // F #F by simplex_size matrix of indices of simplex corners into V
  121. // Output:
  122. // BC #F by dim matrix of 3d vertices
  123. //
  124. </code></pre>
  125. <h2 id="constinputs">Const inputs</h2>
  126. <p>All input parameters should be demarcated <code>const</code>. If an input is also an
  127. output than consider exposing two parameters (one <code>const</code>) or be sure to list
  128. the variable under both <code>// Inputs:</code> and <code>// Outputs:</code> in the header comments.</p>
  129. <h2 id="referenceparameters">Reference parameters</h2>
  130. <p>All but simple types should be passed by reference (e.g. <code>Matrix &amp; mat</code>) rather
  131. than pointers (e.g. <code>Matrix * mat</code>) or value (e.g. <code>Matrix mat</code>).</p>
  132. <h2 id="returnsvsoutputparameters">Returns vs output parameters</h2>
  133. <p>All functions should be implemented with at least one overload that has a
  134. <code>void</code> or simple return type (e.g. <code>bool</code> on success/failure). With this
  135. implementation its then possible to write an overload that returns a single
  136. output. Please see <a href="#templatingwitheigen">Templating with Eigen</a>.</p>
  137. <p>For example:</p>
  138. <pre><code class="cpp">template &lt;typename Atype&gt;
  139. void adjacency_matrix(const ... &amp; F, Eigen::SparseMatrix&lt;AType&gt; &amp; A);
  140. template &lt;typename Atype&gt;
  141. Eigen::SparseMatrix&lt;Atype&gt; adjacency_matrix(const ... &amp; F);
  142. </code></pre>
  143. <h2 id="templatingwitheigen">Templating with Eigen</h2>
  144. <p>Functions taking Eigen dense matrices/arrays as inputs and outputs (but <strong>not</strong>
  145. return arguments), should template on top of <code>Eigen::MatrixBase</code>. <strong>Each
  146. parameter</strong> should be derived using its own template.</p>
  147. <p>For example,</p>
  148. <pre><code class="cpp">template &lt;typename DerivedV, typename DerivedF, typename DerivedBC&gt;
  149. void barycenter(
  150. const Eigen::MatrixBase&lt;DerivedV&gt; &amp; V,
  151. const Eigen::MatrixBase&lt;DerivedF&gt; &amp; F,
  152. const Eigen::MatrixBase&lt;DerivedBC&gt; &amp; BC);
  153. </code></pre>
  154. <p>The <code>Derived*</code> template encodes the scalar type (e.g. <code>double</code>, <code>int</code>), the
  155. number of rows and cols at compile time, and the data storage (Row-major vs.
  156. column-major).</p>
  157. <p>Returning Eigen types is discouraged. In cases where the size and scalar type
  158. are a fixed <strong>and matching</strong> function of an input <code>Derived*</code> template, then
  159. return that <code>Derived*</code> type. <strong>Do not</strong> return
  160. <code>Eigen::PlainObjectBase&lt;...&gt;</code> types. For example, this function scales fits a
  161. given set of points to the unit cube. The return is a new set of vertex
  162. positions so its type should <em>match</em> that of the input points:</p>
  163. <pre><code class="cpp">template &lt;typename DerivedV&gt;
  164. void DerivedV fit_to_unit_cube(const Eigen::PlainObjectBase&lt;DerivedV&gt; &amp; V);
  165. </code></pre>
  166. <p>To implement this function, it is <strong>required</strong> to implement a more generic
  167. output-argument version and call that. So a full implementation looks like:</p>
  168. <p>In <code>igl/fit_in_unit_cube.h</code>:</p>
  169. <pre><code class="cpp">template &lt;typename DerivedV, typename DerivedW&gt;
  170. void fit_to_unit_cube(
  171. const Eigen::MatrixBase&lt;DerivedV&gt; &amp; V,
  172. Eigen::PlainObjectBase&lt;DerivedW&gt; &amp; W);
  173. template &lt;typename DerivedV&gt;
  174. void DerivedV fit_to_unit_cube(const Eigen::PlainObjectBase&lt;DerivedV&gt; &amp; V);
  175. </code></pre>
  176. <p>In <code>igl/fit_in_unit_cube.cpp</code>:</p>
  177. <pre><code>template &lt;typename DerivedV, typename DerivedW&gt;
  178. void fit_to_unit_cube(
  179. const Eigen::MatrixBase&lt;DerivedV&gt; &amp; V,
  180. Eigen::PlainObjectBase&lt;DerivedW&gt; &amp; W)
  181. {
  182. W = (V.rowwise()-V.colwise().minCoeff()).array() /
  183. (V.maxCoeff()-V.minCoeff());
  184. }
  185. template &lt;typename DerivedV&gt;
  186. void DerivedV fit_to_unit_cube(const Eigen::MatrixBase&lt;DerivedV&gt; &amp; V)
  187. {
  188. DerivedV W;
  189. fit_to_unit_cube(V,W);
  190. return W;
  191. }
  192. </code></pre>
  193. <p>Notice that <code>W</code> is declared as a <code>DerivedV</code> type and <strong>not</strong>
  194. <code>Eigen::PlainObjectBase&lt;DerivedV&gt;</code> type.</p>
  195. <p><strong>Note:</strong> Not all functions are suitable for returning Eigen types. For example
  196. <code>igl::barycenter</code> above outputs a #F by dim list of barycenters. Returning a
  197. <code>DerivedV</code> type would be inappropriate since the number of rows in <code>DerivedV</code>
  198. will be #V and may not match the number of rows in <code>DerivedF</code> (#F).</p>
  199. <h2 id="functionnamingconventions">Function naming conventions</h2>
  200. <p>Functions (and <a href="#filefunction">thus also files</a>) should have simple,
  201. descriptive names using lowercase letters and underscores between words. Avoid
  202. unnecessary prefaces. For example, instead of <code>compute_adjacency_matrix</code>,
  203. <code>construct_adjacency_matrix</code>, <code>extract_adjacency_matrix</code>,
  204. <code>get_adjacency_matrix</code>, or <code>set_adjacency_matrix</code> just call the function
  205. <code>adjacency_matrix</code>.</p>
  206. <h2 id="variablenamingconventions">Variable naming conventions</h2>
  207. <p>Libigl prefers short (even single character) variable names <em>with heavy
  208. documentation</em> in the comments in the header file or above the declaration of
  209. the function. When possible use <code>V</code> to mean a list of vertex positions and <code>F</code>
  210. to mean a list of faces/triangles.</p>
  211. <h2 id="classnamingconventions">Class naming conventions</h2>
  212. <p>Classes should be avoided. When naming a class use CamelCase (e.g.
  213. SortableRow.h).</p>
  214. <h2 id="enumnamingconversion">Enum naming conversion</h2>
  215. <p>Enums types should be placed in the appropriate <code>igl::</code> namespace and should be
  216. named in CamelCase (e.g. <code>igl::SolverStatus</code>) and instances should be named in
  217. ALL_CAPS with underscores between words and prefaced with the name of the enum.
  218. For example:</p>
  219. <pre><code class="cpp">namespace igl
  220. {
  221. enum SolverStatus
  222. {
  223. // Good
  224. SOLVER_STATUS_CONVERGED = 0,
  225. // OK
  226. SOLVER_STATUS_MAX_ITER = 1,
  227. // Bad
  228. SOLVER_STATUS_ERROR = 2,
  229. NUM_SOLVER_STATUSES = 3,
  230. };
  231. };
  232. </code></pre>
  233. <h3 id="exceptionforfileio">Exception for file IO</h3>
  234. <p>For legacy reasons, file reading and writing functions use a different naming
  235. convention. A functions reading a <code>.xyz</code> file should be named <code>readXYZ</code> and a
  236. function writing <code>.xyz</code> files should be names <code>writeXYZ</code>.</p>
  237. <h2 id="usingnamespace...inglobalscope"><code>using namespace ...</code> in global scope</h2>
  238. <p>Writing <code>using namespace std;</code>, <code>using namespace Eigen;</code> etc. outside of a
  239. global scope is strictly forbidden. Place these lines at the top of each
  240. function instead.</p>
  241. <h2 id="namespacesandexternaldependencies">Namespaces and external dependencies</h2>
  242. <p>Functions in the main library (directly in <code>include/igl</code>) should only depend on
  243. Eigen and stl. These functions should have the <code>igl::</code> namespace.</p>
  244. <p>Functions with other dependencies should be placed into
  245. appropriate sub-directories (e.g. if <code>myfunction</code> depends on tetgen then create
  246. <code>igl/copyleft/tetgen/myfunction.h</code> and <code>igl/copyleft/tetgen/myfunction.cpp</code> and give the function
  247. the namespace <code>igl::copyleft::tetgen::myfunction</code>.</p>
  248. <h3 id="copyleftsubdirectorynamespace">copyleft subdirectory/namespace</h3>
  249. <p>Dependencies that require users of libigl to release their projects open source
  250. (e.g. GPL) are considered aggressively &#8220;copyleft&#8221; and should be placed in the
  251. <code>include/igl/copyleft/</code> sub-directory and <code>igl::copyleft::</code> namespace.</p>
  252. <h2 id="assertions">Assertions</h2>
  253. <p>Be generous with assertions and always identify the assertion with strings:</p>
  254. <pre><code class="cpp">assert(m &lt; n &amp;&amp; &quot;m must be less than n&quot;);
  255. </code></pre>
  256. <h2 id="ifndefincludeguard">ifndef include guard</h2>
  257. <p>Every header file should be wrapped in an <code>#ifndef</code> compiler directive. The
  258. name of the guard should be in direct correspondence with the path of the .h
  259. file. For example, <code>include/igl/copyleft/tetgen/tetrahedralize.h</code> should be</p>
  260. <pre><code class="cpp">#ifndef IGL_COPYLEFT_TETGEN_TETRAHEDRALIZE_H
  261. #define IGL_COPYLEFT_TETGEN_TETRAHEDRALIZE_H
  262. ...
  263. #endif
  264. </code></pre>
  265. <h2 id="spacesvs.tabsindentation">Spaces vs. tabs indentation</h2>
  266. <p>Do not use tabs. Use 2 spaces for each indentation level.</p>
  267. <h2 id="maxlinelength">Max line length</h2>
  268. <p>Limit lines to 80 characters. Break up long lines into many operations (this
  269. also helps performance).</p>
  270. <h2 id="includeorder">Include order</h2>
  271. <p><code>#include</code> directives at the top of a .h or .cpp file should be sorted
  272. according to a simple principle: place headers of files most likely to be
  273. edited by you first. This means for
  274. <code>include/igl/copyleft/tetgen/tetrahedralize.cpp</code> you might see</p>
  275. <pre><code class="cpp">// [Includes of headers in this directory]
  276. #include &quot;tetrahedralize.h&quot;
  277. #include &quot;mesh_to_tetgenio.h&quot;
  278. #include &quot;tetgenio_to_tetmesh.h&quot;
  279. // [Includes of headers in this project]
  280. #include &quot;../../matrix_to_list.h&quot;
  281. #include &quot;../../list_to_matrix.h&quot;
  282. #include &quot;../../boundary_facets.h&quot;
  283. // [Includes of headers of related projects]
  284. #include &lt;Eigen/Core&gt;
  285. // [Includes of headers of standard libraries]
  286. #include &lt;cassert&gt;
  287. #include &lt;iostream&gt;
  288. </code></pre>
  289. <h2 id="placementofincludes">Placement of includes</h2>
  290. <p>Whenever possible <code>#include</code> directives should be placed in the <code>.cpp</code>
  291. implementation file rather than the <code>.h</code> header file.</p>
  292. <h2 id="warnings">Warnings</h2>
  293. <p>Code should compile without firing any warnings.</p>
  294. <h3 id="anexception">An Exception</h3>
  295. <p>The only exception is for the use of the deprecated
  296. <code>Eigen::DynamicSparseMatrix</code> in core sub-routines (e.g. <code>igl::cat</code>). This class
  297. is still supported and faster than the standard, non-deprecated Eigen
  298. implementation so we&#8217;re keeping it as long as possible and profitable.</p>
  299. </body>
  300. </html>