omdl  v0.9.5
OpenSCAD Mechanical Design Library
length.scad
Go to the documentation of this file.
1 //! Length units and conversions.
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 (Length Units)
31  \amu_define group_brief (Length units and conversions.)
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  \details
45 
46  These functions allow for lengths to be specified with units.
47  Lengths specified with units are independent of (\ref length_unit_base).
48  There are also unit conversion functions for converting from one unit
49  to another.
50 
51  The table below enumerates the supported units.
52 
53  units id | description
54  :---------:|:----------------------:
55  pm | picometer
56  nm | nanometer
57  um | micrometer
58  mm | millimeter
59  cm | centimeter
60  dm | decimeter
61  m | meter
62  km | kilometer
63  thou, mil | thousandth of an inch
64  in | inch
65  ft | feet
66  yd | yard
67  mi | mile
68 
69  \amu_define title (Length base units example)
70  \amu_define scope_id (example)
71  \amu_define output_scad (true)
72  \amu_define output_console (false)
73  \amu_include (include/amu/scope.amu)
74 
75  \amu_define output_scad (false)
76  \amu_define output_console (true)
77 
78  \amu_define title (length_unit_base=mm)
79  \amu_define scope_id (example_mm)
80  \amu_include (include/amu/scope.amu)
81 
82  \amu_define title (length_unit_base=cm)
83  \amu_define scope_id (example_cm)
84  \amu_include (include/amu/scope.amu)
85 
86  \amu_define title (length_unit_base=mil)
87  \amu_define scope_id (example_mil)
88  \amu_include (include/amu/scope.amu)
89 
90  \amu_define title (length_unit_base=in)
91  \amu_define scope_id (example_in)
92  \amu_include (include/amu/scope.amu)
93 
94  /+
95  include diagram
96  +/
97  \amu_define title (Equivalent lengths)
98  \amu_define image_views (top)
99  \amu_define image_size (qvga)
100  \amu_define scope_id (equivalents)
101  \amu_include (include/amu/scope_diagrams_3d.amu)
102 *******************************************************************************/
103 
104 //----------------------------------------------------------------------------//
105 
106 //! <string> The base units for value storage.
107 length_unit_base = "mm";
108 
109 //! <string> The default units when unspecified.
110 length_unit_default = "mm";
111 
112 //! Return the long name for a length unit identifier.
113 /***************************************************************************//**
114  \param u <string> A length unit identifier.
115 
116  \returns <string> The long name for a length unit identifier.
117  Returns \b undef for identifiers that are not defined.
118 
119  \private
120 *******************************************************************************/
121 function length_unit_name_1d
122 (
124 ) = u == "pm" ? "picometer"
125  : u == "nm" ? "nanometer"
126  : u == "um" ? "micrometer"
127  : u == "mm" ? "millimeter"
128  : u == "cm" ? "centimeter"
129  : u == "dm" ? "decimeter"
130  : u == "m" ? "meter"
131  : u == "km" ? "kilometer"
132  : u == "thou" ? "thousandth"
133  : u == "mil" ? "thousandth"
134  : u == "in" ? "inch"
135  : u == "ft" ? "feet"
136  : u == "yd" ? "yard"
137  : u == "mi" ? "mile"
138  : undef;
139 
140 //! Return the name for a length unit identifier with dimension.
141 /***************************************************************************//**
142  \param u <string> A length unit identifier.
143  \param d <integer> The unit dimension. One of [1|2|3].
144  \param w <boolean> \b true for word and \b false for symbol format.
145 
146  \returns <string> The long name for a length unit identifier with
147  dimension.
148  Returns \b undef for identifiers or dimensions that are
149  not defined.
150 *******************************************************************************/
151 function length_unit_name
152 (
154  d = 1,
155  w = false
156 ) = (w == false) ?
157  (
158  d == 1 ? length_unit_name_1d( u )
159  : d == 2 ? str( length_unit_name_1d( u ), "^2" )
160  : d == 3 ? str( length_unit_name_1d( u ), "^3" )
161  : undef
162  )
163  :
164  (
165  d == 1 ? length_unit_name_1d( u )
166  : d == 2 ? str( "square ", length_unit_name_1d( u ) )
167  : d == 3 ? str( "cubic ", length_unit_name_1d( u ) )
168  : undef
169  );
170 
171 //! Convert a value from millimeters to other units.
172 /***************************************************************************//**
173  \param v <decimal-list | decimal> The value to convert.
174  \param to <string> The units to which the value should be converted.
175 
176  \returns <decimal-list | decimal> The conversion result.
177  Returns \b undef for identifiers that are not defined.
178 
179  \private
180 *******************************************************************************/
181 function length_unit_mm2
182 (
183  v,
184  to
185 ) = to == "pm" ? ( v * 1000000000.0 )
186  : to == "nm" ? ( v * 1000000.0 )
187  : to == "um" ? ( v * 1000.0 )
188  : to == "mm" ? ( v )
189  : to == "cm" ? ( v / 10.0 )
190  : to == "dm" ? ( v / 100.0 )
191  : to == "m" ? ( v / 1000.0 )
192  : to == "km" ? ( v / 1000000.0 )
193  : to == "thou" ? ( v / 0.0254 )
194  : to == "mil" ? ( v / 0.0254 )
195  : to == "in" ? ( v / 25.4 )
196  : to == "ft" ? ( v / 304.8 )
197  : to == "yd" ? ( v / 914.4 )
198  : to == "mi" ? ( v / 1609344.0 )
199  : undef;
200 
201 //! Convert a value from some units to millimeters.
202 /***************************************************************************//**
203  \param v <decimal-list | decimal> The value to convert.
204  \param from <string> The units of the value to be converted.
205 
206  \returns <decimal-list | decimal> The conversion result.
207  Returns \b undef for identifiers that are not defined.
208 
209  \private
210 *******************************************************************************/
211 function length_unit_2mm
212 (
213  v,
214  from
215 ) = v / length_unit_mm2( 1, from );
216 
217 //! Convert a value from from one units to another.
218 /***************************************************************************//**
219  \param v <decimal-list | decimal> The value to convert.
220  \param from <string> The units of the value to be converted.
221  \param to <string> A units to which the value should be converted.
222 
223  \returns <decimal-list | decimal> The conversion result.
224  Returns \b undef for identifiers that are not defined.
225 
226  \private
227 *******************************************************************************/
228 function length_1d
229 (
230  v,
231  from = length_unit_default,
232  to = length_unit_base
233 ) = (from == to) ? v
234  : length_unit_mm2( length_unit_2mm( v, from ), to );
235 
236 //! Convert a value from from one units to another with dimensions.
237 /***************************************************************************//**
238  \param v <decimal> The value to convert.
239  \param from <string> The units of the value to be converted.
240  \param to <string> The units to which the value should be converted.
241  \param d <integer> The unit dimension. One of [1|2|3].
242 
243  \returns <decimal> The conversion result.
244  Returns \b undef for identifiers or dimensions that are
245  not defined.
246 *******************************************************************************/
247 function length
248 (
249  v,
250  from = length_unit_default,
251  to = length_unit_base,
252  d = 1
253 ) = d == 1 ? ( length_1d(v, from, to) )
254  // for multi-dimension, 'v' must be a scalar
255  : is_list(v) ? undef
256  : d == 2 ? pow( length_1d(v, from, to), 2 )
257  : d == 3 ? pow( length_1d(v, from, to), 3 )
258  // undefined for other dimensions
259  : undef;
260 
261 //! Convert a value from from one units to another with dimensions.
262 /***************************************************************************//**
263  \param v <decimal> The value to convert.
264  \param from <string> The units of the value to be converted.
265  \param to <string> The units to which the value should be converted.
266  \param d <integer> The unit dimension. One of [1|2|3].
267 
268  \returns <decimal> The conversion result.
269  Returns \b undef for identifiers or dimensions that are
270  not defined.
271 *******************************************************************************/
272 function length_inv
273 (
274  v,
275  from = length_unit_base,
276  to = length_unit_default,
277  d = 1
278 ) = d == 1 ? length_1d(v, from, to)
279  // for multi-dimension, 'v' must be a scalar
280  : is_list(v) ? undef
281  : d == 2 ? length_1d(pow(v, 1/2), from, to)
282  : d == 3 ? length_1d(pow(v, 1/3), from, to)
283  // undefined for other dimensions
284  : undef;
285 
286 //----------------------------------------------------------------------------//
287 // shorthand conversions
288 //----------------------------------------------------------------------------//
289 
290 //! \name Shorts
291 //! @{
292 
293 //! Shorthand length conversion for millimeters.
294 /***************************************************************************//**
295  \param v <decimal> The value to convert.
296 
297  \returns <decimal> The conversion result.
298 *******************************************************************************/
299 function l_mm(v) = length(v=v, from="mm");
300 
301 //! Shorthand length conversion for inches.
302 /***************************************************************************//**
303  \param v <decimal> The value to convert.
304 
305  \returns <decimal> The conversion result.
306 *******************************************************************************/
307 function l_in(v) = length(v=v, from="in");
308 
309 //! @}
310 
311 //! @}
312 //! @}
313 
314 //----------------------------------------------------------------------------//
315 // openscad-amu auxiliary scripts
316 //----------------------------------------------------------------------------//
317 
318 /*
319 BEGIN_SCOPE example;
320  BEGIN_OPENSCAD;
321  include <omdl-base.scad>;
322 
323  length_unit_base = "mm";
324  length_unit_default = "in";
325 
326  // get unit names
327  bu = length_unit_name(length_unit_base);
328  du = length_unit_name();
329 
330  // absolute length measurements in base unit.
331  c1 = length(1/8);
332  c2 = length(3.175, "mm");
333  c3 = length(25, "mil");
334  c4 = length(1, "ft", d=3);
335 
336  // convert between units.
337  c5 = length(10, from="mil", to="in");
338  c6 = length(10, from="ft", to="mm");
339 
340  // end_include
341 
342  echo( bu=bu );
343  echo( du=du );
344  echo( );
345  echo( c1=c1 );
346  echo( c2=c2 );
347  echo( c3=c3 );
348  echo( c4=c4 );
349  echo( c5=c5 );
350  echo( c6=c6 );
351  END_OPENSCAD;
352 
353  BEGIN_MFSCRIPT;
354  include --path "${INCLUDE_PATH}" {var_init,var_gen_term}.mfs;
355 
356  defines name "units" define "length_unit_base" strings "mm cm mil in";
357  variables add_opts_combine "units";
358 
359  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
360  END_MFSCRIPT;
361 END_SCOPE;
362 
363 BEGIN_SCOPE equivalents;
364  BEGIN_OPENSCAD;
365  include <omdl-base.scad>;
366 
367  module dim( uv=1, un="cm" ) {
368  mx = 200.0;
369  my = 1;
370  tx = my;
371  ty = mx / 10;
372  ts = mx / 25;
373 
374  color( "black" )
375  union() {
376  square( [mx, my], true );
377  translate([-mx/2,0,0]) square( [tx, ty], true );
378  translate([+mx/2,0,0]) square( [tx, ty], true );
379  }
380 
381  l1l = length( uv, "in", un );
382  l1u = length_unit_name( un );
383  l1s = str( l1l, " ", l1u );
384 
385  translate( [0, ts, 0] )
386  text( text=l1s, size=ts, font="Courier:style=bold italic", halign="center", valign="center" );
387  }
388 
389  unv = ["um", "mm", "cm", "mil", "in"];
390  for( un = unv )
391  translate( [0, 30 * search([un], unv)[0], 0] ) dim( 1, un );
392  END_OPENSCAD;
393 
394  BEGIN_MFSCRIPT;
395  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
396 
397  views name "views" translate "0,60,0" distance "400" views "top";
398  variables add_opts_combine "views";
399 
400  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
401  END_MFSCRIPT;
402 END_SCOPE;
403 */
404 
405 //----------------------------------------------------------------------------//
406 // end of file
407 //----------------------------------------------------------------------------//
function l_in(v)
Shorthand length conversion for inches.
function length(v, from=length_unit_default, to=length_unit_base, d=1)
Convert a value from from one units to another with dimensions.
function length_unit_name(u=length_unit_default, d=1, w=false)
Return the name for a length unit identifier with dimension.
length_unit_default
<string> The default units when unspecified.
Definition: length.scad:739
function length_inv(v, from=length_unit_base, to=length_unit_default, d=1)
Convert a value from from one units to another with dimensions.
function l_mm(v)
Shorthand length conversion for millimeters.
length_unit_base
<string> The base units for value storage.
Definition: length.scad:736