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 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* Modification History
*
* 2000-December-13 Jason Rohrer
* Created.
*
* 2000-December-14 Jason Rohrer
* Added implementation for all member functions.
*
* 2001-January-10 Jason Rohrer
* Made class serializable.
*
* 2001-January-24 Jason Rohrer
* Fixed a bug in the deserialization function.
*
* 2001-February-2 Jason Rohrer
* Added M_PI definition.
*
* 2001-April-14 Jason Rohrer
* Added line segment and triangle intersection tests.
*/
#ifndef SPHERE_INCLUDED
#define SPHERE_INCLUDED
#include "GeometricObject3D.h"
#include "LineSegment3D.h"
#include "Triangle3D.h"
#include "minorGems/io/Serializable.h"
#include <math.h>
#include <stdlib.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* Geometric sphere (in 3-space).
*
* @author Jason Rohrer
*/
class Sphere : public GeometricObject3D, public Serializable {
public:
Vector3D *mCenter;
double mRadius;
/**
* Constructs a Sphere.
*
* @param inCenter a vector containing the coordinates of the
* sphere's center. The vector is copied, so the caller
* is responsible for deallocating the passed in vector.
*
* @param inRadius the radius of the sphere.
*/
Sphere( Vector3D *inCenter, double inRadius );
/**
* Constructs a Sphere by copying parameters from another Sphere.
*
* @param inOtherSphere the sphere to copy.
*/
Sphere( Sphere *inOtherSphere );
~Sphere();
/**
* Gets whether this sphere intersects with another.
*
* @param inOtherSphere the sphere to test for intersection
* with this sphere.
*
* @return true iff this sphere intersects with the other.
*/
char intersects( Sphere *inOtherSphere );
/**
* Gets the normalized direction of intersection of this
* sphere with another.
*
* @param inOtherSphere the sphere to test for intersection
* with this sphere.
*
* @return a normalized vector direction of the intersection,
* or NULL if there is no intersection.
*/
Vector3D *getIntersection( Sphere *inOtherSphere );
/**
* Gets whether a line segment intersects or is inside this sphere.
*
* @param inSegment the segment to test.
*
* @return true if the segment intersects with or is contained by
* this sphere.
*/
char intersects( LineSegment3D *inSegment );
/**
* Gets whether a triangle intesects or is inside this sphere.
*
*
* @param inTriangle the triangle to test.
*
* @return true if the triangle intersects with or is contained by
* this sphere.
*/
char intersects( Triangle3D *inTriangle );
/**
* Gets the volume of this sphere.
*
* @return the volume of this sphere.
*/
double getVolume();
// 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 Sphere::Sphere( Vector3D *inCenter, double inRadius )
: mCenter( new Vector3D( inCenter ) ), mRadius( inRadius ) {
}
inline Sphere::Sphere( Sphere *inOtherSphere )
: mCenter( new Vector3D( inOtherSphere->mCenter ) ),
mRadius( inOtherSphere->mRadius ) {
}
inline Sphere::~Sphere() {
delete mCenter;
}
inline char Sphere::intersects( Sphere *inOtherSphere ) {
double distance = mCenter->getDistance( inOtherSphere->mCenter );
if( distance <= mRadius + inOtherSphere->mRadius ) {
return true;
}
else {
return false;
}
}
inline Vector3D *Sphere::getIntersection( Sphere *inOtherSphere ) {
if( intersects( inOtherSphere ) ) {
Vector3D * returnVector = new Vector3D( inOtherSphere->mCenter );
returnVector->subtract( mCenter );
returnVector->normalize();
return returnVector;
}
else {
return NULL;
}
}
/*
* These intersection algorithms were found in the following paper:
*
* ERIT -- A Collection of Efficient and Reliable Intersection Tests
* by Martin Held
* Journal of Graphics Tools
*/
inline char Sphere::intersects( LineSegment3D *inSegment ) {
// first check if either endpoint is inside this sphere
if( inSegment->mEndpoints[0]->getDistance( mCenter ) <= mRadius ) {
return true;
}
else if( inSegment->mEndpoints[1]->getDistance( mCenter ) <= mRadius ) {
return true;
}
else {
// both endpoints outside
// project center onto the line
Vector3D *projectedCenter = inSegment->projectPoint( mCenter );
if( projectedCenter == NULL ) {
// projection is outside of the segment
return false;
}
else {
// projection is inside of the segment
// check distance from center
double distance = projectedCenter->getDistance( mCenter );
delete projectedCenter;
if( distance <= mRadius ) {
return true;
}
else {
return false;
}
}
}
}
inline char Sphere::intersects( Triangle3D *inTriangle ) {
char intersectionFound;
// first, check each triangle edge segment for intersection
LineSegment3D *segmentA = inTriangle->getLineSegment( 0 );
intersectionFound = intersects( segmentA );
delete segmentA;
if( intersectionFound ) {
return true;
}
LineSegment3D *segmentB = inTriangle->getLineSegment( 1 );
intersectionFound = intersects( segmentB );
delete segmentB;
if( intersectionFound ) {
return true;
}
LineSegment3D *segmentC = inTriangle->getLineSegment( 2 );
intersectionFound = intersects( segmentC );
delete segmentC;
if( intersectionFound ) {
return true;
}
// if we're at this point, none of the edges intersected.
// we still need to check if the interior region of the triangle
// is inside the sphere
Vector3D *projectedCenter = inTriangle->projectOntoPlane( mCenter );
if( inTriangle->isInBounds( projectedCenter ) ) {
if( projectedCenter->getDistance( mCenter ) <= mRadius ) {
// the projected center is inside the triangle and
// inside the sphere
intersectionFound = true;
}
else {
// the projected center is inside the triangle, but outside
// the sphere
intersectionFound = false;
}
}
else {
// center projection not even in triangle bounds, so no intersection
intersectionFound = false;
}
delete projectedCenter;
return intersectionFound;
}
inline double Sphere::getVolume() {
return (4/3) * M_PI * mRadius * mRadius * mRadius;
}
inline void Sphere::move( Vector3D *inVector ) {
mCenter->add( inVector );
}
inline void Sphere::rotate( Angle3D *inAngle ) {
mCenter->rotate( inAngle );
}
inline void Sphere::reverseRotate( Angle3D *inAngle ) {
mCenter->reverseRotate( inAngle );
}
inline void Sphere::scale( double inScalar ) {
mCenter->scale( inScalar );
mRadius *= inScalar;
}
inline GeometricObject3D *Sphere::copy() {
Sphere *copiedSphere = new Sphere( this );
return (GeometricObject3D*)copiedSphere;
}
inline int Sphere::serialize( OutputStream *inOutputStream ) {
int numBytesWritten = 0;
numBytesWritten += mCenter->serialize( inOutputStream );
unsigned char* doublePointer = (unsigned char *)( &mRadius );
numBytesWritten += inOutputStream->write( doublePointer, sizeof( double ) );
return numBytesWritten;
}
inline int Sphere::deserialize( InputStream *inInputStream ) {
int numBytesRead = 0;
numBytesRead += mCenter->deserialize( inInputStream );
unsigned char* doublePointer = (unsigned char *)( &mRadius );
numBytesRead += inInputStream->read( doublePointer, sizeof( double ) );
return numBytesRead;
}
#endif
|