style-guidelines.html 16 KB

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