00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef TG_SHAPE_H
00019 #define TG_SHAPE_H
00020
00021 #include "Util2D.h"
00022
00023 namespace tagGame
00024 {
00025 class Circle;
00026 class Side;
00027 class Shape;
00028
00029 typedef SharedPtr<Circle>::type CirclePtr;
00030 typedef SharedPtr<Side>::type SidePtr;
00031 typedef SharedPtr<Shape>::type ShapePtr;
00032
00033 template<typename Function, typename Allocator = std::allocator<void> >
00034 class function;
00035
00037 class Shape
00038 {
00039 public:
00040 inline Shape();
00041 inline virtual ~Shape();
00042
00043 inline RealVec const& getPosition() const;
00044 inline void setPosition(RealVec const& position);
00045
00046 inline RealVec const& getOrientation() const;
00047 inline void setOrientation(RealVec const& orientation);
00048
00049 virtual std::ostream& output(std::ostream& out) const = 0;
00050
00051 inline bool isTouching(Shape const& o) const;
00052
00055 virtual RealVec nearestIntersection(RealVec const& p, RealVec const& v) const = 0;
00056
00058 virtual RealVec normalTo(Shape const& o) const = 0;
00059 virtual RealVec normalTo(Circle const& c) const = 0;
00060
00061
00062
00063
00064
00065
00066
00067 virtual Real distanceTo(Shape const& o) const = 0;
00068 virtual Real distanceTo(Circle const& c) const = 0;
00069 virtual Real distanceTo(Side const& s) const = 0;
00070
00071 inline Real distanceSquaredTo(Shape const& o) const;
00072
00073 protected:
00074 private:
00075 RealVec position;
00076 RealVec orientation;
00077 };
00078
00079 typedef std::vector<ShapePtr> ShapeList;
00080 typedef ShapeList::const_iterator ShapeIteratorConst;
00081 typedef ShapeList::iterator ShapeIterator;
00082
00083 Shape::Shape() :
00084 position(Util2D::dim),
00085 orientation(Util2D::dim)
00086 {
00087 orientation[0] = 1;
00088 }
00089
00090 Shape::~Shape()
00091 {
00092 }
00093
00094 inline std::ostream& operator<<(std::ostream& out, Shape const& o)
00095 {
00096 return o.output(out);
00097 }
00098
00099 RealVec const& Shape::getPosition() const
00100 {
00101 return position;
00102 }
00103
00104 void Shape::setPosition(RealVec const& position)
00105 {
00106 this->position = position;
00107 }
00108
00109 RealVec const& Shape::getOrientation() const
00110 {
00111 return orientation;
00112 }
00113
00114 void Shape::setOrientation(RealVec const& orientation)
00115 {
00116 TG_ASSERT(MathUtil::isAlmostEq(1, orientation.length()));
00117
00118 this->orientation = orientation;
00119 }
00120
00121 bool Shape::isTouching(Shape const& o) const
00122 {
00123 return distanceTo(o) <= Real(0);
00124 }
00125
00126
00127
00128
00129
00130 Real Shape::distanceSquaredTo(Shape const& o) const
00131 {
00132 Real const d(distanceTo(o));
00133
00134 return d * d;
00135 }
00136
00137 }
00138
00139 #endif