0032951: Coding - get rid of unused headers [GeomConvert to IGESBasic]
[occt.git] / src / GeomConvert / GeomConvert_Units.cxx
1 // Copyright (c) 2021 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <GeomConvert_Units.hxx>
15 #include <Geom2d_Circle.hxx>
16 #include <Geom2d_Conic.hxx>
17 #include <Geom2d_Curve.hxx>
18 #include <Geom2d_Ellipse.hxx>
19 #include <Geom2d_Hyperbola.hxx>
20 #include <Geom2d_Line.hxx>
21 #include <Geom2d_Parabola.hxx>
22 #include <Geom2dConvert.hxx>
23 #include <Geom_ConicalSurface.hxx>
24 #include <Geom_CylindricalSurface.hxx>
25 #include <Geom_Plane.hxx>
26 #include <Geom_SphericalSurface.hxx>
27 #include <Geom_Surface.hxx>
28 #include <Geom_SurfaceOfRevolution.hxx>
29 #include <Geom_ToroidalSurface.hxx>
30 #include <gp.hxx>
31 #include <gp_Dir2d.hxx>
32 #include <gp_GTrsf2d.hxx>
33 #include <gp_Pnt2d.hxx>
34 #include <gp_Trsf2d.hxx>
35
36 // ============================================================================
37 // Method : RadianToDegree
38 // Purpose:
39 // ============================================================================
40 Handle(Geom2d_Curve) GeomConvert_Units::RadianToDegree(
41   const Handle(Geom2d_Curve)  & theCurve2d,
42   const Handle(Geom_Surface)  & theSurf,
43   const Standard_Real theLengthFactor,
44   const Standard_Real theFactorRadianDegree)
45 {
46   Handle(Geom2d_Curve) aCurve2d = Handle(Geom2d_Curve)::DownCast(theCurve2d->Copy());
47   Standard_Real uFact = 1.;
48   Standard_Real vFact = 1.;
49   Standard_Real LengthFact = 1. / theLengthFactor;
50   Standard_Real AngleFact = theFactorRadianDegree;    // 180./PI;  pilotable
51
52   gp_Pnt2d    Pt1;
53   gp_XY       pXY;
54   gp_GTrsf2d  tMatu, tMatv;
55
56   //  theSurf is a CylindricalSurface or a ConicalSurface or
57   //             a ToroidalSurface or a SphericalSurface or
58   //             a SurfaceOfRevolution
59   if (theSurf->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) || theSurf->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)))
60   {
61     uFact = vFact = AngleFact;
62   }
63   else if (theSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
64   {
65     uFact = AngleFact;
66     vFact = LengthFact;
67   }
68   else if (theSurf->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)))
69   {
70     uFact = AngleFact;
71   }
72   else if (theSurf->IsKind(STANDARD_TYPE(Geom_ConicalSurface)))
73   {
74     Handle(Geom_ConicalSurface) conicS = Handle(Geom_ConicalSurface)::DownCast(theSurf);
75     Standard_Real semAng = conicS->SemiAngle();
76     uFact = AngleFact;
77     vFact = LengthFact * Cos(semAng);
78   }
79   else if (theSurf->IsKind(STANDARD_TYPE(Geom_Plane)))
80   {
81     uFact = vFact = LengthFact;
82     if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Circle)) || aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Ellipse)))
83     {
84       gp_Trsf2d aT;
85       aT.SetScale(gp::Origin2d(), LengthFact);
86       aCurve2d->Transform(aT);
87       return aCurve2d;
88     }
89   }
90   else {
91     return aCurve2d;
92   }
93
94   if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Line)))
95   {
96     Handle(Geom2d_Line) aLine2d = Handle(Geom2d_Line)::DownCast(aCurve2d);
97     gp_Pnt2d myLoc = aLine2d->Location();
98     gp_Dir2d myDir = aLine2d->Direction();
99     gp_Pnt2d myNewLoc;
100     myNewLoc.SetCoord(myLoc.X()*uFact, myLoc.Y()*vFact);
101     gp_Dir2d myNewDir;
102     myNewDir.SetCoord(myDir.X()*uFact, myDir.Y()*vFact);
103     Handle(Geom2d_Line) myNewLine2d = Handle(Geom2d_Line)::DownCast(aLine2d->Copy());
104     myNewLine2d->SetLocation(myNewLoc);
105     myNewLine2d->SetDirection(myNewDir);
106     return myNewLine2d;
107   }
108   else if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Conic)))
109   {
110     if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Circle)) || aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Ellipse)))
111     {
112       Handle(Geom2d_BSplineCurve) aBSpline2d = Geom2dConvert::CurveToBSplineCurve(aCurve2d);
113       aCurve2d = aBSpline2d;
114     }
115     else if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Parabola)))
116     {
117 #ifdef OCCT_DEBUG
118       std::cout << "PCURVE of Parabola type in U or V Periodic Surface" << std::endl;
119       std::cout << "Parameters Not transformed to Degree" << std::endl;
120 #endif
121     }
122     else if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_Hyperbola)))
123     {
124 #ifdef OCCT_DEBUG
125       std::cout << "PCURVE of Hyperbola type in U or V Periodic Surface" << std::endl;
126       std::cout << "Parameters Not transformed to Degree" << std::endl;
127 #endif
128     }
129   }
130
131   // Compute affinity
132   tMatu.SetAffinity(gp::OY2d(), uFact);
133   tMatv.SetAffinity(gp::OX2d(), vFact);
134   if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_BoundedCurve)))
135   {
136     if (aCurve2d->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
137     {
138       Handle(Geom2d_BSplineCurve) aBSpline2d =
139         Handle(Geom2d_BSplineCurve)::DownCast(aCurve2d);
140       Handle(Geom2d_BSplineCurve) myNewBSpline2d =
141         Handle(Geom2d_BSplineCurve)::DownCast(aBSpline2d->Copy());
142       Standard_Integer nbPol = aBSpline2d->NbPoles();
143       for (Standard_Integer i = 1; i <= nbPol; i++)
144       {
145         pXY = aBSpline2d->Pole(i).XY();
146         tMatu.Transforms(pXY);
147         tMatv.Transforms(pXY);
148         Pt1.SetXY(pXY);
149         myNewBSpline2d->SetPole(i, Pt1);
150       }
151       return myNewBSpline2d;
152     }
153     else {
154 #ifdef OCCT_DEBUG
155       std::cout << "PCURVE of Other Types of Bounded Curve in U or V Periodic Surface" << std::endl;
156       std::cout << "Parameters Not transformed to Degree" << std::endl;
157 #endif
158     }
159   }
160   return aCurve2d;
161 }
162
163 // ============================================================================
164 // Method : DegreeToRadian
165 // Purpose: 1. Change definition of the pcurves according to LengthFactor                  
166 //          2. STEP cylinder, torus, cone and sphere are parametrized
167 //             from 0 to 360 degree
168 //             Then pcurves parameter have to be transformed 
169 //             from DEGREE to RADIAN
170 // ============================================================================
171 Handle(Geom2d_Curve) GeomConvert_Units::DegreeToRadian(
172   const Handle(Geom2d_Curve) & thePcurve,
173   const Handle(Geom_Surface) & theSurface,
174   const Standard_Real theLengthFactor,
175   const Standard_Real theFactorRadianDegree)
176 {
177   Handle(Geom2d_Curve)  aPcurve = Handle(Geom2d_Curve)::DownCast(thePcurve->Copy());
178   Standard_Real uFact = 1.;
179   Standard_Real vFact = 1.;
180   Standard_Real LengthFact = theLengthFactor;
181   Standard_Real AngleFact = theFactorRadianDegree;  // PI/180.;  pilotable
182
183   gp_Pnt2d    Pt1;
184   gp_XY       pXY;
185   gp_GTrsf2d  tMatu, tMatv;
186
187   // What to change ??
188
189   if (theSurface->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
190     theSurface->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)))
191   {
192     uFact = vFact = AngleFact;
193   }
194   else if (theSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)))
195   {
196     uFact = AngleFact;
197     vFact = LengthFact;
198   }
199   else if (theSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution)))
200   {
201     uFact = AngleFact;
202   }
203   else if (theSurface->IsKind(STANDARD_TYPE(Geom_ConicalSurface)))
204   {
205     Handle(Geom_ConicalSurface) conicS = Handle(Geom_ConicalSurface)::DownCast(theSurface);
206     Standard_Real semAng = conicS->SemiAngle();
207     uFact = AngleFact;
208     vFact = LengthFact / Cos(semAng);
209   }
210   else if (theSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
211   {
212     uFact = vFact = LengthFact;
213     if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_Circle)) || aPcurve->IsKind(STANDARD_TYPE(Geom2d_Ellipse)))
214     {
215       gp_Trsf2d aT;
216       aT.SetScale(gp::Origin2d(), LengthFact);
217       aPcurve->Transform(aT);
218       return aPcurve;
219     }
220   }
221   else
222   {
223     return aPcurve;
224   }
225
226   if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_Conic)))
227   {
228     if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_Circle)) || aPcurve->IsKind(STANDARD_TYPE(Geom2d_Ellipse)))
229     {
230       Handle(Geom2d_BSplineCurve) aBSpline2d = Geom2dConvert::CurveToBSplineCurve(aPcurve);
231       aPcurve = aBSpline2d;
232     }
233     else if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_Parabola)))
234     {
235 #ifdef OCCT_DEBUG
236       std::cout << "PCURVE of Parabola type" << std::endl;
237       std::cout << "Parameters Not Yet transformed according to LengthUnit" << std::endl;
238 #endif
239       return aPcurve;
240     }
241     else if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_Hyperbola)))
242     {
243 #ifdef OCCT_DEBUG
244       std::cout << "PCURVE of Hyperbola type" << std::endl;
245       std::cout << "Parameters Not Yet transformed according to LengthUnit" << std::endl;
246 #endif
247       return aPcurve;
248     }
249   }
250
251   // Compute affinity
252
253   tMatu.SetAffinity(gp::OY2d(), uFact);
254   tMatv.SetAffinity(gp::OX2d(), vFact);
255
256   if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_Line)))
257   {
258     Handle(Geom2d_Line) aLine2d = Handle(Geom2d_Line)::DownCast(aPcurve);
259
260     gp_Pnt2d myLoc = aLine2d->Location();
261     gp_Dir2d myDir = aLine2d->Direction();
262
263     gp_Pnt2d myNewLoc;
264     myNewLoc.SetCoord(myLoc.X()*uFact, myLoc.Y()*vFact);
265
266     gp_Dir2d myNewDir;
267     myNewDir.SetCoord(myDir.X()*uFact, myDir.Y()*vFact);
268
269     aLine2d->SetLocation(myNewLoc);
270     aLine2d->SetDirection(myNewDir);
271
272     aPcurve = aLine2d;
273   }
274   else if (aPcurve->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)))
275   {
276     Handle(Geom2d_BSplineCurve) aBSpline2d = Handle(Geom2d_BSplineCurve)::DownCast(aPcurve);
277
278     // transform the Poles of the BSplineCurve according to AngleFact and LengthFact
279
280     Standard_Integer nbPol = aBSpline2d->NbPoles();
281     for (Standard_Integer i = 1; i <= nbPol; i++)
282     {
283       pXY = aBSpline2d->Pole(i).XY();
284       tMatu.Transforms(pXY);
285       tMatv.Transforms(pXY);
286       Pt1.SetXY(pXY);
287       aBSpline2d->SetPole(i, Pt1);
288     }
289     aPcurve = aBSpline2d;
290   }
291   else
292   {
293 #ifdef OCCT_DEBUG
294     std::cout << "DegreeToRadian : Type " << aPcurve->DynamicType();
295     std::cout << " not yet implemented" << std::endl;
296 #endif
297   }
298   return aPcurve;
299 }
300
301 // ============================================================================
302 // Method : MirrorPCurve
303 // Purpose:
304 // ============================================================================
305 Handle(Geom2d_Curve) GeomConvert_Units::MirrorPCurve(const Handle(Geom2d_Curve) & theCurve)
306 {
307   Handle(Geom2d_Curve) theMirrored = Handle(Geom2d_Curve)::DownCast(theCurve->Copy());
308   gp_Trsf2d T;
309   gp_Pnt2d  Loc(0., 0.);
310   gp_Dir2d  Dir(1., 0.);
311   gp_Ax2d   ax2(Loc, Dir);
312   T.SetMirror(ax2);
313   theMirrored->Transform(T);
314   return theMirrored;
315 }