Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
geoalgo::Sphere Class Reference

#include <GeoSphere.h>

Public Member Functions

 Sphere ()
 Default ctor. More...
 
virtual ~Sphere ()
 Default dtor. More...
 
 Sphere (const double &x, const double &y, const double &z, const double &r)
 Alternative ctor (0) More...
 
 Sphere (const Point_t &center, const double r=0)
 Altenartive ctor (1) - 1 Point. More...
 
 Sphere (const Point_t &pt1, const Point_t &pt2)
 Alternative ctor (2) - 2 Points. More...
 
 Sphere (const Point_t &A, const Point_t &B, const Point_t &C)
 Alternative ctor (3) - 3 Points. More...
 
 Sphere (const Point_t &A, const Point_t &B, const Point_t &C, const Point_t &D)
 
 Sphere (const std::vector< ::geoalgo::Point_t > &pts)
 
const Point_tCenter () const
 Center getter. More...
 
double Radius () const
 Radius getter. More...
 
void Center (const double x, const double y, const double z)
 Center setter. More...
 
void Center (const Point_t &pt)
 Center setter. More...
 
void Radius (const double &r)
 Radius setter. More...
 
bool Contain (const Point_t &p) const
 Judge if a point is contained within a sphere. More...
 
template<class T >
 Sphere (const T &pt1, const T &pt2)
 
template<class T >
 Sphere (const T &A, const T &B, const T &C)
 
template<class T >
 Sphere (const T &A, const T &B, const T &C, const T &D)
 
template<class T >
 Sphere (const std::vector< T > &pts)
 
template<class T >
void Center (const T &pt)
 
template<class T >
bool Contain (const T &p) const
 

Protected Member Functions

void compat (const Point_t &p, const double r=0) const
 3D point compatibility check More...
 
void compat (const double &r) const
 Positive radius compatibility check. More...
 

Protected Attributes

Point_t _center
 Center of Sphere. More...
 
double _radius
 Radius of Sphere. More...
 

Detailed Description

Definition at line 27 of file GeoSphere.h.

Constructor & Destructor Documentation

geoalgo::Sphere::Sphere ( )

Default ctor.

Definition at line 8 of file GeoSphere.cxx.

8  : _center (3)
9  , _radius (0)
10  { for(auto& v : _center) v=0; }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
virtual geoalgo::Sphere::~Sphere ( )
inlinevirtual

Default dtor.

Definition at line 32 of file GeoSphere.h.

geoalgo::Sphere::Sphere ( const double &  x,
const double &  y,
const double &  z,
const double &  r 
)

Alternative ctor (0)

Definition at line 12 of file GeoSphere.cxx.

13  : Sphere()
14  { _center[0] = x; _center[1] = y; _center[2] = z; _radius = r; }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
list x
Definition: train.py:276
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
geoalgo::Sphere::Sphere ( const Point_t center,
const double  r = 0 
)

Altenartive ctor (1) - 1 Point.

Definition at line 16 of file GeoSphere.cxx.

17  : _radius (r)
18  {_center = center;}
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
def center(depos, point)
Definition: depos.py:117
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
geoalgo::Sphere::Sphere ( const Point_t pt1,
const Point_t pt2 
)

Alternative ctor (2) - 2 Points.

Definition at line 20 of file GeoSphere.cxx.

