1 | // -*- C++ -*- |
---|
2 | /////////////////////////////////////////////////////////////////////////////// |
---|
3 | // File: geom_2d.h // |
---|
4 | // Description: header file for two-dimensional geometry tools // |
---|
5 | // This file is part of the SISCone project. // |
---|
6 | // For more details, see http://projects.hepforge.org/siscone // |
---|
7 | // // |
---|
8 | // Copyright (c) 2006 Gavin Salam and Gregory Soyez // |
---|
9 | // // |
---|
10 | // This program is free software; you can redistribute it and/or modify // |
---|
11 | // it under the terms of the GNU General Public License as published by // |
---|
12 | // the Free Software Foundation; either version 2 of the License, or // |
---|
13 | // (at your option) any later version. // |
---|
14 | // // |
---|
15 | // This program is distributed in the hope that it will be useful, // |
---|
16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
---|
17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
---|
18 | // GNU General Public License for more details. // |
---|
19 | // // |
---|
20 | // You should have received a copy of the GNU General Public License // |
---|
21 | // along with this program; if not, write to the Free Software // |
---|
22 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA // |
---|
23 | // // |
---|
24 | // $Revision:: 859 $// |
---|
25 | // $Date:: 2012-11-28 02:49:23 +0100 (Wed, 28 Nov 2012) $// |
---|
26 | /////////////////////////////////////////////////////////////////////////////// |
---|
27 | |
---|
28 | #ifndef __GEOM_2D_H__ |
---|
29 | #define __GEOM_2D_H__ |
---|
30 | |
---|
31 | #include <iostream> |
---|
32 | #include <math.h> |
---|
33 | #include "defines.h" |
---|
34 | |
---|
35 | #ifndef M_PI |
---|
36 | #define M_PI 3.141592653589793238462643383279502884197 |
---|
37 | #endif |
---|
38 | |
---|
39 | namespace siscone{ |
---|
40 | |
---|
41 | /// return a result that corresponds to phi, but in the |
---|
42 | /// range (-pi..pi]; the result is only correct if -3pi < phi <= 3pi |
---|
43 | inline double phi_in_range(double phi) { |
---|
44 | if (phi <= -M_PI) phi += twopi; |
---|
45 | else if (phi > M_PI) phi -= twopi; |
---|
46 | return phi; |
---|
47 | } |
---|
48 | |
---|
49 | /// return the difference between the two phi values, |
---|
50 | /// placed in the correct range (-pi..pi], , assuming that phi1,phi2 |
---|
51 | /// are already in the correct range. |
---|
52 | inline double dphi(double phi1, double phi2) { |
---|
53 | return phi_in_range(phi1-phi2); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | /// return the absolute difference between the two phi values, |
---|
58 | /// placed in the correct range, assuming that phi1,phi2 are already |
---|
59 | /// in the correct range. |
---|
60 | inline double abs_dphi(double phi1, double phi2) { |
---|
61 | double delta = fabs(phi1-phi2); |
---|
62 | return delta > M_PI ? twopi-delta : delta; |
---|
63 | } |
---|
64 | |
---|
65 | /// return the square of the argument |
---|
66 | inline double pow2(double x) {return x*x;} |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * \class Ctwovect |
---|
71 | * \brief class for holding a two-vector |
---|
72 | */ |
---|
73 | class Ctwovect { |
---|
74 | public: |
---|
75 | /// default ctor |
---|
76 | Ctwovect() : x(0.0), y(0.0) {} |
---|
77 | |
---|
78 | /// ctor with initialisation |
---|
79 | /// \param _x first coordinate |
---|
80 | /// \param _y second coordinate |
---|
81 | Ctwovect(double _x, double _y) : x(_x), y(_y) {} |
---|
82 | |
---|
83 | /// vector coordinates |
---|
84 | double x, y; |
---|
85 | |
---|
86 | /// norm (modulud square) of the vector |
---|
87 | inline double mod2() const {return pow2(x)+pow2(y);} |
---|
88 | |
---|
89 | /// modulus of the vector |
---|
90 | inline double modulus() const {return sqrt(mod2());} |
---|
91 | }; |
---|
92 | |
---|
93 | |
---|
94 | /// dot product of two 2-vectors |
---|
95 | /// \param a first 2-vect |
---|
96 | /// \param b second 2-vect |
---|
97 | /// \return a.b is returned |
---|
98 | inline double dot_product(const Ctwovect & a, const Ctwovect & b) { |
---|
99 | return a.x*b.x + a.y*b.y; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /// cross product of two 2-vectors |
---|
104 | /// \param a first 2-vect |
---|
105 | /// \param b second 2-vect |
---|
106 | /// \return a x b is returned |
---|
107 | inline double cross_product(const Ctwovect & a, const Ctwovect & b) { |
---|
108 | return a.x*b.y - a.y*b.x; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | * \class Ceta_phi_range |
---|
114 | * \brief class for holding a covering range in eta-phi |
---|
115 | * |
---|
116 | * This class deals with ranges in the eta-phi plane. It |
---|
117 | * implements methods to test if two ranges overlap and |
---|
118 | * to take the union of two overlapping intervals. |
---|
119 | */ |
---|
120 | class Ceta_phi_range{ |
---|
121 | public: |
---|
122 | /// default ctor |
---|
123 | Ceta_phi_range(); |
---|
124 | |
---|
125 | /// ctor with initialisation |
---|
126 | /// we initialise with a centre (in eta,phi) and a radius |
---|
127 | /// \param c_eta eta coordinate of the centre |
---|
128 | /// \param c_phi phi coordinate of the centre |
---|
129 | /// \param R radius |
---|
130 | Ceta_phi_range(double c_eta, double c_phi, double R); |
---|
131 | |
---|
132 | /// assignment of range |
---|
133 | /// \param r range to assign to current one |
---|
134 | Ceta_phi_range& operator = (const Ceta_phi_range &r); |
---|
135 | |
---|
136 | /// add a particle to the range |
---|
137 | /// \param eta eta coordinate of the particle |
---|
138 | /// \param phi phi coordinate of the particle |
---|
139 | /// \return 0 on success, 1 on error |
---|
140 | int add_particle(const double eta, const double phi); |
---|
141 | |
---|
142 | /// eta range as a binary coding of covered cells |
---|
143 | unsigned int eta_range; |
---|
144 | |
---|
145 | /// phi range as a binary coding of covered cells |
---|
146 | unsigned int phi_range; |
---|
147 | |
---|
148 | // extremal value for eta |
---|
149 | static double eta_min; ///< minimal value for eta |
---|
150 | static double eta_max; ///< maximal value for eta |
---|
151 | |
---|
152 | private: |
---|
153 | /// return the cell index corrsponding to an eta value |
---|
154 | inline unsigned int get_eta_cell(double eta){ |
---|
155 | return (unsigned int) (1 << ((int) (32*((eta-eta_min)/(eta_max-eta_min))))); |
---|
156 | } |
---|
157 | |
---|
158 | /// return the cell index corrsponding to a phi value |
---|
159 | inline unsigned int get_phi_cell(double phi){ |
---|
160 | return (unsigned int) (1 << ((int) (32*phi/twopi+16)%32)); |
---|
161 | } |
---|
162 | }; |
---|
163 | |
---|
164 | /// test overlap |
---|
165 | /// \param r1 first range |
---|
166 | /// \param r2 second range |
---|
167 | /// \return true if overlap, false otherwise. |
---|
168 | bool is_range_overlap(const Ceta_phi_range &r1, const Ceta_phi_range &r2); |
---|
169 | |
---|
170 | /// compute union |
---|
171 | /// Note: we assume that the two intervals overlap |
---|
172 | /// \param r1 first range |
---|
173 | /// \param r2 second range |
---|
174 | /// \return union of the two ranges |
---|
175 | const Ceta_phi_range range_union(const Ceta_phi_range &r1, const Ceta_phi_range &r2); |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | #endif |
---|