summaryrefslogtreecommitdiff
path: root/src/LocOpe/LocOpe_Gluer.cxx
blob: e4b33a80d6d07fe3ca785a4f05ab932f5e9249cc (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
// File:	LocOpe_Gluer.cxx
// Created:	Tue Jan 30 11:58:43 1996
// Author:	Jacques GOUSSARD
//		<jag@bravox>


#include <LocOpe_Gluer.ixx>

#include <LocOpe_Spliter.hxx>
#include <LocOpe_GluedShape.hxx>
#include <LocOpe_WiresOnShape.hxx>
#include <LocOpe_Generator.hxx>
#include <LocOpe.hxx>

#include <TopExp_Explorer.hxx>
#include <TopExp.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Vec.hxx>
#include <Geom_Surface.hxx>
#include <Geom2d_Curve.hxx>
#include <GeomAdaptor_Surface.hxx>
#include <Extrema_ExtPS.hxx>
#include <BRepExtrema_ExtPF.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>

#include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <Precision.hxx>


#include <Standard_ConstructionError.hxx>


static TopAbs_Orientation GetOrientation(const TopoDS_Face&,
					 const TopoDS_Face&);


static Standard_Boolean Contains(const TopTools_ListOfShape&,
				 const TopoDS_Shape&);


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

void LocOpe_Gluer::Init(const TopoDS_Shape& Sbase,
			const TopoDS_Shape& Snew)
{
  mySb = Sbase;
  mySn = Snew;
  myMapEF.Clear();
  myMapEE.Clear();
  myDescF.Clear();
  myDone = Standard_False;
  myOri  = TopAbs_INTERNAL;
  myOpe  = LocOpe_INVALID;
}
  

//=======================================================================
//function : Bind
//purpose  : 
//=======================================================================

void LocOpe_Gluer::Bind(const TopoDS_Face& Fnew,
			const TopoDS_Face& Fbase)
{
  TopExp_Explorer exp(mySn,TopAbs_FACE);

  for (; exp.More(); exp.Next()) {
    if (exp.Current().IsSame(Fnew)) {
      break;
    }
  }
  if (!exp.More()) {
    Standard_ConstructionError::Raise();
  }

  TopoDS_Shape aLocalFace =  Fnew.Oriented(exp.Current().Orientation());
  TopoDS_Face Fnor = TopoDS::Face(aLocalFace);
//  TopoDS_Face Fnor = TopoDS::Face(Fnew.Oriented(exp.Current().Orientation()));

  for (exp.Init(mySb,TopAbs_FACE); exp.More(); exp.Next()) {
    if (exp.Current().IsSame(Fbase)) {
      break;
    }
  }
  if (!exp.More()) {
    Standard_ConstructionError::Raise();
  }

  aLocalFace = Fbase.Oriented(exp.Current().Orientation());
  TopoDS_Face Fbor = TopoDS::Face(aLocalFace);
//  TopoDS_Face Fbor = TopoDS::Face(Fbase.Oriented(exp.Current().Orientation()));
  TopAbs_Orientation Ori = GetOrientation(Fnor,Fbor);

  if (myOri == TopAbs_INTERNAL) {
    myOri = Ori; 
    if (myOri == TopAbs_REVERSED) {
      mySn.Reverse();
      myOpe = LocOpe_CUT;
    }
    else {
      myOpe = LocOpe_FUSE;
    }
  }
  else if (Ori != TopAbs_FORWARD) {
    myOpe = LocOpe_INVALID;
  }

  for (exp.Init(Fnor, TopAbs_EDGE); exp.More(); exp.Next()) {
    const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
//    if (!myMapEF.IsBound(edg)) {
//      myMapEF.Bind(edg,Fbor);
    if (!myMapEF.Contains(edg)) {
      myMapEF.Add(edg,Fbor);
    }
//    else if (!myMapEF(edg).IsSame(Fbor)) {
    else if (!myMapEF.FindFromKey(edg).IsSame(Fbor)) {
//      myMapEF.UnBind(edg); // edg sur 2 face. a binder avec l`edge commun
      myMapEF.ChangeFromKey(edg).Nullify();
      // edg sur 2 face. a binder avec l`edge commun
    }
  }
//  myMapEF.Bind(Fnor,Fbor);
  myMapEF.Add(Fnor,Fbor);
}


//=======================================================================
//function : Bind
//purpose  : 
//=======================================================================

void LocOpe_Gluer::Bind(const TopoDS_Edge& Enew,
			const TopoDS_Edge& Ebase)
{
  if (myMapEE.IsBound(Enew) && !myMapEE(Enew).IsSame(Ebase)) {
    Standard_ConstructionError::Raise();
  }
  myMapEE.Bind(Enew,Ebase);
}


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

void LocOpe_Gluer::Perform()
{
  Standard_Integer ind ;
  if (myDone) {
    return;
  }
  if (mySb.IsNull() || mySn.IsNull() || 
      myMapEF.IsEmpty() || myOpe ==LocOpe_INVALID) {
    Standard_ConstructionError::Raise();
  }

  Handle(LocOpe_WiresOnShape) theWOnS = new LocOpe_WiresOnShape(mySb);
  Handle(LocOpe_GluedShape)  theGS   = new LocOpe_GluedShape(mySn);

  Standard_Integer lmap = myMapEF.Extent();

  for ( ind = 1; ind <= lmap; ind++) {
    TopoDS_Shape S = myMapEF.FindKey(ind);
    if (S.ShapeType() == TopAbs_EDGE) {
      TopoDS_Shape S2 = myMapEF(ind);
      if (!S2.IsNull()) {
	theWOnS->Bind(TopoDS::Edge(S), TopoDS::Face(S2));
      }
    }
    else { // TopAbs_FACE
      theGS->GlueOnFace(TopoDS::Face(S));
    }
  }
  
  TopTools_DataMapIteratorOfDataMapOfShapeShape itm(myMapEE);
  for (; itm.More(); itm.Next()) {
    theWOnS->Bind(TopoDS::Edge(itm.Key()), TopoDS::Edge(itm.Value()));
  }
  
  theWOnS->BindAll();
  
  if (!theWOnS->IsDone()) {
    return;
  }

  LocOpe_Spliter theSplit(mySb);
  theSplit.Perform(theWOnS);
  if (!theSplit.IsDone()) {
    return;
  }
  // Mise a jour des descendants
//  for (TopExp_Explorer exp(mySb,TopAbs_FACE); exp.More(); exp.Next()) {
  TopExp_Explorer exp(mySb,TopAbs_FACE) ;
  for ( ; exp.More(); exp.Next()) {
    myDescF.Bind(exp.Current(),theSplit.DescendantShapes(exp.Current()));
  }

  for (exp.Init(mySn,TopAbs_FACE); exp.More(); exp.Next()) {
    TopTools_ListOfShape thelist;
    myDescF.Bind(exp.Current(), thelist);
    if (Contains(theGS->OrientedFaces(),exp.Current())) {
      myDescF(exp.Current()).Append(exp.Current());
    }
  }

  LocOpe_Generator theGen(theSplit.ResultingShape());
  theGen.Perform(theGS);

  myDone = theGen.IsDone();
  if (myDone) {
    myRes = theGen.ResultingShape();

    AddEdges();

    // Mise a jour des descendants
    TopTools_DataMapIteratorOfDataMapOfShapeListOfShape itd;
    for (itd.Initialize(myDescF);
	 itd.More(); 
	 itd.Next()) {
      TopTools_ListOfShape newDesc;
      TopTools_ListIteratorOfListOfShape itl;
      for (itl.Initialize(itd.Value());
	   itl.More();
	   itl.Next()) {
	TopTools_ListIteratorOfListOfShape itl2
	  (theGen.DescendantFace(TopoDS::Face(itl.Value())));
	for (; itl2.More(); itl2.Next()) {
	  const TopoDS_Face& descface =TopoDS::Face(itl2.Value());
	  if (!descface.IsNull()) { // sinon la face a disparu
	    newDesc.Append(descface);
	  }
	}
      }
      myDescF(itd.Key()) = newDesc;
    }
  }

  // recodage des regularites
  TopTools_IndexedDataMapOfShapeListOfShape theMapEF1, theMapEF2;
  TopExp::MapShapesAndAncestors(mySn,TopAbs_EDGE,TopAbs_FACE,theMapEF1);
  TopExp::MapShapesAndAncestors(myRes,TopAbs_EDGE,TopAbs_FACE,theMapEF2);
  
  for (ind = 1; ind <= theMapEF1.Extent(); ind++) {
    const TopoDS_Edge& edg = TopoDS::Edge(theMapEF1.FindKey(ind));
    const TopTools_ListOfShape& LL = theMapEF1(ind);
    if (LL.Extent() == 2) {
      const TopoDS_Face& fac1 = TopoDS::Face(LL.First());
      const TopoDS_Face& fac2 = TopoDS::Face(LL.Last());
      GeomAbs_Shape thecont = BRep_Tool::Continuity(edg,fac1,fac2);
      if (thecont >= GeomAbs_G1) {
	// on essaie de recoder
	Standard_Integer ind2 = theMapEF2.FindIndex(edg);
	if (ind2 != 0) {
	  const TopTools_ListOfShape& LL2 = theMapEF2(ind2);
	  if (LL2.Extent() == 2) {
	    const TopoDS_Face& ff1 = TopoDS::Face(LL2.First());
	    const TopoDS_Face& ff2 = TopoDS::Face(LL2.Last());
	    if ((ff1.IsSame(fac1) && ff2.IsSame(fac2)) ||
		(ff1.IsSame(fac2) && ff2.IsSame(fac1))) {

	    }
	    else {
	      BRep_Builder B;
	      B.Continuity(edg, ff1,ff2,thecont);
	    }
	  }
	}
      }	
    }
  }  
  //creation de la liste d`edge
  theWOnS->InitEdgeIterator();
  while(theWOnS->MoreEdge()) {
    TopoDS_Edge edg = theWOnS->Edge();
    for (ind = 1; ind <= theMapEF2.Extent(); ind++) {
      const TopoDS_Edge& edg1 = TopoDS::Edge(theMapEF2.FindKey(ind));
      if(edg1.IsSame(edg)) {
	myEdges.Append(edg); 
	// recodage eventuel des regularites sur cet edge
	const TopTools_ListOfShape& L = theMapEF2(ind);
	if (L.Extent() == 2) {
	  const TopoDS_Face& fac1 = TopoDS::Face(L.First());
	  const TopoDS_Face& fac2 = TopoDS::Face(L.Last());
	  if(LocOpe::TgtFaces(edg, fac1, fac2))  {
	    myTgtEdges.Append(edg);
	    GeomAbs_Shape thecont = BRep_Tool::Continuity(edg,fac1,fac2);
	    if (thecont < GeomAbs_G1) {
	      BRep_Builder B;
	      B.Continuity(edg, fac1,fac2,GeomAbs_G1);
	    }
	  }      
	}
      }
    }
    theWOnS->NextEdge();
  }

      // recodage eventuel des regularites sur cet edge
}




//=======================================================================
//function : DescendantFaces
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& LocOpe_Gluer::DescendantFaces
   (const TopoDS_Face& F) const
{
  if (!myDone) {StdFail_NotDone::Raise();}
  if (myDescF.IsBound(F))
    return myDescF(F);
  static TopTools_ListOfShape nullList;
  return nullList;
}



//=======================================================================
//function : GetOrientation
//purpose  : 
//=======================================================================

static TopAbs_Orientation GetOrientation(const TopoDS_Face& Fn,
					 const TopoDS_Face& Fb)
{

  Handle(Geom_Surface) Sn,Sb;
  Sn = BRep_Tool::Surface(Fn);
  Sb = BRep_Tool::Surface(Fb);
  

  // Find a point on Sb
  TopExp_Explorer exp;
  Standard_Real f,l;
  gp_Pnt2d ptvtx;
  gp_Pnt pvt;
  gp_Vec d1u,d1v, n1,n2;


  for (exp.Init(Fn,TopAbs_EDGE); exp.More(); exp.Next()) {
    const TopoDS_Edge& edg = TopoDS::Edge(exp.Current());
    Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edg,Fn,f,l);
    if (Precision::IsNegativeInfinite(f) && 
	Precision::IsPositiveInfinite(l)) {
      f = -100.;
      l = 100.;
    }
    else if (Precision::IsNegativeInfinite(f)) {
      f = l-200.;
    }
    else if (Precision::IsPositiveInfinite(l)) {
      l = f+200.;
    }
    Standard_Real deltau = (l-f)/20.;
    for (Standard_Integer i=1; i<=21; i++) {
      C2d->D0(f+(i-1)*deltau,ptvtx);
      Sn->D1(ptvtx.X(),ptvtx.Y(),pvt,d1u,d1v);
      n1 = d1u.Crossed(d1v);
      if (n1.Magnitude() > Precision::Confusion() ) {
	n1.Normalize();
	if (Fn.Orientation() == TopAbs_REVERSED) {
	  n1.Reverse();
	}
	
	// Projection sur Sb
	GeomAdaptor_Surface GAS(Sb);
	Standard_Real TolU = GAS.UResolution(Precision::Confusion());
	Standard_Real TolV = GAS.VResolution(Precision::Confusion());
	Extrema_ExtPS dist(pvt,GAS,TolU,TolV);
	if (dist.IsDone()) {
	  Standard_Real dist2min = RealLast();
	  Standard_Integer jmin = 0;
	  for (Standard_Integer j=1; j<=dist.NbExt(); j++) {
	    if (dist.SquareDistance(j)<dist2min) {
	      jmin = j;
	      dist2min = dist.SquareDistance(j);
	    }
	  }
	  if (jmin != 0) {
	    Standard_Real uu,vv;
	    dist.Point(jmin).Parameter(uu,vv);
	    Sb->D1(uu,vv,pvt,d1u,d1v);
	    n2 = d1u.Crossed(d1v);
	    if (n2.Magnitude() > Precision::Confusion()) {
	      n2.Normalize();
	      if (Fb.Orientation() == TopAbs_REVERSED) {
		n2.Reverse();
	      }
	      if (n1.Dot(n2) > 0.) {
		return TopAbs_REVERSED;
	      }
	      return TopAbs_FORWARD;
	    }
	  }
	}
      }
    }
  }
  return TopAbs_INTERNAL;
}


