00001 #pragma once
00002
00003 #ifndef __v_plane_h
00004 #define __v_plane_h
00005
00006 #include "VVector.h"
00007 #include "float.h"
00008
00009 static float EPSILON = FLT_EPSILON;
00014 class VPlane
00015 {
00016 public:
00017 float & nx;
00018 float & ny;
00019 float & nz;
00020 float & d;
00021
00022 float values[4];
00023
00024 VVector mNormal;
00025
00029 VPlane() : nx(values[0]), ny(values[1]), nz(values[2]), d(values[3])
00030 {
00031 values[0] = 0.0f;
00032 values[1] = 0.0f;
00033 values[2] = 0.0f;
00034 values[3] = 0.0f;
00035
00036 mNormal = VVector(nx, ny, nz);
00037 }
00038
00046 VPlane(float n_x, float n_y, float n_z, float _d) : nx(values[0]), ny(values[1]), nz(values[2]), d(values[3])
00047 {
00048 values[0] = n_x;
00049 values[1] = n_y;
00050 values[2] = n_z;
00051 values[3] = _d;
00052
00053 mNormal = VVector(nx, ny, nz);
00054 }
00055
00063 VPlane(VVector m_Vec1, VVector m_Vec2, VVector m_Vec3): nx(values[0]), ny(values[1]), nz(values[2]), d(values[3])
00064 {
00065 VVector auxVec1 = m_Vec1 - m_Vec2;
00066 VVector auxVec2 = m_Vec3 - m_Vec2;
00067
00068 mNormal = auxVec2.getCross(auxVec1);
00069 mNormal.normalize();
00070
00071 nx = mNormal.getX();
00072 ny = mNormal.getY();
00073 nz = mNormal.getZ();
00074
00075 d = -mNormal.getDot(m_Vec2);
00076 }
00077
00083 VPlane(VVector m_Normal, VVector m_Point): nx(values[0]), ny(values[1]), nz(values[2]), d(values[3])
00084 {
00085 nx = m_Normal.getX();
00086 ny = m_Normal.getY();
00087 nz = m_Normal.getZ();
00088
00089 d = -m_Normal.getDot(m_Point);
00090 }
00091
00096 VPlane & normalize()
00097 {
00098 float l = sqrtf(nx * nx + ny * ny + nz * nz);
00099
00100 if (l > 0)
00101 l = 1 / l;
00102 else
00103 l = 0;
00104
00105 nx *= l;
00106 ny *= l;
00107 nz *= l;
00108 d *= l;
00109
00110 return *this;
00111 }
00112
00113 VPlane & operator=(const VPlane rhs)
00114 {
00115 this->d = rhs.d;
00116 this->nx = rhs.nx;
00117 this->ny = rhs.ny;
00118 this->nz = rhs.nz;
00119 this->mNormal = rhs.mNormal;
00120
00121 return (*this);
00122 }
00123
00129 bool isInFront(VVector& p) const
00130 {
00131 return ((nx * p.getX() + ny * p.getY() + nz * p.getZ() + d) >= 0);
00132 }
00133
00139 float distance(const VVector& p) const
00140 {
00141 return (nx * p.getX() + ny * p.getY() + nz * p.getZ() + d);
00142 }
00143
00150 bool intersect(const VVector& start, const VVector& dir, VVector& result) const
00151 {
00152 float denom = nx * dir.getX() + ny * dir.getY() + nz * dir.getZ();
00153 float s = start.getX() * nx + start.getY() * ny + start.getZ() * nz + d;
00154
00155 if (fabs(denom) < EPSILON)
00156 {
00157 if (fabs(s) < EPSILON)
00158 {
00159 result = start;
00160 return true;
00161 }
00162 else
00163 {
00164 return false;
00165 }
00166 }
00167
00168 float t = s / (-denom);
00169 result = start + dir * t;
00170 return true;
00171 }
00172
00178 bool intersects(const VVector& start, const VVector& dir) const
00179 {
00180 VVector temp;
00181 return intersect(start, dir, temp);
00182 }
00183
00188 VVector getNormal()
00189 {
00190 return VVector(nx, ny, nz);
00191 }
00192
00197 std::string toString()
00198 {
00199 std::stringstream s;
00200 s << "P([" << "(" << nx << ", " << ny << ", " << nz << ")" << d << "])";
00201 return s.str();
00202
00203 }
00204
00205 float * getArray()
00206 {
00207 return values;
00208 }
00209 };
00210
00211 #endif //__v_plane_h