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