1 #include <Bnd_Sphere.hxx>
3 Bnd_Sphere::Bnd_Sphere()
4 : myCenter (0., 0., 0.),
6 myIsValid (Standard_False),
11 Bnd_Sphere::Bnd_Sphere( const gp_XYZ& theCenter, const Standard_Real theRadius,
12 const Standard_Integer theU, const Standard_Integer theV )
13 : myCenter (theCenter),
15 myIsValid (Standard_False),
20 void Bnd_Sphere::SquareDistances( const gp_XYZ& theXYZ,
21 Standard_Real& theMin, Standard_Real& theMax ) const
23 theMax = ( theXYZ - myCenter ).SquareModulus();
24 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius * myRadius );
25 theMax += myRadius * myRadius;
28 void Bnd_Sphere::Distances( const gp_XYZ& theXYZ,
29 Standard_Real& theMin, Standard_Real& theMax ) const
31 theMax = ( theXYZ - myCenter ).Modulus();
32 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius );
36 Standard_Boolean Bnd_Sphere::Project(const gp_XYZ& theNode, gp_XYZ& theProjNode, Standard_Real& theDist, Standard_Boolean& theInside) const
38 theProjNode = myCenter;
39 theDist = ( theNode - theProjNode ).Modulus();
40 theInside = Standard_True;
44 Standard_Real Bnd_Sphere::Distance(const gp_XYZ& theNode) const
46 return ( theNode - myCenter ).Modulus();
49 Standard_Real Bnd_Sphere::SquareDistance(const gp_XYZ& theNode) const
51 return ( theNode - myCenter ).SquareModulus();
54 void Bnd_Sphere::Add( const Bnd_Sphere& theOther)
58 // not initialised yet
63 const Standard_Real aDist = (myCenter - theOther.myCenter).Modulus();
64 if ( myRadius + aDist <= theOther.myRadius )
66 // the other sphere is larger and encloses this
71 if ( theOther.myRadius + aDist <= myRadius )
72 return; // this sphere encloses other
75 const Standard_Real dfR = ( aDist + myRadius + theOther.myRadius ) * 0.5;
76 const Standard_Real aParamOnDiam = ( dfR - myRadius ) / aDist;
77 myCenter = myCenter * ( 1.0 - aParamOnDiam ) + theOther.myCenter * aParamOnDiam;
79 myIsValid = Standard_False;
82 Standard_Boolean Bnd_Sphere::IsOut( const Bnd_Sphere& theOther ) const
84 return (myCenter - theOther.myCenter).SquareModulus() > (myRadius + theOther.myRadius) * (myRadius + theOther.myRadius);
87 Standard_Boolean Bnd_Sphere::IsOut( const gp_XYZ& theXYZ,
88 Standard_Real& theMaxDist) const
90 Standard_Real aCurMinDist, aCurMaxDist;
91 Distances( theXYZ, aCurMinDist, aCurMaxDist );
92 if ( aCurMinDist > theMaxDist )
94 if( myIsValid && aCurMaxDist < theMaxDist )
95 theMaxDist = aCurMaxDist;
96 return Standard_False;
99 Standard_Real Bnd_Sphere::SquareExtent() const
101 return 4 * myRadius * myRadius;