0024832: Performance of new boolean operations has become worse
[occt.git] / src / BRepTools / BRepTools.cxx
CommitLineData
b311480e 1// Created on: 1993-01-21
2// Created by: Remi LEQUETTE
3// Copyright (c) 1993-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
17#include <Standard_Stream.hxx>
18
19#include <BRepTools.ixx>
20#include <BRepTools_ShapeSet.hxx>
21#include <BRep_Tool.hxx>
22#include <TopExp.hxx>
23#include <TopExp_Explorer.hxx>
24#include <TopoDS.hxx>
25#include <TopoDS_Iterator.hxx>
26#include <BndLib_Add2dCurve.hxx>
27#include <Geom2dAdaptor_Curve.hxx>
28#include <Geom_Surface.hxx>
29#include <Geom_Curve.hxx>
30#include <Geom2d_Curve.hxx>
31#include <BRepTools_MapOfVertexPnt2d.hxx>
32#include <BRep_CurveRepresentation.hxx>
33#include <BRep_ListIteratorOfListOfCurveRepresentation.hxx>
34#include <BRep_TEdge.hxx>
35#include <TColgp_SequenceOfPnt2d.hxx>
36#include <TColStd_SequenceOfReal.hxx>
9c06009a 37#include <TColStd_Array1OfReal.hxx>
7fd59977 38#include <TColGeom2d_SequenceOfCurve.hxx>
39#include <TopTools_SequenceOfShape.hxx>
40#include <Precision.hxx>
41
42#include <Poly_Triangulation.hxx>
43#include <Poly_PolygonOnTriangulation.hxx>
44#include <TColStd_HArray1OfInteger.hxx>
01697018 45#include <TColStd_MapOfTransient.hxx>
7fd59977 46
47#include <gp_Lin2d.hxx>
48#include <ElCLib.hxx>
49#include <gp_Vec2d.hxx>
50#include <Standard_ErrorHandler.hxx>
51#include <Standard_Failure.hxx>
52
53#include <errno.h>
54
55//=======================================================================
56//function : UVBounds
57//purpose :
58//=======================================================================
59
60void BRepTools::UVBounds(const TopoDS_Face& F,
61 Standard_Real& UMin, Standard_Real& UMax,
62 Standard_Real& VMin, Standard_Real& VMax)
63{
64 Bnd_Box2d B;
65 AddUVBounds(F,B);
66 B.Get(UMin,VMin,UMax,VMax);
67}
68
69//=======================================================================
70//function : UVBounds
71//purpose :
72//=======================================================================
73
74void BRepTools::UVBounds(const TopoDS_Face& F,
75 const TopoDS_Wire& W,
76 Standard_Real& UMin, Standard_Real& UMax,
77 Standard_Real& VMin, Standard_Real& VMax)
78{
79 Bnd_Box2d B;
80 AddUVBounds(F,W,B);
81 B.Get(UMin,VMin,UMax,VMax);
82}
83
84
85//=======================================================================
86//function : UVBounds
87//purpose :
88//=======================================================================
89
90void BRepTools::UVBounds(const TopoDS_Face& F,
91 const TopoDS_Edge& E,
92 Standard_Real& UMin, Standard_Real& UMax,
93 Standard_Real& VMin, Standard_Real& VMax)
94{
95 Bnd_Box2d B;
96 AddUVBounds(F,E,B);
97 B.Get(UMin,VMin,UMax,VMax);
98}
99
100//=======================================================================
101//function : AddUVBounds
102//purpose :
103//=======================================================================
104
105void BRepTools::AddUVBounds(const TopoDS_Face& FF, Bnd_Box2d& B)
106{
107 TopoDS_Face F = FF;
108 F.Orientation(TopAbs_FORWARD);
109 TopExp_Explorer ex(F,TopAbs_EDGE);
110
111 // fill box for the given face
112 Bnd_Box2d aBox;
113 for (;ex.More();ex.Next()) {
114 BRepTools::AddUVBounds(F,TopoDS::Edge(ex.Current()),aBox);
115 }
116
117 // if the box is empty (face without edges or without pcurves),
118 // get natural bounds
119 if (aBox.IsVoid()) {
120 Standard_Real UMin,UMax,VMin,VMax;
121 TopLoc_Location L;
122 BRep_Tool::Surface(F,L)->Bounds(UMin,UMax,VMin,VMax);
123 aBox.Update(UMin,VMin,UMax,VMax);
124 }
125
126 // add face box to result
127 B.Add ( aBox );
128}
129
130
131//=======================================================================
132//function : AddUVBounds
133//purpose :
134//=======================================================================
135
136void BRepTools::AddUVBounds(const TopoDS_Face& F,
137 const TopoDS_Wire& W,
138 Bnd_Box2d& B)
139{
140 TopExp_Explorer ex;
141 for (ex.Init(W,TopAbs_EDGE);ex.More();ex.Next()) {
142 BRepTools::AddUVBounds(F,TopoDS::Edge(ex.Current()),B);
143 }
144}
145
146
147//=======================================================================
148//function : AddUVBounds
149//purpose :
150//=======================================================================
151
152void BRepTools::AddUVBounds(const TopoDS_Face& F,
153 const TopoDS_Edge& E,
154 Bnd_Box2d& B)
155{
156 Standard_Real pf,pl;
157 Bnd_Box2d Baux;
158 const Handle(Geom2d_Curve) C = BRep_Tool::CurveOnSurface(E,F,pf,pl);
c55993db 159 if (C.IsNull()) return;
7fd59977 160 if (pl < pf) { // Petit Blindage
161 Standard_Real aux;
162 aux = pf; pf = pl; pl = aux;
163 }
7fd59977 164 Geom2dAdaptor_Curve PC(C,pf,pl);
165 if (Precision::IsNegativeInfinite(pf) ||
166 Precision::IsPositiveInfinite(pf)) {
167 Geom2dAdaptor_Curve GC(PC);
168 BndLib_Add2dCurve::Add(GC,0.,B);
169 }
170 else {
171
172 // just compute points to get a close box.
173 TopLoc_Location L;
174 Standard_Real Umin,Umax,Vmin,Vmax;
175 const Handle(Geom_Surface)& Surf=BRep_Tool::Surface(F,L);
176 Surf->Bounds(Umin,Umax,Vmin,Vmax);
177 gp_Pnt2d Pa,Pb,Pc;
178
179
9c06009a 180 Standard_Integer i, j, k, nbp = 20;
7fd59977 181 if (PC.GetType() == GeomAbs_Line) nbp = 2;
9c06009a 182 Standard_Integer NbIntC1 = PC.NbIntervals(GeomAbs_C1);
183 if (NbIntC1 > 1)
184 nbp = 10;
185 TColStd_Array1OfReal SharpPoints(1, NbIntC1+1);
186 PC.Intervals(SharpPoints, GeomAbs_C1);
187 TColStd_Array1OfReal Parameters(1, nbp*NbIntC1+1);
188 k = 1;
189 for (i = 1; i <= NbIntC1; i++)
190 {
191 Standard_Real delta = (SharpPoints(i+1) - SharpPoints(i))/nbp;
192 for (j = 0; j < nbp; j++)
193 Parameters(k++) = SharpPoints(i) + j*delta;
194 }
195 Parameters(nbp*NbIntC1+1) = SharpPoints(NbIntC1+1);
196
7fd59977 197 gp_Pnt2d P;
198 PC.D0(pf,P);
199 Baux.Add(P);
200
201 Standard_Real du=0.0;
202 Standard_Real dv=0.0;
203
204 Pc=P;
9c06009a 205 for (i = 2; i < Parameters.Upper(); i++) {
206 pf = Parameters(i);
7fd59977 207 PC.D0(pf,P);
208 Baux.Add(P);
9c06009a 209 if(i==2) { Pb=Pc; Pc=P; }
7fd59977 210 else {
211 //-- Calcul de la fleche
212 Pa=Pb; Pb=Pc; Pc=P;
213 gp_Vec2d PaPc(Pa,Pc);
214// gp_Lin2d L2d(Pa,PaPc);
215// Standard_Real up = ElCLib::Parameter(L2d,Pb);
216// gp_Pnt2d PProj = ElCLib::Value(up,L2d);
217 gp_Pnt2d PProj(Pa.Coord()+(PaPc.XY()/2.));
218 Standard_Real ddu=Abs(Pb.X()-PProj.X());
219 Standard_Real ddv=Abs(Pb.Y()-PProj.Y());
220 if(ddv>dv) dv=ddv;
221 if(ddu>du) du=ddu;
222 }
223 }
224 PC.D0(pl,P);
225 Baux.Add(P);
226
227 //-- cout<<" du="<<du<<" dv="<<dv<<endl;
228 Standard_Real u0,u1,v0,v1;
229 Baux.Get(u0,v0,u1,v1);
230 du*=1.5;
231 dv*=1.5;
232 u0-=du; v0-=dv; u1+=du; v1+=dv;
233 if(Surf->IsUPeriodic()) { }
234 else {
235 if(u0<=Umin) { u0=Umin; }
236 if(u1>=Umax) { u1=Umax; }
237 }
238 if(Surf->IsVPeriodic()) { }
239 else {
240 if(v0<=Vmin) { v0=Vmin; }
241 if(v1>=Vmax) { v1=Vmax; }
242 }
243 P.SetCoord(u0,v0) ; Baux.Add(P);
244 P.SetCoord(u1,v1) ; Baux.Add(P);
cf836671 245
246 Bnd_Box2d FinalBox;
247 Standard_Real aXmin, aYmin, aXmax, aYmax;
248 Baux.Get(aXmin, aYmin, aXmax, aYmax);
249 Standard_Real Tol2d = Precision::PConfusion();
250 if (Abs(aXmin - Umin) <= Tol2d)
251 aXmin = Umin;
252 if (Abs(aYmin - Vmin) <= Tol2d)
253 aYmin = Vmin;
254 if (Abs(aXmax - Umax) <= Tol2d)
255 aXmax = Umax;
256 if (Abs(aYmax - Vmax) <= Tol2d)
257 aYmax = Vmax;
258 FinalBox.Update(aXmin, aYmin, aXmax, aYmax);
259
260 B.Add(FinalBox);
7fd59977 261 }
262}
263
264//=======================================================================
265//function : Update
266//purpose :
267//=======================================================================
268
269void BRepTools::Update(const TopoDS_Vertex&)
270{
271}
272
273//=======================================================================
274//function : Update
275//purpose :
276//=======================================================================
277
278void BRepTools::Update(const TopoDS_Edge&)
279{
280}
281
282//=======================================================================
283//function : Update
284//purpose :
285//=======================================================================
286
287void BRepTools::Update(const TopoDS_Wire&)
288{
289}
290
291//=======================================================================
292//function : Update
293//purpose :
294//=======================================================================
295
296void BRepTools::Update(const TopoDS_Face& F)
297{
298 if (!F.Checked()) {
299 UpdateFaceUVPoints(F);
300 F.TShape()->Checked(Standard_True);
301 }
302}
303
304//=======================================================================
305//function : Update
306//purpose :
307//=======================================================================
308
309void BRepTools::Update(const TopoDS_Shell& S)
310{
311 TopExp_Explorer ex(S,TopAbs_FACE);
312 while (ex.More()) {
313 Update(TopoDS::Face(ex.Current()));
314 ex.Next();
315 }
316}
317
318//=======================================================================
319//function : Update
320//purpose :
321//=======================================================================
322
323void BRepTools::Update(const TopoDS_Solid& S)
324{
325 TopExp_Explorer ex(S,TopAbs_FACE);
326 while (ex.More()) {
327 Update(TopoDS::Face(ex.Current()));
328 ex.Next();
329 }
330}
331
332//=======================================================================
333//function : Update
334//purpose :
335//=======================================================================
336
337void BRepTools::Update(const TopoDS_CompSolid& CS)
338{
339 TopExp_Explorer ex(CS,TopAbs_FACE);
340 while (ex.More()) {
341 Update(TopoDS::Face(ex.Current()));
342 ex.Next();
343 }
344}
345
346//=======================================================================
347//function : Update
348//purpose :
349//=======================================================================
350
351void BRepTools::Update(const TopoDS_Compound& C)
352{
353 TopExp_Explorer ex(C,TopAbs_FACE);
354 while (ex.More()) {
355 Update(TopoDS::Face(ex.Current()));
356 ex.Next();
357 }
358}
359
360//=======================================================================
361//function : Update
362//purpose :
363//=======================================================================
364
365void BRepTools::Update(const TopoDS_Shape& S)
366{
367 switch (S.ShapeType()) {
368
369 case TopAbs_VERTEX :
370 Update(TopoDS::Vertex(S));
371 break;
372
373 case TopAbs_EDGE :
374 Update(TopoDS::Edge(S));
375 break;
376
377 case TopAbs_WIRE :
378 Update(TopoDS::Wire(S));
379 break;
380
381 case TopAbs_FACE :
382 Update(TopoDS::Face(S));
383 break;
384
385 case TopAbs_SHELL :
386 Update(TopoDS::Shell(S));
387 break;
388
389 case TopAbs_SOLID :
390 Update(TopoDS::Solid(S));
391 break;
392
393 case TopAbs_COMPSOLID :
394 Update(TopoDS::CompSolid(S));
395 break;
396
397 case TopAbs_COMPOUND :
398 Update(TopoDS::Compound(S));
399 break;
400
401 default:
402 break;
403
404 }
405}
406
407
408//=======================================================================
409//function : UpdateFaceUVPoints
410//purpose : reset the UV points of a Face
411//=======================================================================
412
413void BRepTools::UpdateFaceUVPoints(const TopoDS_Face& F)
414{
415 // Recompute for each edge the two UV points in order to have the same
416 // UV point on connected edges.
417
418 // First edge loop, store the vertices in a Map with their 2d points
419
420 BRepTools_MapOfVertexPnt2d theVertices;
421 TopoDS_Iterator expE,expV;
422 TopoDS_Iterator EdgeIt,VertIt;
423 TColStd_SequenceOfReal aFSeq, aLSeq;
424 TColGeom2d_SequenceOfCurve aCSeq;
425 TopTools_SequenceOfShape aShSeq;
426 gp_Pnt2d P;
427 Standard_Integer i;
428 // a 3d tolerance for UV !!
429 Standard_Real tolerance = BRep_Tool::Tolerance(F);
430 TColgp_SequenceOfPnt2d emptySequence;
431
432 for (expE.Initialize(F); expE.More(); expE.Next()) {
433 if(expE.Value().ShapeType() != TopAbs_WIRE)
434 continue;
435
436 EdgeIt.Initialize(expE.Value());
437 for( ; EdgeIt.More(); EdgeIt.Next())
438 {
439 const TopoDS_Edge& E = TopoDS::Edge(EdgeIt.Value());
440 Standard_Real f,l;
441 Handle(Geom2d_Curve) C = BRep_Tool::CurveOnSurface(E,F,f,l);
442
443 aFSeq.Append(f);
444 aLSeq.Append(l);
445 aCSeq.Append(C);
446 aShSeq.Append(E);
447
448 if (C.IsNull()) continue;
449
450 for (expV.Initialize(E.Oriented(TopAbs_FORWARD));
451 expV.More(); expV.Next()) {
452
453 const TopoDS_Vertex& V = TopoDS::Vertex(expV.Value());
454
455 TopAbs_Orientation Vori = V.Orientation();
456 if ( Vori == TopAbs_INTERNAL ) {
457 continue;
458 }
459
460 Standard_Real p = BRep_Tool::Parameter(V,E,F);
461 C->D0(p,P);
462 if (!theVertices.IsBound(V))
463 theVertices.Bind(V,emptySequence);
464 TColgp_SequenceOfPnt2d& S = theVertices(V);
465 for (i = 1; i <= S.Length(); i++) {
466 if (P.Distance(S(i)) < tolerance) break;
467 }
468 if (i > S.Length())
469 S.Append(P);
470 }
471 }
472 }
473
474 // second edge loop, update the edges 2d points
475 TopoDS_Vertex Vf,Vl;
476 gp_Pnt2d Pf,Pl;
477
478 for(Standard_Integer j = 1; j <= aShSeq.Length(); j++)
479 {
480 const TopoDS_Edge& E = TopoDS::Edge(aShSeq.Value(j));
481 const Handle(Geom2d_Curve)& C = aCSeq.Value(j);
482 if (C.IsNull()) continue;
483
484 TopExp::Vertices(E,Vf,Vl);
485 if (Vf.IsNull()) {
486 Pf.SetCoord(RealLast(),RealLast());
487 }
488 else {
489 if ( Vf.Orientation() == TopAbs_INTERNAL ) {
490 continue;
491 }
492 const TColgp_SequenceOfPnt2d& seqf = theVertices(Vf);
493 if (seqf.Length() == 1)
494 Pf = seqf(1);
495 else {
496 C->D0(aFSeq.Value(j),Pf);
497 for (i = 1; i <= seqf.Length(); i++) {
498 if (Pf.Distance(seqf(i)) <= tolerance) {
499 Pf = seqf(i);
500 break;
501 }
502 }
503 }
504 }
505 if (Vl.IsNull()) {
506 Pl.SetCoord(RealLast(),RealLast());
507 }
508 else {
509 if ( Vl.Orientation() == TopAbs_INTERNAL ) {
510 continue;
511 }
512 const TColgp_SequenceOfPnt2d& seql = theVertices(Vl);
513 if (seql.Length() == 1)
514 Pl = seql(1);
515 else {
516 C->D0(aLSeq.Value(j),Pl);
517 for (i = 1; i <= seql.Length(); i++) {
518 if (Pl.Distance(seql(i)) <= tolerance) {
519 Pl = seql(i);
520 break;
521 }
522 }
523 }
524 }
525
526 // set the correct points
527 BRep_Tool::SetUVPoints(E,F,Pf,Pl);
528 }
529}
530
531
532
533//=======================================================================
534//function : Compare
535//purpose :
536//=======================================================================
537
538Standard_Boolean BRepTools::Compare(const TopoDS_Vertex& V1,
539 const TopoDS_Vertex& V2)
540{
541 if (V1.IsSame(V2)) return Standard_True;
542 gp_Pnt p1 = BRep_Tool::Pnt(V1);
543 gp_Pnt p2 = BRep_Tool::Pnt(V2);
544 Standard_Real l = p1.Distance(p2);
545 if (l <= BRep_Tool::Tolerance(V1)) return Standard_True;
546 if (l <= BRep_Tool::Tolerance(V2)) return Standard_True;
547 return Standard_False;
548}
549
550//=======================================================================
551//function : Compare
552//purpose :
553//=======================================================================
554
555Standard_Boolean BRepTools::Compare(const TopoDS_Edge& E1,
556 const TopoDS_Edge& E2)
557{
558 if (E1.IsSame(E2)) return Standard_True;
559 return Standard_False;
560}
561
562//=======================================================================
563//function : OuterWire
564//purpose :
565//=======================================================================
566
567TopoDS_Wire BRepTools::OuterWire(const TopoDS_Face& F)
568{
569 TopoDS_Wire Wres;
570 TopExp_Explorer expw (F,TopAbs_WIRE);
571
572 if (expw.More()) {
573 Wres = TopoDS::Wire(expw.Current());
574 expw.Next();
575 if (expw.More()) {
576 Standard_Real UMin, UMax, VMin, VMax;
577 Standard_Real umin, umax, vmin, vmax;
578 BRepTools::UVBounds(F,Wres,UMin,UMax,VMin,VMax);
579 while (expw.More()) {
580 const TopoDS_Wire& W = TopoDS::Wire(expw.Current());
581 BRepTools::UVBounds(F,W,umin, umax, vmin, vmax);
582 if ((umin <= UMin) &&
583 (umax >= UMax) &&
584 (vmin <= VMin) &&
585 (vmax >= VMax)) {
586 Wres = W;
587 UMin = umin;
588 UMax = umax;
589 VMin = vmin;
590 VMax = vmax;
591 }
592 expw.Next();
593 }
594 }
595 }
596 return Wres;
597}
598
7fd59977 599//=======================================================================
600//function : Map3DEdges
601//purpose :
602//=======================================================================
603
604void BRepTools::Map3DEdges(const TopoDS_Shape& S,
605 TopTools_IndexedMapOfShape& M)
606{
607 TopExp_Explorer Ex;
608 for (Ex.Init(S,TopAbs_EDGE); Ex.More(); Ex.Next()) {
609 if (!BRep_Tool::Degenerated(TopoDS::Edge(Ex.Current())))
610 M.Add(Ex.Current());
611 }
612}
613
614//=======================================================================
615//function : Dump
616//purpose :
617//=======================================================================
618
619void BRepTools::Dump(const TopoDS_Shape& Sh, Standard_OStream& S)
620{
621 BRepTools_ShapeSet SS;
622 SS.Add(Sh);
623 SS.Dump(Sh,S);
624 SS.Dump(S);
625}
626
7fd59977 627//=======================================================================
628//function : Write
629//purpose :
630//=======================================================================
631
632void BRepTools::Write(const TopoDS_Shape& Sh, Standard_OStream& S,
633 const Handle(Message_ProgressIndicator)& PR)
634{
635 BRepTools_ShapeSet SS;
636 SS.SetProgress(PR);
637 SS.Add(Sh);
638 SS.Write(S);
639 SS.Write(Sh,S);
640}
641
642
643//=======================================================================
644//function : Read
645//purpose :
646//=======================================================================
647
648void BRepTools::Read(TopoDS_Shape& Sh,
649 istream& S,
650 const BRep_Builder& B,
651 const Handle(Message_ProgressIndicator)& PR)
652{
653 BRepTools_ShapeSet SS(B);
654 SS.SetProgress(PR);
655 SS.Read(S);
656 SS.Read(Sh,S);
657}
658
659//=======================================================================
660//function : Write
661//purpose :
662//=======================================================================
663
664Standard_Boolean BRepTools::Write(const TopoDS_Shape& Sh,
665 const Standard_CString File,
666 const Handle(Message_ProgressIndicator)& PR)
667{
668 ofstream os;
669 // if (!fic.open(File,output)) return Standard_False;
670 os.open(File, ios::out);
671 if (!os.rdbuf()->is_open()) return Standard_False;
672
673 Standard_Boolean isGood = (os.good() && !os.eof());
674 if(!isGood)
675 return isGood;
676
677 BRepTools_ShapeSet SS;
678 SS.SetProgress(PR);
679 SS.Add(Sh);
680
681 os << "DBRep_DrawableShape\n"; // for easy Draw read
682 SS.Write(os);
683 isGood = os.good();
684 if(isGood )
685 SS.Write(Sh,os);
686 os.flush();
687 isGood = os.good();
688
689 errno = 0;
690 os.close();
7fd59977 691 isGood = os.good() && isGood && !errno;
692
693 return isGood;
694}
695
696//=======================================================================
697//function : Read
698//purpose :
699//=======================================================================
700
701Standard_Boolean BRepTools::Read(TopoDS_Shape& Sh,
702 const Standard_CString File,
703 const BRep_Builder& B,
704 const Handle(Message_ProgressIndicator)& PR)
705{
706 filebuf fic;
707 istream in(&fic);
708 if (!fic.open(File, ios::in)) return Standard_False;
709
710 BRepTools_ShapeSet SS(B);
711 SS.SetProgress(PR);
712 SS.Read(in);
713 if(!SS.NbShapes()) return Standard_False;
714 SS.Read(Sh,in);
715 return Standard_True;
716}
717
718
719//=======================================================================
720//function : Clean
721//purpose :
722//=======================================================================
723
724void BRepTools::Clean(const TopoDS_Shape& S)
725{
726 BRep_Builder B;
727 TopExp_Explorer ex;
728 Handle(Poly_Triangulation) TNULL, T;
729 Handle(Poly_PolygonOnTriangulation) PolyNULL, Poly;
730
731 if (!S.IsNull()) {
732 TopLoc_Location L;
733 for (ex.Init(S,TopAbs_FACE);ex.More();ex.Next()) {
734 const TopoDS_Face& F = TopoDS::Face(ex.Current());
735 B.UpdateFace(F, TNULL);
736 }
737 for (ex.Init(S, TopAbs_EDGE); ex.More(); ex.Next()) {
738 const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
739// agv 21.09.01 : Inefficient management of Locations -> improve performance
740// do {
741// BRep_Tool::PolygonOnTriangulation(E, Poly, T, L);
742// B.UpdateEdge(E, PolyNULL, T, L);
743// } while(!Poly.IsNull());
744//
745 Handle(BRep_CurveRepresentation) cr;
746 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &E.TShape());
747 BRep_ListOfCurveRepresentation& lcr = TE -> ChangeCurves();
748 BRep_ListIteratorOfListOfCurveRepresentation itcr(lcr);
749
750 // find and remove all representations
751 while (itcr.More()) {
752 cr = itcr.Value();
753 if (cr->IsPolygonOnTriangulation())
754 lcr.Remove(itcr);
755 else
756 itcr.Next();
757 }
758 TE->Modified(Standard_True);
759// agv : fin
760 }
761 }
762}
763
01697018
J
764//=======================================================================
765//function : RemoveUnusedPCurves
766//purpose :
767//=======================================================================
768
769void BRepTools::RemoveUnusedPCurves(const TopoDS_Shape& S)
770{
771 TColStd_MapOfTransient UsedSurfaces;
772
773 TopExp_Explorer Explo(S, TopAbs_FACE);
774 for (; Explo.More(); Explo.Next())
775 {
776 TopoDS_Face aFace = TopoDS::Face(Explo.Current());
777 TopLoc_Location aLoc;
778 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
779 UsedSurfaces.Add(aSurf);
780 }
781
782 TopTools_IndexedMapOfShape Emap;
783 TopExp::MapShapes(S, TopAbs_EDGE, Emap);
7fd59977 784
01697018
J
785 Standard_Integer i;
786 for (i = 1; i <= Emap.Extent(); i++)
787 {
788 const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &Emap(i).TShape());
789 BRep_ListOfCurveRepresentation& lcr = TE -> ChangeCurves();
790 BRep_ListIteratorOfListOfCurveRepresentation itrep(lcr );
791 while (itrep.More())
792 {
793 Standard_Boolean ToRemove = Standard_False;
794
795 Handle(BRep_CurveRepresentation) CurveRep = itrep.Value();
796 if (CurveRep->IsCurveOnSurface())
797 {
798 Handle(Geom_Surface) aSurface = CurveRep->Surface();
799 if (!UsedSurfaces.Contains(aSurface))
800 ToRemove = Standard_True;
801 }
802 else if (CurveRep->IsRegularity())
803 {
804 Handle(Geom_Surface) Surf1 = CurveRep->Surface();
805 Handle(Geom_Surface) Surf2 = CurveRep->Surface2();
806 ToRemove = (!UsedSurfaces.Contains(Surf1) || !UsedSurfaces.Contains(Surf2));
807 }
808
809 if (ToRemove)
810 lcr.Remove(itrep);
811 else
812 itrep.Next();
813 }
814 }
815}
7fd59977 816
817//=======================================================================
818//function : Triangulation
819//purpose :
820//=======================================================================
821
822Standard_Boolean BRepTools::Triangulation(const TopoDS_Shape& S,
823 const Standard_Real deflec)
824{
825 TopExp_Explorer exf, exe;
826 TopLoc_Location l;
827 Handle(Poly_Triangulation) T;
828 Handle(Poly_PolygonOnTriangulation) Poly;
829
830 for (exf.Init(S, TopAbs_FACE); exf.More(); exf.Next()) {
831 const TopoDS_Face& F = TopoDS::Face(exf.Current());
832 T = BRep_Tool::Triangulation(F, l);
833 if (T.IsNull() || (T->Deflection() > deflec))
834 return Standard_False;
835 for (exe.Init(F, TopAbs_EDGE); exe.More(); exe.Next()) {
836 const TopoDS_Edge& E = TopoDS::Edge(exe.Current());
837 Poly = BRep_Tool::PolygonOnTriangulation(E, T, l);
838 if (Poly.IsNull()) return Standard_False;
839 }
840 }
841 return Standard_True;
842}
843
844
845//=======================================================================
846//function : IsReallyClosed
847//purpose :
848//=======================================================================
849
850Standard_Boolean BRepTools::IsReallyClosed(const TopoDS_Edge& E,
851 const TopoDS_Face& F)
852{
853 if (!BRep_Tool::IsClosed(E,F)) {
854 return Standard_False;
855 }
856 Standard_Integer nbocc = 0;
857 TopExp_Explorer exp;
858 for (exp.Init(F,TopAbs_EDGE);exp.More();exp.Next()) {
859 if (exp.Current().IsSame(E)) {
860 nbocc++;
861 }
862 }
863 return nbocc == 2;
864}
865
866
867