Jpp
JGeometry3DToolkit.hh
Go to the documentation of this file.
1 #ifndef __JGEOMETRY3DTOOLKIT__
2 #define __JGEOMETRY3DTOOLKIT__
3 
4 #include <iterator>
5 
8 #include "JMath/JMathToolkit.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 /**
16  * Auxiliary classes and methods for 3D geometrical objects and operations.
17  */
18 namespace JGEOMETRY3D {}
19 namespace JPP { using namespace JGEOMETRY3D; }
20 
21 namespace JGEOMETRY3D {
22 
23  using JMATH::getDistance;
24 
25 
26  /**
27  * Center.
28  */
29  class JCenter3D :
30  public JPosition3D
31  {
32  public:
33  /**
34  * Constructor.
35  *
36  * \param p0 first position
37  * \param p1 second position
38  */
39  JCenter3D(const JVector3D& p0,
40  const JVector3D& p1) :
41  JPosition3D()
42  {
43  add(p0);
44  add(p1);
45 
46  div(2);
47  }
48 
49 
50  /**
51  * Constructor.
52  *
53  * \param p0 first position
54  * \param p1 second position
55  * \param p2 third position
56  */
57  JCenter3D(const JVector3D& p0,
58  const JVector3D& p1,
59  const JVector3D& p2) :
60  JPosition3D()
61  {
62  add(p0);
63  add(p1);
64  add(p2);
65 
66  div(3);
67  }
68 
69 
70  /**
71  * Constructor.
72  *
73  * \param __begin begin of data
74  * \param __end end of data
75  */
76  template<class T>
77  JCenter3D(T __begin,
78  T __end) :
79  JPosition3D()
80  {
81  if (__begin != __end) {
82 
83  for (T i = __begin; i != __end; ++i)
84  add(*i);
85 
86  div(std::distance(__begin, __end));
87  }
88  }
89  };
90 
91 
92  /**
93  * Weighed center.
94  */
96  public JPosition3D
97  {
98  public:
99  /**
100  * Constructor.
101  *
102  * \param __begin begin of data
103  * \param __end end of data
104  */
105  template<class T>
106  JWeighedCenter3D(T __begin,
107  T __end) :
108  JPosition3D()
109  {
110  if (__begin != __end) {
111 
112  double __w = 0.0;
113 
114  for (T i = __begin; i != __end; ++i) {
115  __x += i->getX() * i->getW();
116  __y += i->getY() * i->getW();
117  __z += i->getZ() * i->getW();
118  __w += i->getW();
119  }
120 
121  div(__w);
122  }
123  }
124  };
125 
126 
127  /**
128  * Auxiliary class for determination of smallest distance between pair of 3D points.
129  */
131  protected:
132  /**
133  * Recursive method to find the smallest distance.
134  *
135  * \param __begin begin of data
136  * \param __end end of data
137  * \return minimal distance
138  */
139  template<class T>
140  static double getDmin(T __begin, T __end)
141  {
142  using namespace std;
143 
144  const int N = distance(__begin, __end);
145 
146  if (N <= 3) {
147 
148  double Dmin = numeric_limits<double>::max();
149 
150  for (T i = __begin; i != __end; ++i) {
151  for (T j = i; ++j != __end; ) {
152 
153  const double d = getDistance(*i, *j);
154 
155  if (d < Dmin) {
156  Dmin = d;
157  }
158  }
159  }
160 
161  return Dmin;
162 
163  } else {
164 
165  T i = __begin;
166 
167  advance(i, N/2);
168 
169  const double dl = getDmin(__begin, i);
170  const double dr = getDmin(i, __end);
171 
172  const double Dmin = min(dl, dr);
173 
174  T il = i;
175  T ir = i;
176 
177  while (--il != __begin && i ->getZ() - il->getZ() < Dmin) {}
178  while (++ir != __end && ir->getZ() - i ->getZ() < Dmin) {}
179 
180  const double dz = JGEOMETRY2D::getSmallestDistance2D(++il, ir, Dmin);
181 
182  sort(il, ir, compareZ);
183 
184  return min(Dmin, dz);
185  }
186  }
187 
188 
189  /**
190  * Auxiliary class for sorting elements.
191  */
192  struct JCompareZ {
193  /**
194  * Compare x-positions of given points.
195  *
196  * \param first first point
197  * \param second second point
198  * \return true if x of first point less than that of second; else false
199  */
200  template<class T>
201  inline bool operator()(const T& first, const T& second) const
202  {
203  return first.getZ() < second.getZ();
204  }
205  };
206 
207 
208  public:
209  /**
210  * Default constructor.
211  */
213  {}
214 
215 
216  static const JCompareZ compareZ; //!< Function object for sorting elements.
217 
218 
219  /**
220  * Get smallest distance between two points.\n
221  * Note that this method changes the order of the elements.
222  *
223  * \param __begin begin of data
224  * \param __end end of data
225  * \return minimal distance
226  */
227  template<class T>
228  double operator()(T __begin, T __end) const
229  {
230  using namespace std;
231 
232  sort(__begin, __end, compareZ);
233 
234  return getDmin(__begin, __end);
235  }
236  };
237 
238 
239  /**
240  * Function object for smallest distance determination.
241  */
243 }
244 
245 #endif
246 
247 
JGEOMETRY3D::JSmallestDistance3D::JCompareZ
Auxiliary class for sorting elements.
Definition: JGeometry3DToolkit.hh:192
JGEOMETRY3D::JSmallestDistance3D
Auxiliary class for determination of smallest distance between pair of 3D points.
Definition: JGeometry3DToolkit.hh:130
JGEOMETRY3D::JCenter3D
Center.
Definition: JGeometry3DToolkit.hh:29
JROOT::advance
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
Definition: JCounter.hh:35
JGEOMETRY3D::JVector3D::__y
double __y
Definition: JVector3D.hh:309
JPosition3D.hh
JTOOLS::j
int j
Definition: JPolint.hh:634
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
JGeometry2DToolkit.hh
JGEOMETRY3D::JCenter3D::JCenter3D
JCenter3D(const JVector3D &p0, const JVector3D &p1)
Constructor.
Definition: JGeometry3DToolkit.hh:39
JGEOMETRY3D::JWeighedCenter3D::JWeighedCenter3D
JWeighedCenter3D(T __begin, T __end)
Constructor.
Definition: JGeometry3DToolkit.hh:106
JGEOMETRY3D::JSmallestDistance3D::JSmallestDistance3D
JSmallestDistance3D()
Default constructor.
Definition: JGeometry3DToolkit.hh:212
JGEOMETRY3D::getSmallestDistance3D
static const JSmallestDistance3D getSmallestDistance3D
Function object for smallest distance determination.
Definition: JGeometry3DToolkit.hh:242
JGEOMETRY3D::JSmallestDistance3D::operator()
double operator()(T __begin, T __end) const
Get smallest distance between two points.
Definition: JGeometry3DToolkit.hh:228
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JMathToolkit.hh
JGEOMETRY3D::JVector3D
Data structure for vector in three dimensions.
Definition: JVector3D.hh:33
JGEOMETRY3D::JWeighedCenter3D
Weighed center.
Definition: JGeometry3DToolkit.hh:95
JGEOMETRY3D::JPosition3D
Data structure for position in three dimensions.
Definition: JPosition3D.hh:35
JGEOMETRY3D::JSmallestDistance3D::compareZ
static const JCompareZ compareZ
Function object for sorting elements.
Definition: JGeometry3DToolkit.hh:216
JMATH::getDistance
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
Definition: JMathToolkit.hh:116
JGEOMETRY3D::JSmallestDistance3D::getDmin
static double getDmin(T __begin, T __end)
Recursive method to find the smallest distance.
Definition: JGeometry3DToolkit.hh:140
std
Definition: jaanetDictionary.h:36
JGEOMETRY3D::JSmallestDistance3D::JCompareZ::operator()
bool operator()(const T &first, const T &second) const
Compare x-positions of given points.
Definition: JGeometry3DToolkit.hh:201
JGEOMETRY3D::JCenter3D::JCenter3D
JCenter3D(const JVector3D &p0, const JVector3D &p1, const JVector3D &p2)
Constructor.
Definition: JGeometry3DToolkit.hh:57
JGEOMETRY3D::JCenter3D::JCenter3D
JCenter3D(T __begin, T __end)
Constructor.
Definition: JGeometry3DToolkit.hh:77
JGEOMETRY3D
Auxiliary classes and methods for 3D geometrical objects and operations.
Definition: JAngle3D.hh:18
JGEOMETRY3D::JVector3D::__z
double __z
Definition: JVector3D.hh:310
JGEOMETRY2D::getSmallestDistance2D
static const JSmallestDistance2D getSmallestDistance2D
Function object for smallest distance determination.
Definition: JGeometry2DToolkit.hh:660
p1
TPaveText * p1
Definition: JDrawModule3D.cc:35
JGEOMETRY3D::JVector3D::__x
double __x
Definition: JVector3D.hh:308
JGEOMETRY3D::JVector3D::add
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:141
JGEOMETRY3D::JVector3D::div
JVector3D & div(const double factor)
Scale vector.
Definition: JVector3D.hh:189