WFMath 1.0.2
shape.h
1// shape.h (A general base class for shapes)
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., 675 Mass Ave, Cambridge, MA 02139, 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// This class borrows heavily from the base shape class in libCoal,
27// plus certain intersection ideas from stage/shepherd/sylvanus
28
29
30#ifndef WFMATH_SHAPE_H
31#define WFMATH_SHAPE_H
32
33#include <wfmath/vector.h>
34#include <wfmath/point.h>
35#include <wfmath/const.h>
36#include <wfmath/rotmatrix.h>
37#include <wfmath/axisbox.h>
38#include <wfmath/ball.h>
39#include <wfmath/intersect_decls.h>
40
41namespace WFMath {
42
44
55template<const int dim>
56class Shape
57{
58 public:
59 // The first things in the Shape class are the functions required
60 // by CLASS_LAYOUT for all classes
61
63 Shape() {}
65 Shape(const Shape<dim>& s) {}
67 ~Shape() {}
68
70 friend std::ostream& operator<< <dim>(std::ostream& os, const Shape& s);
72 friend std::istream& operator>> <dim>(std::istream& is, Shape& s);
73
75 Shape& operator=(const Shape& a);
76
78 bool isEqualTo(const Shape& s, CoordType tolerance = numeric_constants<CoordType>::epsilon()) const;
80 bool operator==(const Shape& s) const {return isEqualTo(s);}
82 bool operator!=(const Shape& s) const {return !isEqualTo(s);}
83
85 bool isValid() const {return m_valid;}
86
87 // Now we begin with the functions in the shape interface
88
89 // Descriptive characteristics
90
92
95 size_t numCorners() const; // The number of corners, returns zero for Ball<>
97 Point<dim> getCorner(size_t i) const; // Must have i >= 0 && i < numCorners()
99 Point<dim> getCenter() const; // Returns the barycenter of the object
100
101 // Movement functions
102
104 Shape& shift(const Vector<dim>& v); // Move the shape a certain distance
106
109 Shape& moveCornerTo(const Point<dim>& p, size_t corner)
110 {return shift(p - getCorner(corner));}
112
116 {return shift(p - getCenter());}
117
118
120
123 Shape& rotateCorner(const RotMatrix<dim>& m, size_t corner)
124 {return rotatePoint(m, getCorner(corner));}
126
130 {return rotatePoint(m, getCenter());}
132
137
138 // Intersection functions
139
146
154
156
164 friend bool Intersect<dim>(Shape<dim>& s1, Shape<dim>& s2, bool proper);
166
180 friend bool Contains<dim>(Shape<dim>& s1, Shape<dim>& s2, bool proper);
181
182 private:
183 bool m_valid;
184};
185
186//#include<wfmath/shape_funcs.h>
187
188} // namespace WFMath
189
190#endif // WFMATH_SHAPE_H
A dim dimensional axis-aligned box.
Definition const.h:48
A dim dimensional ball.
Definition const.h:49
A dim dimensional point.
Definition point.h:96
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition rotmatrix.h:87
A fake class which documents the generic parts of the WFMath interface.
Definition shape.h:57
Shape & rotatePoint(const RotMatrix< dim > &m, const Point< dim > &p)
shape: rotate the shape while holding the Point p fixed.
Shape & moveCenterTo(const Point< dim > &p)
shape: move the shape, moving the center to the Point p
Definition shape.h:115
Point< dim > getCorner(size_t i) const
shape: return the position of the i'th corner, where 0 <= i < numCorners()
Point< dim > getCenter() const
shape: return the position of the center of the shape
bool isEqualTo(const Shape &s, CoordType tolerance=numeric_constants< CoordType >::epsilon()) const
generic: check if two classes are equal, up to a given tolerance
friend bool Contains(Shape< dim > &s1, Shape< dim > &s2, bool proper)
shape: Returns true if the first shape contains the second.
Shape & rotateCenter(const RotMatrix< dim > &m)
shape: rotate the shape while holding the center fixed
Definition shape.h:129
AxisBox< dim > boundingBox() const
shape: return the minimal axis-aligned bounding box
friend bool Intersect(Shape< dim > &s1, Shape< dim > &s2, bool proper)
shape: Returns true if the two shapes intersect.
bool operator!=(const Shape &s) const
generic: check if two classes are not equal, up to tolerance WFMATH_EPSILON
Definition shape.h:82
Shape & moveCornerTo(const Point< dim > &p, size_t corner)
shape: move the shape, moving the given corner to the Point p
Definition shape.h:109
bool isValid() const
generic: returns true if the class instance has been initialized
Definition shape.h:85
Shape & shift(const Vector< dim > &v)
shape: move the shape by an amount given by the Vector v
Ball< dim > boundingSphere() const
shape: return the minimal bounding sphere
bool operator==(const Shape &s) const
generic: check if two classes are equal, up to tolerance WFMATH_EPSILON
Definition shape.h:80
Ball< dim > boundingSphereSloppy() const
size_t numCorners() const
shape: return the number of corners in the shape.
Shape & rotateCorner(const RotMatrix< dim > &m, size_t corner)
shape: rotate the shape while holding the given corner fixed
Definition shape.h:123
A dim dimensional vector.
Definition vector.h:121
Generic library namespace.
Definition atlasconv.h:45
float CoordType
Basic floating point type.
Definition const.h:140