summaryrefslogtreecommitdiff
path: root/src/ShapeExtend/ShapeExtend_WireData.cxx
blob: 0336d0ac15d0ca72521a67cca612587145cf6cfa (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
//:q0 abv 12.03.99: mat-a.stp: protection against no pcurves in SwapSeam()
//    abv 28.04.99  S4137: added method Add(WireData), method SetLast fixed
//    abv 05.05.99  S4174: protection against INTERNAL/EXTERNAL edges

#include <ShapeExtend_WireData.ixx>
#include <Geom2d_Curve.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Iterator.hxx>
#include <TopExp.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepTools_WireExplorer.hxx>

//=======================================================================
//function : ShapeExtend_WireData
//purpose  : 
//=======================================================================

ShapeExtend_WireData::ShapeExtend_WireData()
{
  Clear();  
}

//=======================================================================
//function : ShapeExtend_WireData
//purpose  : 
//=======================================================================

ShapeExtend_WireData::ShapeExtend_WireData (const TopoDS_Wire& wire,
					    const Standard_Boolean chained,
                                            const Standard_Boolean theManifold)
{
  Init ( wire, chained ,theManifold);  
}

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

void ShapeExtend_WireData::Init (const Handle(ShapeExtend_WireData)& other) 
{
  Clear();
  Standard_Integer i, nb = other->NbEdges();
  for (i = 1; i <= nb; i++) Add ( other->Edge(i) );
  nb = other->NbNonManifoldEdges();
  for (i = 1; i <= nb; i++) Add ( other->NonmanifoldEdge(i) );
  myManifoldMode = other->ManifoldMode();
}

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

Standard_Boolean ShapeExtend_WireData::Init (const TopoDS_Wire& wire,
					     const Standard_Boolean chained,
                                             const Standard_Boolean theManifold) 
{
  Clear();
  myManifoldMode = theManifold;
  Standard_Boolean OK = Standard_True;
  TopoDS_Vertex Vlast;
  for ( TopoDS_Iterator it(wire); it.More(); it.Next() ) {
    TopoDS_Edge E = TopoDS::Edge ( it.Value() );

    // protect against INTERNAL/EXTERNAL edges
    if ( (E.Orientation() != TopAbs_REVERSED &&
	 E.Orientation() != TopAbs_FORWARD)) {
      myNonmanifoldEdges->Append(E);
      continue;
    }
    
    TopoDS_Vertex V1, V2;
    for ( TopoDS_Iterator itv(E); itv.More(); itv.Next() ) {
      TopoDS_Vertex V = TopoDS::Vertex ( itv.Value() );
      if ( V.Orientation() == TopAbs_FORWARD ) V1 = V;
      else if ( V.Orientation() == TopAbs_REVERSED ) V2 = V;
    }

    // chainage? Si pas bon et chained False on repart sur WireExplorer
    if ( ! Vlast.IsNull() && ! Vlast.IsSame ( V1 ) && theManifold) {
      OK = Standard_False;
      if ( ! chained ) break;
    }
    Vlast = V2;
    if(wire.Orientation() == TopAbs_REVERSED)
      myEdges->Prepend( E );
    else
      myEdges->Append ( E );
  }

  if(!myManifoldMode) {
    Standard_Integer nb = myNonmanifoldEdges->Length();
    Standard_Integer i =1;
    for( ; i <= nb; i++)
      myEdges->Append(myNonmanifoldEdges->Value(i));
    myNonmanifoldEdges->Clear();
  }
//    refaire chainage ?  Par WireExplorer
  if ( OK || chained ) return OK;

  Clear();
  for ( BRepTools_WireExplorer we(wire); we.More(); we.Next() ) 
    myEdges->Append ( TopoDS::Edge ( we.Current() ) );

  return OK;
}

//=======================================================================
//function : Clear
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Clear() 
{
  myEdges = new TopTools_HSequenceOfShape();
  myNonmanifoldEdges = new TopTools_HSequenceOfShape;
  mySeamF = mySeamR = -1;  
  mySeams.Nullify();
  myManifoldMode = Standard_True;
}

//=======================================================================
//function : ComputeSeams
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::ComputeSeams (const Standard_Boolean enforce) 
{
  if (mySeamF >= 0 && !enforce) return;

  mySeams = new TColStd_HSequenceOfInteger();
  mySeamF = mySeamR = 0;
  TopoDS_Shape S;
  Standard_Integer i, nb = NbEdges();
  TopTools_IndexedMapOfShape ME;
  Standard_Integer* SE = new Standard_Integer [nb+1];

  //  deux passes : d abord on mappe les Edges REVERSED
  //  Pour chacune, on note aussi son RANG dans la liste
  for (i = 1; i <= nb; i ++) {
    S = Edge(i);
    if (S.Orientation() == TopAbs_REVERSED) {
      Standard_Integer num = ME.Add (S);
      SE[num] = i;
    }
  }
  
  //  ensuite on voit les Edges FORWARD qui y seraient deja -> on note leur n0
  //  c-a-d le n0 de la directe ET de la reverse
  for (i = 1; i <= nb; i ++) {
    S = Edge(i);
    if (S.Orientation() == TopAbs_REVERSED) continue;
    Standard_Integer num = ME.FindIndex (S);
    if (num <= 0) continue;
    if (mySeamF == 0) { mySeamF = i; mySeamR = SE[num]; }
    else { mySeams->Append(i); mySeams->Append( SE[num] ); }
  }

  delete [] SE;  // ne pas oublier !!
}

//=======================================================================
//function : SetLast
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::SetLast (const Standard_Integer num) 
{
  if (num == 0) return;
  Standard_Integer i, nb = NbEdges();
  for (i = nb; i > num; i--) {
    TopoDS_Edge edge = TopoDS::Edge ( myEdges->Value(nb) );
    myEdges->Remove (nb);
    myEdges->InsertBefore (1, edge);
  }
  mySeamF = -1;
}

//=======================================================================
//function : SetDegeneratedLast
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::SetDegeneratedLast() 
{
  Standard_Integer i, nb = NbEdges();
  for (i = 1; i <= nb; i ++) {
    if ( BRep_Tool::Degenerated ( Edge(i) ) ) {
      SetLast ( i );
      return;
    }
  }
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Add (const TopoDS_Edge& edge,
			       const Standard_Integer atnum) 
{
  if(edge.Orientation()!= TopAbs_REVERSED &&
	 edge.Orientation() != TopAbs_FORWARD && myManifoldMode) {
    myNonmanifoldEdges->Append(edge);
    return;
  }
    
  if (edge.IsNull()) return;
  if (atnum == 0) {
    myEdges->Append (edge);
  } 
  else {
    myEdges->InsertBefore (atnum,edge);
  }
  mySeamF = -1;
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Add (const TopoDS_Wire& wire,
				const Standard_Integer atnum) 
{
  if (wire.IsNull()) return;
  Standard_Integer n = atnum;
  TopTools_SequenceOfShape aNMEdges;
  for (TopoDS_Iterator it(wire); it.More(); it.Next()) {
    TopoDS_Edge edge = TopoDS::Edge (it.Value());
    if(edge.Orientation()!= TopAbs_REVERSED &&
	 edge.Orientation() != TopAbs_FORWARD) {
      if(myManifoldMode)
        myNonmanifoldEdges->Append(edge);
      else
	aNMEdges.Append(edge);
      continue;
    }
    if (n == 0) {
      myEdges->Append (edge);
    } else {
      myEdges->InsertBefore (n,edge);
      n++;
    }
  }
  Standard_Integer i =1, nb = aNMEdges.Length();
  for( ; i <= nb ; i++)
    myEdges->Append(aNMEdges.Value(i));
  mySeamF = -1;
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Add (const Handle(ShapeExtend_WireData) &wire,
				const Standard_Integer atnum) 
{
  if ( wire.IsNull() ) return;
  TopTools_SequenceOfShape aNMEdges;
  Standard_Integer n = atnum;
  Standard_Integer i=1;
  for (; i <= wire->NbEdges(); i++ ) {
    TopoDS_Edge aE =  wire->Edge(i);
    if(aE.Orientation() == TopAbs_INTERNAL || 
       aE.Orientation() == TopAbs_EXTERNAL) {
      aNMEdges.Append(aE);
      continue;
    }
      
    if (n == 0 ) {
      myEdges->Append ( wire->Edge(i) );
    } 
    else {
      myEdges->InsertBefore ( n, wire->Edge(i) );
      n++;
    }
  }
  
  //non-manifold edges for non-manifold wire shoud be added at end
  for (i=1; i <=aNMEdges.Length(); i++)
    myEdges->Append(aNMEdges.Value(i));
  
  for (i=1; i <= wire->NbNonManifoldEdges(); i++) {
    if( myManifoldMode)
      myNonmanifoldEdges->Append(wire->NonmanifoldEdge(i));
    else {
      if (n == 0) 
        myEdges->Append ( wire->Edge(i) );
    
      else {
        myEdges->InsertBefore ( n, wire->Edge(i) );
        n++;
      }
    }
  }
  
  mySeamF = -1;
}

//=======================================================================
//function : Add
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Add (const TopoDS_Shape& shape,
			       const Standard_Integer atnum) 
{
  if      (shape.ShapeType() == TopAbs_EDGE) Add (TopoDS::Edge (shape), atnum);
  else if (shape.ShapeType() == TopAbs_WIRE) Add (TopoDS::Wire (shape), atnum);
}

//=======================================================================
//function : AddOriented
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::AddOriented (const TopoDS_Edge& edge,
				       const Standard_Integer mode) 
{
  if (edge.IsNull() || mode < 0) return;
  TopoDS_Edge E = edge;
  if (mode == 1 || mode == 3) E.Reverse();
  Add (E, mode/2);  // mode = 0,1 -> 0  mode = 2,3 -> 1
}

//=======================================================================
//function : AddOriented
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::AddOriented (const TopoDS_Wire& wire,
				       const Standard_Integer mode) 
{
  if (wire.IsNull() || mode < 0) return;
  TopoDS_Wire W = wire;
  if (mode == 1 || mode == 3) W.Reverse();
  Add (W, mode/2);  // mode = 0,1 -> 0  mode = 2,3 -> 1
}

void ShapeExtend_WireData::AddOriented (const TopoDS_Shape& shape,
					const Standard_Integer mode) 
{
  if      (shape.ShapeType() == TopAbs_EDGE) AddOriented (TopoDS::Edge (shape), mode);
  else if (shape.ShapeType() == TopAbs_WIRE) AddOriented (TopoDS::Wire (shape), mode);
}

//=======================================================================
//function : Remove
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Remove (const Standard_Integer num) 
{
 
  myEdges->Remove ( num > 0 ? num : NbEdges() );
 
  mySeamF = -1;
}

//=======================================================================
//function : Set
//purpose  : 
//=======================================================================

void ShapeExtend_WireData::Set (const TopoDS_Edge& edge,
			       const Standard_Integer num) 
{
  if(edge.Orientation()!= TopAbs_REVERSED &&
	 edge.Orientation() != TopAbs_FORWARD && myManifoldMode) {
    if(num <= myNonmanifoldEdges->Length())
      myNonmanifoldEdges->SetValue(num,edge);
    else
      myNonmanifoldEdges->Append(edge);
  }
  
  else
    myEdges->SetValue ( ( num > 0 ? num : NbEdges() ), edge);
  mySeamF = -1;
}

//=======================================================================
//function : Reverse
//purpose  : reverse order of edges in the wire
//=======================================================================

void ShapeExtend_WireData::Reverse () 
{
  Standard_Integer i, nb = NbEdges();

  // inverser les edges + les permuter pour inverser le wire
  for (i = 1; i <= nb/2; i ++) {
    TopoDS_Shape S1 = myEdges->Value(i);      S1.Reverse();
    TopoDS_Shape S2 = myEdges->Value(nb+1-i); S2.Reverse();
    myEdges->SetValue (i,      S2);
    myEdges->SetValue (nb+1-i, S1);
  }
  //  nb d edges impair : inverser aussi l edge du milieu (rang inchange)
  if ( nb % 2 ) {    //  test impair
    i = (nb+1)/2;
    TopoDS_Shape SI = myEdges->Value(i);      SI.Reverse();
    myEdges->SetValue (i, SI);
  }
  mySeamF = -1;
}

//=======================================================================
//function : Reverse
//purpose  : 
//=======================================================================

//  Fonction auxiliaire SwapSeam pour inverser
static void SwapSeam (const TopoDS_Shape& S, const TopoDS_Face& F)
{
  TopoDS_Edge E = TopoDS::Edge (S);
  if ( E.IsNull() || F.IsNull() ) return;
  if ( E.Orientation() == TopAbs_REVERSED ) return;  // ne le faire qu une fois !

  TopoDS_Face theface = F;  
  theface.Orientation(TopAbs_FORWARD);
  
//:S4136  Standard_Real Tol = BRep_Tool::Tolerance(theface);
  Handle(Geom2d_Curve) c2df,c2dr;
  Standard_Real uff,ulf,ufr,ulr;

  // d abord FWD puis REV
  c2df = BRep_Tool::CurveOnSurface (E,theface,uff,ulf);
  E.Orientation (TopAbs_REVERSED);
  c2dr = BRep_Tool::CurveOnSurface (E,theface,ufr,ulr);
  if ( c2df.IsNull() || c2dr.IsNull() ) return; //:q0
  // On permute
  E.Orientation (TopAbs_FORWARD);
  BRep_Builder B;
  B.UpdateEdge (E,c2dr,c2df,theface,0.); //:S4136: Tol
  B.Range (E,theface,uff,ulf);
}

void ShapeExtend_WireData::Reverse (const TopoDS_Face &face) 
{
  Reverse();
  if ( face.IsNull() ) return;

  //  ATTENTION aux coutures
  //  Une edge de couture est presente deux fois, FWD et REV
  //  Les inverser revient a permuter leur role ... donc ne rien faire
  //  Il faut donc aussi permuter leurs pcurves
  ComputeSeams(Standard_True);
  if (mySeamF > 0) SwapSeam (myEdges->Value(mySeamF),face);
  if (mySeamR > 0) SwapSeam (myEdges->Value(mySeamR),face);
  Standard_Integer nb = (mySeams.IsNull() ? 0 : mySeams->Length());
  for ( Standard_Integer i = 1; i <= nb; i ++) {
    SwapSeam (myEdges->Value(mySeams->Value(i)),face);
  }
  mySeamF = -1;
}

//=======================================================================
//function : NbEdges
//purpose  : 
//=======================================================================

Standard_Integer ShapeExtend_WireData::NbEdges() const
{
  return myEdges->Length();
}

//=======================================================================
//function : Edge
//purpose  : 
//=======================================================================

TopoDS_Edge ShapeExtend_WireData::Edge (const Standard_Integer num) const
{
  if (num < 0) {
    TopoDS_Edge E = Edge (-num);
    E.Reverse();
    return E;
  }
  return TopoDS::Edge ( myEdges->Value ( num ) );
}
//=======================================================================
//function : NbNonManifoldEdges
//purpose  : 
//=======================================================================

Standard_Integer ShapeExtend_WireData::NbNonManifoldEdges() const
{
  return myNonmanifoldEdges->Length();
}

//=======================================================================
//function : Edge
//purpose  : 
//=======================================================================

TopoDS_Edge ShapeExtend_WireData::NonmanifoldEdge (const Standard_Integer num) const
{
  TopoDS_Edge E;
  if (num < 0) 
    return E;
  
  return TopoDS::Edge ( myNonmanifoldEdges->Value ( num ) );
}
//=======================================================================
//function : Index
//purpose  : 
//=======================================================================

Standard_Integer ShapeExtend_WireData::Index (const TopoDS_Edge& edge)
{
  for (Standard_Integer i = 1; i <= NbEdges(); i++)
    if (Edge (i).IsSame (edge) && (Edge(i).Orientation() == edge.Orientation() || !IsSeam(i)))
      return i;
  return 0;
}

//=======================================================================
//function : IsSeam
//purpose  : 
//=======================================================================

Standard_Boolean ShapeExtend_WireData::IsSeam (const Standard_Integer num) 
{
  if (mySeamF < 0) ComputeSeams();
  if (mySeamF == 0) return Standard_False;
  
  if (num == mySeamF || num == mySeamR) return Standard_True;
//  Pas suffisant : on regarde dans la liste
  Standard_Integer i, nb = mySeams->Length();
  for (i = 1; i <= nb; i ++) {
    if (num == mySeams->Value(i)) return Standard_True;
  }
  return Standard_False;
}

//=======================================================================
//function : Make
//purpose  : 
//=======================================================================

TopoDS_Wire ShapeExtend_WireData::Wire() const
{
  TopoDS_Wire W;
  BRep_Builder B;
  B.MakeWire (W); 
  Standard_Integer i, nb = NbEdges();
  Standard_Boolean ismanifold = Standard_True;
  for (i = 1; i <= nb; i ++) {
    TopoDS_Edge aE =  Edge(i);
    if (aE.Orientation() != TopAbs_FORWARD &&
	aE.Orientation() != TopAbs_REVERSED)
      ismanifold = Standard_False;
    B.Add (W, aE);
  }
  if(ismanifold) {
    TopoDS_Vertex vf, vl;
    TopExp::Vertices (W, vf, vl);
    if (!vf.IsNull() && !vl.IsNull() && vf.IsSame (vl)) W.Closed (Standard_True);
  }
  if(myManifoldMode) {
    nb = NbNonManifoldEdges();
    for (i = 1; i <= nb; i ++) B.Add (W, NonmanifoldEdge(i));
  }
  return W;
}

//=======================================================================
//function : APIMake
//purpose  : 
//=======================================================================

TopoDS_Wire ShapeExtend_WireData::WireAPIMake() const
{
  TopoDS_Wire W;
  BRepBuilderAPI_MakeWire MW;
  Standard_Integer i, nb = NbEdges();
  for (i = 1; i <= nb; i ++)  MW.Add (Edge(i));
  if(myManifoldMode) {
    nb = NbNonManifoldEdges();
    for (i = 1; i <= nb; i ++) MW.Add (NonmanifoldEdge(i));
  }
  if ( MW.IsDone() ) W = MW.Wire();
  return W;
}

//=======================================================================
//function : NonmanifoldEdges
//purpose  : 
//=======================================================================
Handle(TopTools_HSequenceOfShape) ShapeExtend_WireData::NonmanifoldEdges() const
{
  return myNonmanifoldEdges;
}

//=======================================================================
//function : ManifoldMode
//purpose  : 
//=======================================================================
Standard_Boolean&  ShapeExtend_WireData::ManifoldMode()
{
  return myManifoldMode;
}