0023367: New functionality restoring the middle path of pipe-like shape
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_SplitCurve2dContinuity.cxx
1 // Created on: 1999-04-14
2 // Created by: Roman LYGIN
3 // Copyright (c) 1999-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
23 #include <ShapeUpgrade_SplitCurve2dContinuity.ixx>
24 #include <Standard_ErrorHandler.hxx>
25 #include <Standard_Failure.hxx>
26 #include <Geom2d_BSplineCurve.hxx>
27 #include <Precision.hxx>
28 #include <ShapeUpgrade.hxx>
29 #include <Geom2d_TrimmedCurve.hxx>
30 #include <Geom2d_OffsetCurve.hxx>
31 #include <TColGeom2d_HArray1OfCurve.hxx>
32 #include <TColStd_HSequenceOfReal.hxx>
33 #include <TColGeom2d_HArray1OfCurve.hxx>
34 #include <ShapeExtend.hxx>
35
36
37 //=======================================================================
38 //function : ShapeUpgrade_SplitCurve2dContinuity
39 //purpose  : 
40 //=======================================================================
41
42 ShapeUpgrade_SplitCurve2dContinuity::ShapeUpgrade_SplitCurve2dContinuity()
43 {
44   myCriterion = GeomAbs_C1;
45   myTolerance = Precision::PConfusion();
46   myCont =1;
47 }
48
49 //=======================================================================
50 //function : SetCriterion
51 //purpose  : 
52 //=======================================================================
53
54  void ShapeUpgrade_SplitCurve2dContinuity::SetCriterion(const GeomAbs_Shape Criterion) 
55 {
56   myCriterion = Criterion;
57   switch (myCriterion) {
58     case GeomAbs_C0 : myCont = 0; break;
59     case GeomAbs_C1 : myCont = 1; break;
60     case GeomAbs_C2 : myCont = 2; break;
61     case GeomAbs_C3 : myCont = 3; break;
62     case GeomAbs_CN : myCont = 4; break;
63     default         : myCont = 1;  
64   }
65 }
66
67 //=======================================================================
68 //function : SetTolerance
69 //purpose  : 
70 //=======================================================================
71
72  void ShapeUpgrade_SplitCurve2dContinuity::SetTolerance(const Standard_Real Tol) 
73 {
74   myTolerance = Tol;
75 }
76
77 //=======================================================================
78 //function : Compute
79 //purpose  : 
80 //=======================================================================
81
82 void ShapeUpgrade_SplitCurve2dContinuity::Compute()    
83
84   if(myCurve->Continuity() < myCriterion) 
85     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE2);
86   if (mySplitValues->Length() > 2)
87     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
88   Standard_Real precision = Precision::PConfusion();
89   Standard_Real First =  mySplitValues->Value(1);
90   Standard_Real Last = mySplitValues->Value(mySplitValues->Length());
91   if (myCurve->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
92     Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (myCurve);
93     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
94     ShapeUpgrade_SplitCurve2dContinuity spc;
95 //    spc.Init(BasCurve,Max(First,tmp->FirstParameter()),Min(Last,tmp->LastParameter())); 
96     spc.Init(BasCurve,First,Last);
97     spc.SetSplitValues(mySplitValues);
98     spc.SetTolerance(myTolerance);
99     spc.SetCriterion(myCriterion);
100     spc.Compute();
101     mySplitValues->Clear();
102     mySplitValues->ChangeSequence() = spc.SplitValues()->Sequence();
103     //mySplitValues = spc.SplitValues();
104     myStatus |= spc.myStatus;
105     return;
106   }
107   else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
108     GeomAbs_Shape BasCriterion;
109     switch (myCriterion) {
110       default         :
111       case GeomAbs_C1 : BasCriterion = GeomAbs_C2; break;
112       case GeomAbs_C2 : BasCriterion = GeomAbs_C3; break;
113       case GeomAbs_C3 : //if (ShapeUpgrade::Debug()) cout<<". this criterion is not suitable for a Offset curve"<<endl; 
114 #ifdef DEB
115                         cout << "Warning: ShapeUpgrade_SplitCurve2dContinuity: criterion C3 for Offset curve" << endl; 
116 #endif
117       case GeomAbs_CN : BasCriterion = GeomAbs_CN; break;
118     }
119     Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (myCurve);
120     Handle(Geom2d_Curve) BasCurve = tmp->BasisCurve();
121     //Standard_Real Offset = tmp->Offset(); // Offset not used (skl)
122     ShapeUpgrade_SplitCurve2dContinuity spc;
123 //    spc.Init(BasCurve,Max(tmp->FirstParameter(),First),Min(tmp->LastParameter(),Last));
124     spc.Init(BasCurve,First,Last);
125     spc.SetSplitValues(mySplitValues);
126     spc.SetTolerance(myTolerance);
127     spc.SetCriterion(BasCriterion); 
128     spc.Compute();
129     mySplitValues->Clear();
130     mySplitValues->ChangeSequence() = spc.SplitValues()->Sequence();
131     myStatus |= spc.myStatus;
132     return;
133   }
134
135   Handle(Geom2d_BSplineCurve) MyBSpline = Handle(Geom2d_BSplineCurve)::DownCast (myCurve);
136   if (MyBSpline.IsNull()) {
137 //    if (ShapeUpgrade::Debug()) cout<<". curve is not a Bspline"<<endl;
138     return;
139   }
140   
141   myNbCurves=1;
142   Standard_Integer Deg=MyBSpline->Degree();
143   Standard_Integer NbKnots= MyBSpline->NbKnots(); 
144 //  if (ShapeUpgrade::Debug()) cout<<". NbKnots="<<NbKnots<<endl;
145   if (NbKnots <= 2) {
146     return;
147   }
148   Standard_Integer FirstInd =MyBSpline->FirstUKnotIndex()+1,
149   LastInd = MyBSpline->LastUKnotIndex()-1;
150   Standard_Integer iknot = FirstInd;
151   for(Standard_Integer j =2; j <= mySplitValues->Length(); j++) {
152     Last =  mySplitValues->Value(j);
153     for (; iknot <= LastInd; iknot++) {
154       Standard_Real valknot = MyBSpline->Knot(iknot);
155       if(valknot <= First + precision) continue;
156       if(valknot >= Last - precision) break;
157       Standard_Integer Continuity=Deg-MyBSpline->Multiplicity(iknot);
158       //Standard_Real tt = MyBSpline->Knot(iknot); // tt not used (skl)
159       if (Continuity < myCont) { 
160         // At this knot, the curve is C0; try to remove Knot.
161         Standard_Boolean corrected = Standard_False;
162         Standard_Integer newMultiplicity = Deg - myCont;
163         if (newMultiplicity < 0) newMultiplicity = 0;
164         {
165           try {
166             OCC_CATCH_SIGNALS
167             corrected = MyBSpline->RemoveKnot(iknot, newMultiplicity, myTolerance);
168           }
169           catch (Standard_Failure) {
170             corrected = Standard_False;
171           }
172         }
173         if (corrected && newMultiplicity > 0) {
174          Continuity=Deg-MyBSpline->Multiplicity(iknot);
175          corrected = (Continuity >= myCont);
176         }
177         if (corrected) {
178           // at this knot, the continuity is now C1. Nothing else to do.
179 //        if (ShapeUpgrade::Debug()) cout<<". Correction at Knot "<<iknot<<endl;
180           myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
181         }
182         else {
183           // impossible to force C1 within the tolerance: 
184           // this knot will be a splitting value.
185           mySplitValues->InsertBefore(j++,MyBSpline->Knot(iknot));
186           myNbCurves++;
187 //        if (ShapeUpgrade::Debug()) cout<<". Splitting at Knot "<<iknot<<endl;
188         }
189       }
190     }
191     
192     First = Last;
193   }
194   
195   if (mySplitValues->Length() > 2)
196     myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
197 }
198