00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef VCG_PLANE3_H
00067 #define VCG_PLANE3_H
00068
00069 #include <vcg/space/point3.h>
00070
00071 namespace vcg {
00072
00082 template <class T, bool NORM=true> class Plane3 {
00083 public:
00084 typedef T ScalarType;
00085 typedef Point3<T> PointType;
00086
00087 private:
00089 ScalarType _offset;
00091 PointType _dir;
00092
00093 public:
00095
00097
00098 Plane3() {}
00100 Plane3(const ScalarType &dist, const PointType &dir) { Set(dist, dir); }
00101
00102 template <class Q>
00103 inline void Import( const Plane3<Q,false> & b )
00104 {
00105 _offset = ScalarType(b.Offset());
00106 _dir = Point3<T>::Construct(b.Direction());
00107 }
00108
00110
00115 const ScalarType &Offset() const { return _offset; }
00116 ScalarType &Offset() { return _offset; }
00118 void SetOffset( const ScalarType &o ) { _offset=o; }
00119
00120 const PointType &Direction() const { return _dir; }
00122 void SetDirection( const PointType & dir) {
00123 _dir=dir;
00124 if (NORM) _dir.Normalize();
00125 }
00127 void Set( const ScalarType & off, const PointType & dir ) {
00128 if (NORM) {
00129 const ScalarType normFactor = dir.Norm();
00130 this->_dir = dir / normFactor;
00131 this->_offset = off / normFactor;
00132 }
00133 else {
00134 this->_offset = off;
00135 this->_dir = dir;
00136 }
00137 }
00138 void Set( const PointType & dir, const ScalarType & off) {Set(off,dir);}
00139
00141 bool operator==(Plane3 const &p) const {
00142 return _offset == p._offset && _dir == p._dir;
00143 }
00145 bool operator!=(Plane3 const &p) const {
00146 return _offset != p._offset || _dir != p._dir;
00147 }
00148
00150 PointType Projection(const PointType &p) const {
00151 ScalarType k = p.dot(_dir) - _offset;
00152 return p - _dir * k;
00153 }
00154
00156 void Normalize() {
00157 _dir.Normalize();
00158 }
00159
00161 void Init(const PointType &p0, const PointType &p1, const PointType &p2) {
00162 _dir = (p2 - p0) ^ (p1 - p0);
00163 if(NORM) Normalize();
00164 _offset = p0.dot(_dir);
00165 }
00166
00168 inline void Init(const PointType &p0, const PointType &norm) {
00169 _dir = norm;
00170 _offset = p0.dot(_dir);
00171 }
00172 };
00173
00174 typedef Plane3<float> Plane3f;
00175 typedef Plane3<double> Plane3d;
00176
00178 template<class T> T Distance(const Plane3<T> & plane, const Point3<T> & point)
00179 {
00180 return plane.Direction().dot(point) - plane.Offset();
00181 }
00182
00183 template<class T> T SquaredDistance(const Plane3<T> & plane, const Point3<T> & point)
00184 {
00185 const T d = Distance(plane, point);
00186 return (d * d);
00187 }
00188
00189 template<class T> T Distance(const Point3<T> & point, const Plane3<T> & plane)
00190 {
00191 return Distance(plane, point);
00192 }
00193
00194 template<class T> T SquaredDistance(const Point3<T> & point, const Plane3<T> & plane)
00195 {
00196 return SquaredDistance(plane, point);
00197 }
00198
00199 }
00200
00201
00202 #endif