ROOT logo

#include "hmdcsizescells.h"

#include "hkalgeomtools.h"

// from ROOT
#include "TGeoTorus.h"
#include "TMath.h"
#include "TVector3.h"

#include <iostream>
using namespace std;

ClassImp(HKalGeomTools)

//_HADES_CLASS_DESCRIPTION
///////////////////////////////////////////////////////////////////////////////
//
// Provides static functions to solve some geometric problems.
//
///////////////////////////////////////////////////////////////////////////////

//  -----------------------------------
//  Implementation of public methods
//  -----------------------------------

Double_t HKalGeomTools::distance2Points(const TVector3 &vec1, const TVector3 &vec2) {
    // Calculates the distance between two points.
    TVector3 distV = vec1 - vec2;
    return distV.Mag();
}

Double_t HKalGeomTools::distancePointToPlane(const TVector3 &point, const TVector3 &planeCenter, const TVector3 &planeNormal) {
    // Calculates the distance of vector pos to the plane.

    // Plane equation in Hesse Normal Form: center * normal - d = 0
    // Distance of point to plane = point * normal - d
    return TMath::Abs(signedDistancePointToPlane(point, planeCenter, planeNormal));
}

Double_t HKalGeomTools::distancePointToLine(TVector3 &pca, Int_t &pcaFlag, const TVector3 &point, const TVector3 &segPoint1, const TVector3 &segPoint2) {
    // Calculates the distance between a test point and a line defined by two points.
    // This function also find the point on the segment that is closest to the test point.
    // For method used see:
    // http://www.geometrictools.com/Documentation/DistancePointLine.pdf
    //
    // return value: Distance of the test point to the segment.
    // output parameters:
    // pca:          The point on the line that is closest to the test point.
    // iFlag:        =0 if the PCA is on the segment,
    //               =1 the PCA is one of the segment end points.
    // input parameters:
    // point:        Find the distance of this test point to the line segment.
    // segPoint1, segPoint2: The two points defining the line segment.

    if(segPoint1 == segPoint2) {
	::Warning("distancePointToLine()", "Both line points are equal.");
        return (point - segPoint1).Mag();
    }

    TVector3 segDir = segPoint2 - segPoint1;
    TVector3 diff   = point - segPoint1;
    Double_t segPar = segDir * diff / (segDir * segDir);
    Double_t dist   = 0.;

    if(segPar > 1. || segPar <= 0.) {
        pcaFlag = 1;
    } else {
        pcaFlag = 0;
    }
    pca = segPoint1 + segPar * segDir;
    dist = (diff - segPar * segDir).Mag();
    return dist;
}

Double_t HKalGeomTools::distancePointToSegment(const TVector3 &point, const TVector3 &segPoint1, const TVector3 &segPoint2) {
    // Calculates the distance between a test point and a line segment.
    // For method used see:
    // http://www.geometrictools.com/Documentation/DistancePointLine.pdf
    //
    // input:
    // point: the test point
    // segPoint1, segPoint2: The two points defining the line segment.

    TVector3 pca;
    Int_t iFlag;
    return distancePointToSegment(pca, iFlag, point, segPoint1, segPoint2);
}

Double_t HKalGeomTools::distancePointToSegment(TVector3 &pca, Int_t &pcaFlag, const TVector3 &point, const TVector3 &segPoint1, const TVector3 &segPoint2) {
    // Calculates the distance between a test point and a line segment defined by two points.
    // This function also find the point on the segment that is closest to the test point.
    // For method used see:
    // http://www.geometrictools.com/Documentation/DistancePointLine.pdf
    //
    // return value: Distance of the test point to the segment.
    // output parameters:
    // pca:          The point on the line segment that is closest to the test point.
    // iFlag:        =0 if the PCA is on the segment,
    //               =1 the PCA is one of the segment end points.
    // input parameters:
    // point:        Find the distance of this test point to the line segment.
    // segPoint1, segPoint2: The two points defining the line segment.

    if(segPoint1 == segPoint2) {
	::Warning("distancePointToSegment()", "Both segment end points are equal.");
        return (point - segPoint1).Mag();
    }

    TVector3 segDir = segPoint2 - segPoint1;
    TVector3 diff   = point - segPoint1;
    Double_t segPar = segDir * diff / (segDir * segDir);
    Double_t dist   = 0.;
    pcaFlag           = 0;
    if(segPar > 0.) {
        // 0 < segPar < 1
        if(segPar < 1.) {
            dist = (diff - segPar * segDir).Mag();
            pca  = segPoint1 + segPar * segDir;
        } else { // segPar >= 1
	    dist  = (diff - segDir).Mag();
            pca   = segPoint2;
            pcaFlag = 1;
        }
    } else { // segPar <= 0
	dist  = diff.Mag();
        pca   = segPoint1;
        pcaFlag = 1;
    }
    return dist;
}


