LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
geoalgo::Sphere Class Reference

#include "GeoSphere.h"

Public Member Functions

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

Protected Member Functions

void compat (Point_t const &p, double const r=0) const
 3D point compatibility check More...
 
void compat (double const &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.

References _center.

Referenced by Sphere(), and ~Sphere().

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

Default dtor.

Definition at line 31 of file GeoSphere.h.

References Center(), compat(), Contain(), pt, pt1, pt2, r, Radius(), Sphere(), x, y, and z.

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

Alternative ctor (0)

Definition at line 14 of file GeoSphere.cxx.

References _center, _radius, r, x, y, and z.

14  : Sphere()
15  {
16  _center[0] = x;
17  _center[1] = y;
18  _center[2] = z;
19  _radius = r;
20  }
Float_t x
Definition: compare.C:6
TRandom r
Definition: spectrum.C:23
Float_t y
Definition: compare.C:6
Double_t z
Definition: plot.C:276
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:74
double _radius
Radius of Sphere.
Definition: GeoSphere.h:77
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
geoalgo::Sphere::Sphere ( Point_t const &  center,
double const  r = 0 
)

Altenartive ctor (1) - 1 Point.

Definition at line 22 of file GeoSphere.cxx.

References _center.

22  : _radius(r)
23  {
24  _center = center;
25  }
TRandom r
Definition: spectrum.C:23
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:74
double _radius
Radius of Sphere.
Definition: GeoSphere.h:77
geoalgo::Sphere::Sphere ( Point_t const &  pt1,
Point_t const &  pt2 
)

Alternative ctor (2) - 2 Points.

Definition at line 27 of file GeoSphere.cxx.

References _center, _radius, compat(), geoalgo::Vector::Dist(), and pt2.

28  {
29  compat(pt1);
30  compat(pt2);
31  _center = (pt1 + pt2) / 2.;
32  _radius = pt1.Dist(pt2) / 2.;
33  }
TText * pt2
Definition: plot.C:64
void compat(Point_t const &p, double const r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:371
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:74
double _radius
Radius of Sphere.
Definition: GeoSphere.h:77
TText * pt1
Definition: plot.C:61
geoalgo::Sphere::Sphere ( Point_t const &  A,
Point_t const &  B,
Point_t const &  C 
)

Alternative ctor (3) - 3 Points.

Definition at line 38 of file GeoSphere.cxx.

References _center, _radius, compat(), d, geoalgo::Vector::Dist(), geoalgo::Vector::Dot(), and geoalgo::Vector::Length().

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

Definition at line 189 of file GeoSphere.cxx.

References _center, _radius, Center(), compat(), Contain(), d, geoalgo::Vector::Dist(), geoalgo::Vector::Dot(), pt, Radius(), Sphere(), and tmp.

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

Definition at line 321 of file GeoSphere.cxx.

References _center, and Sphere().

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

Definition at line 91 of file GeoSphere.h.

92  {}
TText * pt2
Definition: plot.C:64
Vector Point_t
Definition: GeoVector.h:204
TText * pt1
Definition: plot.C:61
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
template<class T >
geoalgo::Sphere::Sphere ( T const &  A,
T const &  B,
T const &  C 
)
inline

Definition at line 95 of file GeoSphere.h.

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

Definition at line 99 of file GeoSphere.h.

100  : Sphere(Point_t(A), Point_t(B), Point_t(C), Point_t(D))
101  {}
Vector Point_t
Definition: GeoVector.h:204
Sphere()
Default ctor.
Definition: GeoSphere.cxx:8
template<class T >
geoalgo::Sphere::Sphere ( const std::vector< T > &  pts)
inline

Definition at line 104 of file GeoSphere.h.

References Sphere().

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

Member Function Documentation

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

Center getter.

Definition at line 336 of file GeoSphere.cxx.

References _center.

Referenced by geoalgo::GeoAlgo::_RemainingPoints_(), geoalgo::GeoObjCollection::Add(), Center(), Sphere(), and ~Sphere().

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

Center setter.

Definition at line 346 of file GeoSphere.cxx.

References _center, x, y, and z.

347  {
348  _center[0] = x;
349  _center[1] = y;
350  _center[2] = z;
351  }
Float_t x
Definition: compare.C:6
Float_t y
Definition: compare.C:6
Double_t z
Definition: plot.C:276
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:74
void geoalgo::Sphere::Center ( Point_t const &  pt)

Center setter.

Definition at line 353 of file GeoSphere.cxx.

References _center, and compat().

354  {
355  compat(center);
356  _center = center;
357  }
void compat(Point_t const &p, double const r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:371
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:74
template<class T >
void geoalgo::Sphere::Center ( T const &  pt)
inline

Definition at line 114 of file GeoSphere.h.

References Center().

115  {
116  Center(Point_t(pt));
117  }
Point_t const & Center() const
Center getter.
Definition: GeoSphere.cxx:336
TMarker * pt
Definition: egs.C:25
Vector Point_t
Definition: GeoVector.h:204
void geoalgo::Sphere::compat ( Point_t const &  p,
double const  r = 0 
) const
protected

3D point compatibility check

Definition at line 371 of file GeoSphere.cxx.

Referenced by geoalgo::GeoAlgo::boundingSphere(), Center(), Radius(), Sphere(), and ~Sphere().

372  {
373  if (p.size() != 3) throw GeoAlgoException("Only 3D points allowed for sphere");
374  compat(r);
375  }
TRandom r
Definition: spectrum.C:23
void compat(Point_t const &p, double const r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:371
void geoalgo::Sphere::compat ( double const &  r) const
protected

Positive radius compatibility check.

Definition at line 377 of file GeoSphere.cxx.

378  {
379  if (r < 0) throw GeoAlgoException("Only positive value allowed for radius");
380  }
TRandom r
Definition: spectrum.C:23
bool geoalgo::Sphere::Contain ( Point_t const &  p) const

Judge if a point is contained within a sphere.

Definition at line 365 of file GeoSphere.cxx.

References _center, geoalgo::Vector::_Dist_(), _radius, and geoalgo::Vector::compat().

Referenced by geoalgo::GeoAlgo::_RemainingPoints_(), geoalgo::GeoAlgo::_WelzlSphere_(), Contain(), Sphere(), and ~Sphere().

366  {
367  _center.compat(p);
368  return (p._Dist_(_center) < _radius);
369  }
void compat(Vector const &obj) const
Dimensional check for a compatibility.
Definition: GeoVector.cxx:128
Point_t _center
Center of Sphere.
Definition: GeoSphere.h:74
double _radius
Radius of Sphere.
Definition: GeoSphere.h:77
template<class T >
bool geoalgo::Sphere::Contain ( T const &  p) const
inline

Definition at line 120 of file GeoSphere.h.

References Contain().

121  {
122  return Contain(Point_t(p));
123  }
Vector Point_t
Definition: GeoVector.h:204
bool Contain(Point_t const &p) const
Judge if a point is contained within a sphere.
Definition: GeoSphere.cxx:365
double geoalgo::Sphere::Radius ( ) const

Radius getter.

Definition at line 341 of file GeoSphere.cxx.

References _radius.

Referenced by geoalgo::GeoAlgo::_RemainingPoints_(), Sphere(), and ~Sphere().

342  {
343  return _radius;
344  }
double _radius
Radius of Sphere.
Definition: GeoSphere.h:77
void geoalgo::Sphere::Radius ( double const &  r)

Radius setter.

Definition at line 359 of file GeoSphere.cxx.

References _radius, compat(), and r.

360  {
361  compat(r);
362  _radius = r;
363  }
TRandom r
Definition: spectrum.C:23
void compat(Point_t const &p, double const r=0) const
3D point compatibility check
Definition: GeoSphere.cxx:371
double _radius
Radius of Sphere.
Definition: GeoSphere.h:77

Member Data Documentation

Point_t geoalgo::Sphere::_center
protected

Center of Sphere.

Definition at line 74 of file GeoSphere.h.

Referenced by Center(), Contain(), and Sphere().

double geoalgo::Sphere::_radius
protected

Radius of Sphere.

Definition at line 77 of file GeoSphere.h.

Referenced by Contain(), Radius(), and Sphere().


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