WFMath
0.3.12
|
00001 // shape.h (A general base class for shapes) 00002 // 00003 // The WorldForge Project 00004 // Copyright (C) 2001 The WorldForge Project 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 // 00020 // For information about WorldForge and its authors, please contact 00021 // the Worldforge Web Site at http://www.worldforge.org. 00022 // 00023 00024 // Author: Ron Steinke 00025 00026 // This class borrows heavily from the base shape class in libCoal, 00027 // plus certain intersection ideas from stage/shepherd/sylvanus 00028 00029 00030 #ifndef WFMATH_SHAPE_H 00031 #define WFMATH_SHAPE_H 00032 00033 #include <wfmath/vector.h> 00034 #include <wfmath/point.h> 00035 #include <wfmath/const.h> 00036 #include <wfmath/rotmatrix.h> 00037 #include <wfmath/axisbox.h> 00038 #include <wfmath/ball.h> 00039 #include <wfmath/intersect_decls.h> 00040 00041 namespace WFMath { 00042 00044 00055 template<const int dim> 00056 class Shape 00057 { 00058 public: 00059 // The first things in the Shape class are the functions required 00060 // by CLASS_LAYOUT for all classes 00061 00063 Shape() {} 00065 Shape(const Shape<dim>& s) {} 00067 ~Shape() {} 00068 00070 friend std::ostream& operator<< <dim>(std::ostream& os, const Shape& s); 00072 friend std::istream& operator>> <dim>(std::istream& is, Shape& s); 00073 00075 Shape& operator=(const Shape& a); 00076 00078 bool isEqualTo(const Shape& s, double tolerance = WFMATH_EPSILON) const; 00080 bool operator==(const Shape& s) const {return isEqualTo(s);} 00082 bool operator!=(const Shape& s) const {return !isEqualTo(s);} 00083 00085 bool isValid() const {return m_valid;} 00086 00087 // Now we begin with the functions in the shape interface 00088 00089 // Descriptive characteristics 00090 00092 00095 int numCorners() const; // The number of corners, returns zero for Ball<> 00097 Point<dim> getCorner(int i) const; // Must have i >= 0 && i < numCorners() 00099 Point<dim> getCenter() const; // Returns the barycenter of the object 00100 00101 // Movement functions 00102 00104 Shape& shift(const Vector<dim>& v); // Move the shape a certain distance 00106 00109 Shape& moveCornerTo(const Point<dim>& p, int corner) 00110 {return shift(p - getCorner(corner));} 00112 00115 Shape& moveCenterTo(const Point<dim>& p) 00116 {return shift(p - getCenter());} 00117 00118 00120 00123 Shape& rotateCorner(const RotMatrix<dim>& m, int corner) 00124 {return rotatePoint(m, getCorner(corner));} 00126 00129 Shape& rotateCenter(const RotMatrix<dim>& m) 00130 {return rotatePoint(m, getCenter());} 00132 00136 Shape& rotatePoint(const RotMatrix<dim>& m, const Point<dim>& p); 00137 00138 // Intersection functions 00139 00141 AxisBox<dim> boundingBox() const; 00143 Ball<dim> boundingSphere() const; 00146 00153 Ball<dim> boundingSphereSloppy() const; 00154 00156 00164 friend bool Intersect<dim>(Shape<dim>& s1, Shape<dim>& s2, bool proper); 00166 00180 friend bool Contains<dim>(Shape<dim>& s1, Shape<dim>& s2, bool proper); 00181 00182 private: 00183 bool m_valid; 00184 }; 00185 00186 //#include<wfmath/shape_funcs.h> 00187 00188 } // namespace WFMath 00189 00190 #endif // WFMATH_SHAPE_H