0024042: Performance improvements: Foundation Classes
[occt.git] / src / Bnd / Bnd_Sphere.cxx
CommitLineData
b311480e 1// Copyright (c) 1999-2012 OPEN CASCADE SAS
2//
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.
7//
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.
10//
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.
17
92d1589b
A
18#include <Bnd_Sphere.hxx>
19
20Bnd_Sphere::Bnd_Sphere()
21 : myCenter (0., 0., 0.),
22 myRadius (0.),
23 myIsValid (Standard_False),
24 myU (0),
25 myV (0)
26{}
27
28Bnd_Sphere::Bnd_Sphere( const gp_XYZ& theCenter, const Standard_Real theRadius,
29 const Standard_Integer theU, const Standard_Integer theV )
30 : myCenter (theCenter),
31 myRadius (theRadius),
32 myIsValid (Standard_False),
33 myU (theU),
34 myV (theV)
35{}
36
37void Bnd_Sphere::SquareDistances( const gp_XYZ& theXYZ,
38 Standard_Real& theMin, Standard_Real& theMax ) const
39{
40 theMax = ( theXYZ - myCenter ).SquareModulus();
41 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius * myRadius );
42 theMax += myRadius * myRadius;
43}
44
45void Bnd_Sphere::Distances( const gp_XYZ& theXYZ,
46 Standard_Real& theMin, Standard_Real& theMax ) const
47{
48 theMax = ( theXYZ - myCenter ).Modulus();
49 theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius );
50 theMax += myRadius;
51}
52
53Standard_Boolean Bnd_Sphere::Project(const gp_XYZ& theNode, gp_XYZ& theProjNode, Standard_Real& theDist, Standard_Boolean& theInside) const
54{
55 theProjNode = myCenter;
56 theDist = ( theNode - theProjNode ).Modulus();
57 theInside = Standard_True;
58 return Standard_True;
59}
60
61Standard_Real Bnd_Sphere::Distance(const gp_XYZ& theNode) const
62{
63 return ( theNode - myCenter ).Modulus();
64}
65
66Standard_Real Bnd_Sphere::SquareDistance(const gp_XYZ& theNode) const
67{
68 return ( theNode - myCenter ).SquareModulus();
69}
70
71void Bnd_Sphere::Add( const Bnd_Sphere& theOther)
72{
73 if ( myRadius < 0.0 )
74 {
75 // not initialised yet
76 *this = theOther;
77 return;
78 }
79
80 const Standard_Real aDist = (myCenter - theOther.myCenter).Modulus();
81 if ( myRadius + aDist <= theOther.myRadius )
82 {
83 // the other sphere is larger and encloses this
84 *this = theOther;
85 return;
86 }
87
88 if ( theOther.myRadius + aDist <= myRadius )
89 return; // this sphere encloses other
90
91 // expansion
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;
95 myRadius = dfR;
96 myIsValid = Standard_False;
97}
98
99Standard_Boolean Bnd_Sphere::IsOut( const Bnd_Sphere& theOther ) const
100{
101 return (myCenter - theOther.myCenter).SquareModulus() > (myRadius + theOther.myRadius) * (myRadius + theOther.myRadius);
102}
103
104Standard_Boolean Bnd_Sphere::IsOut( const gp_XYZ& theXYZ,
105 Standard_Real& theMaxDist) const
106{
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;
114}
115
116Standard_Real Bnd_Sphere::SquareExtent() const
117{
118 return 4 * myRadius * myRadius;
119}