]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPolygon.cxx
Update for 2012
[u/mrichter/AliRoot.git] / MUON / AliMUONPolygon.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 /// \class AliMUONPolygon
19 ///
20 /// A simple planar polygon, with a given orientation
21 ///
22 /// \author Laurent Aphecetche, Subatech
23
24 #include "AliMUONPolygon.h"
25
26 #include "AliLog.h"
27 #include "Riostream.h"
28 #include "TMath.h"
29
30 ///\cond CLASSIMP
31 ClassImp(AliMUONPolygon)
32 ///\endcond
33
34 //_____________________________________________________________________________
35 AliMUONPolygon::AliMUONPolygon(Int_t nvertices) 
36 : TObject(),
37 fN(nvertices),
38 fX(new Double_t[fN]),
39 fY(new Double_t[fN])
40 {
41   /// Ctor with a predefined number of vertices.
42 }
43
44 //_____________________________________________________________________________
45 AliMUONPolygon::AliMUONPolygon(Double_t xpos, Double_t ypos, Double_t halfsizex, Double_t halfsizey)
46 : TObject(),
47 fN(5),
48 fX(new Double_t[fN]),
49 fY(new Double_t[fN])
50 {
51   /// Ctor. Polygon will be a rectangle.
52   
53   
54   double xmin(xpos-halfsizex);
55   double xmax(xpos+halfsizex);
56   double ymin(ypos-halfsizey);
57   double ymax(ypos+halfsizey);
58
59   SetVertex(0,xmin,ymin);
60   SetVertex(1,xmax,ymin);
61   SetVertex(2,xmax,ymax);
62   SetVertex(3,xmin,ymax);
63   
64   Close();
65 }
66
67
68 //_____________________________________________________________________________
69 AliMUONPolygon::~AliMUONPolygon()
70 {
71   /// dtor
72   delete[] fX;
73   delete[] fY;
74 }
75
76 //______________________________________________________________________________
77 AliMUONPolygon::AliMUONPolygon(const AliMUONPolygon& rhs) 
78 : TObject(rhs), 
79 fN(rhs.fN),
80 fX(0x0),
81 fY(0x0)
82 {
83   /// Copy constructor.
84   
85   if ( fN > 0 ) 
86   {
87     fX = new Double_t[fN];
88     fY = new Double_t[fN];
89     
90     for ( Int_t i = 0; i < fN; ++i )
91     {
92       fX[i] = rhs.fX[i];
93       fY[i] = rhs.fY[i];
94     }
95     
96   }
97   
98 }
99
100 //______________________________________________________________________________
101 AliMUONPolygon&
102 AliMUONPolygon::operator=(const AliMUONPolygon& rhs)
103 {
104   /// Assignment operator
105   if ( this != &rhs ) 
106   {
107     static_cast<TObject&>(*this)=rhs;
108
109     delete[] fX;
110     delete[] fY;
111     
112     fX = 0;
113     fY = 0;
114     
115     fN = rhs.fN;
116     
117     if ( fN > 0 ) 
118     {
119       fX = new Double_t[fN];
120       fY = new Double_t[fN];
121       
122       for ( Int_t i = 0; i < fN; ++i )
123       {
124         fX[i] = rhs.fX[i];
125         fY[i] = rhs.fY[i];
126       }
127       
128     }
129   }
130   return *this;
131 }
132
133 //______________________________________________________________________________
134 Bool_t
135 AliMUONPolygon::Contains(Double_t x, Double_t y) const
136 {
137   /// Whether the polygon contains point (x,y)
138   
139   // Note that the polygon must be a closed polygon (1st and last point
140   // must be identical), which should be the case here.
141
142   return TMath::IsInside(x,y,fN,fX,fY);
143 }
144
145 //_____________________________________________________________________________
146 void
147 AliMUONPolygon::Close()
148 {
149   /// Make that last point = first point
150   
151   SetVertex(fN-1,X(0),Y(0));
152 }
153
154 //_____________________________________________________________________________
155 void AliMUONPolygon::Print(Option_t*) const
156 {
157   /// Printout
158   cout << Form("AliMUONPolygon : %3d vertices. Signed Area=%e",NumberOfVertices(),SignedArea()) << endl;
159   for ( Int_t i = 0; i < NumberOfVertices(); ++i )
160   {
161     cout << Form("%10.5f,%10.5f",X(i),Y(i)) << endl;
162   }
163 }
164
165 //_____________________________________________________________________________
166 Double_t 
167 AliMUONPolygon::SignedArea() const
168 {
169   /// Compute the signed area of this polygon
170   /// Algorithm from F. Feito, J.C. Torres and A. Urena,
171   /// Comput. & Graphics, Vol. 19, pp. 595-600, 1995
172   
173   Double_t area(0.0);
174   
175   for ( Int_t i = 0; i < NumberOfVertices()-1; ++i ) 
176   {
177     area += X(i)*Y(i+1) - X(i+1)*Y(i);
178   }
179  
180   return area;
181 }
182
183 //_____________________________________________________________________________
184 void 
185 AliMUONPolygon::ReverseOrientation()
186 {
187   /// Reverse the orientation of this polygon
188   Double_t* x = new Double_t[fN];
189   Double_t* y = new Double_t[fN];
190   
191   for ( Int_t i = fN-1; i >= 0; --i )
192   {
193     x[i] = X(fN-i-1);
194     y[i] = Y(fN-i-1);
195   }
196
197   delete[] fX;
198   delete[] fY;
199   
200   fX = x;
201   fY = y;
202 }
203
204 //_____________________________________________________________________________
205 void 
206 AliMUONPolygon::SetVertex(Int_t i, Double_t x, Double_t y)
207 {
208   /// Set one vertex
209   if ( i >= fN ) 
210   {
211     AliFatal("Wrong index");
212   }
213   fX[i] = x;
214   fY[i] = y;
215 }
216