Jpp  15.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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>
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 
static const JSmallestDistance3D getSmallestDistance3D
Function object for smallest distance determination.
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.
JSmallestDistance3D()
Default constructor.
static const JSmallestDistance2D getSmallestDistance2D
Function object for smallest distance determination.
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
static double getDmin(T __begin, T __end)
Recursive method to find the smallest distance.
JCenter3D(T __begin, T __end)
Constructor.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
static const JCompareZ compareZ
Function object for sorting elements.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
counter_type advance(counter_type &counter, const counter_type value, const counter_type limit=std::numeric_limits< counter_type >::max())
Advance counter.
double operator()(T __begin, T __end) const
Get smallest distance between two points.
p2
Definition: module-Z:fit.sh:74
then JMuonMCEvt f $INPUT_FILE o $INTERMEDIATE_FILE d
Definition: JMuonPath.sh:47
JCenter3D(const JVector3D &p0, const JVector3D &p1, const JVector3D &p2)
Constructor.
JCenter3D(const JVector3D &p0, const JVector3D &p1)
Constructor.
Auxiliary class for sorting elements.
int j
Definition: JPolint.hh:666
Data structure for position in three dimensions.
Definition: JPosition3D.hh:36
JVector3D & div(const double factor)
Scale vector.
Definition: JVector3D.hh:190
JWeighedCenter3D(T __begin, T __end)
Constructor.
bool operator()(const T &first, const T &second) const
Compare x-positions of given points.
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:142
Auxiliary class for determination of smallest distance between pair of 3D points. ...