1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Modification History
*
* 2001-April-14 Jason Rohrer
* Created.
*
* 2005-February-15 Jason Rohrer
* Made destructor virtual to quell warnings.
*/
#ifndef TRIANGLE_3D_INCLUDED
#define TRIANGLE_3D_INCLUDED
#include "GeometricObject3D.h"
#include "LineSegment3D.h"
#include "minorGems/io/Serializable.h"
#include <math.h>
#include <stdlib.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* Triangle (in 3-space).
*
* @author Jason Rohrer
*/
class Triangle3D : public GeometricObject3D, public Serializable {
public:
Vector3D *mPoints[3];
/**
* Constructs a triangle.
*
* The vectors are copied, so the caller
* is responsible for deallocating the passed in vectors.
*
* @param inPointA the first point.
* @param inPointB the second point.
* @param inPointC the third point.
*
* The "front" face of this trangle is defined as the face
* viewed when the above vertices are in counter-clockwise order.
*/
Triangle3D( Vector3D *inPointA, Vector3D *inPointB,
Vector3D *inPointC);
/**
* Constructs a triangle by copying parameters
* from another triangle.
*
* @param inOtherTriangle the triangle to copy.
*/
Triangle3D( Triangle3D *inOtherTriangle );
virtual ~Triangle3D();
/**
* Gets a line segment from this triangle.
*
* @param inIndex the line segment to get, in [0,2].
*
* @return a new line segment, or NULL if the index is out of range.
* Must be destroyed by caller.
*/
LineSegment3D *getLineSegment( int inIndex );
/**
* Gets the normal for the plane containing this triangle.
*
* @return the normal vector. Must be destroyed by caller.
*/
Vector3D *getNormal();
/**
* Computes the normal projection of a point onto the plane
* defined by this triangle. A point is returned even
* if it is not inside the bounds of the triangle.
*
* @param inPoint the point to project. Must be destroyed by caller.
*
* @return a new vector representing the projection of the point.
* Must be destroyed by caller.
*/
Vector3D *projectOntoPlane( Vector3D *inPoint );
/**
* Checks if a point is inside the bounds of this triangle.
*
* @param inPoint the point to check. Must be destroyed by caller.
*
* @return true iff the point is in bounds.
*/
char isInBounds( Vector3D *inPoint );
// these implement the GeometricObject3D interface
void move( Vector3D *inVector );
void rotate( Angle3D *inAngle );
void reverseRotate( Angle3D *inAngle );
void scale( double inScalar );
GeometricObject3D *copy();
// implement the Serializable interface
virtual int serialize( OutputStream *inOutputStream );
virtual int deserialize( InputStream *inInputStream );
};
inline Triangle3D::Triangle3D( Vector3D *inEndpointA,
Vector3D *inEndpointB,
Vector3D *inEndpointC ) {
mPoints[0] = new Vector3D( inEndpointA );
mPoints[1] = new Vector3D( inEndpointB );
mPoints[2] = new Vector3D( inEndpointC );
}
inline Triangle3D::Triangle3D( Triangle3D *inOtherTriangle ) {
mPoints[0] = new Vector3D( inOtherTriangle->mPoints[0] );
mPoints[1] = new Vector3D( inOtherTriangle->mPoints[1] );
mPoints[2] = new Vector3D( inOtherTriangle->mPoints[2] );
}
inline Triangle3D::~Triangle3D() {
delete mPoints[0];
delete mPoints[1];
delete mPoints[2];
}
inline LineSegment3D *Triangle3D::getLineSegment( int inIndex ) {
if( inIndex < 0 || inIndex > 2 ) {
return NULL;
}
else {
// can pass in mPoints directly, since constructor copies them
return new LineSegment3D( mPoints[inIndex],
mPoints[ ( inIndex + 1 ) % 2 ] );
}
}
inline Vector3D *Triangle3D::getNormal() {
// cross C-B with A-B to get a normal pointing towards
// the viewer when seeing the points in counter-clockwise order
Vector3D *firstLeg = new Vector3D( mPoints[2] );
firstLeg->subtract( mPoints[1] );
Vector3D *secondLeg = new Vector3D( mPoints[0] );
secondLeg->subtract( mPoints[1] );
Vector3D *normal = firstLeg->cross( secondLeg );
normal->normalize();
delete firstLeg;
delete secondLeg;
return normal;
}
inline Vector3D *Triangle3D::projectOntoPlane( Vector3D *inPoint ) {
// formula found at:
// http://astronomy.swin.edu.au/pbourke/geometry/pointplane/
//minimum distance =
//(A (xa - xb) + B (ya - yb) + C (za - zb)) / sqrt(A^2 + B^2 + C^2)
Vector3D *normal = getNormal();
double minDistance =
normal->mX * ( inPoint->mX - mPoints[0]->mX ) +
normal->mY * ( inPoint->mY - mPoints[0]->mY ) +
normal->mZ * ( inPoint->mZ - mPoints[0]->mZ );
minDistance = minDistance /
sqrt( normal->mX * normal->mX +
normal->mY * normal->mY +
normal->mZ * normal->mZ );
double dot = inPoint->dot( normal );
Vector3D *returnPoint = new Vector3D( inPoint );
if( dot > 0 ) {
// inPoint on front side of plane
normal->scale( -minDistance );
}
else {
// inPoint on back side of plane
normal->scale( minDistance );
}
returnPoint->add( normal );
delete normal;
return returnPoint;
}
inline char Triangle3D::isInBounds( Vector3D *inPoint ) {
// this is a nice formula, found at
// http://astronomy.swin.edu.au/pbourke/geometry/insidepoly/
// compute the angle between inPoint and every pair of points in the
// triangle (each edge). If the sum is 2*pi, then the point
// is inside the triangle.
// note that we have a hard-coded epsilon value here
double epsilon = 0.000001;
double angleSum = 0;
Vector3D *firstLeg = new Vector3D( mPoints[0] );
firstLeg->subtract( inPoint );
Vector3D *secondLeg = new Vector3D( mPoints[1] );
secondLeg->subtract( inPoint );
Vector3D *thirdLeg = new Vector3D( mPoints[2] );
thirdLeg->subtract( inPoint );
angleSum += acos( firstLeg->dot( secondLeg ) /
( firstLeg->getLength() * secondLeg->getLength() ) );
angleSum += acos( secondLeg->dot( thirdLeg ) /
( secondLeg->getLength() * thirdLeg->getLength() ) );
angleSum += acos( thirdLeg->dot( firstLeg ) /
( thirdLeg->getLength() * firstLeg->getLength() ) );
delete firstLeg;
delete secondLeg;
delete thirdLeg;
if( angleSum < ( 2 * M_PI - epsilon ) ) {
// angle too small for point to be inside plane
return false;
}
else {
return true;
}
}
inline void Triangle3D::move( Vector3D *inVector ) {
mPoints[0]->add( inVector );
mPoints[1]->add( inVector );
mPoints[2]->add( inVector );
}
inline void Triangle3D::rotate( Angle3D *inAngle ) {
mPoints[0]->rotate( inAngle );
mPoints[1]->rotate( inAngle );
mPoints[2]->rotate( inAngle );
}
inline void Triangle3D::reverseRotate( Angle3D *inAngle ) {
mPoints[0]->reverseRotate( inAngle );
mPoints[1]->reverseRotate( inAngle );
mPoints[2]->reverseRotate( inAngle );
}
inline void Triangle3D::scale( double inScalar ) {
mPoints[0]->scale( inScalar );
mPoints[1]->scale( inScalar );
mPoints[2]->scale( inScalar );
}
inline GeometricObject3D *Triangle3D::copy() {
Triangle3D *copiedTriangle = new Triangle3D( this );
return (GeometricObject3D*)copiedTriangle;
}
inline int Triangle3D::serialize( OutputStream *inOutputStream ) {
int numBytesWritten = 0;
numBytesWritten += mPoints[0]->serialize( inOutputStream );
numBytesWritten += mPoints[1]->serialize( inOutputStream );
numBytesWritten += mPoints[2]->serialize( inOutputStream );
return numBytesWritten;
}
inline int Triangle3D::deserialize( InputStream *inInputStream ) {
int numBytesRead = 0;
numBytesRead += mPoints[0]->deserialize( inInputStream );
numBytesRead += mPoints[1]->deserialize( inInputStream );
numBytesRead += mPoints[2]->deserialize( inInputStream );
return numBytesRead;
}
#endif
|