1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
18 #include <Bnd_Sphere.hxx>
20 Bnd_Sphere::Bnd_Sphere()
21 : myCenter (0., 0., 0.),
23 myIsValid (Standard_False),
28 Bnd_Sphere::Bnd_Sphere( const gp_XYZ& theCenter, const Standard_Real theRadius,
29 const Standard_Integer theU, const Standard_Integer theV )
30 : myCenter (theCenter),
32 myIsValid (Standard_False),
37 void Bnd_Sphere::SquareDistances( const gp_XYZ& theXYZ,
38 Standard_Real& theMin, Standard_Real& theMax ) const
40 theMax = ( theXYZ - myCenter ).SquareModulus();
41 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius * myRadius );
42 theMax += myRadius * myRadius;
45 void Bnd_Sphere::Distances( const gp_XYZ& theXYZ,
46 Standard_Real& theMin, Standard_Real& theMax ) const
48 theMax = ( theXYZ - myCenter ).Modulus();
49 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius );
53 Standard_Boolean Bnd_Sphere::Project(const gp_XYZ& theNode, gp_XYZ& theProjNode, Standard_Real& theDist, Standard_Boolean& theInside) const
55 theProjNode = myCenter;
56 theDist = ( theNode - theProjNode ).Modulus();
57 theInside = Standard_True;
61 Standard_Real Bnd_Sphere::Distance(const gp_XYZ& theNode) const
63 return ( theNode - myCenter ).Modulus();
66 Standard_Real Bnd_Sphere::SquareDistance(const gp_XYZ& theNode) const
68 return ( theNode - myCenter ).SquareModulus();
71 void Bnd_Sphere::Add( const Bnd_Sphere& theOther)
75 // not initialised yet
80 const Standard_Real aDist = (myCenter - theOther.myCenter).Modulus();
81 if ( myRadius + aDist <= theOther.myRadius )
83 // the other sphere is larger and encloses this
88 if ( theOther.myRadius + aDist <= myRadius )
89 return; // this sphere encloses other
92 const Standard_Real dfR = ( aDist + myRadius + theOther.myRadius ) * 0.5;
93 const Standard_Real aParamOnDiam = ( dfR - myRadius ) / aDist;
94 myCenter = myCenter * ( 1.0 - aParamOnDiam ) + theOther.myCenter * aParamOnDiam;
96 myIsValid = Standard_False;
99 Standard_Boolean Bnd_Sphere::IsOut( const Bnd_Sphere& theOther ) const
101 return (myCenter - theOther.myCenter).SquareModulus() > (myRadius + theOther.myRadius) * (myRadius + theOther.myRadius);
104 Standard_Boolean Bnd_Sphere::IsOut( const gp_XYZ& theXYZ,
105 Standard_Real& theMaxDist) const
107 Standard_Real aCurMinDist, aCurMaxDist;
108 Distances( theXYZ, aCurMinDist, aCurMaxDist );
109 if ( aCurMinDist > theMaxDist )
110 return Standard_True;
111 if( myIsValid && aCurMaxDist < theMaxDist )
112 theMaxDist = aCurMaxDist;
113 return Standard_False;
116 Standard_Real Bnd_Sphere::SquareExtent() const
118 return 4 * myRadius * myRadius;