Fixed memory leak
[u/mrichter/AliRoot.git] / MUON / AliMUONPolygon.cxx
CommitLineData
0b936dc0 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
b80faac0 30using std::cout;
31using std::endl;
0b936dc0 32///\cond CLASSIMP
33ClassImp(AliMUONPolygon)
34///\endcond
35
36//_____________________________________________________________________________
37AliMUONPolygon::AliMUONPolygon(Int_t nvertices)
38: TObject(),
39fN(nvertices),
40fX(new Double_t[fN]),
41fY(new Double_t[fN])
42{
43 /// Ctor with a predefined number of vertices.
44}
45
46//_____________________________________________________________________________
47AliMUONPolygon::AliMUONPolygon(Double_t xpos, Double_t ypos, Double_t halfsizex, Double_t halfsizey)
48: TObject(),
49fN(5),
50fX(new Double_t[fN]),
51fY(new Double_t[fN])
52{
53 /// Ctor. Polygon will be a rectangle.
54
55
56 double xmin(xpos-halfsizex);
57 double xmax(xpos+halfsizex);
58 double ymin(ypos-halfsizey);
59 double ymax(ypos+halfsizey);
60
61 SetVertex(0,xmin,ymin);
62 SetVertex(1,xmax,ymin);
63 SetVertex(2,xmax,ymax);
64 SetVertex(3,xmin,ymax);
65
66 Close();
67}
68
69
70//_____________________________________________________________________________
71AliMUONPolygon::~AliMUONPolygon()
72{
73 /// dtor
74 delete[] fX;
75 delete[] fY;
76}
77
78//______________________________________________________________________________
79AliMUONPolygon::AliMUONPolygon(const AliMUONPolygon& rhs)
80: TObject(rhs),
c6496ae2 81fN(rhs.fN),
0b936dc0 82fX(0x0),
83fY(0x0)
84{
85 /// Copy constructor.
86
c6496ae2 87 if ( fN > 0 )
88 {
89 fX = new Double_t[fN];
90 fY = new Double_t[fN];
91
92 for ( Int_t i = 0; i < fN; ++i )
93 {
94 fX[i] = rhs.fX[i];
95 fY[i] = rhs.fY[i];
96 }
97
98 }
99
0b936dc0 100}
101
102//______________________________________________________________________________
103AliMUONPolygon&
104AliMUONPolygon::operator=(const AliMUONPolygon& rhs)
105{
106 /// Assignment operator
107 if ( this != &rhs )
108 {
c6496ae2 109 static_cast<TObject&>(*this)=rhs;
110
111 delete[] fX;
112 delete[] fY;
113
114 fX = 0;
115 fY = 0;
116
117 fN = rhs.fN;
118
119 if ( fN > 0 )
120 {
121 fX = new Double_t[fN];
122 fY = new Double_t[fN];
123
124 for ( Int_t i = 0; i < fN; ++i )
125 {
126 fX[i] = rhs.fX[i];
127 fY[i] = rhs.fY[i];
128 }
129
130 }
0b936dc0 131 }
132 return *this;
133}
134
135//______________________________________________________________________________
136Bool_t
137AliMUONPolygon::Contains(Double_t x, Double_t y) const
138{
139 /// Whether the polygon contains point (x,y)
140
141 // Note that the polygon must be a closed polygon (1st and last point
142 // must be identical), which should be the case here.
143
144 return TMath::IsInside(x,y,fN,fX,fY);
145}
146
0b936dc0 147//_____________________________________________________________________________
148void
149AliMUONPolygon::Close()
150{
151 /// Make that last point = first point
152
153 SetVertex(fN-1,X(0),Y(0));
154}
155
156//_____________________________________________________________________________
157void AliMUONPolygon::Print(Option_t*) const
158{
159 /// Printout
160 cout << Form("AliMUONPolygon : %3d vertices. Signed Area=%e",NumberOfVertices(),SignedArea()) << endl;
161 for ( Int_t i = 0; i < NumberOfVertices(); ++i )
162 {
163 cout << Form("%10.5f,%10.5f",X(i),Y(i)) << endl;
164 }
165}
166
167//_____________________________________________________________________________
168Double_t
169AliMUONPolygon::SignedArea() const
170{
171 /// Compute the signed area of this polygon
172 /// Algorithm from F. Feito, J.C. Torres and A. Urena,
173 /// Comput. & Graphics, Vol. 19, pp. 595-600, 1995
174
175 Double_t area(0.0);
176
177 for ( Int_t i = 0; i < NumberOfVertices()-1; ++i )
178 {
179 area += X(i)*Y(i+1) - X(i+1)*Y(i);
180 }
181
182 return area;
183}
184
185//_____________________________________________________________________________
186void
187AliMUONPolygon::ReverseOrientation()
188{
189 /// Reverse the orientation of this polygon
190 Double_t* x = new Double_t[fN];
191 Double_t* y = new Double_t[fN];
192
193 for ( Int_t i = fN-1; i >= 0; --i )
194 {
195 x[i] = X(fN-i-1);
196 y[i] = Y(fN-i-1);
197 }
198
199 delete[] fX;
200 delete[] fY;
201
202 fX = x;
203 fY = y;
204}
205
206//_____________________________________________________________________________
207void
208AliMUONPolygon::SetVertex(Int_t i, Double_t x, Double_t y)
209{
210 /// Set one vertex
211 if ( i >= fN )
212 {
fc2293be 213 AliFatal("Wrong index");
0b936dc0 214 }
215 fX[i] = x;
216 fY[i] = y;
217}
218