0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_SplitCurve3d.cxx
1 // Created on: 1998-03-12
2 // Created by: Roman LYGIN
3 // Copyright (c) 1998-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 <ShapeUpgrade_SplitCurve3d.ixx>
18 #include <Geom_BSplineCurve.hxx>
19 #include <Precision.hxx>
20 #include <ShapeUpgrade.hxx>
21 #include <TColStd_ListIteratorOfListOfReal.hxx>
22 #include <TColStd_HSequenceOfReal.hxx>
23 #include <TColGeom_HArray1OfCurve.hxx>
24 #include <Geom_TrimmedCurve.hxx>
25 #include <Geom_OffsetCurve.hxx>
26 #include <Standard_ErrorHandler.hxx>
27 #include <Standard_Failure.hxx>
28 #include <ShapeExtend.hxx>
29 #include <Precision.hxx>
30 #include <Geom_BezierCurve.hxx>
31 #include <ShapeAnalysis_Curve.hxx>
32 #include <Geom_Curve.hxx>
33
34 //=======================================================================
35 //function : ShapeUpgrade_SplitCurve3d
36 //purpose  : 
37 //=======================================================================
38
39 ShapeUpgrade_SplitCurve3d::ShapeUpgrade_SplitCurve3d()
40 {
41 }
42
43 //=======================================================================
44 //function : Init
45 //purpose  : 
46 //=======================================================================
47
48  void ShapeUpgrade_SplitCurve3d::Init(const Handle(Geom_Curve)& C) 
49 {
50   Init (C, C->FirstParameter(), C->LastParameter());
51 }
52
53 //=======================================================================
54 //function : Init
55 //purpose  : 
56 //=======================================================================
57
58  void ShapeUpgrade_SplitCurve3d::Init(const Handle(Geom_Curve)& C,
59                                       const Standard_Real First,
60                                       const Standard_Real Last) 
61 {
62 //  if (ShapeUpgrade::Debug()) cout << "SplitCurve3d::Init"<<endl;
63   Handle(Geom_Curve) CopyOfC = Handle(Geom_Curve)::DownCast(C->Copy());
64   myCurve = CopyOfC;
65   Standard_Real precision = Precision::PConfusion();
66   Standard_Real firstPar = First;
67   Standard_Real lastPar = Last;
68   Handle (Geom_Curve) aCurve = myCurve;
69   if(aCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) 
70     aCurve=Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
71   // 15.11.2002 PTV OCC966
72   if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
73     Standard_Real fP = aCurve->FirstParameter();
74     Standard_Real lP  = aCurve->LastParameter();
75     if(Abs(firstPar-fP) < precision)
76        firstPar = fP;
77     if(Abs(lastPar-lP) < precision)
78       lastPar = lP;
79     if(firstPar < fP){
80 #ifdef OCCT_DEBUG
81       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
82 #endif
83       firstPar = fP;
84     }
85     if(lastPar > lP){
86 #ifdef OCCT_DEBUG
87       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
88 #endif
89       lastPar = lP;
90     }
91     if( (lastPar-firstPar) < precision)
92       lastPar=firstPar+2*precision;
93   }
94   
95   ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
96
97   // first, we make a copy of C to prevent modification:
98 //  if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
99   
100   myNbCurves = 1;
101 }
102
103 //=======================================================================
104 //function : Build
105 //purpose  : 
106 //=======================================================================
107
108  void ShapeUpgrade_SplitCurve3d::Build(const Standard_Boolean Segment) 
109 {
110 //  if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve3d::Build"<<endl;
111   Standard_Real First =  mySplitValues->Value(1);
112   Standard_Real Last =  mySplitValues->Value(mySplitValues->Length());
113     
114   if (mySplitValues->Length() >2)
115     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
116   if (myCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
117     Handle(Geom_TrimmedCurve) tmp = Handle(Geom_TrimmedCurve)::DownCast (myCurve);
118     Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
119     ShapeUpgrade_SplitCurve3d spc;
120     spc.Init(BasCurve,First,Last);
121     spc.SetSplitValues(mySplitValues);
122     spc.Build(Segment);
123     myNbCurves = spc.GetCurves()->Length();
124     myResultingCurves=new TColGeom_HArray1OfCurve (1,myNbCurves);
125     if(myNbCurves == 1) {
126       Handle(Geom_TrimmedCurve) NewTrimCurve = 
127         new Geom_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
128       myResultingCurves->SetValue(1,NewTrimCurve);
129     }
130     else 
131       myResultingCurves = spc.GetCurves();   
132     myStatus |= spc.myStatus;
133     return;
134   }
135   else if (myCurve->IsKind(STANDARD_TYPE(Geom_OffsetCurve))) {
136     Handle(Geom_OffsetCurve) tmp = Handle(Geom_OffsetCurve)::DownCast (myCurve);
137     Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
138     Standard_Real Offset = tmp->Offset();
139     gp_Dir Direct = tmp->Direction();
140     ShapeUpgrade_SplitCurve3d spc;
141     spc.Init(BasCurve,First,Last);
142     spc.SetSplitValues(mySplitValues);  
143     spc.Build(Segment);
144     myNbCurves = spc.GetCurves()->Length();
145     myResultingCurves = new TColGeom_HArray1OfCurve (1,myNbCurves);
146     for(Standard_Integer i = 1; i <= myNbCurves; i++) {
147       Handle(Geom_OffsetCurve) NewOffsetCurve = 
148         new Geom_OffsetCurve(spc.GetCurves()->Value(i),Offset,Direct);
149       myResultingCurves->SetValue(i,NewOffsetCurve);
150     }
151     myStatus |= spc.myStatus;
152     return;
153   }
154   
155   //pdn fix on BuildCurve 3d
156   // 15.11.2002 PTV OCC966
157   if(!ShapeAnalysis_Curve::IsPeriodic(myCurve)) {
158     //pdn exceptons only on non periodic curves
159     Standard_Real precision = Precision::PConfusion();
160     Standard_Real firstPar = myCurve->FirstParameter();
161     Standard_Real lastPar  = myCurve->LastParameter();
162     if(Abs(First-firstPar) < precision)
163       First = firstPar;
164     if(Abs(Last-lastPar) < precision)
165       Last = lastPar;
166     if(First < firstPar){
167 #ifdef OCCT_DEBUG
168       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
169 #endif
170       First = firstPar;
171       mySplitValues->SetValue(1,First);
172     }
173     if(Last > lastPar){
174 #ifdef OCCT_DEBUG
175       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
176 #endif
177       Last = lastPar;
178       mySplitValues->SetValue(mySplitValues->Length(),Last);
179     }
180   }
181
182   myNbCurves = mySplitValues->Length() -1;
183   myResultingCurves = new TColGeom_HArray1OfCurve (1, myNbCurves);
184   if (myNbCurves == 1) {
185     Standard_Boolean filled = Standard_True;
186     if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() && 
187          Abs ( myCurve->LastParameter()  - Last  ) < Precision::PConfusion() )
188       myResultingCurves->SetValue(1,myCurve);
189     
190     else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) &&
191              !myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
192 /*      if(myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) ||
193              myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve) )) {
194         Handle(Geom_Curve) theNewCurve = Handle(Geom_Curve)::DownCast(myCurve->Copy());
195         try {  
196           OCC_CATCH_SIGNALS
197           if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) 
198             Handle(Geom_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
199           else if (myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve)))
200             Handle(Geom_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
201         }
202       catch (Standard_Failure) {
203 #ifdef OCCT_DEBUG
204           cout << "Warning: ShapeUpgrade_Split3dCurve::Build(): Exception in Segment      :";
205           Standard_Failure::Caught()->Print(cout); cout << endl;
206 #endif
207           theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),First,Last);  
208         }
209         myResultingCurves->SetValue (1, theNewCurve);
210       }
211       else {*/
212         Handle(Geom_TrimmedCurve) theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),First,Last);
213         myResultingCurves->SetValue (1, theNewCurve);
214      // }
215     }
216     else filled = Standard_False;
217     if ( filled ) return;
218   }
219   if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) {
220     Handle(Geom_BSplineCurve) BsCurve = Handle(Geom_BSplineCurve)::DownCast(myCurve->Copy());
221     Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(), 
222     LastInd = BsCurve->LastUKnotIndex();
223     Standard_Integer j =  FirstInd;
224     for(Standard_Integer ii =1; ii <= mySplitValues->Length(); ii++) {
225       Standard_Real spval = mySplitValues->Value(ii);
226       for(; j <=LastInd;j++) {
227         if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
228         if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
229         mySplitValues->SetValue(ii,BsCurve->Knot(j));
230       }
231       if(j == LastInd) break;
232     }
233   }
234   
235   for (Standard_Integer i = 1; i <= myNbCurves; i++) {
236     // skl : in the next block I change "First","Last" to "Firstt","Lastt"
237     Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
238     Handle(Geom_Curve) theNewCurve;
239     if(Segment) {
240       if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) ||
241           myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve))) {
242         theNewCurve = Handle(Geom_Curve)::DownCast(myCurve->Copy());
243         try {  
244           OCC_CATCH_SIGNALS
245           if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) 
246             Handle(Geom_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
247           else if (myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve)))
248             Handle(Geom_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
249           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
250         }
251         catch (Standard_Failure) {
252 #ifdef OCCT_DEBUG
253           cout << "Warning: ShapeUpgrade_Split3dCurve::Build(): Exception in Segment      :";
254           Standard_Failure::Caught()->Print(cout); cout << endl;
255 #endif
256           theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);  
257         }
258       }
259       else theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);  
260     }
261     myResultingCurves->SetValue (i, theNewCurve);
262   }
263
264 }
265
266 //=======================================================================
267 //function : GetCurves
268 //purpose  : 
269 //=======================================================================
270
271 const Handle(TColGeom_HArray1OfCurve)& ShapeUpgrade_SplitCurve3d::GetCurves() const
272 {
273   return myResultingCurves;
274 }