Bool_t HKalGeomTools::findIntersectionLinePlane(TVector3 &pointIntersect,
						const TVector3 &pos, const TVector3 &dir,
						const TVector3 &planeCenter, const  TVector3 &planeNormal) {
    // Finds the intersection point of a straight line with any plane.
    // pointIntersect: the intersection point (return value).
    // pos:            a point on the straight line.
    // dir:            direction of the straight line.
    // planeCenter:    a point on the plane.
    // planeNormal:    normal vector of the plane.

    Double_t denom = planeNormal.Dot(dir);
    if (denom != 0.0) {
        Double_t t = ((planeCenter.x() - pos.x()) * planeNormal.x() +
		      (planeCenter.y() - pos.y()) * planeNormal.y() +
		      (planeCenter.z() - pos.z()) * planeNormal.z()) / denom;

        pointIntersect = pos + (t * dir);
        return kTRUE;
    } else {
	::Warning("findIntersection()", "No intersection point found : (plane || track)");
        return kFALSE;
    }
    return kFALSE;
}

Bool_t HKalGeomTools::isPointOnPlane(const TVector3 &point, const TVector3 &planeCenter, const TVector3 &planeNormal){
    // Checks if point is on the plane.
    Double_t dist = distancePointToPlane(point, planeCenter, planeNormal);
    return ((TMath::Abs(dist) < 1.e-10) ? kTRUE : kFALSE);
}

Double_t HKalGeomTools::signedDistancePointToPlane(const TVector3 &point, const TVector3 &planeCenter, const TVector3 &planeNormal) {
    // Calculates the distance of a test point to the plane.
    // The signed distance is positive if the origin of the coordinate system and the test point
    // are on opposite sides of the plane and negative if they are on the same side.
    //
    // input parameters:
    // point: test point
    // planeCenter: a point on the plane
    // planeNormal: normal vector of the plane


    // Plane equation in Hesse Normal Form: center * normal - d = 0
    // Distance of point to plane = point * normal - d
    if(planeCenter * planeNormal < 0.) { // Normal vector of the plane is pointing in the wrong direction.
	return (point - planeCenter) * planeNormal.Unit() * -1.;
    }
    return (point - planeCenter) * planeNormal.Unit();
}

void HKalGeomTools::Track2ToLine(TVector3 &Pfinal, TVector3 &Pwire, Int_t &Iflag,
                  Double_t &Dist, Double_t &Length,
                  const TVector3 &X1, const TVector3 &X2,
                  const TVector3 &w1, const TVector3 &w2) {
    // Code from GEANE.
    //
    // Closest approach to a line from 2 GEANE points
    //
    // METHOD: the nearest points on the two lines
    //         x1,x2 and w1,w2 is found.
    //         The method is described in:
    //  http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
    //  http://www.geometrictools.com/Documentation/DistanceLine3Line3.pdf.
    //
    // INPUT: x1, x2   closest appoach GEANE points
    //        w1, w2   points of the line  (wire)  to approach
    //
    // OUTPUT: Pfinal  point of closest approach on the track
    //         Pwire    point of closest approach on the wire
    //         Dist    distance between Pfian and w1
    //         Length  arc length to add to the GEANE length of x1
    //         Iflag   =1 when Pwire is outside [w1,w2]
    //                 In this case, when w1 and w2 are the extremes
    //                 of the wire, the user could remake the procedure
    //                 by calling Track3ToPoint or Track2ToPoint, where the
    //                 Point is w1 or w2;
    //                 = 2 when the two lines are parallel and the solution
    //                 does not exists.
    //
    // Authors: Andrea Fontana and Alberto Rotondi 20 MAy 2007
    //


    TVector3 x21, x32, w21;
    TVector3 xw1, xw2;

    Double_t a1, b1, c1, d1, e1, t1, s1;
    Double_t Delta1;

    Double_t Eps = 1.E-08;

    Iflag =0;

    // line-line distance
    x21 = X2-X1;
    w21 = w2-w1;

    xw1 = X1-w1;
    xw2 = X2-w1;

    a1 =  x21.Mag2();
    b1 =  x21.Dot(w21);
    c1 =  w21.Mag2();
    d1 =  xw1.Dot(x21);
    e1 =  xw1.Dot(w21);

    Delta1 = a1*c1-b1*b1;

    if(Delta1 > Eps) {
      t1 = (a1*e1-b1*d1)/Delta1;
      s1 = (b1*e1-c1*d1)/Delta1;

      Pfinal = (X1 + x21*s1);
      Pwire  = (w1 + w21*t1);
      Length = s1*x21.Mag();
      Dist= (Pfinal-Pwire).Mag();
    }
    else {
      // lines are parallel, no solution does exist
      Pfinal.SetXYZ(0.,0.,0.);
      Pwire.SetXYZ(0.,0.,0.);
      Dist=0.;
      Length=0.;
      Iflag = 2;
      return;
    }
    // flag when the point on the wire is outside (w1,w2)
    if((((Pwire[0]<w1[0] && Pwire[0]<w2[0]) || (w2[0]<Pwire[0] && w1[0]<Pwire[0]))
        && (fabs(Pwire[0]-w1[0]) > 1e-11 && fabs(Pwire[0]- w2[0]) > 1e-11))
       || (((Pwire[1]<w1[1] && Pwire[1]<w2[1]) || (w2[1]<Pwire[1] && w1[1]<Pwire[1]))
           && (fabs(Pwire[1]-w1[1]) > 1e-11 && fabs(Pwire[1]- w2[1]) > 1e-11))
       || (((Pwire[2]<w1[2] && Pwire[2]<w2[2]) || (w2[2]<Pwire[2] && w1[2]<Pwire[2]))
           && (fabs(Pwire[2]-w1[2]) > 1e-11 && fabs(Pwire[2]- w2[2]) > 1e-11)))
      {
        Iflag=1;
      }
}

void HKalGeomTools::TransModuleToSector(Double_t &x, Double_t &y, Double_t &z,
					Int_t sector, Int_t module) {
    HMdcSizesCells* fSizesCells = HMdcSizesCells::getExObject();  // check if is already there
    if(fSizesCells) {
	((*fSizesCells)[sector][module]).transFrom(x,y,z); // transform to sector coordinate system
    } else {
	::Error("TransModuleToSector()", "No HMdcSizesCells.");
    }
}

void HKalGeomTools::TransLayerToSector(Double_t &x, Double_t &y, Double_t &z,
				       Int_t sector, Int_t module, Int_t layer) {
    HMdcSizesCells* fSizesCells = HMdcSizesCells::getExObject();  // check if is already there
    if(fSizesCells) {
	((*fSizesCells)[sector][module][layer]).transFrom(x,y,z); // transform to sector coordinate system
    } else {
	::Error("TransLayerToSector()", "No HMdcSizesCells.");
    }
}
 hkalgeomtools.cc:1
 hkalgeomtools.cc:2
 hkalgeomtools.cc:3
 hkalgeomtools.cc:4
 hkalgeomtools.cc:5
 hkalgeomtools.cc:6
 hkalgeomtools.cc:7
 hkalgeomtools.cc:8
 hkalgeomtools.cc:9
 hkalgeomtools.cc:10
 hkalgeomtools.cc:11
 hkalgeomtools.cc:12
 hkalgeomtools.cc:13
 hkalgeomtools.cc:14
 hkalgeomtools.cc:15
 hkalgeomtools.cc:16
 hkalgeomtools.cc:17
 hkalgeomtools.cc:18
 hkalgeomtools.cc:19
 hkalgeomtools.cc:20
 hkalgeomtools.cc:21
 hkalgeomtools.cc:22
 hkalgeomtools.cc:23
 hkalgeomtools.cc:24
 hkalgeomtools.cc:25
 hkalgeomtools.cc:26
 hkalgeomtools.cc:27
 hkalgeomtools.cc:28
 hkalgeomtools.cc:29
 hkalgeomtools.cc:30
 hkalgeomtools.cc:31
 hkalgeomtools.cc:32
 hkalgeomtools.cc:33
 hkalgeomtools.cc:34
 hkalgeomtools.cc:35
 hkalgeomtools.cc:36
 hkalgeomtools.cc:37
 hkalgeomtools.cc:38
 hkalgeomtools.cc:39
 hkalgeomtools.cc:40
 hkalgeomtools.cc:41
 hkalgeomtools.cc:42
 hkalgeomtools.cc:43
 hkalgeomtools.cc:44
 hkalgeomtools.cc:45
 hkalgeomtools.cc:46
 hkalgeomtools.cc:47
 hkalgeomtools.cc:48
 hkalgeomtools.cc:49
 hkalgeomtools.cc:50
 hkalgeomtools.cc:51
 hkalgeomtools.cc:52
 hkalgeomtools.cc:53
 hkalgeomtools.cc:54
 hkalgeomtools.cc:55
 hkalgeomtools.cc:56
 hkalgeomtools.cc:57
 hkalgeomtools.cc:58
 hkalgeomtools.cc:59
 hkalgeomtools.cc:60
 hkalgeomtools.cc:61
 hkalgeomtools.cc:62
 hkalgeomtools.cc:63
 hkalgeomtools.cc:64
 hkalgeomtools.cc:65
 hkalgeomtools.cc:66
 hkalgeomtools.cc:67
 hkalgeomtools.cc:68
 hkalgeomtools.cc:69
 hkalgeomtools.cc:70
 hkalgeomtools.cc:71
 hkalgeomtools.cc:72
 hkalgeomtools.cc:73
 hkalgeomtools.cc:74
 hkalgeomtools.cc:75
 hkalgeomtools.cc:76
 hkalgeomtools.cc:77
 hkalgeomtools.cc:78
 hkalgeomtools.cc:79
 hkalgeomtools.cc:80
 hkalgeomtools.cc:81
 hkalgeomtools.cc:82
 hkalgeomtools.cc:83
 hkalgeomtools.cc:84
 hkalgeomtools.cc:85
 hkalgeomtools.cc:86
 hkalgeomtools.cc:87
 hkalgeomtools.cc:88
 hkalgeomtools.cc:89
 hkalgeomtools.cc:90
 hkalgeomtools.cc:91
 hkalgeomtools.cc:92
 hkalgeomtools.cc:93
 hkalgeomtools.cc:94
 hkalgeomtools.cc:95
 hkalgeomtools.cc:96
 hkalgeomtools.cc:97
 hkalgeomtools.cc:98
 hkalgeomtools.cc:99
 hkalgeomtools.cc:100
 hkalgeomtools.cc:101
 hkalgeomtools.cc:102
 hkalgeomtools.cc:103
 hkalgeomtools.cc:104
 hkalgeomtools.cc:105
 hkalgeomtools.cc:106
 hkalgeomtools.cc:107
 hkalgeomtools.cc:108
 hkalgeomtools.cc:109
 hkalgeomtools.cc:110
 hkalgeomtools.cc:111
 hkalgeomtools.cc:112
 hkalgeomtools.cc:113
 hkalgeomtools.cc:114
 hkalgeomtools.cc:115
 hkalgeomtools.cc:116
 hkalgeomtools.cc:117
 hkalgeomtools.cc:118
 hkalgeomtools.cc:119
 hkalgeomtools.cc:120
 hkalgeomtools.cc:121
 hkalgeomtools.cc:122
 hkalgeomtools.cc:123
 hkalgeomtools.cc:124
 hkalgeomtools.cc:125
 hkalgeomtools.cc:126
 hkalgeomtools.cc:127
 hkalgeomtools.cc:128
 hkalgeomtools.cc:129
 hkalgeomtools.cc:130
 hkalgeomtools.cc:131
 hkalgeomtools.cc:132
 hkalgeomtools.cc:133
 hkalgeomtools.cc:134
 hkalgeomtools.cc:135
 hkalgeomtools.cc:136
 hkalgeomtools.cc:137
 hkalgeomtools.cc:138
 hkalgeomtools.cc:139
 hkalgeomtools.cc:140
 hkalgeomtools.cc:141
 hkalgeomtools.cc:142
 hkalgeomtools.cc:143
 hkalgeomtools.cc:144
 hkalgeomtools.cc:145
 hkalgeomtools.cc:146
 hkalgeomtools.cc:147
 hkalgeomtools.cc:148
 hkalgeomtools.cc:149
 hkalgeomtools.cc:150
 hkalgeomtools.cc:151
 hkalgeomtools.cc:152
 hkalgeomtools.cc:153
 hkalgeomtools.cc:154
 hkalgeomtools.cc:155
 hkalgeomtools.cc:156
 hkalgeomtools.cc:157
 hkalgeomtools.cc:158
 hkalgeomtools.cc:159
 hkalgeomtools.cc:160
 hkalgeomtools.cc:161
 hkalgeomtools.cc:162
 hkalgeomtools.cc:163
 hkalgeomtools.cc:164
 hkalgeomtools.cc:165
 hkalgeomtools.cc:166
 hkalgeomtools.cc:167
 hkalgeomtools.cc:168
 hkalgeomtools.cc:169
 hkalgeomtools.cc:170
 hkalgeomtools.cc:171
 hkalgeomtools.cc:172
 hkalgeomtools.cc:173
 hkalgeomtools.cc:174
 hkalgeomtools.cc:175
 hkalgeomtools.cc:176
 hkalgeomtools.cc:177
 hkalgeomtools.cc:178
 hkalgeomtools.cc:179
 hkalgeomtools.cc:180
 hkalgeomtools.cc:181
 hkalgeomtools.cc:182
 hkalgeomtools.cc:183
 hkalgeomtools.cc:184
 hkalgeomtools.cc:185
 hkalgeomtools.cc:186
 hkalgeomtools.cc:187
 hkalgeomtools.cc:188
 hkalgeomtools.cc:189
 hkalgeomtools.cc:190
 hkalgeomtools.cc:191
 hkalgeomtools.cc:192
 hkalgeomtools.cc:193
 hkalgeomtools.cc:194
 hkalgeomtools.cc:195
 hkalgeomtools.cc:196
 hkalgeomtools.cc:197
 hkalgeomtools.cc:198
 hkalgeomtools.cc:199
 hkalgeomtools.cc:200
 hkalgeomtools.cc:201
 hkalgeomtools.cc:202
 hkalgeomtools.cc:203
 hkalgeomtools.cc:204
 hkalgeomtools.cc:205
 hkalgeomtools.cc:206
 hkalgeomtools.cc:207
 hkalgeomtools.cc:208
 hkalgeomtools.cc:209
 hkalgeomtools.cc:210
 hkalgeomtools.cc:211
 hkalgeomtools.cc:212
 hkalgeomtools.cc:213
 hkalgeomtools.cc:214
 hkalgeomtools.cc:215
 hkalgeomtools.cc:216
 hkalgeomtools.cc:217
 hkalgeomtools.cc:218
 hkalgeomtools.cc:219
 hkalgeomtools.cc:220
 hkalgeomtools.cc:221
 hkalgeomtools.cc:222
 hkalgeomtools.cc:223
 hkalgeomtools.cc:224
 hkalgeomtools.cc:225
 hkalgeomtools.cc:226
 hkalgeomtools.cc:227
 hkalgeomtools.cc:228
 hkalgeomtools.cc:229
 hkalgeomtools.cc:230
 hkalgeomtools.cc:231
 hkalgeomtools.cc:232
 hkalgeomtools.cc:233
 hkalgeomtools.cc:234
 hkalgeomtools.cc:235
 hkalgeomtools.cc:236
 hkalgeomtools.cc:237
 hkalgeomtools.cc:238
 hkalgeomtools.cc:239
 hkalgeomtools.cc:240
 hkalgeomtools.cc:241
 hkalgeomtools.cc:242
 hkalgeomtools.cc:243
 hkalgeomtools.cc:244
 hkalgeomtools.cc:245
 hkalgeomtools.cc:246
 hkalgeomtools.cc:247
 hkalgeomtools.cc:248
 hkalgeomtools.cc:249
 hkalgeomtools.cc:250
 hkalgeomtools.cc:251
 hkalgeomtools.cc:252
 hkalgeomtools.cc:253
 hkalgeomtools.cc:254
 hkalgeomtools.cc:255
 hkalgeomtools.cc:256
 hkalgeomtools.cc:257
 hkalgeomtools.cc:258
 hkalgeomtools.cc:259
 hkalgeomtools.cc:260
 hkalgeomtools.cc:261
 hkalgeomtools.cc:262
 hkalgeomtools.cc:263
 hkalgeomtools.cc:264
 hkalgeomtools.cc:265
 hkalgeomtools.cc:266
 hkalgeomtools.cc:267
 hkalgeomtools.cc:268
 hkalgeomtools.cc:269
 hkalgeomtools.cc:270
 hkalgeomtools.cc:271
 hkalgeomtools.cc:272
 hkalgeomtools.cc:273
 hkalgeomtools.cc:274
 hkalgeomtools.cc:275
 hkalgeomtools.cc:276
 hkalgeomtools.cc:277
 hkalgeomtools.cc:278
 hkalgeomtools.cc:279
 hkalgeomtools.cc:280
 hkalgeomtools.cc:281
 hkalgeomtools.cc:282
 hkalgeomtools.cc:283
 hkalgeomtools.cc:284
 hkalgeomtools.cc:285
 hkalgeomtools.cc:286
 hkalgeomtools.cc:287
 hkalgeomtools.cc:288
 hkalgeomtools.cc:289
 hkalgeomtools.cc:290