wfmath  1.0.3
A math library for the Worldforge system.
basis.h
1 // basis.h (Cartesian/polar/spherical conversion for the WFMath library)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2001 The WorldForge Project
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // For information about WorldForge and its authors, please contact
21 // the Worldforge Web Site at http://www.worldforge.org.
22 
23 // Author: Ron Steinke
24 // Created: 2001-12-15
25 
26 #ifndef WFMATH_BASIS_H
27 #define WFMATH_BASIS_H
28 
29 #include <wfmath/const.h>
30 
31 #include <cmath>
32 
33 namespace WFMath {
34 
35 // These are used internally in point.cpp and vector.cpp. This header
36 // file is not included in any other, nor are these functions exported,
37 // so we don't install this in $(includedir)/wfmath.
38 
39 // Expects (r, theta) for polar, (x, y) for cart
40 inline void _CartToPolar(const CoordType *in, CoordType *out)
41 {
42  out[0] = std::sqrt(in[0] * in[0] + in[1] * in[1]);
43  out[1] = std::atan2(in[0], in[1]);
44 }
45 
46 // Expects (r, theta) for polar, (x, y) for cart
47 inline void _PolarToCart(const CoordType *in, CoordType *out)
48 {
49  out[0] = in[0] * std::cos(in[1]);
50  out[1] = in[0] * std::sin(in[1]);
51 }
52 
53 // Expects (r, theta, phi) for spherical, (x, y, z) for cart
54 inline void _CartToSpherical(const CoordType *in, CoordType *out)
55 {
56  out[0] = std::sqrt(in[0] * in[0] + in[1] * in[1] + in[2] * in[2]);
57  out[1] = std::atan2(in[2], std::sqrt(in[0] * in[0] + in[1] * in[1]));
58  out[2] = std::atan2(in[0], in[1]);
59 }
60 
61 // Expects (r, theta, phi) for spherical, (x, y, z) for cart
62 inline void _SphericalToCart(const CoordType *in, CoordType *out)
63 {
64  CoordType stheta = std::sin(in[1]);
65 
66  out[0] = in[0] * stheta * std::cos(in[2]);
67  out[1] = in[0] * stheta * std::sin(in[2]);
68  out[2] = in[0] * std::cos(in[1]);
69 }
70 
71 } // namespace WFMath
72 
73 #endif // WFMATH_BASIS_H
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140