summaryrefslogtreecommitdiff
path: root/src/BRepTools/BRepTools_Quilt.cxx
blob: 5ded198e4552ed47b81eb206311c6940054504b6 (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
// File:	BRepTools_Quilt.cxx
// Created:	Fri Dec 23 10:37:05 1994
// Author:	Remi LEQUETTE
//		<rle@bravox>


#include <BRepTools_Quilt.ixx>

#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Shell.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Iterator.hxx>

#include <TopTools_DataMapIteratorOfDataMapOfShapeShape.hxx>
#include <TopTools_DataMapOfShapeShape.hxx>
#include <TopExp_Explorer.hxx>

#include <Geom2d_Curve.hxx>

#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
#include <TopTools_MapOfShape.hxx>
#include <TopoDS_Shape.hxx>
#include <TopTools_MapIteratorOfMapOfShape.hxx>
#include <TopTools_ListOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeShape.hxx>

//=======================================================================
//function : BRepTools_Quilt
//purpose  : 
//=======================================================================

BRepTools_Quilt::BRepTools_Quilt() : hasCopy(Standard_False)
{
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================
static Standard_Boolean NeedCopied(const TopoDS_Shape& theShape,const TopTools_IndexedDataMapOfShapeShape& myBounds)
{
    // test if the shape must be copied
    // i.e. it contains a bound subshape
  Standard_Boolean IsCopied = Standard_False;
  TopoDS_Iterator itv(theShape) ;
  for ( ; itv.More(); itv.Next()) {
    if (myBounds.Contains(itv.Value())) {
      IsCopied = Standard_True;
      break;
    }
  }
  return IsCopied;
}
static void CopyShape(const TopoDS_Edge& E,TopTools_IndexedDataMapOfShapeShape& myBounds)
{
  TopoDS_Edge NE = E;
  NE.EmptyCopy();
  NE.Orientation(TopAbs_FORWARD);
  BRep_Builder B;
  // add the edges
  TopoDS_Iterator itv;
  itv.Initialize(E,Standard_False) ; //TCollection_DataMap
  for ( ; itv.More(); itv.Next()) {
    const TopoDS_Shape& V = itv.Value();
    if (myBounds.Contains(V)) {
      B.Add(NE,myBounds.FindFromKey(V).Oriented(V.Orientation()));
    }
    else {
      B.Add(NE,V);
    }
  }
  // set the 3d range
  Standard_Real f,l;
  BRep_Tool::Range(E,f,l);
  B.Range(NE,f,l);
  myBounds.Add(E,NE.Oriented(TopAbs_FORWARD));
}
/*static void CopyShape(const TopoDS_Wire& W,TopTools_DataMapOfShapeShape& myBounds)
{
  TopoDS_Wire NW;
  B.MakeWire(NW);
  TopoDS_Iterator ite(W,Standard_False);
  for ( ; ite.More(); ite.Next()){
    const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
    TopAbs_Orientation OE = E.Orientation();
    if (myBounds.IsBound(E)) {
      TopoDS_Edge& NE = TopoDS::Edge(myBounds(E));
      B.Add(NW,NE.Oriented(OE));
    }
    else
      B.Add(NW,E);
  }
  NW.Orientation(W.Orientation());
  myBounds.Bind(W,NW);
}*/
void BRepTools_Quilt::Add(const TopoDS_Shape& S)
{
  //cout <<" Version of sewing with free edges"<<endl;
  // Binds all the faces of S
  //  - to the face itself if it is not copied
  //  - to the copy if it is copied
  if(myBounds.Contains(S)) return;
  BRep_Builder B;
  for (TopExp_Explorer wex(S,TopAbs_WIRE,TopAbs_FACE); wex.More(); wex.Next()) 
     myBounds.Add(wex.Current(),wex.Current());
  
  for (TopExp_Explorer eex(S,TopAbs_EDGE,TopAbs_WIRE); eex.More(); eex.Next())
    myBounds.Add(eex.Current(),eex.Current());
  
  for (TopExp_Explorer vex(S,TopAbs_VERTEX,TopAbs_EDGE); vex.More(); vex.Next())
    myBounds.Add(vex.Current(),vex.Current());
  
    // explore the faces
    for (TopExp_Explorer fex(S,TopAbs_FACE); fex.More(); fex.Next()) {
      
      // explore the edges of the face and try to copy them
      // if one edge is bound the face must be copied
      
      Standard_Boolean copyFace = Standard_False;
      const TopoDS_Face& F = TopoDS::Face(fex.Current());
      
      if (hasCopy) { // if their is no binding, do not test for copy
	
	for (TopExp_Explorer fed(F,TopAbs_EDGE); fed.More(); fed.Next()) {
	  
	  if (myBounds.Contains(fed.Current())) {
	    copyFace = Standard_True;
	  }
	  else {
	    // test if the edge must be copied
	    // i.e. it contains a bound vertex
	    
	    Standard_Boolean copyEdge = NeedCopied(fed.Current(),myBounds);
	    //Standard_Boolean copyEdge = Standard_False;
	    const TopoDS_Edge& E = TopoDS::Edge(fed.Current());
	    
	    // TopoDS_Iterator itv(E) ;
	    // for ( ; itv.More(); itv.Next()) {
	      // if (myBounds.IsBound(itv.Value())) {
		//	copyEdge = Standard_True;
		// break;
		//     }
	      //  }
	    
	    if (copyEdge) {
	      
	      // copy of an edge
	      
	      copyFace = Standard_True;
	      CopyShape(E,myBounds);
	      //TopoDS_Edge NE = E; //gka version for free edges
	      //NE.EmptyCopy();
	      
	      //NE.Orientation(TopAbs_FORWARD);
	      // add the edges
              //itv.Initialize(E,Standard_False) ;
	      //for ( ; itv.More(); itv.Next()) {
		//const TopoDS_Shape& V = itv.Value();
		//if (myBounds.IsBound(V)) {
		 // B.Add(NE,myBounds(V).Oriented(V.Orientation()));
		//}
		//else {
		 // B.Add(NE,V);
		//}
	      //}
	      // set the 3d range
	      //Standard_Real f,l;
	      //BRep_Tool::Range(E,f,l);
	      //B.Range(NE,f,l);
	      
	      //myBounds.Bind(E,NE.Oriented(TopAbs_FORWARD));
	    }
	  }
	}
      }
      
      // NF will be the copy of F or F itself
      TopoDS_Face NF = F;

      if (copyFace) {

	// copy of a face 
	
	NF.EmptyCopy();
	NF.Orientation(TopAbs_FORWARD);
	
	for (TopoDS_Iterator itw(F,Standard_False); itw.More(); itw.Next()) {
	  const TopoDS_Wire& W = TopoDS::Wire(itw.Value());
	  
	  TopoDS_Wire NW;
	  B.MakeWire(NW);
	  TopoDS_Iterator ite(W,Standard_False);
	  Standard_Real   UFirst,ULast;
	  
	  // Reconstruction des wires.
	  
	  for ( ; ite.More(); ite.Next()){
	    const TopoDS_Edge& E = TopoDS::Edge(ite.Value());
	    TopAbs_Orientation OE = E.Orientation();
	    if (myBounds.Contains(E)) {
	      const TopoDS_Edge& NE = TopoDS::Edge(myBounds.FindFromKey(E));
	      // pcurve.
	      if (NE.Orientation() == TopAbs_FORWARD) {
		B.UpdateEdge(NE,
			     BRep_Tool::CurveOnSurface(E,F,UFirst,ULast),
			     F,BRep_Tool::Tolerance(E));
	      }
	      else {
		// Si NE est REVERSED 
		// => les curve3d n ont pas la meme orientation.
		// ( C est une convention cf BRepTools_Quilt.cdl. et la methode
		// Bind.)
		// => la PCurve de E sur F doit etre inversee.
		
		OE = TopAbs::Reverse(OE);
		Handle(Geom2d_Curve) CE = 
		  BRep_Tool::CurveOnSurface(E,F,UFirst,ULast);
		Handle(Geom2d_Curve) NCE = CE->Reversed();
		B.UpdateEdge(NE,NCE,F,BRep_Tool::Tolerance(E));
		Standard_Real tmp = UFirst;
		UFirst = CE->ReversedParameter(ULast);
		ULast  = CE->ReversedParameter(tmp);
	      }
	      // pcurve range
	      B.Range(NE,F,UFirst,ULast);

	      B.Add(NW,NE.Oriented(OE));
	    }
	    else {
	      B.Add(NW,E);
	    }
	  }
	  NW.Orientation(W.Orientation());
	  B.Add(NF,NW);
	}
	NF.Orientation(F.Orientation());
      }

      // binds the face to itself or its copy
      myBounds.Add(F,NF);
    }
  
}

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

void BRepTools_Quilt::Bind(const TopoDS_Vertex& Vold, 
			   const TopoDS_Vertex& Vnew)
{
  if (!myBounds.Contains(Vold)) {
    myBounds.Add(Vold,Vnew);
  }
}

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

void BRepTools_Quilt::Bind(const TopoDS_Edge& Eold, const TopoDS_Edge& Enew)
{
  if (!myBounds.Contains(Eold)) {
    TopoDS_Edge ENew = Enew;
    if (IsCopied(Enew)) {
      ENew = TopoDS::Edge(Copy(Enew));
      ENew.Orientation(Enew.Orientation());
    }
    
    if (Eold.Orientation() != ENew.Orientation()) {
      myBounds.Add(Eold.Oriented(TopAbs_FORWARD),
		    ENew.Oriented(TopAbs_REVERSED));
    }
    else {
      myBounds.Add(Eold.Oriented(TopAbs_FORWARD),
		    ENew.Oriented(TopAbs_FORWARD));
    }  
    // if new binding bind also the vertices
    TopoDS_Iterator itold(Eold);
    while (itold.More()) {
      if (!myBounds.Contains(itold.Value())) {
	// find the vertex of Enew with same orientation
	TopAbs_Orientation anOrien = itold.Value().Orientation();
	TopoDS_Iterator itnew(ENew);
	while (itnew.More()) {
	  if (itnew.Value().Orientation() == anOrien) {
	    TopoDS_Vertex VNew = TopoDS::Vertex(itnew.Value());
	    if (IsCopied(VNew)) {
	      // if VNew has been copied take the copy
	      VNew = TopoDS::Vertex(Copy(VNew));
	    }
	    myBounds.Add(itold.Value(),VNew);
	    break;
	  }
	  itnew.Next();
	}
      }
      itold.Next();
    }
    hasCopy = Standard_True;
  }
}

//=======================================================================
//function : IsBound
//purpose  : 
//=======================================================================

Standard_Boolean BRepTools_Quilt::IsCopied(const TopoDS_Shape& S) const 
{
  if (myBounds.Contains(S)) {
    return !S.IsSame(myBounds.FindFromKey(S));
  }
  else
    return Standard_False;
}

//=======================================================================
//function : Copy
//purpose  : 
//=======================================================================

const TopoDS_Shape& BRepTools_Quilt::Copy(const TopoDS_Shape& S) const 
{
  Standard_NoSuchObject_Raise_if(!IsCopied(S),"BRepTools_Quilt::Copy");
  return myBounds.FindFromKey(S);
}

//=======================================================================
//function : Shells
//purpose  : 
//=======================================================================

TopoDS_Shape BRepTools_Quilt::Shells() const 
{
  // Outline of the algorithm
  //
  // In the map M we bind the free edges to their shells
  // We explore all the faces in myBounds
  // For each one we search the edges in the map and either :
  //
  // - Start a new shell if no edge is a free edge.
  // - Add the face to an existing shell
  // - Connect other shells if the face touch more than one shell

  // In the Map M the Shell is bound withe the relative orientation of E 
  // in the shell
  // In the map MF we binb the face to its shell.
  // In the Map MF the Shell is bound with the relative orientation of F 
  // in the shell

  TopTools_DataMapOfShapeShape M, MF;
  BRep_Builder                 B;
  TopoDS_Compound              result;

  B.MakeCompound(result);
  
  TopTools_MapOfShape MapOtherShape; //gka
  TopTools_MapOfShape EdgesFaces;
  
  // loop on the face in myBounds
  //TopTools_DataMapIteratorOfDataMapOfShapeShape it(myBounds);

  //while (it.More()) 
  for(Standard_Integer ii =1; ii <= myBounds.Extent(); ii++) {
    const TopoDS_Shape& Shape = myBounds.FindFromIndex(ii); //it.Value();
    if (Shape.ShapeType() == TopAbs_FACE) {
      for(TopExp_Explorer aExpEdg(Shape,TopAbs_EDGE); aExpEdg.More(); aExpEdg.Next()) //gka
	EdgesFaces.Add(aExpEdg.Current());
      
      TopoDS_Shell       SH;
      TopAbs_Orientation NewO;

      TopExp_Explorer itf1( Shape,TopAbs_EDGE);
      for ( ; itf1.More(); itf1.Next()) {
	const TopoDS_Shape& E = itf1.Current();
	if (M.IsBound(E)) {
	  SH = TopoDS::Shell(M(E));
	  if (SH.Orientation() == E.Orientation())
	    NewO = TopAbs::Reverse(Shape.Orientation());
	  else
	    NewO = Shape.Orientation(); 

	  MF.Bind (Shape,SH.Oriented(NewO));
	  break;
	}
      }

      if (SH.IsNull()) {
	// Create a new shell, closed. Add it to the result.
	B.MakeShell(SH);
	SH.Closed(Standard_True);
	B.Add(result,SH);
	MF.Bind (Shape,SH.Oriented(Shape.Orientation()));
      }


      // Add the face to the shell
      SH.Free(Standard_True);
//      B.Add(SH.Oriented(TopAbs_FORWARD), F .Oriented(MF(F).Orientation()));
      TopoDS_Shape arefShape = SH.Oriented(TopAbs_FORWARD) ;
      B.Add( arefShape , Shape.Oriented(MF(Shape).Orientation()));

      TopExp_Explorer itf(Shape.Oriented(TopAbs_FORWARD),TopAbs_EDGE);

      for ( ;itf.More(); itf.Next()) {
	const TopoDS_Edge& E = TopoDS::Edge(itf.Current());
	
	if (M.IsBound(E)) {
	  const TopoDS_Shape oldShell = M(E);
	  if (!oldShell.IsSame(SH)) {
	    // Fuse the old shell with the new one	  
	    // Compare the orientation of E in SH and in oldshell.
	    TopAbs_Orientation anOrien = E.Orientation();
	    if (MF(Shape).Orientation() == TopAbs_REVERSED) 
	      anOrien = TopAbs::Reverse(anOrien);

	    Standard_Boolean Rev = (anOrien == oldShell.Orientation());
	    //if rev = True oldShell has to be reversed.

	    // Add the faces of oldShell in SH.
	    for (TopoDS_Iterator its(oldShell); its.More(); its.Next()) {
	      const TopoDS_Face Fo = TopoDS::Face(its.Value());
	      TopAbs_Orientation NewO;
	      // update the orientation of Fo in SH.
	      if (Rev) 
		NewO = TopAbs::Reverse(MF(Fo).Orientation());
	      else
		NewO = MF(Fo).Orientation();

	      MF.Bind(Fo,SH.Oriented(NewO));
//	      B.Add  (SH.Oriented(TopAbs_FORWARD),Fo.Oriented(NewO));
              TopoDS_Shape arefShape = SH.Oriented(TopAbs_FORWARD) ;
	      B.Add  ( arefShape ,Fo.Oriented(NewO));
	    }
	    // Rebind the free edges of the old shell to the new shell
            //gka BUG 6491
            TopExp_Explorer aexp(SH,TopAbs_EDGE);
            for( ; aexp.More(); aexp.Next()) {
	    //for (TopTools_DataMapIteratorOfDataMapOfShapeShape itm(M);
//		 itm.More(); ) {
              if(!M.IsBound(aexp.Current()))
                 continue;
              TopoDS_Shape ae = aexp.Current();
              TopoDS_Shape as = M.Find(ae);
	      if (as.IsSame(oldShell)) {
		// update the orientation of free edges in SH.
		if (Rev)
		  NewO = TopAbs::Reverse(as.Orientation());
		else
		  NewO = as.Orientation();
		
		M.Bind(ae,SH.Oriented(NewO));
	      }
	    }
	    // remove the old shell from the result
	    B.Remove(result,oldShell.Oriented(TopAbs_FORWARD));
	  }
	  // Test if SH is always orientable.
	  TopAbs_Orientation anOrien   =  E.Orientation();
	  if (MF(Shape).Orientation() == TopAbs_REVERSED)
	    anOrien = TopAbs::Reverse(anOrien);

	  if (M(E).Orientation() == anOrien)
	    SH.Orientable(Standard_False);
	  
	  // remove the edge from M (no more a free edge)	    
	  M.UnBind(E);
	}
	else {
	  NewO = E.Orientation();
	  if (MF(Shape).Orientation() == TopAbs_REVERSED)
	    NewO = TopAbs::Reverse(NewO);
	  if(!E.IsNull())
            M.Bind(E,SH.Oriented(NewO));
          else
            continue;
	}
      }

      // freeze the shell
      SH.Free(Standard_False);
    }
    else 
     MapOtherShape.Add(Shape); 
    
    //it.Next();
  }
 
  // Unclose all shells having free edges
  for (TopTools_DataMapIteratorOfDataMapOfShapeShape it(M); it.More(); it.Next()) {
    TopoDS_Shape S = it.Value();
    S.Closed(Standard_Boolean(Standard_False));
  }
  
  TopTools_MapIteratorOfMapOfShape itother(MapOtherShape); //gka version for free edges
  for( ; itother.More() ; itother.Next()) {
    if(!EdgesFaces.Contains(itother.Key()) && myBounds.Contains(itother.Key())) {
      TopoDS_Shape aSh = myBounds.FindFromKey(itother.Key());
      B.Add(result,aSh);
    }
  }
  return result;
}