omdl  v0.5
OpenSCAD Mechanical Design Library
units_length.scad
Go to the documentation of this file.
1 //! Length units and conversions.
2 /***************************************************************************//**
3  \file units_length.scad
4  \author Roy Allen Sutton
5  \date 2015-2017
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  \note Include this library file using the \b include statement.
31 
32  \ingroup units units_length
33 *******************************************************************************/
34 
35 //----------------------------------------------------------------------------//
36 /***************************************************************************//**
37  \addtogroup units
38  @{
39 
40  \defgroup units_length Length
41  \brief Length units and conversions.
42 
43  \details
44 
45  These functions allow for lengths to be specified with units.
46  Lengths specified with units are independent of (\ref base_unit_length).
47  There are also unit conversion functions for converting from one unit
48  to another.
49 
50  The table below enumerates the supported unit identifiers and their
51  descriptions.
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  \b Example
70 
71  \dontinclude units_length_example.scad
72  \skip include
73  \until to="mm");
74 
75  \b Result (base_unit_length = \b mm): \include units_length_example_mm.log
76  \b Result (base_unit_length = \b cm): \include units_length_example_cm.log
77  \b Result (base_unit_length = \b mil): \include units_length_example_mil.log
78  \b Result (base_unit_length = \b in): \include units_length_example_in.log
79 
80  \b Example (equivalent lengths)
81 
82  \image html units_length_dim_qvga_top.png "Unit Lengths"
83  \image latex units_length_dim_qvga_top.eps "Unit Lengths" width=4in
84 
85  @{
86 *******************************************************************************/
87 //----------------------------------------------------------------------------//
88 
89 //! <string> Base unit for length measurements.
91 
92 //! Return the name of the given length \p unit identifier.
93 /***************************************************************************//**
94  \param units <string> A length unit identifier.
95  \returns <string> The units name for the given length unit identifier.
96  Returns \b 'undef' for identifiers that are not defined.
97  \private
98 *******************************************************************************/
99 function unit_length_name_id_lookup
100 (
101  units = base_unit_length
102 ) = units == "pm" ? "picometer"
103  : units == "nm" ? "nanometer"
104  : units == "um" ? "micrometer"
105  : units == "mm" ? "millimeter"
106  : units == "cm" ? "centimeter"
107  : units == "dm" ? "decimeter"
108  : units == "m" ? "meter"
109  : units == "km" ? "kilometer"
110  : units == "thou" ? "thousandth"
111  : units == "mil" ? "thousandth"
112  : units == "in" ? "inch"
113  : units == "ft" ? "feet"
114  : units == "yd" ? "yard"
115  : units == "mi" ? "mile"
116  : undef;
117 
118 //! Return the name of the given \p unit identifier with dimension (symbol).
119 /***************************************************************************//**
120  \param units <string> A length unit identifier.
121  \param d <decimal> A dimension set to one of \p [1|2|3].
122  \returns <string> The units name for the given length unit identifier with
123  is specified dimension. Returns \b 'undef' for identifiers or
124  dimensions that are not defined.
125  \private
126 *******************************************************************************/
127 function unit_length_name_symbol
128 (
129  units = base_unit_length,
130  d = 1
131 ) = d == 1 ? unit_length_name_id_lookup( units )
132  : d == 2 ? str( unit_length_name_id_lookup( units ), "^2" )
133  : d == 3 ? str( unit_length_name_id_lookup( units ), "^3" )
134  : undef;
135 
136 //! Return the name of the given \p unit identifier with dimension (word).
137 /***************************************************************************//**
138  \copydetails unit_length_name_symbol()
139  \private
140 *******************************************************************************/
141 function unit_length_name_word
142 (
143  units = base_unit_length,
144  d = 1
145 ) = d == 1 ? unit_length_name_id_lookup( units )
146  : d == 2 ? str( "square ", unit_length_name_id_lookup( units ) )
147  : d == 3 ? str( "cubic ", unit_length_name_id_lookup( units ) )
148  : undef;
149 
150 //! Return the name of the given \p unit identifier with dimension.
151 /***************************************************************************//**
152  \param w <boolean> \b true: use word format, \b false: use symbol format.
153  \copydetails unit_length_name_symbol()
154 *******************************************************************************/
155 function unit_length_name
156 (
157  units = base_unit_length,
158  d = 1,
159  w = false
160 ) = w == true ? unit_length_name_word( units, d )
161  : unit_length_name_symbol( units, d );
162 
163 //! Convert the \p value from millimeters to \p to units.
164 /***************************************************************************//**
165  \param value <decimal> A value to convert.
166  \param to <string> The units to which the value should be converted.
167  \returns <decimal> The conversion result. Returns \b 'undef' for identifiers
168  that are not defined.
169  \private
170 *******************************************************************************/
171 function unit_length_mm_to
172 (
173  value,
174  to
175 ) = to == "pm" ? ( value * 1000000000.0 )
176  : to == "nm" ? ( value * 1000000.0 )
177  : to == "um" ? ( value * 1000.0 )
178  : to == "mm" ? ( value )
179  : to == "cm" ? ( value / 10.0 )
180  : to == "dm" ? ( value / 100.0 )
181  : to == "m" ? ( value / 1000.0 )
182  : to == "km" ? ( value / 1000000.0 )
183  : to == "thou" ? ( value / 0.0254 )
184  : to == "mil" ? ( value / 0.0254 )
185  : to == "in" ? ( value / 25.4 )
186  : to == "ft" ? ( value / 304.8 )
187  : to == "yd" ? ( value / 914.4 )
188  : to == "mi" ? ( value / 1609344.0 )
189  : undef;
190 
191 //! Convert the \p value from \p from units to millimeters.
192 /***************************************************************************//**
193  \param value <decimal> A value to convert.
194  \param from <string> The units of the value to be converted.
195  \returns <decimal> The conversion result. Returns \b 'undef' for identifiers
196  that are not defined.
197  \private
198 *******************************************************************************/
199 function unit_length_to_mm
200 (
201  value,
202  from
203 ) = value / unit_length_mm_to( 1, from );
204 
205 //! Convert the \p value from \p from units to \p to units.
206 /***************************************************************************//**
207  \param value <decimal> A value to convert.
208  \param from <string> The units of the value to be converted.
209  \param to <string> A units to which the value should be converted.
210  \returns <decimal> The conversion result. Returns \b 'undef' for identifiers
211  that are not defined.
212  \private
213 *******************************************************************************/
214 function unit_length_convert
215 (
216  value,
217  from = base_unit_length,
218  to = base_unit_length
219 ) = unit_length_mm_to( unit_length_to_mm( value, from ), to );
220 
221 //! Convert the \p value from \p from units to \p to units with dimensions.
222 /***************************************************************************//**
223  \param value <decimal> A value to convert.
224  \param from <string> The units of the value to be converted.
225  \param to <string> A units to which the value should be converted.
226  \param d <decimal> The dimension set to one of \p [1|2|3].
227  \returns <decimal> The conversion result. Returns \b 'undef' for identifiers or
228  dimensions that are not defined.
229 *******************************************************************************/
230 function convert_length
231 (
232  value,
233  from = base_unit_length,
234  to = base_unit_length,
235  d = 1
236 ) = d == 1 ? ( unit_length_convert(value, from, to) )
237  : d == 2 ? pow( unit_length_convert(value, from, to), 2 )
238  : d == 3 ? pow( unit_length_convert(value, from, to), 3 )
239  : undef;
240 
241 //! @}
242 //! @}
243 
244 //----------------------------------------------------------------------------//
245 // openscad-amu auxiliary scripts
246 //----------------------------------------------------------------------------//
247 
248 /*
249 BEGIN_SCOPE example;
250  BEGIN_OPENSCAD;
251  include <units_length.scad>;
252 
253  base_unit_length = "mm";
254 
255  // get base unit name
256  un = unit_length_name();
257 
258  // absolute length measurements in base unit.
259  c1 = convert_length(1/8, "in");
260  c2 = convert_length(3.175, "mm");
261  c3 = convert_length(25, "mil");
262  c4 = convert_length(1, "ft", d=3);
263 
264  // convert between units.
265  c5 = convert_length(10, from="mil", to="in");
266  c6 = convert_length(10, from="ft", to="mm");
267 
268  echo( un=un );
269  echo( c1=c1 );
270  echo( c2=c2 );
271  echo( c3=c3 );
272  echo( c4=c4 );
273  echo( c5=c5 );
274  echo( c6=c6 );
275  END_OPENSCAD;
276 
277  BEGIN_MFSCRIPT;
278  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
279 
280  defines name "units" define "base_unit_length" strings "mm cm mil in";
281  variables add_opts_combine "units";
282 
283  include --path "${INCLUDE_PATH}" script_std.mfs;
284  END_MFSCRIPT;
285 END_SCOPE;
286 
287 BEGIN_SCOPE dim;
288  BEGIN_OPENSCAD;
289  include <units_length.scad>;
290 
291  module dim( uv=1, un="cm" ) {
292  mx = 200.0;
293  my = 1;
294  tx = my;
295  ty = mx / 10;
296  ts = mx / 25;
297 
298  color( "black" )
299  union() {
300  square( [mx, my], true );
301  translate([-mx/2,0,0]) square( [tx, ty], true );
302  translate([+mx/2,0,0]) square( [tx, ty], true );
303  }
304 
305  l1l = convert_length( uv, "in", un );
306  l1u = unit_length_name( un );
307  l1s = str( l1l, " ", l1u );
308 
309  translate( [0, ts, 0] )
310  text( text=l1s, size=ts, font="Courier:style=bold italic", halign="center", valign="center" );
311  }
312 
313  unv = ["um", "mm", "cm", "mil", "in"];
314  for( un = unv )
315  translate( [0, 30 * search([un], unv)[0], 0] ) dim( 1, un );
316  END_OPENSCAD;
317 
318  BEGIN_MFSCRIPT;
319  include --path "${INCLUDE_PATH}" {config_base,config_png}.mfs;
320 
321  views name "views" translate "0,60,0" distance "400" views "top";
322  variables add_opts_combine "views";
323 
324  include --path "${INCLUDE_PATH}" script_std.mfs;
325  END_MFSCRIPT;
326 END_SCOPE;
327 */
328 
329 //----------------------------------------------------------------------------//
330 // end of file
331 //----------------------------------------------------------------------------//
function convert_length(value, from=base_unit_length, to=base_unit_length, d=1)
Convert the value from from units to to units with dimensions.
base_unit_length
Base unit for length measurements.
function unit_length_name(units=base_unit_length, d=1, w=false)
Return the name of the given unit identifier with dimension.