1 // File: IntPatch_PolyLine.cxx
2 // Created: Fri Jan 29 17:14:59 1993
3 // Author: Isabelle GRIGNON
4 // Copyright: Matra Datavision 1993
6 //-- lbr le 12 juin : Ajout des fleches sur les Lines
7 //-- msv 13.03.2002 : compute deflection for WLine; Error() returns deflection
9 #include <IntPatch_PolyLine.ixx>
11 #include <Precision.hxx>
13 #define INITDEFLE Precision::PConfusion()*100.
15 //=======================================================================
16 //function : IntPatch_PolyLine
18 //=======================================================================
20 IntPatch_PolyLine::IntPatch_PolyLine ()
21 : IntPatch_Polygo(INITDEFLE)
24 //=======================================================================
25 //function : IntPatch_PolyLine
27 //=======================================================================
29 IntPatch_PolyLine::IntPatch_PolyLine (const Standard_Real InitDefle)
30 : IntPatch_Polygo(InitDefle)
33 //=======================================================================
36 //=======================================================================
38 void IntPatch_PolyLine::SetWLine(const Standard_Boolean OnFirst, const Handle(IntPatch_WLine)& Line)
40 typ = IntPatch_Walking;
46 //=======================================================================
49 //=======================================================================
51 void IntPatch_PolyLine::SetRLine(const Standard_Boolean OnFirst, const Handle(IntPatch_RLine)& Line)
53 typ = IntPatch_Restriction;
59 //=======================================================================
62 //=======================================================================
64 void IntPatch_PolyLine::Prepare()
68 Standard_Integer n=NbPoints();
69 Standard_Real eps = myError;
73 P1 = Point(1); P2 = Point(2);
75 for (i=1; i<=n ;i++) {
76 const gp_Pnt2d& P3 = Point(i);
78 gp_XY V13 = P3.XY() - P1.XY();
79 gp_XY V12 = P2.XY() - P1.XY();
80 Standard_Real d13 = V13.Modulus(), d;
82 d = V13.CrossMagnitude(V12) / d13;
86 // try to compute deflection more precisely using parabola interpolation
87 gp_XY V23 = P3.XY() - P2.XY();
88 Standard_Real d12 = V12.Modulus(), d23 = V23.Modulus();
89 // compute parameter of P2 (assume parameters of P1,P3 are 0,1)
90 Standard_Real tm = d12 / (d12+d23);
91 if (tm > 0.1 && tm < 0.9) {
93 Standard_Real tm1mtm = tm*(1-tm);
94 // coefficients of parabola
95 Standard_Real Ax = (tm*V13.X() - V12.X()) / tm1mtm;
96 Standard_Real Bx = (V12.X() - tm*tm*V13.X()) / tm1mtm;
97 Standard_Real Cx = P1.X();
98 Standard_Real Ay = (tm*V13.Y() - V12.Y()) / tm1mtm;
99 Standard_Real By = (V12.Y() - tm*tm*V13.Y()) / tm1mtm;
100 Standard_Real Cy = P1.Y();
101 // equations of lines P1-P2 and P2-P3
102 Standard_Real A1 = V12.Y() / d12;
103 Standard_Real B1 = -V12.X() / d12;
104 Standard_Real C1 = (P2.X()*P1.Y() - P1.X()*P2.Y()) / d12;
105 Standard_Real A2 = V23.Y() / d23;
106 Standard_Real B2 = -V23.X() / d23;
107 Standard_Real C2 = (P3.X()*P2.Y() - P2.X()*P3.Y()) / d23;
108 // points on parabola with max deflection
109 Standard_Real t1 = -0.5 * (A1*Bx + B1*By) / (A1*Ax + B1*Ay);
110 Standard_Real t2 = -0.5 * (A2*Bx + B2*By) / (A2*Ax + B2*Ay);
111 Standard_Real xt1 = Ax*t1*t1 + Bx*t1 + Cx;
112 Standard_Real yt1 = Ay*t1*t1 + By*t1 + Cy;
113 Standard_Real xt2 = Ax*t2*t2 + Bx*t2 + Cx;
114 Standard_Real yt2 = Ay*t2*t2 + By*t2 + Cy;
115 // max deflection on segments P1-P2 and P2-P3
116 Standard_Real d1 = Abs (A1*xt1 + B1*yt1 + C1);
117 Standard_Real d2 = Abs (A2*xt2 + B2*yt2 + C2);
118 if (d2 > d1) d1 = d2;
119 // select min deflection from linear and parabolic ones
122 if (d > myError) myError=d;
128 myBox.Enlarge(myError);
131 //=======================================================================
132 //function : ResetError
134 //=======================================================================
136 void IntPatch_PolyLine::ResetError()
141 //=======================================================================
142 //function : NbPoints
144 //=======================================================================
146 Standard_Integer IntPatch_PolyLine::NbPoints() const
148 return (typ == IntPatch_Walking ? wpoly->NbPnts() : rpoly->NbPnts());
151 //=======================================================================
154 //=======================================================================
156 gp_Pnt2d IntPatch_PolyLine::Point(const Standard_Integer Index ) const
158 Standard_Real X,Y,X1,Y1,DX,DY;
161 if (typ == IntPatch_Walking) {
162 wpoly->Point(Index).ParametersOnS1(X,Y);
164 wpoly->Point(2).ParametersOnS1(X1,Y1);
168 else if(Index==wpoly->NbPnts()) {
169 wpoly->Point(Index-1).ParametersOnS1(X1,Y1);
175 rpoly->Point(Index).ParametersOnS1(X,Y);
179 if (typ == IntPatch_Walking) {
180 wpoly->Point(Index).ParametersOnS2(X,Y);
182 wpoly->Point(2).ParametersOnS2(X1,Y1);
186 else if(Index==wpoly->NbPnts()) {
187 wpoly->Point(Index-1).ParametersOnS2(X1,Y1);
193 rpoly->Point(Index).ParametersOnS2(X,Y);
197 return(gp_Pnt2d(X+DX,Y+DY));