ROOT logo
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
*                                                                        *
* Author: The ALICE Off-line Project.                                    *
* Contributors are mentioned in the code where appropriate.              *
*                                                                        *
* Permission to use, copy, modify and distribute this software and its   *
* documentation strictly for non-commercial purposes is hereby granted   *
* without fee, provided that the above copyright notice appears in all   *
* copies and that both the copyright notice and this permission notice   *
* appear in the supporting documentation. The authors make no claims     *
* about the suitability of this software for any purpose. It is          *
* provided "as is" without express or implied warranty.                  *
**************************************************************************/

// $Id$

/// \class AliMUONSegment
///
/// A basic line segment, to be used in contour making algorithms. 
///
/// In particular, this class defines what a left or right edge is.
///
/// Also, please note that, due to the way Root collections are sorted (relying
/// on TObject::Compare method), the way the AliMUONSegment::Compare method
/// is implemented below is really important when it comes to understand
/// contour making algorithm. Keep that in mind. 
///
/// \author Laurent Aphecetche, Subatech
///

#include "AliMUONSegment.h"

#include "TMath.h"
#include "Riostream.h"
#include "AliMpConstants.h"

using std::cout;
using std::endl;
/// \cond CLASSIMP
ClassImp(AliMUONSegment)
/// \endcond

const Double_t AliMUONSegment::fgkPrecision(AliMpConstants::LengthTolerance());
  
//_____________________________________________________________________________
AliMUONSegment::AliMUONSegment() : 
TObject(),
fStartX(), fStartY(), fEndX(), fEndY(), fSmallerY(), fIsHorizontal(), fIsVertical(),
fIsLeftEdge(), fIsRightEdge(), fIsAPoint(kTRUE)
{
  /// Ctor
  Set(fStartX,fStartY,fEndX,fEndY);
}

//_____________________________________________________________________________
AliMUONSegment::AliMUONSegment(Double_t xstart, Double_t ystart, Double_t xend, Double_t yend)
: TObject(),
fStartX(xstart), fStartY(ystart), fEndX(xend), fEndY(yend), fSmallerY(), fIsHorizontal(), fIsVertical(),
fIsLeftEdge(), fIsRightEdge(), fIsAPoint(kTRUE)
{
  /// Ctor
  Set(xstart,ystart,xend,yend);
}

//_____________________________________________________________________________
Bool_t
AliMUONSegment::AreEqual(double a, double b)
{
  /// Whether the two floats are equal within the given precision
  return (TMath::Abs(b-a) < fgkPrecision);
}

//_____________________________________________________________________________
Int_t	
AliMUONSegment::Compare(const TObject* obj) const
{
  /// Compare method, which sort segments in ascending x order
  /// if same x, insure that left edges are before right edges
  /// within same x, order by increasing bottommost y
  /// Mind your steps ! This method is critical to the contour merging algorithm !
  
  const AliMUONSegment* rhs = static_cast<const AliMUONSegment*>(obj);
  
  if ( AreEqual(StartX(),rhs->StartX()) )
  {
    if ( IsLeftEdge() && rhs->IsRightEdge() ) return -1;
    if ( IsRightEdge() && rhs->IsLeftEdge() ) return 1;
    if ( SmallerY() < rhs->SmallerY() ) return -1;
    if ( SmallerY() > rhs->SmallerY() ) return 1;
    return 0;
  }
  else if ( StartX() < rhs->StartX() )
  {
    return -1;
  }
  else //if ( StartX() > rhs->StartX() ) 
  {
    return 1;
  }
}

//_____________________________________________________________________________
double AliMUONSegment::Top() const 
{
  /// Max Y of the segment
  return TMath::Max(fStartY,fEndY); 
}

//_____________________________________________________________________________
double AliMUONSegment::Distance() const 
{
  /// Length of the segment
  return TMath::Sqrt((fStartX-fEndX)*(fStartX-fEndX) +
                     (fStartY-fEndY)*(fStartY-fEndY)); 
}

//_____________________________________________________________________________
void AliMUONSegment::Print(Option_t*) const
{
  /// Printout
  cout << AsString() << endl;
}

//_____________________________________________________________________________
const char* AliMUONSegment::AsString() const 
{
  /// Return a string representation of this object
  return Form("[ (%10.5f,%10.5f) -> (%10.5f,%10.5f) %s ] (d=%e)",fStartX,fStartY,fEndX,fEndY,
              IsLeftEdge() ? "L" : ( IsRightEdge() ? "R" : ( IsHorizontal() ? "H" : "" )),
              Distance() ); 
}  

