1 // Created on: 1999-04-14
2 // Created by: Roman LYGIN
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <ShapeUpgrade_SplitCurve2dContinuity.ixx>
18 #include <Standard_ErrorHandler.hxx>
19 #include <Standard_Failure.hxx>
20 #include <Geom2d_BSplineCurve.hxx>
21 #include <Precision.hxx>
22 #include <ShapeUpgrade.hxx>
23 #include <Geom2d_TrimmedCurve.hxx>
24 #include <Geom2d_OffsetCurve.hxx>
25 #include <TColGeom2d_HArray1OfCurve.hxx>
26 #include <TColStd_HSequenceOfReal.hxx>
27 #include <TColGeom2d_HArray1OfCurve.hxx>
28 #include <ShapeExtend.hxx>
31 //=======================================================================
32 //function : ShapeUpgrade_SplitCurve2dContinuity
34 //=======================================================================
36 ShapeUpgrade_SplitCurve2dContinuity::ShapeUpgrade_SplitCurve2dContinuity()
38 myCriterion = GeomAbs_C1;
39 myTolerance = Precision::PConfusion();
43 //=======================================================================
44 //function : SetCriterion
46 //=======================================================================
48 void ShapeUpgrade_SplitCurve2dContinuity::SetCriterion(const GeomAbs_Shape Criterion)
50 myCriterion = Criterion;
51 switch (myCriterion) {
52 case GeomAbs_C0 : myCont = 0; break;
53 case GeomAbs_C1 : myCont = 1; break;
54 case GeomAbs_C2 : myCont = 2; break;
55 case GeomAbs_C3 : myCont = 3; break;
56 case GeomAbs_CN : myCont = 4; break;
61 //=======================================================================
62 //function : SetTolerance
64 //=======================================================================
66 void ShapeUpgrade_SplitCurve2dContinuity::SetTolerance(const Standard_Real Tol)
71 //=======================================================================
74 //=======================================================================
76 void ShapeUpgrade_SplitCurve2dContinuity::Compute()
78 if(myCurve->Continuity() < myCriterion)
79 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE2);
80 if (mySplitValues->Length() > 2)
81 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
82 Standard_Real precision = Precision::PConfusion();
83 Standard_Real First = mySplitValues->Value(1);
84 Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
85 if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
86 Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
87 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
88 ShapeUpgrade_SplitCurve2dContinuity spc;
89 // spc.Init(BasCurve,Max(First,tmp->FirstParameter()),Min(Last,tmp->LastParameter()));
90 spc.Init(BasCurve,First,Last);
91 spc.SetSplitValues(mySplitValues);
92 spc.SetTolerance(myTolerance);
93 spc.SetCriterion(myCriterion);
95 mySplitValues->Clear();
96 mySplitValues->ChangeSequence() = spc.SplitValues()->Sequence();
97 //mySplitValues = spc.SplitValues();
98 myStatus |= spc.myStatus;
101 else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
102 GeomAbs_Shape BasCriterion;
103 switch (myCriterion) {
105 case GeomAbs_C1 : BasCriterion = GeomAbs_C2; break;
106 case GeomAbs_C2 : BasCriterion = GeomAbs_C3; break;
107 case GeomAbs_C3 : //if (ShapeUpgrade::Debug()) cout<<". this criterion is not suitable for a Offset curve"<<endl;
109 cout << "Warning: ShapeUpgrade_SplitCurve2dContinuity: criterion C3 for Offset curve" << endl;
111 case GeomAbs_CN : BasCriterion = GeomAbs_CN; break;
113 Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
114 Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
115 //Standard_Real Offset = tmp->Offset(); // Offset not used (skl)
116 ShapeUpgrade_SplitCurve2dContinuity spc;
117 // spc.Init(BasCurve,Max(tmp->FirstParameter(),First),Min(tmp->LastParameter(),Last));
118 spc.Init(BasCurve,First,Last);
119 spc.SetSplitValues(mySplitValues);
120 spc.SetTolerance(myTolerance);
121 spc.SetCriterion(BasCriterion);
123 mySplitValues->Clear();
124 mySplitValues->ChangeSequence() = spc.SplitValues()->Sequence();
125 myStatus |= spc.myStatus;
129 Handle(Geom2d_BSplineCurve) MyBSpline = Handle(Geom2d_BSplineCurve)::DownCast (myCurve);
130 if (MyBSpline.IsNull()) {
131 // if (ShapeUpgrade::Debug()) cout<<". curve is not a Bspline"<<endl;
136 Standard_Integer Deg=MyBSpline->Degree();
137 Standard_Integer NbKnots= MyBSpline->NbKnots();
138 // if (ShapeUpgrade::Debug()) cout<<". NbKnots="<<NbKnots<<endl;
142 Standard_Integer FirstInd =MyBSpline->FirstUKnotIndex()+1,
143 LastInd = MyBSpline->LastUKnotIndex()-1;
144 Standard_Integer iknot = FirstInd;
145 for(Standard_Integer j =2; j <= mySplitValues->Length(); j++) {
146 Last = mySplitValues->Value(j);
147 for (; iknot <= LastInd; iknot++) {
148 Standard_Real valknot = MyBSpline->Knot(iknot);
149 if(valknot <= First + precision) continue;
150 if(valknot >= Last - precision) break;
151 Standard_Integer Continuity=Deg-MyBSpline->Multiplicity(iknot);
152 //Standard_Real tt = MyBSpline->Knot(iknot); // tt not used (skl)
153 if (Continuity < myCont) {
154 // At this knot, the curve is C0; try to remove Knot.
155 Standard_Boolean corrected = Standard_False;
156 Standard_Integer newMultiplicity = Deg - myCont;
157 if (newMultiplicity < 0) newMultiplicity = 0;
161 corrected = MyBSpline->RemoveKnot(iknot, newMultiplicity, myTolerance);
163 catch (Standard_Failure) {
164 corrected = Standard_False;
167 if (corrected && newMultiplicity > 0) {
168 Continuity=Deg-MyBSpline->Multiplicity(iknot);
169 corrected = (Continuity >= myCont);
172 // at this knot, the continuity is now C1. Nothing else to do.
173 // if (ShapeUpgrade::Debug()) cout<<". Correction at Knot "<<iknot<<endl;
174 myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
177 // impossible to force C1 within the tolerance:
178 // this knot will be a splitting value.
179 mySplitValues->InsertBefore(j++,MyBSpline->Knot(iknot));
181 // if (ShapeUpgrade::Debug()) cout<<". Splitting at Knot "<<iknot<<endl;
189 if (mySplitValues->Length() > 2)
190 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);