00001
00002
00003 #include "Plane.h"
00004
00005 #include "MathUtils.h"
00006 #include "Triangle.h"
00007
00008 #include <stdio.h>
00009
00010
00011 #define PLANE_ZERO_TOLERANCE 0.00001f
00012
00013
00014 namespace pge {
00015
00016
00017
00018
00019
00020
00021
00022 Plane::Plane(void) {
00023 m_d = 0.0f;
00024 }
00025
00026
00027
00028
00029
00030
00031
00032 Plane::Plane(Vector3f a, Vector3f b, Vector3f c) {
00033 Vector3f u;
00034 Vector3f v;
00035
00036
00037 u = b.subtract(a);
00038 v = c.subtract(a);
00039
00040 m_normal = u.crossProduct(v);
00041 m_normal.normalize();
00042
00043 m_d = -(a.dotProduct(m_normal));
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 Plane::Plane(Vector3f start, Vector3f u, Vector3f v, int i) {
00053 m_normal = u.crossProduct(v);
00054 m_normal.normalize();
00055 m_d = -(start.dotProduct(m_normal));
00056 }
00057
00058
00059
00060
00061
00062
00063
00064 Plane::Plane(Vector3f normal, float d) {
00065 normal.normalize();
00066 m_normal = normal;
00067 m_d = d;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 Plane::Plane(Triangle *triangle) {
00077 Vector3f a;
00078 Vector3f b;
00079 Vector3f c;
00080 Vector3f u;
00081 Vector3f v;
00082
00083 a = triangle->m_vertices[0];
00084 b = triangle->m_vertices[1];
00085 c = triangle->m_vertices[2];
00086
00087
00088 u = b.subtract(a);
00089 v = c.subtract(a);
00090
00091 m_normal = u.crossProduct(v);
00092 m_normal.normalize();
00093
00094 m_d = -(a.dotProduct(m_normal));
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 Plane::~Plane(void) {
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 bool Plane::isPointOnPlane(Vector3f point) {
00113
00114
00115
00116
00117
00118
00119
00120 float d;
00121
00122 d = getDistanceToPoint(point);
00123
00124
00125 return mathutils::isValueInTolerance(d, 0.0f, PLANE_ZERO_TOLERANCE);
00126
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 float Plane::getDistanceToPoint(Vector3f point) {
00136 return m_normal.dotProduct(point) + m_d;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145 Vector3f Plane::getFootPoint(Vector3f point) {
00146 float n;
00147 Vector3f v;
00148 n = (m_normal.dotProduct(point) + m_d) / m_normal.dotProduct(m_normal);
00149 v = m_normal.multiply(n);
00150 v = point.subtract(v);
00151 return v;
00152 }
00153
00154
00155
00156
00157
00158
00159
00160 Vector3f Plane::getIntersectionPoint(Vector3f lineStart, Vector3f lineEnd) {
00161
00162
00163
00164 Vector3f lineDirection;
00165 Vector3f result;
00166 float distPoint;
00167 float dotLine;
00168 float distance;
00169
00170
00171 lineDirection = lineEnd.subtract(lineStart);
00172 lineDirection.normalize();
00173
00174
00175 distPoint = -(m_normal.m_v[0] * lineStart.m_v[0] +
00176 m_normal.m_v[1] * lineStart.m_v[1] +
00177 m_normal.m_v[2] * lineStart.m_v[2] + m_d);
00178
00179 dotLine = m_normal.dotProduct(lineDirection);
00180
00181
00182
00183
00184 if(mathutils::isValueInTolerance(dotLine, 0.0f, PLANE_ZERO_TOLERANCE)) {
00185 return lineStart;
00186 }
00187
00188
00189 distance = distPoint / dotLine;
00190
00191 result.m_v[0] = lineStart.m_v[0] + (lineDirection.m_v[0] * distance);
00192 result.m_v[1] = lineStart.m_v[1] + (lineDirection.m_v[1] * distance);
00193 result.m_v[2] = lineStart.m_v[2] + (lineDirection.m_v[2] * distance);
00194
00195 return result;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204 bool Plane::isLineIntersecting(Vector3f lineStart, Vector3f lineEnd) {
00205
00206
00207
00208 float dist1;
00209 float dist2;
00210
00211
00212 dist1 = 0.0f;
00213 dist2 = 0.0f;
00214
00215 dist1 = ((m_normal.m_v[0] * lineStart.m_v[0]) +
00216 (m_normal.m_v[1] * lineStart.m_v[1]) +
00217 (m_normal.m_v[2] * lineStart.m_v[2])) + m_d;
00218
00219 dist2 = ((m_normal.m_v[0] * lineEnd.m_v[0]) +
00220 (m_normal.m_v[1] * lineEnd.m_v[1]) +
00221 (m_normal.m_v[2] * lineEnd.m_v[2])) + m_d;
00222
00223 if(dist1 * dist2 >= 0.0f) {
00224 return false;
00225 }
00226
00227 return true;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 std::string Plane::toString() {
00237
00238
00239 return std::string("TODO");
00240 }
00241 };