00001 #ifndef _PLANE_H_
00002 #define _PLANE_H_
00003
00004 #include "fv_float.h"
00005 #include "MathHelper.h"
00006
00007 #include <cstring>
00008
00009
00010
00011 namespace FemViewer {
00012
00013 class Plane {
00014
00015 public:
00016 typedef fvmath::Vec3d vec3;
00017 typedef fvmath::CVec3d cvec3;
00018 typedef fvmath::Vec4<CoordType> vec4;
00019
00020 static double DistanceToPlane(double x[3], double n[3], double p0[3]);
00021
00022 static double Evaluate(const double n[4],const double x[3]);
00023
00024 static int Check(const double n[4],const double x[3]);
00025
00026 static int IntersectWithLine(const double pn[4], double p2[3], double p1[3], double & u);
00027
00028 static int GetCuttedLine(double pn[4], double p2[3], double p1[3], double p0[3]);
00029
00030 static int CreatePlanesForPrism(const vec3 el_coords[6],vec4 el_planes[5]);
00031
00032 static int CreatePlanesForTetra(const vec3 el_coords[4],vec4 el_planes[4]);
00033
00034 static void Extract(const vec3* pt,const vec3* n,vec4* prams);
00035
00036 protected:
00037
00038 typedef union {
00039 struct { double a, b, c, d; };
00040 double n[4];
00041 } params;
00042
00043 params p;
00044
00045 public:
00046 Plane()
00047 {
00048 Reset();
00049 }
00050
00051 Plane(const Plane& rhs)
00052 {
00053 if (this != &rhs)
00054 {
00055 memcpy(&p,&rhs.p,sizeof(params));
00056 }
00057 }
00058
00059 void operator=(const Plane& rhs)
00060 {
00061 memcpy(p.n, rhs.p.n, sizeof(this->p));
00062 }
00063
00064 void Reset()
00065 {
00066 memset(p.n, 0x0, sizeof(p));
00067 }
00068
00069 virtual ~Plane() {}
00070
00071 virtual void SetPlane(double n[3], double p[3]);
00072
00073 double * GetParams() { return p.n; }
00074 const double * GetParams() const { return p.n; }
00075
00076 double EvaluatePlane(const double x1,const double x2,const double x3) const
00077 {
00078 return x1*p.a + x2*p.b + x3*p.c + p.d;
00079 }
00080
00081 inline double Calculate(const double *v) {
00082 return (p.a*v[0] + p.b*v[1] +p.c*v[2] + p.d);
00083 }
00084
00085 inline int CheckLocation(const double& x1,const double& x2,const double& x3) const
00086 {
00087 double res = EvaluatePlane(x1,x2,x3);
00088
00089
00090 if(fvmath::Compare(res,0.0)) return( 0);
00091 if(res < 0.0) return(-1);
00092 return( 1);
00093 }
00094
00095 inline int CheckLocation(const double x[]) const
00096 {
00097 double res = EvaluatePlane(x[0],x[1],x[2]);
00098 if(fvmath::Compare(res,0.0)) return( 0);
00099 if(res < 0.0) return(-1);
00100 return( 1);
00101 }
00102
00103
00104
00105 };
00106
00107 inline double Plane::Evaluate(const double n[4],const double x[3])
00108 {
00109 return (n[0]*x[0]+n[1]*x[1]+n[2]*x[2]+n[3]);
00110 }
00111
00112 inline double Plane::DistanceToPlane(double x[3], double n[3], double p0[3])
00113 {
00114 #define abs_val(x) ((x)>0 ? (x) : -(x))
00115 return abs_val( n[0]*(x[0]-p0[0]) + n[1]*(x[1]-p0[1]) + n[2]*(x[2]-p0[2]) );
00116 }
00117
00118 }
00119
00120 #endif
00121