//_____________________________________________________________________________
void 
AliMUONSegment::Set(Double_t xstart, Double_t ystart, Double_t xend, Double_t yend)
{
  /// Set start and end point, and (re)compute internal values
  fStartX = xstart;
  fEndX = xend;
  fStartY = ystart;
  fEndY = yend;
  fSmallerY = TMath::Min(fStartY,fEndY); 
  fIsHorizontal = AreEqual(fStartY,fEndY); 
  fIsVertical = AreEqual(fStartX,fEndX); 
  fIsLeftEdge = fIsVertical && ( fStartY > fEndY );
  fIsRightEdge = fIsVertical && ( fStartY < fEndY );
  fIsAPoint = ( Distance() < fgkPrecision );
}

 AliMUONSegment.cxx:1
 AliMUONSegment.cxx:2
 AliMUONSegment.cxx:3
 AliMUONSegment.cxx:4
 AliMUONSegment.cxx:5
 AliMUONSegment.cxx:6
 AliMUONSegment.cxx:7
 AliMUONSegment.cxx:8
 AliMUONSegment.cxx:9
 AliMUONSegment.cxx:10
 AliMUONSegment.cxx:11
 AliMUONSegment.cxx:12
 AliMUONSegment.cxx:13
 AliMUONSegment.cxx:14
 AliMUONSegment.cxx:15
 AliMUONSegment.cxx:16
 AliMUONSegment.cxx:17
 AliMUONSegment.cxx:18
 AliMUONSegment.cxx:19
 AliMUONSegment.cxx:20
 AliMUONSegment.cxx:21
 AliMUONSegment.cxx:22
 AliMUONSegment.cxx:23
 AliMUONSegment.cxx:24
 AliMUONSegment.cxx:25
 AliMUONSegment.cxx:26
 AliMUONSegment.cxx:27
 AliMUONSegment.cxx:28
 AliMUONSegment.cxx:29
 AliMUONSegment.cxx:30
 AliMUONSegment.cxx:31
 AliMUONSegment.cxx:32
 AliMUONSegment.cxx:33
 AliMUONSegment.cxx:34
 AliMUONSegment.cxx:35
 AliMUONSegment.cxx:36
 AliMUONSegment.cxx:37
 AliMUONSegment.cxx:38
 AliMUONSegment.cxx:39
 AliMUONSegment.cxx:40
 AliMUONSegment.cxx:41
 AliMUONSegment.cxx:42
 AliMUONSegment.cxx:43
 AliMUONSegment.cxx:44
 AliMUONSegment.cxx:45
 AliMUONSegment.cxx:46
 AliMUONSegment.cxx:47
 AliMUONSegment.cxx:48
 AliMUONSegment.cxx:49
 AliMUONSegment.cxx:50
 AliMUONSegment.cxx:51
 AliMUONSegment.cxx:52
 AliMUONSegment.cxx:53
 AliMUONSegment.cxx:54
 AliMUONSegment.cxx:55
 AliMUONSegment.cxx:56
 AliMUONSegment.cxx:57
 AliMUONSegment.cxx:58
 AliMUONSegment.cxx:59
 AliMUONSegment.cxx:60
 AliMUONSegment.cxx:61
 AliMUONSegment.cxx:62
 AliMUONSegment.cxx:63
 AliMUONSegment.cxx:64
 AliMUONSegment.cxx:65
 AliMUONSegment.cxx:66
 AliMUONSegment.cxx:67
 AliMUONSegment.cxx:68
 AliMUONSegment.cxx:69
 AliMUONSegment.cxx:70
 AliMUONSegment.cxx:71
 AliMUONSegment.cxx:72
 AliMUONSegment.cxx:73
 AliMUONSegment.cxx:74
 AliMUONSegment.cxx:75
 AliMUONSegment.cxx:76
 AliMUONSegment.cxx:77
 AliMUONSegment.cxx:78
 AliMUONSegment.cxx:79
 AliMUONSegment.cxx:80
 AliMUONSegment.cxx:81
 AliMUONSegment.cxx:82
 AliMUONSegment.cxx:83
 AliMUONSegment.cxx:84
 AliMUONSegment.cxx:85
 AliMUONSegment.cxx:86
 AliMUONSegment.cxx:87
 AliMUONSegment.cxx:88
 AliMUONSegment.cxx:89
 AliMUONSegment.cxx:90
 AliMUONSegment.cxx:91
 AliMUONSegment.cxx:92
 AliMUONSegment.cxx:93
 AliMUONSegment.cxx:94
 AliMUONSegment.cxx:95
 AliMUONSegment.cxx:96
 AliMUONSegment.cxx:97
 AliMUONSegment.cxx:98
 AliMUONSegment.cxx:99
 AliMUONSegment.cxx:100
 AliMUONSegment.cxx:101
 AliMUONSegment.cxx:102
 AliMUONSegment.cxx:103
 AliMUONSegment.cxx:104
 AliMUONSegment.cxx:105
 AliMUONSegment.cxx:106
 AliMUONSegment.cxx:107
 AliMUONSegment.cxx:108
 AliMUONSegment.cxx:109
 AliMUONSegment.cxx:110
 AliMUONSegment.cxx:111
 AliMUONSegment.cxx:112
 AliMUONSegment.cxx:113
 AliMUONSegment.cxx:114
 AliMUONSegment.cxx:115
 AliMUONSegment.cxx:116
 AliMUONSegment.cxx:117
 AliMUONSegment.cxx:118
 AliMUONSegment.cxx:119
 AliMUONSegment.cxx:120
 AliMUONSegment.cxx:121
 AliMUONSegment.cxx:122
 AliMUONSegment.cxx:123
 AliMUONSegment.cxx:124
 AliMUONSegment.cxx:125
 AliMUONSegment.cxx:126
 AliMUONSegment.cxx:127
 AliMUONSegment.cxx:128
 AliMUONSegment.cxx:129
 AliMUONSegment.cxx:130
 AliMUONSegment.cxx:131
 AliMUONSegment.cxx:132
 AliMUONSegment.cxx:133
 AliMUONSegment.cxx:134
 AliMUONSegment.cxx:135
 AliMUONSegment.cxx:136
 AliMUONSegment.cxx:137
 AliMUONSegment.cxx:138
 AliMUONSegment.cxx:139
 AliMUONSegment.cxx:140
 AliMUONSegment.cxx:141
 AliMUONSegment.cxx:142
 AliMUONSegment.cxx:143
 AliMUONSegment.cxx:144
 AliMUONSegment.cxx:145
 AliMUONSegment.cxx:146
 AliMUONSegment.cxx:147
 AliMUONSegment.cxx:148
 AliMUONSegment.cxx:149
 AliMUONSegment.cxx:150