summaryrefslogtreecommitdiff
path: root/src/ShapeConstruct/ShapeConstruct_Curve.cxx
blob: cea3df7aa4d2e40c31cfb3ffce33cd5b4bba422b (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <ShapeConstruct_Curve.ixx>

#include <Standard_ErrorHandler.hxx>
#include <Standard_Failure.hxx>

#include <gp_Vec.hxx>
#include <gp_Dir.hxx>
#include <gp_Lin.hxx>

#include <ElCLib.hxx>
#include <Precision.hxx>

#include <Geom_BSplineCurve.hxx>
#include <Geom_BezierCurve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_Line.hxx>

#include <Geom2d_BSplineCurve.hxx>
#include <Geom2d_BezierCurve.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_Line.hxx>

#include <GeomConvert.hxx>
#include <GeomAdaptor_HCurve.hxx>
#include <Approx_Curve3d.hxx>

#include <Geom2dConvert.hxx>
#include <Geom2dAdaptor_HCurve.hxx>
#include <Approx_Curve2d.hxx>

//sln 29.12.2001 OCC90 : Method FixKnots was added

//=======================================================================
//function : AdjustCurve
//purpose  : 
//=======================================================================

Standard_Boolean ShapeConstruct_Curve::AdjustCurve(const Handle(Geom_Curve)& C3D,const gp_Pnt& P1,const gp_Pnt& P2,const Standard_Boolean take1,const Standard_Boolean take2) const
{
  if (!take1 && !take2) return Standard_True;

  if (C3D->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
    Handle(Geom_BSplineCurve) BSPL = Handle(Geom_BSplineCurve)::DownCast(C3D);
    if (take1) BSPL->SetPole (1,P1);
    if (take2) BSPL->SetPole (BSPL->NbPoles(),P2);
    return Standard_True;
  }

  if (C3D->IsKind(STANDARD_TYPE(Geom_Line))) {
    Handle(Geom_Line) L3D = Handle(Geom_Line)::DownCast(C3D);
//   ATTENTION, P1 et P2 sont supposes tous deux pertinents ...
    gp_Vec avec (P1,P2);  gp_Dir adir (avec);  gp_Lin alin (P1,adir);
    Standard_Real theParam = ElCLib::Parameter(alin, L3D->Lin().Location());
    alin.SetLocation (ElCLib::Value(theParam, alin));
    L3D->SetLin (alin);
    return Standard_True;
  }

  return Standard_False;
}

//=======================================================================
//function : AdjustCurveSegment
//purpose  : 
//=======================================================================

Standard_Boolean ShapeConstruct_Curve::AdjustCurveSegment(const Handle(Geom_Curve)& C3D,const gp_Pnt& P1,const gp_Pnt& P2,const Standard_Real U1,const Standard_Real U2) const
{
  if (C3D->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
    Handle(Geom_BSplineCurve) BSPL = Handle(Geom_BSplineCurve)::DownCast(C3D);
//    Forcer l extremite c est bien
//    Propager sur le reste, c est pas mal non plus
    if (U1 >= U2) return Standard_False;
    Standard_Real UU1 = Max (U1,BSPL->FirstParameter());
    Standard_Real UU2 = Min (U2,BSPL->LastParameter());
    BSPL->Segment (UU1,UU2);
    BSPL->SetPole (1,P1);
    BSPL->SetPole (BSPL->NbPoles(),P2);
    return Standard_True;
  }
    
  if (C3D->IsKind(STANDARD_TYPE(Geom_Line))) {
    Handle(Geom_Line) L3D = Handle(Geom_Line)::DownCast(C3D);
//   ATTENTION, P1 et P2 sont supposes tous deux pertinents ...
//   NB : on ne s aide pas de U1 et U2
    gp_Vec avec (P1,P2);  gp_Dir adir (avec);  gp_Lin alin (P1,adir);
    Standard_Real theParam = ElCLib::Parameter(alin, L3D->Lin().Location());
    alin.SetLocation (ElCLib::Value(theParam, alin));
    L3D->SetLin (alin);
    return Standard_True;
  }

  return Standard_False;
}

//=======================================================================
//function : AdjustCurve2d
//purpose  : 
//=======================================================================

Standard_Boolean ShapeConstruct_Curve::AdjustCurve2d(const Handle(Geom2d_Curve)& C2D,const gp_Pnt2d& P1,const gp_Pnt2d& P2,const Standard_Boolean take1,const Standard_Boolean take2) const
{
  if (!take1 && !take2) return Standard_True;

  if (C2D->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
    Handle(Geom2d_BSplineCurve) BSPL = Handle(Geom2d_BSplineCurve)::DownCast(C2D);
    if (take1) BSPL->SetPole (1,P1);
    if (take2) BSPL->SetPole (BSPL->NbPoles(),P2);
    return Standard_True;
  }

  if (C2D->IsKind(STANDARD_TYPE(Geom2d_Line))) {
    Handle(Geom2d_Line) L2D = Handle(Geom2d_Line)::DownCast(C2D);
//   ATTENTION, P1 et P2 sont supposes tous deux pertinents ...
    gp_Vec2d avec (P1,P2);  gp_Dir2d adir (avec);  gp_Lin2d alin (P1,adir);
    Standard_Real theParam = ElCLib::Parameter(alin, L2D->Lin2d().Location());
    alin.SetLocation (ElCLib::Value(theParam, alin));
    L2D->SetLin2d (alin);
    return Standard_True;
  }

  return Standard_False;
}

//=======================================================================
//function : ConvertToBSpline
//purpose  : 
//=======================================================================

Handle(Geom_BSplineCurve) ShapeConstruct_Curve::ConvertToBSpline (const Handle(Geom_Curve) &C,
								  const Standard_Real first, 
								  const Standard_Real last,
								  const Standard_Real prec) const
{
  Handle(Geom_BSplineCurve) bspl;
  
  if ( C->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ) {
    bspl = Handle(Geom_BSplineCurve)::DownCast ( C );
  }
  else if ( C->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ||
	    C->IsKind(STANDARD_TYPE(Geom_Line)) ) {
    Handle(Geom_Curve) tc = new Geom_TrimmedCurve ( C, first, last );
    try {
      OCC_CATCH_SIGNALS
      bspl = GeomConvert::CurveToBSplineCurve(tc);
    }
    catch ( Standard_Failure ) {
#ifdef DEB 
      cout << "Warning: ShapeConstruct_Curve::ConvertToBSpline(): Exception in GeomConvert: ";
      Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
    }
  }

  if ( ! bspl.IsNull() ) {
    // take segment if trim and range differ
    Standard_Real fbsp = bspl->FirstParameter(), lbsp = bspl->LastParameter();
    Standard_Boolean segment = Standard_False;
    if ( first > fbsp + Precision::PConfusion() ) { fbsp = first; segment = Standard_True; }
    if ( last  < lbsp - Precision::PConfusion() ) { lbsp = last;  segment = Standard_True; }
    if ( ! segment ) return bspl;
    try {
      OCC_CATCH_SIGNALS
      bspl = Handle(Geom_BSplineCurve)::DownCast ( bspl->Copy() );
      bspl->Segment ( fbsp, lbsp );
      return bspl;
    }
    catch ( Standard_Failure ) {
#ifdef DEB 
      cout << "Warning: ShapeConstruct_Curve::ConvertToBSpline(): Exception in Segment: ";
      Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
    }
  }

  // Approx is used for conics and when ordinary methods fail
  Handle(Geom_Curve) newc = C;
  if ( ! bspl.IsNull() ) { newc = bspl; bspl.Nullify(); }
  try {
    OCC_CATCH_SIGNALS
    Approx_Curve3d Conv ( new GeomAdaptor_HCurve(newc,first,last),
			  prec, GeomAbs_C1, 9, 1000 );
    if ( Conv.IsDone() || Conv.HasResult() ) 
      bspl = Conv.Curve();
  }
  catch ( Standard_Failure ) {
#ifdef DEB 
    cout << "Warning: ShapeConstruct_Curve::ConvertToBSpline(): Exception in Approx_Curve3d: ";
    Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
  }
  return bspl;
}

//=======================================================================
//function : ConvertToBSpline
//purpose  : 
//=======================================================================

Handle(Geom2d_BSplineCurve) ShapeConstruct_Curve::ConvertToBSpline (const Handle(Geom2d_Curve) &C,
								    const Standard_Real first, 
								    const Standard_Real last,
								    const Standard_Real prec) const
{
  Handle(Geom2d_BSplineCurve) bspl;
  
  if ( C->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve)) ) {
    bspl = Handle(Geom2d_BSplineCurve)::DownCast ( C );
  }
  else if ( C->IsKind(STANDARD_TYPE(Geom2d_BezierCurve)) ||
	    C->IsKind(STANDARD_TYPE(Geom2d_Line)) ) {
    Handle(Geom2d_Curve) tc = new Geom2d_TrimmedCurve ( C, first, last );
    try {
      OCC_CATCH_SIGNALS
      bspl = Geom2dConvert::CurveToBSplineCurve(tc);
    }
    catch ( Standard_Failure ) {
#ifdef DEB 
      cout << "Warning: ShapeConstruct_Curve::ConvertToBSpline(): Exception in Geom2dConvert: ";
      Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
    }
  }

  if ( ! bspl.IsNull() ) {
    // take segment if trim and range differ
    Standard_Real fbsp = bspl->FirstParameter(), lbsp = bspl->LastParameter();
    Standard_Boolean segment = Standard_False;
    if ( first > fbsp + Precision::PConfusion() ) { fbsp = first; segment = Standard_True; }
    if ( last  < lbsp - Precision::PConfusion() ) { lbsp = last;  segment = Standard_True; }
    if ( ! segment ) return bspl;
    try {
      OCC_CATCH_SIGNALS
      bspl = Handle(Geom2d_BSplineCurve)::DownCast ( bspl->Copy() );
      bspl->Segment ( fbsp, lbsp );
      return bspl;
    }
    catch ( Standard_Failure ) {
#ifdef DEB 
      cout << "Warning: ShapeConstruct_Curve::ConvertToBSpline(): Exception in Segment: ";
      Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
    }
  }

  // Approx is used for conics and when ordinary methods fail
  Handle(Geom2d_Curve) newc = C;
  if ( ! bspl.IsNull() ) { newc = bspl; bspl.Nullify(); }
  try {
    OCC_CATCH_SIGNALS
    Approx_Curve2d Conv ( new Geom2dAdaptor_HCurve(newc,first,last),
			  first, last,
			  prec, prec, GeomAbs_C1, 9, 1000 );
    if ( Conv.IsDone() || Conv.HasResult() ) 
      bspl = Conv.Curve();
  }
  catch ( Standard_Failure ) {
#ifdef DEB 
    cout << "Warning: ShapeConstruct_Curve::ConvertToBSpline(): Exception in Approx_Curve3d: ";
    Standard_Failure::Caught()->Print(cout); cout << endl;
#endif
  }
  return bspl;
}

