0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_SplitCurve2d.cxx
1 // Created on: 1998-03-12
2 // Created by: Pierre BARRAS
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_SplitCurve2d.ixx>
18 #include <ShapeUpgrade.hxx>
19 #include <Geom2d_BSplineCurve.hxx>
20 #include <TColStd_ListIteratorOfListOfReal.hxx>
21 #include <TColStd_HSequenceOfReal.hxx>
22 #include <TColGeom2d_HArray1OfCurve.hxx>
23 #include <Geom2d_TrimmedCurve.hxx>
24 #include <Geom2d_OffsetCurve.hxx>
25 #include <Standard_ErrorHandler.hxx>
26 #include <Standard_Failure.hxx>
27 #include <ShapeExtend.hxx>
28 #include <Precision.hxx>
29 #include <Geom2d_BezierCurve.hxx>
30 #include <ShapeAnalysis_Curve.hxx>
31 #include <Geom2d_Curve.hxx>
32
33 //=======================================================================
34 //function : ShapeUpgrade_SplitCurve2d
35 //purpose  : 
36 //=======================================================================
37
38 ShapeUpgrade_SplitCurve2d::ShapeUpgrade_SplitCurve2d()
39 {
40 }
41
42 //=======================================================================
43 //function : Init
44 //purpose  : 
45 //=======================================================================
46
47  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C) 
48 {
49   Init (C, C->FirstParameter(), C->LastParameter());
50 }
51
52 //=======================================================================
53 //function : Init
54 //purpose  : 
55 //=======================================================================
56
57  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C,
58                                       const Standard_Real First,
59                                       const Standard_Real Last) 
60 {
61 //  if (ShapeUpgrade::Debug()) cout << "SplitCurve2d::Init"<<endl;
62   Handle(Geom2d_Curve) CopyOfC = Handle(Geom2d_Curve)::DownCast(C->Copy());
63   myCurve = CopyOfC;
64   
65   Standard_Real precision = Precision::PConfusion();
66   Standard_Real firstPar = First;
67   Standard_Real lastPar = Last;
68   Handle (Geom2d_Curve) aCurve = myCurve;
69   if(aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) 
70     aCurve=Handle(Geom2d_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_SplitCurve2d::Build(const Standard_Boolean Segment) 
109 {
110 //  if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve2d::Build"<<endl;
111   Standard_Real First =  mySplitValues->Value(1);
112   Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
113   //PrepareKnots();
114   if (mySplitValues->Length() > 2)
115     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
116   if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
117     Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
118     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
119     ShapeUpgrade_SplitCurve2d spc;
120     spc.Init(BasCurve,First,Last); 
121     spc.SetSplitValues(mySplitValues);
122     spc.Build(Segment);
123     myNbCurves = spc.GetCurves()->Length();
124     
125     myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
126     if (myNbCurves == 1) {
127       Handle(Geom2d_TrimmedCurve) NewTrimCurve = 
128         new Geom2d_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
129       myResultingCurves->SetValue(1,NewTrimCurve);
130     }
131     else 
132       myResultingCurves = spc.GetCurves();
133     myStatus |= spc.myStatus;
134     return;
135   }
136   else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
137     Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
138     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
139     Standard_Real Offset = tmp->Offset();
140     ShapeUpgrade_SplitCurve2d spc;
141     spc.Init(BasCurve,First,Last);
142     spc.SetSplitValues(mySplitValues);
143     spc.Build(Segment);
144     myNbCurves = spc.GetCurves()->Length();
145     myResultingCurves = new TColGeom2d_HArray1OfCurve (1,myNbCurves);
146     for(Standard_Integer i = 1; i <= myNbCurves; i++) {
147       Handle(Geom2d_OffsetCurve) NewOffsetCurve = new Geom2d_OffsetCurve(spc.GetCurves()->Value(i),Offset);
148       myResultingCurves->SetValue(i,NewOffsetCurve);
149     }
150     myStatus |= spc.myStatus;
151     return;
152   }
153
154   myNbCurves = mySplitValues->Length() -1;
155   myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
156   if(myNbCurves ==1) {
157     Standard_Boolean filled = Standard_True;
158     if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() && 
159          Abs ( myCurve->LastParameter()  - Last  ) < Precision::PConfusion() )
160       myResultingCurves->SetValue(1,myCurve);
161     
162     else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) &&
163              !myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
164       if(myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
165              myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve) )) {
166         Handle(Geom2d_Curve) theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
167         try {
168           OCC_CATCH_SIGNALS
169           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
170             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
171           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
172             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
173         }
174         catch (Standard_Failure) {
175 #ifdef OCCT_DEBUG
176           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
177           Standard_Failure::Caught()->Print(cout); cout << endl;
178 #endif
179           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
180         }
181         myResultingCurves->SetValue (1, theNewCurve);
182       
183       }
184       else {
185         Handle(Geom2d_TrimmedCurve) theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
186         myResultingCurves->SetValue (1, theNewCurve);
187       }
188     }
189     else filled = Standard_False;
190     if ( filled ) return;
191   }
192   
193   
194   if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve))) {
195     Handle(Geom2d_BSplineCurve) BsCurve = Handle(Geom2d_BSplineCurve)::DownCast(myCurve->Copy());
196     Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(), 
197     LastInd = BsCurve->LastUKnotIndex();
198     Standard_Integer j =  FirstInd;
199     for(Standard_Integer ii =1 ; ii <= mySplitValues->Length(); ii++) {
200       Standard_Real spval = mySplitValues->Value(ii);
201       for(; j <=LastInd;j++) {
202         if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
203         if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
204         mySplitValues->SetValue(ii,BsCurve->Knot(j));
205       }
206       if(j == LastInd) break;
207     }
208     
209   }
210   for (Standard_Integer i = 1; i <= myNbCurves; i++) {
211     // skl : in the next block I change "First","Last" to "Firstt","Lastt"
212     Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
213     Handle(Geom2d_Curve) theNewCurve;
214     if(Segment) {
215       // creates a copy of myCurve before to segment:
216       if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
217           myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) {
218         theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
219         try {
220           OCC_CATCH_SIGNALS
221           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
222             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
223           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
224             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
225           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
226         }
227         catch (Standard_Failure) {
228 #ifdef OCCT_DEBUG
229           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
230           Standard_Failure::Caught()->Print(cout); cout << endl;
231 #endif
232           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
233         }
234       }
235       else 
236         theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
237     }
238     myResultingCurves->SetValue (i, theNewCurve);
239   }
240 }
241
242 //=======================================================================
243 //function : GetCurves
244 //purpose  : 
245 //=======================================================================
246
247 const Handle(TColGeom2d_HArray1OfCurve)& ShapeUpgrade_SplitCurve2d::GetCurves() const
248 {
249   return myResultingCurves;
250 }