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