//=======================================================================
//function : FixKnots
//purpose  : Fix coincided knots
//=======================================================================
Standard_Boolean ShapeConstruct_Curve::FixKnots (Handle(TColStd_HArray1OfReal) &knots) 
{
  Standard_Boolean Fixed = Standard_False;
  Standard_Integer nbKnots = knots->Length();
  Standard_Real knotVal = knots->Value(1);
  for ( Standard_Integer i=2; i <= nbKnots; i++ ) {
    Standard_Real knotNext = knots->Value(i);
    if ( knotNext - knotVal <= Epsilon(knotVal) ) {
      knotNext = knotVal + 2. * Epsilon(knotVal);
      knots->SetValue ( i, knotNext );
      Fixed = Standard_True;
    }
    knotVal = knotNext;
  }
  return Fixed;
}

//=======================================================================
//function : FixKnots
//purpose  : Fix coincided knots
//=======================================================================
Standard_Boolean ShapeConstruct_Curve::FixKnots (TColStd_Array1OfReal& knots) 
{
  Standard_Boolean Fixed = Standard_False;
  Standard_Integer nbKnots = knots.Length();
  Standard_Real knotVal = knots.Value(1);
  for ( Standard_Integer i=2; i <= nbKnots; i++ ) {
    Standard_Real knotNext = knots.Value(i);
    if ( knotNext - knotVal <= Epsilon(knotVal) ) {
      knotNext = knotVal + 2. * Epsilon(knotVal);
      knots.SetValue ( i, knotNext );
      Fixed = Standard_True;
    }
    knotVal = knotNext;
  }
  return Fixed;
}