1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <Bnd_Sphere.hxx>
16 Bnd_Sphere::Bnd_Sphere()
17 : myCenter (0., 0., 0.),
19 myIsValid (Standard_False),
24 Bnd_Sphere::Bnd_Sphere( const gp_XYZ& theCenter, const Standard_Real theRadius,
25 const Standard_Integer theU, const Standard_Integer theV )
26 : myCenter (theCenter),
28 myIsValid (Standard_False),
33 void Bnd_Sphere::SquareDistances( const gp_XYZ& theXYZ,
34 Standard_Real& theMin, Standard_Real& theMax ) const
36 theMax = ( theXYZ - myCenter ).SquareModulus();
37 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius * myRadius );
38 theMax += myRadius * myRadius;
41 void Bnd_Sphere::Distances( const gp_XYZ& theXYZ,
42 Standard_Real& theMin, Standard_Real& theMax ) const
44 theMax = ( theXYZ - myCenter ).Modulus();
45 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius );
49 Standard_Boolean Bnd_Sphere::Project(const gp_XYZ& theNode, gp_XYZ& theProjNode, Standard_Real& theDist, Standard_Boolean& theInside) const
51 theProjNode = myCenter;
52 theDist = ( theNode - theProjNode ).Modulus();
53 theInside = Standard_True;
57 Standard_Real Bnd_Sphere::Distance(const gp_XYZ& theNode) const
59 return ( theNode - myCenter ).Modulus();
62 Standard_Real Bnd_Sphere::SquareDistance(const gp_XYZ& theNode) const
64 return ( theNode - myCenter ).SquareModulus();
67 void Bnd_Sphere::Add( const Bnd_Sphere& theOther)
71 // not initialised yet
76 const Standard_Real aDist = (myCenter - theOther.myCenter).Modulus();
77 if ( myRadius + aDist <= theOther.myRadius )
79 // the other sphere is larger and encloses this
84 if ( theOther.myRadius + aDist <= myRadius )
85 return; // this sphere encloses other
88 const Standard_Real dfR = ( aDist + myRadius + theOther.myRadius ) * 0.5;
89 const Standard_Real aParamOnDiam = ( dfR - myRadius ) / aDist;
90 myCenter = myCenter * ( 1.0 - aParamOnDiam ) + theOther.myCenter * aParamOnDiam;
92 myIsValid = Standard_False;
95 Standard_Boolean Bnd_Sphere::IsOut( const Bnd_Sphere& theOther ) const
97 return (myCenter - theOther.myCenter).SquareModulus() > (myRadius + theOther.myRadius) * (myRadius + theOther.myRadius);
100 Standard_Boolean Bnd_Sphere::IsOut( const gp_XYZ& theXYZ,
101 Standard_Real& theMaxDist) const
103 Standard_Real aCurMinDist, aCurMaxDist;
104 Distances( theXYZ, aCurMinDist, aCurMaxDist );
105 if ( aCurMinDist > theMaxDist )
106 return Standard_True;
107 if( myIsValid && aCurMaxDist < theMaxDist )
108 theMaxDist = aCurMaxDist;
109 return Standard_False;
112 Standard_Real Bnd_Sphere::SquareExtent() const
114 return 4 * myRadius * myRadius;