summaryrefslogtreecommitdiff
path: root/src/LocOpe/LocOpe_Pipe.cxx
blob: 34e5b892039f83bf9b1d4f14b5df93867909a890 (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
// File:	LocOpe_Pipe.cxx
// Created:	Wed Sep  4 15:01:13 1996
// Author:	Jacques GOUSSARD
//		<jag@mobilox.lyon.matra-dtv.fr>


#include <LocOpe_Pipe.ixx>

#include <LocOpe.hxx>

#include <LocOpe_BuildShape.hxx>

#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>

#include <TopTools_MapOfShape.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_MapIteratorOfMapOfShape.hxx>
#include <TopTools_DataMapOfShapeListOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>
#include <TopExp_Explorer.hxx>

#include <TopoDS_Face.hxx>
#include <TopoDS_Edge.hxx>

#include <TColStd_Array1OfReal.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <TColgp_Array1OfPnt.hxx>

#include <Geom_Surface.hxx>
#include <Geom_RectangularTrimmedSurface.hxx>
#include <Geom_Plane.hxx>
#include <Geom_Curve.hxx>
#include <Geom_BSplineCurve.hxx>

#include <gp_Pln.hxx>

#include <TopoDS.hxx>
#include <TopExp.hxx>
#include <Precision.hxx>
#include <BSplCLib.hxx>

#include <GeomConvert.hxx>

static TopAbs_Orientation Orientation(const TopoDS_Shape&,
				      const TopoDS_Shape&);


//=======================================================================
//function : LocOpe_Pipe
//purpose  : 
//=======================================================================

LocOpe_Pipe::LocOpe_Pipe(const TopoDS_Wire& Spine,
			 const TopoDS_Shape& Profile) : 
	   myPipe(Spine,Profile,Standard_False)
{

  TopoDS_Shape Result = myPipe.Shape();

  // On enleve les faces generees par les edges de connexite du profile,
  // et on fusionne les plans si possible

  TopTools_IndexedDataMapOfShapeListOfShape theEFMap;
  TopExp::MapShapesAndAncestors(Profile,TopAbs_EDGE,TopAbs_FACE,theEFMap);
  TopExp_Explorer exp;
  TopTools_ListOfShape Empty;
  TopTools_ListIteratorOfListOfShape it;

  TopTools_ListOfShape goodfaces;

  for (Standard_Integer i=1; i<=theEFMap.Extent(); i++) { 
    const TopoDS_Edge& edgpr = TopoDS::Edge(theEFMap.FindKey(i));
    myMap.Bind(edgpr,Empty);
    if (theEFMap(i).Extent() >= 2) {
      // on ne prend pas les faces generees
    }
    else {
      TopTools_MapOfShape MapFac; // on mappe les plans generes par cet edge
      for (exp.Init(Spine,TopAbs_EDGE); exp.More(); exp.Next()) {
	const TopoDS_Edge& edgsp = TopoDS::Edge(exp.Current());
	TopoDS_Face resfac = myPipe.Face(edgsp,edgpr);
	if (!resfac.IsNull()) {
	  Handle(Geom_Surface) P = BRep_Tool::Surface(resfac);
	  if (P->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
	    P = Handle(Geom_RectangularTrimmedSurface)::DownCast(P)->BasisSurface();
	  }
	  if (P->DynamicType() == STANDARD_TYPE(Geom_Plane)) {
	    MapFac.Add(resfac);
	  }
	  else {
	    myMap(edgpr).Append(resfac);
	    goodfaces.Append(resfac);
	  }
	}
      }
      
      // Chercher les composantes connexes sur cet ensemble de faces., avec meme
      // support geometrique

      TopTools_MapIteratorOfMapOfShape itm(MapFac);
      if (MapFac.Extent() <= 1) { // un seul plan. Rien a faire
	if (MapFac.Extent() == 1) {
	  myMap(edgpr).Append(itm.Key());
	  goodfaces.Append(itm.Key());
	}
	continue;
      }
      
      while (MapFac.Extent() >= 2) {
	itm.Reset();
	TopTools_ListOfShape FacFuse;
	TopoDS_Face FaceRef = TopoDS::Face(itm.Key());
	FacFuse.Append(FaceRef);
	Handle(Geom_Surface) P = BRep_Tool::Surface(FaceRef);
	if (P->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
	  P = Handle(Geom_RectangularTrimmedSurface)::DownCast(P)->BasisSurface();
	}
	gp_Pln Plref = Handle(Geom_Plane)::DownCast(P)->Pln();
	
	for (itm.Next(); itm.More(); itm.Next()) {
	  P = BRep_Tool::Surface(TopoDS::Face(itm.Key()));
	  if (P->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
	    P = Handle(Geom_RectangularTrimmedSurface)::DownCast(P)->BasisSurface();
	  }
	  gp_Pln Pl = Handle(Geom_Plane)::DownCast(P)->Pln();
	  if (Pl.Axis().IsParallel(Plref.Axis(),Precision::Angular()) &&
	      Plref.Contains(Pl.Location(),Precision::Confusion())) {
	    FacFuse.Append(itm.Key());
	  }
	}
	
	// FacFuse contient des faces de meme support. Il faut en faire 
	// des composantes connexes
	
	while (FacFuse.Extent() >= 2) {
	  FaceRef = TopoDS::Face(FacFuse.First());
	  // Recuperer l'orientation
	  TopAbs_Orientation orref = Orientation(FaceRef,Result);
	  P = BRep_Tool::Surface(FaceRef);
	  if (P->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
	    P = Handle(Geom_RectangularTrimmedSurface)::DownCast(P)->BasisSurface();
	  }
	  Plref = Handle(Geom_Plane)::DownCast(P)->Pln();
	  gp_Dir Dirref(Plref.Axis().Direction());
	  if ((Plref.Direct() && orref == TopAbs_REVERSED) ||
	      (!Plref.Direct() && orref == TopAbs_FORWARD)) {
	    Dirref.Reverse();
	  }
	  
	  TopTools_MapOfShape MapEd;
	  for (exp.Init(FaceRef.Oriented(TopAbs_FORWARD),TopAbs_EDGE);
	       exp.More(); exp.Next()) {
	    MapEd.Add(exp.Current());
	  }
	  
	  MapFac.Remove(FaceRef);
	  FacFuse.RemoveFirst(); // on enleve FaceRef
	  Standard_Boolean FaceToFuse = Standard_False;
	  Standard_Boolean MoreFound;
	  
	  
	  do {
	    MoreFound = Standard_False;
	    for (it.Initialize(FacFuse); it.More(); it.Next()) {
	      for (exp.Init(it.Value(),TopAbs_EDGE);
		   exp.More(); exp.Next()) {
		if (MapEd.Contains(exp.Current())) {
		  FaceToFuse = Standard_True;
		  MoreFound = Standard_True;
		  break;
		}
	      }
	      if (exp.More()) {
		break;
	      }
	    }
	    if (MoreFound) {
	      const TopoDS_Face& fac = TopoDS::Face(it.Value());
	      TopAbs_Orientation orrelat = Orientation(fac,Result);
	      Handle(Geom_Surface) OtherP = BRep_Tool::Surface(fac);
	      if (OtherP->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
		OtherP = Handle(Geom_RectangularTrimmedSurface)::
	        DownCast(OtherP)->BasisSurface();
	      }
	      gp_Pln Pl = Handle(Geom_Plane)::DownCast(OtherP)->Pln();
	      gp_Dir Dirpl(Pl.Axis().Direction());
	      if ((Pl.Direct() && orrelat == TopAbs_REVERSED) ||
		  (!Plref.Direct() && orrelat == TopAbs_FORWARD)) {
		Dirpl.Reverse();
	      }
	      if (Dirpl.Dot(Dirref) > 0) {
		orrelat = TopAbs_FORWARD;
	      }
	      else {
		orrelat = TopAbs_REVERSED;
	      }
	      for (exp.Init(fac.Oriented(orrelat),TopAbs_EDGE);
		   exp.More(); exp.Next()) {
		if (!MapEd.Add(exp.Current())) {
		  MapEd.Remove(exp.Current());
		}
	      }	    
	      MapFac.Remove(fac);
	      FacFuse.Remove(it);
	    }
	  } while (MoreFound);
	  
	  if (FaceToFuse) {
	    TopoDS_Face NewFace;
	    BRep_Builder B;
	    B.MakeFace(NewFace,P,BRep_Tool::Tolerance(FaceRef));
	    TopoDS_Wire NewWire;
	    B.MakeWire(NewWire);
	    for (TopTools_MapIteratorOfMapOfShape itm2(MapEd);
		 itm2.More(); itm2.Next()) {
	      B.Add(NewWire,itm2.Key());
	    }
	    exp.Init(FaceRef.Oriented(TopAbs_FORWARD),TopAbs_WIRE);
	    NewWire.Orientation(exp.Current().Orientation());
	    B.Add(NewFace,NewWire);
	    myMap(edgpr).Append(NewFace);
	    goodfaces.Append(NewFace);
	  }
	}
	if (FacFuse.Extent() == 1) {
	  MapFac.Remove(FacFuse.First());
	  myMap(edgpr).Append(FacFuse.First());
	  goodfaces.Append(FacFuse.First());
	}
      }
    }
  }

  for (exp.Init(myPipe.FirstShape(),TopAbs_FACE); exp.More(); exp.Next()) {
    goodfaces.Append(exp.Current());
  }
  for (exp.Init(myPipe.LastShape(),TopAbs_FACE); exp.More(); exp.Next()) {
    goodfaces.Append(exp.Current());
  }

  LocOpe_BuildShape BS(goodfaces);
  myRes = BS.Shape();
}


//=======================================================================
//function : Shape
//purpose  : 
//=======================================================================

const TopoDS_Shape& LocOpe_Pipe::Shape () const
{
  return myRes;
}


//=======================================================================
//function : Shapes
//purpose  : 
//=======================================================================

const TopTools_ListOfShape& LocOpe_Pipe::Shapes (const TopoDS_Shape& S)
{
  TopAbs_ShapeEnum typS = S.ShapeType();
  if (typS != TopAbs_EDGE && typS != TopAbs_VERTEX) {
    Standard_DomainError::Raise();
  }
//  for (TopExp_Explorer exp(myPipe.Profile(),typS); exp.More(); exp.Next()) {
  TopExp_Explorer exp(myPipe.Profile(),typS) ;
  for ( ; exp.More(); exp.Next()) {
    if (exp.Current().IsSame(S)) {
      break;
    }
  }
  if (!exp.More()) {
    Standard_NoSuchObject::Raise();
  }

  myGShap.Clear();
  if (typS == TopAbs_VERTEX) {
    const TopoDS_Vertex& VProfile = TopoDS::Vertex(S);
    for (exp.Init(myPipe.Spine(),TopAbs_EDGE); exp.More(); exp.Next()) {
      const TopoDS_Edge& edsp = TopoDS::Edge(exp.Current());
      TopoDS_Edge resed = myPipe.Edge(edsp,VProfile);
      if (!resed.IsNull()) {
	myGShap.Append(resed);
      }
    }
    return myGShap;
  }
  // TopAbs_EDGE
  const TopoDS_Edge& EProfile = TopoDS::Edge(S);
  return myMap(EProfile);
}


//=======================================================================
//function : GetCurves
//purpose  : 
//=======================================================================

const TColGeom_SequenceOfCurve& 
    LocOpe_Pipe::Curves(const TColgp_SequenceOfPnt& Spt) 
{

  myCrvs.Clear();
  TopTools_MapOfShape Map;

  Standard_Integer i , j , k , Nbpnt = Spt.Length();
  Standard_Real p1,p2;
//  gp_Pnt ptbid;

  for ( i = 1; i <= Nbpnt; i++) {
    gp_Pnt P1 = Spt(i);
    Standard_Integer MaxDeg = 0;
    TColGeom_SequenceOfCurve seq;
    TopoDS_Wire W = myPipe.PipeLine(P1);
    
    TopExp_Explorer ex(W, TopAbs_EDGE);
    for (; ex.More(); ex.Next()) {
      Handle(Geom_Curve) C1 = BRep_Tool::Curve(TopoDS::Edge(ex.Current()), p1, p2);
      Handle(Geom_BSplineCurve) C = GeomConvert::CurveToBSplineCurve (C1);
      if (C.IsNull()) {
	continue;
      }
      MaxDeg = Max(MaxDeg,C->Degree());
      P1 = C->Value(p2);
      if (p1 != C->FirstParameter() || p2 != C->LastParameter()) {
	C->Segment(p1,p2);
      }
      Standard_Integer Nbkn = C->NbKnots();
      TColStd_Array1OfReal Tkn(1,Nbkn);
      C->Knots(Tkn);
      BSplCLib::Reparametrize(seq.Length(),seq.Length()+1,Tkn);
      C->SetKnots(Tkn);
      seq.Append(C);
    }

    Handle(Geom_Curve) newC;
    Standard_Integer Nbkn=0 ,Nbp=0;
    Standard_Integer Nbcurv = seq.Length();
    if (Nbcurv == 0) {
      myCrvs.Append(newC);
      continue;
    }

    Handle(Geom_BSplineCurve) Bsp;
    for (j=1; j<=Nbcurv; j++) {
      Bsp = Handle(Geom_BSplineCurve)::DownCast(seq(j));
      Bsp->IncreaseDegree(MaxDeg);
      Nbp += Bsp->NbPoles();
      Nbkn += Bsp->NbKnots();
    }
    Nbp  -= Nbcurv-1;
    Nbkn -= Nbcurv-1;
    TColStd_Array1OfReal Tkn(1,Nbkn);
    TColStd_Array1OfInteger Tmu(1,Nbkn);
    TColgp_Array1OfPnt Tpol(1,Nbp);
    Standard_Integer Ik=0,Ip=0;

    Bsp = Handle(Geom_BSplineCurve)::DownCast(seq(1));
    for ( k = 1; k<= Bsp->NbPoles(); k++) {
      Ip++;
      Tpol(Ip) = Bsp->Pole(k);
    }
    for (k = 1; k<= Bsp->NbKnots(); k++) {
      Ik++;
      Tkn(Ik) = Bsp->Knot(k);
      Tmu(Ik) = Bsp->Multiplicity(k);
    }
    Tmu(Ik)--;
    
    for (j=2; j<=Nbcurv; j++) {
      Bsp = Handle(Geom_BSplineCurve)::DownCast(seq(j));
      for (k = 2; k<= Bsp->NbPoles(); k++) {
	Ip++;
	Tpol(Ip) = Bsp->Pole(k);
      }
      for (k = 2; k<= Bsp->NbKnots(); k++) {
	Ik++;
	Tkn(Ik) = Bsp->Knot(k);
	Tmu(Ik) = Bsp->Multiplicity(k);
      }
      Tmu(Ik)--;
    }
    Tmu(Ik)++;
    newC = new Geom_BSplineCurve(Tpol,Tkn,Tmu,MaxDeg);
    myCrvs.Append(newC);
  }

  return myCrvs;
}


//=======================================================================
//function : Orientation
//purpose  : static, not member
//=======================================================================

static TopAbs_Orientation Orientation(const TopoDS_Shape& Sub,
				     const TopoDS_Shape& S)
{
  TopExp_Explorer exp;
  for (exp.Init(S,Sub.ShapeType()); exp.More(); exp.Next()) {
    if (exp.Current().IsSame(Sub)) {
      return exp.Current().Orientation();
    }
  }
  Standard_NoSuchObject::Raise();
  return TopAbs_INTERNAL;
}


//=======================================================================
//function : BarycCurve
//purpose  : 
//=======================================================================

Handle(Geom_Curve) LocOpe_Pipe::BarycCurve() 
{
  Standard_Integer j , k ;

  gp_Pnt bar(0., 0., 0.);
  TColgp_SequenceOfPnt spt;
  TopoDS_Shape Base = FirstShape();
  LocOpe::SampleEdges(Base, spt);
  for (Standard_Integer jj=1;jj<=spt.Length(); jj++) {
    const gp_Pnt& pvt = spt(jj);
    bar.ChangeCoord() += pvt.XYZ();
  }
  bar.ChangeCoord().Divide(spt.Length());

  Standard_Real p1,p2;
//  gp_Pnt ptbid;
  gp_Pnt P1 = bar;

  Standard_Integer MaxDeg = 0;
  TColGeom_SequenceOfCurve seq;  
  TopoDS_Wire W = myPipe.PipeLine(P1);
  
  TopExp_Explorer ex(W, TopAbs_EDGE);
  for (; ex.More(); ex.Next()) {
    Handle(Geom_Curve) C1 = BRep_Tool::Curve(TopoDS::Edge(ex.Current()), p1, p2);
    Handle(Geom_BSplineCurve) C = GeomConvert::CurveToBSplineCurve (C1);

    if (C.IsNull()) {
      continue;
    }
    MaxDeg = Max(MaxDeg,C->Degree());
    P1 = C->Value(p2);
    if (p1 != C->FirstParameter() || p2 != C->LastParameter()) {
      C->Segment(p1,p2);
    }
    Standard_Integer Nbkn = C->NbKnots();
    TColStd_Array1OfReal Tkn(1,Nbkn);
    C->Knots(Tkn);
    BSplCLib::Reparametrize(seq.Length(),seq.Length()+1,Tkn);
      C->SetKnots(Tkn);
    seq.Append(C);
  }
  Handle(Geom_Curve) newC;
  Standard_Integer Nbkn=0 ,Nbp=0;
  Standard_Integer Nbcurv = seq.Length();
  if (Nbcurv == 0) {
    myCrvs.Append(newC);
  }
  Handle(Geom_BSplineCurve) Bsp;
  for ( j=1; j<=Nbcurv; j++) {
    Bsp = Handle(Geom_BSplineCurve)::DownCast(seq(j));
    Bsp->IncreaseDegree(MaxDeg);
    Nbp += Bsp->NbPoles();
    Nbkn += Bsp->NbKnots();
  }
  Nbp  -= Nbcurv-1;
  Nbkn -= Nbcurv-1;
  TColStd_Array1OfReal Tkn(1,Nbkn);
  TColStd_Array1OfInteger Tmu(1,Nbkn);
  TColgp_Array1OfPnt Tpol(1,Nbp);
  Standard_Integer Ik=0,Ip=0;

  Bsp = Handle(Geom_BSplineCurve)::DownCast(seq(1));
  for ( k = 1; k<= Bsp->NbPoles(); k++) {
    Ip++;
    Tpol(Ip) = Bsp->Pole(k);
  }
  for (k = 1; k<= Bsp->NbKnots(); k++) {
    Ik++;
    Tkn(Ik) = Bsp->Knot(k);
    Tmu(Ik) = Bsp->Multiplicity(k);
  }
  Tmu(Ik)--;
  
  for (j=2; j<=Nbcurv; j++) {
    Bsp = Handle(Geom_BSplineCurve)::DownCast(seq(j));
    for (k = 2; k<= Bsp->NbPoles(); k++) {
      Ip++;
	Tpol(Ip) = Bsp->Pole(k);
    }
      for (k = 2; k<= Bsp->NbKnots(); k++) {
	Ik++;
	Tkn(Ik) = Bsp->Knot(k);
	Tmu(Ik) = Bsp->Multiplicity(k);
      }
    Tmu(Ik)--;
  }
  Tmu(Ik)++;
  newC = new Geom_BSplineCurve(Tpol,Tkn,Tmu,MaxDeg);

  return newC;
}