summaryrefslogtreecommitdiff
path: root/src/Bisector/Bisector_Bisec.cxx
blob: 6b2430216c43d733e5138e35c70fc656ae765341 (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
// File:	Bisector_Bisec.cxx
// Created:	Mon Jul  4 10:05:48 1994
// Author:	Yves FRICAUD
//		<yfr@phobox>

#include <Bisector_Bisec.ixx>
#include <Bisector.hxx>
#include <Geom2d_Circle.hxx>
#include <Geom2d_Line.hxx>
#include <Geom2d_Point.hxx>
#include <Geom2d_CartesianPoint.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <gp.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Vec2d.hxx>
#include <StdFail_NotDone.hxx>
#include <Standard_NotImplemented.hxx>
#include <Precision.hxx>
#include <Bisector_Curve.hxx>
#include <Bisector_BisecAna.hxx>
#include <Bisector_BisecPC.hxx>
#include <Bisector_BisecCC.hxx>

/*
#include <DrawTrSurf.hxx>
static char tname[100];
static Standard_CString name = tname ;
static Standard_Integer nbb  = 0;
*/

#ifdef DEB
static Standard_Boolean  PointIsOnCurve(const Handle(Geom2d_Curve)& C,
					const gp_Pnt2d&             P,
					      Standard_Real&        U);
#endif
static Standard_Boolean IsMaxRC (const Handle(Geom2d_Curve)& C,
				       Standard_Real         U,
        			       Standard_Real&        R);

static void ReplaceByLineIfIsToSmall (Handle(Geom2d_Curve)& Bis,
				      Standard_Real&        UFirst,
				      Standard_Real&        ULast);					
//=============================================================================
//function : Constructeur vide                                                
//=============================================================================
Bisector_Bisec::Bisector_Bisec()
{
}

//===========================================================================
//    calcul de la bissectrice entre deux courbes issue d un point.         
//                                                                          
//   afirstcurve   : \ courbes entre lesquelles on veut calculer la         
//   asecondcurve  : / bissectrice.                                         
//   apoint        :   point par lequel doit passer la bissectrice.         
//   afirstvector  : \ vecteurs pour determiner le secteur dans lequel      
//   asecondvector : / la bissectrice doit se trouver.                      
//   adirection    :   indique le cote de la bissectrice a conserver.       
//   tolerance     :   seuil a partir duquel les bisectrices sont degenerees
//===========================================================================

void Bisector_Bisec::Perform(const Handle(Geom2d_Curve)& afirstcurve   ,
			     const Handle(Geom2d_Curve)& asecondcurve  ,
			     const gp_Pnt2d&             apoint        ,
			     const gp_Vec2d&             afirstvector  ,
			     const gp_Vec2d&             asecondvector ,
			     const Standard_Real         adirection    ,
			     const Standard_Real         tolerance     ,
			     const Standard_Boolean      oncurve       )
{
  Handle(Standard_Type)  Type1 = afirstcurve ->DynamicType();
  Handle(Standard_Type)  Type2 = asecondcurve->DynamicType();
  Handle(Bisector_Curve) Bis;
  Standard_Real          UFirst,ULast;
  
  if (Type1 == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
    Type1 = Handle(Geom2d_TrimmedCurve)::DownCast(afirstcurve)
            ->BasisCurve()->DynamicType();
  }
  if (Type2 == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
    Type2 = Handle(Geom2d_TrimmedCurve)::DownCast(asecondcurve)
            ->BasisCurve()->DynamicType();
  }

  if ( (Type1 == STANDARD_TYPE(Geom2d_Circle) || Type1 == STANDARD_TYPE(Geom2d_Line)) &&
       (Type2 == STANDARD_TYPE(Geom2d_Circle) || Type2 == STANDARD_TYPE(Geom2d_Line))   ) {    
    //------------------------------------------------------------------
    // Bissectrice analytique.
    //------------------------------------------------------------------
     Handle(Bisector_BisecAna) BisAna = new Bisector_BisecAna();
     BisAna->Perform(afirstcurve   ,
		     asecondcurve  ,
		     apoint        ,
		     afirstvector  ,
		     asecondvector ,
		     adirection    ,
		     tolerance     ,
		     oncurve       );
     UFirst = BisAna->ParameterOfStartPoint();
     ULast  = BisAna->ParameterOfEndPoint();    
     Bis = BisAna;   
  }
  else {  
     Standard_Boolean IsLine = Standard_False;

    if (oncurve) {
      gp_Dir2d Fd(afirstvector);
      gp_Dir2d Sd(asecondvector);
      //if (Fd.Dot(Sd) < Precision::Angular() - 1.) { 
      //if (Fd.Dot(Sd) < 10*Precision::Angular() - 1.) //patch
      if (Fd.Dot(Sd) < Sqrt(2.*Precision::Angular()) - 1.)
	IsLine = Standard_True;
    }
    if (IsLine) {     
      //------------------------------------------------------------------
      // Demi-Droite.
      //------------------------------------------------------------------
      gp_Dir2d N ( - adirection*afirstvector.Y(), adirection*afirstvector.X());
      Handle (Geom2d_CartesianPoint) PG     = new Geom2d_CartesianPoint(apoint);
      Handle (Geom2d_Line)           L      = new Geom2d_Line (apoint,N);
      Handle (Geom2d_TrimmedCurve)   
	BisL   = new Geom2d_TrimmedCurve (L,0,Precision::Infinite());
      Handle(Bisector_BisecAna)      BisAna = new Bisector_BisecAna ();
      BisAna->Init(BisL);
      UFirst = BisAna->ParameterOfStartPoint();
      ULast  = BisAna->ParameterOfEndPoint();
      Bis    = BisAna;
    }
    else {
      //-------------------------------------------------------------------
      // Bissectrice algo
      //-------------------------------------------------------------------
      Handle(Bisector_BisecCC) BisCC = new Bisector_BisecCC();
      BisCC -> Perform(asecondcurve, 
		       afirstcurve ,
		       adirection  , 
		       adirection  , 
		       apoint);

      if (BisCC -> IsEmpty()) {
	// la bissectrice est vide apoint se projette a la fin de la courbe
	// guide . Construction d une fausse bissectrice.
//  modified by NIZHNY-EAP Mon Feb 21 12:00:13 2000 ___BEGIN___
	gp_Dir2d dir1(afirstvector), dir2(asecondvector);
	Standard_Real
	  Nx = - dir1.X() - dir2.X(),
	  Ny = - dir1.Y() - dir2.Y();
	if (Abs(Nx) <= gp::Resolution() && Abs(Ny) <= gp::Resolution()) {
	  Nx = - afirstvector.Y();
	  Ny = afirstvector.X();
	}
	//gp_Dir2d N ( - adirection*afirstvector.Y(), adirection*afirstvector.X());
	gp_Dir2d N ( adirection*Nx, adirection*Ny);
//  modified by NIZHNY-EAP Mon Feb 21 12:00:19 2000 ___END___
	
	Handle (Geom2d_CartesianPoint) PG     = new Geom2d_CartesianPoint(apoint);
	Handle (Geom2d_Line)           L      = new Geom2d_Line (apoint,N);
	Handle (Geom2d_TrimmedCurve)   
	  BisL   = new Geom2d_TrimmedCurve (L,0,Precision::Infinite());
	Handle(Bisector_BisecAna)      BisAna = new Bisector_BisecAna ();
	BisAna->Init(BisL);
	UFirst = BisAna->ParameterOfStartPoint();
	ULast  = BisAna->ParameterOfEndPoint();
	Bis    = BisAna;
      }
      else {
	UFirst = BisCC->FirstParameter();
	ULast  = BisCC->LastParameter ();
	Bis    = BisCC;
	ReplaceByLineIfIsToSmall(Bis,UFirst,ULast);
      }
    }
  }
  thebisector = new Geom2d_TrimmedCurve(Bis,UFirst,ULast);

/*
  sprintf( name, "c1_%d", ++nbb );
  DrawTrSurf::Set( name, afirstcurve );
  sprintf( name, "c2_%d", nbb );
  DrawTrSurf::Set( name, asecondcurve );
  sprintf( name, "p%d", nbb );
  DrawTrSurf::Set( name, apoint );
  sprintf( name, "b%d", nbb );
  DrawTrSurf::Set( name, thebisector );
*/
}

//===========================================================================
//  calcul de la bissectrice entre une courbe et un point issue d un point. 
//                                                                          
//   afirstcurve   : \ courbe et point entre lesquelles on veut calculer la 
//   asecondpoint  : / bissectrice.                                         
//   apoint        :   point par lequel doit passer la bissectrice.         
//   afirstvector  : \ vecteurs pour determiner le secteur dans lequel      
//   asecondvector : / la bissectrice doit se trouver.                      
//   adirection    :   indique le cote de la bissectrice a conserver.       
//   tolerance     :   seuil a partir duquel les bisectrices sont degenerees
//===========================================================================

void Bisector_Bisec::Perform(const Handle(Geom2d_Curve)& afirstcurve  ,
			     const Handle(Geom2d_Point)& asecondpoint ,
			     const gp_Pnt2d&             apoint       ,
			     const gp_Vec2d&             afirstvector ,
			     const gp_Vec2d&             asecondvector,
			     const Standard_Real         adirection   ,
			     const Standard_Real         tolerance    ,
			     const Standard_Boolean      oncurve       )
{  
  //gp_Pnt2d SecondPnt = asecondpoint->Pnt2d();

  Handle(Bisector_Curve) Bis;
  Handle(Standard_Type)  Type1 = afirstcurve ->DynamicType();
  Standard_Real          UFirst,ULast;

  if (Type1 == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
    Type1 = Handle(Geom2d_TrimmedCurve)::DownCast(afirstcurve)
            ->BasisCurve()->DynamicType();
  }

  if ( Type1 == STANDARD_TYPE(Geom2d_Circle) || Type1 == STANDARD_TYPE(Geom2d_Line)) {
    //------------------------------------------------------------------
    // Bissectrice analytique.
    //------------------------------------------------------------------
    Handle(Bisector_BisecAna) BisAna = new Bisector_BisecAna();
    BisAna -> Perform (afirstcurve   ,
		       asecondpoint  ,
		       apoint        ,
		       afirstvector  ,
		       asecondvector ,
		       adirection    ,
		       tolerance     ,
		       oncurve       );
    UFirst = BisAna->ParameterOfStartPoint();
    ULast  = BisAna->ParameterOfEndPoint();
    Bis    = BisAna;
  }
  else {  
    Standard_Boolean IsLine    = Standard_False;
    Standard_Real    RC        = Precision::Infinite();
    
    if (oncurve) {
      if (Bisector::IsConvex(afirstcurve,adirection) || 
	  IsMaxRC(afirstcurve,afirstcurve->LastParameter(),RC)) { 
	IsLine = Standard_True; 
      }
    }
    if (IsLine) {     
      //------------------------------------------------------------------
      // Demi-Droite.
      //------------------------------------------------------------------
      gp_Dir2d N ( -adirection*afirstvector.Y(), adirection*afirstvector.X());
      Handle (Geom2d_Line)         L      = new Geom2d_Line (apoint,N);
      Handle (Geom2d_TrimmedCurve) BisL   = new Geom2d_TrimmedCurve(L,0,RC);
      Handle(Bisector_BisecAna)    BisAna = new Bisector_BisecAna ();
      BisAna->Init(BisL);
      UFirst = BisAna->ParameterOfStartPoint();
      ULast  = BisAna->ParameterOfEndPoint();
      Bis    = BisAna;
    }
    else {
      //-------------------------------------------------------------------
      // Bissectrice algo
      //-------------------------------------------------------------------
      Handle(Bisector_BisecPC) BisPC = new Bisector_BisecPC();
      Handle(Geom2d_Curve) afirstcurvereverse = afirstcurve->Reversed();
      
      BisPC -> Perform(afirstcurvereverse   ,
		       asecondpoint->Pnt2d(),
		       - adirection         );
//  Modified by Sergey KHROMOV - Thu Feb 21 16:49:54 2002 Begin
      if (BisPC -> IsEmpty()) {
	gp_Dir2d dir1(afirstvector), dir2(asecondvector);
	Standard_Real
	  Nx = - dir1.X() - dir2.X(),
	  Ny = - dir1.Y() - dir2.Y();
	if (Abs(Nx) <= gp::Resolution() && Abs(Ny) <= gp::Resolution()) {
	  Nx = - afirstvector.Y();
	  Ny = afirstvector.X();
	}
// 	gp_Dir2d N ( -adirection*afirstvector.Y(), adirection*afirstvector.X());
	gp_Dir2d N ( adirection*Nx, adirection*Ny);
	Handle (Geom2d_Line)         L      = new Geom2d_Line (apoint,N);
	Handle (Geom2d_TrimmedCurve) BisL   = new Geom2d_TrimmedCurve(L,0,RC);
	Handle(Bisector_BisecAna)    BisAna = new Bisector_BisecAna ();
	BisAna->Init(BisL);
	UFirst = BisAna->ParameterOfStartPoint();
	ULast  = BisAna->ParameterOfEndPoint();
	Bis    = BisAna;
      } else {
//  Modified by Sergey KHROMOV - Wed Mar  6 17:01:08 2002 End
	UFirst = BisPC->Parameter(apoint);
	ULast  = BisPC->LastParameter();
	if(UFirst >= ULast)
	  {
	  //Standard_Real t = .9;
	  //UFirst = (1. - t) * BisPC->FirstParameter() + t * ULast;
	    //Extrapolate by line
	    //gp_Dir2d N ( -adirection*afirstvector.Y(), adirection*afirstvector.X());
	    gp_Vec2d V( BisPC->Value(BisPC->FirstParameter()), BisPC->Value(ULast) );
	    gp_Dir2d N( V );
	    Handle (Geom2d_Line)         L      = new Geom2d_Line         (apoint,N);
	    Handle (Geom2d_TrimmedCurve) BisL   = new Geom2d_TrimmedCurve (L,0,RC);
	    Handle(Bisector_BisecAna)    BisAna = new Bisector_BisecAna   ();
	    BisAna->Init(BisL);
	    UFirst = BisAna->ParameterOfStartPoint();
	    ULast  = BisAna->ParameterOfEndPoint();
	    Bis    = BisAna;
	  }
	else
	  Bis    = BisPC;
      }
    }
  }
  thebisector = new Geom2d_TrimmedCurve(Bis,UFirst,ULast);

/*
  sprintf( name, "c1_%d", ++nbb );
  DrawTrSurf::Set( name, afirstcurve );
  sprintf( name, "c2_%d", nbb );
  DrawTrSurf::Set( name, SecondPnt );
  sprintf( name, "p%d", nbb );
  DrawTrSurf::Set( name, apoint );
  sprintf( name, "b%d", nbb );
  DrawTrSurf::Set( name, thebisector );
*/
}

//===========================================================================
//  calcul de la bissectrice entre une courbe et un point issue d un point. 
//                                                                          
//   afirstpoint   : \ point et courbe entre lesquelles on veut calculer la         
//   asecondcurve  : / bissectrice.                                         
//   apoint        :   point par lequel doit passer la bissectrice.         
//   afirstvector  : \ vecteurs pour determiner le secteur dans lequel      
//   asecondvector : / la bissectrice doit se trouver.                      
//   adirection    :   indique le cote de la bissectrice a conserver.       
//   tolerance     :   seuil a partir duquel les bisectrices sont degenerees
//===========================================================================

void Bisector_Bisec::Perform(const Handle(Geom2d_Point)& afirstpoint  ,
			     const Handle(Geom2d_Curve)& asecondcurve ,
			     const gp_Pnt2d&             apoint       ,
			     const gp_Vec2d&             afirstvector ,
			     const gp_Vec2d&             asecondvector,
			     const Standard_Real         adirection   ,
			     const Standard_Real         tolerance    ,
			     const Standard_Boolean      oncurve       )

{  
  //gp_Pnt2d FirstPnt = afirstpoint->Pnt2d();

  Handle(Bisector_Curve) Bis;
  Handle(Standard_Type)  Type1 = asecondcurve ->DynamicType();
  Standard_Real          UFirst,ULast;
  
  if (Type1 == STANDARD_TYPE(Geom2d_TrimmedCurve)) {
    Type1 = Handle(Geom2d_TrimmedCurve)::DownCast(asecondcurve)
            ->BasisCurve()->DynamicType();
  }
  
  if ( Type1 == STANDARD_TYPE(Geom2d_Circle) || Type1 == STANDARD_TYPE(Geom2d_Line)) {
    //------------------------------------------------------------------
    // Bissectrice analytique.
    //------------------------------------------------------------------
    Handle(Bisector_BisecAna) BisAna = new Bisector_BisecAna();
    BisAna -> Perform (afirstpoint   ,
		       asecondcurve  ,
		       apoint        ,
		       afirstvector  ,
		       asecondvector ,
		       adirection    ,
		       tolerance     ,
		       oncurve       );
    UFirst = BisAna->ParameterOfStartPoint();
    ULast  = BisAna->ParameterOfEndPoint();
    Bis    = BisAna;
  }
  else {   
//  Standard_Real    UPoint    = 0.;
    Standard_Boolean IsLine    = Standard_False;
    Standard_Real    RC        = Precision::Infinite();
    
    if (oncurve) {
      if (Bisector::IsConvex(asecondcurve, adirection) || 
	  IsMaxRC(asecondcurve,asecondcurve->FirstParameter(),RC)) {
	IsLine = Standard_True;
      }
    }    
    if (IsLine) {     
      //------------------------------------------------------------------
      // Demi-Droite.
      //------------------------------------------------------------------
      gp_Dir2d N ( -adirection*afirstvector.Y(), adirection*afirstvector.X());
      Handle (Geom2d_Line)         L      = new Geom2d_Line         (apoint,N);
      Handle (Geom2d_TrimmedCurve) BisL   = new Geom2d_TrimmedCurve (L,0,RC);
      Handle(Bisector_BisecAna)    BisAna = new Bisector_BisecAna   ();
      BisAna->Init(BisL);
      UFirst = BisAna->ParameterOfStartPoint();
      ULast  = BisAna->ParameterOfEndPoint();
      Bis    = BisAna;
    }
    else {
      //-------------------------------------------------------------------
      // Bissectrice algo
      //-------------------------------------------------------------------
      Handle(Bisector_BisecPC) BisPC = new Bisector_BisecPC();
      BisPC -> Perform(asecondcurve        ,
		       afirstpoint->Pnt2d(),
		       adirection          );
//  Modified by Sergey KHROMOV - Thu Feb 21 16:49:54 2002 Begin
      if (BisPC -> IsEmpty()) {
	gp_Dir2d dir1(afirstvector), dir2(asecondvector);
	Standard_Real
	  Nx = - dir1.X() - dir2.X(),
	  Ny = - dir1.Y() - dir2.Y();
	if (Abs(Nx) <= gp::Resolution() && Abs(Ny) <= gp::Resolution()) {
	  Nx = - afirstvector.Y();
	  Ny = afirstvector.X();
	}
// 	gp_Dir2d N ( -adirection*afirstvector.Y(), adirection*afirstvector.X());
	gp_Dir2d N ( adirection*Nx, adirection*Ny);
	Handle (Geom2d_Line)         L      = new Geom2d_Line (apoint,N);
	Handle (Geom2d_TrimmedCurve) BisL   = new Geom2d_TrimmedCurve(L,0,RC);
	Handle(Bisector_BisecAna)    BisAna = new Bisector_BisecAna ();
	BisAna->Init(BisL);
	UFirst = BisAna->ParameterOfStartPoint();
	ULast  = BisAna->ParameterOfEndPoint();
	Bis    = BisAna;
      } else {
//  Modified by Sergey KHROMOV - Thu Feb 21 16:49:58 2002 End
	UFirst = BisPC->Parameter(apoint);
	ULast  = BisPC->LastParameter();
	if(UFirst >= ULast)
	  {
	    //Extrapolate by line
	    //gp_Dir2d N ( -adirection*afirstvector.Y(), adirection*afirstvector.X());
	    gp_Vec2d V( BisPC->Value(BisPC->FirstParameter()), BisPC->Value(ULast) );
	    gp_Dir2d N( V );
	    Handle (Geom2d_Line)         L      = new Geom2d_Line         (apoint,N);
	    Handle (Geom2d_TrimmedCurve) BisL   = new Geom2d_TrimmedCurve (L,0,RC);
	    Handle(Bisector_BisecAna)    BisAna = new Bisector_BisecAna   ();
	    BisAna->Init(BisL);
	    UFirst = BisAna->ParameterOfStartPoint();
	    ULast  = BisAna->ParameterOfEndPoint();
	    Bis    = BisAna;
	  }
	else
	  Bis    = BisPC;
      }
    }
  }
  thebisector = new Geom2d_TrimmedCurve(Bis,UFirst,ULast);

/*
  sprintf( name, "c1_%d", ++nbb );
  DrawTrSurf::Set( name, FirstPnt );
  sprintf( name, "c2_%d", nbb );
  DrawTrSurf::Set( name, asecondcurve );
  sprintf( name, "p%d", nbb );
  DrawTrSurf::Set( name, apoint );
  sprintf( name, "b%d", nbb );
  DrawTrSurf::Set( name, thebisector );
*/
}

//===========================================================================
//        calcul de la bissectrice entre deux points issue d un point.      
//                                                                          
//   afirstpoint   : \ courbes entre lesquelles on veut calculer la         
//   asecondpoint  : / bissectrice.                                         
//   apoint        :   point par lequel doit passer la bissectrice.         
//   afirstvector  : \ vecteurs pour determiner le secteur dans lequel      
//   asecondvector : / la bissectrice doit se trouver.                      
//   adirection    :   indique le cote de la bissectrice a conserver.       
//===========================================================================

void Bisector_Bisec::Perform(const Handle(Geom2d_Point)& afirstpoint  ,
			     const Handle(Geom2d_Point)& asecondpoint ,
			     const gp_Pnt2d&             apoint       ,
			     const gp_Vec2d&             afirstvector ,
			     const gp_Vec2d&             asecondvector,
			     const Standard_Real         adirection   ,
			     const Standard_Real         tolerance    ,
			     const Standard_Boolean      oncurve      )
{
  Handle(Bisector_BisecAna) Bis = new Bisector_BisecAna();

  Bis -> Perform (afirstpoint   ,
		  asecondpoint  ,
		  apoint        ,
		  afirstvector  ,
		  asecondvector ,
		  adirection    ,
		  tolerance     ,
		  oncurve       ); 
  thebisector = new Geom2d_TrimmedCurve(Bis,
					Bis->ParameterOfStartPoint(),
					Bis->ParameterOfEndPoint());

/*
  sprintf( name, "c1_%d", ++nbb );
  DrawTrSurf::Set( name, afirstpoint->Pnt2d() );
  sprintf( name, "c2_%d", nbb );
  DrawTrSurf::Set( name, asecondpoint->Pnt2d() );
  sprintf( name, "p%d", nbb );
  DrawTrSurf::Set( name, apoint );
  sprintf( name, "b%d", nbb );
  DrawTrSurf::Set( name, thebisector );
*/
}

//=============================================================================
//function : Value
//purpose  :
//=============================================================================
const Handle(Geom2d_TrimmedCurve)&  Bisector_Bisec::Value() const
{
  return thebisector;
}

//=============================================================================
//function : ChangeValue
//purpose  :
//=============================================================================
const Handle(Geom2d_TrimmedCurve)&  Bisector_Bisec::ChangeValue()
{
  return thebisector;
}

//=============================================================================
//function : PointIsOnCurve
// purpose  :
//=============================================================================
#ifdef DEB
static Standard_Boolean  PointIsOnCurve(const Handle(Geom2d_Curve)& C,
					const gp_Pnt2d&             P,
					      Standard_Real&        U)
{
  if (C->Value(C->FirstParameter()).IsEqual(P,Precision::Confusion())) {
    U = C->FirstParameter();
    return Standard_True;
  }
  if (C->Value(C->LastParameter()).IsEqual(P,Precision::Confusion()))  {
    U = C->LastParameter();
    return Standard_True;
  }
  return Standard_False;
}
#endif
//=============================================================================
//function : ReplaceByLineIfIsToSmall 
//purpose  : Si une bissectrice algorithmique est de taille negligeable elle est
//           remplace par une demi-droite.
//=============================================================================
static void ReplaceByLineIfIsToSmall (Handle(Geom2d_Curve)& Bis,
				      Standard_Real&        UFirst,
				      Standard_Real&        ULast )

{
  if (Abs(ULast - UFirst) > 2.*Precision::PConfusion()*10.) return; //patch

  gp_Pnt2d PF = Bis->Value(UFirst);
  gp_Pnt2d PL = Bis->Value(ULast);

  if (PF.Distance(PL) > Precision::Confusion()*10.) return;

  gp_Vec2d T1 = Bis->DN(UFirst,1);

  Handle (Geom2d_CartesianPoint) PG     = new Geom2d_CartesianPoint(PF);
  Handle (Geom2d_Line)           L      = new Geom2d_Line (PF,T1);
  Handle (Geom2d_TrimmedCurve)   
    BisL   = new Geom2d_TrimmedCurve (L,0,Precision::Infinite());
  Handle(Bisector_BisecAna)      BisAna = new Bisector_BisecAna ();
  BisAna->Init(BisL);
  UFirst = BisAna->ParameterOfStartPoint();
  ULast  = BisAna->ParameterOfEndPoint();
  Bis    = BisAna;
}

//=============================================================================
//function : IsMaxRC
//purpose  :
//=============================================================================
static Standard_Boolean  IsMaxRC (const Handle(Geom2d_Curve)& C,
				        Standard_Real         U,
					Standard_Real&        R)
{  
  Standard_Real KF,KL;
  Standard_Real US = C->FirstParameter();
  Standard_Real UL = C->LastParameter();

  gp_Vec2d      D1,D2;
  gp_Pnt2d      P;
  Standard_Real Norm2;

  C->D2(US,P,D1,D2);
  Norm2 = D1.SquareMagnitude();;
  if (Norm2 < gp::Resolution()) { KF = 0.0;}
  else                          { KF = Abs(D1^D2)/(Norm2*sqrt(Norm2));}
  
  C->D2(UL,P,D1,D2);
  Norm2 = D1.SquareMagnitude();;
  if (Norm2 < gp::Resolution()) { KL = 0.0;}
  else                          { KL = Abs(D1^D2)/(Norm2*sqrt(Norm2));}

  Standard_Boolean IsMax = Standard_False;

  if (U == UL) {
    if (KL < KF) {
      if (KL == 0.0) R = Precision::Infinite(); else R = 1/KL;
      IsMax = Standard_True;
    }
  }
  else {   
    if (KF < KL) {
      if (KF == 0.0) R = Precision::Infinite(); else R = 1/KF;
      IsMax = Standard_True;
    }
  }
  return IsMax;
}