Warnings on vc14 were eliminated
[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
18 #include <Geom2d_BezierCurve.hxx>
19 #include <Geom2d_BSplineCurve.hxx>
20 #include <Geom2d_Curve.hxx>
21 #include <Geom2d_OffsetCurve.hxx>
22 #include <Geom2d_TrimmedCurve.hxx>
23 #include <Precision.hxx>
24 #include <ShapeAnalysis_Curve.hxx>
25 #include <ShapeExtend.hxx>
26 #include <ShapeUpgrade.hxx>
27 #include <ShapeUpgrade_SplitCurve2d.hxx>
28 #include <Standard_ErrorHandler.hxx>
29 #include <Standard_Failure.hxx>
30 #include <Standard_Type.hxx>
31 #include <TColGeom2d_HArray1OfCurve.hxx>
32 #include <TColStd_HSequenceOfReal.hxx>
33 #include <TColStd_ListIteratorOfListOfReal.hxx>
34
35 IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_SplitCurve2d,ShapeUpgrade_SplitCurve)
36
37 //=======================================================================
38 //function : ShapeUpgrade_SplitCurve2d
39 //purpose  : 
40 //=======================================================================
41 ShapeUpgrade_SplitCurve2d::ShapeUpgrade_SplitCurve2d()
42 {
43 }
44
45 //=======================================================================
46 //function : Init
47 //purpose  : 
48 //=======================================================================
49
50  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C) 
51 {
52   Init (C, C->FirstParameter(), C->LastParameter());
53 }
54
55 //=======================================================================
56 //function : Init
57 //purpose  : 
58 //=======================================================================
59
60  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C,
61                                       const Standard_Real First,
62                                       const Standard_Real Last) 
63 {
64 //  if (ShapeUpgrade::Debug()) cout << "SplitCurve2d::Init"<<endl;
65   Handle(Geom2d_Curve) CopyOfC = Handle(Geom2d_Curve)::DownCast(C->Copy());
66   myCurve = CopyOfC;
67   
68   Standard_Real precision = Precision::PConfusion();
69   Standard_Real firstPar = First;
70   Standard_Real lastPar = Last;
71   Handle (Geom2d_Curve) aCurve = myCurve;
72   if(aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) 
73     aCurve=Handle(Geom2d_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
74   // 15.11.2002 PTV OCC966
75   if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
76     Standard_Real fP = aCurve->FirstParameter();
77     Standard_Real lP  = aCurve->LastParameter();
78     if(Abs(firstPar-fP) < precision)
79        firstPar = fP;
80     if(Abs(lastPar-lP) < precision)
81       lastPar = lP;
82     if(firstPar < fP){
83 #ifdef OCCT_DEBUG
84       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
85 #endif
86       firstPar = fP;
87     }
88     if(lastPar > lP){
89 #ifdef OCCT_DEBUG
90       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
91 #endif
92       lastPar = lP;
93     }
94     if( (lastPar-firstPar) < precision)
95       lastPar=firstPar+2*precision;
96   }
97   
98   ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
99
100   // first, we make a copy of C to prevent modification:
101 //  if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
102   
103   myNbCurves = 1;
104 }
105
106 //=======================================================================
107 //function : Build
108 //purpose  : 
109 //=======================================================================
110
111  void ShapeUpgrade_SplitCurve2d::Build(const Standard_Boolean Segment) 
112 {
113 //  if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve2d::Build"<<endl;
114   Standard_Real First =  mySplitValues->Value(1);
115   Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
116   //PrepareKnots();
117   if (mySplitValues->Length() > 2)
118     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
119   if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
120     Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
121     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
122     ShapeUpgrade_SplitCurve2d spc;
123     spc.Init(BasCurve,First,Last); 
124     spc.SetSplitValues(mySplitValues);
125     spc.Build(Segment);
126     myNbCurves = spc.GetCurves()->Length();
127     
128     myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
129     if (myNbCurves == 1) {
130       Handle(Geom2d_TrimmedCurve) NewTrimCurve = 
131         new Geom2d_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
132       myResultingCurves->SetValue(1,NewTrimCurve);
133     }
134     else 
135       myResultingCurves = spc.GetCurves();
136     myStatus |= spc.myStatus;
137     return;
138   }
139   else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
140     Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
141     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
142     Standard_Real Offset = tmp->Offset();
143     ShapeUpgrade_SplitCurve2d spc;
144     spc.Init(BasCurve,First,Last);
145     spc.SetSplitValues(mySplitValues);
146     spc.Build(Segment);
147     myNbCurves = spc.GetCurves()->Length();
148     myResultingCurves = new TColGeom2d_HArray1OfCurve (1,myNbCurves);
149     for(Standard_Integer i = 1; i <= myNbCurves; i++) {
150       Handle(Geom2d_OffsetCurve) NewOffsetCurve = new Geom2d_OffsetCurve(spc.GetCurves()->Value(i),Offset);
151       myResultingCurves->SetValue(i,NewOffsetCurve);
152     }
153     myStatus |= spc.myStatus;
154     return;
155   }
156
157   myNbCurves = mySplitValues->Length() -1;
158   myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
159   if(myNbCurves ==1) {
160     Standard_Boolean filled = Standard_True;
161     if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() && 
162          Abs ( myCurve->LastParameter()  - Last  ) < Precision::PConfusion() )
163       myResultingCurves->SetValue(1,myCurve);
164     
165     else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) &&
166              !myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
167       if(myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
168              myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve) )) {
169         Handle(Geom2d_Curve) theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
170         try {
171           OCC_CATCH_SIGNALS
172           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
173             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
174           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
175             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
176         }
177         catch (Standard_Failure const& anException) {
178 #ifdef OCCT_DEBUG
179           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
180           anException.Print(cout); cout << endl;
181 #endif
182           (void)anException;
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 const& anException) {
232 #ifdef OCCT_DEBUG
233           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
234           anException.Print(cout); cout << endl;
235 #endif
236           (void)anException;
237           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
238         }
239       }
240       else 
241         theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
242     }
243     myResultingCurves->SetValue (i, theNewCurve);
244   }
245 }
246
247 //=======================================================================
248 //function : GetCurves
249 //purpose  : 
250 //=======================================================================
251
252 const Handle(TColGeom2d_HArray1OfCurve)& ShapeUpgrade_SplitCurve2d::GetCurves() const
253 {
254   return myResultingCurves;
255 }