summaryrefslogtreecommitdiff
path: root/src/TopOpeBRepDS/TopOpeBRepDS_GapFiller.cxx
blob: 2740bb557edabc847cee720b797e3c6fab430502 (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
// File:	TopOpeBRepDS_GapFiller.cxx
// Created:	Wed Aug 19 16:30:29 1998
// Author:	Yves FRICAUD
//		<yfr@claquox.paris1.matra-dtv.fr>


#include <TopOpeBRepDS_GapFiller.ixx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Shape.hxx>
#include <TopAbs_ShapeEnum.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopOpeBRepDS_ListIteratorOfListOfInterference.hxx>
#include <Precision.hxx>
#include <TopOpeBRepDS_connex.hxx>
#include <TopOpeBRepDS_Point.hxx>
#include <TColgp_SequenceOfPnt.hxx>
#include <TColStd_MapOfTransient.hxx>

#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>

#include <gp.hxx>
#include <gp_Pnt.hxx>
#include <gp_Pln.hxx>
#include <gp_Dir.hxx>
#include <gp_Vec.hxx>

#include <Geom2d_Curve.hxx>

#ifdef DEB
Standard_IMPORT Standard_Boolean TopOpeBRepDS_GettraceGAP();
Standard_IMPORT Standard_Boolean TopOpeBRepDS_GetcontextNOGAP();
#endif

//=======================================================================
//function : TopOpeBRepDS_GapFiller
//purpose  : 
//=======================================================================

TopOpeBRepDS_GapFiller::TopOpeBRepDS_GapFiller(const Handle(TopOpeBRepDS_HDataStructure)& HDS) 
:myHDS(HDS)
{
  myGapTool   = new TopOpeBRepDS_GapTool(HDS);
  myAsso = new TopOpeBRepDS_Association();
}


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

void TopOpeBRepDS_GapFiller::Perform() 
{

#ifdef DEB
  if (TopOpeBRepDS_GetcontextNOGAP () != 0) return;
#endif

  myGapTool->Init(myHDS);
  TColStd_MapOfInteger View;
  //------------------------------------------------------------
  // APPAIRAGE ...
  //------------------------------------------------------------
  Standard_Integer NbCurves = myHDS->NbCurves();
  for (Standard_Integer i = 1; i <= NbCurves; i++) {
    TopOpeBRepDS_ListOfInterference& LI = myHDS->ChangeDS().ChangeCurveInterferences(i);
    for (TopOpeBRepDS_ListIteratorOfListOfInterference it(LI); it.More(); it.Next()) {
      TopOpeBRepDS_ListOfInterference ALI;
      const Handle(TopOpeBRepDS_Interference)& I = it.Value();
      if (I->GeometryType() == TopOpeBRepDS_POINT) {
	if (View.Add(I->Geometry())) {
	  FindAssociatedPoints (I,ALI);
	  myAsso->Associate(I,ALI);
	}
      }
    }
  }
  //--------------------------------------------------------
  // Modification des Geometries
  //--------------------------------------------------------
  BuildNewGeometries();
}

//=======================================================================
//function : Contains
//purpose  : 
//=======================================================================

Standard_Boolean Contains (const TopoDS_Shape& F,const TopoDS_Shape& E)
{
  TopExp_Explorer exp;
  for (exp.Init(F,E.ShapeType()); exp.More(); exp.Next()){
//  for (TopExp_Explorer exp(F,E.ShapeType()); exp.More(); exp.Next()){
    if (exp.Current().IsSame(E)) return 1;
  }
  return 0;
}


//=======================================================================
//function : FindAssociatedPoints
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::FindAssociatedPoints(const Handle(TopOpeBRepDS_Interference)& I,
						  TopOpeBRepDS_ListOfInterference&         LI) 
{
  TopOpeBRepDS_ListIteratorOfListOfInterference itSI(myGapTool->SameInterferences(I));
  for (; itSI.More();itSI.Next()){ 
    if (myAsso->HasAssociation(itSI.Value())) return;
  }

#ifdef DEB
  if (TopOpeBRepDS_GettraceGAP() == 1) {
    cout<<endl;
    cout<<"-----------------------------------------------------"<<endl;
    cout<<" Association Point "<<I->Geometry()<<endl;
  }
#endif

  //------------------------------------------------------------------------
  // PREMIERE PASSE : Recherche de l association parmi les points qui sont:
  //                  - sur la meme arete
  //                  - sur l autre face connexe a l arete.
  //                  - Dans la meme face coupante.
  //------------------------------------------------------------------------
  //----------------------------------------------
  // LI = { ensemble des interference sur l'arete}
  //----------------------------------------------
  TopoDS_Shape E ;
  if (!myGapTool->EdgeSupport(I,E)) {
    return;
  }
  AddPointsOnShape(E,LI);
  
  TopoDS_Face F1,F2,F;
  if (!myGapTool->FacesSupport(I,F1,F2)) {
    LI.Clear(); return;
  }

  if (!Contains(F1,E)) {F = F2; F2 = F1; F1 = F;}

  const TopTools_ListOfShape& LF = FDSCNX_EdgeConnexitySameShape(E,myHDS);
  //------------------------------------------------------------------------
  // Si le point de I est sur deux faces connexes a E => connexite des sections
  // => pas d association a faire.
  //------------------------------------------------------------------------
//   for (TopTools_ListIteratorOfListOfShape itLF(LF); itLF.More(); itLF.Next()) {
   TopTools_ListIteratorOfListOfShape itLF(LF) ;
   for ( ; itLF.More(); itLF.Next()) {
    if (!itLF.Value().IsSame(F1)) {
      if (IsOnFace(I,TopoDS::Face(itLF.Value()))) {
	LI.Clear(); return;
      }
    }
  }

  //------------------------------------------------------------------------
  // LI = { ensemble des interference sur l'arete && sur une autre section ds 
  // la Face connexe par E} 
  //------------------------------------------------------------------------

  for (itLF.Initialize(LF); itLF.More(); itLF.Next()) {
    if (!itLF.Value().IsSame(F1)) {
      FilterByFace(TopoDS::Face(itLF.Value()),LI);
    }
  }
  //--------------------------------------------
  // LI = LI && point dans la meme face Support2
  //--------------------------------------------
  if (!LI.IsEmpty()) 
    FilterByFace (F2, LI);

  //-------------------------------
  // Controle et Selection metrique 
  //-------------------------------
  if (!LI.IsEmpty()) 
    FilterByIncidentDistance (F2,I,LI);

  if (!LI.IsEmpty()) {
    LI.Append(I);
  }
   
#ifdef DEB
   if (TopOpeBRepDS_GettraceGAP() != 0) {  
     TopOpeBRepDS_ListIteratorOfListOfInterference it(LI);
     for  (it.Initialize(LI); it.More(); it.Next()) {
       cout<<" "<<it.Value()->Geometry();
     }
     cout<<endl;
   }
#endif
 }


//=======================================================================
//function : CheckConnexity
//purpose  : 
//=======================================================================

//Standard_Boolean TopOpeBRepDS_GapFiller::CheckConnexity(TopOpeBRepDS_ListOfInterference& LI) 
Standard_Boolean TopOpeBRepDS_GapFiller::CheckConnexity(TopOpeBRepDS_ListOfInterference& ) 
{
  return 1;
}

//=======================================================================
//function : AddPointsOnFace
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::AddPointsOnShape (const TopoDS_Shape&               S,
					       TopOpeBRepDS_ListOfInterference& LI) 
{
  const TopOpeBRepDS_ListOfInterference& LIOnE = myHDS->DS().ShapeInterferences(S);
  for (TopOpeBRepDS_ListIteratorOfListOfInterference it(LIOnE); it.More(); it.Next()){
    LI.Append(it.Value());
  }
}

//=======================================================================
//function : AddPointsOnConnexShape
//purpose  : 
//=======================================================================

//void TopOpeBRepDS_GapFiller::AddPointsOnConnexShape(const TopoDS_Shape&         //           F,
//						    const TopOpeBRepDS_ListOfInterference& LI) 
void TopOpeBRepDS_GapFiller::AddPointsOnConnexShape(const TopoDS_Shape&                    ,
						    const TopOpeBRepDS_ListOfInterference& ) 
{
}


//=======================================================================
//function : FilterbyFace
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::FilterByFace(const TopoDS_Face&               F,
					  TopOpeBRepDS_ListOfInterference& LI) 
{
  //------------------------------------------------------------------------
  // il ne restera dans LI que les interference dont une des representation 
  // a ete calculee sur F .
  //------------------------------------------------------------------------
  TopOpeBRepDS_ListIteratorOfListOfInterference it(LI); 
  while (it.More()) {
    if (!IsOnFace(it.Value(),F)) {
      LI.Remove(it);
    }
    else {	
      it.Next();
    }
  }
}

//=======================================================================
//function : IsOnFace
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepDS_GapFiller::IsOnFace
(const Handle(TopOpeBRepDS_Interference)& I,
 const TopoDS_Face&                       F) const
{
  TopOpeBRepDS_Curve C;
  if (myGapTool->Curve(I,C)) {
    TopoDS_Shape S1,S2;
    C.GetShapes(S1,S2);
    if (S1.IsSame(F)) return 1;
    if (S2.IsSame(F)) return 1;
  }
  return 0;
}


//=======================================================================
//function : FilterbyEdge
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::FilterByEdge(const TopoDS_Edge&               E,
					  TopOpeBRepDS_ListOfInterference& LI) 
{  
  // il ne restera dans LI que les interference dont une des representation 
  // a pour support E
  TopOpeBRepDS_ListIteratorOfListOfInterference it(LI);
  while (it.More()) {
    if (!IsOnEdge(it.Value(),E)) {
      LI.Remove(it);
    }
    else {	
      it.Next();
    }
  }
}

//=======================================================================
//function : IsOnEdge
//purpose  : 
//=======================================================================

Standard_Boolean TopOpeBRepDS_GapFiller::IsOnEdge
(const Handle(TopOpeBRepDS_Interference)& I,
 const TopoDS_Edge&                       E) const
{
  const TopOpeBRepDS_ListOfInterference& LI = myGapTool->SameInterferences(I);
  for (TopOpeBRepDS_ListIteratorOfListOfInterference it(LI); it.More(); it.Next()) {
    const Handle(TopOpeBRepDS_Interference)& IC = it.Value();
    if (IC->SupportType() == TopOpeBRepDS_EDGE) {
      const TopoDS_Shape& S1 = myHDS->Shape(IC->Support());
      if (S1.IsSame(E)) return 1;
    }
  }
  return 0;
}



//=======================================================================
//function : Normale
//purpose  : 
//=======================================================================

static Standard_Boolean Normal(const Handle(TopOpeBRepDS_GapTool)&      A,
			       const Handle(TopOpeBRepDS_HDataStructure)& HDS,
			       const Handle(TopOpeBRepDS_Interference)&   I,
			       const TopoDS_Face&                         F,
			       gp_Dir&                                    D)

{ 
  TopOpeBRepDS_Kind GK,SK;
  Standard_Integer  IG,IS;
  gp_Pnt   PS;
  gp_Vec   TU,TV,N;
  gp_Pnt2d P2d;
  
  BRepAdaptor_Surface S(F);
  if (S.GetType() == GeomAbs_Plane) {
    D = S.Plane().Axis().Direction();
    return 1;
  }
  
  const TopOpeBRepDS_ListOfInterference& LI = A->SameInterferences(I);
  for (TopOpeBRepDS_ListIteratorOfListOfInterference it(LI); it.More(); it.Next()) {
    const Handle(TopOpeBRepDS_Interference)&   IC = it.Value();
    IC->GKGSKS(GK,IG,SK,IS);
    if (SK == TopOpeBRepDS_CURVE) {
      const TopOpeBRepDS_Curve& C = HDS->Curve(IS);
      Standard_Real P   = (*((Handle(TopOpeBRepDS_CurvePointInterference)*)&IC))->Parameter();
      
      TopoDS_Shape S1,S2;
      C.GetShapes(S1,S2);
      if (F.IsSame(S1)) {
	if (C.Curve1().IsNull()) return 0;
	P2d = C.Curve1()->Value(P);
      }
      else {
	if (C.Curve2().IsNull()) return 0;
	P2d = C.Curve2()->Value(P);
      }
      
      if (S.UContinuity() >= GeomAbs_C1 && S.VContinuity() >= GeomAbs_C1) {
	S.D1(P2d.X(),P2d.Y(),PS,TU,TV);
	N = TU^TV;
//	if (N.SquareMagnitude() >= gp::Resolution()); { // !!
	if (N.SquareMagnitude() >= gp::Resolution()) {
	  D = gp_Dir(N);
	  return 1;
	}
      }
      return 0;
    }
  } 
  return 0;
}

//=======================================================================
//function : FilterByIncidentDistance
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::FilterByIncidentDistance(const TopoDS_Face& F,
						      const Handle(TopOpeBRepDS_Interference)& I,
						      TopOpeBRepDS_ListOfInterference& LI) 
{
  Standard_Real DistMin = Precision::Infinite();  
  Handle(TopOpeBRepDS_Interference) ISol;
  
  const TopOpeBRepDS_Point& PI1 = myHDS->Point(I->Geometry());
  const gp_Pnt              GPI = PI1.Point();
 
  BRepAdaptor_Surface S(F,0);

  Standard_Real TolDef  = 0.94;// cos(20degre);
  Standard_Real TolDist = 20*PI1.Tolerance();
  gp_Dir N1,N2;
  Standard_Boolean Ok1 = Normal(myGapTool,myHDS,I,F,N1);
  
  for (TopOpeBRepDS_ListIteratorOfListOfInterference it(LI);it.More(); it.Next()) {
    
    const Handle(TopOpeBRepDS_Interference)& CI = it.Value();
  
    if (CI->HasSameGeometry(I)) continue;

    Standard_Boolean          Ok2  = Normal(myGapTool,myHDS,CI,F,N2);
    const TopOpeBRepDS_Point& P    = myHDS->Point((CI->Geometry()));
    const gp_Pnt              GP   = P.Point();
    Standard_Real             Dist = GP.Distance(GPI);
    
    //---------------------------------------------------------------------------
    // Test resjection 
    // Si les points sont plutot ecartes, on regarde si les Normales sur la face
    // sont proches   
    //---------------------------------------------------------------------------
    if (Dist > TolDist) {
      if (Ok1 && Ok2) {
	if (N1.Dot(N2) < TolDef) {
	  continue;  // Reject
	}
      }
      continue; // Reject
    }
    
    if (Dist < DistMin) {
      DistMin = Dist;
      ISol    = it.Value();
    }
  }

  LI.Clear();
  if (!ISol.IsNull()) {
    LI.Append(ISol);
#ifdef DEB  
    if (TopOpeBRepDS_GettraceGAP() == 1){
      cout << " Distance Minimum :"<<DistMin<<endl; 
    }
#endif
  }
}

//=======================================================================
//function : BuildNewPoint
//purpose  : 
//=======================================================================
//unreferenced function, commented    
/*
#ifdef DEB  
static void BuildNewPoint (const TColgp_SequenceOfPnt& LP,
			   const Standard_Real&    TolMax,
			   TopOpeBRepDS_Point& Point) 
{
  if (LP.IsEmpty()) return;
  Standard_Real xp = 0.,yp = 0.,zp = 0.;

  Standard_Integer i,Nb = LP.Length();
  for (i = 1; i <= Nb; i++) {
    const gp_Pnt& CP = LP(i);
    xp+= CP.X();yp+=CP.Y();zp+=CP.Z();
  }
  xp/=Nb;yp/=Nb;zp/=Nb;
  gp_Pnt NP(xp,yp,zp);  

  Standard_Real DistMax = 0;
  for (i = 1; i <= Nb; i++) {
    const gp_Pnt& CP = LP(i);
    DistMax = Max(CP.Distance(NP),DistMax);
  }
  Point.ChangePoint() = NP;
  Point.Tolerance(DistMax+TolMax);
}
#endif
*/
//=======================================================================
//function : ReBuildGeom
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::ReBuildGeom(const Handle(TopOpeBRepDS_Interference)& I,
					 TColStd_MapOfInteger&                    View) 
{
  if (!myAsso->HasAssociation(I)) return;

  TopOpeBRepDS_ListOfInterference& LI = myAsso->Associated(I);
  TopOpeBRepDS_ListIteratorOfListOfInterference it(LI);

#ifdef DEB
  if (TopOpeBRepDS_GettraceGAP() == 1) {
    cout <<"Points ";
    for  (it.Initialize(LI); it.More(); it.Next()) {
      cout <<" "<<it.Value()->Geometry();
    }
    cout <<endl<<" ---->";
  }
#endif

  Standard_Real TolMax = 0,UMin = Precision::Infinite();
  Standard_Real UMax = -UMin, U;
  TopoDS_Edge   E,CE ;
  myGapTool->EdgeSupport(I,E);

  // Construction du nouveau point
  for  (it.Initialize(LI); it.More(); it.Next()) {
    TopOpeBRepDS_Point PP = myHDS->Point(it.Value()->Geometry());
    TolMax = Max(TolMax,PP.Tolerance());
    if (myGapTool->ParameterOnEdge (it.Value(),E,U)) {
      UMin = Min(UMin,U);
      UMax = Max(UMax,U);
    }
    myGapTool->EdgeSupport(it.Value(),CE);
    if (!CE.IsSame(E)) {
#ifdef DEB   
      if (TopOpeBRepDS_GettraceGAP() == 1){
	cout <<"GapFiller : points pas sur la meme edge -> cas non traite"<<endl;
      }
#endif
      return;
    }
  }
  U = (UMax+UMin)*0.5;
  BRepAdaptor_Curve C(E); gp_Pnt GP = C.Value(U);
  TopOpeBRepDS_Point P(GP,TolMax);

  // Mise a jour.
  Standard_Integer IP = myHDS->ChangeDS().AddPoint(P);
#ifdef DEB
 if (TopOpeBRepDS_GettraceGAP() == 1) {
   cout <<" New Point : "<<IP<<endl;
 }
#endif
  for (it.Initialize(LI); it.More(); it.Next()) {
    View.Add(it.Value()->Geometry());
    myGapTool->SetParameterOnEdge(it.Value(),E,U); 
    myGapTool->SetPoint(it.Value(),IP);
  }
  myGapTool->SetParameterOnEdge(I,E,U);
  myGapTool->SetPoint(I,IP);
  View.Add(IP);
}

//=======================================================================
//function : BuildNewGeometries
//purpose  : 
//=======================================================================

void TopOpeBRepDS_GapFiller::BuildNewGeometries() 
{
  TColStd_MapOfInteger View;
  Standard_Integer NbCurves = myHDS->NbCurves();
  Standard_Integer NbPoints = myHDS->NbPoints();
  for (Standard_Integer i = 1; i <= NbCurves; i++) {
    TopOpeBRepDS_ListOfInterference& LI = myHDS->ChangeDS().ChangeCurveInterferences(i);
    for (TopOpeBRepDS_ListIteratorOfListOfInterference it(LI); it.More(); it.Next()) {
      Handle(TopOpeBRepDS_Interference) I = it.Value();
      Standard_Integer IP = I->Geometry();
      if (View.Add(IP) && IP <= NbPoints) ReBuildGeom(I,View);
    }
  } 
}