summaryrefslogtreecommitdiff
path: root/src/ShapeUpgrade/ShapeUpgrade_SplitCurve3d.cxx
blob: 68e388f9093b44e8cfd69d5a35d4d4edfc52c472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// File:	ShapeUpgrade_SplitCurve3d.cxx
// Created:	Thu Mar 12 12:26:05 1998
// Author:	Roman LYGIN
//		<rln@kinox.nnov.matra-dtv.fr>

#include <ShapeUpgrade_SplitCurve3d.ixx>
#include <Geom_BSplineCurve.hxx>
#include <Precision.hxx>
#include <ShapeUpgrade.hxx>
#include <TColStd_ListIteratorOfListOfReal.hxx>
#include <TColStd_HSequenceOfReal.hxx>
#include <TColGeom_HArray1OfCurve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_OffsetCurve.hxx>
#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>
#include <ShapeExtend.hxx>
#include <Precision.hxx>
#include <Geom_BezierCurve.hxx>
#include <ShapeAnalysis_Curve.hxx>

//=======================================================================
//function : ShapeUpgrade_SplitCurve3d
//purpose  : 
//=======================================================================

ShapeUpgrade_SplitCurve3d::ShapeUpgrade_SplitCurve3d()
{
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

 void ShapeUpgrade_SplitCurve3d::Init(const Handle(Geom_Curve)& C) 
{
  Init (C, C->FirstParameter(), C->LastParameter());
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

 void ShapeUpgrade_SplitCurve3d::Init(const Handle(Geom_Curve)& C,
				      const Standard_Real First,
				      const Standard_Real Last) 
{
//  if (ShapeUpgrade::Debug()) cout << "SplitCurve3d::Init"<<endl;
  Handle(Geom_Curve) CopyOfC = Handle(Geom_Curve)::DownCast(C->Copy());
  myCurve = CopyOfC;
  Standard_Real precision = Precision::PConfusion();
  Standard_Real firstPar = First;
  Standard_Real lastPar = Last;
  Handle (Geom_Curve) aCurve = myCurve;
  if(aCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) 
    aCurve=Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
  // 15.11.2002 PTV OCC966
  if(!ShapeAnalysis_Curve::IsPeriodic(C)) {
    Standard_Real fP = aCurve->FirstParameter();
    Standard_Real lP  = aCurve->LastParameter();
    if(Abs(firstPar-fP) < precision)
       firstPar = fP;
    if(Abs(lastPar-lP) < precision)
      lastPar = lP;
    if(firstPar < fP){
#ifdef DEB
      cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
#endif
      firstPar = fP;
    }
    if(lastPar > lP){
#ifdef DEB
      cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
#endif
      lastPar = lP;
    }
    if( (lastPar-firstPar) < precision)
      lastPar=firstPar+2*precision;
  }
  
  ShapeUpgrade_SplitCurve::Init (firstPar, lastPar);

  // first, we make a copy of C to prevent modification:
//  if (ShapeUpgrade::Debug()) cout << ". copy of the curve"<<endl;
  
  myNbCurves = 1;
}

//=======================================================================
//function : Build
//purpose  : 
//=======================================================================

 void ShapeUpgrade_SplitCurve3d::Build(const Standard_Boolean Segment) 
{
//  if (ShapeUpgrade::Debug()) cout<<"ShapeUpgrade_SplitCurve3d::Build"<<endl;
  Standard_Real First =  mySplitValues->Value(1);
  Standard_Real Last =  mySplitValues->Value(mySplitValues->Length());
    
  if (mySplitValues->Length() >2)
    myStatus = ShapeExtend::EncodeStatus (ShapeExtend_DONE1);
  if (myCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
    Handle(Geom_TrimmedCurve) tmp = Handle(Geom_TrimmedCurve)::DownCast (myCurve);
    Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
    ShapeUpgrade_SplitCurve3d spc;
    spc.Init(BasCurve,First,Last);
    spc.SetSplitValues(mySplitValues);
    spc.Build(Segment);
    myNbCurves = spc.GetCurves()->Length();
    myResultingCurves=new TColGeom_HArray1OfCurve (1,myNbCurves);
    if(myNbCurves == 1) {
      Handle(Geom_TrimmedCurve) NewTrimCurve = 
	new Geom_TrimmedCurve(spc.GetCurves()->Value(1),First,Last);
      myResultingCurves->SetValue(1,NewTrimCurve);
    }
    else 
      myResultingCurves = spc.GetCurves();   
    myStatus |= spc.myStatus;
    return;
  }
  else if (myCurve->IsKind(STANDARD_TYPE(Geom_OffsetCurve))) {
    Handle(Geom_OffsetCurve) tmp = Handle(Geom_OffsetCurve)::DownCast (myCurve);
    Handle(Geom_Curve) BasCurve = tmp->BasisCurve();
    Standard_Real Offset = tmp->Offset();
    gp_Dir Direct = tmp->Direction();
    ShapeUpgrade_SplitCurve3d spc;
    spc.Init(BasCurve,First,Last);
    spc.SetSplitValues(mySplitValues);  
    spc.Build(Segment);
    myNbCurves = spc.GetCurves()->Length();
    myResultingCurves = new TColGeom_HArray1OfCurve (1,myNbCurves);
    for(Standard_Integer i = 1; i <= myNbCurves; i++) {
      Handle(Geom_OffsetCurve) NewOffsetCurve = 
	new Geom_OffsetCurve(spc.GetCurves()->Value(i),Offset,Direct);
      myResultingCurves->SetValue(i,NewOffsetCurve);
    }
    myStatus |= spc.myStatus;
    return;
  }
  
  //pdn fix on BuildCurve 3d
  // 15.11.2002 PTV OCC966
  if(!ShapeAnalysis_Curve::IsPeriodic(myCurve)) {
    //pdn exceptons only on non periodic curves
    Standard_Real precision = Precision::PConfusion();
    Standard_Real firstPar = myCurve->FirstParameter();
    Standard_Real lastPar  = myCurve->LastParameter();
    if(Abs(First-firstPar) < precision)
      First = firstPar;
    if(Abs(Last-lastPar) < precision)
      Last = lastPar;
    if(First < firstPar){
#ifdef DEB
      cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
#endif
      First = firstPar;
      mySplitValues->SetValue(1,First);
    }
    if(Last > lastPar){
#ifdef DEB
      cout <<"Warning: The range of the edge exceeds the curve domain" <<endl;
#endif
      Last = lastPar;
      mySplitValues->SetValue(mySplitValues->Length(),Last);
    }
  }

  myNbCurves = mySplitValues->Length() -1;
  myResultingCurves = new TColGeom_HArray1OfCurve (1, myNbCurves);
  if (myNbCurves == 1) {
    Standard_Boolean filled = Standard_True;
    if ( Abs ( myCurve->FirstParameter() - First ) < Precision::PConfusion() && 
	 Abs ( myCurve->LastParameter()  - Last  ) < Precision::PConfusion() )
      myResultingCurves->SetValue(1,myCurve);
    
    else if ( ! Segment || (!myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) &&
	     !myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve))) || ! Status ( ShapeExtend_DONE2 ) ) {
/*      if(myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) ||
	     myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve) )) {
	Handle(Geom_Curve) theNewCurve = Handle(Geom_Curve)::DownCast(myCurve->Copy());
	try {  
	  OCC_CATCH_SIGNALS
	  if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) 
	    Handle(Geom_BSplineCurve)::DownCast(theNewCurve)->Segment (First, Last);
	  else if (myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve)))
	    Handle(Geom_BezierCurve)::DownCast(theNewCurve)->Segment (First, Last);
	}
      catch (Standard_Failure) {
#ifdef DEB 
	  cout << "Warning: ShapeUpgrade_Split3dCurve::Build(): Exception in Segment      :";
	  Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
	  theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),First,Last);  
	}
	myResultingCurves->SetValue (1, theNewCurve);
      }
      else {*/
	Handle(Geom_TrimmedCurve) theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),First,Last);
	myResultingCurves->SetValue (1, theNewCurve);
     // }
    }
    else filled = Standard_False;
    if ( filled ) return;
  }
  if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) {
    Handle(Geom_BSplineCurve) BsCurve = Handle(Geom_BSplineCurve)::DownCast(myCurve->Copy());
    Standard_Integer FirstInd =BsCurve->FirstUKnotIndex(), 
    LastInd = BsCurve->LastUKnotIndex();
    Standard_Integer j =  FirstInd;
    for(Standard_Integer ii =1; ii <= mySplitValues->Length(); ii++) {
      Standard_Real spval = mySplitValues->Value(ii);
      for(; j <=LastInd;j++) {
	if( spval > BsCurve->Knot(j) + Precision::PConfusion()) continue;
	if( spval < BsCurve->Knot(j) - Precision::PConfusion()) break;
	mySplitValues->SetValue(ii,BsCurve->Knot(j));
      }
      if(j == LastInd) break;
    }
  }
  
  for (Standard_Integer i = 1; i <= myNbCurves; i++) {
    // skl : in the next block I change "First","Last" to "Firstt","Lastt"
    Standard_Real Firstt = mySplitValues->Value(i), Lastt = mySplitValues->Value(i+1);
    Handle(Geom_Curve) theNewCurve;
    if(Segment) {
      if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve)) ||
	  myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve))) {
	theNewCurve = Handle(Geom_Curve)::DownCast(myCurve->Copy());
	try {  
	  OCC_CATCH_SIGNALS
	  if (myCurve->IsKind (STANDARD_TYPE (Geom_BSplineCurve))) 
	    Handle(Geom_BSplineCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
	  else if (myCurve->IsKind (STANDARD_TYPE (Geom_BezierCurve)))
	    Handle(Geom_BezierCurve)::DownCast(theNewCurve)->Segment (Firstt, Lastt);
	  myStatus |= ShapeExtend::EncodeStatus ( ShapeExtend_DONE3 );
	}
	catch (Standard_Failure) {
#ifdef DEB 
	  cout << "Warning: ShapeUpgrade_Split3dCurve::Build(): Exception in Segment      :";
	  Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
	  theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);  
	}
      }
      else theNewCurve = new Geom_TrimmedCurve(Handle(Geom_Curve)::DownCast(myCurve->Copy()),Firstt,Lastt);  
    }
    myResultingCurves->SetValue (i, theNewCurve);
  }

}

//=======================================================================
//function : GetCurves
//purpose  : 
//=======================================================================

const Handle(TColGeom_HArray1OfCurve)& ShapeUpgrade_SplitCurve3d::GetCurves() const
{
  return myResultingCurves;
}