Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sphere UpdateSphereOne( std::vector< Vector >& pointSet, Vector& newPoint )
- {
- pointSet.push_back( newPoint );
- return Sphere( pointSet[0], pointSet[1] );
- }
- Sphere UpdateSphereTwo( std::vector< Vector >& pointSet, Vector& newPoint )
- {
- Sphere minimumSphere;
- // have a circle currently consisting of p0 and p1
- Vector p0 = pointSet[0];
- Vector p1 = pointSet[1];
- // need to test a circle of p0 and newpoint & one of p1 and newpoint
- Sphere possibleSpheres[2];
- float minRadius = RAND_MAX;
- int index = -1;
- // means I can have two possible spheres
- possibleSpheres[0] = Sphere( p0, newPoint );
- possibleSpheres[1] = Sphere( p1, newPoint );
- if( possibleSpheres[0].Contains( p1 ) )
- {
- minRadius = possibleSpheres[0].radius;
- index = 0;
- }
- else if( possibleSpheres[1].Contains( p0 ) && possibleSpheres[1].radius < minRadius )
- {
- minRadius = possibleSpheres[1].radius;
- index = 1;
- }
- if( index != -1 ) // meaning we found a valid sphere
- {
- minimumSphere = possibleSpheres[index];
- pointSet[1-index] = newPoint; // replace unecessary point with the new point
- }
- else
- {
- minimumSphere = Sphere( p0, p1, newPoint );
- pointSet.push_back( newPoint ); // add point to set because all three are now required
- }
- return minimumSphere;
- }
- Sphere UpdateSphereThree( std::vector< Vector >& pointSet, Vector& newPoint )
- {
- Sphere minimumSphere;
- Sphere possibleSpheres[6]; // 6 possible spheres
- // test points
- Vector p0 = pointSet[0];
- Vector p1 = pointSet[1];
- Vector p2 = pointSet[2];
- int sphereIndex = 0; // all possible 2 pairs of spheres
- // initialize all possible spheres
- for( size_t k1 = 0; k1 < pointSet.size(); ++k1 )
- {
- possibleSpheres[sphereIndex++] = Sphere( pointSet[k1], newPoint );
- }
- possibleSpheres[3] = Sphere( p0, p1, newPoint );
- possibleSpheres[4] = Sphere( p0, p2, newPoint );
- possibleSpheres[5] = Sphere( p1, p2, newPoint );
- int chosenSphere = -1;
- float minRadius = RAND_MAX;
- if( possibleSpheres[0].Contains( p1 ) && possibleSpheres[0].Contains( p2 ) )
- {
- chosenSphere = 0;
- minRadius = possibleSpheres[0].radius;
- }
- if( possibleSpheres[1].radius < minRadius && possibleSpheres[1].Contains( p0 ) && possibleSpheres[1].Contains( p2 ) )
- {
- chosenSphere = 1;
- minRadius = possibleSpheres[1].radius;
- }
- if( possibleSpheres[2].radius < minRadius && possibleSpheres[2].Contains( p0 ) && possibleSpheres[2].Contains( p1 ) )
- {
- chosenSphere = 2;
- minRadius = possibleSpheres[2].radius;
- }
- if( possibleSpheres[3].radius < minRadius && possibleSpheres[3].Contains( p2 ) )
- {
- chosenSphere = 3;
- minRadius = possibleSpheres[3].radius;
- }
- if( possibleSpheres[4].radius < minRadius && possibleSpheres[4].Contains( p1 ) )
- {
- chosenSphere = 4;
- minRadius = possibleSpheres[4].radius;
- }
- if( possibleSpheres[5].radius < minRadius && possibleSpheres[5].Contains( p0 ) )
- {
- chosenSphere = 5;
- minRadius = possibleSpheres[5].radius;
- }
- if( chosenSphere == -1 )
- {
- minimumSphere = Sphere( pointSet[0], pointSet[1], pointSet[2], newPoint );
- pointSet.push_back( newPoint );
- }
- else
- {
- minimumSphere = possibleSpheres[chosenSphere];
- switch( chosenSphere )
- {
- case 0: pointSet.resize( 2 );
- pointSet[1] = newPoint;
- break;
- case 1: pointSet.resize( 2 );
- pointSet[0] = newPoint;
- break;
- case 2: pointSet[0] = pointSet[3];
- pointSet.resize( 2 );
- pointSet[1] = newPoint;
- break;
- case 3: pointSet.resize( 3 );
- pointSet[2] = newPoint;
- break;
- case 4: pointSet.resize( 3 );
- pointSet[1] = newPoint;
- break;
- case 5: pointSet.resize( 3 );
- pointSet[0] = newPoint;
- break;
- }
- }
- return minimumSphere;
- }
- Sphere UpdateSphereFour( std::vector< Vector >& pointSet, Vector& newPoint )
- {
- Sphere updatedSphere;
- Sphere possibleSpheres[14]; // 14 possible spheres
- // test points
- Vector p0 = pointSet[0];
- Vector p1 = pointSet[1];
- Vector p2 = pointSet[2];
- Vector p3 = pointSet[3];
- int sphereIndex = -1;
- float minRadius = RAND_MAX;
- possibleSpheres[0] = Sphere( p0, newPoint );
- possibleSpheres[1] = Sphere( p1, newPoint );
- possibleSpheres[2] = Sphere( p2, newPoint );
- possibleSpheres[3] = Sphere( p3, newPoint );
- possibleSpheres[4] = Sphere( p0, p1, newPoint );
- possibleSpheres[5] = Sphere( p0, p2, newPoint );
- possibleSpheres[6] = Sphere( p0, p3, newPoint );
- possibleSpheres[7] = Sphere( p1, p2, newPoint );
- possibleSpheres[8] = Sphere( p1, p3, newPoint );
- possibleSpheres[9] = Sphere( p2, p3, newPoint );
- possibleSpheres[10] = Sphere( p0, p1, p2, newPoint );
- possibleSpheres[11] = Sphere( p0, p1, p3, newPoint );
- possibleSpheres[12] = Sphere( p1, p2, p3, newPoint );
- possibleSpheres[13] = Sphere( p2, p3, p0, newPoint );
- // test all two spheres
- if( possibleSpheres[0].Contains( p1 ) && possibleSpheres[0].Contains( p2 ) && possibleSpheres[0].Contains( p3 ) )
- {
- sphereIndex = 0;
- minRadius = possibleSpheres[0].radius;
- }
- if( possibleSpheres[1].radius < minRadius && possibleSpheres[1].Contains( p0 ) && possibleSpheres[1].Contains( p2 ) && possibleSpheres[1].Contains( p3 ) )
- {
- sphereIndex = 1;
- minRadius = possibleSpheres[1].radius;
- }
- if( possibleSpheres[2].radius < minRadius && possibleSpheres[2].Contains( p0 ) && possibleSpheres[2].Contains( p1 ) && possibleSpheres[2].Contains( p3 ) )
- {
- sphereIndex = 2;
- minRadius = possibleSpheres[2].radius;
- }
- if( possibleSpheres[3].radius < minRadius && possibleSpheres[3].Contains( p0 ) && possibleSpheres[3].Contains( p1 ) && possibleSpheres[3].Contains( p2 ) )
- {
- sphereIndex = 3;
- minRadius = possibleSpheres[3].radius;
- }
- // test all three spheres
- if( possibleSpheres[4].radius < minRadius && possibleSpheres[4].Contains( p2 ) && possibleSpheres[4].Contains( p3 ) )
- {
- sphereIndex = 4;
- minRadius = possibleSpheres[4].radius;
- }
- if( possibleSpheres[5].radius < minRadius && possibleSpheres[5].Contains( p1 ) && possibleSpheres[5].Contains( p3 ) )
- {
- sphereIndex = 5;
- minRadius = possibleSpheres[5].radius;
- }
- if( possibleSpheres[6].radius < minRadius && possibleSpheres[6].Contains( p1 ) && possibleSpheres[6].Contains( p2 ) )
- {
- sphereIndex = 6;
- minRadius = possibleSpheres[6].radius;
- }
- if( possibleSpheres[7].radius < minRadius && possibleSpheres[7].Contains( p0 ) && possibleSpheres[7].Contains( p3 ) )
- {
- sphereIndex = 7;
- minRadius = possibleSpheres[7].radius;
- }
- if( possibleSpheres[8].radius < minRadius && possibleSpheres[8].Contains( p0 ) && possibleSpheres[8].Contains( p2 ) )
- {
- sphereIndex = 8;
- minRadius = possibleSpheres[8].radius;
- }
- if( possibleSpheres[9].radius < minRadius && possibleSpheres[9].Contains( p0 ) && possibleSpheres[9].Contains( p1 ) )
- {
- sphereIndex = 9;
- minRadius = possibleSpheres[9].radius;
- }
- // test all four spheres
- if( possibleSpheres[10].radius < minRadius && possibleSpheres[10].Contains( p3 ) )
- {
- sphereIndex = 10;
- minRadius = possibleSpheres[10].radius;
- }
- if( possibleSpheres[11].radius < minRadius && possibleSpheres[11].Contains( p2 ) )
- {
- sphereIndex = 11;
- minRadius = possibleSpheres[11].radius;
- }
- if( possibleSpheres[12].radius < minRadius && possibleSpheres[12].Contains( p0 ) )
- {
- sphereIndex = 12;
- minRadius = possibleSpheres[12].radius;
- }
- if( possibleSpheres[13].radius < minRadius && possibleSpheres[13].Contains( p1 ) )
- {
- sphereIndex = 13;
- minRadius = possibleSpheres[13].radius;
- }
- if( sphereIndex == -1 )
- {
- _ASSERT( false );
- }
- updatedSphere = possibleSpheres[sphereIndex];
- switch( sphereIndex )
- {
- case 0:
- pointSet.resize( 2 );
- pointSet[1] = newPoint;
- break;
- case 1:
- pointSet.resize( 2 );
- pointSet[0] = newPoint;
- break;
- case 2:
- pointSet[0] = p2;
- pointSet.resize( 2 );
- pointSet[1] = newPoint;
- break;
- case 3:
- pointSet[0] = p3;
- pointSet.resize( 2 );
- pointSet[1] = newPoint;
- break;
- case 4:
- pointSet.resize( 3 );
- pointSet[2] = newPoint;
- break;
- case 5:
- pointSet.resize( 3 );
- pointSet[1] = newPoint;
- break;
- case 6:
- pointSet[1] = p3;
- pointSet.resize( 3 );
- pointSet[2] = newPoint;
- break;
- case 7:
- pointSet[0] = p2;
- pointSet.resize( 3 );
- pointSet[2] = newPoint;
- break;
- case 8:
- pointSet[0] = p3;
- pointSet.resize( 3 );
- pointSet[2] = newPoint;
- break;
- case 9:
- pointSet[0] = p3;
- pointSet[1] = newPoint;
- pointSet.resize( 3 );
- break;
- case 10:
- pointSet.resize( 4 );
- pointSet[3] = newPoint;
- break;
- case 11:
- pointSet[2] = newPoint;
- pointSet.resize( 4 );
- break;
- case 12:
- pointSet[0] = newPoint;
- pointSet.resize( 4 );
- break;
- case 13:
- pointSet[1] = newPoint;
- pointSet.resize( 4 );
- break;
- }
- return updatedSphere;
- }
- Sphere UpdateSphere( std::vector< Vector >& pointSet, Vector& newPoint )
- {
- Sphere updatedSphere;
- switch( pointSet.size() )
- {
- case 1:
- updatedSphere = UpdateSphereOne( pointSet, newPoint );
- break;
- case 2:
- updatedSphere = UpdateSphereTwo( pointSet, newPoint );
- break;
- case 3:
- updatedSphere = UpdateSphereThree( pointSet, newPoint );
- break;
- case 4:
- updatedSphere = UpdateSphereFour( pointSet, newPoint );
- break;
- }
- return updatedSphere;
- }
- Sphere MyExporter::CalculateBoundingSphere( std::vector< MyVertex >& vertices )
- {
- std::vector< Vector > pointSet;
- pointSet.push_back( Vector( vertices[0].pos.x, vertices[0].pos.y, vertices[0].pos.z ) );
- Sphere boundingSphere = Sphere( Vector( vertices[0].pos.x, vertices[0].pos.y, vertices[0].pos.z ) );
- int index = 0;
- while( index < vertices.size() )
- {
- Vector vPos = Vector( vertices[index].pos.x, vertices[index].pos.y, vertices[index].pos.z );
- bool supportSetContains = false;
- for( size_t k1 = 0; k1 < pointSet.size(); ++k1 )
- {
- if( pointSet[k1] == vPos )
- supportSetContains = true;
- }
- if( !supportSetContains ) // if the current vertex is not in the pointset
- {
- if( !boundingSphere.Contains( vPos ) ) // if the current vertex is not in the bounding sphere
- {
- Sphere newCircle = UpdateSphere( pointSet, vPos );
- if( newCircle.radius > boundingSphere.radius )
- {
- boundingSphere = newCircle;
- index = 0;
- continue;
- }
- }
- }
- ++index;
- }
- return boundingSphere;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement