1. Shankar Giri
  2. ogre

Commits

Steve Streeting  committed 796aa84

Added Sphere::merge utility method

  • Participants
  • Parent commits a49a6a7
  • Branches default

Comments (0)

Files changed (1)

File OgreMain/include/OgreSphere.h

View file
 		{
             return ((v - mCenter).squaredLength() <= Math::Sqr(mRadius));
 		}
+		/** Merges another Sphere into the current sphere */
+		void merge(const Sphere& oth)
+		{
+			Vector3 diff =  oth.getCenter() - mCenter;
+			float lengthSq = diff.squaredLength();
+			float radiusDiff = oth.getRadius() - mRadius;
+			
+			// Early-out
+			if (Math::Sqr(radiusDiff) >= lengthSq) 
+			{
+				// One fully contains the other
+				if (radiusDiff <= 0.0f) 
+					return; // no change
+				else 
+				{
+					mCenter = oth.getCenter();
+					mRadius = oth.getRadius();
+					return;
+				}
+			}
+			
+			float length = Math::Sqrt(lengthSq);
+			
+			Vector3 newCenter;
+			float newRadius;
+			if ((length + oth.getRadius()) > mRadius) 
+			{
+				float t = (length + radiusDiff) / (2.0f * length);
+				newCenter = mCenter + diff * t;
+			} 
+			// otherwise, we keep our existing center
+			
+			newRadius = 0.5f * (length + mRadius + oth.getRadius());
+			
+			mCenter = newCenter;
+			mRadius = newRadius;
+		}
         
 
     };