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 
87  div(std::distance(__begin, __end));
88  }
89  }
90  };
91 
92 
93  /**
94  * Weighed center.
95  */
97  public JPosition3D
98  {
99  public:
100  /**
101  * Constructor.
102  *
103  * \param __begin begin of data
104  * \param __end end of data
105  */
106  template<class T>
107  JWeighedCenter3D(T __begin,
108  T __end) :
109  JPosition3D()
110  {
111  if (__begin != __end) {
112 
113  double __w = 0.0;
114 
115  for (T i = __begin; i != __end; ++i) {
116  __x += i->getX() * i->getW();
117  __y += i->getY() * i->getW();
118  __z += i->getZ() * i->getW();
119  __w += i->getW();
120  }
121 
122  div(__w);
123  }
124  }
125  };
126 
127 
128  /**
129  * Auxiliary class for determination of smallest distance between pair of 3D points.
130  */
132  protected:
133  /**
134  * Recursive method to find the smallest distance.
135  *
136  * \param __begin begin of data
137  * \param __end end of data
138  * \return minimal distance
139  */
140  template<class T>
141  static double getDmin(T __begin, T __end)
142  {
143  using namespace std;
144 
145  const int N = distance(__begin, __end);
146 
147  if (N <= 3) {
148 
149  double Dmin = numeric_limits<double>::max();
150 
151  for (T i = __begin; i != __end; ++i) {
152  for (T j = i; ++j != __end; ) {
153 
154  const double d = getDistance(*i, *j);
155 
156  if (d < Dmin) {
157  Dmin = d;
158  }
159  }
160  }
161 
162  return Dmin;
163 
164  } else {
165 
166  T i = __begin;
167 
168  advance(i, N/2);
169 
170  const double dl = getDmin(__begin, i);
171  const double dr = getDmin(i, __end);
172 
173  const double Dmin = min(dl, dr);
174 
175  T il = i;
176  T ir = i;
177 
178  while (--il != __begin && i ->getZ() - il->getZ() < Dmin) {}
179  while (++ir != __end && ir->getZ() - i ->getZ() < Dmin) {}
180 
181  const double dz = JGEOMETRY2D::getSmallestDistance2D(++il, ir, Dmin);
182 
183  sort(il, ir, compareZ);
184 
185  return min(Dmin, dz);
186  }
187  }
188 
189 
190  /**
191  * Auxiliary class for sorting elements.
192  */
193  struct JCompareZ {
194  /**
195  * Compare x-positions of given points.
196  *
197  * \param first first point
198  * \param second second point
199  * \return true if x of first point less than that of second; else false
200  */
201  template<class T>
202  inline bool operator()(const T& first, const T& second) const
203  {
204  return first.getZ() < second.getZ();
205  }
206  };
207 
208 
209  public:
210  /**
211  * Default constructor.
212  */
214  {}
215 
216 
217  static const JCompareZ compareZ; //!< Function object for sorting elements.
218 
219 
220  /**
221  * Get smallest distance between two points.\n
222  * Note that this method changes the order of the elements.
223  *
224  * \param __begin begin of data
225  * \param __end end of data
226  * \return minimal distance
227  */
228  template<class T>
229  double operator()(T __begin, T __end) const
230  {
231  using namespace std;
232 
233  sort(__begin, __end, compareZ);
234 
235  return getDmin(__begin, __end);
236  }
237  };
238 
239 
240  /**
241  * Function object for smallest distance determination.
242  */
244 }
245 
246 #endif
247 
248 
JGEOMETRY3D::JSmallestDistance3D::JCompareZ
Auxiliary class for sorting elements.
Definition: JGeometry3DToolkit.hh:193
JGEOMETRY3D::JSmallestDistance3D
Auxiliary class for determination of smallest distance between pair of 3D points.
Definition: JGeometry3DToolkit.hh:131
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:310
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:107
JGEOMETRY3D::JSmallestDistance3D::JSmallestDistance3D
JSmallestDistance3D()
Default constructor.
Definition: JGeometry3DToolkit.hh:213
JGEOMETRY3D::getSmallestDistance3D
static const JSmallestDistance3D getSmallestDistance3D
Function object for smallest distance determination.
Definition: JGeometry3DToolkit.hh:243
JGEOMETRY3D::JSmallestDistance3D::operator()
double operator()(T __begin, T __end) const
Get smallest distance between two points.
Definition: JGeometry3DToolkit.hh:229
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:96
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:217
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:141
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:202
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:311
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:309
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