0026489: The class ShapeUpgrade_UnifySameDomain provides the results that are wrong...
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_SplitCurve2d.cxx
CommitLineData
b311480e 1// Created on: 1998-03-12
2// Created by: Pierre BARRAS
3// Copyright (c) 1998-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
42cf5bc1 17
18#include <Geom2d_BezierCurve.hxx>
7fd59977 19#include <Geom2d_BSplineCurve.hxx>
42cf5bc1 20#include <Geom2d_Curve.hxx>
7fd59977 21#include <Geom2d_OffsetCurve.hxx>
42cf5bc1 22#include <Geom2d_TrimmedCurve.hxx>
7fd59977 23#include <Precision.hxx>
7fd59977 24#include <ShapeAnalysis_Curve.hxx>
42cf5bc1 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>
7fd59977 34
35//=======================================================================
36//function : ShapeUpgrade_SplitCurve2d
37//purpose :
38//=======================================================================
7fd59977 39ShapeUpgrade_SplitCurve2d::ShapeUpgrade_SplitCurve2d()
40{
41}
42
43//=======================================================================
44//function : Init
45//purpose :
46//=======================================================================
47
48 void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C)
49{
50 Init (C, C->FirstParameter(), C->LastParameter());
51}
52
53//=======================================================================
54//function : Init
55//purpose :
56//=======================================================================
57
58 void ShapeUpgrade_SplitCurve2d::Init(const Handle(Geom2d_Curve)& C,
59 const Standard_Real First,
60 const Standard_Real Last)
61{
62// if (ShapeUpgrade::Debug()) cout << "SplitCurve2d::Init"<<endl;
63 Handle(Geom2d_Curve) CopyOfC = Handle(Geom2d_Curve)::DownCast(C->Copy());
64 myCurve = CopyOfC;
65
66 Standard_Real precision = Precision::PConfusion();
67 Standard_Real firstPar = First;
68 Standard_Real lastPar = Last;
69 Handle (Geom2d_Curve) aCurve = myCurve;
70 if(aCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve)))
71 aCurve=Handle(Geom2d_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
72 // 15.11.2002 PTV OCC966
73 if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
74 Standard_Real fP = aCurve->FirstParameter();
75 Standard_Real lP = aCurve->LastParameter();
76 if(Abs(firstPar-fP) < precision)
77 firstPar = fP;
78 if(Abs(lastPar-lP) < precision)
79 lastPar = lP;
80 if(firstPar < fP){
0797d9d3 81#ifdef OCCT_DEBUG
7fd59977 82 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
83#endif
84 firstPar = fP;
85 }
86 if(lastPar > lP){
0797d9d3 87#ifdef OCCT_DEBUG
7fd59977 88 cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
89#endif
90 lastPar = lP;
91 }
92 if( (lastPar-firstPar) < precision)
93 lastPar=firstPar+2*precision;
94 }
95
96 ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);
97
98 // first, we make a copy of C to prevent modification:
99// if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
100
101 myNbCurves = 1;
102}
103
104//=======================================================================
105//function : Build
106//purpose :
107//=======================================================================
108
109 void ShapeUpgrade_SplitCurve2d::Build(const Standard_Boolean Segment)
110{
111// if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve2d::Build"<<endl;
112 Standard_Real First = mySplitValues->Value(1);
113 Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
114 //PrepareKnots();
115 if (mySplitValues->Length() > 2)
116 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
117 if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
118 Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
119 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
120 ShapeUpgrade_SplitCurve2d spc;
121 spc.Init(BasCurve,First,Last);
122 spc.SetSplitValues(mySplitValues);
123 spc.Build(Segment);
124 myNbCurves = spc.GetCurves()->Length();
125
126 myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
127 if (myNbCurves == 1) {
128 Handle(Geom2d_TrimmedCurve) NewTrimCurve =
129 new Geom2d_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
130 myResultingCurves->SetValue(1,NewTrimCurve);
131 }
132 else
133 myResultingCurves = spc.GetCurves();
134 myStatus |= spc.myStatus;
135 return;
136 }
137 else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
138 Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
139 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
140 Standard_Real Offset = tmp->Offset();
141 ShapeUpgrade_SplitCurve2d spc;
142 spc.Init(BasCurve,First,Last);
143 spc.SetSplitValues(mySplitValues);
144 spc.Build(Segment);
145 myNbCurves = spc.GetCurves()->Length();
146 myResultingCurves = new TColGeom2d_HArray1OfCurve (1,myNbCurves);
147 for(Standard_Integer i = 1; i <= myNbCurves; i++) {
148 Handle(Geom2d_OffsetCurve) NewOffsetCurve = new Geom2d_OffsetCurve(spc.GetCurves()->Value(i),Offset);
149 myResultingCurves->SetValue(i,NewOffsetCurve);
150 }
151 myStatus |= spc.myStatus;
152 return;
153 }
154
155 myNbCurves = mySplitValues->Length() -1;
156 myResultingCurves=new TColGeom2d_HArray1OfCurve (1,myNbCurves);
157 if(myNbCurves ==1) {
158 Standard_Boolean filled = Standard_True;
159 if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() &&
160 Abs ( myCurve->LastParameter() - Last ) < Precision::PConfusion() )
161 myResultingCurves->SetValue(1,myCurve);
162
163 else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) &&
164 !myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
165 if(myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
166 myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve) )) {
167 Handle(Geom2d_Curve) theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
168 try {
169 OCC_CATCH_SIGNALS
170 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
171 Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
172 else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
173 Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
174 }
175 catch (Standard_Failure) {
0797d9d3 176#ifdef OCCT_DEBUG
7fd59977 177 cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment :";
178 Standard_Failure::Caught()->Print(cout); cout << endl;
179#endif
180 theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
181 }
182 myResultingCurves->SetValue (1, theNewCurve);
183
184 }
185 else {
186 Handle(Geom2d_TrimmedCurve) theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),First,Last);
187 myResultingCurves->SetValue (1, theNewCurve);
188 }
189 }
190 else filled = Standard_False;
191 if ( filled ) return;
192 }
193
194
195 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve))) {
196 Handle(Geom2d_BSplineCurve) BsCurve = Handle(Geom2d_BSplineCurve)::DownCast(myCurve->Copy());
197 Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(),
198 LastInd = BsCurve->LastUKnotIndex();
199 Standard_Integer j = FirstInd;
200 for(Standard_Integer ii =1 ; ii <= mySplitValues->Length(); ii++) {
201 Standard_Real spval = mySplitValues->Value(ii);
202 for(; j <=LastInd;j++) {
203 if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
204 if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
205 mySplitValues->SetValue(ii,BsCurve->Knot(j));
206 }
207 if(j == LastInd) break;
208 }
209
210 }
211 for (Standard_Integer i = 1; i <= myNbCurves; i++) {
212 // skl : in the next block I change "First","Last" to "Firstt","Lastt"
213 Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
214 Handle(Geom2d_Curve) theNewCurve;
215 if(Segment) {
216 // creates a copy of myCurve before to segment:
217 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)) ||
218 myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve))) {
219 theNewCurve = Handle(Geom2d_Curve)::DownCast(myCurve->Copy());
220 try {
221 OCC_CATCH_SIGNALS
222 if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BSplineCurve)))
223 Handle(Geom2d_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
224 else if (myCurve->IsKind (STANDARD_TYPE (Geom2d_BezierCurve)))
225 Handle(Geom2d_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
226 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
227 }
228 catch (Standard_Failure) {
0797d9d3 229#ifdef OCCT_DEBUG
7fd59977 230 cout << "Warning: ShapeUpgrade_Split2dCurve::Build(): Exception in Segment :";
231 Standard_Failure::Caught()->Print(cout); cout << endl;
232#endif
233 theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
234 }
235 }
236 else
237 theNewCurve = new Geom2d_TrimmedCurve(Handle(Geom2d_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);
238 }
239 myResultingCurves->SetValue (i, theNewCurve);
240 }
241}
242
243//=======================================================================
244//function : GetCurves
245//purpose :
246//=======================================================================
247
248const Handle(TColGeom2d_HArray1OfCurve)& ShapeUpgrade_SplitCurve2d::GetCurves() const
249{
250 return myResultingCurves;
251}