21  {
22  compat(pt1);
23  compat(pt2);
24  _center = (pt1+pt2)/2.;
25  _radius = pt1.Dist(pt2)/2.;
26  }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
void compat(const Point_t &p, const double r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:363
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
geoalgo::Sphere::Sphere ( const Point_t A,
const Point_t B,
const Point_t C 
)

Alternative ctor (3) - 3 Points.

Definition at line 31 of file GeoSphere.cxx.

32  {
33  compat(A);
34  compat(B);
35  compat(C);
36  // any three points are co-planar
37  // (if collinear no sphere passing through all 3)
38  // These 3 points make a triangle
39  // find the perpendicular bi-sectors to the segments
40  // making up the triangle. They will intersect
41  // at the sphere's center
42 
43  // check if collinear. If so return exception
44  Vector_t AB(B-A);
45  Vector_t AC(C-A);
46  Vector_t BC(C-B);
47 
48  double dABAB = AB.Dot(AB);
49  double dACAC = AC.Dot(AC);
50  double dABAC = AB.Dot(AC);
51 
52  double d = dABAB * dACAC - dABAC * dABAC;
53  double s,t;
54 
55  // if d == 0 they lie on one line
56  if (d == 0){
57  std::cout << "d is 0!" << std::endl;
58  double lenAB = AB.Length();
59  double lenAC = AC.Length();
60  double lenBC = BC.Length();
61  // which segment is longest?
62  if ( (lenAB > lenAC) && (lenAB > lenBC) ){
63  _center = (A+B)/2.;
64  _radius = _center.Dist(A);
65  }
66  else if( lenAC > lenBC ){
67  _center = (A+C)/2.;
68  _radius = _center.Dist(A);
69  }
70  else{
71  _center = (B+C)/2;
72  _radius = _center.Dist(B);
73  }
74  }// if d == 0
75 
76  else{
77  s = 0.5 * ( dABAB * dACAC - dACAC * dABAC ) / d;
78  t = 0.5 * ( dACAC * dABAB - dABAB * dABAC ) / d;
79 
80  // if s & t both > 0 && 1-s-t also > 0 then P = A + s*(B-A) + t*(C-A) is the center
81  if ( (s > 0) && (t > 0) && ((1-s-t) > 0) ){
82  _center = A+(B-A)*s+(C-A)*t;
83  _radius = _center.Dist(A);
84  }
85 
86  // otherwise only one will be negative. The side it belongs on will be
87  // the longest side and will determine the side to take as diameter
88  else if (s <= 0){
89  // side AB is the one
90  _center = (A+C)/2.;
91  _radius = _center.Dist(A);
92  }
93  else if (t <= 0){
94  // AC is the side
95  _center = (A+B)/2.;
96  _radius = _center.Dist(A);
97  }
98  else{
99  _center = (B+C)/2;
100  _radius = _center.Dist(B);
101  }
102  }// else (if d not equal to 0)
103 
104  }
double Dist(const Vector &obj) const
Compute the distance to another vector.
Definition: GeoVector.cxx:48
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
#define A
Definition: memgrp.cpp:38
void compat(const Point_t &p, const double r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:363
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
recob::tracking::Vector_t Vector_t
static QCString * s
Definition: config.cpp:1042
QTextStream & endl(QTextStream &s)
geoalgo::Sphere::Sphere ( const Point_t A,
const Point_t B,
const Point_t C,
const Point_t D 
)

Definition at line 183 of file GeoSphere.cxx.

183  {
184 
185  compat(A);
186  compat(B);
187  compat(C);
188  compat(D);
189 
190  // let's make sure there aren't duplicates...if so -> call a
191  // different constructor
192  std::vector<geoalgo::Point_t> valid_points = {A};
193  bool duplicate = false;
194  for (auto const& pt : valid_points){
195  if (pt.SqDist(B) < 0.0001)
196  duplicate = true;
197  }
198  if (duplicate == false)
199  valid_points.push_back(B);
200  duplicate = false;
201  for (auto const& pt : valid_points){
202  if (pt.SqDist(C) < 0.0001)
203  duplicate = true;
204  }
205  if (duplicate == false)
206  valid_points.push_back(C);
207  duplicate = false;
208  for (auto const& pt : valid_points){
209  if (pt.SqDist(D) < 0.0001)
210  duplicate = true;
211  }
212  if (duplicate == false)
213  valid_points.push_back(D);
214 
215  // if we have less then 4 points -> call the appropriate constructor
216  if (valid_points.size() < 4){
217  (*this) = Sphere(valid_points);
218  return;
219  }
220 
221  // get sphere from 3 points (A,B,C)
222  Vector_t AB(B-A);
223  Vector_t AC(C-A);
224  Vector_t AD(D-A);
225 
226  double dABAB = AB.Dot(AB);
227  double dACAC = AC.Dot(AC);
228  double dADAD = AD.Dot(AD);
229  double dABAC = AB.Dot(AC);
230  double dABAD = AB.Dot(AD);
231  double dACAD = AC.Dot(AD);
232 
233  double d = 4*dABAC*dABAD*dACAD;
234 
235  if (d==0){
236  // are any points duplicates? if so
237  // find the points that are collinear and call constructor
238  // for the
239  throw GeoAlgoException("GeoSphere Exception: I think it means 3 points collinear. Find out which and call 3 point constructor - TO DO");
240  }
241 
242  double s = (dABAC*dACAD*dADAD + dABAD*dACAC*dACAD - dABAB*dACAD*dACAD)/d;
243  double t = (dABAB*dACAD*dABAD + dABAD*dABAC*dADAD - dABAD*dABAD*dACAC)/d;
244  double u = (dABAB*dABAC*dACAD + dABAC*dABAD*dACAC - dABAC*dABAC*dADAD)/d;
245 
246  // if everything positive! P = A + s(B-A) + t(C-A) + u(D-A)
247  if ( (s > 0) && (t > 0) && (u > 0) && ((1-s-t-u) > 0) ){
248  _center = A + AB*s + AC*t + AD*u;
249  _radius = _center.Dist(A);
250  }
251  else{
252  // take the largest side and use it as the diameter
253  double maxdist = A.Dist(B);
254  Vector_t max1 = A;
255  Vector_t max2 = B;
256  if (A.Dist(C) > maxdist){
257  maxdist = A.Dist(C);
258  max1 = A;
259  max2 = C;
260  }
261  if (A.Dist(D) > maxdist){
262  maxdist = A.Dist(D);
263  max1 = A;
264  max2 = D;
265  }
266  if (B.Dist(C) > maxdist){
267  maxdist = B.Dist(C);
268  max1 = B;
269  max2 = C;
270  }
271  if (B.Dist(D) > maxdist){
272  maxdist = B.Dist(D);
273  max1 = B;
274  max2 = D;
275  }
276  if (C.Dist(D) > maxdist){
277  maxdist = C.Dist(D);
278  max1 = C;
279  max2 = D;
280  }
281  _center = (max1+max2)/2.;
282  _radius = max1.Dist(max2)/2.;
283  }
284 
285 
286  // TEMPORARY
287  // otherwise find the 4 possible sphere combinations,
288  // which contains the 4th point,
289  // and if multiple ones choose the one with the smallest radius
290  Sphere tmp = Sphere(A,B,C);
291  //_radius = kINVALID_DOUBLE;
292  if (tmp.Contain(D)){
293  _center = tmp.Center();
294  _radius = tmp.Radius();
295  }
296  tmp = Sphere(A,B,D);
297  if (tmp.Contain(C)){
298  if (tmp.Radius() < _radius){
299  _center = tmp.Center();
300  _radius = tmp.Radius();
301  }
302  }
303  tmp = Sphere(A,C,D);
304  if (tmp.Contain(B)){
305  if (tmp.Radius() < _radius){
306  _center = tmp.Center();
307  _radius = tmp.Radius();
308  }
309  }
310  tmp = Sphere(B,C,D);
311  if (tmp.Contain(A)){
312  if (tmp.Radius() < _radius){
313  _center = tmp.Center();
314  _radius = tmp.Radius();
315  }
316  }
317 
318  }
double Dist(const Vector &obj) const
Compute the distance to another vector.
Definition: GeoVector.cxx:48
#define D
Debug message.
Definition: tclscanner.cpp:775
string tmp
Definition: languages.py:63
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
#define A
Definition: memgrp.cpp:38
void compat(const Point_t &p, const double r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:363
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
recob::tracking::Vector_t Vector_t
static QCString * s
Definition: config.cpp:1042
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
geoalgo::Sphere::Sphere ( const std::vector< ::geoalgo::Point_t > &  pts)

Definition at line 322 of file GeoSphere.cxx.

323  : _center(0,0,0)
324  , _radius(0)
325  {
326 
327  switch(pts.size()) {
328  case 0:
329  break;
330  case 1: _center = pts.front();
331  break;
332  case 2: (*this) = Sphere(pts[0],pts[1]);
333  break;
334  case 3: (*this) = Sphere(pts[0],pts[1],pts[2]);
335  break;
336  case 4: (*this) = Sphere(pts[0],pts[1],pts[2],pts[3]);
337  break;
338  default:
339  throw GeoAlgoException("Cannot call Sphere constructor with more than 4 points. Something went wront");
340  }
341 
342  }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
template<class T >
geoalgo::Sphere::Sphere ( const T &  pt1,
const T &  pt2 
)
inline

Definition at line 93 of file GeoSphere.h.

94  : Sphere(Point_t(pt1), Point_t(pt2))
95  {}
Vector Point_t
Definition: GeoVector.h:196
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
template<class T >
geoalgo::Sphere::Sphere ( const T &  A,
const T &  B,
const T &  C 
)
inline

Definition at line 97 of file GeoSphere.h.

98  : Sphere(Point_t(A), Point_t(B), Point_t(C))
99  {}
Vector Point_t
Definition: GeoVector.h:196
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
template<class T >
geoalgo::Sphere::Sphere ( const T &  A,
const T &  B,
const T &  C,
const T &  D 
)
inline

Definition at line 101 of file GeoSphere.h.

103  {}
Vector Point_t
Definition: GeoVector.h:196
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
template<class T >
geoalgo::Sphere::Sphere ( const std::vector< T > &  pts)
inline

Definition at line 105 of file GeoSphere.h.

106  {
107  std::vector< ::geoalgo::Vector> geo_pts;
108  geo_pts.reserve(pts);
109  for(auto const& p : pts) geo_pts.emplace_back(p);
110  (*this) = Sphere(geo_pts);
111  }
p
Definition: test.py:223
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8

Member Function Documentation

const Point_t & geoalgo::Sphere::Center ( ) const

Center getter.

Definition at line 344 of file GeoSphere.cxx.

344 { return _center; }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
void geoalgo::Sphere::Center ( const double  x,
const double  y,
const double  z 
)

Center setter.

Definition at line 348 of file GeoSphere.cxx.

349  { _center[0] = x; _center[1] = y; _center[2] = z; }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
list x
Definition: train.py:276
void geoalgo::Sphere::Center ( const Point_t pt)

Center setter.

Definition at line 351 of file GeoSphere.cxx.

352  { compat(center); _center = center; }
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
def center(depos, point)
Definition: depos.py:117
void compat(const Point_t &p, const double r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:363
template<class T >
void geoalgo::Sphere::Center ( const T &  pt)
inline

Definition at line 113 of file GeoSphere.h.

114  { Center(Point_t(pt)); }
const Point_t & Center() const
Center getter.
Definition: GeoSphere.cxx:344
Vector Point_t
Definition: GeoVector.h:196
void geoalgo::Sphere::compat ( const Point_t p,
const double  r = 0 
) const
protected

3D point compatibility check

Definition at line 363 of file GeoSphere.cxx.

364  {
365  if(p.size()!=3) throw GeoAlgoException("Only 3D points allowed for sphere");
366  compat(r);
367  }
p
Definition: test.py:223
void compat(const Point_t &p, const double r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:363
void geoalgo::Sphere::compat ( const double &  r) const
protected

Positive radius compatibility check.

Definition at line 369 of file GeoSphere.cxx.

370  { if(r<0) throw GeoAlgoException("Only positive value allowed for radius"); }
bool geoalgo::Sphere::Contain ( const Point_t p) const

Judge if a point is contained within a sphere.

Definition at line 357 of file GeoSphere.cxx.

358  {
359  _center.compat(p);
360  return ( p._Dist_(_center) < _radius );
361  }
void compat(const Vector &obj) const
Dimensional check for a compatibility.
Definition: GeoVector.cxx:97
p
Definition: test.py:223
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:76
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
template<class T >
bool geoalgo::Sphere::Contain ( const T &  p) const
inline

Definition at line 116 of file GeoSphere.h.

117  { return Contain(Point_t(p)); }
bool Contain(const Point_t &p) const
Judge if a point is contained within a sphere.
Definition: GeoSphere.cxx:357
p
Definition: test.py:223
Vector Point_t
Definition: GeoVector.h:196
double geoalgo::Sphere::Radius ( ) const

Radius getter.

Definition at line 346 of file GeoSphere.cxx.

346 { return _radius; }
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79
void geoalgo::Sphere::Radius ( const double &  r)

Radius setter.

Definition at line 354 of file GeoSphere.cxx.

355  { compat(r); _radius = r; }
void compat(const Point_t &p, const double r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:363
double _radius
Radius of Sphere.
Definition: GeoSphere.h:79

Member Data Documentation

Point_t geoalgo::Sphere::_center
protected

Center of Sphere.

Definition at line 76 of file GeoSphere.h.

double geoalgo::Sphere::_radius
protected

Radius of Sphere.

Definition at line 79 of file GeoSphere.h.


The documentation for this class was generated from the following files: