4b99933d45c0b660d5f10397ba13453983708595
[occt.git] / src / BRepTools / BRepTools_TrsfModification.cxx
1 // Created on: 1994-08-25
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <BRepTools_TrsfModification.ixx>
18
19 #include <Geom_Line.hxx>
20 #include <Geom_TrimmedCurve.hxx>
21 #include <Geom2d_TrimmedCurve.hxx>
22 #include <BRep_Tool.hxx>
23 #include <gp_GTrsf2d.hxx>
24 #include <gp_TrsfForm.hxx>
25
26 #include <gp.hxx>
27 #include <GeomLib.hxx>
28 #include <TopAbs.hxx>
29 #include <TopExp.hxx>
30
31 #include <TopoDS.hxx>
32 #include <Precision.hxx>
33 #include <GeomAdaptor_Surface.hxx>
34
35 //=======================================================================
36 //function : BRepTools_TrsfModification
37 //purpose  : 
38 //=======================================================================
39
40 BRepTools_TrsfModification::BRepTools_TrsfModification(const gp_Trsf& T) :
41 myTrsf(T)
42 {
43 }
44
45
46 //=======================================================================
47 //function : Trsf
48 //purpose  : 
49 //=======================================================================
50
51 gp_Trsf& BRepTools_TrsfModification::Trsf ()
52 {
53   return myTrsf;
54 }
55
56 //=======================================================================
57 //function : NewSurface
58 //purpose  : 
59 //=======================================================================
60
61 Standard_Boolean BRepTools_TrsfModification::NewSurface
62       (const TopoDS_Face& F,
63        Handle(Geom_Surface)& S,
64        TopLoc_Location& L,
65        Standard_Real& Tol,
66        Standard_Boolean& RevWires,
67        Standard_Boolean& RevFace)
68 {
69   S = BRep_Tool::Surface(F,L);
70   Tol = BRep_Tool::Tolerance(F);
71   Tol *= Abs(myTrsf.ScaleFactor());
72   RevWires = Standard_False;
73   RevFace = myTrsf.IsNegative();
74
75   gp_Trsf LT = L.Transformation();
76   LT.Invert();
77   LT.Multiply(myTrsf);
78   LT.Multiply(L.Transformation());
79
80   S = Handle(Geom_Surface)::DownCast(S->Transformed(LT));
81   
82   return Standard_True;
83 }
84
85
86 //=======================================================================
87 //function : NewCurve
88 //purpose  : 
89 //=======================================================================
90
91 Standard_Boolean BRepTools_TrsfModification::NewCurve
92     (const TopoDS_Edge& E, 
93      Handle(Geom_Curve)& C,
94      TopLoc_Location& L, 
95      Standard_Real& Tol)
96 {
97   Standard_Real f,l;
98   C = BRep_Tool::Curve(E,L,f,l);
99
100   Tol = BRep_Tool::Tolerance(E);
101   Tol *= Abs(myTrsf.ScaleFactor());
102
103   gp_Trsf LT = L.Transformation();
104   LT.Invert();
105   LT.Multiply(myTrsf);
106   LT.Multiply(L.Transformation());
107
108   if (!C.IsNull()) {
109     C = Handle(Geom_Curve)::DownCast(C->Transformed(LT));
110   }
111
112   return Standard_True;
113 }
114
115 //=======================================================================
116 //function : NewPoint
117 //purpose  : 
118 //=======================================================================
119
120 Standard_Boolean BRepTools_TrsfModification::NewPoint
121     (const TopoDS_Vertex& V, 
122      gp_Pnt& P, 
123      Standard_Real& Tol)
124 {
125   P = BRep_Tool::Pnt(V);
126   Tol = BRep_Tool::Tolerance(V);
127   Tol *= Abs(myTrsf.ScaleFactor());
128   P.Transform(myTrsf);
129
130   return Standard_True;
131 }
132
133 //=======================================================================
134 //function : NewCurve2d
135 //purpose  : 
136 //=======================================================================
137
138 Standard_Boolean BRepTools_TrsfModification::NewCurve2d
139     (const TopoDS_Edge& E, 
140      const TopoDS_Face& F, 
141      const TopoDS_Edge&, 
142      const TopoDS_Face&, 
143      Handle(Geom2d_Curve)& C,
144      Standard_Real& Tol)
145 {
146   TopLoc_Location loc;
147   Tol = BRep_Tool::Tolerance(E);
148   Standard_Real scale = myTrsf.ScaleFactor();
149   Tol *= Abs(scale);
150   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,loc);
151   GeomAdaptor_Surface GAsurf(S);
152   if (GAsurf.GetType() == GeomAbs_Plane)
153     return Standard_False;
154
155   Standard_Real f,l;
156   Handle(Geom2d_Curve) NewC = BRep_Tool::CurveOnSurface(E,F,f,l);
157   if (NewC.IsNull())
158     return Standard_False;
159   
160   Standard_Real newf,newl;
161
162   Handle(Standard_Type) TheType = NewC->DynamicType();
163     
164   if ( TheType == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
165     Handle(Geom2d_TrimmedCurve) TC = Handle(Geom2d_TrimmedCurve)::DownCast(NewC);
166     NewC = TC->BasisCurve();
167   }
168
169   Standard_Real fc = NewC->FirstParameter(), lc = NewC->LastParameter();
170
171   if(!NewC->IsPeriodic()) {
172     if(fc - f > Precision::PConfusion()) f = fc;
173     if(l - lc > Precision::PConfusion()) l = lc;
174   }
175
176   newf = f;
177   newl = l;
178   if (Abs(scale) != 1.) {
179     
180     NewC = new Geom2d_TrimmedCurve(NewC,f,l);
181     gp_GTrsf2d gtrsf = S->ParametricTransformation(myTrsf);
182     
183     if ( gtrsf.Form() != gp_Identity) {
184       NewC = GeomLib::GTransform(NewC,gtrsf);
185       if (NewC.IsNull()) {
186         Standard_DomainError::Raise("TrsfModification:Error in NewCurve2d");
187           return Standard_False;
188         }
189       newf = NewC->FirstParameter();
190       newl = NewC->LastParameter();
191     }
192   }
193   // il faut parfois recadrer les ranges 3d / 2d
194   TopoDS_Vertex V1, V2;
195   TopExp::Vertices(E,V1,V2);
196   TopoDS_Shape initEFOR = E.Oriented(TopAbs_FORWARD); // skl
197   TopoDS_Edge EFOR = TopoDS::Edge(initEFOR/*E.Oriented(TopAbs_FORWARD)*/); //skl
198   NewParameter(V1,EFOR,f,Tol);
199   NewParameter(V2,EFOR,l,Tol);
200   GeomLib::SameRange(Tol,NewC,newf,newl,f,l,C);
201   
202   return Standard_True;
203 }
204
205 //=======================================================================
206 //function : NewParameter
207 //purpose  : 
208 //=======================================================================
209
210 Standard_Boolean BRepTools_TrsfModification::NewParameter
211    (const TopoDS_Vertex& V, 
212     const TopoDS_Edge& E, 
213     Standard_Real& P, 
214     Standard_Real& Tol)
215 {
216   if (V.IsNull()) return Standard_False; // infinite edge may have Null vertex
217   
218   TopLoc_Location loc;
219   Tol = BRep_Tool::Tolerance(V);
220   Tol *= Abs(myTrsf.ScaleFactor());
221   P = BRep_Tool::Parameter(V,E);
222
223   Standard_Real f,l;
224
225   Handle(Geom_Curve) C = BRep_Tool::Curve(E,loc,f,l);
226   if (!C.IsNull()) {
227     P = C->TransformedParameter(P,myTrsf);
228   }
229
230   return Standard_True;
231 }
232
233 //=======================================================================
234 //function : Continuity
235 //purpose  : 
236 //=======================================================================
237
238 GeomAbs_Shape BRepTools_TrsfModification::Continuity
239   (const TopoDS_Edge& E,
240    const TopoDS_Face& F1,
241    const TopoDS_Face& F2,
242    const TopoDS_Edge&,
243    const TopoDS_Face&,
244    const TopoDS_Face&)
245 {
246   return BRep_Tool::Continuity(E,F1,F2);
247 }
248
249