summaryrefslogtreecommitdiff
path: root/src/Extrema/Extrema_ExtPS.cxx
blob: 290e5b6539908e2c0a69b773f22d401f96977197 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// File Extrema_ExtPS.cxx
//-----------------------------------------------------------------
// modif JMB le 17 Mai 1999.
//           on trimme la surface a +/- 10000 plutot qu'a l'infini 
//           (sinon ca raise dans les Math)
//           voir bug ID260065 
//-----------------------------------------------------------------

#include <Extrema_ExtPS.ixx>
#include <Extrema_GenExtPS.hxx>
#include <StdFail_NotDone.hxx>
#include <Standard_NotImplemented.hxx>
#include <Precision.hxx>
#include <GeomAbs_CurveType.hxx>
#include <Adaptor3d_SurfaceOfLinearExtrusion.hxx>
#include <Adaptor3d_SurfaceOfRevolution.hxx>
#include <ElCLib.hxx>
#include <gp_Pnt.hxx>
#include <gp_Pln.hxx>
#include <gp_Cylinder.hxx>
#include <gp_Cone.hxx>
#include <gp_Sphere.hxx>
#include <gp_Torus.hxx>
#include <Extrema_ExtPExtS.hxx>
#include <Extrema_ExtPRevS.hxx>
#include <Extrema_POnSurf.hxx>
#include <GeomAbs_IsoType.hxx>

//=======================================================================
//function : IsoIsDeg
//purpose  : 
//=======================================================================

static Standard_Boolean IsoIsDeg  (const Adaptor3d_Surface& S,
				   const Standard_Real      Param,
				   const GeomAbs_IsoType    IT,
				   const Standard_Real      TolMin,
				   const Standard_Real      TolMax) 
{
    Standard_Real U1=0.,U2=0.,V1=0.,V2=0.,T;
    Standard_Boolean Along = Standard_True;
    U1 = S.FirstUParameter();
    U2 = S.LastUParameter();
    V1 = S.FirstVParameter();
    V2 = S.LastVParameter();
    gp_Vec D1U,D1V;
    gp_Pnt P;
    Standard_Real Step,D1NormMax;
    if (IT == GeomAbs_IsoV) 
    {
      Step = (U2 - U1)/10;
      if(Step < Precision::PConfusion()) {
        return Standard_False;
      }
      if(Step < Precision::PConfusion()) {
        return Standard_False;
      }
      D1NormMax=0.;
      for (T=U1;T<=U2;T=T+Step) 
      {
        S.D1(T,Param,P,D1U,D1V);
        D1NormMax=Max(D1NormMax,D1U.Magnitude());
      }

      if (D1NormMax >TolMax || D1NormMax < TolMin ) 
           Along = Standard_False;
    }
    else 
    {
      Step = (V2 - V1)/10;
      if(Step < Precision::PConfusion()) {
        return Standard_False;
      }
      if(Step < Precision::PConfusion()) {
        return Standard_False;
      }
      D1NormMax=0.;
      for (T=V1;T<=V2;T=T+Step) 
      {
	S.D1(Param,T,P,D1U,D1V);
        D1NormMax=Max(D1NormMax,D1V.Magnitude());
      }

      if (D1NormMax >TolMax || D1NormMax < TolMin ) 
           Along = Standard_False;


    }
    return Along;
}

//=======================================================================
//function : TreatSolution
//purpose  : 
//=======================================================================

void Extrema_ExtPS::TreatSolution (const Extrema_POnSurf& PS,
				   const Standard_Real Val)
{
  Standard_Real U, V;
  PS.Parameter(U, V);
  if (myS->IsUPeriodic()) {
    U = ElCLib::InPeriod(U, myuinf, myuinf+myS->UPeriod());
  }
  if (myS->IsVPeriodic()) {
    V = ElCLib::InPeriod(V, myvinf, myvinf+myS->VPeriod());
  }
  if ((myuinf-U) <= mytolu && (U-myusup) <= mytolu &&
      (myvinf-V) <= mytolv && (V-myvsup) <= mytolv) {
    myPoints.Append(Extrema_POnSurf (U, V, PS.Value()));
    mySqDist.Append(Val);
  }
}


//=======================================================================
//function : Extrema_ExtPS
//purpose  : 
//=======================================================================

Extrema_ExtPS::Extrema_ExtPS() 
{
  myDone = Standard_False;
}


//=======================================================================
//function : Extrema_ExtPS
//purpose  : 
//=======================================================================

Extrema_ExtPS::Extrema_ExtPS(const gp_Pnt&          P,
			     const Adaptor3d_Surface& S,
			     const Standard_Real    TolU,
			     const Standard_Real    TolV,
           const Extrema_ExtFlag F,
           const Extrema_ExtAlgo A)

{
  myExtPS.SetFlag(F);
  myExtPS.SetAlgo(A);
  Initialize(S, S.FirstUParameter(), S.LastUParameter(), 
	        S.FirstVParameter(), S.LastVParameter(), 
	        TolU, TolV);
  Perform(P);
}

//=======================================================================
//function : Extrema_ExtPS
//purpose  : 
//=======================================================================

Extrema_ExtPS::Extrema_ExtPS(const gp_Pnt&          P,
			     const Adaptor3d_Surface& S,
			     const Standard_Real    Uinf,	
			     const Standard_Real    Usup,
			     const Standard_Real    Vinf,	
			     const Standard_Real    Vsup,
			     const Standard_Real    TolU,
			     const Standard_Real    TolV,
           const Extrema_ExtFlag F,
           const Extrema_ExtAlgo A)

{
  myExtPS.SetFlag(F);
  myExtPS.SetAlgo(A);
  Initialize(S, Uinf, Usup, Vinf, Vsup, TolU, TolV);
  Perform(P);
}


//=======================================================================
//function : Initialize
//purpose  : 
//=======================================================================

void Extrema_ExtPS::Initialize(const Adaptor3d_Surface& S,
			       const Standard_Real    Uinf,	
			       const Standard_Real    Usup,
			       const Standard_Real    Vinf,	
			       const Standard_Real    Vsup,
			       const Standard_Real    TolU,
			       const Standard_Real    TolV)
{
  myS = (Adaptor3d_SurfacePtr)&S;
  myuinf = Uinf;
  myusup = Usup;
  myvinf = Vinf;
  myvsup = Vsup;

  if (Precision::IsNegativeInfinite(myuinf)) myuinf = -1e10;
  if (Precision::IsPositiveInfinite(myusup)) myusup = 1e10;
  if (Precision::IsNegativeInfinite(myvinf)) myvinf = -1e10;
  if (Precision::IsPositiveInfinite(myvsup)) myvsup = 1e10;

  mytolu = TolU;
  mytolv = TolV;
  mytype = myS->GetType();

  Standard_Boolean isB = ( myS->GetType() == GeomAbs_BSplineSurface ||
			   myS->GetType() == GeomAbs_BezierSurface );

  Standard_Integer nbU = (isB) ? 44 : 32;
  Standard_Integer nbV = (isB) ? 44 : 32;

  Standard_Boolean bUIsoIsDeg = Standard_False, bVIsoIsDeg = Standard_False;

  if(myS->GetType() != GeomAbs_Plane) {
    bUIsoIsDeg = IsoIsDeg(S, myuinf, GeomAbs_IsoU, 0., 1.e-9) ||
                 IsoIsDeg(S, myusup, GeomAbs_IsoU, 0., 1.e-9);
    bVIsoIsDeg = IsoIsDeg(S, myvinf, GeomAbs_IsoV, 0., 1.e-9) ||
                 IsoIsDeg(S, myvsup, GeomAbs_IsoV, 0., 1.e-9);
  }

  if(bUIsoIsDeg) nbU = 300;
  if(bVIsoIsDeg) nbV = 300;

  myExtPS.Initialize(*myS, nbU, nbV, myuinf, myusup, myvinf, myvsup, mytolu, mytolv);
}

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

