omdl  v0.9.5
OpenSCAD Mechanical Design Library
angle.scad
Go to the documentation of this file.
1 //! Angle 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 (Angle Units)
31  \amu_define group_brief (Angle 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 angles to be specified with units.
47  Angles specified with units are independent of (\ref angle_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 | type |
54  :---------:|:----------------------:|:---------------:|
55  r | radian | decimal |
56  d | degree | decimal |
57  dms | degree, minute, second | decimal-list-3 |
58 
59 
60  \amu_define title (Angle base unit example)
61  \amu_define scope_id (example)
62  \amu_define output_scad (true)
63  \amu_define output_console (false)
64  \amu_include (include/amu/scope.amu)
65 
66  \amu_define output_scad (false)
67  \amu_define output_console (true)
68 
69  \amu_define title (angle_unit_base=r)
70  \amu_define scope_id (example_r)
71  \amu_include (include/amu/scope.amu)
72 
73  \amu_define title (angle_unit_base=d)
74  \amu_define scope_id (example_d)
75  \amu_include (include/amu/scope.amu)
76 
77  \amu_define title (angle_unit_base=dms)
78  \amu_define scope_id (example_dms)
79  \amu_include (include/amu/scope.amu)
80 *******************************************************************************/
81 
82 //----------------------------------------------------------------------------//
83 
84 //! <string> The base units for value storage.
85 angle_unit_base = "d";
86 
87 //! <string> The default units when unspecified.
88 angle_unit_default = "d";
89 
90 //! Return the name of an angle unit identifier.
91 /***************************************************************************//**
92  \param u <string> An angle unit identifier.
93 
94  \returns <string> The units name for the given angle unit identifier.
95  Returns \b undef for identifiers that are not defined.
96 *******************************************************************************/
97 function angle_unit_name
98 (
100 ) = u == "r" ? "radian"
101  : u == "d" ? "degree"
102  : u == "dms" ? "degree, minute, second"
103  : undef;
104 
105 //! Convert an angle from degrees to other units.
106 /***************************************************************************//**
107  \param a <decimal | decimal-list-3> An angle to convert.
108  \param to <string> The units to which the angle should be converted.
109 
110  \returns <decimal | decimal-list-3> The conversion result.
111  Returns \b undef for identifiers that are not defined.
112 
113  \private
114 *******************************************************************************/
115 function angle_unit_d2
116 (
117  a,
118  to
119 ) = to == "r" ? (a * tau / 360)
120  : to == "d" ? (a)
121  : to == "dms" ? ([
122  floor(a),
123  floor((a - floor(a)) * 60),
124  (a - floor(a) - floor((a - floor(a)) * 60) / 60) * 3600
125  ])
126  : undef;
127 
128 //! Convert an angle from some units to degrees.
129 /***************************************************************************//**
130  \param a <decimal | decimal-list-3> An angle to convert.
131  \param from <string> The units of the angle to be converted.
132 
133  \returns <decimal | decimal-list-3> The conversion result.
134  Returns \b undef for identifiers that are not defined.
135  \private
136 *******************************************************************************/
137 function angle_unit_2d
138 (
139  a,
140  from
141 ) = from == "r" ? (a * 360 / tau)
142  : from == "d" ? (a)
143  : from == "dms" ? (a[0] + a[1]/60 + a[2]/3600)
144  : undef;
145 
146 //! Convert an angle from some units to another.
147 /***************************************************************************//**
148  \param a <decimal | decimal-list-3> An angle to convert.
149  \param from <string> The units of the angle to be converted.
150  \param to <string> A units to which the angle should be converted.
151 
152  \returns <decimal | decimal-list-3> The conversion result.
153  Returns \b undef for identifiers that are not defined.
154 *******************************************************************************/
155 function angle
156 (
157  a,
158  from = angle_unit_default,
159  to = angle_unit_base
160 ) = (from == to) ? a
161  : angle_unit_d2( angle_unit_2d( a, from ), to );
162 
163 //! Convert an angle from some units to another.
164 /***************************************************************************//**
165  \param a <decimal | decimal-list-3> An angle to convert.
166  \param from <string> The units of the angle to be converted.
167  \param to <string> A units to which the angle should be converted.
168 
169  \returns <decimal | decimal-list-3> The conversion result.
170  Returns \b undef for identifiers that are not defined.
171 *******************************************************************************/
172 function angle_inv
173 (
174  a,
175  from = angle_unit_base,
176  to = angle_unit_default
177 ) = (from == to) ? a
178  : angle_unit_d2( angle_unit_2d( a, from ), to );
179 
180 //----------------------------------------------------------------------------//
181 // shorthand conversions
182 //----------------------------------------------------------------------------//
183 
184 //! \name Shorts
185 //! @{
186 
187 //! Shorthand angle conversion for degrees.
188 /***************************************************************************//**
189  \param a <decimal> The angle to convert.
190 
191  \returns <decimal> The conversion result.
192 *******************************************************************************/
193 function a_deg(a) = angle(a=a, from="d");
194 
195 //! Shorthand angle conversion for radians.
196 /***************************************************************************//**
197  \param a <decimal> The angle to convert.
198 
199  \returns <decimal> The conversion result.
200 *******************************************************************************/
201 function a_rad(a) = angle(a=a, from="r");
202 
203 //! @}
204 
205 //! @}
206 //! @}
207 
208 //----------------------------------------------------------------------------//
209 // openscad-amu auxiliary scripts
210 //----------------------------------------------------------------------------//
211 
212 /*
213 BEGIN_SCOPE example;
214  BEGIN_OPENSCAD;
215  include <omdl-base.scad>;
216 
217  angle_unit_base = "d";
218  angle_unit_default = "r";
219 
220  // get unit names
221  bu = angle_unit_name(angle_unit_base);
222  du = angle_unit_name();
223 
224  // absolute angle measurements in base unit.
225  c1 = angle(pi/6);
226  c2 = angle(pi/4);
227  c3 = angle(180, "d");
228  c4 = angle([30, 15, 50], "dms");
229 
230  // convert between units.
231  c5 = angle([30, 15, 50], from="dms", to="r");
232  c6 = angle(0.528205, to="dms");
233 
234  // end_include
235 
236  echo( bu=bu );
237  echo( du=du );
238  echo( );
239  echo( c1=c1 );
240  echo( c2=c2 );
241  echo( c3=c3 );
242  echo( c4=c4 );
243  echo( c5=c5 );
244  echo( c6=c6 );
245  END_OPENSCAD;
246 
247  BEGIN_MFSCRIPT;
248  include --path "${INCLUDE_PATH}" {var_init,var_gen_term}.mfs;
249 
250  defines name "units" define "angle_unit_base" strings "r d dms";
251  variables add_opts_combine "units";
252 
253  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
254  END_MFSCRIPT;
255 END_SCOPE;
256 */
257 
258 //----------------------------------------------------------------------------//
259 // end of file
260 //----------------------------------------------------------------------------//
tau
<decimal> The ratio of a circle's circumference to its radius.
Definition: constants.scad:201
function angle(a, from=angle_unit_default, to=angle_unit_base)
Convert an angle from some units to another.
angle_unit_default
<string> The default units when unspecified.
Definition: angle.scad:494
function a_rad(a)
Shorthand angle conversion for radians.
function angle_unit_name(u=angle_unit_default)
Return the name of an angle unit identifier.
angle_unit_base
<string> The base units for value storage.
Definition: angle.scad:491
function angle_inv(a, from=angle_unit_base, to=angle_unit_default)
Convert an angle from some units to another.
function a_deg(a)
Shorthand angle conversion for degrees.