wfmath  1.0.3
A math library for the Worldforge system.
point.cpp
1 // point.cpp (Point<> backend)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2000, 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 
24 // Author: Ron Steinke
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include "point_funcs.h"
31 #include "axisbox_funcs.h"
32 
33 #include "basis.h"
34 #include "quaternion.h"
35 
36 #include <vector>
37 #include <list>
38 
39 namespace WFMath {
40 
41 template<>
42 Point<2>& Point<2>::polar(CoordType r, CoordType theta)
43 {
44  CoordType d[2] = {r, theta};
45  _PolarToCart(d, m_elem);
46  m_valid = true;
47  return *this;
48 }
49 
50 template<>
51 void Point<2>::asPolar(CoordType& r, CoordType& theta) const
52 {
53  CoordType d[2];
54  _CartToPolar(m_elem, d);
55  r = d[0];
56  theta = d[1];
57 }
58 
59 template<>
60 Point<3>& Point<3>::polar(CoordType r, CoordType theta, CoordType z)
61 {
62  CoordType d[2] = {r, theta};
63  _PolarToCart(d, m_elem);
64  m_elem[2] = z;
65  m_valid = true;
66  return *this;
67 }
68 
69 template<>
70 void Point<3>::asPolar(CoordType& r, CoordType& theta, CoordType& z) const
71 {
72  CoordType d[2];
73  _CartToPolar(m_elem, d);
74  r = d[0];
75  theta = d[1];
76  z = m_elem[2];
77 }
78 
79 template<>
80 Point<3>& Point<3>::spherical(CoordType r, CoordType theta, CoordType phi)
81 {
82  CoordType d[3] = {r, theta, phi};
83  _SphericalToCart(d, m_elem);
84  m_valid = true;
85  return *this;
86 }
87 
88 template<>
89 void Point<3>::asSpherical(CoordType& r, CoordType& theta,
90  CoordType& phi) const
91 {
92  CoordType d[3];
93  _CartToSpherical(m_elem, d);
94  r = d[0];
95  theta = d[1];
96  phi = d[2];
97 }
98 
99 template<>
100 Point<3>& Point<3>::rotate(const Quaternion& q, const Point<3>& p)
101 {
102  return (*this = p + (*this - p).rotate(q));
103 }
104 
105 template<>
106 Point<3>& Point<3>::rotatePoint(const Quaternion& q, const Point<3>& p)
107 {
108  return rotate(q, p);
109 }
110 
111 template<>
112 Point<3> Point<3>::toLocalCoords(const Point<3>& origin,
113  const Quaternion& rotation) const
114 {
115  return Point().setToOrigin() + (*this - origin).rotate(rotation.inverse());
116 }
117 
118 template<>
119 Point<3> Point<3>::toParentCoords(const Point<3>& origin,
120  const Quaternion& rotation) const
121 {
122  return origin + (*this - Point().setToOrigin()).rotate(rotation);
123 }
124 
125 template class Point<3>;
126 template class Point<2>;
127 
128 static_assert(std::is_standard_layout<Point<3>>::value, "Point should be standard layout.");
129 static_assert(std::is_trivially_copyable<Point<3>>::value, "Point should be trivially copyable.");
130 
131 static_assert(std::is_standard_layout<Point<2>>::value, "Point should be standard layout.");
132 static_assert(std::is_trivially_copyable<Point<2>>::value, "Point should be trivially copyable.");
133 
134 template CoordType SquaredDistance<3>(const Point<3> &, const Point<3> &);
135 template CoordType SquaredDistance<2>(const Point<2> &, const Point<2> &);
136 
137 template Point<3> Midpoint<3>(const Point<3> &, const Point<3> &, CoordType);
138 template Point<2> Midpoint<2>(const Point<2> &, const Point<2> &, CoordType);
139 
140 template Point<3> Barycenter<3, std::vector>(const std::vector<Point<3> > &);
141 template Point<3> Barycenter<3, std::vector, std::list>(const std::vector<Point<3> > &, const std::list<CoordType> &);
142 
143 template Point<2> Barycenter<2, std::vector>(const std::vector<Point<2> > &);
144 template Point<2> Barycenter<2, std::vector, std::list>(const std::vector<Point<2> > &, const std::list<CoordType> &);
145 
146 template Vector<3> operator-<3>(const Point<3> &, const Point<3> &);
147 template Vector<2> operator-<2>(const Point<2> &, const Point<2> &);
148 
149 template Point<3> operator-<3>(const Point<3> &, const Vector<3> &);
150 template Point<2> operator-<2>(const Point<2> &, const Vector<2> &);
151 
152 template Point<3>& operator-=<3>(Point<3> &, const Vector<3> &);
153 template Point<2>& operator-=<2>(Point<2> &, const Vector<2> &);
154 
155 template Point<3> operator+<3>(const Vector<3> &, const Point<3> &);
156 template Point<2> operator+<2>(const Vector<2> &, const Point<2> &);
157 
158 template Point<3> operator+<3>(const Point<3> &, const Vector<3> &);
159 template Point<2> operator+<2>(const Point<2> &, const Vector<2> &);
160 
161 template Point<3>& operator+=<3>(Point<3> &, const Vector<3> &);
162 template Point<2>& operator+=<2>(Point<2> &, const Vector<2> &);
163 
164 } // namespace WFMath
WFMath::Point::polar
Point & polar(CoordType r, CoordType theta)
2D only: construct a vector from polar coordinates
WFMath::Point< 3 >::rotate
Point & rotate(const RotMatrix< dim > &m, const Point &p)
Rotate about point p.
Definition: point.h:146
WFMath
Generic library namespace.
Definition: shape.h:41
WFMath::CoordType
double CoordType
Basic floating point type.
Definition: const.h:140
WFMath::Point< 2 >::asPolar
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
WFMath::Point< 3 >::spherical
Point & spherical(CoordType r, CoordType theta, CoordType phi)
3D only: construct a vector from spherical coordinates
WFMath::Point< 3 >::asSpherical
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to spherical coordinates