1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// shape.h (A general base class for shapes)
//
// The WorldForge Project
// Copyright (C) 2001 The WorldForge Project
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// For information about WorldForge and its authors, please contact
// the Worldforge Web Site at http://www.worldforge.org.
//
// Author: Ron Steinke
// This class borrows heavily from the base shape class in libCoal,
// plus certain intersection ideas from stage/shepherd/sylvanus
#ifndef WFMATH_SHAPE_H
#define WFMATH_SHAPE_H
#include <wfmath/vector.h>
#include <wfmath/point.h>
#include <wfmath/const.h>
#include <wfmath/rotmatrix.h>
#include <wfmath/axisbox.h>
#include <wfmath/ball.h>
#include <wfmath/intersect_decls.h>
namespace WFMath {
/// A fake class which documents the generic parts of the WFMath interface
/**
* This fake class documents two parts of the WFMath generic
* class interface. With a few exceptions (e.g. classes derived
* from std::exception), every class in WFMath implements
* the part of the interface labeled as 'generic'. The 'shape'
* interface is implemented by several classes, which identify
* themselves in their own documentation. Every class which
* implements the 'shape' interface also implements the 'generic'
* interface. Classes will not generally document their
* generic and shape interface functions.
**/
template<const int dim>
class Shape
{
public:
// The first things in the Shape class are the functions required
// by CLASS_LAYOUT for all classes
///
Shape() {}
///
Shape(const Shape<dim>& s) {}
///
~Shape() {}
/// generic: Print an instance to a stream
friend std::ostream& operator<< <dim>(std::ostream& os, const Shape& s);
/// generic: Parse an instance from a stream
friend std::istream& operator>> <dim>(std::istream& is, Shape& s);
///
Shape& operator=(const Shape& a);
/// generic: check if two classes are equal, up to a given tolerance
bool isEqualTo(const Shape& s, double tolerance = WFMATH_EPSILON) const;
/// generic: check if two classes are equal, up to tolerance WFMATH_EPSILON
bool operator==(const Shape& s) const {return isEqualTo(s);}
/// generic: check if two classes are not equal, up to tolerance WFMATH_EPSILON
bool operator!=(const Shape& s) const {return !isEqualTo(s);}
/// generic: returns true if the class instance has been initialized
bool isValid() const {return m_valid;}
// Now we begin with the functions in the shape interface
// Descriptive characteristics
/// shape: return the number of corners in the shape.
/**
* For many shape classes, this is a fixed constant
**/
int numCorners() const; // The number of corners, returns zero for Ball<>
/// shape: return the position of the i'th corner, where 0 <= i < numCorners()
Point<dim> getCorner(int i) const; // Must have i >= 0 && i < numCorners()
/// shape: return the position of the center of the shape
Point<dim> getCenter() const; // Returns the barycenter of the object
// Movement functions
/// shape: move the shape by an amount given by the Vector v
Shape& shift(const Vector<dim>& v); // Move the shape a certain distance
/// shape: move the shape, moving the given corner to the Point p
/**
* The corner in question is getCorner(corner).
**/
Shape& moveCornerTo(const Point<dim>& p, int corner)
{return shift(p - getCorner(corner));}
/// shape: move the shape, moving the center to the Point p
/**
* The center is defined by getCenter()
**/
Shape& moveCenterTo(const Point<dim>& p)
{return shift(p - getCenter());}
/// shape: rotate the shape while holding the given corner fixed
/**
* The corner in question is getCorner(corner).
**/
Shape& rotateCorner(const RotMatrix<dim>& m, int corner)
{return rotatePoint(m, getCorner(corner));}
/// shape: rotate the shape while holding the center fixed
/**
* The center is defined by getCenter()
**/
Shape& rotateCenter(const RotMatrix<dim>& m)
{return rotatePoint(m, getCenter());}
/// shape: rotate the shape while holding the Point p fixed.
/**
* Note that p can be any Point, it does not have to lie within
* the shape.
**/
Shape& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p);
// Intersection functions
/// shape: return the minimal axis-aligned bounding box
AxisBox<dim> boundingBox() const;
/// shape: return the minimal bounding sphere
Ball<dim> boundingSphere() const;
/// shape: return an approximate bounding sphere, guaranteed
/// to contain the minimal bounding sphere
/**
* boundingSphereSloppy() uses
* SloppyDistance() instead of Distance() to calculate it's
* radius, except in cases like Point<> and Ball<> where it
* would be silly. Thus, the result of boundingSphereSloppy()
* is guaranteed to contain the result of boundingSphere().
**/
Ball<dim> boundingSphereSloppy() const;
/// shape: Returns true if the two shapes intersect.
/**
* If the 'proper' argument is true, shapes which only touch on their
* boundary do not count as intersecting. If it is false, they do.
* This function is symmetric in its first two arguments
* (Intersect(a, b, proper) == Intersect(b, a, proper)).
* The two shapes do
* not have to be the same class, but must have the same dimension.
**/
friend bool Intersect<dim>(Shape<dim>& s1, Shape<dim>& s2, bool proper);
/// shape: Returns true if the first shape contains the second.
/**
* If the 'proper' argument is true, the inner shape is not contained
* if it touches the boundary of the outer shape. Otherwise, it
* does. Therefore, any shape contains itself
* (Contains(foo, foo, false) == true), but no shape contains itself
* properly (Contains(foo, foo, true) == false). Because of this,
* an empty shape (e.g. a Polygon with zero corners)
* is properly contained by any other shape. A Point, or any single
* point shape (e.g. a Segment where the endpoints are equal)
* properly contains an empty shape, and contains (but not properly)
* any other single point shape which occupies the same point.
* The two shapes do
* not have to be the same class, but must have the same dimension.
**/
friend bool Contains<dim>(Shape<dim>& s1, Shape<dim>& s2, bool proper);
private:
bool m_valid;
};
//#include<wfmath/shape_funcs.h>
} // namespace WFMath
#endif // WFMATH_SHAPE_H
|