From: <pat...@us...> - 2012-10-26 21:24:58
|
Revision: 1281 http://ggt.svn.sourceforge.net/ggt/?rev=1281&view=rev Author: patrickh Date: 2012-10-26 21:24:52 +0000 (Fri, 26 Oct 2012) Log Message: ----------- 1. Change mEmpty to mUninitialized in Box, and add mUninitialized to Sphere. Add an isInitialized() function to both Sphere and Box, but also keep isEmpty(). This give us a better name and also maintains backwards compatibility. 2. For the extendVolume() variants that extend a Sphere by a single Point or a single other volume, add the initialization checks to make Sphere work like box. Submitted by Paul Martz. Modified Paths: -------------- trunk/gmtl/AABox.h trunk/gmtl/Containment.h trunk/gmtl/Sphere.h Modified: trunk/gmtl/AABox.h =================================================================== --- trunk/gmtl/AABox.h 2012-09-21 13:56:48 UTC (rev 1280) +++ trunk/gmtl/AABox.h 2012-10-26 21:24:52 UTC (rev 1281) @@ -34,10 +34,10 @@ public: /** - * Creates a new empty box. + * Creates a new uninitialized box. */ AABox() - : mMin(0,0,0), mMax(0,0,0), mEmpty(true) + : mMin(0,0,0), mMax(0,0,0), mInitialized(false) {} /** @@ -50,7 +50,7 @@ * @pre bot min and max are not zero */ AABox(const Point<DATA_TYPE, 3>& min, const Point<DATA_TYPE, 3>& max) - : mMin(min), mMax(max), mEmpty(false) + : mMin(min), mMax(max), mInitialized(true) {} /** @@ -59,7 +59,7 @@ * @param box the box the make a copy of */ AABox(const AABox<DATA_TYPE>& box) - : mMin(box.mMin), mMax(box.mMax), mEmpty(box.mEmpty) + : mMin(box.mMin), mMax(box.mMax), mInitialized(box.mInitialized) {} /** @@ -84,15 +84,28 @@ /** * Tests if this box occupies no space. + * This method is deprecated. Use isInitialized() instead. + * \deprecated * - * @return true if the box is empty, false otherwise + * @return true if the box is empty/uninitialized, false otherwise */ bool isEmpty() const { - return mEmpty; + return !mInitialized; } /** + * Tests if this box is initialized. An initialized box could have + * zero volume but would contain one point. + * + * @return true if the box is initialized, false otherwise + */ + bool isInitialized() const + { + return mInitialized; + } + + /** * Sets the minimum point of the box. * * @param min the min point @@ -114,14 +127,27 @@ /** * Sets the empty flag on this box. + * This method is deprecated. Use setInitialized() instead. + * \deprecated * * @param empty true to make the box empty, false otherwise */ void setEmpty(bool empty) { - mEmpty = empty; + mInitialized = !empty; } + /** + * Marks a box as initialized. Boxes that are initialized (isInitialized()==true) + * but have zero volume (mMax==mMin) contain exactly one point. + * + * @param initialized true to mark the box as initialized, false otherwise + */ + void setInitialized(bool initialized=true) + { + mInitialized = initialized; + } + public: /** * The minimum point of the box. @@ -134,9 +160,9 @@ Point<DATA_TYPE, 3> mMax; /** - * Flag for empty box. True if the box is empty. + * Initialization flag. False initially, true if the box is initialized. */ - bool mEmpty; + bool mInitialized; }; // --- helper types --- // Modified: trunk/gmtl/Containment.h =================================================================== --- trunk/gmtl/Containment.h 2012-09-21 13:56:48 UTC (rev 1280) +++ trunk/gmtl/Containment.h 2012-10-26 21:24:52 UTC (rev 1281) @@ -79,27 +79,36 @@ void extendVolume( Sphere<DATA_TYPE>& container, const Point<DATA_TYPE, 3>& pt ) { - // check if we already contain the point - if ( isInVolume( container, pt ) ) + if (container.isInitialized()) { - return; - } + // check if we already contain the point + if ( isInVolume( container, pt ) ) + { + return; + } - // make a vector pointing from the center of the sphere to pt. this is the - // direction in which we need to move the sphere's center - Vec<DATA_TYPE, 3> dir = pt - container.mCenter; - DATA_TYPE len = normalize( dir ); + // make a vector pointing from the center of the sphere to pt. this is the + // direction in which we need to move the sphere's center + Vec<DATA_TYPE, 3> dir = pt - container.mCenter; + DATA_TYPE len = normalize( dir ); - // compute what the new radius should be - DATA_TYPE newRadius = (len + container.mRadius) * static_cast<DATA_TYPE>(0.5); + // compute what the new radius should be + DATA_TYPE newRadius = (len + container.mRadius) * static_cast<DATA_TYPE>(0.5); - // compute the new center for the sphere - Point<DATA_TYPE, 3> newCenter = container.mCenter + + // compute the new center for the sphere + Point<DATA_TYPE, 3> newCenter = container.mCenter + (dir * (newRadius - container.mRadius)); - // modify container to its new values - container.mCenter = newCenter; - container.mRadius = newRadius; + // modify container to its new values + container.mCenter = newCenter; + container.mRadius = newRadius; + } + else + { + container.mCenter = pt; + container.mRadius = (DATA_TYPE)0.0; + container.mInitialized = true; + } } /** @@ -112,28 +121,43 @@ void extendVolume( Sphere<DATA_TYPE>& container, const Sphere<DATA_TYPE>& sphere ) { - // check if we already contain the sphere - if ( isInVolume( container, sphere ) ) + // Can't extend by an empty sphere + if (! sphere.isInitialized()) { return; } - // make a vector pointing from the center of container to sphere. this is the - // direction in which we need to move container's center - Vec<DATA_TYPE, 3> dir = sphere.mCenter - container.mCenter; - DATA_TYPE len = normalize( dir ); + if (container.isInitialized()) + { + // check if we already contain the sphere + if ( isInVolume( container, sphere ) ) + { + return; + } - // compute what the new radius should be - DATA_TYPE newRadius = (len + sphere.mRadius + container.mRadius) * + // make a vector pointing from the center of container to sphere. this is the + // direction in which we need to move container's center + Vec<DATA_TYPE, 3> dir = sphere.mCenter - container.mCenter; + DATA_TYPE len = normalize( dir ); + + // compute what the new radius should be + DATA_TYPE newRadius = (len + sphere.mRadius + container.mRadius) * static_cast<DATA_TYPE>(0.5); - // compute the new center for container - Point<DATA_TYPE, 3> newCenter = container.mCenter + + // compute the new center for container + Point<DATA_TYPE, 3> newCenter = container.mCenter + (dir * (newRadius - container.mRadius)); - // modify container to its new values - container.mCenter = newCenter; - container.mRadius = newRadius; + // modify container to its new values + container.mCenter = newCenter; + container.mRadius = newRadius; + } + else + { + container.mCenter = sphere.mCenter; + container.mRadius = sphere.mRadius; + container.mInitialized = true; + } } /** @@ -305,7 +329,7 @@ bool isInVolume(const AABox<DATA_TYPE>& container, const Point<DATA_TYPE, 3>& pt) { - if (! container.isEmpty()) + if (container.isInitialized()) { return ( pt[0] >= container.mMin[0] && pt[1] >= container.mMin[1] && @@ -334,7 +358,7 @@ bool isInVolumeExclusive(const AABox<DATA_TYPE>& container, const Point<DATA_TYPE, 3>& pt) { - if (! container.isEmpty()) + if (container.isInitialized()) { return ( pt[0] > container.mMin[0] && pt[1] > container.mMin[1] && @@ -366,7 +390,7 @@ const AABox<DATA_TYPE>& box) { // Empty boxes don't overlap - if (container.isEmpty() || box.isEmpty()) + if (!container.isInitialized() || !box.isInitialized()) { return false; } @@ -393,7 +417,7 @@ void extendVolume(AABox<DATA_TYPE>& container, const Point<DATA_TYPE, 3>& pt) { - if (! container.isEmpty()) + if (container.isInitialized()) { // X coord if (pt[0] > container.mMax[0]) @@ -445,13 +469,13 @@ const AABox<DATA_TYPE>& box) { // Can't extend by an empty box - if (box.isEmpty()) + if (! box.isInitialized()) { return; } // An empty container is extended to be the box - if (container.isEmpty()) + if (! container.isInitialized()) { container = box; } Modified: trunk/gmtl/Sphere.h =================================================================== --- trunk/gmtl/Sphere.h 2012-09-21 13:56:48 UTC (rev 1280) +++ trunk/gmtl/Sphere.h 2012-10-26 21:24:52 UTC (rev 1281) @@ -28,7 +28,7 @@ * Constructs a sphere centered at the origin with a radius of 0. */ Sphere() - : mRadius( 0 ) + : mRadius( 0 ), mInitialized( false ) {} /** @@ -38,7 +38,7 @@ * @param radius the radius of the sphere */ Sphere( const Point<DATA_TYPE, 3>& center, const DATA_TYPE& radius ) - : mCenter( center ), mRadius( radius ) + : mCenter( center ), mRadius( radius ), mInitialized( true ) {} /** @@ -47,7 +47,7 @@ * @param sphere the sphere to make a copy of */ Sphere( const Sphere<DATA_TYPE>& sphere ) - : mCenter( sphere.mCenter ), mRadius( sphere.mRadius ) + : mCenter( sphere.mCenter ), mRadius( sphere.mRadius ), mInitialized( sphere.mInitialized ) {} /** @@ -71,6 +71,17 @@ } /** + * Tests if this sphere is initialized. An initialized sphere could have + * zero volume but would contain one point. + * + * @return true if the sphere is initialized, false otherwise + */ + bool isInitialized() const + { + return mInitialized; + } + + /** * Sets the center point of the sphere. * * @param center the new point at which to center the sphere @@ -90,6 +101,18 @@ mRadius = radius; } + + /** + * Marks a sphere as initialized. Spheres that are initialized (isInitialized()==true) + * but have zero volume (getRadius()==0.0) contain exactly one point. + * + * @param initialized true to mark the sphere as initialized, false otherwise + */ + void setInitialized(bool initialized=true) + { + mInitialized = initialized; + } + public: /** * The center of the sphere. @@ -100,6 +123,11 @@ * The radius of the sphere. */ DATA_TYPE mRadius; + + /** + * Initialization flag. False initially, true if the sphere is initialized. + */ + bool mInitialized; }; // --- helper types --- // This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |