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 #ifndef ACG_PLANE_HH
00054 #define ACG_PLANE_HH
00055
00056
00057
00058
00059
00060 #include "../../Math/VectorT.hh"
00061 #include "../../Math/Matrix4x4T.hh"
00062
00063
00064
00065
00066
00067 namespace ACG {
00068 namespace Geometry {
00069
00070
00071
00072
00073
00080 template <typename Scalar>
00081 class PlaneT
00082 {
00083 public:
00084
00086 typedef VectorT<Scalar, 3> Vec3;
00087 typedef VectorT<Scalar, 4> Vec4;
00088 typedef Matrix4x4T<Scalar> Mat4x4;
00089
00090
00092 PlaneT( Scalar _a=0, Scalar _b=0, Scalar _c=0, Scalar _d=0 )
00093 : coeffs_(_a, _b, _c, _d)
00094 { HNF(); }
00095
00096
00098 PlaneT( const Vec3& _o, const Vec3& _n )
00099 : coeffs_(_n[0], _n[1], _n[2], -(_n|_o))
00100 { HNF(); }
00101
00102
00104 PlaneT( const Vec3& _v0, const Vec3& _v1, const Vec3& _v2 )
00105 {
00106 Vec3 n = (_v1-_v0) % (_v2-_v0);
00107 coeffs_ = Vec4(n[0], n[1], n[2], -(n|_v0));
00108 HNF();
00109 }
00110
00111
00113 Vec3 normal() const { return Vec3(coeffs_[0], coeffs_[1], coeffs_[2]); }
00114
00115
00117 const Vec4& coeffs() const { return coeffs_; }
00118
00119
00121 Scalar distance( const Vec3& _v ) const
00122 {
00123 return ( _v[0]*coeffs_[0] +
00124 _v[1]*coeffs_[1] +
00125 _v[2]*coeffs_[2] +
00126 coeffs_[3] );
00127 }
00128
00129
00131 bool operator() ( const Vec3& _v ) const { return distance(_v) > 0; }
00132
00133
00134
00135 enum IntersectionTarget { Line, LineSegment, Ray };
00136
00137
00138 bool intersect_line( const Vec3& _v0, const Vec3& _v1,
00139 Vec3& _v, Scalar& _t ) const
00140 { return intersect(_v0, _v1, _v, _t, Line); }
00141
00142
00143 bool intersect_ray( const Vec3& _v0, const Vec3& _v1,
00144 Vec3& _v, Scalar& _t ) const
00145 { return intersect(_v0, _v1, _v, _t, Ray); }
00146
00147
00148 bool intersect_linesegment( const Vec3& _v0, const Vec3& _v1,
00149 Vec3& _v, Scalar& _t ) const
00150 { return intersect(_v0, _v1, _v, _t, LineSegment); }
00151
00153 bool intersect( const Vec3& _v0,
00154 const Vec3& _v1,
00155 Vec3& _v,
00156 Scalar& _t,
00157 IntersectionTarget _target ) const
00158 {
00159 #define SGN(d) ((d>0.0) ? 1 : -1)
00160
00161 Scalar d0(distance(_v0)), d1(distance(_v1));
00162 Scalar a0(fabs(d0)), a1(fabs(d1));
00163
00164
00165 if (a0 < FLT_MIN) { _v = _v0; _t = 0.0; return true; }
00166 if (a1 < FLT_MIN) { _v = _v1; _t = 1.0; return true; }
00167
00168
00169 if (SGN(d0) != SGN(d1))
00170 {
00171 _t = (a0/(a0+a1));
00172 _v = _v0*(1.0-_t) + _v1*_t;
00173 return true;
00174 }
00175
00176
00177 else
00178 {
00179 if (_target == LineSegment) return false;
00180
00181 if (fabs(d0-d1) < FLT_MIN) return false;
00182 else _t = d0/(d0-d1);
00183
00184 if (_target == Ray && _t < 0.0) return false;
00185
00186 _v = _v0*(1.0-_t) + _v1*_t;
00187 return true;
00188 }
00189 #undef SGN
00190 }
00191
00192
00194 bool affine_transformation( const Mat4x4& _M )
00195 {
00196 Mat4x4 M(_M);
00197 if (!M.invert()) return false;
00198 M.transpone();
00199 affineTransformation_precomp(M);
00200 return true;
00201 }
00202
00203
00205 void affine_transformation_precomp( const Mat4x4& _M_inverseTransposed )
00206 {
00207 coeffs_ = _M_inverseTransposed*coeffs_;
00208 HNF();
00209 }
00210
00211
00212 private:
00213
00214 void HNF() {
00215 Scalar n = normal().norm();
00216 if (n != 0.0) coeffs_ /= n;
00217 }
00218
00219 Vec4 coeffs_;
00220 };
00221
00222
00223
00224 typedef PlaneT<float> Planef;
00225 typedef PlaneT<double> Planed;
00226
00227
00228
00229 }
00230 }
00231
00232 #endif // ACG_PLANE_HH defined
00233
00234