void Extrema_ExtPS::Perform(const gp_Pnt& P)
{
  myPoints.Clear();
  mySqDist.Clear();
  Standard_Integer i;
  P11 = myS->Value(myuinf, myvinf);
  P12 = myS->Value(myuinf, myvsup);
  P21 = myS->Value(myusup, myvinf);
  P22 = myS->Value(myusup, myvsup);
  d11 = P.SquareDistance(P11);
  d12 = P.SquareDistance(P12);
  d21 = P.SquareDistance(P21);
  d22 = P.SquareDistance(P22);
  
  switch(mytype) {
    
  case GeomAbs_Cylinder:
    myExtPElS.Perform(P, myS->Cylinder(), Precision::Confusion());
    break;
  case GeomAbs_Plane:
    myExtPElS.Perform(P, myS->Plane(), Precision::Confusion());
    break;
  case GeomAbs_Cone:
    myExtPElS.Perform(P, myS->Cone(), Precision::Confusion());
    break;
  case GeomAbs_Sphere:
    myExtPElS.Perform(P, myS->Sphere(), Precision::Confusion());
    break;
  case GeomAbs_Torus:
    myExtPElS.Perform(P, myS->Torus(), Precision::Confusion());
    break;

    
  case GeomAbs_SurfaceOfExtrusion: {
    Extrema_ExtPExtS anExtPExtS(P,
				Adaptor3d_SurfaceOfLinearExtrusion(myS->BasisCurve(),
								   myS->Direction()),
				myuinf,myusup, myvinf,myvsup, mytolu,mytolv);
    myDone = anExtPExtS.IsDone();
    if (myDone) 
      for (i = 1; i <= anExtPExtS.NbExt(); i++) {
	TreatSolution (anExtPExtS.Point(i), anExtPExtS.SquareDistance(i));
      }
    return;
  }

    
  case GeomAbs_SurfaceOfRevolution: {
    Extrema_ExtPRevS anExtPRevS(P,
				Adaptor3d_SurfaceOfRevolution(myS->BasisCurve(),
							      myS->AxeOfRevolution()),
				myuinf, myusup,myvinf, myvsup,mytolu, mytolv);
    myDone = anExtPRevS.IsDone();
    if (myDone) 
      for (i = 1; i <= anExtPRevS.NbExt(); i++) {
	TreatSolution (anExtPRevS.Point(i), anExtPRevS.SquareDistance(i));
      }
    return;
  }

    
  default: 
    myExtPS.Perform(P);
    myDone = myExtPS.IsDone();
    if (myDone) 
      for (i = 1; i <= myExtPS.NbExt(); i++) 
	TreatSolution (myExtPS.Point(i), myExtPS.SquareDistance(i));
    return;
  }

  
  myDone = myExtPElS.IsDone();
  if (myDone) 
    for (i = 1; i <= myExtPElS.NbExt(); i++) 
      TreatSolution (myExtPElS.Point(i), myExtPElS.SquareDistance(i));
  return;
}



Standard_Boolean Extrema_ExtPS::IsDone() const
{
  return myDone;
}


Standard_Real Extrema_ExtPS::SquareDistance(const Standard_Integer N) const
{
  if(!myDone) StdFail_NotDone::Raise();
  if ((N < 1) || (N > mySqDist.Length())) Standard_OutOfRange::Raise();
  return mySqDist.Value(N);
}


Standard_Integer Extrema_ExtPS::NbExt() const
{
  if(!myDone) StdFail_NotDone::Raise();
  return mySqDist.Length();
}



Extrema_POnSurf Extrema_ExtPS::Point(const Standard_Integer N) const
{
  if(!myDone) StdFail_NotDone::Raise();
  return myPoints.Value(N);
}


void Extrema_ExtPS::TrimmedSquareDistances(Standard_Real& dUfVf,
				      Standard_Real& dUfVl,
				      Standard_Real& dUlVf,
				      Standard_Real& dUlVl,
				      gp_Pnt&        PUfVf,
				      gp_Pnt&        PUfVl,
				      gp_Pnt&        PUlVf,
				      gp_Pnt&        PUlVl) const
{
  dUfVf = d11;
  dUfVl = d12;
  dUlVf = d21;
  dUlVl = d22;
  PUfVf = P11;
  PUfVl = P12;
  PUlVf = P21;
  PUlVl = P22;
}

void Extrema_ExtPS::SetFlag(const Extrema_ExtFlag F)
{
  myExtPS.SetFlag(F);
}

void Extrema_ExtPS::SetAlgo(const Extrema_ExtAlgo A)
{
  myExtPS.SetAlgo(A);
}