ATLAS Offline Software
Loading...
Searching...
No Matches
cyl_geom.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
5// emacs this is -*-C++-*-
6#ifndef _CYL_GEOM_HH_
7#define _CYL_GEOM_HH_
8
16
17#include <set>
18#include <list>
19#include <vector>
20#include <cmath>
21
22namespace JetGeom {
23
25class point_t : public std::pair<float , float>{
26public:
27 point_t(float x, float y){first = x; second = y;}
28};
29class point_set_t : public std::set<point_t> {};
30class point_vect_t : public std::vector<point_t> {};
31
32typedef std::list<point_t> point_list_t;
33
34
37class line_t {
38public:
39
43 line_t(float cx, float cy, float cc, bool to_r) : m_cx(cx),m_cy(cy),m_cc(cc), m_oriented_r(to_r) {};
45 line_t(point_t p1, point_t p2);
46
47 float m_cx, m_cy, m_cc;
49
50
51 bool is_above(point_t &p);
52 bool is_below(point_t &p);
53
54 bool is_left(point_t &p) {return m_oriented_r ? is_above(p) : is_below(p);};
55 bool is_right(point_t &p) {return m_oriented_r ? is_below(p) : is_above(p);};
56
57 void phi_shift(float dphi) {m_cc = m_cc - m_cy*dphi;}
58
59 point_t intercept_y(float y);
60 point_t intercept_x(float x);
61
62};
63
64
65
66
69template< class inT, class ouT>
70void findConvexHull(inT &inSet, ouT &outSet );
72template<class ouT>
73void findConvexHull(point_set_t &inSet, ouT &outSet );
74// internally used by findConvexHull
75template< class inT>
76void _findConvexHull(point_set_t &inSet, inT &outSet );
77
78// internally used by findConvexHull
79void testHullLine(point_list_t &hull, point_t p);
80
81
82template<class T>
83float polygon_area(T& line);
84
85template<class T>
86float polygon_lenght(T& line);
87
88
89
90// *******************************************************
91// Cylindrical geometry **********************************
92// *******************************************************
93
98template <class T>
99float getMeanPhi(T &set);
101template<class T>
102float max_deltaR(point_t p, T& set);
103
104
106template<class T , class T2>
107void recenter_set(T &inSet, T2 &outSet, float phicenter);
110template<class T , class T2>
111void recenter_set(T &inSet, T2 &outSet);
112
113
115inline float in_mPI_pPI(float phi);
117inline void fix2pi(point_t &p);
119inline float deltaR(point_t &p1, point_t &p2);
120inline float deltaR2(point_t &p1, point_t &p2);
121
122inline float deltaPhi(point_t &p1, point_t &p2);
123inline float deltaPhi(float phi1, float phi2);
124
125
126
127// *******************************************************
128// Utils function **********************************
129// *******************************************************
130void listToSet(point_list_t &inList, point_set_t &outSet);
131 template<class T>
132 void clear_delete(T &container);
133 template<class T>
134 void delete_content(T &container);
135
136
137
138
139// ***************************************************************************
140
141// ***********************************************
142// Inlined and template implementations ****************************
143// ***********************************************
144#ifndef G__DICTIONARY
145
146
147template<class T>
149 typedef typename T::iterator it_t;
150 it_t it = container.begin();
151 it_t itE = container.end();
152 for(; it != itE; ++it) delete *it;
153 container.clear();
154}
155template<class T>
157 typedef typename T::iterator it_t;
158 it_t it = container.begin();
159 it_t itE = container.end();
160 for(; it != itE; ++it) delete *it;
161}
162
163template<class T>
164float max_deltaR(point_t p, T& set){
165 typedef typename T::iterator it_t;
166 it_t it = set.begin();
167 it_t itE = set.end();
168 float r=0;
169 for(; it != itE; ++it){
170 float t = deltaR(p, *it);
171 if(t>r) r=t;
172 }
173 return r;
174}
175
176template<class T>
177float polygon_area(T& line){
178 typedef typename T::iterator it_t;
179 it_t it = line.begin();
180 it_t itE = line.end();
181 float a=0;
182 for(; it != itE; ++it){
183 it_t itp=it;
184 ++itp;
185 if(itp == itE) itp = line.begin();
186 a += ( (*it).first*(*itp).second - (*itp).first*(*it).second );
187 }
188 return a/2;
189}
190
191template<class T>
192float polygon_lenght(T &line){
193 typedef typename T::iterator it_t;
194 it_t it = line.begin();
195 it_t itE = line.end();
196 float a=0;
197 for(; it != itE; ++it){
198 it_t itp=it;
199 ++itp;
200 if(itp == itE) itp = line.begin();
201 a += deltaR(*it,*itp);
202 }
203 return a;
204}
205
206inline float abs_dphi(float phi1, float phi2){
207 float d = fabs(phi1 - phi2);
208 return d < M_PI ? d: fabs(d-2*M_PI);
209}
210inline float deltaPhi(float phi1, float phi2){
211 return in_mPI_pPI(phi1-phi2);
212}
213inline float deltaPhi(point_t &p1, point_t &p2){
214 return deltaPhi(p1.second,p2.second);
215}
216inline float in_mPI_pPI(float phi){
217 while(phi < -M_PI) phi += 2*M_PI;
218 while(phi >= M_PI) phi -= 2*M_PI;
219 return phi;
220}
221inline void fix2pi(point_t &p){
222 while(p.second < -M_PI) p.second += 2*M_PI;
223 while(p.second >= M_PI) p.second -= 2*M_PI;
224}
225
226inline point_t recenter(const point_t &p, const point_t &center){
227 point_t n(p.first, p.second - center.second);
228 fix2pi(n);
229 return n;
230}
231inline point_t recenter(const point_t &p, float phicenter){
232 point_t n(p.first, p.second - phicenter);
233 fix2pi(n);
234 return n;
235}
236inline float deltaR2(point_t &p1, point_t &p2){
237 return (p1.first-p2.first)*(p1.first-p2.first) +
238 abs_dphi(p1.second,p2.second)*abs_dphi(p1.second,p2.second) ;
239}
240inline float deltaR(point_t &p1, point_t &p2){
241 return sqrt( deltaR2(p1,p2) );
242}
243
244template<class T , class T2>
245void recenter_set(T &inSet, T2 &outSet, float phicenter){
246 typedef typename T::iterator it_t;
247 typedef typename T2::iterator it_t2;
248 it_t it = inSet.begin();
249 it_t itE = inSet.end();
250 it_t2 it2 = outSet.begin();
251 for(; it!= itE; ++it){
252 it2 = outSet.insert(it2,recenter(*it,phicenter));
253 }
254}
255template<class T , class T2>
256void recenter_set(T &inSet, T2 &outSet){
257 float phicenter = getMeanPhi(inSet);
258 if (phicenter <-9) return;
259 recenter_set(inSet,outSet,phicenter);
260}
261
262template <class T>
263float getMeanPhi(T &set){
264 // return mean
265 typedef typename T::iterator it_t;
266 it_t it = set.begin();
267 it_t end = set.end();
268 point_t fp = (*it);
269 float max= 0,min=0, mean=0;
270 int n=0;
271 for(; it!= end; ++it){
272 float phi = recenter((*it),fp).second;
273 if( phi >0){
274 max = phi>max ? phi : max;
275 }else{
276 min = phi<min ? phi : min;
277 }
278 mean+=phi; n++;
279 }
280 if ( (max-min) <= (M_PI) )
281 return in_mPI_pPI(mean/n+fp.second);
282 else return -10;
283}
284
285
286
288 m_cy = p1.first - p2.first;
289 m_cx = p2.second - p1.second;
290 m_cc = -(m_cx*p1.first+m_cy*p1.second);
291 m_oriented_r = (p1.first < p2.first);
292}
293
294inline bool line_t::is_above(point_t &p){
295 return ( (m_cy!=0) && ( p.second >= - (m_cx*p.first+m_cc)/m_cy) );
296}
297
298inline bool line_t::is_below(point_t &p){
299 return ( (m_cy!=0) && ( p.second <= - (m_cx*p.first+m_cc)/m_cy) ) ;
300}
301
303 if(m_cx==0) return point_t(-99999,y);
304 return point_t( (-m_cc - m_cy*y)/m_cx , y);
305}
306
308 if(m_cy==0) return point_t(x,-99999);
309 return point_t( x, (-m_cc - m_cx*x)/m_cy );
310}
311
312
313
314// Convex Hull *************************************
315
316template< class inT, class ouT>
317void findConvexHull(inT &inSet, ouT &outSet ){
318 point_set_t rset;
319 typedef typename inT::iterator it_t;
320 it_t it = inSet.begin();
321 it_t itE = inSet.end();
322 for(; it!=itE; ++it) rset.insert(*it);
323 _findConvexHull(rset,outSet);
324}
325
326template<class ouT>
327void findConvexHull(point_set_t &inSet, ouT &outSet ){
328 _findConvexHull(inSet, outSet);
329}
330
331template <class T>
332void _findConvexHull(point_set_t &inSet, T &outSet ){
333
334 if(inSet.size()<4){
335 outSet.insert(outSet.begin(), inSet.begin(), inSet.end() );
336 return;
337 }
338
339 point_set_t::iterator s_it = inSet.begin();
340 point_t point00 = (*s_it);
341 point_t point01= point00;
342 while(point01.first == point00.first){
343 ++s_it;
344 point01 = *s_it;
345 }
346 --s_it; point01 = *s_it;
347
348 s_it = inSet.end(); --s_it;
349 point_t point11 = (*s_it);
350 point_t point10= point11;
351 while(point10.first == point11.first){
352 --s_it;
353 point10 = *s_it;
354 }
355 ++s_it; point10 = *s_it;
356
357 // lower line :
358 line_t lmin(point00, point10);
359 // lower line :
360 line_t lmax(point01, point11);
361
362 // fill upper and lower list :
363 point_list_t lowPoints, upPoints;
364
365 s_it = inSet.begin();
366 if(point00 == point01 ){ // make sure point is in both list
367 lowPoints.push_back(point00);
368 upPoints.push_back(point00);
369 ++s_it;
370 }
371 //std::cout << " lp size =" << lowPoints.size() << std::endl;
372 point_set_t::iterator s_itE = inSet.end();
373 --s_itE; // we'll deal ws_ith the end by hand
374 for(; s_it!= s_itE; ++s_it){
375 point_t p = *s_it;
376 //std::cout << " point "<< p.first << " , "<< p.second << std::endl;
377 if( lmin.is_below(p)) {
378 lowPoints.push_back(p);
379 continue;
380 }
381 if( lmax.is_above(p)) {
382 upPoints.push_front(p); // insert front soit is well sorted
383 }
384
385 }
386 //std::cout << " lp size =" << lowPoints.size() << std::endl;
387 if(point11 == point10 ){ // make sure point is in both list
388 lowPoints.push_back(point11);
389 upPoints.push_front(point11);
390 ++s_it;
391 }else{
392 lowPoints.push_back(point10);
393 upPoints.push_front(point00);
394 }
395 //std::cout << " lp size =" << lowPoints.size() << std::endl;
396 //std::cout << lowPoints.size() << " " << upPoints.size() << std::endl;
397
398 // find lower hull :
399 point_list_t lowHull;
400 point_list_t::iterator it = lowPoints.begin();
401 point_list_t::iterator itE = lowPoints.end();
402 lowHull.push_back(point00); // here point00 == (*it)
403 ++it;
404 lowHull.push_back(*it); // always put next point in stack
405 ++it;
406 unsigned int nn = lowPoints.size();
407 if(nn>2){
408 //std::cout << "building low hull : "<< lowPoints.size() <<std::endl;
409 while(it!=itE){
410 testHullLine(lowHull, *it);
411 ++it;
412 }
413 }
414 // find upper hull :
415 point_list_t upHull;
416 it = upPoints.begin();
417 itE = upPoints.end();
418 upHull.push_back(point11); // here point11 == (*it)
419 ++it;
420 upHull.push_back(*it); // always put next point in stack
421 ++it;
422 if(upPoints.size()>2){
423 //std::cout << "building up hull : "<< upPoints.size() <<std::endl;
424 while(it!=itE){
425 testHullLine(upHull, *it);
426 ++it;
427 }
428 }
429// std::cout << " low size = "<< lowHull.size() << std::endl;
430// std::cout << " up size = "<< upHull.size() << std::endl;
431 // join Hull !
432 outSet.insert(outSet.begin(), lowHull.begin(), lowHull.end() );
433 outSet.insert((outSet.end()), ++(upHull.begin()), --(upHull.end()) ); // avoid duplication
434
435 // std::cout << " Out size = "<< outSet.size() << std::endl;
436
437}
438
439
440#endif //DICT
441
442
443} // namespace JetGeom
444#endif
445
#define M_PI
Scalar phi() const
phi method
static Double_t a
#define y
#define x
#define min(a, b)
Definition cfImp.cxx:40
#define max(a, b)
Definition cfImp.cxx:41
oriented segment/line in a simplistic way
Definition cyl_geom.h:37
bool is_right(point_t &p)
Definition cyl_geom.h:55
void phi_shift(float dphi)
Definition cyl_geom.h:57
line_t(float cx, float cy, float cc, bool to_r)
constructor by giving the coeff of equation of the line (
Definition cyl_geom.h:43
bool is_left(point_t &p)
Definition cyl_geom.h:54
bool is_above(point_t &p)
Definition cyl_geom.h:294
point_t intercept_x(float x)
Definition cyl_geom.h:307
point_t intercept_y(float y)
Definition cyl_geom.h:302
bool is_below(point_t &p)
Definition cyl_geom.h:298
bool m_oriented_r
Definition cyl_geom.h:48
Very basic point objects.
Definition cyl_geom.h:25
point_t(float x, float y)
Definition cyl_geom.h:27
STL class.
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
int r
Definition globals.cxx:22
A collection of routines for geometric tasks in 2D and on a cylinder.
Definition cyl_geom.h:22
float polygon_area(T &line)
Definition cyl_geom.h:177
float getMeanPhi(T &set)
return average phi (i.e.
Definition cyl_geom.h:263
void findConvexHull(inT &inSet, ouT &outSet)
Find convex hull of a set of points in euclidian plan.
Definition cyl_geom.h:317
float abs_dphi(float phi1, float phi2)
Definition cyl_geom.h:206
float in_mPI_pPI(float phi)
convert
Definition cyl_geom.h:216
float deltaR2(point_t &p1, point_t &p2)
Definition cyl_geom.h:236
void listToSet(point_list_t &inList, point_set_t &outSet)
Definition cyl_geom.cxx:27
float max_deltaR(point_t p, T &set)
Return max distance betweens point.
Definition cyl_geom.h:164
point_t recenter(const point_t &p, const point_t &center)
Definition cyl_geom.h:226
void _findConvexHull(point_set_t &inSet, inT &outSet)
void clear_delete(T &container)
Definition cyl_geom.h:148
void fix2pi(point_t &p)
convert
Definition cyl_geom.h:221
void testHullLine(point_list_t &hull, point_t p)
Definition cyl_geom.cxx:9
void delete_content(T &container)
Definition cyl_geom.h:156
std::list< point_t > point_list_t
Definition cyl_geom.h:32
float deltaR(point_t &p1, point_t &p2)
distances between points
Definition cyl_geom.h:240
float deltaPhi(point_t &p1, point_t &p2)
Definition cyl_geom.h:213
void recenter_set(T &inSet, T2 &outSet, float phicenter)
copy
Definition cyl_geom.h:245
float polygon_lenght(T &line)
Definition cyl_geom.h:192