0023258: Missing parenthesis
[occt.git] / src / IGESGeom / IGESGeom_ConicArc.cxx
1 // Created by: CKY / Contract Toubro-Larsen
2 // Copyright (c) 1993-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 //--------------------------------------------------------------------
21 //--------------------------------------------------------------------
22 //#59 rln 29.12.98 PRO17015
23
24 #include <IGESGeom_ConicArc.ixx>
25 #include <gp_Dir2d.hxx>
26 #include <gp_GTrsf.hxx>
27
28
29 IGESGeom_ConicArc::IGESGeom_ConicArc ()    {  }
30
31
32     void  IGESGeom_ConicArc::Init
33   (const Standard_Real A, const Standard_Real B,
34    const Standard_Real C, const Standard_Real D,  const Standard_Real E,
35    const Standard_Real F, const Standard_Real ZT, const gp_XY& aStart,
36    const gp_XY& anEnd)
37 {
38   theA = A;
39   theB = B;
40   theC = C;
41   theD = D;
42   theE = E;
43   theF = F;
44   theZT = ZT;
45   theStart = aStart;
46   theEnd   = anEnd;
47
48   Standard_Integer fn = FormNumber();
49   if (fn == 0) fn = ComputedFormNumber();
50   InitTypeAndForm(104,fn);
51 }
52
53     Standard_Boolean  IGESGeom_ConicArc::OwnCorrect ()
54 {
55   Standard_Integer cfn = ComputedFormNumber();
56   if (FormNumber() == cfn) return Standard_False;
57   InitTypeAndForm(104,cfn);
58   return Standard_True;
59 }
60
61     void  IGESGeom_ConicArc::Equation
62   (Standard_Real& A, Standard_Real& B, Standard_Real& C, Standard_Real& D,
63    Standard_Real& E, Standard_Real& F) const
64 {
65   A = theA;
66   B = theB;
67   C = theC;
68   D = theD;
69   E = theE;
70   F = theF;
71 }
72
73     Standard_Real  IGESGeom_ConicArc::ZPlane () const
74 {
75   return theZT;
76 }
77
78     gp_Pnt2d  IGESGeom_ConicArc::StartPoint () const
79 {
80   gp_Pnt2d start(theStart.X(), theStart.Y());
81   return start;
82 }
83
84     gp_Pnt  IGESGeom_ConicArc::TransformedStartPoint () const
85 {
86   gp_XYZ start(theStart.X(), theStart.Y(), theZT);
87   if (HasTransf()) Location().Transforms(start);
88   gp_Pnt transStart(start);
89   return transStart;
90 }
91
92     gp_Pnt2d  IGESGeom_ConicArc::EndPoint () const
93 {
94   gp_Pnt2d end(theEnd.X(), theEnd.Y());
95   return end;
96 }
97
98     gp_Pnt  IGESGeom_ConicArc::TransformedEndPoint () const
99 {
100   gp_XYZ end(theEnd.X(), theEnd.Y(), theZT);
101   if (HasTransf()) Location().Transforms(end);
102   gp_Pnt transEnd(end);
103   return transEnd;
104 }
105
106     Standard_Integer  IGESGeom_ConicArc::ComputedFormNumber () const
107 {
108   Standard_Real eps,eps2,eps4;
109   eps = 1.E-08;  eps2 = eps*eps; eps4 = eps2*eps2;//#59 rln
110   Standard_Real Q1 = theA   * (theC*theF   - theE*theE/4.)
111     + theB/2. * (theE*theD/4. - theB*theF/2.)
112       + theD/2. * (theB*theE/4. - theC*theD/2.);
113   Standard_Real Q2 = theA*theC - theB*theB/4;
114   Standard_Real Q3 = theA + theC;
115
116 //  Resultats
117   //#59 rln 29.12.98 PRO17015 face#67, ellipse
118   //each Qi has its own dimension:
119   //[Q1] = L^-4, [Q2]=L^-4, [Q3]=L^-2
120   if (Q2       >  eps4 && Q1*Q3    < 0   ) return 1;    // Ellipse
121   if (Q2       < -eps4 && Abs (Q1) > eps4) return 2;    // Hyperbola
122   if (Abs (Q2) <= eps4 && Abs (Q1) > eps4) return 3;    // Parabola
123   return 0;
124 }
125
126     Standard_Boolean  IGESGeom_ConicArc::IsFromParabola () const
127 {
128   Standard_Integer fn = FormNumber();
129   if (fn == 0)     fn = ComputedFormNumber();
130   return (fn == 3);
131 }
132
133     Standard_Boolean  IGESGeom_ConicArc::IsFromEllipse () const
134 {
135   Standard_Integer fn = FormNumber();
136   if (fn == 0)     fn = ComputedFormNumber();
137   return (fn == 1);
138 }
139
140     Standard_Boolean  IGESGeom_ConicArc::IsFromHyperbola () const
141 {
142   Standard_Integer fn = FormNumber();
143   if (fn == 0)     fn = ComputedFormNumber();
144   return (fn == 2);
145 }
146
147     Standard_Boolean  IGESGeom_ConicArc::IsClosed () const
148 {
149   return ((theStart.X() == theEnd.X()) && (theStart.Y() == theEnd.Y()));
150 }
151
152     gp_Dir IGESGeom_ConicArc::Axis () const
153 {
154   gp_Dir axis(0.0 , 0.0, 1.0);
155   return axis;
156 }
157
158 //    Valeurs calculees
159
160     gp_Dir IGESGeom_ConicArc::TransformedAxis () const
161 {
162   gp_XYZ axis(0.0 , 0.0, 1.0);
163   if (!HasTransf()) return gp_Dir(axis);
164   gp_GTrsf loc = Location();
165   loc.SetTranslationPart (gp_XYZ(0.,0.,0.));
166   loc.Transforms(axis);
167   return gp_Dir(axis);
168 }
169
170
171     void  IGESGeom_ConicArc::Definition
172   (gp_Pnt& Center, gp_Dir& MainAxis,
173    Standard_Real& Rmin, Standard_Real& Rmax) const
174 {
175   Standard_Real Xcen,Ycen, Xax,Yax;
176   ComputedDefinition (Xcen,Ycen, Xax,Yax, Rmin,Rmax);
177   Center.SetCoord (Xcen,Ycen,theZT);
178   MainAxis.SetCoord (Xax,Yax,0.);
179 }
180
181     void  IGESGeom_ConicArc::TransformedDefinition
182   (gp_Pnt& Center, gp_Dir& MainAxis,
183    Standard_Real& Rmin, Standard_Real& Rmax) const
184 {
185   if (!HasTransf()) {
186     Definition (Center,MainAxis,Rmin,Rmax);
187     return;
188   }
189   Standard_Real Xcen,Ycen, Xax,Yax;
190   ComputedDefinition (Xcen,Ycen, Xax,Yax, Rmin,Rmax);
191   gp_GTrsf loc = Location();
192   gp_XYZ cen  (Xcen,Ycen,theZT);
193   gp_XYZ axis (Xax, Yax, 0.);
194   loc.Transforms (cen);
195   loc.SetTranslationPart (gp_XYZ(0.,0.,0.));
196   loc.Transforms (axis);
197   Center.SetCoord   (cen.X(), cen.Y(), cen.Z() );
198   MainAxis.SetCoord (axis.X(),axis.Y(),axis.Z());
199 }
200
201
202     void  IGESGeom_ConicArc::ComputedDefinition
203   (Standard_Real& Xcen, Standard_Real& Ycen,
204    Standard_Real& Xax,  Standard_Real& Yax,
205    Standard_Real& Rmin, Standard_Real& Rmax) const
206 {
207   Standard_Real a,b,c,d,e,f;
208   //  conic : a*x2 + 2*b*x*y + c*y2 + 2*d*x + 2*e*y + f = 0.
209   Equation (a,b,c,d,e,f);
210   b = b/2.;  d = d/2.;  e = e/2.;    // chgt de variable
211
212   Standard_Real eps = 1.E-08;  // ?? comme ComputedForm
213
214   if (IsFromParabola()) {
215     Rmin = Rmax = -1.;  // rayons : yena pas
216     if ( (Abs(a) <= eps) && (Abs(b) <= eps)) {
217       Xcen = (f*c - e*e) /c /d /2.;
218       Ycen = e/c;
219       Standard_Real focal = -d/c;
220       Xax  = (focal >= 0 ? 1. : -1.);
221       Yax  = 0.;
222       Rmin = Rmax = Abs(focal);
223     } 
224     else {
225       Standard_Real ss = a+c;
226       Standard_Real cc =   - (a*d+b*e) / ss;
227       Standard_Real dd = d + (c*d - b*e) / ss;
228       Standard_Real fc =     (a*e - b*d) / ss;
229       Standard_Real ee = e + fc;
230
231       Standard_Real dn = a*ee - dd*b;
232       Xcen = ( cc*ee + f*b) / dn;
233       Ycen = (-cc*dd - f*a) / dn;
234
235       Standard_Real teta = M_PI/2.;
236       if (Abs(b) > eps) teta = ATan (-a/b);
237       if (fc < 0) teta += M_PI;
238       Xax  = Cos(teta);
239       Yax  = Sin(teta);
240
241       Rmin = Rmax = Abs(fc)/sqrt(a*a+b*b)/2.;
242     }
243   } 
244
245   else {
246     //   -> Conique a centre, cas general
247     //  On utilise les Determinants des matrices :
248     //               | a b d |
249     //  gdet (3x3) = | b c e |  et pdet (2X2) = | a b |
250     //               | d e f |                  | b c |
251     
252     Standard_Real gdet = a*c*f + 2*b*d*e - c*d*d - a*e*e - b*b*f;
253     Standard_Real pdet = a*c - b*b;
254     
255     Xcen = (b*e - c*d) / pdet;
256     Ycen = (b*d - a*e) / pdet;
257
258     Standard_Real term1 = a-c;
259     Standard_Real term2 = 2*b;
260     Standard_Real cos2t;
261     Standard_Real auxil;
262
263     if (Abs(term2)<= eps && Abs(term1)<= eps) {
264       cos2t = 1.;
265       auxil = 0.;
266     }
267     else {
268       Standard_Real t2d = term2/term1; //skl 28.12.2001
269       cos2t = 1./sqrt(1+t2d*t2d);
270       auxil = sqrt (term1*term1 + term2*term2);
271     }
272     
273     Standard_Real cost = sqrt ( (1+cos2t)/2. );
274     Standard_Real sint = sqrt ( (1-cos2t)/2. );
275
276     Standard_Real aprim = (a+c+auxil)/2.;
277     Standard_Real cprim = (a+c-auxil)/2.;
278     
279     term1 = -gdet/(aprim*pdet);
280     term2 = -gdet/(cprim*pdet);
281       
282     if (IsFromEllipse()) {
283       Xax = cost;
284       Yax = sint;
285       Rmin = sqrt ( term1);
286       Rmax = sqrt ( term2);
287       if(Rmax<Rmin){    //skl 28.12.2001
288         Rmax = sqrt ( term1);
289         Rmin = sqrt ( term2);
290       }
291     }
292     else if (term1 <= eps){
293       Xax  = -sint;
294       Yax  =  cost;
295       Rmin = sqrt (-term1);
296       Rmax = sqrt (term2);
297     } 
298     else {
299       Xax  =  cost;
300       Yax  =  sint;
301       Rmin = sqrt (-term2);
302       Rmax = sqrt (term1);
303     }
304   }
305 }