0024955: Misuse of DownCast
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_SplitCurve2dContinuity.cxx
CommitLineData
b311480e 1// Created on: 1999-04-14
2// Created by: Roman LYGIN
3// Copyright (c) 1999-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
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>
29
30
31//=======================================================================
32//function : ShapeUpgrade_SplitCurve2dContinuity
33//purpose :
34//=======================================================================
35
36ShapeUpgrade_SplitCurve2dContinuity::ShapeUpgrade_SplitCurve2dContinuity()
37{
38 myCriterion = GeomAbs_C1;
39 myTolerance = Precision::PConfusion();
40 myCont =1;
41}
42
43//=======================================================================
44//function : SetCriterion
45//purpose :
46//=======================================================================
47
48 void ShapeUpgrade_SplitCurve2dContinuity::SetCriterion(const GeomAbs_Shape Criterion)
49{
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;
57 default : myCont = 1;
58 }
59}
60
61//=======================================================================
62//function : SetTolerance
63//purpose :
64//=======================================================================
65
66 void ShapeUpgrade_SplitCurve2dContinuity::SetTolerance(const Standard_Real Tol)
67{
68 myTolerance = Tol;
69}
70
71//=======================================================================
72//function : Compute
73//purpose :
74//=======================================================================
75
76void ShapeUpgrade_SplitCurve2dContinuity::Compute()
77{
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);
94 spc.Compute();
95 mySplitValues->Clear();
96 mySplitValues->ChangeSequence() = spc.SplitValues()->Sequence();
97 //mySplitValues = spc.SplitValues();
98 myStatus |= spc.myStatus;
99 return;
100 }
101 else if (myCurve->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
102 GeomAbs_Shape BasCriterion;
103 switch (myCriterion) {
104 default :
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;
108#ifdef DEB
109 cout << "Warning: ShapeUpgrade_SplitCurve2dContinuity: criterion C3 for Offset curve" << endl;
110#endif
111 case GeomAbs_CN : BasCriterion = GeomAbs_CN; break;
112 }
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);
122 spc.Compute();
123 mySplitValues->Clear();
124 mySplitValues->ChangeSequence() = spc.SplitValues()->Sequence();
125 myStatus |= spc.myStatus;
126 return;
127 }
128
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;
132 return;
133 }
134
135 myNbCurves=1;
136 Standard_Integer Deg=MyBSpline->Degree();
137 Standard_Integer NbKnots= MyBSpline->NbKnots();
138// if (ShapeUpgrade::Debug()) cout<<". NbKnots="<<NbKnots<<endl;
139 if (NbKnots <= 2) {
140 return;
141 }
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;
158 {
159 try {
160 OCC_CATCH_SIGNALS
161 corrected = MyBSpline->RemoveKnot(iknot, newMultiplicity, myTolerance);
162 }
163 catch (Standard_Failure) {
164 corrected = Standard_False;
165 }
166 }
167 if (corrected && newMultiplicity > 0) {
168 Continuity=Deg-MyBSpline->Multiplicity(iknot);
169 corrected = (Continuity >= myCont);
170 }
171 if (corrected) {
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 );
175 }
176 else {
177 // impossible to force C1 within the tolerance:
178 // this knot will be a splitting value.
179 mySplitValues->InsertBefore(j++,MyBSpline->Knot(iknot));
180 myNbCurves++;
181// if (ShapeUpgrade::Debug()) cout<<". Splitting at Knot "<<iknot<<endl;
182 }
183 }
184 }
185
186 First = Last;
187 }
188
189 if (mySplitValues->Length() > 2)
190 myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
191}
192