1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
19 /// Maker/merger of contours. Can create contour from a set polygons or
20 /// merger a set of contours into a single one.
22 /// This is based on (one of the) algorithm found in
23 /// Diane L. Souvaine and Iliana Bjorling-Sachs,
24 /// Proceedings of the IEEE, Vol. 80, No. 9, September 1992, p. 1449
26 /// Note that besides the AliMUON prefix, nothing is really MUON specific
29 /// \author Laurent Aphecetche, Subatech
32 #include "AliMUONContourMaker.h"
34 #include "AliCodeTimer.h"
36 #include "AliMUONContour.h"
37 #include "AliMUONPointWithRef.h"
38 #include "AliMUONPolygon.h"
39 #include "AliMUONSegment.h"
40 #include "AliMUONSegmentTree.h"
41 #include "Riostream.h"
47 ClassImp(AliMUONContourMaker)
52 void PrintSegments(const TObjArray& array)
57 while ( ( s = static_cast<AliMUONSegment*>(next()) ) )
59 cout << Form("i=%d %s",i,s->AsString()) << endl;
65 //_____________________________________________________________________________
66 AliMUONContourMaker::AliMUONContourMaker()
70 //_____________________________________________________________________________
71 AliMUONContourMaker::~AliMUONContourMaker()
75 //_____________________________________________________________________________
77 AliMUONContourMaker::CreateContour(const TObjArray& polygons, const char* name) const
79 /// Create the contour of the polygon array
80 /// and get back the intermediate verticals and horizontal segments
81 /// both arrays are arrays of AliMUONSegment objects.
85 if ( polygons.IsEmpty() ) return 0x0; // protection against user error...
87 // Sanity check : insure that all polygons are oriented counter-clockwise
88 TIter next(&polygons);
90 while ( ( pol = static_cast<AliMUONPolygon*>(next()) ) )
92 if ( !pol->IsCounterClockwiseOriented() )
94 AliError(Form("Got a clockwise oriented polygon in CreateContour(%s). That's not OK !",name));
95 StdoutToAliError(polygons.Print());
100 AliMUONContour* contour(0x0);
102 if ( polygons.GetLast() == 0 )
104 AliCodeTimerAuto("Trivial case");
105 contour = new AliMUONContour(name);
106 pol = static_cast<const AliMUONPolygon*>(polygons.First());
108 contour->AssertOrientation();
112 TObjArray polygonVerticalEdges; // arrray of AliMUONSegment objects
113 polygonVerticalEdges.SetOwner(kTRUE);
114 // get vertical edges of input polygons
115 GetVerticalEdges(polygons,polygonVerticalEdges);
117 // sort them in ascending x order
118 // if same x, insure that left edges are before right edges
119 // within same x, order by increasing bottommost y (see AliMUONSegment::Compare method)
120 polygonVerticalEdges.Sort();
122 if ( polygonVerticalEdges.GetLast()+1 < 2 )
124 AliError(Form("Got too few edges here for createContour %s",name));
130 // Find the vertical edges of the merged contour. This is the meat of the algorithm...
131 TObjArray contourVerticalEdges;
132 contourVerticalEdges.SetOwner(kTRUE);
133 Sweep(polygonVerticalEdges,contourVerticalEdges);
135 TObjArray horizontals;
136 horizontals.SetOwner(kTRUE);
137 VerticalToHorizontal(contourVerticalEdges,horizontals);
139 contour = FinalizeContour(contourVerticalEdges,horizontals);
141 if ( contour && name ) contour->SetName(name);
146 //_____________________________________________________________________________
148 AliMUONContourMaker::FinalizeContour(const TObjArray& verticals,
149 const TObjArray& horizontals) const
151 /// For a list of vertical and horizontal edges, we build the final
154 AliCodeTimerAuto("");
156 TObjArray all; // array of AliMUONSegment
157 TObjArray inorder; // array of AliMUONSegment
159 all.SetOwner(kFALSE);
160 inorder.SetOwner(kFALSE);
162 for ( Int_t i = 0; i <= verticals.GetLast(); ++i )
164 all.Add(verticals.UncheckedAt(i));
165 all.Add(horizontals.UncheckedAt(i));
170 AliMUONContour* contour = new AliMUONContour;
174 while ( !all.IsEmpty() )
180 AliError("Total 1000 reached !!!!");
184 AliMUONSegment* si = static_cast<AliMUONSegment*>(all.UncheckedAt(i));
186 const AliMUONSegment* all0 = static_cast<const AliMUONSegment*>(all.First());
187 if ( i != 0 && AliMUONSegment::AreEqual(si->EndX(),all0->StartX()) && AliMUONSegment::AreEqual(si->EndY(),all0->StartY()) )
191 AliMUONPolygon polygon(inorder.GetLast()+2);
193 // we got a cycle. Add it to the contour
194 for ( Int_t j = 0; j <= inorder.GetLast(); ++j )
196 AliMUONSegment* s = static_cast<AliMUONSegment*>(inorder.UncheckedAt(j));
197 polygon.SetVertex(++n,s->StartX(),s->StartY());
205 contour->Add(polygon);
207 if ( ! all.IsEmpty() )
215 for ( Int_t j = 0; j <= all.GetLast(); ++j)
219 const AliMUONSegment* sj = static_cast<const AliMUONSegment*>(all.UncheckedAt(j));
220 if ( AliMUONSegment::AreEqual(si->EndX(),sj->StartX()) && AliMUONSegment::AreEqual(si->EndY(),sj->StartY()))
229 contour->AssertOrientation(kTRUE);
234 //_____________________________________________________________________________
236 AliMUONContourMaker::GetVerticalEdges(const TObjArray& polygons, TObjArray& polygonVerticalEdges) const
238 /// From an array of polygons, extract the list of vertical edges.
239 /// Output array polygonVerticalEdges should be empty before calling.
241 AliCodeTimerAuto("");
243 for ( Int_t i = 0; i <= polygons.GetLast(); ++i )
245 const AliMUONPolygon* g = static_cast<const AliMUONPolygon*>(polygons.UncheckedAt(i));
246 for ( Int_t j = 0; j < g->NumberOfVertices()-1; ++j )
248 if ( AliMUONSegment::AreEqual(g->X(j),g->X(j+1)) ) // segment is vertical
250 polygonVerticalEdges.Add(new AliMUONSegment(g->X(j),g->Y(j),g->X(j+1),g->Y(j+1)));
257 //_____________________________________________________________________________
259 AliMUONContourMaker::GetYPositions(const TObjArray& polygonVerticalEdges,
260 TArrayD& yPositions) const
262 /// Fill the array yPositions with the different y positions found in
263 /// polygonVerticalEdges
265 AliCodeTimerAuto("");
267 Double_t* y = new Double_t[polygonVerticalEdges.GetSize()*2];
270 for ( Int_t i = 0; i < polygonVerticalEdges.GetLast(); ++i )
272 AliMUONSegment* s = static_cast<AliMUONSegment*>(polygonVerticalEdges.UncheckedAt(i));
277 Int_t* ix = new Int_t[n+1];
279 TMath::Sort(n,y,ix,kFALSE);
286 for ( Int_t i = 0; i < n; ++i )
290 yPositions[u] = y[ix[i]];
303 //_____________________________________________________________________________
305 AliMUONContourMaker::MergeContour(const TObjArray& contours, const char* name) const
307 /// Merge all the polygons of all contours into a single contour
309 AliCodeTimerAuto("");
312 polygons.SetOwner(kTRUE);
314 TIter next(&contours);
315 AliMUONContour* contour;
316 while ( ( contour = static_cast<AliMUONContour*>(next()) ) )
318 const TObjArray* contourPolygons = contour->Polygons();
319 TIter nextPol(contourPolygons);
321 while ( ( pol = static_cast<AliMUONPolygon*>(nextPol()) ) )
323 polygons.Add(new AliMUONPolygon(*pol));
327 if ( polygons.IsEmpty() ) return 0x0;
329 contour = CreateContour(polygons,name);
334 //_____________________________________________________________________________
336 AliMUONContourMaker::SortPoints(const TObjArray& polygonVerticalEdges,
337 TObjArray& sortedPoints) const
339 /// Sort the point of the vertical edges in ascending order, first on ordinate,
340 /// then on abcissa, and put them in output vector sortedPoints.
341 /// Output array sortedPoints should be empty before calling this method.
343 AliCodeTimerAuto("");
345 for ( Int_t i = 0; i <= polygonVerticalEdges.GetLast(); ++i )
347 const AliMUONSegment* e = static_cast<const AliMUONSegment*>(polygonVerticalEdges.UncheckedAt(i));
348 sortedPoints.Add(new AliMUONPointWithRef(e->StartX(),e->StartY(),i));
349 sortedPoints.Add(new AliMUONPointWithRef(e->EndX(),e->EndY(),i));
350 // note that we keep track of the original edge, which is used
351 // later on to deduce orientation of horizontal edges.
357 //_____________________________________________________________________________
359 AliMUONContourMaker::Sweep(const TObjArray& polygonVerticalEdges,
360 TObjArray& contourVerticalEdges) const
362 /// This is the meat of the algorithm of the contour merging...
364 AliCodeTimerAuto("");
367 GetYPositions(polygonVerticalEdges,yPositions);
369 AliMUONSegmentTree segmentTree(yPositions);
371 for ( Int_t i = 0; i <= polygonVerticalEdges.GetLast(); ++i )
373 const AliMUONSegment* edge = static_cast<const AliMUONSegment*>(polygonVerticalEdges.UncheckedAt(i));
377 if ( edge->IsLeftEdge() )
379 segmentTree.Contribution(edge->Bottom(),edge->Top());
380 segmentTree.InsertInterval(edge->Bottom(),edge->Top());
384 segmentTree.DeleteInterval(edge->Bottom(),edge->Top());
385 segmentTree.Contribution(edge->Bottom(),edge->Top());
388 AliMUONSegment e1(*edge);
390 if ( i < polygonVerticalEdges.GetLast() )
392 const AliMUONSegment* next = static_cast<const AliMUONSegment*>(polygonVerticalEdges.UncheckedAt(i+1));
396 if ( ( edge->IsLeftEdge() != e1.IsLeftEdge() ) ||
397 ( !AliMUONSegment::AreEqual(edge->StartX(),e1.StartX() ) ) ||
398 ( i == polygonVerticalEdges.GetLast() ) )
400 const TObjArray& stack = segmentTree.Stack();
402 double x = edge->StartX();
404 for ( Int_t j = 0; j <= stack.GetLast(); ++j )
406 AliMUONSegment* sj = static_cast<AliMUONSegment*>(stack.UncheckedAt(j));
407 AliMUONSegment* s = new AliMUONSegment(x,sj->StartY(),x,sj->EndY());
415 if ( edge->IsLeftEdge() != s->IsLeftEdge() )
417 s->Set(x,sj->EndY(),x,sj->StartY());
419 contourVerticalEdges.Add(s);
421 segmentTree.ResetStack();
426 //_____________________________________________________________________________
428 AliMUONContourMaker::VerticalToHorizontal(const TObjArray& polygonVerticalEdges,
429 TObjArray& horizontalEdges) const
431 /// Deduce the set of horizontal edges from the vertical edges
432 /// Output array horizontalEdges should be empty before calling this method
434 AliCodeTimerAuto("");
436 TObjArray points; // array of AliMUONPointWithRef
437 points.SetOwner(kTRUE);
439 SortPoints(polygonVerticalEdges,points);
441 for ( Int_t k = 0; k < (points.GetLast()+1)/2; ++k )
443 const AliMUONPointWithRef* p1 = static_cast<AliMUONPointWithRef*>(points.UncheckedAt(k*2));
444 const AliMUONPointWithRef* p2 = static_cast<AliMUONPointWithRef*>(points.UncheckedAt(k*2+1));
446 const AliMUONSegment* refEdge = static_cast<const AliMUONSegment*>(polygonVerticalEdges.UncheckedAt(p1->Ref()));
448 // (p1,p2) is the horizontal edge.
449 // refEdge is used to deduce the orientation of (p1,p2)
451 if ( AliMUONSegment::AreEqual(p1->X(),refEdge->EndX()) && AliMUONSegment::AreEqual(p1->Y(),refEdge->EndY()) )
452 // if ( AreEqual(p1,refEdge->End()) )
454 horizontalEdges.Add(new AliMUONSegment(p1->X(),p1->Y(),p2->X(),p2->Y()));
458 horizontalEdges.Add(new AliMUONSegment(p2->X(),p2->Y(),p1->X(),p1->Y()));