omdl  v0.9.5
OpenSCAD Mechanical Design Library
polyhedron.scad
Go to the documentation of this file.
1 //! Polyhedron shapes, conversions, properties, and tests functions..
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 2015-2024
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \amu_define group_name (Polyhedrons)
31  \amu_define group_brief (Polyhedron mathematical functions; 3-polytope.)
32 
33  \amu_include (include/amu/pgid_path_pstem_pg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/group_in_parent_start.amu)
42  \amu_include (include/amu/includes_required.amu)
43 *******************************************************************************/
44 
45 //----------------------------------------------------------------------------//
46 
47 //----------------------------------------------------------------------------//
48 // shape properties
49 //----------------------------------------------------------------------------//
50 
51 //! \name Properties
52 //! @{
53 
54 //! Compute the surface area of a polyhedron in a Euclidean 3d-space.
55 /***************************************************************************//**
56  \param c <coords-3d> A list of 3d cartesian coordinates
57  [[x, y, z], ...].
58  \param f <integer-list-list> A list of faces that enclose
59  the shape where each face is a list of coordinate indexes.
60 
61  \returns <decimal> The surface area of the given polyhedron.
62 *******************************************************************************/
63 function polyhedron_area
64 (
65  c,
66  f
67 ) = sum([for (fi = f) polygon3d_area(c, [fi])]);
68 
69 //! Compute the volume of a triangulated polyhedron in a Euclidean 3d-space.
70 /***************************************************************************//**
71  \param c <coords-3d> A list of 3d cartesian coordinates
72  [[x, y, z], ...].
73  \param f <integer-list-3-list> A list of triangular faces that
74  enclose the polyhedron where each face is a list of three
75  coordinate indexes.
76 
77  \returns <decimal> The volume of the given polyhedron.
78 
79  \details
80 
81  See [Wikipedia] for more information on volumes determined using
82  the [divergence theorem].
83 
84  \note All faces are assumed to be a union of triangles oriented
85  clockwise from the outside inwards.
86 
87  [Wikipedia]: https://en.wikipedia.org/wiki/Polyhedron#Volume
88  [divergence theorem]: https://en.wikipedia.org/wiki/Divergence_theorem
89 *******************************************************************************/
90 function polyhedron_tf_volume
91 (
92  c,
93  f
94 ) =
95  let
96  (
97  vv =
98  [
99  for (fi = f)
100  (len(fi) != 3) ? undef
101  : let
102  (
103  a = c[fi[1]],
104  b = c[fi[0]],
105  c = c[fi[2]]
106  )
107  a * cross_ll([a, b], [a, c])
108  ],
109 
110  sv = sum(vv)
111  )
112  sv/6;
113 
114 //! Compute the center of mass of a triangulated polyhedron in a Euclidean 3d-space.
115 /***************************************************************************//**
116  \param c <coords-3d> A list of 3d cartesian coordinates
117  [[x, y, z], ...].
118  \param f <integer-list-3-list> A list of triangular faces that
119  enclose the polyhedron where each face is a list of three
120  coordinate indexes.
121 
122  \returns <point-3d> The center of mass of the given polyhedron.
123 
124  \details
125 
126  See [Wikipedia] for more information on centroid determined via
127  the [divergence theorem] and midpoint quadrature.
128 
129  \note All faces are assumed to be a union of triangles oriented
130  clockwise from the outside inwards.
131 
132  [Wikipedia]: https://en.wikipedia.org/wiki/Centroid
133  [divergence theorem]: https://en.wikipedia.org/wiki/Divergence_theorem
134 *******************************************************************************/
135 function polyhedron_tf_centroid
136 (
137  c,
138  f
139 ) =
140  let
141  (
142  wv =
143  [
144  for (fi = f)
145  let
146  (
147  a = c[fi[1]],
148  b = c[fi[0]],
149  c = c[fi[2]],
150 
151  r = a * cross_ll([a, b], [a, c])
152  )
153  (a+b+c) * r
154  ],
155 
156  ws = sum(wv),
157  tv = polyhedron_tf_volume(c, f)
158  )
159  ws/(24*tv);
160 
161 //! @}
162 
163 //! @}
164 //! @}
165 
166 //----------------------------------------------------------------------------//
167 // end of file
168 //----------------------------------------------------------------------------//
function cross_ll(l1, l2)
Compute the cross product of two lines or vectors in a 3d or 2d-space.
function sum(v, i1, i2)
Compute the sum of a list of numbers.
function polygon3d_area(c, p, n)
Compute the area of a polygon in a Euclidean 3d-space.
function polyhedron_area(c, f)
Compute the surface area of a polyhedron in a Euclidean 3d-space.
function polyhedron_tf_volume(c, f)
Compute the volume of a triangulated polyhedron in a Euclidean 3d-space.
function polyhedron_tf_centroid(c, f)
Compute the center of mass of a triangulated polyhedron in a Euclidean 3d-space.