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
39namespace WFMath {
40
41template<>
42Point<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
50template<>
51void 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
59template<>
60Point<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
69template<>
70void 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
79template<>
80Point<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
88template<>
89void 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
99template<>
100Point<3>& Point<3>::rotate(const Quaternion& q, const Point<3>& p)
101{
102 return (*this = p + (*this - p).rotate(q));
103}
104
105template<>
106Point<3>& Point<3>::rotatePoint(const Quaternion& q, const Point<3>& p)
107{
108 return rotate(q, p);
109}
110
111template<>
112Point<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
118template<>
119Point<3> Point<3>::toParentCoords(const Point<3>& origin,
120 const Quaternion& rotation) const
121{
122 return origin + (*this - Point().setToOrigin()).rotate(rotation);
123}
124
125template class Point<3>;
126template class Point<2>;
127
128static_assert(std::is_standard_layout<Point<3>>::value, "Point should be standard layout.");
129static_assert(std::is_trivially_copyable<Point<3>>::value, "Point should be trivially copyable.");
130
131static_assert(std::is_standard_layout<Point<2>>::value, "Point should be standard layout.");
132static_assert(std::is_trivially_copyable<Point<2>>::value, "Point should be trivially copyable.");
133
134template CoordType SquaredDistance<3>(const Point<3> &, const Point<3> &);
135template CoordType SquaredDistance<2>(const Point<2> &, const Point<2> &);
136
137template Point<3> Midpoint<3>(const Point<3> &, const Point<3> &, CoordType);
138template Point<2> Midpoint<2>(const Point<2> &, const Point<2> &, CoordType);
139
140template Point<3> Barycenter<3, std::vector>(const std::vector<Point<3> > &);
141template Point<3> Barycenter<3, std::vector, std::list>(const std::vector<Point<3> > &, const std::list<CoordType> &);
142
143template Point<2> Barycenter<2, std::vector>(const std::vector<Point<2> > &);
144template Point<2> Barycenter<2, std::vector, std::list>(const std::vector<Point<2> > &, const std::list<CoordType> &);
145
146template Vector<3> operator-<3>(const Point<3> &, const Point<3> &);
147template Vector<2> operator-<2>(const Point<2> &, const Point<2> &);
148
149template Point<3> operator-<3>(const Point<3> &, const Vector<3> &);
150template Point<2> operator-<2>(const Point<2> &, const Vector<2> &);
151
152template Point<3>& operator-=<3>(Point<3> &, const Vector<3> &);
153template Point<2>& operator-=<2>(Point<2> &, const Vector<2> &);
154
155template Point<3> operator+<3>(const Vector<3> &, const Point<3> &);
156template Point<2> operator+<2>(const Vector<2> &, const Point<2> &);
157
158template Point<3> operator+<3>(const Point<3> &, const Vector<3> &);
159template Point<2> operator+<2>(const Point<2> &, const Vector<2> &);
160
161template Point<3>& operator+=<3>(Point<3> &, const Vector<3> &);
162template Point<2>& operator+=<2>(Point<2> &, const Vector<2> &);
163
164} // namespace WFMath
Point & rotate(const RotMatrix< dim > &m, const Point &p)
Rotate about point p.
Definition: point.h:146
Point & spherical(CoordType r, CoordType theta, CoordType phi)
3D only: construct a vector from spherical coordinates
void asSpherical(CoordType &r, CoordType &theta, CoordType &phi) const
3D only: convert a vector to spherical coordinates
void asPolar(CoordType &r, CoordType &theta) const
2D only: convert a vector to polar coordinates
Point & polar(CoordType r, CoordType theta)
2D only: construct a vector from polar coordinates
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140