//=======================================================================
//function : GetOrientation
//purpose  : 
//=======================================================================

static Standard_Boolean Contains(const TopTools_ListOfShape& L,
				 const TopoDS_Shape& S)
{
  TopTools_ListIteratorOfListOfShape it;
  for (it.Initialize(L);
       it.More();
       it.Next()) {
    if (it.Value().IsSame(S)) {
      return Standard_True;
    }
  }
  return Standard_False;
}





//=======================================================================
//function : AddEdges
//purpose  : 
//=======================================================================

void LocOpe_Gluer::AddEdges()
{
  TopExp_Explorer exp, expsb;
  exp.Init(mySn, TopAbs_EDGE);

  TopLoc_Location Loc;
//  Standard_Real l, f;
  TopTools_IndexedMapOfShape MapV, MapFPrism, MapE;
  TopExp_Explorer vexp;
  Standard_Integer flag, i;

  TopExp::MapShapes(mySn, TopAbs_FACE, MapFPrism);

  for (expsb.Init(myRes, TopAbs_FACE); expsb.More(); expsb.Next()) {
    if (!MapFPrism.Contains(expsb.Current())) {
      MapV.Clear();
      TopExp::MapShapes(expsb.Current(), TopAbs_VERTEX, MapV);
      TopExp::MapShapes(expsb.Current(), TopAbs_EDGE, MapE);
      for(exp.Init(mySn, TopAbs_EDGE); exp.More(); exp.Next()) {
	TopoDS_Edge e = TopoDS::Edge(exp.Current());
	if (MapE.Contains(e)) continue;
	flag = 0;
	vexp.Init(e, TopAbs_VERTEX);
	for(; vexp.More(); vexp.Next()) {
	  TopoDS_Vertex v = TopoDS::Vertex(vexp.Current());
	  if (MapV.Contains(v)) { 
	    flag = 1;
	  }
	}
	if (flag == 1) {
	  
	  vexp.Init(e, TopAbs_VERTEX);
	  BRepExtrema_ExtPF ext;
	  ext.Initialize(TopoDS::Face(expsb.Current()));
	  flag = 0;
	  for(; vexp.More(); vexp.Next()) {
	    TopoDS_Vertex v = TopoDS::Vertex(vexp.Current());
	    if (!MapV.Contains(v)) { 
	      ext.Perform(v, TopoDS::Face(expsb.Current()));
	      if (!ext.IsDone() || ext.NbExt() == 0) { flag = 0; break;}
	      else {
		Standard_Real dist2min = ext.SquareDistance(1);
		for (i = 2; i <= ext.NbExt(); i++) {
		  dist2min = Min(dist2min, ext.SquareDistance(i));
		}
		if (dist2min >= BRep_Tool::Tolerance(v) * BRep_Tool::Tolerance(v)) {
		  flag = 0;
		  break;
		}
		else flag = 1;
	      }
	    }
	    else flag = 1;
	  }	  
	  if (flag == 1) {

	  }
	}
      }
    }  
  }


}