0025266: Debug statements in the source are getting flushed on to the console
[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
32 //=======================================================================
33 //function : ShapeUpgrade_SplitCurve2d
34 //purpose  : 
35 //=======================================================================
36
37 ShapeUpgrade_SplitCurve2d::ShapeUpgrade_SplitCurve2d()
38 {
39 }
40
41 //=======================================================================
42 //function : Init
43 //purpose  : 
44 //=======================================================================
45
46  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C) 
47 {
48   Init (C, C->FirstParameter(), C->LastParameter());
49 }
50
51 //=======================================================================
52 //function : Init
53 //purpose  : 
54 //=======================================================================
55
56  void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C,
57                                       const Standard_Real First,
58                                       const Standard_Real Last) 
59 {
60 //  if (ShapeUpgrade::Debug()) cout << "SplitCurve2d::Init"<<endl;
61   Handle(Geom2d_Curve) CopyOfC = Handle(Geom2d_Curve)::DownCast(C->Copy());
62   myCurve = CopyOfC;
63   
64   Standard_Real precision = Precision::PConfusion();
65   Standard_Real firstPar = First;
66   Standard_Real lastPar = Last;
67   Handle (Geom2d_Curve) aCurve = myCurve;
68   if(aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) 
69     aCurve=Handle(Geom2d_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
70   // 15.11.2002 PTV OCC966
71   if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
72     Standard_Real fP = aCurve->FirstParameter();
73     Standard_Real lP  = aCurve->LastParameter();
74     if(Abs(firstPar-fP) < precision)
75        firstPar = fP;
76     if(Abs(lastPar-lP) < precision)
77       lastPar = lP;
78     if(firstPar < fP){
79 #ifdef SHAPEUPGRADE_DEB
80       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
81 #endif
82       firstPar = fP;
83     }
84     if(lastPar > lP){
85 #ifdef SHAPEUPGRADE_DEB
86       cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
87 #endif
88       lastPar = lP;
89     }
90     if( (lastPar-firstPar) < precision)
91       lastPar=firstPar+2*precision;
92   }
93   
94   ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
95
96   // first, we make a copy of C to prevent modification:
97 //  if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
98   
99   myNbCurves = 1;
100 }
101
102 //=======================================================================
103 //function : Build
104 //purpose  : 
105 //=======================================================================
106
107  void ShapeUpgrade_SplitCurve2d::Build(const Standard_Boolean Segment) 
108 {
109 //  if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve2d::Build"<<endl;
110   Standard_Real First =  mySplitValues->Value(1);
111   Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
112   //PrepareKnots();
113   if (mySplitValues->Length() > 2)
114     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
115   if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
116     Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
117     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
118     ShapeUpgrade_SplitCurve2d spc;
119     spc.Init(BasCurve,First,Last); 
120     spc.SetSplitValues(mySplitValues);
121     spc.Build(Segment);
122     myNbCurves = spc.GetCurves()->Length();
123     
124     myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
125     if (myNbCurves == 1) {
126       Handle(Geom2d_TrimmedCurve) NewTrimCurve = 
127         new Geom2d_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(Geom2d_OffsetCurve))) {
136     Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
137     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
138     Standard_Real Offset = tmp->Offset();
139     ShapeUpgrade_SplitCurve2d spc;
140     spc.Init(BasCurve,First,Last);
141     spc.SetSplitValues(mySplitValues);
142     spc.Build(Segment);
143     myNbCurves = spc.GetCurves()->Length();
144     myResultingCurves = new TColGeom2d_HArray1OfCurve (1,myNbCurves);
145     for(Standard_Integer i = 1; i <= myNbCurves; i++) {
146       Handle(Geom2d_OffsetCurve) NewOffsetCurve = new Geom2d_OffsetCurve(spc.GetCurves()->Value(i),Offset);
147       myResultingCurves->SetValue(i,NewOffsetCurve);
148     }
149     myStatus |= spc.myStatus;
150     return;
151   }
152
153   myNbCurves = mySplitValues->Length() -1;
154   myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
155   if(myNbCurves ==1) {
156     Standard_Boolean filled = Standard_True;
157     if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() && 
158          Abs ( myCurve->LastParameter()  - Last  ) < Precision::PConfusion() )
159       myResultingCurves->SetValue(1,myCurve);
160     
161     else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) &&
162              !myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
163       if(myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
164              myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve) )) {
165         Handle(Geom2d_Curve) theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
166         try {
167           OCC_CATCH_SIGNALS
168           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
169             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
170           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
171             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
172         }
173         catch (Standard_Failure) {
174 #ifdef SHAPEUPGRADE_DEB
175           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
176           Standard_Failure::Caught()->Print(cout); cout << endl;
177 #endif
178           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
179         }
180         myResultingCurves->SetValue (1, theNewCurve);
181       
182       }
183       else {
184         Handle(Geom2d_TrimmedCurve) theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
185         myResultingCurves->SetValue (1, theNewCurve);
186       }
187     }
188     else filled = Standard_False;
189     if ( filled ) return;
190   }
191   
192   
193   if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve))) {
194     Handle(Geom2d_BSplineCurve) BsCurve = Handle(Geom2d_BSplineCurve)::DownCast(myCurve->Copy());
195     Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(), 
196     LastInd = BsCurve->LastUKnotIndex();
197     Standard_Integer j =  FirstInd;
198     for(Standard_Integer ii =1 ; ii <= mySplitValues->Length(); ii++) {
199       Standard_Real spval = mySplitValues->Value(ii);
200       for(; j <=LastInd;j++) {
201         if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
202         if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
203         mySplitValues->SetValue(ii,BsCurve->Knot(j));
204       }
205       if(j == LastInd) break;
206     }
207     
208   }
209   for (Standard_Integer i = 1; i <= myNbCurves; i++) {
210     // skl : in the next block I change "First","Last" to "Firstt","Lastt"
211     Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
212     Handle(Geom2d_Curve) theNewCurve;
213     if(Segment) {
214       // creates a copy of myCurve before to segment:
215       if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
216           myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) {
217         theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
218         try {
219           OCC_CATCH_SIGNALS
220           if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
221             Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
222           else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
223             Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
224           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
225         }
226         catch (Standard_Failure) {
227 #ifdef SHAPEUPGRADE_DEB
228           cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment      :";
229           Standard_Failure::Caught()->Print(cout); cout << endl;
230 #endif
231           theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
232         }
233       }
234       else 
235         theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
236     }
237     myResultingCurves->SetValue (i, theNewCurve);
238   }
239 }
240
241 //=======================================================================
242 //function : GetCurves
243 //purpose  : 
244 //=======================================================================
245
246 const Handle(TColGeom2d_HArray1OfCurve)& ShapeUpgrade_SplitCurve2d::GetCurves() const
247 {
248   return myResultingCurves;
249 }