summaryrefslogtreecommitdiff
path: root/src/GeomFill/GeomFill_Profiler.cxx
blob: 4cded018680c4efe86901fea8ba6f34f2e009663 (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
// File:	GeomFill_Profiler.cxx
// Created:	Thu Feb 17 11:33:12 1994
// Author:	Bruno DUMORTIER
//		<dub@fuegox>

#include <GeomFill_Profiler.ixx>
#include <GeomConvert.hxx>
#include <BSplCLib.hxx>
#include <Geom_BSplineCurve.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Geom_Conic.hxx>
#include <GeomConvert_ApproxCurve.hxx>


//=======================================================================
//function : GeomFill_Profiler
//purpose  : 
//=======================================================================

GeomFill_Profiler::GeomFill_Profiler()
{
  myIsDone = Standard_False;
  myIsPeriodic = Standard_True;
}


//=======================================================================

void GeomFill_Profiler::Delete()
{}


//=======================================================================
//function : AddCurve
//purpose  : 
//=======================================================================

void GeomFill_Profiler::AddCurve(const Handle(Geom_Curve)& Curve)
{
  Handle(Geom_Curve) C;
  //// modified by jgv, 19.01.05 for OCC7354 ////
  Handle(Geom_Curve) theCurve = Curve;
  if (theCurve->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
    theCurve = (*((Handle(Geom_TrimmedCurve)*)&theCurve))->BasisCurve();
  if (theCurve->IsKind(STANDARD_TYPE(Geom_Conic)))
    {
      GeomConvert_ApproxCurve appr(Curve, Precision::Confusion(), GeomAbs_C1, 16, 14);
      if (appr.HasResult())
	C = appr.Curve();
    }
  if (C.IsNull())
    C = GeomConvert::CurveToBSplineCurve(Curve);
  /*
  if ( Curve->IsKind(STANDARD_TYPE(Geom_BSplineCurve))) {
    C = Handle(Geom_Curve)::DownCast(Curve->Copy());
  }
  else {
    C = GeomConvert::CurveToBSplineCurve(Curve,Convert_QuasiAngular);
  }
  */
  ///////////////////////////////////////////////

  mySequence.Append( C);

  if ( myIsPeriodic && !C->IsPeriodic())
    myIsPeriodic = Standard_False;
}


//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void GeomFill_Profiler::Perform(const Standard_Real PTol)
{
  Standard_Integer i;
//  Standard_Integer myDegree = 0, myNbPoles = 0;
  Standard_Integer myDegree = 0;
  Handle(Geom_BSplineCurve) C;
  Standard_Real U1, U2, UFirst=0, ULast=0;
  Standard_Real EcartMax = 0.;

  for ( i = 1; i <= mySequence.Length(); i++) {
    C = Handle(Geom_BSplineCurve)::DownCast(mySequence(i));

    // si non periodique, il faut deperiodiser toutes les courbes
    // on les segmente ensuite pour assurer K(1) et K(n) de multiplicite
    // degre + 1

    U2 = C->Knot(C->LastUKnotIndex());
    U1 = C->Knot(C->FirstUKnotIndex());

    if ( !myIsPeriodic && C->IsPeriodic()) {
      C->SetNotPeriodic();
      C->Segment( U1, U2);
    }

    // evaluate the max degree
    myDegree = Max( myDegree, C->Degree());
    
    // Calcul de Max ( Ufin - Udeb) sur l ensemble des courbes.
    if ( ( U2 - U1) > EcartMax) {
      EcartMax = U2 - U1;
      UFirst = U1;
      ULast  = U2;
    }
  }
  
  // increase the degree of the curves to my degree
  // reparametrize them in the range U1, U2.
  for ( i = 1; i <= mySequence.Length(); i++) {
    C = Handle(Geom_BSplineCurve)::DownCast(mySequence(i));

    C->IncreaseDegree( myDegree);

    TColStd_Array1OfReal Knots(1,C->NbKnots());
    C->Knots(Knots);
    BSplCLib::Reparametrize(UFirst,ULast,Knots);
    C->SetKnots(Knots);
  }
  
  // inserting in the first curve the knot-vector of all the others.
  C = Handle(Geom_BSplineCurve)::DownCast(mySequence(1));

  for ( i = 2; i <= mySequence.Length(); i++) {
    Handle(Geom_BSplineCurve) Ci = 
      Handle(Geom_BSplineCurve)::DownCast(mySequence(i));
    TColStd_Array1OfReal Ki(1,Ci->NbKnots());
    Ci->Knots(Ki);
    TColStd_Array1OfInteger Mi(1,Ci->NbKnots());
    Ci->Multiplicities(Mi);

    C->InsertKnots( Ki, Mi, PTol, Standard_False);
  }

  TColStd_Array1OfReal NewKnots(1,C->NbKnots());
  C->Knots(NewKnots);
  TColStd_Array1OfInteger NewMults(1,C->NbKnots());
  C->Multiplicities(NewMults);
  for ( i = 2; i <= mySequence.Length(); i++) {
    Handle(Geom_BSplineCurve) Ci = 
      Handle(Geom_BSplineCurve)::DownCast(mySequence(i));
    Ci->InsertKnots(NewKnots, NewMults, PTol, Standard_False);
  }

  // essai : tentative mise des poids sur chaque section a une moyenne 1
  for ( i = 1; i <= mySequence.Length(); i++) {
    Handle(Geom_BSplineCurve) Ci = 
      Handle(Geom_BSplineCurve)::DownCast(mySequence(i));
    if ( Ci->IsRational() ) {
      Standard_Integer np = Ci->NbPoles();
      Standard_Real sigma = 0.;
      Standard_Integer j;
      for ( j = 1; j <= np; j++) {
	sigma += Ci->Weight(j);
      }
      sigma /= np;
      for ( j= 1; j<= np; j++) {
	Ci->SetWeight(j,Ci->Weight(j) / sigma);
      }
    }
  }
  // fin de l essai

  myIsDone = Standard_True;
}


//=======================================================================
//function : Degree
//purpose  : 
//=======================================================================

Standard_Integer GeomFill_Profiler::Degree() const 
{
  if ( !myIsDone)
    StdFail_NotDone::Raise("GeomFill_Profiler::Degree");

  Handle(Geom_BSplineCurve) C = 
    Handle(Geom_BSplineCurve)::DownCast(mySequence(1));
  return C->Degree();
}


//=======================================================================
//function : NbPoles
//purpose  : 
//=======================================================================

Standard_Integer GeomFill_Profiler::NbPoles() const 
{
  if ( !myIsDone)
    StdFail_NotDone::Raise("GeomFill_Profiler::Degree");

  Handle(Geom_BSplineCurve) C = 
    Handle(Geom_BSplineCurve)::DownCast(mySequence(1));
  return C->NbPoles();
}


//=======================================================================
//function : Poles
//purpose  : 
//=======================================================================

void GeomFill_Profiler::Poles(const Standard_Integer Index, 
			            TColgp_Array1OfPnt& Poles) const 
{
  if ( !myIsDone)
    StdFail_NotDone::Raise("GeomFill_Profiler::Degree");

  Standard_DomainError_Raise_if( Poles.Length() != NbPoles(),
				"GeomFill_Profiler::Poles");
  Standard_DomainError_Raise_if( Index < 1 || Index > mySequence.Length(),
				"GeomFill_Profiler::Poles");

  Handle(Geom_BSplineCurve) C = 
    Handle(Geom_BSplineCurve)::DownCast(mySequence(Index));

  C->Poles(Poles);
}


//=======================================================================
//function : Weights
//purpose  : 
//=======================================================================

void GeomFill_Profiler::Weights(const Standard_Integer      Index, 
				      TColStd_Array1OfReal& Weights) const 
{
  if ( !myIsDone)
    StdFail_NotDone::Raise("GeomFill_Profiler::Degree");

  Standard_DomainError_Raise_if( Weights.Length() != NbPoles(),
				"GeomFill_Profiler::Weights");
  Standard_DomainError_Raise_if( Index < 1 || Index > mySequence.Length(),
				"GeomFill_Profiler::Weights");

  Handle(Geom_BSplineCurve) C = 
    Handle(Geom_BSplineCurve)::DownCast(mySequence(Index));

  C->Weights(Weights);
}


//=======================================================================
//function : NbKnots
//purpose  : 
//=======================================================================

Standard_Integer GeomFill_Profiler::NbKnots() const 
{
  if ( !myIsDone)
    StdFail_NotDone::Raise("GeomFill_Profiler::Degree");

  Handle(Geom_BSplineCurve) C = 
    Handle(Geom_BSplineCurve)::DownCast(mySequence(1));

  return C->NbKnots();
}


//=======================================================================
//function : KnotsAndMults
//purpose  : 
//=======================================================================

void GeomFill_Profiler::KnotsAndMults(TColStd_Array1OfReal&    Knots, 
				      TColStd_Array1OfInteger& Mults ) const 
{
  if ( !myIsDone)
    StdFail_NotDone::Raise("GeomFill_Profiler::Degree");

#ifndef No_Exception
  Standard_Integer n = NbKnots();
#endif
  Standard_DomainError_Raise_if( Knots.Length() != n  || Mults.Length() != n,
				"GeomFill_Profiler::KnotsAndMults");

  Handle(Geom_BSplineCurve) C = 
    Handle(Geom_BSplineCurve)::DownCast(mySequence(1));

  C->Knots(Knots);
  C->Multiplicities(Mults);
}