summaryrefslogtreecommitdiff
path: root/src/GeomFill/GeomFill_SectionPlacement.cxx
blob: f55a8c2af291b3cc28c06295fa037a6277abdf15 (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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
// File:	GeomFill_SectionPlacement.cxx
// Created:	Mon Dec 15 17:09:47 1997
// Author:	Philippe MANGIN
//		<pmn@sgi29>


#include <GeomFill_SectionPlacement.ixx>

#include <GeomLib.hxx>
#include <Geom_Plane.hxx>
#include <GeomLProp_CLProps.hxx>
#include <GeomAbs_CurveType.hxx> 
#include <GeomAdaptor_HSurface.hxx>

#include <gp_Ax3.hxx>
#include <gp_Ax2.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <gp_Trsf.hxx>
#include <TColgp_HArray1OfPnt.hxx>
#include <Bnd_Box.hxx>
#include <BndLib_Add3dCurve.hxx>

#include <Precision.hxx>
#include <gp.hxx>
#include <Extrema_ExtCC.hxx>
#include <Extrema_POnCurv.hxx>
#include <IntCurveSurface_HInter.hxx>
#include <IntCurveSurface_IntersectionPoint.hxx>

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

#include <Geom_CartesianPoint.hxx>


//===============================================================
// Function :
// Purpose :
//===============================================================
static void Tangente(const Adaptor3d_Curve& Path,
		     const Standard_Real Param,
		     gp_Pnt& P,
		     gp_Vec& Tang)
{
  Path.D1(Param, P, Tang);
  Standard_Real Norm = Tang.Magnitude();

  for (Standard_Integer ii=2; (ii<12) && (Norm < Precision::Confusion()); 
       ii++) {
    Tang =  Path.DN(Param, ii);
    Norm = Tang.Magnitude();
  }

  if (Norm > 100*gp::Resolution()) Tang /= Norm; 
}
                   

static Standard_Real Penalite(const Standard_Real angle,
			      const Standard_Real dist)
{
  Standard_Real penal;
  
  if (dist < 1)
    penal = Sqrt(dist);
  else if (dist <2)
    penal = Pow(dist, 2);
  else
    penal = dist + 2;

  if (angle > 1.e-3) {
    penal += 1./angle -2./PI;
  }
  else {
    penal += 1.e3;
  }

  return penal;
}

static Standard_Real EvalAngle(const gp_Vec& V1, 
			       const gp_Vec& V2)
{
 Standard_Real angle;
 angle = V1.Angle(V2);
 if (angle > PI/2) angle = PI - angle;
 return angle;
}

static Standard_Integer NbSamples(const Handle(Geom_Curve)& aCurve)
{
  Standard_Real nbs = 100.; //on default

  Handle(Geom_Curve) theCurve = aCurve;
  if (aCurve->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
    theCurve = (Handle(Geom_TrimmedCurve)::DownCast(aCurve))->BasisCurve();

  if (theCurve->IsInstance(STANDARD_TYPE(Geom_Line)))
    nbs = 1;
  else if (theCurve->IsKind(STANDARD_TYPE(Geom_Conic)))
    nbs = 4;
  else if (theCurve->IsInstance(STANDARD_TYPE(Geom_BezierCurve)))
    {
      Handle(Geom_BezierCurve) BC = Handle(Geom_BezierCurve)::DownCast(theCurve);
      nbs = 3 + BC->NbPoles();
    }
  else if (theCurve->IsInstance(STANDARD_TYPE(Geom_BSplineCurve)))
    {
      Handle(Geom_BSplineCurve) BC = Handle(Geom_BSplineCurve)::DownCast(theCurve);
      nbs  = BC->NbKnots();
      nbs *= BC->Degree();
      Standard_Real ratio =
	(aCurve->LastParameter() - aCurve->FirstParameter())/(BC->LastParameter() - BC->FirstParameter());
      nbs *= ratio;
      if(nbs < 4.0)
	nbs = 4;
    }

  if (nbs > 300.)
    nbs = 300;

  return ((Standard_Integer)nbs);
}

//===============================================================
// Function :DistMini
// Purpose : Examine un extrema pour updater <Dist> & <Param>
//===============================================================
static void DistMini(const Extrema_ExtPC& Ext,
		     const Adaptor3d_Curve& C,
		     Standard_Real& Dist,
		     Standard_Real& Param)
{
  Standard_Real dist1, dist2;
  Standard_Integer ii;
  gp_Pnt P1, P2;
  Standard_Real Dist2 = RealLast();

  Ext.TrimmedSquareDistances(dist1, dist2, P1, P2);
  if ( (dist1<Dist2) || (dist2<Dist2) ) {
    if (dist1 < dist2) {
      Dist2 = dist1;
      Param  = C.FirstParameter();
    }
    else {
      Dist2 = dist2;
      Param  = C.LastParameter();
    }
  }

  if (Ext.IsDone())
    {
      for (ii=1; ii<= Ext.NbExt(); ii++) {
	if (Ext.SquareDistance(ii) < Dist2) {
	  Dist2 = Ext.SquareDistance(ii);
	  Param  =  Ext.Point(ii).Parameter();
	}
      }
    }
  Dist = sqrt (Dist2);
}
		     

//===============================================================
// Function : Constructeur
// Purpose :
//===============================================================
GeomFill_SectionPlacement::
GeomFill_SectionPlacement(const Handle(GeomFill_LocationLaw)& L,
			  const Handle(Geom_Geometry)& Section) :
			  myLaw(L), /* myAdpSection(Section),  mySection(Section), */
			  Dist(RealLast()), AngleMax(0.)
{

  done   = Standard_False;
  isplan = Standard_False;
  myIsPoint = Standard_False;

  if (Section->IsInstance(STANDARD_TYPE(Geom_CartesianPoint)))
    {
      myIsPoint = Standard_True;
      myPoint   = Handle(Geom_CartesianPoint)::DownCast(Section)->Pnt();
      isplan    = Standard_True;
    }
  else
    {
      Handle(Geom_Curve) CurveSection = Handle(Geom_Curve)::DownCast(Section);
      myAdpSection.Load(CurveSection);
      mySection = CurveSection;
    }

  Standard_Integer i, j, NbPoles=0;
  Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;

  // Boite d'encombrement de la section pour en deduire le gabarit
  Bnd_Box box;
  if (myIsPoint)
    box.Add(myPoint);
  else
    BndLib_Add3dCurve::Add(myAdpSection, 1.e-4, box);
  box.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);

  Standard_Real DX = aXmax - aXmin ;
  Standard_Real DY = aYmax - aYmin ;
  Standard_Real DZ = aZmax - aZmin ;
  Gabarit = Sqrt( DX*DX+ DY*DY + DZ*DZ )/2. ;

  Gabarit += Precision::Confusion(); // Cas des toute petite
  

  // Initialisation de TheAxe pour les cas singulier
  if (!myIsPoint)
    {
      gp_Pnt P;
      gp_Vec V;
      Tangente(myAdpSection, 
	       (myAdpSection.FirstParameter()+myAdpSection.LastParameter())/2,
	       P, V);
      TheAxe.SetLocation(P);
      TheAxe.SetDirection(V);
 
      // y a t'il un Plan moyen ?
      GeomAbs_CurveType TheType = myAdpSection.GetType();
      switch  (TheType) {
      case GeomAbs_Circle:
	{
	  isplan = Standard_True;
	  TheAxe =  myAdpSection.Circle().Axis();
	  break;
	}
      case GeomAbs_Ellipse:
	{
	  isplan = Standard_True;
	  TheAxe =  myAdpSection.Ellipse().Axis();
	  break;
	}
      case GeomAbs_Hyperbola:
	{
	  isplan = Standard_True;
	  TheAxe =  myAdpSection.Hyperbola().Axis();
	  break;
	}
      case GeomAbs_Parabola:
	{
	  isplan = Standard_True;
	  TheAxe =  myAdpSection.Parabola().Axis();
	  break;
	}
      case GeomAbs_Line:
	{
	  NbPoles = 0; // Pas de Plan !!
	  break;
	}
      case GeomAbs_BezierCurve:
      case GeomAbs_BSplineCurve:
	{
	  NbPoles = myAdpSection.NbPoles();
	  break;
	}
      default:
	NbPoles = 21;
      }
      
      
      if (!isplan && NbPoles>2)
	{
	  // Calcul d'un plan moyen.
	  Handle(TColgp_HArray1OfPnt) Pnts;
	  Standard_Real first = myAdpSection.FirstParameter();
	  Standard_Real last =  myAdpSection.LastParameter();
	  Standard_Real t, delta;
	  if (myAdpSection.GetType() == GeomAbs_BSplineCurve)
	    {
	      Handle(Geom_BSplineCurve) BC =
		Handle(Geom_BSplineCurve)::DownCast(myAdpSection.Curve());
	      Standard_Integer I1, I2, I3, I4;
	      BC->LocateU( first, Precision::Confusion(), I1, I2 );
	      BC->LocateU( last,  Precision::Confusion(), I3, I4 );
	      Standard_Integer NbKnots = I3 - I2 + 1;
	      
	      Standard_Integer NbLocalPnts = 10;
	      Standard_Integer NbPnts = (NbKnots-1) * NbLocalPnts;
	      if (I1 != I2)
		NbPnts += NbLocalPnts;
	      if (I3 != I4 && first < BC->Knot(I3))
		NbPnts += NbLocalPnts;
	      if (!myAdpSection.IsClosed())
		NbPnts++;
	      Pnts = new TColgp_HArray1OfPnt(1, NbPnts);
	      Standard_Integer nb = 1;
	      if (I1 != I2)
		{
		  Standard_Real locallast = (BC->Knot(I2) < last)? BC->Knot(I2) : last;
		  delta = (locallast - first) / NbLocalPnts;
		  for (j = 0; j < NbLocalPnts; j++)
		    {
		      t = first + j*delta;
		      Pnts->SetValue( nb++, myAdpSection.Value(t) );
		    }
		}
	      for (i = I2; i < I3; i++)
		{
		  t = BC->Knot(i);
		  delta = (BC->Knot(i+1) - t) / NbLocalPnts;
		  for (j = 0; j < NbLocalPnts; j++)
		    {
		      t += delta;
		      Pnts->SetValue( nb++, myAdpSection.Value(t) );
		    }
		}
	      if (I3 != I4 && first < BC->Knot(I3))
		{
		  t = BC->Knot(I3);
		  delta = (last - t) / NbLocalPnts;
		  for (j = 0; j < NbLocalPnts; j++)
		    {
		      t += delta;
		      Pnts->SetValue( nb++, myAdpSection.Value(t) );
		    }
		}
	      if (!myAdpSection.IsClosed())
		Pnts->SetValue( nb, myAdpSection.Value(last) );
	    }
	  else // other type
	    {
	      Standard_Integer NbPnts = NbPoles-1;
	      if (!myAdpSection.IsClosed())
		NbPnts++;
	      Pnts = new TColgp_HArray1OfPnt(1, NbPnts);
	      delta = (last - first) / (NbPoles-1);
	      for (i = 0; i < NbPoles-1; i++)
		{
		  t = first + i*delta;
		  Pnts->SetValue( i+1, myAdpSection.Value(t) );
		}
	      if (!myAdpSection.IsClosed())
		Pnts->SetValue( NbPnts, myAdpSection.Value(last) );
	    }
	  
	  Standard_Boolean issing;
	  gp_Ax2 axe;
	  GeomLib::AxeOfInertia(Pnts->Array1(), axe, issing, Precision::Confusion());
	  if (!issing) {
	    isplan = Standard_True;
	    TheAxe.SetLocation(axe.Location());
	    TheAxe.SetDirection(axe.Direction());
	  }
	}
      
      
      myExt.Initialize(myAdpSection, 
		       myAdpSection.FirstParameter(), 
		       myAdpSection.LastParameter(), 
		       Precision::Confusion());
    }
}

//===============================================================
// Function :SetLocation
// Purpose :
//===============================================================
void GeomFill_SectionPlacement::
SetLocation(const Handle(GeomFill_LocationLaw)& L) 
{
  myLaw = L;
}

//===============================================================
// Function : Perform
// Purpose : Le plus simple
//===============================================================
void GeomFill_SectionPlacement::Perform(const Standard_Real Tol) 
{
  Handle(Adaptor3d_HCurve) Path;
  Path = myLaw->GetCurve();
  Perform(Path, Tol);
}

//===============================================================
// Function :Perform
// Purpose : Recherche automatique
//===============================================================
void GeomFill_SectionPlacement::Perform(const Handle(Adaptor3d_HCurve)& Path,
					const Standard_Real Tol) 
{
  Standard_Real IntTol = 1.e-5;
  Standard_Real DistCenter = Precision::Infinite();

  if (myIsPoint)
    {
      Extrema_ExtPC Projector(myPoint, Path->Curve(), Precision::Confusion());
      DistMini( Projector, Path->Curve(), Dist, PathParam );
      AngleMax = PI/2;
    }
  else
    {
      PathParam = Path->FirstParameter();
      SecParam =  myAdpSection.FirstParameter();
      
      Standard_Real distaux, taux, alpha;
      gp_Pnt PonPath, PonSec, P;
      gp_Vec VRef, dp1;
      VRef.SetXYZ(TheAxe.Direction().XYZ());
      
      Tangente( Path->Curve(), PathParam, PonPath, dp1);
      PonSec = myAdpSection.Value(SecParam);
      Dist =  PonPath.Distance(PonSec);
      if (Dist > Tol) { // On Cherche un meilleur point sur la section
	myExt.Perform(PonPath);  
	if ( myExt.IsDone() ) { 
	  DistMini(myExt, myAdpSection, Dist, SecParam);
	  PonSec = myAdpSection.Value(SecParam);
	}
      }
      AngleMax = EvalAngle(VRef, dp1);
      if (isplan) AngleMax = PI/2 - AngleMax;
      
      Standard_Boolean Trouve = Standard_False; 
      Standard_Integer ii;
      
      if (isplan) {
	// (1.1) Distances Point-Plan
	Standard_Real DistPlan;
	gp_Vec V1 (PonPath, TheAxe.Location());
	DistPlan = Abs(V1.Dot(VRef));
	if (DistPlan <= IntTol)
	  DistCenter = V1.Magnitude();
	
	gp_Pnt Plast = Path->Value( Path->LastParameter() );
	V1.SetXYZ( TheAxe.Location().XYZ() - Plast.XYZ() );
	DistPlan = Abs(V1.Dot(VRef));
	if (DistPlan <= IntTol)
	  {
	    Standard_Real aDist = V1.Magnitude();
	    if (aDist < DistCenter)
	      {
		DistCenter = aDist;
		PonPath = Plast;
		PathParam = Path->LastParameter();
	      }
	  }
	
	// (1.2) Intersection Plan-courbe
	gp_Ax3 axe (TheAxe.Location(), TheAxe.Direction());
	Handle(Geom_Plane) plan = new (Geom_Plane)(axe);
	Handle(GeomAdaptor_HSurface) adplan = 
	  new (GeomAdaptor_HSurface)(plan);
	IntCurveSurface_HInter Intersector;
	Intersector.Perform(Path, adplan);
	if (Intersector.IsDone())
	  {
	    Standard_Real w;
	    gp_Pnt P;
	    Standard_Real aDist;
	    for (ii=1; ii<=Intersector.NbPoints(); ii++)
	      {
		w = Intersector.Point(ii).W();
		P = Path->Value( w );
		aDist = P.Distance( TheAxe.Location() );
		if (aDist < DistCenter)
		  {
		    DistCenter = aDist;
		    PonPath = P;
		    PathParam = w;
		  }
	      }
	  }
	if (!Intersector.IsDone() || Intersector.NbPoints() == 0)
	  {
	    Standard_Integer NbPnts = NbSamples( mySection );
	    TColgp_Array1OfPnt Pnts( 1, NbPnts+1 );
	    Standard_Real delta = (mySection->LastParameter()-mySection->FirstParameter())/NbPnts;
	    for (ii = 0; ii <= NbPnts; ii++)
	      Pnts(ii+1) = mySection->Value( mySection->FirstParameter() + ii*delta );
	    
	    gp_Pnt BaryCenter;
	    gp_Dir Xdir, Ydir;
	    Standard_Real Xgap, Ygap, Zgap;
	    GeomLib::Inertia( Pnts, BaryCenter, Xdir, Ydir, Xgap, Ygap, Zgap );
	    
	    gp_Pnt Pfirst = Path->Value( Path->FirstParameter() );
	    if (Pfirst.Distance(BaryCenter) < Plast.Distance(BaryCenter))
	      PathParam = Path->FirstParameter();
	    else
	      {
		PathParam = Path->LastParameter();
		Tangente( Path->Curve(), PathParam, PonPath, dp1);
		PonSec = myAdpSection.Value(SecParam);
		Dist =  PonPath.Distance(PonSec);
		if (Dist > Tol) { // On Cherche un meilleur point sur la section
		  myExt.Perform(PonPath);  
		  if ( myExt.IsDone() ) { 
		    DistMini(myExt, myAdpSection, Dist, SecParam);
		    PonSec = myAdpSection.Value(SecParam);
		  }
		}
		AngleMax = EvalAngle(VRef, dp1);
		AngleMax = PI/2 - AngleMax;
	      }
	  }
    
	/*
	   // (1.1) Distances Point-Plan
	   Standard_Real DistPlan;
	   gp_Vec V1 (PonPath, TheAxe.Location());
	   DistPlan = Abs(V1.Dot(VRef));
	   
	   // On examine l'autre extremite
	   gp_Pnt P;
	   Tangente(Path->Curve(), Path->LastParameter(), P, dp1);
	   V1.SetXYZ(TheAxe.Location().XYZ()-P.XYZ());
	   if  (Abs(V1.Dot(VRef)) <= DistPlan ) { // On prend l'autre extremite
	   alpha =  PI/2 - EvalAngle(VRef, dp1);
	   distaux =  PonPath.Distance(PonSec);
	   if (distaux > Tol) {
	   myExt.Perform(P);  
	   if ( myExt.IsDone() ) 
	   DistMini(myExt, myAdpSection, distaux, taux);
	   }
	   else 
	   taux = SecParam;
	   
	   if (Choix(distaux, alpha)) {
	   Dist = distaux;
	   AngleMax = alpha;
	   PonPath = P;
	   PathParam = Path->LastParameter();
	   }
	   }
	   
	   // (1.2) Intersection Plan-courbe
	   gp_Ax3 axe (TheAxe.Location(), TheAxe.Direction());
	   Handle(Geom_Plane) plan = new (Geom_Plane)(axe);
	   Handle(GeomAdaptor_HSurface) adplan = 
	   new (GeomAdaptor_HSurface)(plan);
	   IntCurveSurface_HInter Intersector;
	   Intersector.Perform(Path, adplan);
	   if (Intersector.IsDone()) {
	   Standard_Real w;
	   gp_Vec V;
	   for (ii=1; ii<=Intersector.NbPoints(); ii++) {
	   w = Intersector.Point(ii).W();
	   //(1.3) test d'angle
	   Tangente( Path->Curve(), w, P, V);
	   alpha = PI/2 - EvalAngle(V, VRef);
	   //(1.4) Test de distance Point-Courbe
	   myExt.Perform(P);  
	   if ( myExt.IsDone() ) {
	   DistMini(myExt, myAdpSection, distaux, taux);
	   if (Choix(distaux, alpha)) {
	   Dist = distaux;
	   SecParam  = taux;
	   AngleMax = alpha;
	   PonPath = P;
	   PathParam = w;
	   PonSec = myAdpSection.Value(SecParam);
	   }
	   }
	   else {
	   distaux = P.Distance(PonSec);
	   if (Choix(distaux, alpha)) {
	   Dist = distaux;
	   AngleMax = alpha;
	   PonPath = P;
	   PathParam = w;
	   }
	   }
	   }
	   }
    */
#if DEB
	if (Intersector.NbPoints() == 0) {
	  Intersector.Dump();
	} 
#endif
      } 
      
      // Cas General
      if (! isplan) {
	// (2.1) Distance avec les extremites ...
	myExt.Perform(PonPath);  
	if ( myExt.IsDone() ) {
	  DistMini(myExt, myAdpSection, distaux, taux);
	  if (distaux < Dist) {
	    Dist = distaux;
	    SecParam  = taux;
	  } 
	}
	Trouve = (Dist <= Tol);
	if (!Trouve) {
	  Tangente( Path->Curve(), Path->LastParameter(), P, dp1);
	  alpha = EvalAngle(VRef, dp1);
	  myExt.Perform(P);     
	  if ( myExt.IsDone() ) {
	    if ( myExt.IsDone() ) {
	      DistMini(myExt, myAdpSection, distaux, taux);
	      if (Choix(distaux, alpha)) {
		Dist = distaux;
		SecParam  = taux;
		AngleMax = alpha;
		PonPath = P;
		PathParam = Path->LastParameter();
	      }
	    }
	  }
	  Trouve = (Dist <= Tol); 
	}
	
	// (2.2) Distance courbe-courbe
	if (!Trouve) {
	  Extrema_ExtCC Ext(Path->Curve(), myAdpSection, 
			    Path->FirstParameter(), Path->LastParameter(),
			    myAdpSection.FirstParameter(), 
			    myAdpSection.LastParameter(),
			    Path->Resolution(Tol/100), 
			    myAdpSection.Resolution(Tol/100));
	  if (Ext.IsDone()) {
	    Extrema_POnCurv P1, P2;
	    for (ii=1; ii<=Ext.NbExt(); ii++) {
	      distaux = sqrt (Ext.SquareDistance(ii));
	      Ext.Points(ii, P1, P2);
	      Tangente(Path->Curve(), P1.Parameter(), P, dp1);
	      alpha =  EvalAngle(VRef, dp1);
	      if (Choix(distaux, alpha)) {
		Trouve = Standard_True;
		Dist = distaux;
		PathParam = P1.Parameter();
		SecParam = P2.Parameter();
		PonSec =  P2.Value();
		PonPath = P;
		AngleMax = alpha;
	      }
	    }
	  }
	  if (!Trouve){ 
	    // Si l'on a toujours rien, on essai une distance point/path
	    // c'est la derniere chance.
	    Extrema_ExtPC PExt;
	    PExt.Initialize(Path->Curve(), 
			    Path->FirstParameter(),  
			    Path->LastParameter(),
			    Precision::Confusion());
	    PExt.Perform(PonSec);
	    if ( PExt.IsDone() ) {
	      // modified for OCC13595  Tue Oct 17 15:00:08 2006.BEGIN
	      // 	      DistMini(PExt, myAdpSection, distaux, taux);
	      DistMini(PExt, Path->Curve(), distaux, taux);
	      // modified for OCC13595  Tue Oct 17 15:00:11 2006.END
	      Tangente(Path->Curve(), taux, P, dp1);
	      alpha = EvalAngle(VRef, dp1);
	      if (Choix(distaux, alpha)) {
		Dist = distaux;
		PonPath = P;
		AngleMax = alpha;
		PathParam = taux;
	      }
	    }
	  }
	}
      }
    }

  done = Standard_True;
}


//===============================================================
// Function : Perform
// Purpose : Calcul le placement pour un parametre donne.
//===============================================================
void GeomFill_SectionPlacement::Perform(const Standard_Real Param,
					const Standard_Real Tol) 
{
  done = Standard_True;
  Handle(Adaptor3d_HCurve) Path;
  Path = myLaw->GetCurve();

  PathParam = Param;
  if (myIsPoint)
    {
      gp_Pnt PonPath = Path->Value( PathParam );
      Dist = PonPath.Distance(myPoint);
      AngleMax = PI/2;
    }
  else
    {
      SecParam =  myAdpSection.FirstParameter();
      
      //  Standard_Real distaux, taux, alpha;
      //  gp_Pnt PonPath, PonSec, P;
      gp_Pnt PonPath, PonSec;
      gp_Vec VRef, dp1;
      VRef.SetXYZ(TheAxe.Direction().XYZ()); 
      
      Tangente( Path->Curve(), PathParam, PonPath, dp1);
      PonSec = myAdpSection.Value(SecParam);
      Dist =  PonPath.Distance(PonSec);
      if (Dist > Tol) { // On Cherche un meilleur point sur la section
	myExt.Perform(PonPath);  
	if ( myExt.IsDone() ) { 
	  DistMini(myExt, myAdpSection, Dist, SecParam);
	  PonSec = myAdpSection.Value(SecParam);
	}
      }
      AngleMax = EvalAngle(VRef, dp1);
      if (isplan) AngleMax = PI/2 - AngleMax;
    }

  done = Standard_True;
}

//===============================================================
// Function :IsDone
// Purpose :
//===============================================================
 Standard_Boolean GeomFill_SectionPlacement::IsDone() const
{
  return done; 
}

//===============================================================
// Function : ParameterOnPath
// Purpose :
//===============================================================
 Standard_Real GeomFill_SectionPlacement::ParameterOnPath() const
{
  return PathParam;
}

//===============================================================
// Function : ParameterOnSection
// Purpose :
//===============================================================
 Standard_Real GeomFill_SectionPlacement::ParameterOnSection() const
{
 return SecParam;
}

//===============================================================
// Function : Distance
// Purpose :
//===============================================================
 Standard_Real GeomFill_SectionPlacement::Distance() const
{
  return Dist;
}

//===============================================================
// Function : Angle
// Purpose :
//===============================================================
 Standard_Real GeomFill_SectionPlacement::Angle() const
{
  return AngleMax;
}

//===============================================================
// Function : Transformation
// Purpose :
//===============================================================
 gp_Trsf GeomFill_SectionPlacement::Transformation(
         const Standard_Boolean WithTranslation,
	 const Standard_Boolean WithCorrection) const
{
  gp_Vec V;
  gp_Mat M;
  gp_Dir DT, DN, D;
// modified by NIZHNY-MKK  Fri Oct 17 15:27:07 2003
  gp_Pnt P(0., 0., 0.), PSection(0., 0., 0.);

  // Calcul des reperes
  myLaw->D0(PathParam, M, V);

  P.SetXYZ(V.XYZ());
  D.SetXYZ (M.Column(3));
  DN.SetXYZ(M.Column(1));
  gp_Ax3 Paxe(P, D, DN);


  if (WithTranslation || WithCorrection) {
    if (myIsPoint)
      PSection = myPoint;
    else
      PSection = mySection->Value(SecParam);   
  }
  // modified by NIZHNY-MKK  Wed Oct  8 15:03:19 2003.BEGIN
  gp_Trsf Rot;

  if (WithCorrection && !myIsPoint) { 
    Standard_Real angle;
    
    if (!isplan)
      Standard_Failure::Raise("Illegal usage: can't rotate non-planar profile");
    
    gp_Dir ProfileNormal = TheAxe.Direction();
    gp_Dir SpineStartDir = Paxe.Direction();

    if (! ProfileNormal.IsParallel( SpineStartDir, Precision::Angular() )) {
      gp_Dir DirAxeOfRotation = ProfileNormal ^ SpineStartDir;
      angle = ProfileNormal.AngleWithRef( SpineStartDir, DirAxeOfRotation );
      gp_Ax1 AxeOfRotation( TheAxe.Location(), DirAxeOfRotation );
      Rot.SetRotation( AxeOfRotation, angle );
    }
    PSection.Transform(Rot);
  }
  // modified by NIZHNY-MKK  Wed Oct  8 15:03:21 2003.END

  if (WithTranslation) { 
    P.ChangeCoord().SetLinearForm(-1,PSection.XYZ(), 
				  V.XYZ());
  }
  else {
    P.SetCoord(0., 0., 0.);
  }

  gp_Ax3 Saxe(P, gp::DZ(), gp::DX());

  // Transfo
  gp_Trsf Tf;
  Tf.SetTransformation(Saxe, Paxe);

  if (WithCorrection) {
    // modified by NIZHNY-MKK  Fri Oct 17 15:26:36 2003.BEGIN
    //     Standard_Real angle;
    //     gp_Trsf Rot;
    
    //     if (!isplan)
    //       Standard_Failure::Raise("Illegal usage: can't rotate non-planar profile");
    
    //     gp_Dir ProfileNormal = TheAxe.Direction();
    //     gp_Dir SpineStartDir = Paxe.Direction();
    //     if (! ProfileNormal.IsParallel( SpineStartDir, Precision::Angular() ))
    //       {
    // 	gp_Dir DirAxeOfRotation = ProfileNormal ^ SpineStartDir;
    // 	angle = ProfileNormal.AngleWithRef( SpineStartDir, DirAxeOfRotation );
    // 	gp_Ax1 AxeOfRotation( TheAxe.Location(), DirAxeOfRotation );
    // 	Rot.SetRotation( AxeOfRotation, angle );
    //       }
    // modified by NIZHNY-MKK  Fri Oct 17 15:26:42 2003.END

    Tf *= Rot;
  }

  return Tf;
}

//===============================================================
// Function : Section
// Purpose :
//===============================================================
 Handle(Geom_Curve) GeomFill_SectionPlacement::
 Section(const Standard_Boolean WithTranslation) const
{
  Handle(Geom_Curve) TheSection =  
    Handle(Geom_Curve)::DownCast(mySection->Copy());
  TheSection->Transform(Transformation(WithTranslation, Standard_False));
  return TheSection;
}


//===============================================================
// Function :
// Purpose :
//===============================================================
 Handle(Geom_Curve) GeomFill_SectionPlacement::
 ModifiedSection(const Standard_Boolean WithTranslation) const
{
  Handle(Geom_Curve) TheSection =  
    Handle(Geom_Curve)::DownCast(mySection->Copy());
  TheSection->Transform(Transformation(WithTranslation, Standard_True));
  return TheSection;
}

//===============================================================
// Function :SectionAxis
// Purpose :
//===============================================================
 void GeomFill_SectionPlacement::SectionAxis(const gp_Mat& M,
					     gp_Vec& T,
					     gp_Vec& N, 
					     gp_Vec& BN) const
{
  Standard_Real Eps = 1.e-10;
  gp_Dir D;
  gp_Vec PathNormal;
  GeomLProp_CLProps CP(mySection, SecParam, 2, Eps);
  if (CP.IsTangentDefined()) {
    CP.Tangent(D);
    T.SetXYZ(D.XYZ());
    T.Normalize();
    if (CP.Curvature() > Eps) {
      CP.Normal(D);
      N.SetXYZ(D.XYZ());
    }
    else { 
      // Cas ambigu, on essai de recuperer la normal a la trajectoire
      // Reste le probleme des points d'inflexions, qui n'est pas 
      // bien traiter par LProp (pas de recuperation de la normal) : 
      // A voir...
      PathNormal.SetXYZ(M.Column(1));
      PathNormal.Normalize();
      BN = T ^ PathNormal;
      if (BN.Magnitude() > Eps) {
	BN.Normalize();
	//N = BN ^ T;
      }
      N = BN ^ T;
    }
  }
  else { // Cas indefinie, on prend le triedre 
        // complet sur la trajectoire
    T.SetXYZ(M.Column(3));
    N.SetXYZ(M.Column(2));
  }
  BN = T ^ N;
}


//===============================================================
// Function :Choix
// Purpose : Decide si le couple (dist, angle) est "meilleur" que
// couple courrant.
//===============================================================
 Standard_Boolean GeomFill_SectionPlacement::Choix(const Standard_Real dist,
						   const Standard_Real  angle) const
{
  Standard_Real evoldist, evolangle;
  evoldist = dist - Dist;
  evolangle = angle - AngleMax;
  // (1) Si la gain en distance est > que le gabarit, on prend
  if (evoldist < - Gabarit) 
    return Standard_True;

 //  (2) si l'ecart en distance est de l'ordre du gabarit
  if (Abs(evoldist) < Gabarit) {
//  (2.1) si le gain en angle est important on garde
    if  (evolangle > 0.5)
       return Standard_True;
 //  (2.2) si la variation d'angle est moderee on evalue
 //  une fonction de penalite
    if (Penalite(angle, dist/Gabarit) < Penalite(AngleMax, Dist/Gabarit) )
      return Standard_True;
  }

  return Standard_False;
}