summaryrefslogtreecommitdiff
path: root/inc/LProp_SLProps.gxx
blob: 8bea7c93c19ac275a0ff1cf49d3a7ce5d089b543 (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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <LProp_Status.hxx>
#include <LProp_NotDefined.hxx>
#include <Standard_OutOfRange.hxx>
#include <Standard_DomainError.hxx>
#include <CSLib.hxx>
#include <CSLib_DerivativeStatus.hxx>
#include <CSLib_NormalStatus.hxx>
#include <TColgp_Array2OfVec.hxx>
#include <math_DirectPolynomialRoots.hxx>


static Standard_Boolean IsTangentDefined (LProp_SLProps& SProp,
                                 const Standard_Integer  cn,
                                 const Standard_Real     linTol,
                                 const Standard_Integer  Derivative, 
                                 Standard_Integer&       Order,
                                 LProp_Status&  Status) 
{
  Standard_Real Tol = linTol * linTol;
  gp_Vec V[2];
  Order = 0;
  while (Order < 3) {
    Order++;
    if(cn >= Order) {
      switch(Order) {
      case 1 :
	V[0] = SProp.D1U();
	V[1] = SProp.D1V();
	break;
      case 2 :
	V[0] = SProp.D2U();
	V[1] = SProp.D2V();
	break;
      };
      if(V[Derivative].SquareMagnitude() > Tol) {
	Status = LProp_Defined;
	return Standard_True;
      }
    }
    else {
      Status = LProp_Undefined;
      return Standard_False;
    }
  }
  return Standard_False;
}

LProp_SLProps::LProp_SLProps (const Surface& S, 
			      const Standard_Real     U,  
                              const Standard_Real     V, 
                              const Standard_Integer  N, 
                              const Standard_Real     Resolution) 
     : surf(S),
       level(N),
       cn(4), // (Tool::Continuity(S)),
       linTol(Resolution)
{

  Standard_OutOfRange_Raise_if(N < 0 || N > 2, 
                               "LProp_SLProps::LProp_SLProps()");

  SetParameters(U, V);
}

LProp_SLProps::LProp_SLProps (const Surface& S, 
                              const Standard_Integer  N, 
                              const Standard_Real     Resolution) 
     : surf(S),
       u(RealLast()), v(RealLast()),
       level(N),
       cn(4), // (Tool::Continuity(S)),
       linTol(Resolution),
       uTangentStatus (LProp_Undecided),
       vTangentStatus (LProp_Undecided),
       normalStatus   (LProp_Undecided),
       curvatureStatus(LProp_Undecided)
{
  Standard_OutOfRange_Raise_if(N < 0 || N > 2, 
                               "LProp_SLProps::LProp_SLProps()");
}

LProp_SLProps::LProp_SLProps (const Standard_Integer  N, 
                              const Standard_Real     Resolution) 
       :u(RealLast()), v(RealLast()),
       level(N),
       cn(0),
       linTol(Resolution),
       uTangentStatus (LProp_Undecided),
       vTangentStatus (LProp_Undecided),
       normalStatus   (LProp_Undecided),
       curvatureStatus(LProp_Undecided)
{
  Standard_OutOfRange_Raise_if(N < 0 || N > 2, 
                               "LProp_SLProps::LProp_SLProps() bad level");
}

void LProp_SLProps::SetSurface (const Surface& S ) {

     surf = S;
     cn = 4; // =Tool::Continuity(S);
}

void LProp_SLProps::SetParameters (const Standard_Real U, const Standard_Real V)
{
  u = U;
  v = V;
  switch (level) {
  case 0:
    Tool::Value(surf, u, v, pnt);
    break;
  case 1:
    Tool::D1(surf, u, v, pnt, d1U, d1V);
    break;
  case 2:
    Tool::D2(surf, u, v, pnt, d1U, d1V, d2U, d2V, dUV);
    break;
  };
  uTangentStatus  = LProp_Undecided;
  vTangentStatus  = LProp_Undecided;
  normalStatus    = LProp_Undecided;
  curvatureStatus = LProp_Undecided;
}

const gp_Pnt& LProp_SLProps::Value() const
{
  return pnt;
}

const gp_Vec& LProp_SLProps::D1U()
{
  if (level < 1) {
    level =1;
    Tool::D1(surf,u,v,pnt,d1U,d1V);
  }
  return d1U;
}

const gp_Vec& LProp_SLProps::D1V()
{
  if (level < 1) {
    level =1;
    Tool::D1(surf,u,v,pnt,d1U,d1V);
  }
  return d1V;
}

const gp_Vec& LProp_SLProps::D2U()
{
  if (level < 2) {
    level =2;
    Tool::D2(surf,u,v,pnt,d1U,d1V,d2U,d2V,dUV);
  }
  return d2U;
}

const gp_Vec& LProp_SLProps::D2V()
{
  if (level < 2) {
    level =2;
    Tool::D2(surf,u,v,pnt,d1U,d1V,d2U,d2V,dUV);
  }
  return d2V;
}
const gp_Vec& LProp_SLProps::DUV()
{
  if (level < 2) {
    level =2;
    Tool::D2(surf,u,v,pnt,d1U,d1V,d2U,d2V,dUV);
  }
  return dUV;
}

Standard_Boolean LProp_SLProps::IsTangentUDefined ()
{
  if (uTangentStatus == LProp_Undefined) { 
    return Standard_False;
  }
  else if (uTangentStatus >= LProp_Defined) { 
    return Standard_True; 
  }
  // uTangentStatus == Lprop_Undecided 
  // we have to calculate the first non null U derivative
  return IsTangentDefined(*this, cn, linTol, 0, 
                          significantFirstUDerivativeOrder, uTangentStatus);
}

void LProp_SLProps::TangentU (gp_Dir& D) {

  if(!IsTangentUDefined()) { LProp_NotDefined::Raise(); }
  if(significantFirstUDerivativeOrder == 1) {
    D = gp_Dir(d1U);
  }
  else {
    D = gp_Dir(d2U);
  }
}

Standard_Boolean LProp_SLProps::IsTangentVDefined ()
{

  if (vTangentStatus == LProp_Undefined) { 
    return Standard_False;
  }
  else if (vTangentStatus >= LProp_Defined) { 
    return Standard_True; 
  }
  // vTangentStatus == Lprop_Undecided 
  // we have to calculate the first non null V derivative
  return IsTangentDefined(*this, cn, linTol, 1, 
                          significantFirstVDerivativeOrder, vTangentStatus);
}

void LProp_SLProps::TangentV (gp_Dir& D) {

  if(!IsTangentVDefined()) { LProp_NotDefined::Raise(); }
  if(significantFirstVDerivativeOrder == 1) {
    D = gp_Dir(d1V);
  }
  else {
    D = gp_Dir(d2V);
  }
}

Standard_Boolean LProp_SLProps::IsNormalDefined()
{

  if (normalStatus == LProp_Undefined) {
    return Standard_False;
  }
  else if (normalStatus >= LProp_Defined) {
    return Standard_True;
  }
  // status = UnDecided 
  
  // first try the standard computation of the normal.
  CSLib_DerivativeStatus Status;
  CSLib::Normal(d1U, d1V, linTol, Status, normal);
  if (Status  == CSLib_Done ) { 
    normalStatus = LProp_Computed;
    return Standard_True;
  }
  // else solve the degenerated case only if continuity >= 2
/*  if (cn >= 2) {
    if(level < 2) this->D2U();
    Standard_Boolean Done;
    CSLib_NormalStatus Stat;
    CSLib::Normal(d1U, d1V, d2U, d2V, dUV, linTol, Done, Stat, normal);
    if (Done) {
      normalStatus = LProp_Computed;
      return Standard_True;
    }
  }*/
  /*
  else {
    Standard_Integer MaxOrder=3;
    CSLib_NormalStatus Stat;
    gp_Dir thenormal;
    TColgp_Array2OfVec DerNUV(0,MaxOrder,0,MaxOrder);
    TColgp_Array2OfVec DerSurf(0,MaxOrder+1,0,MaxOrder+1);
    Standard_Integer i,j,OrderU,OrderV;
    Standard_Real Umin,Umax,Vmin,Vmax;
 
    Tool::Bounds(surf, Umin, Vmin, Umax, Vmax);

    // Calcul des derivees
    for(i=1;i<=MaxOrder+1;i++){
      DerSurf.SetValue(i,0, Tool::DN(surf,u,v,i,0));
    }
  
    for(i=0;i<=MaxOrder+1;i++)
      for(j=1;j<=MaxOrder+1;j++){
	DerSurf.SetValue(i,j, Tool::DN(surf,u,v,i,j));
      }

    for(i=0;i<=MaxOrder;i++)
      for(j=0;j<=MaxOrder;j++){
	DerNUV.SetValue(i,j,CSLib::DNNUV(i,j,DerSurf));
      }

    CSLib::Normal(MaxOrder,DerNUV, 1.e-9, u,v,
		  Umin,Umax,Vmin,Vmax,
		  Stat, thenormal, OrderU, OrderV);
    normal.SetXYZ(thenormal.XYZ());
    if (Stat == CSLib_Defined) {
      normalStatus = LProp_Computed;
      return Standard_True;
    }
  }
  */

  normalStatus = LProp_Undefined;
  return Standard_False;
}

const gp_Dir& LProp_SLProps::Normal () {

  if(!IsNormalDefined()) { LProp_NotDefined::Raise(); }
  return normal;
}

Standard_Boolean LProp_SLProps::IsCurvatureDefined ()
{
  
  if (curvatureStatus == LProp_Undefined) {
    return Standard_False;
  }
  else if (curvatureStatus >= LProp_Defined) {
    return Standard_True;
  }
  if(cn < 2) {
    curvatureStatus = LProp_Undefined;
    return Standard_False;
  }
  // status = UnDecided 
  if (!IsNormalDefined()) {
    curvatureStatus = LProp_Undefined;
    return Standard_False;
  }
  // pour eviter un plantage dans le cas du caro pointu
  // en fait on doit pouvoir calculer les courbure
  // avoir
  if(!IsTangentUDefined() || !IsTangentVDefined()) {
    curvatureStatus = LProp_Undefined;
    return Standard_False;
  }

  // here we compute the curvature features of the surface

  gp_Vec Norm (normal);

  Standard_Real E = d1U.SquareMagnitude();
  Standard_Real F = d1U.Dot(d1V);
  Standard_Real G = d1V.SquareMagnitude();

  if(level < 2) this->D2U();

  Standard_Real L = Norm.Dot(d2U);
  Standard_Real M = Norm.Dot(dUV);
  Standard_Real N = Norm.Dot(d2V);

  Standard_Real A = E * M - F * L;
  Standard_Real B = E * N - G * L;
  Standard_Real C = F * N - G * M;

  Standard_Real MaxABC = Max(Max(Abs(A),Abs(B)),Abs(C));
  if (MaxABC < RealEpsilon()) {          // ombilic
    minCurv = N / G;
    maxCurv = minCurv;
    dirMinCurv = gp_Dir (d1U);
    dirMaxCurv = gp_Dir (d1U.Crossed(Norm));
    meanCurv = minCurv;            // (Cmin + Cmax) / 2.
    gausCurv = minCurv * minCurv;  // (Cmin * Cmax)
    curvatureStatus = LProp_Computed;
    return Standard_True;
  }

  A = A / MaxABC;
  B = B / MaxABC;
  C = C / MaxABC;
  Standard_Real Curv1, Curv2, Root1, Root2;
  gp_Vec VectCurv1, VectCurv2;
  if (Abs(A) > RealEpsilon()) {
    math_DirectPolynomialRoots Root (A, B, C);
    if(Root.NbSolutions() != 2) {
      curvatureStatus = LProp_Undefined;
      return Standard_False;
    }
    else {
       Root1 = Root.Value(1);
      Root2 = Root.Value(2);
      Curv1 = ((L * Root1 + 2. * M) * Root1 + N) /
	      ((E * Root1 + 2. * F) * Root1 + G);
      Curv2 = ((L * Root2 + 2. * M) * Root2 + N) /
	      ((E * Root2 + 2. * F) * Root2 + G);
      VectCurv1 = Root1 * d1U + d1V;
      VectCurv2 = Root2 * d1U + d1V;
    }
  }
  else if (Abs(C) > RealEpsilon()) {
    math_DirectPolynomialRoots Root(C, B, A);
    if((Root.NbSolutions() != 2)) {
      curvatureStatus = LProp_Undefined;
      return Standard_False;
    }
    else {
      Root1 = Root.Value(1);
      Root2 = Root.Value(2);
      Curv1 = ((N * Root1 + 2. * M) * Root1 + L) /
	      ((G * Root1 + 2. * F) * Root1 + E);
      Curv2 = ((N * Root2 + 2. * M) * Root2 + L) /
	      ((G * Root2 + 2. * F) * Root2 + E);
      VectCurv1 = d1U + Root1 * d1V;
      VectCurv2 = d1U + Root2 * d1V;
    }
  }
  else {
    Curv1 = L / E;
    Curv2 = N / G;
    VectCurv1 = d1U;
    VectCurv2 = d1V;
  }
  if (Curv1 < Curv2) {
    minCurv = Curv1;
    maxCurv = Curv2;
    dirMinCurv = gp_Dir (VectCurv1);
    dirMaxCurv = gp_Dir (VectCurv2);
  }
  else {
    minCurv = Curv2;
    maxCurv = Curv1;
    dirMinCurv = gp_Dir (VectCurv2);
    dirMaxCurv = gp_Dir (VectCurv1);
  }
  meanCurv = ((N * E) - (2. * M * F) + (L * G)) // voir Farin p.282
               / (2. * ((E * G) - (F * F)));
  gausCurv = ((L * N) - (M * M)) 
               / ((E * G) - (F * F));
  curvatureStatus = LProp_Computed;
  return Standard_True;
} 


Standard_Boolean LProp_SLProps::IsUmbilic ()
{
  if(!IsCurvatureDefined()) { LProp_NotDefined::Raise(); }
  return Abs(maxCurv - minCurv) < Abs(Epsilon(maxCurv));
}

Standard_Real LProp_SLProps::MaxCurvature ()
{
  if(!IsCurvatureDefined()) { LProp_NotDefined::Raise(); }
  return maxCurv;
}

Standard_Real LProp_SLProps::MinCurvature ()
{
  if(!IsCurvatureDefined()) { LProp_NotDefined::Raise(); }
  return minCurv;
}

void LProp_SLProps::CurvatureDirections(gp_Dir& Max, gp_Dir& Min)
{

  if(!IsCurvatureDefined()) { LProp_NotDefined::Raise(); }
  Max = dirMaxCurv;
  Min = dirMinCurv;
}

Standard_Real LProp_SLProps::MeanCurvature () {

  if(!IsCurvatureDefined()) { LProp_NotDefined::Raise(); }
  return meanCurv;

}

Standard_Real LProp_SLProps::GaussianCurvature () {

  if(!IsCurvatureDefined()) { LProp_NotDefined::Raise(); }
  return gausCurv;
}