wfmath 1.0.3
A math library for the Worldforge system.
rotbox.h
1// rotbox.h (A box with arbitrary orientation)
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#ifndef WFMATH_ROT_BOX_H
27#define WFMATH_ROT_BOX_H
28
29#include <wfmath/point.h>
30#include <wfmath/rotmatrix.h>
31#include <wfmath/intersect_decls.h>
32
33namespace WFMath {
34
35template<int dim>
36std::ostream& operator<<(std::ostream& os, const RotBox<dim>& r);
37template<int dim>
38std::istream& operator>>(std::istream& is, RotBox<dim>& r);
39
41
45template<int dim = 3>
46class RotBox
47{
48 public:
50 RotBox() : m_corner0{}, m_size{}, m_orient{} {}
52
58 const RotMatrix<dim>& orientation) : m_corner0(p), m_size(size),
59 m_orient(orientation) {}
61 RotBox(const RotBox& b) = default;
63 explicit RotBox(const AtlasInType& a);
64
65 ~RotBox() = default;
66
68 AtlasOutType toAtlas() const;
70 void fromAtlas(const AtlasInType& a);
71
72 friend std::ostream& operator<< <dim>(std::ostream& os, const RotBox& r);
73 friend std::istream& operator>> <dim>(std::istream& is, RotBox& r);
74
75 RotBox& operator=(const RotBox& s) = default;
76
77 bool isEqualTo(const RotBox& b, CoordType epsilon = numeric_constants<CoordType>::epsilon()) const;
78
79 bool operator==(const RotBox& b) const {return isEqualTo(b);}
80 bool operator!=(const RotBox& b) const {return !isEqualTo(b);}
81
82 bool isValid() const {return m_corner0.isValid() && m_size.isValid()
83 && m_orient.isValid();}
84
85 // Descriptive characteristics
86
87 size_t numCorners() const {return 1 << dim;}
88 Point<dim> getCorner(size_t i) const;
89 Point<dim> getCenter() const {return m_corner0 + Prod(m_size / 2, m_orient);}
90
92 const Point<dim>& corner0() const {return m_corner0;}
94 Point<dim>& corner0() {return m_corner0;}
96 const Vector<dim>& size() const {return m_size;}
98 Vector<dim>& size() {return m_size;}
100 const RotMatrix<dim>& orientation() const {return m_orient;}
102 RotMatrix<dim>& orientation() {return m_orient;}
103
104 // Movement functions
105
106 RotBox& shift(const Vector<dim>& v)
107 {m_corner0 += v; return *this;}
108 RotBox& moveCornerTo(const Point<dim>& p, size_t corner)
109 {return shift(p - getCorner(corner));}
110 RotBox& moveCenterTo(const Point<dim>& p)
111 {return shift(p - getCenter());}
112
113 RotBox& rotateCorner(const RotMatrix<dim>& m, size_t corner)
114 {rotatePoint(m, getCorner(corner)); return *this;}
115 RotBox& rotateCenter(const RotMatrix<dim>& m)
116 {rotatePoint(m, getCenter()); return *this;}
117 RotBox& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p)
118 {m_orient = Prod(m_orient, m); m_corner0.rotate(m, p); return *this;}
119
120 // 3D rotation functions
121 RotBox& rotateCorner(const Quaternion& q, size_t corner);
122 RotBox& rotateCenter(const Quaternion& q);
123 RotBox& rotatePoint(const Quaternion& q, const Point<dim>& p);
124
125 // Intersection functions
126
127 AxisBox<dim> boundingBox() const;
128 Ball<dim> boundingSphere() const
129 {return Ball<dim>(getCenter(), m_size.mag() / 2);}
130 Ball<dim> boundingSphereSloppy() const
131 {return Ball<dim>(getCenter(), m_size.sqrMag() / 2);}
132
133 RotBox toParentCoords(const Point<dim>& origin,
134 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
135 {return RotBox(m_corner0.toParentCoords(origin, rotation), m_size,
136 m_orient * rotation);}
137 RotBox toParentCoords(const AxisBox<dim>& coords) const
138 {return RotBox(m_corner0.toParentCoords(coords), m_size, m_orient);}
139 RotBox toParentCoords(const RotBox<dim>& coords) const
140 {return RotBox(m_corner0.toParentCoords(coords), m_size,
141 m_orient * coords.m_orient);}
142
143 // toLocal is just like toParent, expect we reverse the order of
144 // translation and rotation and use the opposite sense of the rotation
145 // matrix
146
147 RotBox toLocalCoords(const Point<dim>& origin,
148 const RotMatrix<dim>& rotation = RotMatrix<dim>().identity()) const
149 {return RotBox(m_corner0.toLocalCoords(origin, rotation), m_size,
150 rotation * m_orient);}
151 RotBox toLocalCoords(const AxisBox<dim>& coords) const
152 {return RotBox(m_corner0.toLocalCoords(coords), m_size, m_orient);}
153 RotBox toLocalCoords(const RotBox<dim>& coords) const
154 {return RotBox(m_corner0.toLocalCoords(coords), m_size,
155 coords.m_orient * m_orient);}
156
157 // 3D only
158 RotBox toParentCoords(const Point<dim>& origin, const Quaternion& rotation) const;
159 RotBox toLocalCoords(const Point<dim>& origin, const Quaternion& rotation) const;
160
161 friend bool Intersect<dim>(const RotBox& r, const Point<dim>& p, bool proper);
162 friend bool Contains<dim>(const Point<dim>& p, const RotBox& r, bool proper);
163
164 friend bool Intersect<dim>(const RotBox& r, const AxisBox<dim>& b, bool proper);
165 friend bool Contains<dim>(const RotBox& r, const AxisBox<dim>& b, bool proper);
166 friend bool Contains<dim>(const AxisBox<dim>& b, const RotBox& r, bool proper);
167
168 friend bool Intersect<dim>(const RotBox& r, const Ball<dim>& b, bool proper);
169 friend bool Contains<dim>(const RotBox& r, const Ball<dim>& b, bool proper);
170 friend bool Contains<dim>(const Ball<dim>& b, const RotBox& r, bool proper);
171
172 friend bool Intersect<dim>(const RotBox& r, const Segment<dim>& s, bool proper);
173 friend bool Contains<dim>(const RotBox& r, const Segment<dim>& s, bool proper);
174 friend bool Contains<dim>(const Segment<dim>& s, const RotBox& r, bool proper);
175
176 friend bool Intersect<dim>(const RotBox& r1, const RotBox& r2, bool proper);
177 friend bool Contains<dim>(const RotBox& outer, const RotBox& inner, bool proper);
178
179 friend bool Intersect<dim>(const Polygon<dim>& p, const RotBox& r, bool proper);
180 friend bool Contains<dim>(const Polygon<dim>& p, const RotBox& r, bool proper);
181 friend bool Contains<dim>(const RotBox& r, const Polygon<dim>& p, bool proper);
182
183 private:
184
185 Point<dim> m_corner0;
186 Vector<dim> m_size;
187 RotMatrix<dim> m_orient;
188};
189
190template<int dim>
191inline bool RotBox<dim>::isEqualTo(const RotBox<dim>& b, CoordType epsilon) const
192{
193 return Equal(m_corner0, b.m_corner0, epsilon)
194 && Equal(m_size, b.m_size, epsilon)
195 && Equal(m_orient, b.m_orient, epsilon);
196}
197
198} // namespace WFMath
199
200#endif // WFMATH_ROT_BOX_H
Point & rotate(const RotMatrix< dim > &m, const Point &p)
Rotate about point p.
Definition: point.h:146
A dim dimensional box, lying at an arbitrary angle.
Definition: rotbox.h:47
RotBox()
construct an uninitialized box
Definition: rotbox.h:50
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:452
RotBox(const Point< dim > &p, const Vector< dim > &size, const RotMatrix< dim > &orientation)
construct a box from the given parameters
Definition: rotbox.h:57
Point< dim > & corner0()
returns the base corner of the box
Definition: rotbox.h:94
RotMatrix< dim > & orientation()
returns the orientation of the box
Definition: rotbox.h:102
RotBox(const RotBox &b)=default
construct a copy of the box
const Vector< dim > & size() const
returns the size of the box
Definition: rotbox.h:96
const RotMatrix< dim > & orientation() const
returns the orientation of the box
Definition: rotbox.h:100
const Point< dim > & corner0() const
returns the base corner of the box
Definition: rotbox.h:92
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:480
Vector< dim > & size()
returns the size of the box
Definition: rotbox.h:98
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition: rotmatrix.h:87
CoordType mag() const
The magnitude of a vector.
Definition: vector.h:217
CoordType sqrMag() const
The squared magnitude of a vector.
Definition: vector_funcs.h:220
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140
RotMatrix< dim > Prod(const RotMatrix< dim > &m1, const RotMatrix< dim > &m2)
returns m1 * m2