omdl  v1.0
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,2026
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/doxyg_init_pd_gds_ipg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group and macros.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/doxyg_define_in_parent_open.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 polyhedra data set, include the specific
51  table of interest rather than \ref polyhedra_all.scad, as shown in
52  the following example. For a list of those available, see the \ref
53  database_geometry_polyhedra "polyhedra database" section.
54 
55  Here is an example that uses a limited set.
56 
57  \amu_define title (Cupolas example)
58  \amu_define image_views (top bottom diag)
59  \amu_define image_size (sxga)
60 
61  \amu_include (include/amu/scope_diagrams_3d.amu)
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 tr <table> The polyhedra data table rows.
114  \param tc <map> The polyhedra data table columns.
115 
116  \details
117 
118  The polyhedron is constructed at the origin and can be repositioned
119  using \p align. Each axis is aligned independently, with 0 indicating
120  the center, +1 the positive bounds, and -1 the negative bounds.
121  Fractional alignments and alignment values with magnitudes greater
122  than 1 are also supported.
123 
124  align | value description
125  :-----:|:---------------------------------
126  0 | align at bounding box center
127  -1 | align axis at lower shape bounds
128  +1 | align axis at upper shape bounds
129 
130  Using this module, polyhedron can be incorporated into designs as
131  shown in this simple example.
132 
133  \amu_define title (Design example)
134  \amu_define image_views (front diag)
135  \amu_define image_size (sxga)
136  \amu_define scope_id (example_design)
137 
138  \amu_include (include/amu/scope_diagrams_3d.amu)
139 *******************************************************************************/
140 module ph_db_polyhedron
141 (
142  id,
143  size,
144  align,
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  // x, y, and z size
170  sx = defined_e_or(size, 0, size);
171  sy = defined_e_or(size, 1, sx);
172  sz = defined_e_or(size, 2, sy);
173 
174  // decode alignment
175  ax = defined_e_or(align, 0, 0);
176  ay = defined_e_or(align, 1, 0);
177  az = defined_e_or(align, 2, 0);
178 
179  // resize iff size has been defined
180  s = is_undef(size) ? c : resize_p(c, [sx, sy, sz]);
181 
182  // get bounding box for alignment
183  b = polytope_limits(s, f, s=false);
184 
185  // calculate bounding box center location
186  z = [for (i = b) (first(i)+second(i))/2 ];
187 
188  // calculate bounding box range (from center to bounds)
189  r = [for (i = b) (second(i)-first(i))/2 ];
190 
191  // translate to alignment location
192  m = translate_p(s, [r.x * ax - z.x, r.y * ay - z.y, r.z * az - z.z] );
193 
194  polyhedron(m, f);
195  }
196 }
197 
198 
199 //! @}
200 //! @}
201 
202 //----------------------------------------------------------------------------//
203 // openscad-amu auxiliary scripts
204 //----------------------------------------------------------------------------//
205 
206 /*
207 BEGIN_SCOPE example;
208  BEGIN_OPENSCAD;
209  include <omdl-base.scad>;
210  include <database/geometry/polyhedra/cupolas.scad>;
211  include <shapes/polyhedron_db.scad>;
212 
213  ph_db_dtc = dtc_polyhedra_cupolas;
214  ph_db_dtr = dtr_polyhedra_cupolas;
215 
216  gx = ceil( sqrt( ph_db_get_size() ) );
217  gy = gx;
218 
219  sx = 2.25;
220  sy = sx;
221 
222  for ( i = [ 1 : ph_db_get_size() ] )
223  translate([sx * (i%gx), sy * (floor(i/gx)%gy), 0])
224  rotate([90, 0, 0])
225  ph_db_polyhedron( id=ph_db_get_id(i) );
226 
227  // end_include
228  END_OPENSCAD;
229 
230  BEGIN_MFSCRIPT;
231  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
232  table_unset_all sizes;
233 
234  images name "sizes" types "sxga";
235  views name "views" views "top bottom diag";
236 
237  variables set_opts_combine "sizes views";
238  variables add_opts "--viewall --autocenter";
239 
240  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
241  END_MFSCRIPT;
242 END_SCOPE;
243 */
244 
245 /*
246 BEGIN_SCOPE example_design;
247  BEGIN_OPENSCAD;
248  include <omdl-base.scad>;
249  include <database/geometry/polyhedra/johnson.scad>;
250  include <shapes/polyhedron_db.scad>;
251 
252  ph_db_dtc = dtc_polyhedra_johnson;
253  ph_db_dtr = dtr_polyhedra_johnson;
254 
255  $fn = 5;
256 
257  // shape sizes
258  h1 = 1; s1 = [10, h1, 10];
259  h2 = 5; s2 = 2;
260  h3 = 3; s3 = [15, h3, 15];
261 
262  translate([0, 0, h1])
263  {
264  rotate([90,0,0])
265  ph_db_polyhedron( id=ph_db_get_id(65), size=s1, align=[0,-1,0] );
266 
267  rotate(-360/4/$fn)
268  cylinder(r=s2, h=h2);
269 
270  translate([0, 0, h2]) mirror([0,0,1]) rotate([90,0,0])
271  ph_db_polyhedron( id=ph_db_get_id(41), size=s3, align=[0,-1,0] );
272  }
273 
274  // end_include
275  END_OPENSCAD;
276 
277  BEGIN_MFSCRIPT;
278  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
279  table_unset_all sizes;
280 
281  images name "sizes" types "sxga";
282  views name "views" views "front diag";
283 
284  variables set_opts_combine "sizes views";
285  variables add_opts "--viewall --autocenter --view=axes";
286 
287  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
288  END_MFSCRIPT;
289 END_SCOPE;
290 */
291 
292 //----------------------------------------------------------------------------//
293 // end of file
294 //----------------------------------------------------------------------------//
dtr_polyhedra_polyhedra_all
dtc_polyhedra_polyhedra_all
function defined_e_or(v, i, d)
Returns an element from an iterable if it exists, or a default value if not.
function second(v)
Return the second element of an iterable value.
function first(v)
Return the first element of an iterable value.
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 resize_p(c, v, center=false, o)
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.
function ph_db_get_id(n, tr=ph_db_dtr)
Get data table identifier name (or names).
module ph_db_polyhedron(id, size, align, tr=ph_db_dtr, tc=ph_db_dtc)
Construct a named polyhedron.
ph_db_dtc
<map> The default polyhedra data table columns.
ph_db_dtr
<table> The default polyhedra data table rows.