style-guidelines.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!DOCTYPE html>
  2. <html>
  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>
  9. <link rel='stylesheet' href='http://yandex.st/highlightjs/7.3/styles/default.min.css'>
  10. <script src='http://yandex.st/highlightjs/7.3/highlight.min.js'></script>
  11. <script>hljs.initHighlightingOnLoad();</script>
  12. </head>
  13. <body>
  14. <h1 id="libiglstyleguidelines">Libigl Style Guidelines</h1>
  15. <p>Libigl is used and developed by many people. This document highlights some
  16. style guidelines for <em>developers</em> of the library, but also acts as
  17. best-practices for users.</p>
  18. <h2 id="filefunction">One function, one .h/.cpp pair</h2>
  19. <p>The structure of libigl is very flat and function-based. For every
  20. function/sub-routine, create a single .h and .cpp file. For example, if you have
  21. a function that determines connected components from a face list <code>F</code> you would
  22. create the header <code>connected_components.h</code> and <code>connected_components.cpp</code> and the only
  23. function defined should be <code>void connected_components(const ... F, ... C)</code>. If the
  24. implementation of <code>connected_components</code> requires a subroutine to compute an
  25. adjacency matrix then <em>create another pair</em> <code>adjacency_matrix.h</code> and
  26. <code>adjacency_matrix.cpp</code> with a single function <code>void adjacency_matrix(const ... F, ... A)</code>.</p>
  27. <h3 id="example">Example</h3>
  28. <p>Here is an example function that would be defined in
  29. <code>include/igl/example_fun.h</code> and implemented in <code>include/igl/example_fun.cpp</code>.</p>
  30. <h4 id="example_fun.h"><code>example_fun.h</code></h4>
  31. <pre><code class="cpp">// This file is part of libigl, a simple c++ geometry processing library.
  32. //
  33. // Copyright (C) 2015 [Your Name] [your email address]
  34. //
  35. // This Source Code Form is subject to the terms of the Mozilla Public License
  36. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  37. // obtain one at http://mozilla.org/MPL/2.0/
  38. #ifndef IGL_EXAMPLE_FUN_H
  39. #define IGL_EXAMPLE_FUN_H
  40. #include &quot;igl_inline.h&quot;
  41. namespace igl
  42. {
  43. // This is an example of a function, it takes a templated parameter and
  44. // shovels it into cout
  45. //
  46. // Templates:
  47. // T type that supports
  48. // Input:
  49. // input some input of a Printable type
  50. // Returns true for the sake of returning something
  51. template &lt;typename Printable&gt;
  52. IGL_INLINE bool example_fun(const Printable &amp; input);
  53. }
  54. #ifndef IGL_STATIC_LIBRARY
  55. # include &quot;example_fun.cpp&quot;
  56. #endif
  57. #endif
  58. </code></pre>
  59. <h4 id="example_fun.cpp"><code>example_fun.cpp</code></h4>
  60. <pre><code>// This file is part of libigl, a simple c++ geometry processing library.
  61. //
  62. // Copyright (C) 2015 [Your Name] [your email address]
  63. //
  64. // This Source Code Form is subject to the terms of the Mozilla Public License
  65. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  66. // obtain one at http://mozilla.org/MPL/2.0/
  67. #include &quot;igl/example_fun.h&quot;
  68. #include &lt;iostream&gt;
  69. template &lt;typename Printable&gt;
  70. IGL_INLINE bool igl::example_fun(const Printable &amp; input)
  71. {
  72. using namespace std;
  73. cout&lt;&lt;&quot;example_fun: &quot;&lt;&lt;input&lt;&lt;endl;
  74. return true;
  75. }
  76. #ifdef IGL_STATIC_LIBRARY
  77. template bool igl::example_fun&lt;double&gt;(const double&amp; input);
  78. template bool igl::example_fun&lt;int&gt;(const int&amp; input);
  79. #endif
  80. </code></pre>
  81. <h3 id="avoidstatichelperfunctions">Avoid static &#8220;helper&#8221; functions</h3>
  82. <p>Strive to encapsulate sub-functions that could possibly be useful outside of
  83. the implementation of your current function. This might mean abstracting the
  84. interface a bit. If it doesn&#8217;t dramatically effect performance then create a
  85. new pair of .h/.cpp files with this sub-function.</p>
  86. <h4 id="lambdafunctions">Lambda functions</h4>
  87. <p>If encapsulation in a separate file is not possible or does not make sense,
  88. then avoid crowding the namespace by creating lambda functions within the
  89. function implmentation.</p>
  90. <h3 id="avoidhelperclasses">Avoid &#8220;helper&#8221; classes</h3>
  91. <p>Libigl is built around the high-performance paradigm of &#8220;struct of arrays&#8221;
  92. rather than &#8220;array of structs&#8221;. The way we achieve this is to avoid classes and
  93. pass &#8220;basic types&#8221; directly. The price we pay is long function interfaces, but
  94. this increases code reuse dramatically. A &#8220;basic type&#8221; in our context is a
  95. Eigen type, stl type, or basic C type.</p>
  96. <h2 id="headerdocumentation">Header Documentation</h2>
  97. <p>Each function prototype should be well documented in its corresponding .h
  98. header file. A typical documentation consists of four parts:</p>
  99. <pre><code class="cpp">// [A human readable description of what the function does.]
  100. //
  101. // Inputs:
  102. // [variable name of first (const) input] [dimensions and
  103. // description of this input variable]
  104. // [variable name of second (const) input] [dimensions and
  105. // description of this input variable]
  106. // ...
  107. // Outputs:
  108. // [variable name of first output ] [dimensions and
  109. // description of this output variable]
  110. // [variable name of second output ] [dimensions and
  111. // description of this output variable]
  112. // ...
  113. // Returns [description of return value]
  114. </code></pre>
  115. <h3 id="example">Example</h3>
  116. <p>For example the header <code>barycenter.h</code></p>
  117. <pre><code>// Computes the barycenter of every simplex
  118. //
  119. // Inputs:
  120. // V #V x dim matrix of vertex coordinates
  121. // F #F x simplex_size matrix of indices of simplex corners into V
  122. // Output:
  123. // BC #F x dim matrix of 3d vertices
  124. //
  125. </code></pre>
  126. <h2 id="constinputs">Const inputs</h2>
  127. <p>All input parameters should be demarcated <code>const</code>. If an input is also an
  128. output than consider exposing two parameters (one <code>const</code>) or be sure to list
  129. the variable under both <code>// Inputs:</code> and <code>// Outputs:</code> in the header comments.</p>
  130. <h2 id="referenceparameters">Reference parameters</h2>
  131. <p>All but simple types should be passed by reference (e.g. <code>Matrix &amp; mat</code>) rather
  132. than pointers (e.g. <code>Matrix * mat</code>) or value (e.g. <code>Matrix mat</code>).</p>
  133. <h2 id="returnsvsoutputparameters">Returns vs output parameters</h2>
  134. <p>All functions should be implemented with at least one overload that has a
  135. <code>void</code> or simple return type (e.g. <code>bool</code> on success/failure). With this
  136. implementation its then possible to write an overload that returns a single
  137. output.</p>
  138. <p>For example:</p>
  139. <pre><code class="cpp">template &lt;typename Atype&gt;
  140. void adjacency_matrix(const ... &amp; F, Eigen::SparseMatrix&lt;AType&gt; &amp; A);
  141. template &lt;typename Atype&gt;
  142. Eigen::SparseMatrix&lt;Atype&gt; adjacency_matrix(const ... &amp; F);
  143. </code></pre>
  144. <h2 id="functionnamingconventions">Function naming conventions</h2>
  145. <p>Functions (and <a href="#filefunction">thus also files</a>) should have simple,
  146. descriptive names using lowercase letters and underscores between words. Avoid
  147. unnecessary prefaces. For example, instead of <code>compute_adjacency_matrix</code>,
  148. <code>construct_adjacency_matrix</code>, <code>extract_adjacency_matrix</code>,
  149. <code>get_adjacency_matrix</code>, or <code>set_adjacency_matrix</code> just call the function
  150. <code>adjacency_matrix</code>.</p>
  151. <h2 id="variablenamingconventions">Variable naming conventions</h2>
  152. <p>Libigl prefers short (even single character) variable names <em>with heavy
  153. documentation</em> in the comments in the header file or above the declaration of
  154. the function. When possible use <code>V</code> to mean a list of vertex positions and <code>F</code>
  155. to mean a list of faces/triangles.</p>
  156. <h2 id="classnamingconventions">Class naming conventions</h2>
  157. <p>Classes should be avoided. When naming a class use CamelCase (e.g.
  158. SortableRow.h).</p>
  159. <h2 id="enumnamingconvertion">Enum naming convertion</h2>
  160. <p>Enums types should be placed in the appropriate <code>igl::</code> namespace and should be
  161. named in CamelCase (e.g. <code>igl::SolverStatus</code>) and instances should be named in
  162. ALL_CAPS with underscores between words and prefaced with the name of the enum.
  163. For example:</p>
  164. <pre><code class="cpp">namespace igl
  165. {
  166. enum SolverStatus
  167. {
  168. // Good
  169. SOLVER_STATUS_CONVERGED = 0,
  170. // OK
  171. SOLVER_STATUS_MAX_ITER = 1,
  172. // Bad
  173. SOLVER_STATUS_ERROR = 2,
  174. NUM_SOLVER_STATUSES = 3,
  175. };
  176. };
  177. </code></pre>
  178. <h3 id="exceptionforfileio">Exception for file IO</h3>
  179. <p>For legacy reasons, file reading and writing functions use a different naming
  180. convention. A functions reading a <code>.xyz</code> file should be named <code>readXYZ</code> and a
  181. function writing <code>.xyz</code> files should be names <code>writeXYZ</code>.</p>
  182. <h2 id="usingnamespace...inglobalscope"><code>using namespace ...</code> in global scope</h2>
  183. <p>Writing <code>using namespace std;</code>, <code>using namespace Eigen;</code> etc. outside of a
  184. global scope is strictly forbidden. Place these lines at the top of each
  185. function instead.</p>
  186. <h2 id="namespacesandexternaldependencies">Namespaces and external dependencies</h2>
  187. <p>Functions in the main library (directly in <code>include/igl</code>) should only depend on
  188. Eigen and stl. These functions should have the <code>igl::</code> namespace.</p>
  189. <p>Functions with other dependencies should be placed into
  190. appropriate sub-directories (e.g. if <code>myfunction</code> depends on tetgen then create
  191. <code>igl/copyleft/tetgen/myfunction.h</code> and <code>igl/copyleft/tetgen/myfunction.cpp</code> and give the function
  192. the namespace <code>igl::copyleft::tetgen::myfunction</code>.</p>
  193. <h3 id="copyleftsubdirectorynamespace">copyleft subdirectory/namespace</h3>
  194. <p>Dependencies that require users of libigl to release their projects open source
  195. (e.g. GPL) are considered aggressively &#8220;copyleft&#8221; and should be placed in the
  196. <code>include/igl/copyleft/</code> sub-directory and <code>igl::copyleft::</code> namespace.</p>
  197. <h2 id="assertions">Assertions</h2>
  198. <p>Be generous with assertions and always identify the assertion with strings:</p>
  199. <pre><code class="cpp">assert(m &lt; n &amp;&amp; &quot;m must be less than n&quot;);
  200. </code></pre>
  201. <h2 id="ifndefincludeguard">ifndef include guard</h2>
  202. <p>Every header file should be wrapped in an <code>#ifndef</code> compiler directive. The
  203. name of the guard should be in direct correspondence with the path of the .h
  204. file. For example, <code>include/igl/copyleft/tetgen/tetrahedralize.h</code> should be</p>
  205. <pre><code class="cpp">#ifndef IGL_COPYLEFT_TETGEN_TETRAHEDRALIZE_H
  206. #define IGL_COPYLEFT_TETGEN_TETRAHEDRALIZE_H
  207. ...
  208. #endif
  209. </code></pre>
  210. <h2 id="spacesvs.tabsindentation">Spaces vs. tabs indentation</h2>
  211. <p>Do not use tabs. Use 2 spaces for each indentation level.</p>
  212. <h2 id="maxlinelength">Max line length</h2>
  213. <p>Limit lines to 80 characters. Break up long lines into many operations (this
  214. also helps performance).</p>
  215. <h2 id="includeorder">Include order</h2>
  216. <p><code>#include</code> directives at the top of a .h or .cpp file should be sorted
  217. according to a simple principle: place headers of files most likely to be
  218. edited by you first. This means for
  219. <code>include/igl/copyleft/tetgen/tetrahedralize.cpp</code> you might see</p>
  220. <pre><code class="cpp">// [Includes of headers in this directory]
  221. #include &quot;tetrahedralize.h&quot;
  222. #include &quot;mesh_to_tetgenio.h&quot;
  223. #include &quot;tetgenio_to_tetmesh.h&quot;
  224. // [Includes of headers in this project]
  225. #include &quot;../../matrix_to_list.h&quot;
  226. #include &quot;../../list_to_matrix.h&quot;
  227. #include &quot;../../boundary_facets.h&quot;
  228. // [Includes of headers of related projects]
  229. #include &lt;Eigen/Core&gt;
  230. // [Includes of headers of standard libraries]
  231. #include &lt;cassert&gt;
  232. #include &lt;iostream&gt;
  233. </code></pre>
  234. <h2 id="placementofincludes">Placement of includes</h2>
  235. <p>Whenever possible <code>#include</code> directives should be placed in the <code>.cpp</code>
  236. implementation file rather than the <code>.h</code> header file.</p>
  237. </body>
  238. </html>