summaryrefslogtreecommitdiff
path: root/src/Bnd/Bnd_Sphere.cxx
blob: 5217d4818a1700eff24de570593789a85cd3e619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <Bnd_Sphere.hxx>

Bnd_Sphere::Bnd_Sphere()
  : myCenter (0., 0., 0.),
    myRadius (0.),
    myIsValid (Standard_False),
    myU (0),
    myV (0)
{}

Bnd_Sphere::Bnd_Sphere( const gp_XYZ& theCenter, const Standard_Real theRadius,
                        const Standard_Integer theU, const Standard_Integer theV )
  : myCenter (theCenter),
    myRadius (theRadius),
    myIsValid (Standard_False),
    myU (theU),
    myV (theV)
{}

void Bnd_Sphere::SquareDistances( const gp_XYZ& theXYZ,
                                  Standard_Real& theMin, Standard_Real& theMax ) const
{
  theMax = ( theXYZ - myCenter ).SquareModulus();
  theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius * myRadius );
  theMax += myRadius * myRadius;
}

void Bnd_Sphere::Distances( const gp_XYZ& theXYZ,
                            Standard_Real& theMin, Standard_Real& theMax ) const
{
  theMax = ( theXYZ - myCenter ).Modulus();
  theMin = ( theMax - myRadius <0 ? 0.0 : theMax - myRadius );
  theMax += myRadius;
}

Standard_Boolean Bnd_Sphere::Project(const gp_XYZ& theNode, gp_XYZ& theProjNode, Standard_Real& theDist, Standard_Boolean& theInside) const
{ 
  theProjNode = myCenter;
  theDist = ( theNode - theProjNode ).Modulus();
  theInside = Standard_True;
  return Standard_True;
}

Standard_Real Bnd_Sphere::Distance(const gp_XYZ& theNode) const
{
  return ( theNode - myCenter ).Modulus();
}

Standard_Real Bnd_Sphere::SquareDistance(const gp_XYZ& theNode) const
{
  return ( theNode - myCenter ).SquareModulus();
}

void Bnd_Sphere::Add( const Bnd_Sphere& theOther)
{
  if ( myRadius < 0.0 )
  {
    // not initialised yet
    *this = theOther;
    return;
  }

  const Standard_Real aDist = (myCenter - theOther.myCenter).Modulus();
  if ( myRadius + aDist <= theOther.myRadius )
  {
    // the other sphere is larger and encloses this
    *this = theOther;
    return;
  }

  if ( theOther.myRadius + aDist <= myRadius )
    return; // this sphere encloses other

  // expansion
  const Standard_Real dfR = ( aDist + myRadius + theOther.myRadius ) * 0.5;
  const Standard_Real aParamOnDiam = ( dfR - myRadius ) / aDist;
  myCenter = myCenter * ( 1.0 - aParamOnDiam ) + theOther.myCenter * aParamOnDiam;
  myRadius = dfR;
  myIsValid = Standard_False;
}

Standard_Boolean Bnd_Sphere::IsOut( const Bnd_Sphere& theOther ) const
{ 
  return (myCenter - theOther.myCenter).SquareModulus() > (myRadius + theOther.myRadius) * (myRadius + theOther.myRadius); 
}

Standard_Boolean Bnd_Sphere::IsOut( const gp_XYZ& theXYZ,
                                    Standard_Real& theMaxDist) const
{
  Standard_Real aCurMinDist, aCurMaxDist;
  Distances( theXYZ, aCurMinDist, aCurMaxDist );
  if ( aCurMinDist > theMaxDist )
    return Standard_True;
  if( myIsValid && aCurMaxDist < theMaxDist )
    theMaxDist = aCurMaxDist;
  return Standard_False;
}

Standard_Real Bnd_Sphere::SquareExtent() const
{ 
  return 4 * myRadius * myRadius; 
}