style-guidelines.html 12 KB

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