omdl  v0.5
OpenSCAD Mechanical Design Library
units_angle.scad
Go to the documentation of this file.
1 //! Angle units and conversions.
2 /***************************************************************************//**
3  \file units_angle.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_angle
33 *******************************************************************************/
34 
35 include <constants.scad>;
36 
37 //----------------------------------------------------------------------------//
38 /***************************************************************************//**
39  \addtogroup units
40  @{
41 
42  \defgroup units_angle Angle
43  \brief Angle units and conversions.
44 
45  \details
46 
47  These functions allow for angles to be specified with units.
48  Angles specified with units are independent of (\ref base_unit_angle).
49  There are also unit conversion functions for converting from one unit
50  to another.
51 
52  The table below enumerates the supported unit identifiers and their
53  descriptions.
54 
55  units id | description
56  :---------:|:----------------------:
57  r | radian
58  d | degree
59  dms | degree, minute, second
60 
61  \b Example
62 
63  \dontinclude units_angle_example.scad
64  \skip include
65  \until to="dms");
66 
67  \b Result (base_angle_length = \b r): \include units_angle_example_r.log
68  \b Result (base_angle_length = \b d): \include units_angle_example_d.log
69  \b Result (base_angle_length = \b dms): \include units_angle_example_dms.log
70 
71  @{
72 *******************************************************************************/
73 //----------------------------------------------------------------------------//
74 
75 //! <string> Base unit for angle measurements.
77 
78 //! Return the name of the given angle \p unit identifier.
79 /***************************************************************************//**
80  \param units <string> An angle unit identifier.
81  \returns <string> The units name for the given angle unit identifier.
82  Returns \b 'undef' for identifiers that are not defined.
83 *******************************************************************************/
84 function unit_angle_name
85 (
86  units = base_unit_angle
87 ) = units == "r" ? "radian"
88  : units == "d" ? "degree"
89  : units == "dms" ? "degree, minute, second"
90  : undef;
91 
92 //! Convert the \p angle from degrees to \p to units.
93 /***************************************************************************//**
94  \param angle <decimal|vector> An angle to convert (dms angles are
95  3-tuple vector [d, m, s]).
96  \param to <string> The units to which the angle should be converted.
97  \returns <decimal|vector> The conversion result (dms angles are
98  3-tuple vector [d, m, s]). Returns \b 'undef' for identifiers
99  that are not defined.
100  \private
101 *******************************************************************************/
102 function unit_angle_d_to
103 (
104  angle,
105  to
106 ) = to == "r" ? ( angle * tau / 360 )
107  : to == "d" ? ( angle )
108  : to == "dms" ? ( [ floor(angle),
109  floor( ( angle - floor(angle) ) * 60 ),
110  ( angle
111  - floor(angle)
112  - floor( ( angle - floor(angle) ) * 60 ) / 60 ) * 3600
113  ] )
114  : undef;
115 
116 //! Convert the \p angle from \p from units to degrees.
117 /***************************************************************************//**
118  \param angle <decimal|vector> An angle to convert (dms angles are
119  3-tuple vector [d, m, s]).
120  \param from <string> The units of the angle to be converted.
121  \returns <decimal|vector> The conversion result (dms angles are
122  3-tuple vector [d, m, s]). Returns \b 'undef' for identifiers
123  that are not defined.
124  \private
125 *******************************************************************************/
126 function unit_angle_to_d
127 (
128  angle,
129  from
130 ) = from == "r" ? ( angle * 360 / tau )
131  : from == "d" ? ( angle )
132  : from == "dms" ? ( angle[0] + angle[1]/60 + angle[2]/3600 )
133  : undef;
134 
135 //! Convert the \p angle from \p from units to \p to units.
136 /***************************************************************************//**
137  \param angle <decimal|vector> An angle to convert (dms angles are
138  3-tuple vector [d, m, s]).
139  \param from <string> The units of the angle to be converted.
140  \param to <string> A units to which the angle should be converted.
141  \returns <decimal|vector> The conversion result (dms angles are
142  3-tuple vector [d, m, s]). Returns \b 'undef' for identifiers
143  that are not defined.
144 *******************************************************************************/
145 function convert_angle
146 (
147  angle,
148  from = base_unit_angle,
149  to = base_unit_angle
150 ) = unit_angle_d_to( unit_angle_to_d( angle, from ), to );
151 
152 //! @}
153 //! @}
154 
155 //----------------------------------------------------------------------------//
156 // openscad-amu auxiliary scripts
157 //----------------------------------------------------------------------------//
158 
159 /*
160 BEGIN_SCOPE example;
161  BEGIN_OPENSCAD;
162  include <units_angle.scad>;
163 
164  base_unit_angle = "d";
165 
166  // get base unit name
167  un = unit_angle_name();
168 
169  // absolute angle measurements in base unit.
170  c1 = convert_angle(pi/6, "r");
171  c2 = convert_angle(pi/4, "r");
172  c3 = convert_angle(180, "d");
173  c4 = convert_angle([30, 15, 50], "dms");
174 
175  // convert between units.
176  c5 = convert_angle([30, 15, 50], from="dms", to="r");
177  c6 = convert_angle(0.528205, from="r", to="dms");
178 
179  echo( un=un );
180  echo( c1=c1 );
181  echo( c2=c2 );
182  echo( c3=c3 );
183  echo( c4=c4 );
184  echo( c5=c5 );
185  echo( c6=c6 );
186  END_OPENSCAD;
187 
188  BEGIN_MFSCRIPT;
189  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
190 
191  defines name "units" define "base_unit_angle" strings "r d dms";
192  variables add_opts_combine "units";
193 
194  include --path "${INCLUDE_PATH}" script_std.mfs;
195  END_MFSCRIPT;
196 END_SCOPE;
197 */
198 
199 //----------------------------------------------------------------------------//
200 // end of file
201 //----------------------------------------------------------------------------//
tau
The ratio of a circle's circumference to its radius
Definition: constants.scad:53
base_unit_angle
Base unit for angle measurements.
function unit_angle_name(units=base_unit_angle)
Return the name of the given angle unit identifier.
function convert_angle(angle, from=base_unit_angle, to=base_unit_angle)
Convert the angle from from units to to units.