omdl  v0.9.5
OpenSCAD Mechanical Design Library
polyhedron_db.scad
Go to the documentation of this file.
1 //! Sizable polyhedra generated from shape database.
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 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 db)
31  \amu_define group_brief (Sizable polyhedra generated from shape database.)
32 
33  \amu_include (include/amu/pgid_path_pstem_pg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group and macros.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/group_in_parent_start.amu)
42  \amu_define includes_required_add
43  (
44  database/geometry/polyhedra/polyhedra_all.scad
45  )
46  \amu_include (include/amu/includes_required.amu)
47 
48  \details
49 
50  To work with a smaller data table set, include the specific table
51  of interest rather than \ref polyhedra_all.scad, shown as required
52  above. Here is an example that uses a limited set.
53 
54  \amu_define title (Cupolas example)
55  \amu_define image_views (top bottom diag)
56  \amu_define image_size (sxga)
57 
58  \amu_include (include/amu/scope_diagrams_3d.amu)
59 
60  For a list of all available polyhedra, see the polyhedra
61  \ref database_geometry_polyhedra "datbase".
62 *******************************************************************************/
63 
64 //----------------------------------------------------------------------------//
65 
66 //! <map> The default polyhedra data table columns.
68 
69 //! <table> The default polyhedra data table rows.
71 
72 //! Get the number of shape identifiers in data table.
73 /***************************************************************************//**
74  \param tr <table> The polyhedra data table rows.
75 
76  \returns <integer> The total number of identifiers
77 *******************************************************************************/
78 function ph_db_get_size
79 (
80  tr = ph_db_dtr
81 ) = let ( ids = table_get_row_ids( r=tr ) )
82  is_list( ids ) ? len(ids) : undef;
83 
84 //! Get data table identifier name (or names).
85 /***************************************************************************//**
86  \param n <integer> An index number.
87  \param tr <table> The polyhedra data table rows.
88 
89  \returns <string | string-list> The identifier name at index \c n or
90  the list of all identifier names for \p n = 0.
91 
92  \details
93 
94  Identifiers numbering start from 1 and end at ph_db_get_size().
95 *******************************************************************************/
96 function ph_db_get_id
97 (
98  n,
99  tr = ph_db_dtr
100 ) = let ( ids = table_get_row_ids( r=tr ) )
101  (n == 0) ? ids : ids[n-1];
102 
103 //! Construct a named polyhedron.
104 /***************************************************************************//**
105  \param id <string> The polyhedron identifier name.
106 
107  \param size <decimal-list-3 | decimal> A list [x, y, z] of decimals
108  or a single decimal for (x=y=z).
109 
110  \param align <decimal-list-3 | decimal> A list [x, y, z] of decimals
111  or a single decimal for (x=y=z).
112 
113  \param yz <boolean> An internal dataset rotation control variable.
114 
115  \param tr <table> The polyhedra data table rows.
116  \param tc <map> The polyhedra data table columns.
117 
118  \details
119 
120  Each axis may be assigned an alignment value of 0, 1 or 2 as
121  summarized in the following table.
122 
123  align | value description
124  :---------------|:---------------------------------
125  0 | do not align axis
126  1 | align axis at lower shape bounds
127  2 | align axis at upper shape bounds
128 
129  Using this module, polyhedron can be incorporated into designs as
130  shown in this simple example.
131 
132  \amu_define title (Design example)
133  \amu_define image_views (front diag)
134  \amu_define image_size (sxga)
135  \amu_define scope_id (example_design)
136 
137  \amu_include (include/amu/scope_diagrams_3d.amu)
138 *******************************************************************************/
139 module ph_db_polyhedron
140 (
141  id,
142  size,
143  align,
144  yz = true,
145  tr = ph_db_dtr,
146  tc = ph_db_dtc
147 )
148 {
149  shape_id_exists = table_exists(tr, tc, ri=id);
150 
151  // shape id must exists
152  assert
153  (
154  shape_id_exists,
155  str
156  (
157  "id does not exists for id=[", id,
158  "], tr=[ph_db_dtr], and tc=[ph_db_dtc]."
159  )
160  );
161 
162  // only when id exists
163  if ( shape_id_exists )
164  {
165  // lookup (1) coordinates and (2) face-list
166  c = table_get_value(tr, tc, id, "c");
167  f = table_get_value(tr, tc, id, "f");
168 
169  // data structured more for shape viewing; rotate y to z
170  r = rotate_p(c, [yz ? 90 : 0, 0, 0]);
171 
172  // x, y, and z size
173  sx = defined_e_or(size, 0, size);
174  sy = defined_e_or(size, 1, sx);
175  sz = defined_e_or(size, 2, sy);
176 
177  // resize iff size has be defined
178  s = is_undef(size) ? r : resize_p(r, [sx, sy, sz]);
179 
180  // get bounding box for alignment
181  b = polytope_limits(s, f, s=false);
182 
183  ax = defined_e_or(align, 0, 0);
184  ay = defined_e_or(align, 1, 0);
185  az = defined_e_or(align, 2, 0);
186 
187  tx = defined_e_or(b[0], ax-1, 0);
188  ty = defined_e_or(b[1], ay-1, 0);
189  tz = defined_e_or(b[2], az-1, 0);
190 
191  // translate to alignment location
192  m = yz
193  ? translate_p(s, [tx, ty, -tz] )
194  : translate_p(s, [tx, -ty, tz] );
195 
196  polyhedron(m, f);
197  }
198 }
199 
200 
201 //! @}
202 //! @}
203 
204 //----------------------------------------------------------------------------//
205 // openscad-amu auxiliary scripts
206 //----------------------------------------------------------------------------//
207 
208 /*
209 BEGIN_SCOPE example;
210  BEGIN_OPENSCAD;
211  include <omdl-base.scad>;
212  include <database/geometry/polyhedra/cupolas.scad>;
213  include <shapes/polyhedron_db.scad>;
214 
215  ph_db_dtc = dtc_polyhedra_cupolas;
216  ph_db_dtr = dtr_polyhedra_cupolas;
217 
218  gx = ceil( sqrt( ph_db_get_size() ) );
219  gy = gx;
220 
221  sx = 2.25;
222  sy = sx;
223 
224  for ( i = [ 1 : ph_db_get_size() ] )
225  translate([sx * (i%gx), sy * (floor(i/gx)%gy), 0])
226  ph_db_polyhedron( id=ph_db_get_id(i) );
227 
228  // end_include
229  END_OPENSCAD;
230 
231  BEGIN_MFSCRIPT;
232  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
233  table_unset_all sizes;
234 
235  images name "sizes" types "sxga";
236  views name "views" views "top bottom diag";
237 
238  variables set_opts_combine "sizes views";
239  variables add_opts "--viewall --autocenter";
240 
241  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
242  END_MFSCRIPT;
243 END_SCOPE;
244 
245 BEGIN_SCOPE example_design;
246  BEGIN_OPENSCAD;
247  include <omdl-base.scad>;
248  include <database/geometry/polyhedra/johnson.scad>;
249  include <shapes/polyhedron_db.scad>;
250 
251  ph_db_dtc = dtc_polyhedra_johnson;
252  ph_db_dtr = dtr_polyhedra_johnson;
253 
254  $fn = 5;
255 
256  // shape sizes
257  h1 = 1; s1 = [10, 10, h1];
258  h2 = 5; s2 = 2;
259  h3 = 3; s3 = [15, 15, h3];
260 
261  translate([0, 0, h1])
262  {
263  ph_db_polyhedron( id=ph_db_get_id(65), size=s1, align=[0,0,2] );
264 
265  rotate(-360/4/$fn)
266  cylinder(r=s2, h=h2);
267 
268  translate([0, 0, h2]) mirror([0,0,1])
269  ph_db_polyhedron( id=ph_db_get_id(41), size=s3, align=[0,0,2] );
270  }
271 
272  // end_include
273  END_OPENSCAD;
274 
275  BEGIN_MFSCRIPT;
276  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
277  table_unset_all sizes;
278 
279  images name "sizes" types "sxga";
280  views name "views" views "front diag";
281 
282  variables set_opts_combine "sizes views";
283  variables add_opts "--viewall --autocenter --view=axes";
284 
285  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
286  END_MFSCRIPT;
287 END_SCOPE;
288 */
289 
290 //----------------------------------------------------------------------------//
291 // end of file
292 //----------------------------------------------------------------------------//
dtr_polyhedra_polyhedra_all
dtc_polyhedra_polyhedra_all
function defined_e_or(v, i, d)
Return an element of an iterable when it exists or a default value otherwise.
function table_exists(r, c, ri, ci)
Test the existence of a table row identifier, table column identifier, or both.
function table_get_row_ids(r)
Form a list of all table row identifiers.
function table_get_value(r, c, ri, ci)
Get the table cell value for a specified row and column identifier.
function rotate_p(c, a, v, o=origin3d)
Rotate all coordinates about one or more axes in 2D or 3D.
function resize_p(c, v)
Scale all coordinates dimensions proportionately to fit inside a region.
function translate_p(c, v)
Translate all coordinates dimensions.
function polytope_limits(c, f, a, d, s=true)
Determine the bounding limits of a polytope.
function ph_db_get_size(tr=ph_db_dtr)
Get the number of shape identifiers in data table.
module ph_db_polyhedron(id, size, align, yz=true, tr=ph_db_dtr, tc=ph_db_dtc)
Construct a named polyhedron.
function ph_db_get_id(n, tr=ph_db_dtr)
Get data table identifier name (or names).
ph_db_dtc
<map> The default polyhedra data table columns.
ph_db_dtr
<table> The default polyhedra data table rows.