summaryrefslogtreecommitdiff
path: root/src/ShapeAnalysis/ShapeAnalysis_TransferParametersProj.cxx
blob: cbb239b547a7f6e69547efe742a2bf31a52916a2 (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
#include <ShapeAnalysis_TransferParametersProj.ixx>
#include <Geom2dAdaptor_HCurve.hxx>
#include <Geom2d_Curve.hxx>
#include <Adaptor3d_CurveOnSurface.hxx>
#include <Geom_Surface.hxx>
#include <GeomAdaptor_HSurface.hxx>
#include <BRep_Tool.hxx>
#include <TColStd_HArray1OfReal.hxx>
#include <TopLoc_Location.hxx>
#include <ShapeAnalysis_Edge.hxx>
#include <TColgp_SequenceOfPnt.hxx>
#include <ShapeAnalysis_Curve.hxx>
#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
#include <BRep_TEdge.hxx>
#include <BRep_GCurve.hxx>
#include <BRep_ListOfCurveRepresentation.hxx>
#include <BRep_ListOfCurveRepresentation.hxx>
#include <Precision.hxx>
#include <ShapeBuild_Edge.hxx>
#include <BRep_Builder.hxx>
#include <GeomAdaptor_Curve.hxx>
#include <ShapeAnalysis.hxx>
#include <Geom2d_TrimmedCurve.hxx>
#include <Geom2d_OffsetCurve.hxx>
#include <Geom2d_BSplineCurve.hxx>
#include <BRep_ListOfPointRepresentation.hxx>
#include <BRep_TVertex.hxx>
#include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
#include <BRep_PointRepresentation.hxx>
#include <BRep_PointOnSurface.hxx>
#include <BRep_PointOnCurve.hxx>
#include <BRep_PointOnCurveOnSurface.hxx>
#include <TopoDS.hxx>  
#include <ShapeAnalysis_Surface.hxx>
//=======================================================================
//function : ShapeAnalysis_TransferParametersProj
//purpose  : 
//=======================================================================

ShapeAnalysis_TransferParametersProj::ShapeAnalysis_TransferParametersProj()
{
  myMaxTolerance = 1; //Precision::Infinite(); ?? pdn
  myForceProj = Standard_False;
  myInitOK = Standard_False;
}


//=======================================================================
//function : ShapeAnalysis_TransferParametersProj
//purpose  : 
//=======================================================================

ShapeAnalysis_TransferParametersProj::ShapeAnalysis_TransferParametersProj(const TopoDS_Edge& E,
                                                                           const TopoDS_Face& F)
{
  myMaxTolerance = 1; //Precision::Infinite(); ?? pdn
  myForceProj = Standard_False;
  Init(E,F);
}


//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

void ShapeAnalysis_TransferParametersProj::Init(const TopoDS_Edge& E,
                                                const TopoDS_Face& F)
{
  myInitOK = Standard_False;
  ShapeAnalysis_TransferParameters::Init(E,F);
  myEdge = E;
  myPrecision = BRep_Tool::Tolerance(E); // it is better - skl OCC2851
  //myPrecision = Precision::Confusion();

  myCurve = BRep_Tool::Curve (E, myFirst, myLast);
  if ( myCurve.IsNull() ) { myFirst = 0.; myLast = 1.; return;}
    
  if ( F.IsNull() ) return;
    
  Standard_Real f2d, l2d;
  ShapeAnalysis_Edge sae;
  if(sae.PCurve (E, F, myCurve2d, f2d, l2d, Standard_False)) {
      
    Handle(Geom2dAdaptor_HCurve) AC2d  = new Geom2dAdaptor_HCurve(myCurve2d,f2d,l2d);
    Handle(Geom_Surface) aSurface = BRep_Tool::Surface(F,myLocation);
    Handle(GeomAdaptor_HSurface) AdS = new GeomAdaptor_HSurface(aSurface);
    
    Adaptor3d_CurveOnSurface Ad1(AC2d,AdS);
    myAC3d = Ad1;//new Adaptor3d_CurveOnSurface(AC2d,AdS);
    myInitOK = Standard_True;
  }
}


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

Handle(TColStd_HSequenceOfReal) ShapeAnalysis_TransferParametersProj::Perform
          (const Handle(TColStd_HSequenceOfReal)& Knots,
           const Standard_Boolean To2d) 
{
  //pdn
  if( !myInitOK || 
     (! myForceProj && myPrecision < myMaxTolerance && BRep_Tool::SameParameter(myEdge)))
    return ShapeAnalysis_TransferParameters::Perform(Knots,To2d);
  
  Handle(TColStd_HSequenceOfReal) resKnots = new TColStd_HSequenceOfReal;

  Standard_Integer len = Knots->Length();
  Standard_Real preci = 2*Precision::PConfusion();

  Standard_Real first = (To2d ? myAC3d.FirstParameter() : myFirst);
  Standard_Real last  = (To2d ? myAC3d.LastParameter() : myLast);
  Standard_Real maxPar = first;
  Standard_Real lastPar = last;
  Standard_Real prevPar = maxPar;

  Standard_Integer j; // svv Jan 10 2000 : porting on DEC
  for(j = 1; j <= len; j++) {
    Standard_Real par = PreformSegment(Knots->Value(j),To2d,prevPar,lastPar);
    prevPar = par;
    if(prevPar > lastPar)
      prevPar -= preci;
    resKnots->Append(par);
    if(par > maxPar)
      maxPar = par;
  }
  
  //pdn correcting on periodic
  if(myCurve->IsClosed())
    for(j = len; j >=1; j--) 
      if(resKnots->Value(j) < maxPar)
	resKnots->SetValue(j,(To2d ? myAC3d.LastParameter() : myCurve->LastParameter())-(len-j)*preci);
      else
	break;
  //pdn correction on range
  for ( j=1; j <= len; j++ ) {
    if ( resKnots->Value (j) < first ) resKnots->SetValue ( j, first );
    if ( resKnots->Value (j) > last  ) resKnots->SetValue ( j, last );
  }
  
  return resKnots;
}


//=======================================================================
//function : PreformSegment
//purpose  : 
//=======================================================================

Standard_Real ShapeAnalysis_TransferParametersProj::PreformSegment(const Standard_Real Param,
                                                                   const Standard_Boolean To2d,
                                                                   const Standard_Real First,
                                                                   const Standard_Real Last)
{
  Standard_Real linPar = ShapeAnalysis_TransferParameters::Perform(Param, To2d);
  if( !myInitOK || 
     (! myForceProj && myPrecision < myMaxTolerance && BRep_Tool::SameParameter(myEdge)))
    return linPar;
  
  Standard_Real linDev, projDev;
  
  ShapeAnalysis_Curve sac;
  gp_Pnt pproj;
  Standard_Real ppar;
  if(To2d) {
    gp_Pnt p1 = myCurve->Value(Param).Transformed(myLocation.Inverted());
    Handle(Adaptor3d_HSurface) AdS = myAC3d.GetSurface();
    Handle(Geom2dAdaptor_HCurve) AC2d  = new Geom2dAdaptor_HCurve(myCurve2d,First,Last);
    Adaptor3d_CurveOnSurface Ad1(AC2d,AdS);
    projDev = sac.Project(Ad1,p1,myPrecision,pproj,ppar);//pdn
    linDev = p1.Distance(Ad1.Value(linPar));
  }
  else {
    gp_Pnt p1 = myAC3d.Value(Param).Transformed(myLocation);
    projDev = sac.Project(myCurve,p1,myPrecision,pproj,ppar,First,Last,Standard_False);
    linDev = p1.Distance(myCurve->Value(linPar));
  }
  
  if ( linDev <= projDev ||  (linDev < myPrecision && linDev <= 2 * projDev ) ) 
    ppar = linPar;
  return ppar;
}


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

Standard_Real ShapeAnalysis_TransferParametersProj::Perform(const Standard_Real Knot,
                                                            const Standard_Boolean To2d) 
{
  if( !myInitOK || 
     (! myForceProj && myPrecision < myMaxTolerance && BRep_Tool::SameParameter(myEdge)))
    return ShapeAnalysis_TransferParameters::Perform(Knot, To2d);
  
  Standard_Real res;
  if(To2d) 
    res = PreformSegment(Knot,To2d,myAC3d.FirstParameter(),myAC3d.LastParameter());
  else 
    res = PreformSegment(Knot,To2d,myFirst,myLast);

  //pdn correction on range
  Standard_Real first = (To2d ? myAC3d.FirstParameter() : myFirst);
  Standard_Real last  = (To2d ? myAC3d.LastParameter() : myLast);
  if ( res < first ) res = first;
  if ( res > last  ) res = last;
  return res;
}


//=======================================================================
//function : CorrectParameter
//purpose  : auxilary
//=======================================================================
static Standard_Real CorrectParameter(const Handle(Geom2d_Curve) crv,
				      const Standard_Real param)
{
  if(crv->IsKind(STANDARD_TYPE(Geom2d_TrimmedCurve))) {
    Handle(Geom2d_TrimmedCurve) tmp = Handle(Geom2d_TrimmedCurve)::DownCast (crv);
    return CorrectParameter(tmp->BasisCurve(),param);
  } 
  else if(crv->IsKind(STANDARD_TYPE(Geom2d_OffsetCurve))) {
    Handle(Geom2d_OffsetCurve) tmp = Handle(Geom2d_OffsetCurve)::DownCast (crv);
    return CorrectParameter(tmp->BasisCurve(),param);
  } 
  else if(crv->IsKind(STANDARD_TYPE(Geom2d_BSplineCurve))) {
    Handle(Geom2d_BSplineCurve) bspline = Handle(Geom2d_BSplineCurve)::DownCast (crv);
    for(Standard_Integer j = bspline->FirstUKnotIndex(); j <= bspline->LastUKnotIndex(); j++) {
      Standard_Real valknot = bspline->Knot(j);
      if( Abs(valknot-param)<Precision::PConfusion() )
	return valknot;
    }
  }
  return param;
}
    
  
//=======================================================================
//function : TransferRange
//purpose  : 
//=======================================================================

void ShapeAnalysis_TransferParametersProj::TransferRange(TopoDS_Edge& newEdge,
                                                         const Standard_Real prevPar,
                                                         const Standard_Real currPar,
                                                         const Standard_Boolean Is2d) 
{
  if( !myInitOK || 
     (! myForceProj && myPrecision < myMaxTolerance && BRep_Tool::SameParameter(myEdge))) {
    ShapeAnalysis_TransferParameters::TransferRange(newEdge,prevPar,currPar,Is2d);
    return;
  }

  BRep_Builder B;
  Standard_Boolean samerange = Standard_True;
  ShapeBuild_Edge sbe;
  sbe.CopyRanges(newEdge,myEdge);
  gp_Pnt p1;
  gp_Pnt p2;
  Standard_Real alpha = 0, beta = 1;
  Standard_Real preci = Precision::PConfusion();
  Standard_Real firstPar, lastPar;
  if(prevPar < currPar) {
    firstPar = prevPar;
    lastPar  = currPar;
  }
  else {
    firstPar = currPar;
    lastPar  = prevPar;
  }
  if(Is2d) {
    p1 = myAC3d.Value(firstPar).Transformed(myLocation);
    p2 = myAC3d.Value(lastPar).Transformed(myLocation);
    Standard_Real fact = myAC3d.LastParameter() - myAC3d.FirstParameter();
    if( fact > Epsilon(myAC3d.LastParameter()) ) {
      alpha = ( firstPar - myAC3d.FirstParameter() ) / fact;
      beta  = ( lastPar - myAC3d.FirstParameter() ) / fact;
    }
  }
  else {
    p1 = myCurve->Value(firstPar);
    p2 = myCurve->Value(lastPar);
    Standard_Real fact = myLast - myFirst;
    if( fact > Epsilon(myLast) ) {
      alpha = ( firstPar - myFirst ) / fact;
      beta  = ( lastPar - myFirst ) / fact;
    }
  }
  const Standard_Boolean useLinearFirst = (alpha < preci);
  const Standard_Boolean useLinearLast = (1-beta < preci);
  TopLoc_Location EdgeLoc = myEdge.Location();
  ShapeAnalysis_Curve sac;
  gp_Pnt pproj;
  Standard_Real ppar1,ppar2;
  BRep_ListOfCurveRepresentation& tolist = (*((Handle(BRep_TEdge)*)&newEdge.TShape()))->ChangeCurves();
  Handle(BRep_GCurve) toGC;
  for (BRep_ListIteratorOfListOfCurveRepresentation toitcr (tolist); toitcr.More(); toitcr.Next()) {
    toGC = Handle(BRep_GCurve)::DownCast(toitcr.Value());
    if ( toGC.IsNull() ) continue;
    TopLoc_Location loc = ( EdgeLoc * toGC->Location() ).Inverted();
    if ( toGC->IsCurve3D() ) {
      if (!Is2d) {
	ppar1 = firstPar;
	ppar2 = lastPar;
      } 
      else {
	Handle(Geom_Curve) C3d = toGC->Curve3D();
	if (C3d.IsNull()) continue;
	Standard_Real first = toGC->First();
	Standard_Real last  = toGC->Last();
	Standard_Real len = last -first;
	gp_Pnt ploc1 = p1.Transformed(loc);
	gp_Pnt ploc2 = p2.Transformed(loc); 
	GeomAdaptor_Curve GAC(C3d,first,last);
	// CATIA bplseitli.model FAC1155 - Copy: protection for degenerated edges(3d case for symmetry)
	Standard_Real linFirst = first+alpha*len;
	Standard_Real linLast  = first+beta*len;
	Standard_Real dist1 = sac.NextProject(linFirst,GAC,ploc1,myPrecision,pproj,ppar1);
	Standard_Real dist2 = sac.NextProject(linLast,GAC,ploc2,myPrecision,pproj,ppar2);
	Standard_Boolean useLinear = Abs(ppar1-ppar2) < preci;
	
	gp_Pnt pos1 = C3d->Value ( linFirst );
	gp_Pnt pos2 = C3d->Value ( linLast );
	Standard_Real d01 = pos1.Distance ( ploc1 );
	Standard_Real d02 = pos2.Distance ( ploc2 );
	if ( useLinearFirst || useLinear || d01 <= dist1 || ( d01 < myPrecision && d01 <= 2 * dist1 ) ) 
	  ppar1 = linFirst;
	if ( useLinearLast || useLinear || d02 <= dist2 || ( d02 < myPrecision && d02 <= 2 * dist2 ) ) 
	  ppar2 = linLast;
      }
      if(ppar1 > ppar2) {
	Standard_Real tmpP = ppar2; ppar2 = ppar1; ppar1 = tmpP;
      }
      if(ppar2-ppar1 < preci) {
	if(ppar1-toGC->First() < preci)
	  ppar2+=2*preci;
	else if(toGC->Last()-ppar2 < preci)
	  ppar1-=2*preci;
	else {
	  ppar1 -= preci;
	  ppar2 += preci;
	}
      }
      toGC->SetRange ( ppar1, ppar2);
      //if(fabs(ppar1- firstPar) > Precision::PConfusion() || 
	//     fabs(ppar2 - lastPar) >Precision::PConfusion()) // by LSS
      if(ppar1!=firstPar || ppar2!=lastPar)
        samerange = Standard_False;
      
    }
    else if (toGC->IsCurveOnSurface()) { //continue;  || 

      Standard_Boolean localLinearFirst = useLinearFirst;
      Standard_Boolean localLinearLast  = useLinearLast;
      Handle(Geom2d_Curve) C2d = toGC->PCurve();
      Standard_Real first = toGC->First();
      Standard_Real last  = toGC->Last();
      Standard_Real len = last -first;
      Handle(Geom2dAdaptor_HCurve) AC2d  = new Geom2dAdaptor_HCurve(toGC->PCurve(),first,last);
      Handle(GeomAdaptor_HSurface) AdS = new GeomAdaptor_HSurface( toGC->Surface());
      Adaptor3d_CurveOnSurface Ad1(AC2d,AdS);
      ShapeAnalysis_Curve sac1;
      
      //gp_Pnt p1 = Ad1.Value(prevPar);
      //gp_Pnt p2 = Ad1.Value(currPar);
      gp_Pnt ploc1 = p1.Transformed(loc);
      gp_Pnt ploc2 = p2.Transformed(loc);
      // CATIA bplseitli.model FAC1155 - Copy: protection for degenerated edges
      Standard_Real linFirst = first+alpha*len;
      Standard_Real linLast  = first+beta*len;
      Standard_Real dist1 = sac1.NextProject(linFirst, Ad1, ploc1, myPrecision,pproj,ppar1);
      Standard_Real dist2 = sac1.NextProject(linLast, Ad1, ploc2, myPrecision,pproj,ppar2);

      Standard_Boolean isFirstOnEnd = (ppar1-first)/len < Precision::PConfusion();
      Standard_Boolean isLastOnEnd = (last-ppar2)/len < Precision::PConfusion();
      Standard_Boolean useLinear = Abs(ppar1-ppar2) < Precision::PConfusion();
      if(isFirstOnEnd && ! localLinearFirst)
	localLinearFirst = Standard_True;
      if(isLastOnEnd && ! localLinearLast)
	localLinearLast  = Standard_True;

      gp_Pnt pos1 = Ad1.Value ( linFirst );
      gp_Pnt pos2 = Ad1.Value ( linLast );
      Standard_Real d01 = pos1.Distance ( ploc1 );
      Standard_Real d02 = pos2.Distance ( ploc2 );
      if ( localLinearFirst || useLinear || d01 <= dist1 || ( d01 < myPrecision && d01 <= 2 * dist1 ) ) 
	ppar1 = linFirst;
      if ( localLinearLast  || useLinear || d02 <= dist2 || ( d02 < myPrecision && d02 <= 2 * dist2 ) ) 
	ppar2 = linLast;

      if(ppar1 > ppar2) {
	Standard_Real tmpP = ppar2; ppar2 = ppar1; ppar1 = tmpP;
      }
      ppar1 = CorrectParameter(C2d,ppar1);
      ppar2 = CorrectParameter(C2d,ppar2);
      if(ppar2-ppar1 < preci) {
	if(ppar1-toGC->First() < preci)
	  ppar2+=2*preci;
	else if(toGC->Last()-ppar2 < preci)
	  ppar1-=2*preci;
	else {
	  ppar1 -= preci;
	  ppar2 += preci;
	}
      }
      toGC->SetRange ( ppar1, ppar2);
      //if(fabs(ppar1 - firstPar) > Precision::PConfusion() || 
	//    fabs(ppar2 -lastPar) > Precision::PConfusion())// by LSS
      if(ppar1 != firstPar || ppar2 != lastPar)
	    samerange = Standard_False;
    }
  }
  B.SameRange(newEdge, samerange);
}


//=======================================================================
//function : IsSameRange
//purpose  : 
//=======================================================================

Standard_Boolean ShapeAnalysis_TransferParametersProj::IsSameRange() const
{

  if( !myInitOK || 
     (! myForceProj && myPrecision < myMaxTolerance && BRep_Tool::SameParameter(myEdge)))
    return ShapeAnalysis_TransferParameters::IsSameRange();
  else
    return Standard_False;
}


//=======================================================================
//function : ForceProjection
//purpose  : 
//=======================================================================

Standard_Boolean& ShapeAnalysis_TransferParametersProj::ForceProjection() 
{
  return myForceProj;
}

//=======================================================================
//function : CopyNMVertex
//purpose  : 
//=======================================================================

TopoDS_Vertex ShapeAnalysis_TransferParametersProj::CopyNMVertex (const TopoDS_Vertex& theV,
				   const TopoDS_Edge& toedge, 
				   const TopoDS_Edge& fromedge)
{
  TopoDS_Vertex anewV;
  if(theV.Orientation() != TopAbs_INTERNAL &&
     theV.Orientation() != TopAbs_EXTERNAL)
    return anewV;
  
  TopLoc_Location fromLoc;
  Standard_Real f1,l1;
  const Handle(Geom_Curve)& C1 = BRep_Tool::Curve(fromedge,fromLoc,f1,l1);
  fromLoc = fromLoc.Predivided(theV.Location());
  
  Standard_Real f2,l2;
  Handle(Geom_Curve) C2 = BRep_Tool::Curve(toedge,f2,l2);
  
  anewV = TopoDS::Vertex(theV.EmptyCopied());
  gp_Pnt apv = BRep_Tool::Pnt(anewV);
  
   BRep_ListOfPointRepresentation& alistrep = 
    (*((Handle(BRep_TVertex)*)&anewV.TShape()))->ChangePoints();
  
  BRep_ListIteratorOfListOfPointRepresentation itpr
	  ((*((Handle(BRep_TVertex)*) &theV.TShape()))->Points());
  
  Standard_Real aOldPar = RealLast();
  Standard_Boolean hasRepr = Standard_False;
  for ( ;itpr.More(); itpr.Next()) {
    const Handle(BRep_PointRepresentation)& pr = itpr.Value();
    if(pr.IsNull())
      continue;
    if(pr->IsPointOnCurve(C1,fromLoc)) {
      aOldPar = pr->Parameter();
      hasRepr =Standard_True;
      continue;
    }
    else if(pr->IsPointOnSurface()) {
      Handle(BRep_PointOnSurface) aOld = Handle(BRep_PointOnSurface)::DownCast(pr);
      Handle(BRep_PointOnSurface) aPS = new BRep_PointOnSurface(aOld->Parameter(),
								  aOld->Parameter2(),
								  aOld->Surface(),
								  aOld->Location());
      alistrep.Append(aPS);
      continue;
    }
    else if(pr->IsPointOnCurveOnSurface()) {
      Standard_Boolean found = Standard_False;
      BRep_ListIteratorOfListOfCurveRepresentation fromitcr
	((*((Handle(BRep_TEdge)*)&fromedge.TShape()))->ChangeCurves());
      
      for( ;fromitcr.More() && !found; fromitcr.Next()) {
	Handle(BRep_GCurve) fromGC = Handle(BRep_GCurve)::DownCast(fromitcr.Value());
	if ( fromGC.IsNull() || !fromGC->IsCurveOnSurface()) continue;
	
	TopLoc_Location aL = fromGC->Location();
	aL.Predivided(theV.Location());
	Handle(Geom_Surface) surface1 = fromGC->Surface();
	Handle(Geom2d_Curve) ac2d1 = fromGC->PCurve();
	if (pr->IsPointOnCurveOnSurface(ac2d1,surface1,aL)) {
      found = Standard_True;
	  if(!hasRepr) {
	    aOldPar = pr->Parameter();
	  }
	}
      }
      if(found) continue;
    }
    if(pr->IsPointOnCurve()) {
      Handle(BRep_PointOnCurve) aPRep = new BRep_PointOnCurve(pr->Parameter(),pr->Curve(),pr->Location());
      alistrep.Append(aPRep);
    }
    else if(pr->IsPointOnCurveOnSurface() ) {
      Handle(BRep_PointOnCurveOnSurface) aPonCS = 
	new BRep_PointOnCurveOnSurface(pr->Parameter(),pr->PCurve(),pr->Surface(),pr->Location()); 
      alistrep.Append(aPonCS);
    }
  }
  Standard_Real apar = aOldPar;
  Standard_Real aTol = BRep_Tool::Tolerance(theV);
  if(!hasRepr || (fabs(f1-f2) > Precision::PConfusion() || fabs(l1-l2)> Precision::PConfusion())) {
    gp_Pnt projP;
    ShapeAnalysis_Curve sae;
    Standard_Real adist = sae.Project(C2,apv,Precision::Confusion(),projP,apar);
    if(aTol < adist)
      aTol = adist;
  }
  BRep_Builder aB;
  aB.UpdateVertex(anewV,apar,toedge,aTol);
  
  //update tolerance
  Standard_Boolean needUpdate = Standard_False;
  gp_Pnt aPV = (*((Handle(BRep_TVertex)*)&anewV.TShape()))->Pnt();
  TopLoc_Location toLoc = toedge.Location();
  BRep_ListIteratorOfListOfCurveRepresentation toitcr
	((*((Handle(BRep_TEdge)*)&toedge.TShape()))->ChangeCurves());
      
  for( ;toitcr.More() ; toitcr.Next()) {
	Handle(BRep_GCurve) toGC = Handle(BRep_GCurve)::DownCast(toitcr.Value());
	if ( toGC.IsNull() || !toGC->IsCurveOnSurface()) continue;
	
	TopLoc_Location aL = (toLoc*toGC->Location()).Predivided(theV.Location());
	//aL.Predivided(theV.Location());
	Handle(Geom_Surface) surface1 = toGC->Surface();
	Handle(Geom2d_Curve) ac2d1 = toGC->PCurve();
	gp_Pnt2d aP2d = ac2d1->Value(apar);
    gp_Pnt aP3d = surface1->Value(aP2d.X(),aP2d.Y());
    aP3d.Transform(aL.Transformation());
    Standard_Real adist = aPV.Distance(aP3d);
    if(adist > aTol) {
      aTol = adist;
      needUpdate = Standard_True;
    }

  } 
  if(needUpdate)
    aB.UpdateVertex(anewV,aTol);
  return anewV;
}

//=======================================================================
//function : CopyNMVertex
//purpose  : 
//=======================================================================

TopoDS_Vertex ShapeAnalysis_TransferParametersProj::CopyNMVertex (const TopoDS_Vertex& theV,
				   const TopoDS_Face& toFace, 
				   const TopoDS_Face& fromFace)
{
  TopoDS_Vertex anewV;
  if(theV.Orientation() != TopAbs_INTERNAL &&
     theV.Orientation() != TopAbs_EXTERNAL)
    return anewV;
  

  TopLoc_Location fromLoc;
  TopLoc_Location toLoc;
  Handle(Geom_Surface) fromSurf = BRep_Tool::Surface(fromFace,fromLoc);
  Handle(Geom_Surface) toSurf = BRep_Tool::Surface(toFace,toLoc);
  fromLoc = fromLoc.Predivided(theV.Location());
  
  anewV = TopoDS::Vertex(theV.EmptyCopied());
  gp_Pnt apv = BRep_Tool::Pnt(anewV);
  
  
  BRep_ListOfPointRepresentation& alistrep = 
    (*((Handle(BRep_TVertex)*)&anewV.TShape()))->ChangePoints();
  
  BRep_ListIteratorOfListOfPointRepresentation itpr
	  ((*((Handle(BRep_TVertex)*) &theV.TShape()))->Points());
  
  Standard_Boolean hasRepr = Standard_False;
  Standard_Real apar1=0., apar2=0.;
  for ( ;itpr.More(); itpr.Next()) {
    const Handle(BRep_PointRepresentation)& pr = itpr.Value();
    if(pr.IsNull())
      continue;
    TopLoc_Location aLoc = pr->Location();
    if( pr->IsPointOnCurveOnSurface()) {
      Handle(BRep_PointOnCurveOnSurface) aPonCS = 
	    new BRep_PointOnCurveOnSurface(pr->Parameter(),pr->PCurve(),pr->Surface(),aLoc); 
      alistrep.Append(aPonCS);
    }
    else if(pr->IsPointOnCurve()) {
      Handle(BRep_PointOnCurve) aPRep = new BRep_PointOnCurve(pr->Parameter(),pr->Curve(),aLoc);
      alistrep.Append(aPRep);
    }
    else if(pr->IsPointOnSurface()) {
      Handle(BRep_PointOnSurface) aOld = Handle(BRep_PointOnSurface)::DownCast(pr);
      
      if(pr->IsPointOnSurface(fromSurf,fromLoc)) {
	apar1= aOld->Parameter();
	apar2 = aOld->Parameter2();
	hasRepr = Standard_True;
      }
      else {
	Handle(BRep_PointOnSurface) aPS = new BRep_PointOnSurface(aOld->Parameter(),
								    aOld->Parameter2(),
								    aOld->Surface(),
								    aOld->Location());
	alistrep.Append(aPS);
      }
    }
    
  }
  Standard_Real aTol = BRep_Tool::Tolerance(anewV);
  if(!hasRepr || (fromSurf != toSurf || fromLoc != toLoc)) {
    Handle(Geom_Surface) aS = BRep_Tool::Surface(toFace);
    Handle(ShapeAnalysis_Surface) aSurfTool = new ShapeAnalysis_Surface(aS);
    gp_Pnt2d aP2d = aSurfTool->ValueOfUV(apv,Precision::Confusion());
    apar1 = aP2d.X();
    apar2 = aP2d.Y();
    
    if(aTol < aSurfTool->Gap())
      aTol = aSurfTool->Gap() + 0.1*Precision::Confusion();
    //Handle(BRep_PointOnSurface) aPS = new BRep_PointOnSurface(aP2d.X(),aP2d.Y(),toSurf,toLoc);
    //alistrep.Append(aPS);
  }
  
  BRep_Builder aB;
  aB.UpdateVertex(anewV,apar1,apar2,toFace,aTol);
  return anewV;
}