summaryrefslogtreecommitdiff
path: root/src/GeomFill/GeomFill_GuideTrihedronPlan.cxx
blob: fae7cbb8fb360b303e79d42c732be3f6d8880eee (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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// File:	GeomFill_GuideTrihedronPlan.cxx
// Created:	Thu Jul 02 15:39:24 1998
// Author:	Stephanie HUMEAU
//		<shu@sun17>


#include <GeomFill_GuideTrihedronPlan.ixx>

#include <gp_Pnt.hxx>
#include <gp_Pnt2d.hxx>
//#include <gp_Trsf2d.hxx>
//#include <Bnd_Box2d.hxx>
#include <ElCLib.hxx>

#include <Adaptor3d_Curve.hxx>
#include <GeomAdaptor_HCurve.hxx>
#include <GeomAdaptor_HSurface.hxx>

#include <Geom_Plane.hxx>

#include <IntCurveSurface_IntersectionPoint.hxx>
#include <IntCurveSurface_HInter.hxx>

#include <GeomFill_Frenet.hxx>
#include <GeomFill_PlanFunc.hxx>

#include <math_Vector.hxx>

#include <math_FunctionRoot.hxx>
#include <math_Matrix.hxx>

#include <Precision.hxx>

#if DRAW
#include <DrawTrSurf.hxx>
#endif

#if DEB
static void TracePlan(const Handle(Geom_Surface)& Plan)
{
  cout << "Pas d'intersection Guide/Plan" << endl;	
#if DRAW
  char* Temp = "ThePlan" ;
  DrawTrSurf::Set(Temp, Plan);
//  DrawTrSurf::Set("ThePlan", Plan);
#endif 
}
#endif

//==================================================================
//Function: InGoodPeriod
//Purpose : Recadre un paramtere
//==================================================================
static void InGoodPeriod(const Standard_Real Prec,
			 const Standard_Real  Period,
                         Standard_Real& Current)
{
  Standard_Real Diff=Current-Prec;
  Standard_Integer nb = (Standard_Integer ) IntegerPart(Diff/Period);
  Current -= nb*Period;
  Diff = Current-Prec;
  if (Diff > Period/2) Current -= Period;
  else if (Diff < -Period/2) Current += Period;
}

//=======================================================================
//function : GuideTrihedronPlan
//purpose  : Constructor
//=======================================================================
GeomFill_GuideTrihedronPlan::GeomFill_GuideTrihedronPlan (const Handle(Adaptor3d_HCurve)& theGuide) :
							  X(1,1),  
							  XTol(1,1),
							  Inf(1,1), Sup(1,1),
							  myStatus(GeomFill_PipeOk)
{
  myCurve.Nullify();
  myGuide = theGuide; // guide
  myTrimG = theGuide;
  myNbPts = 20;     // nb points pour calculs
  Pole = new (TColgp_HArray2OfPnt2d)(1,1,1,myNbPts);//tab pr stocker Pprime (pt sur guide)
  frenet = new (GeomFill_Frenet)();
  XTol.Init(1.e-6);
  XTol(1) = myGuide->Resolution(1.e-6);
}

//=======================================================================
//function : Init
//purpose  : calcule myNbPts points sur la courbe guide (<=> normale)
//=======================================================================
 void GeomFill_GuideTrihedronPlan::Init()
{
  myStatus = GeomFill_PipeOk;
  gp_Pnt P;
//  Bnd_Box2d Box;
//  Box.Update(-0.1, -0.1, 0.1, 0.1); // Taille minimal
  gp_Vec Tangent,Normal,BiNormal;
  Standard_Integer ii;
  Standard_Real t, DeltaG, w = 0;
  Standard_Real f = myCurve->FirstParameter();
  Standard_Real l = myCurve->LastParameter();
  


  Handle(Geom_Plane) Plan;
  Handle(GeomAdaptor_HSurface) Pl;
  IntCurveSurface_IntersectionPoint PInt;
  IntCurveSurface_HInter Int;
  frenet->SetCurve(myCurve);
  DeltaG = (myGuide->LastParameter() -  myGuide->FirstParameter())/2;
  
  Inf(1) = myGuide->FirstParameter() - DeltaG;
  Sup(1) = myGuide->LastParameter() + DeltaG;

  if (!myGuide->IsPeriodic()) {
    myTrimG = myGuide->Trim(myGuide->FirstParameter()- DeltaG/100, 
			    myGuide->LastParameter() + DeltaG/100, 
			    DeltaG*1.e-7);
  }
  else {
    myTrimG = myGuide; 
  }
//  Standard_Real Step = DeltaG/100;
  DeltaG /= 3;
  for (ii=1; ii<=myNbPts; ii++) 
    {
      t = Standard_Real(myNbPts - ii)*f + Standard_Real(ii - 1)*l;
      t /= (myNbPts-1);
      myCurve->D0(t, P); 
      frenet->D0(t, Tangent, Normal, BiNormal);
      Plan = new (Geom_Plane) (P, Tangent);
      Pl = new(GeomAdaptor_HSurface) (Plan);

      Int.Perform(myTrimG, Pl); // intersection plan / guide 
      if (Int.NbPoints() == 0) {
#if DEB
	TracePlan(Plan);
#endif
        w = (fabs(myGuide->LastParameter() -w) > fabs(myGuide->FirstParameter()-w) ? myGuide->FirstParameter() : myGuide->LastParameter());
                                                                                   
        myStatus = GeomFill_PlaneNotIntersectGuide;
	//return;
      }
      else 
	{
	  gp_Pnt Pmin;
	  PInt = Int.Point(1);
	  Pmin = PInt.Pnt();
	  Standard_Real Dmin = P.Distance(Pmin);
	  for (Standard_Integer jj=2;jj<=Int.NbPoints();jj++)
	    {
	      Pmin = Int.Point(jj).Pnt();
	      if (P.Distance(Pmin) < Dmin) 
		{
		  PInt = Int.Point(jj);
		  Dmin = P.Distance(Pmin);
		}
	    }//for_jj
          
	  w = PInt.W();
        }
      if (ii>1) {
        Standard_Real Diff = w -  Pole->Value(1, ii-1).Y();
        if (Abs(Diff) > DeltaG) {
          if (myGuide->IsPeriodic()) {
            InGoodPeriod (Pole->Value(1, ii-1).Y(),
                          myGuide->Period(), w);
            
            Diff =  w -  Pole->Value(1, ii-1).Y();
          }
        }
        
#if DEB		
        if (Abs(Diff) > DeltaG) {
          cout << "Trihedron Plan Diff on Guide : " << 
            Diff << endl;
        }
#endif
      }
      
      gp_Pnt2d p1(t, w); // on stocke les parametres
      Pole->SetValue(1, ii, p1);
      
    }// for_ii
}

//=======================================================================
//function : SetCurve
//purpose  : calculation of trihedron
//=======================================================================
void GeomFill_GuideTrihedronPlan::SetCurve(const Handle(Adaptor3d_HCurve)& C)
{
  myCurve = C;
  if (!myCurve.IsNull()) Init();
}

//=======================================================================
//function : Guide
//purpose  : calculation of trihedron
//=======================================================================

 Handle(Adaptor3d_HCurve) GeomFill_GuideTrihedronPlan::Guide()const
{
  return myGuide;
}

//=======================================================================
//function : D0
//purpose  : calculation of trihedron
//=======================================================================
 Standard_Boolean GeomFill_GuideTrihedronPlan::D0(const Standard_Real Param,
						  gp_Vec& Tangent,
						  gp_Vec& Normal,
						  gp_Vec& BiNormal) 
{ 
  gp_Pnt P, Pprime;
//  gp_Vec To;

  myCurve->D0(Param, P); 

  frenet->D0(Param,Tangent,Normal,BiNormal);
  
  //initialisation de la recherche
  InitX(Param);
      
  Standard_Integer Iter = 50;
  
  // fonction dont il faut trouver la racine : G(W)-Pl(U,V)=0
  GeomFill_PlanFunc E(P, Tangent, myGuide);
  
  // resolution
  math_FunctionRoot Result(E, X(1), XTol(1), 
			   Inf(1), Sup(1), Iter); 
  
  if (Result.IsDone()) 
    {
      Standard_Real Res = Result.Root();
//      R = Result.Root();    // solution    
   
      Pprime = myTrimG->Value(Res); // pt sur courbe guide 
      gp_Vec n (P, Pprime); // vecteur definissant la normale du triedre 
            
      Normal = n.Normalized();
      BiNormal = Tangent.Crossed(Normal);
      BiNormal.Normalized();   
    }
  else { // Erreur...
#if DEB
    cout << "D0 :";
    // plan ortho a la trajectoire pour determiner Pprime
    Handle(Geom_Plane) Plan = new (Geom_Plane)(P, Tangent);
    TracePlan(Plan);
#endif
    myStatus = GeomFill_PlaneNotIntersectGuide;
    return Standard_False;
  }
  
  return Standard_True;
}

//=======================================================================
//function : D1
//purpose  : calculation of trihedron and first derivative
//=======================================================================
 Standard_Boolean GeomFill_GuideTrihedronPlan::D1(const Standard_Real Param,
						  gp_Vec& Tangent,
						  gp_Vec& DTangent,
						  gp_Vec& Normal,
						  gp_Vec& DNormal,
						  gp_Vec& BiNormal,	   
						  gp_Vec& DBiNormal) 
{ 
//  return Standard_False;
  gp_Pnt P, PG;
  gp_Vec To,TG;


  
  // triedre de frenet sur la trajectoire
  myCurve->D1(Param, P, To);
  frenet->D1(Param,Tangent,DTangent,Normal,DNormal,BiNormal,DBiNormal);

 
  // tolerance sur E
  Standard_Integer Iter = 50;

  // fonction dont il faut trouver la racine : G(W)-Pl(U,V)=0
  InitX(Param);
  GeomFill_PlanFunc E(P, Tangent, myGuide);

  // resolution
  math_FunctionRoot Result(E, X(1), XTol(1), 
			   Inf(1), Sup(1), Iter);

  if (Result.IsDone()) 
    {
      Standard_Real Res =  Result.Root();
//      R = Result.Root();    // solution  
      myTrimG->D1(Res, PG, TG);
      gp_Vec n (P, PG), dn; // vecteur definissant la normale du triedre
      Standard_Real Norm = n.Magnitude();
      if (Norm < 1.e-12) {
	Norm = 1.0;
      }
      n /=Norm;

      
      Normal = n; 
      BiNormal = Tangent.Crossed(Normal);

// derivee premiere du triedre
      Standard_Real dedx, dedt, dtg_dt;
      E.Derivative(Res, dedx);
      E.DEDT(Res, To, DTangent, dedt);
      dtg_dt = -dedt/dedx;


/*      Standard_Real h=1.e-7, e, etg, etc;
      E.Value(Res, e);
      E.Value(Res+h, etg);
      if ( Abs( (etg-e)/h - dedx) > 1.e-4) {
	cout << "err :" <<  (etg-e)/h - dedx << endl;
      }
      gp_Pnt pdbg;
      gp_Vec td, nb, bnb;
      myCurve->D0(Param+h, pdbg);      
      frenet->D0(Param+h,td, nb, bnb);

      GeomFill_PlanFunc Edeb(pdbg, td, myGuide); 
      Edeb.Value(Res, etc);
      if ( Abs( (etc-e)/h - dedt) > 1.e-4) {
	cout << "err :" <<  (etc-e)/h - dedt << endl;
      } */           

      dn.SetLinearForm(dtg_dt, TG, -1, To);
      
      DNormal.SetLinearForm(-(n*dn), n, dn);
      DNormal /= Norm;
      DBiNormal.SetLinearForm(Tangent.Crossed(DNormal),
			      DTangent.Crossed(Normal));
    }
  else {// Erreur...
#if DEB
    cout << "D1 :";
    // plan ortho a la trajectoire
    Handle(Geom_Plane) Plan = new (Geom_Plane)(P, Tangent);
    TracePlan(Plan);
#endif
    myStatus = GeomFill_PlaneNotIntersectGuide;
    return Standard_False;
  }
 
  return Standard_True; 
}


//=======================================================================
//function : D2
//purpose  : calculation of trihedron and derivatives
//=======================================================================
 Standard_Boolean GeomFill_GuideTrihedronPlan::D2(const Standard_Real Param,
						  gp_Vec& Tangent,
						  gp_Vec& DTangent,
						  gp_Vec& D2Tangent,
						  gp_Vec& Normal,
						  gp_Vec& DNormal,
						  gp_Vec& D2Normal,
						  gp_Vec& BiNormal,			  
						  gp_Vec& DBiNormal,		  
						  gp_Vec& D2BiNormal) 
{ 
//  gp_Pnt P, PG;
  gp_Pnt P;
//  gp_Vec To,DTo,TG,DTG;
  gp_Vec To,DTo;

  myCurve->D2(Param, P, To, DTo); 

  // triedre de Frenet sur la trajectoire
  frenet->D2(Param,Tangent,DTangent,D2Tangent,
	     Normal,DNormal,D2Normal,
	     BiNormal,DBiNormal,D2BiNormal);

/*
  // plan ortho a Tangent pour trouver la pt Pprime sur le guide
  Handle(Geom_Plane) Plan = new (Geom_Plane)(P, Tangent); 
  Handle(GeomAdaptor_HSurface) Pl= new(GeomAdaptor_HSurface)(Plan);
  

  Standard_Integer Iter = 50;
  // fonction dont il faut trouver la racine : G(W) - Pl(U,V)=0
  GeomFill_FunctionPipe E(Pl , myGuide);
  InitX(Param);
  
  // resolution
  math_FunctionSetRoot Result(E, X, XTol, 
				    Inf, Sup, Iter); 
  if (Result.IsDone()) 
    {
      math_Vector R(1,3); 
      R = Result.Root();    // solution 
      myTrimG->D2(R(1), PG, TG, DTG); 

      gp_Vec n (P, PG); // vecteur definissant la normale du triedre
      Standard_Real Norm = n.Magnitude();
      n /= Norm;      
      Normal = n.Normalized(); 
      BiNormal = Tangent.Crossed(Normal);

 

   // derivee premiere du triedre 
      Standard_Real dtp_dt;
      dtp_dt = (To*Tangent - Norm*(n*DTangent))/(Tangent*TG);
      gp_Vec dn, d2n;
      dn.SetLinearForm(dtp_dt, TG, -1,  To);
      
      DNormal.SetLinearForm(-(n*dn), n, dn);
      DNormal /= Norm;  
      DBiNormal = Tangent.Crossed(DNormal) + DTangent.Crossed(Normal);
  
    // derivee seconde du triedre
      Standard_Real d2tp_dt2;
      d2tp_dt2 = (DTo*Tangent+To*DTangent - dn*DTangent-Norm*n*D2Tangent)/(TG*Tangent)
	- (To*Tangent-Norm*n*DTangent) * (DTG*dtp_dt*Tangent+TG*DTangent)
	  / ((TG*Tangent)*(TG*Tangent));


      d2n.SetLinearForm(dtp_dt*dtp_dt, DTG, d2tp_dt2, TG, -DTo);
      dn/=Norm;
      d2n/=Norm;

      D2Normal.SetLinearForm(3*Pow(n*dn,2)- (dn.SquareMagnitude() + n*d2n), n,
			     -2*(n*dn), dn,
			     d2n);
 
      D2BiNormal.SetLinearForm(1, D2Tangent.Crossed(Normal),
			       2, DTangent.Crossed(DNormal), 
			       Tangent.Crossed(D2Normal));
    }
  else {// Erreur...
#if DEB
    cout << "D2 :";
    TracePlan(Plan);
#endif
    myStatus = GeomFill_PlaneNotIntersectGuide;
    return Standard_False;
   }
*/
//  return Standard_True;
 return Standard_False;
}


//=======================================================================
//function : Copy
//purpose  : 
//=======================================================================
 Handle(GeomFill_TrihedronLaw) GeomFill_GuideTrihedronPlan::Copy() const
{
 Handle(GeomFill_GuideTrihedronPlan) copy = 
   new (GeomFill_GuideTrihedronPlan) (myGuide);
 copy->SetCurve(myCurve);
 return copy;
}

//=======================================================================
//function : ErrorStatus
//purpose  : 
//=======================================================================
 GeomFill_PipeError GeomFill_GuideTrihedronPlan::ErrorStatus() const
{
  return myStatus;
}
 

//=======================================================================
//function : NbIntervals
//purpose  : Version provisoire : Il faut tenir compte du guide
//=======================================================================
 Standard_Integer GeomFill_GuideTrihedronPlan::NbIntervals(const GeomAbs_Shape S)const
{
  Standard_Integer Nb;
  GeomAbs_Shape tmpS;
  switch (S) {
  case GeomAbs_C0: tmpS = GeomAbs_C1; break;
  case GeomAbs_C1: tmpS = GeomAbs_C2; break;
  case GeomAbs_C2: tmpS = GeomAbs_C3; break;
  default: tmpS = GeomAbs_CN;
  }  

  Nb = myCurve->NbIntervals(tmpS);
  return Nb;
}
//======================================================================
//function :Intervals
//purpose  : 
//=======================================================================
 void GeomFill_GuideTrihedronPlan::Intervals(TColStd_Array1OfReal& TT,
					    const GeomAbs_Shape S) const
{
  GeomAbs_Shape tmpS;
  switch (S) {
  case GeomAbs_C0: tmpS = GeomAbs_C1; break;
  case GeomAbs_C1: tmpS = GeomAbs_C2; break;
  case GeomAbs_C2: tmpS = GeomAbs_C3; break;
  default: tmpS = GeomAbs_CN;
  }
  myCurve->Intervals(TT, tmpS);
}

//======================================================================
//function :SetInterval
//purpose  : 
//=======================================================================
void GeomFill_GuideTrihedronPlan::SetInterval(const Standard_Real First,
					      const Standard_Real Last) 
{
  myTrimmed = myCurve->Trim(First, Last, Precision::Confusion());  
}


//=======================================================================
//function : GetAverageLaw
//purpose  : 
//=======================================================================
 void GeomFill_GuideTrihedronPlan::GetAverageLaw(gp_Vec& ATangent,
				    gp_Vec& ANormal,
				    gp_Vec& ABiNormal) 
{
  Standard_Integer ii;
  Standard_Real t, Delta = (myCurve->LastParameter() - 
			    myCurve->FirstParameter())/20.001;

  ATangent.SetCoord(0.,0.,0.);
  ANormal.SetCoord(0.,0.,0.);
  ABiNormal.SetCoord(0.,0.,0.);
  gp_Vec T, N, B;
  
  for (ii=1; ii<=20; ii++) {
    t = myCurve->FirstParameter() +(ii-1)*Delta;
    D0(t, T, N, B);
    ATangent +=T;
    ANormal  +=N;
    ABiNormal+=B;
  }
  ATangent  /= 20;
  ANormal   /= 20;
  ABiNormal /= 20; 
}

//=======================================================================
//function : IsConstant
//purpose  : 
//=======================================================================
 Standard_Boolean GeomFill_GuideTrihedronPlan::IsConstant() const
{
  if ((myCurve->GetType() == GeomAbs_Line) &&
      (myGuide->GetType() == GeomAbs_Line)) {
    Standard_Real Angle;
    Angle = myCurve->Line().Angle(myGuide->Line());
    if ((Angle<1.e-12) || ((2*PI-Angle)<1.e-12) )
      return Standard_True;
  }

  return Standard_False;
}

//=======================================================================
//function : IsOnlyBy3dCurve
//purpose  : 
//=======================================================================
 Standard_Boolean GeomFill_GuideTrihedronPlan::IsOnlyBy3dCurve() const
{
  return Standard_False;
}

//=======================================================================
//function : Origine
//purpose  : Nothing!!
//=======================================================================
void  GeomFill_GuideTrihedronPlan::Origine(const Standard_Real ,
					   const Standard_Real  )
{
}

//==================================================================
//Function : InitX
//Purpose : recherche par interpolation d'une valeur initiale
//==================================================================
void GeomFill_GuideTrihedronPlan::InitX(const Standard_Real Param)
{

  Standard_Integer Ideb = 1, Ifin =  Pole->RowLength(), Idemi;
  Standard_Real Valeur, t1, t2;

  
  Valeur = Pole->Value(1, Ideb).X();
  if (Param == Valeur) {
    Ifin = Ideb+1; 
  }
  
  Valeur =  Pole->Value(1, Ifin).X();
  if (Param == Valeur) {
    Ideb = Ifin-1; 
  } 
  
  while ( Ideb+1 != Ifin) {
    Idemi = (Ideb+Ifin)/2;
    Valeur = Pole->Value(1, Idemi).X();
    if (Valeur < Param) {
      Ideb = Idemi;
    }
    else { 
      if ( Valeur > Param) { Ifin = Idemi;}
      else { 
	Ideb = Idemi;		     
	Ifin = Ideb+1;
      }
    }
  }

  t1 = Pole->Value(1,Ideb).X();
  t2 = Pole->Value(1,Ifin).X();
  Standard_Real diff = t2-t1;
  if (diff > 1.e-7) {
    Standard_Real b = (Param-t1) / diff,
    a = (t2-Param) / diff;

    X(1) = Pole->Value(1,Ideb).Coord(2) * a 
      + Pole->Value(1,Ifin).Coord(2) * b; //param guide 
  }
  else {
    X(1) = (Pole->Value(1, Ideb).Coord(2) + 
	    Pole->Value(1, Ifin).Coord(2)) / 2;
  }
  if (myGuide->IsPeriodic()) {
    X(1) = ElCLib::InPeriod(X(1), myGuide->FirstParameter(), 
			          myGuide->LastParameter());
  }
}