0023301: Comparing variable to itself in ShapeUpgrade_WireDivide.cxx
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <ShapeUpgrade_SplitCurve2d.ixx>
23 #include <ShapeUpgrade.hxx>
24 #include <Geom2d_BSplineCurve.hxx>
25 #include <TColStd_ListIteratorOfListOfReal.hxx>
26 #include <TColStd_HSequenceOfReal.hxx>
27 #include <TColGeom2d_HArray1OfCurve.hxx>
28 #include <Geom2d_TrimmedCurve.hxx>
29 #include <Geom2d_OffsetCurve.hxx>
30 #include <Standard_ErrorHandler.hxx>
31 #include <Standard_Failure.hxx>
32 #include <ShapeExtend.hxx>
33 #include <Precision.hxx>
34 #include <Geom2d_BezierCurve.hxx>
35 #include <ShapeAnalysis_Curve.hxx>
36
37 //=======================================================================
38 //function : ShapeUpgrade_SplitCurve2d
39 //purpose  : 
40 //=======================================================================
41
42 ShapeUpgrade_SplitCurve2d::ShapeUpgrade_SplitCurve2d()
43 {
44 }
45
46 //=======================================================================
47 //function : Init
48 //purpose  : 
49 //=======================================================================
50
51  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C) 
52 {
53   Init (C, C->FirstParameter(), C->LastParameter());
54 }
55
56 //=======================================================================
57 //function : Init
58 //purpose  : 
59 //=======================================================================
60
61  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C,
62                                       const Standard_Real First,
63                                       const Standard_Real Last) 
64 {
65 //  if (ShapeUpgrade::Debug()) cout << "SplitCurve2d::Init"<<endl;
66   Handle(Geom2d_Curve) CopyOfC = Handle(Geom2d_Curve)::DownCast(C->Copy());
67   myCurve = CopyOfC;
68   
69   Standard_Real precision = Precision::PConfusion();
70   Standard_Real firstPar = First;
71   Standard_Real lastPar = Last;
72   Handle (Geom2d_Curve) aCurve = myCurve;
73   if(aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) 
74     aCurve=Handle(Geom2d_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
75   // 15.11.2002 PTV OCC966
76   if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
77     Standard_Real fP = aCurve->FirstParameter();
78     Standard_Real lP  = aCurve->LastParameter();
79     if(Abs(firstPar-fP) < precision)
80        firstPar = fP;
81     if(Abs(lastPar-lP) < precision)
82       lastPar = lP;
83     if(firstPar < fP){
84 #ifdef DEB
85       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
86 #endif
87       firstPar = fP;
88     }
89     if(lastPar > lP){
90 #ifdef DEB
91       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
92 #endif
93       lastPar = lP;
94     }
95     if( (lastPar-firstPar) < precision)
96       lastPar=firstPar+2*precision;
97   }
98   
99   ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
100
101   // first, we make a copy of C to prevent modification:
102 //  if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
103   
104   myNbCurves = 1;
105 }
106
107 //=======================================================================
108 //function : Build
109 //purpose  : 
110 //=======================================================================
111
112  void ShapeUpgrade_SplitCurve2d::Build(const Standard_Boolean Segment) 
113 {
114 //  if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve2d::Build"<<endl;
115   Standard_Real First =  mySplitValues->Value(1);
116   Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
117   //PrepareKnots();
118   if (mySplitValues->Length() > 2)
119     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
120   if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
121     Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
122     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
123     ShapeUpgrade_SplitCurve2d spc;
124     spc.Init(BasCurve,First,Last); 
125     spc.SetSplitValues(mySplitValues);
126     spc.Build(Segment);
127     myNbCurves = spc.GetCurves()->Length();
128     
129     myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
130     if (myNbCurves == 1) {
131       Handle(Geom2d_TrimmedCurve) NewTrimCurve = 
132         new Geom2d_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
133       myResultingCurves->SetValue(1,NewTrimCurve);
134     }
135     else 
136       myResultingCurves = spc.GetCurves();
137     myStatus |= spc.myStatus;
138     return;
139   }
140   else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
141     Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
142     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
143     Standard_Real Offset = tmp->Offset();
144     ShapeUpgrade_SplitCurve2d spc;
145     spc.Init(BasCurve,First,Last);
146     spc.SetSplitValues(mySplitValues);
147     spc.Build(Segment);
148     myNbCurves = spc.GetCurves()->Length();
149     myResultingCurves = new TColGeom2d_HArray1OfCurve (1,myNbCurves);
150     for(Standard_Integer i = 1; i <= myNbCurves; i++) {
151       Handle(Geom2d_OffsetCurve) NewOffsetCurve = new Geom2d_OffsetCurve(spc.GetCurves()->Value(i),Offset);
152       myResultingCurves->SetValue(i,NewOffsetCurve);
153     }
154     myStatus |= spc.myStatus;
155     return;
156   }
157
158   myNbCurves = mySplitValues->Length() -1;
159   myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
160   if(myNbCurves ==1) {
161     Standard_Boolean filled = Standard_True;
162     if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() && 
163          Abs ( myCurve->LastParameter()  - Last  ) < Precision::PConfusion() )
164       myResultingCurves->SetValue(1,myCurve);
165     
166     else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) &&
167              !myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
168       if(myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
169              myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve) )) {
170         Handle(Geom2d_Curve) theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
171         try {
172           OCC_CATCH_SIGNALS
173           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
174             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
175           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
176             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
177         }
178         catch (Standard_Failure) {
179 #ifdef DEB 
180           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
181           Standard_Failure::Caught()->Print(cout); cout << endl;
182 #endif
183           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
184         }
185         myResultingCurves->SetValue (1, theNewCurve);
186       
187       }
188       else {
189         Handle(Geom2d_TrimmedCurve) theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
190         myResultingCurves->SetValue (1, theNewCurve);
191       }
192     }
193     else filled = Standard_False;
194     if ( filled ) return;
195   }
196   
197   
198   if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve))) {
199     Handle(Geom2d_BSplineCurve) BsCurve = Handle(Geom2d_BSplineCurve)::DownCast(myCurve->Copy());
200     Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(), 
201     LastInd = BsCurve->LastUKnotIndex();
202     Standard_Integer j =  FirstInd;
203     for(Standard_Integer ii =1 ; ii <= mySplitValues->Length(); ii++) {
204       Standard_Real spval = mySplitValues->Value(ii);
205       for(; j <=LastInd;j++) {
206         if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
207         if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
208         mySplitValues->SetValue(ii,BsCurve->Knot(j));
209       }
210       if(j == LastInd) break;
211     }
212     
213   }
214   for (Standard_Integer i = 1; i <= myNbCurves; i++) {
215     // skl : in the next block I change "First","Last" to "Firstt","Lastt"
216     Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
217     Handle(Geom2d_Curve) theNewCurve;
218     if(Segment) {
219       // creates a copy of myCurve before to segment:
220       if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
221           myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) {
222         theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
223         try {
224           OCC_CATCH_SIGNALS
225           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
226             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
227           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
228             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
229           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
230         }
231         catch (Standard_Failure) {
232 #ifdef DEB 
233           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
234           Standard_Failure::Caught()->Print(cout); cout << endl;
235 #endif
236           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
237         }
238       }
239       else 
240         theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
241     }
242     myResultingCurves->SetValue (i, theNewCurve);
243   }
244 }
245
246 //=======================================================================
247 //function : GetCurves
248 //purpose  : 
249 //=======================================================================
250
251 const Handle(TColGeom2d_HArray1OfCurve)& ShapeUpgrade_SplitCurve2d::GetCurves() const
252 {
253   return myResultingCurves;
254 }