0026127: Visualization - Default camera is not copied in copy constructor of V3d_View
[occt.git] / src / ShapeUpgrade / ShapeUpgrade_UnifySameDomain.cxx
CommitLineData
973c2be1 1// Created on: 2012-06-09
2// Created by: jgv@ROLEX
3// Copyright (c) 2012-2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
2277323d 15
42cf5bc1 16
2277323d 17#include <BRep_Builder.hxx>
42cf5bc1 18#include <BRep_Tool.hxx>
19#include <BRepLib.hxx>
20#include <BRepLib_MakeEdge.hxx>
21#include <BRepTopAdaptor_TopolTool.hxx>
22#include <GC_MakeCircle.hxx>
23#include <Geom2d_Line.hxx>
2277323d 24#include <Geom2d_TrimmedCurve.hxx>
2277323d 25#include <Geom2dConvert.hxx>
2277323d 26#include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
2277323d 27#include <Geom_BezierCurve.hxx>
42cf5bc1 28#include <Geom_BSplineCurve.hxx>
29#include <Geom_Circle.hxx>
30#include <Geom_CylindricalSurface.hxx>
31#include <Geom_ElementarySurface.hxx>
32#include <Geom_Line.hxx>
33#include <Geom_OffsetSurface.hxx>
f16a6cc5 34#include <Geom_Plane.hxx>
42cf5bc1 35#include <Geom_RectangularTrimmedSurface.hxx>
36#include <Geom_Surface.hxx>
37#include <Geom_SurfaceOfLinearExtrusion.hxx>
38#include <Geom_SurfaceOfRevolution.hxx>
39#include <Geom_SweptSurface.hxx>
40#include <Geom_TrimmedCurve.hxx>
41#include <GeomAdaptor_HSurface.hxx>
42#include <GeomConvert.hxx>
43#include <GeomConvert_CompCurveToBSplineCurve.hxx>
44#include <GeomLib_IsPlanarSurface.hxx>
45#include <gp_Cylinder.hxx>
46#include <gp_Dir.hxx>
47#include <gp_Lin.hxx>
48#include <IntPatch_ImpImpIntersection.hxx>
49#include <ShapeAnalysis_Edge.hxx>
2277323d 50#include <ShapeAnalysis_WireOrder.hxx>
51#include <ShapeBuild_Edge.hxx>
42cf5bc1 52#include <ShapeBuild_ReShape.hxx>
2277323d 53#include <ShapeFix_Edge.hxx>
42cf5bc1 54#include <ShapeFix_Face.hxx>
2277323d 55#include <ShapeFix_Shell.hxx>
42cf5bc1 56#include <ShapeFix_Wire.hxx>
42cf5bc1 57#include <ShapeUpgrade_UnifySameDomain.hxx>
58#include <Standard_Type.hxx>
59#include <TColGeom2d_Array1OfBSplineCurve.hxx>
60#include <TColGeom2d_HArray1OfBSplineCurve.hxx>
61#include <TColGeom2d_SequenceOfBoundedCurve.hxx>
62#include <TColGeom_Array1OfBSplineCurve.hxx>
63#include <TColGeom_HArray1OfBSplineCurve.hxx>
42cf5bc1 64#include <TColGeom_SequenceOfSurface.hxx>
65#include <TColStd_Array1OfReal.hxx>
66#include <TColStd_MapOfInteger.hxx>
67#include <TopExp.hxx>
68#include <TopExp_Explorer.hxx>
69#include <TopoDS.hxx>
70#include <TopoDS_Edge.hxx>
71#include <TopoDS_Face.hxx>
72#include <TopoDS_Shape.hxx>
73#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
74#include <TopTools_IndexedMapOfShape.hxx>
75#include <TopTools_ListIteratorOfListOfShape.hxx>
b4d4dbe8 76#include <TopTools_MapOfShape.hxx>
42cf5bc1 77#include <TopTools_SequenceOfShape.hxx>
f7d70540 78#include <gp_Circ.hxx>
79#include <BRepAdaptor_Curve.hxx>
f0144633 80#include <BRepAdaptor_Curve2d.hxx>
81#include <gp_Vec2d.hxx>
f7d70540 82
25e59720 83IMPLEMENT_STANDARD_RTTIEXT(ShapeUpgrade_UnifySameDomain,Standard_Transient)
92efcf78 84
f7d70540 85struct SubSequenceOfEdges
86{
87 TopTools_SequenceOfShape SeqsEdges;
88 TopoDS_Edge UnionEdges;
89};
90
f0144633 91static Standard_Boolean IsLikeSeam(const TopoDS_Edge& anEdge,
92 const TopoDS_Face& aFace,
93 const Handle(Geom_Surface)& aBaseSurface)
94{
95 if (!aBaseSurface->IsUPeriodic() && !aBaseSurface->IsVPeriodic())
96 return Standard_False;
97
98 BRepAdaptor_Curve2d BAcurve2d(anEdge, aFace);
99 gp_Pnt2d FirstPoint, LastPoint;
100 gp_Vec2d FirstDir, LastDir;
101 BAcurve2d.D1(BAcurve2d.FirstParameter(), FirstPoint, FirstDir);
102 BAcurve2d.D1(BAcurve2d.LastParameter(), LastPoint, LastDir);
103 Standard_Real Length = FirstDir.Magnitude();
104 if (Length <= gp::Resolution())
105 return Standard_False;
106 else
107 FirstDir /= Length;
108 Length = LastDir.Magnitude();
109 if (Length <= gp::Resolution())
110 return Standard_False;
111 else
112 LastDir /= Length;
113
114 Standard_Real Tol = 1.e-7;
115 if (aBaseSurface->IsUPeriodic() &&
116 (Abs(FirstDir.X()) < Tol) &&
117 (Abs(LastDir.X()) < Tol))
118 return Standard_True;
119
120 if (aBaseSurface->IsVPeriodic() &&
121 (Abs(FirstDir.Y()) < Tol) &&
122 (Abs(LastDir.Y()) < Tol))
123 return Standard_True;
124
125 return Standard_False;
126}
2277323d 127
128//=======================================================================
129//function : AddOrdinaryEdges
130//purpose : auxilary
131//=======================================================================
132// adds edges from the shape to the sequence
133// seams and equal edges are dropped
134// Returns true if one of original edges dropped
135static Standard_Boolean AddOrdinaryEdges(TopTools_SequenceOfShape& edges,
136 const TopoDS_Shape aShape,
137 Standard_Integer& anIndex)
138{
139 //map of edges
140 TopTools_IndexedMapOfShape aNewEdges;
141 //add edges without seams
142 for(TopExp_Explorer exp(aShape,TopAbs_EDGE); exp.More(); exp.Next()) {
143 TopoDS_Shape edge = exp.Current();
144 if(aNewEdges.Contains(edge))
3f5aa017 145 aNewEdges.RemoveKey(edge);
2277323d 146 else
147 aNewEdges.Add(edge);
148 }
149
150 Standard_Boolean isDropped = Standard_False;
151 //merge edges and drop seams
152 Standard_Integer i;
153 for (i = 1; i <= edges.Length(); i++) {
154 TopoDS_Shape current = edges(i);
155 if(aNewEdges.Contains(current)) {
156
3f5aa017 157 aNewEdges.RemoveKey(current);
2277323d 158 edges.Remove(i);
159 i--;
160
161 if(!isDropped) {
162 isDropped = Standard_True;
163 anIndex = i;
164 }
165 }
166 }
167
9ed6494b 168 //add edges to the sequence
2277323d 169 for (i = 1; i <= aNewEdges.Extent(); i++)
170 edges.Append(aNewEdges(i));
171
172 return isDropped;
173}
174
175//=======================================================================
176//function : getCylinder
177//purpose : auxilary
178//=======================================================================
179static Standard_Boolean getCylinder(Handle(Geom_Surface)& theInSurface,
180 gp_Cylinder& theOutCylinder)
181{
182 Standard_Boolean isCylinder = Standard_False;
183
184 if (theInSurface->IsKind(STANDARD_TYPE(Geom_CylindricalSurface))) {
185 Handle(Geom_CylindricalSurface) aGC = Handle(Geom_CylindricalSurface)::DownCast(theInSurface);
186
187 theOutCylinder = aGC->Cylinder();
188 isCylinder = Standard_True;
189 }
190 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfRevolution))) {
191 Handle(Geom_SurfaceOfRevolution) aRS =
192 Handle(Geom_SurfaceOfRevolution)::DownCast(theInSurface);
193 Handle(Geom_Curve) aBasis = aRS->BasisCurve();
194 if (aBasis->IsKind(STANDARD_TYPE(Geom_Line))) {
195 Handle(Geom_Line) aBasisLine = Handle(Geom_Line)::DownCast(aBasis);
196 gp_Dir aDir = aRS->Direction();
197 gp_Dir aBasisDir = aBasisLine->Position().Direction();
198 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
199 // basis line is parallel to the revolution axis: it is a cylinder
200 gp_Pnt aLoc = aRS->Location();
201 Standard_Real aR = aBasisLine->Lin().Distance(aLoc);
202 gp_Ax3 aCylAx (aLoc, aDir);
203
204 theOutCylinder = gp_Cylinder(aCylAx, aR);
205 isCylinder = Standard_True;
206 }
207 }
208 }
209 else if (theInSurface->IsKind(STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion))) {
210 Handle(Geom_SurfaceOfLinearExtrusion) aLES =
211 Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(theInSurface);
212 Handle(Geom_Curve) aBasis = aLES->BasisCurve();
213 if (aBasis->IsKind(STANDARD_TYPE(Geom_Circle))) {
214 Handle(Geom_Circle) aBasisCircle = Handle(Geom_Circle)::DownCast(aBasis);
215 gp_Dir aDir = aLES->Direction();
216 gp_Dir aBasisDir = aBasisCircle->Position().Direction();
217 if (aBasisDir.IsParallel(aDir, Precision::Angular())) {
218 // basis circle is normal to the extrusion axis: it is a cylinder
219 gp_Pnt aLoc = aBasisCircle->Location();
220 Standard_Real aR = aBasisCircle->Radius();
221 gp_Ax3 aCylAx (aLoc, aDir);
222
223 theOutCylinder = gp_Cylinder(aCylAx, aR);
224 isCylinder = Standard_True;
225 }
226 }
227 }
228 else {
229 }
230
231 return isCylinder;
232}
233
234//=======================================================================
235//function : ClearRts
236//purpose : auxilary
237//=======================================================================
238static Handle(Geom_Surface) ClearRts(const Handle(Geom_Surface)& aSurface)
239{
240 if(aSurface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface))) {
241 Handle(Geom_RectangularTrimmedSurface) rts =
242 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
243 return rts->BasisSurface();
244 }
245 return aSurface;
246}
247
10ce3246 248//=======================================================================
249//function : GetNormalToSurface
250//purpose : Gets the normal to surface by the given parameter on edge.
251// Returns True if normal was computed.
252//=======================================================================
253static Standard_Boolean GetNormalToSurface(const TopoDS_Face& theFace,
254 const TopoDS_Edge& theEdge,
255 const Standard_Real theP,
256 gp_Dir& theNormal)
257{
258 Standard_Real f, l;
259 // get 2d curve to get point in 2d
260 const Handle(Geom2d_Curve)& aC2d = BRep_Tool::CurveOnSurface(theEdge, theFace, f, l);
261 if (aC2d.IsNull()) {
262 return Standard_False;
263 }
264 //
265 // 2d point
266 gp_Pnt2d aP2d;
267 aC2d->D0(theP, aP2d);
268 //
269 // get D1
270 gp_Vec aDU, aDV;
271 gp_Pnt aP3d;
272 TopLoc_Location aLoc;
273 const Handle(Geom_Surface)& aS = BRep_Tool::Surface(theFace, aLoc);
274 aS->D1(aP2d.X(), aP2d.Y(), aP3d, aDU, aDV);
275 //
276 // compute normal
277 gp_Vec aVNormal = aDU.Crossed(aDV);
278 if (aVNormal.Magnitude() < Precision::Confusion()) {
279 return Standard_False;
280 }
281 //
282 if (theFace.Orientation() == TopAbs_REVERSED) {
283 aVNormal.Reverse();
284 }
285 //
286 aVNormal.Transform(aLoc.Transformation());
287 theNormal = gp_Dir(aVNormal);
288 return Standard_True;
289}
290
2277323d 291//=======================================================================
292//function : IsSameDomain
293//purpose :
294//=======================================================================
295static Standard_Boolean IsSameDomain(const TopoDS_Face& aFace,
2ba9eb30 296 const TopoDS_Face& aCheckedFace,
297 const Standard_Real theLinTol,
298 const Standard_Real theAngTol)
2277323d 299{
300 //checking the same handles
301 TopLoc_Location L1, L2;
302 Handle(Geom_Surface) S1, S2;
303
304 S1 = BRep_Tool::Surface(aFace,L1);
305 S2 = BRep_Tool::Surface(aCheckedFace,L2);
306
307 if (S1 == S2 && L1 == L2)
308 return Standard_True;
309
2277323d 310 S1 = BRep_Tool::Surface(aFace);
311 S2 = BRep_Tool::Surface(aCheckedFace);
312
313 S1 = ClearRts(S1);
314 S2 = ClearRts(S2);
315
316 //Handle(Geom_OffsetSurface) aGOFS1, aGOFS2;
317 //aGOFS1 = Handle(Geom_OffsetSurface)::DownCast(S1);
318 //aGOFS2 = Handle(Geom_OffsetSurface)::DownCast(S2);
319 //if (!aGOFS1.IsNull()) S1 = aGOFS1->BasisSurface();
320 //if (!aGOFS2.IsNull()) S2 = aGOFS2->BasisSurface();
321
2ba9eb30 322 // case of two planar surfaces:
323 // all kinds of surfaces checked, including b-spline and bezier
324 GeomLib_IsPlanarSurface aPlanarityChecker1(S1, theLinTol);
325 if (aPlanarityChecker1.IsPlanar()) {
326 GeomLib_IsPlanarSurface aPlanarityChecker2(S2, theLinTol);
327 if (aPlanarityChecker2.IsPlanar()) {
328 gp_Pln aPln1 = aPlanarityChecker1.Plan();
329 gp_Pln aPln2 = aPlanarityChecker2.Plan();
330
331 if (aPln1.Position().Direction().IsParallel(aPln2.Position().Direction(), theAngTol) &&
332 aPln1.Distance(aPln2) < theLinTol) {
333 return Standard_True;
334 }
335 }
336 }
337
2277323d 338 // case of two elementary surfaces: use OCCT tool
339 // elementary surfaces: ConicalSurface, CylindricalSurface,
340 // Plane, SphericalSurface and ToroidalSurface
341 if (S1->IsKind(STANDARD_TYPE(Geom_ElementarySurface)) &&
342 S2->IsKind(STANDARD_TYPE(Geom_ElementarySurface)))
343 {
344 Handle(GeomAdaptor_HSurface) aGA1 = new GeomAdaptor_HSurface(S1);
345 Handle(GeomAdaptor_HSurface) aGA2 = new GeomAdaptor_HSurface(S2);
346
347 Handle(BRepTopAdaptor_TopolTool) aTT1 = new BRepTopAdaptor_TopolTool();
348 Handle(BRepTopAdaptor_TopolTool) aTT2 = new BRepTopAdaptor_TopolTool();
349
350 try {
2ba9eb30 351 IntPatch_ImpImpIntersection anIIInt(aGA1, aTT1, aGA2, aTT2, theLinTol, theLinTol);
2277323d 352 if (!anIIInt.IsDone() || anIIInt.IsEmpty())
353 return Standard_False;
354
355 return anIIInt.TangentFaces();
356 }
357 catch (Standard_Failure) {
358 return Standard_False;
359 }
360 }
361
2277323d 362 // case of two cylindrical surfaces, at least one of which is a swept surface
363 // swept surfaces: SurfaceOfLinearExtrusion, SurfaceOfRevolution
364 if ((S1->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
365 S1->IsKind(STANDARD_TYPE(Geom_SweptSurface))) &&
366 (S2->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) ||
367 S2->IsKind(STANDARD_TYPE(Geom_SweptSurface))))
368 {
369 gp_Cylinder aCyl1, aCyl2;
370 if (getCylinder(S1, aCyl1) && getCylinder(S2, aCyl2)) {
2ba9eb30 371 if (fabs(aCyl1.Radius() - aCyl2.Radius()) < theLinTol) {
2277323d 372 gp_Dir aDir1 = aCyl1.Position().Direction();
373 gp_Dir aDir2 = aCyl2.Position().Direction();
374 if (aDir1.IsParallel(aDir2, Precision::Angular())) {
375 gp_Pnt aLoc1 = aCyl1.Location();
376 gp_Pnt aLoc2 = aCyl2.Location();
377 gp_Vec aVec12 (aLoc1, aLoc2);
2ba9eb30 378 if (aVec12.SquareMagnitude() < theLinTol*theLinTol ||
2277323d 379 aVec12.IsParallel(aDir1, Precision::Angular())) {
380 return Standard_True;
381 }
382 }
383 }
384 }
385 }
386
387 return Standard_False;
388}
389
632175c3 390//=======================================================================
391//function : UpdateMapOfShapes
392//purpose :
393//=======================================================================
394static void UpdateMapOfShapes(TopTools_MapOfShape& theMapOfShapes,
395 Handle(ShapeBuild_ReShape)& theContext)
396{
397 for (TopTools_MapIteratorOfMapOfShape it(theMapOfShapes); it.More(); it.Next()) {
398 const TopoDS_Shape& aShape = it.Value();
399 TopoDS_Shape aContextShape = theContext->Apply(aShape);
400 if (!aContextShape.IsSame(aShape))
401 theMapOfShapes.Add(aContextShape);
402 }
403}
404
2277323d 405//=======================================================================
406//function : GlueEdgesWithPCurves
407//purpose : Glues the pcurves of the sequence of edges
408// and glues their 3d curves
409//=======================================================================
410static TopoDS_Edge GlueEdgesWithPCurves(const TopTools_SequenceOfShape& aChain,
411 const TopoDS_Vertex& FirstVertex,
412 const TopoDS_Vertex& LastVertex)
413{
414 Standard_Integer i, j;
415
416 TopoDS_Edge FirstEdge = TopoDS::Edge(aChain(1));
2277323d 417 TColGeom_SequenceOfSurface SurfSeq;
fbfdf61a 418 NCollection_Sequence<TopLoc_Location> LocSeq;
2277323d 419
fbfdf61a 420 for (int aCurveIndex = 0;; aCurveIndex++)
2277323d 421 {
fbfdf61a 422 Handle(Geom2d_Curve) aCurve;
423 Handle(Geom_Surface) aSurface;
424 TopLoc_Location aLocation;
425 Standard_Real aFirst, aLast;
426 BRep_Tool::CurveOnSurface (FirstEdge, aCurve, aSurface, aLocation, aFirst, aLast, aCurveIndex);
427 if (aCurve.IsNull())
428 break;
429
430 SurfSeq.Append(aSurface);
431 LocSeq.Append(aLocation);
2277323d 432 }
433
434 Standard_Real fpar, lpar;
435 BRep_Tool::Range(FirstEdge, fpar, lpar);
436 TopoDS_Edge PrevEdge = FirstEdge;
437 TopoDS_Vertex CV;
438 Standard_Real MaxTol = 0.;
439
440 TopoDS_Edge ResEdge;
441 BRep_Builder BB;
442
443 Standard_Integer nb_curve = aChain.Length(); //number of curves
444 TColGeom_Array1OfBSplineCurve tab_c3d(0,nb_curve-1); //array of the curves
445 TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2); //array of the tolerances
446
447 TopoDS_Vertex PrevVertex = FirstVertex;
448 for (i = 1; i <= nb_curve; i++)
449 {
450 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
451 TopoDS_Vertex VF, VL;
452 TopExp::Vertices(anEdge, VF, VL);
453 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
454
455 Standard_Real Tol1 = BRep_Tool::Tolerance(VF);
456 Standard_Real Tol2 = BRep_Tool::Tolerance(VL);
457 if (Tol1 > MaxTol)
458 MaxTol = Tol1;
459 if (Tol2 > MaxTol)
460 MaxTol = Tol2;
461
462 if (i > 1)
463 {
464 TopExp::CommonVertex(PrevEdge, anEdge, CV);
465 Standard_Real Tol = BRep_Tool::Tolerance(CV);
466 tabtolvertex(i-2) = Tol;
467 }
468
469 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, fpar, lpar);
470 Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(aCurve, fpar, lpar);
471 tab_c3d(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
472 GeomConvert::C0BSplineToC1BSplineCurve(tab_c3d(i-1), Precision::Confusion());
473 if (ToReverse)
474 tab_c3d(i-1)->Reverse();
475 PrevVertex = (ToReverse)? VF : VL;
476 PrevEdge = anEdge;
477 }
478 Handle(TColGeom_HArray1OfBSplineCurve) concatcurve; //array of the concatenated curves
479 Handle(TColStd_HArray1OfInteger) ArrayOfIndices; //array of the remining Vertex
480 GeomConvert::ConcatC1(tab_c3d,
481 tabtolvertex,
482 ArrayOfIndices,
483 concatcurve,
484 Standard_False,
485 Precision::Confusion()); //C1 concatenation
486
487 if (concatcurve->Length() > 1)
488 {
489 GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
490
491 for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
492 Concat.Add( concatcurve->Value(i), MaxTol, Standard_True );
493
494 concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
495 }
496 Handle(Geom_BSplineCurve) ResCurve = concatcurve->Value(concatcurve->Lower());
497
498 TColGeom2d_SequenceOfBoundedCurve ResPCurves;
2277323d 499 for (j = 1; j <= SurfSeq.Length(); j++)
500 {
501 TColGeom2d_Array1OfBSplineCurve tab_c2d(0,nb_curve-1); //array of the pcurves
502
503 PrevVertex = FirstVertex;
504 PrevEdge = FirstEdge;
2277323d 505 for (i = 1; i <= nb_curve; i++)
506 {
507 TopoDS_Edge anEdge = TopoDS::Edge(aChain(i));
508 TopoDS_Vertex VF, VL;
509 TopExp::Vertices(anEdge, VF, VL);
510 Standard_Boolean ToReverse = (!VF.IsSame(PrevVertex));
511
2277323d 512 Handle(Geom2d_Curve) aPCurve =
fbfdf61a 513 BRep_Tool::CurveOnSurface(anEdge, SurfSeq(j), LocSeq(j), fpar, lpar);
514 if (aPCurve.IsNull())
515 continue;
2277323d 516 Handle(Geom2d_TrimmedCurve) aTrPCurve = new Geom2d_TrimmedCurve(aPCurve, fpar, lpar);
517 tab_c2d(i-1) = Geom2dConvert::CurveToBSplineCurve(aTrPCurve);
518 Geom2dConvert::C0BSplineToC1BSplineCurve(tab_c2d(i-1), Precision::Confusion());
519 if (ToReverse)
520 tab_c2d(i-1)->Reverse();
521 PrevVertex = (ToReverse)? VF : VL;
522 PrevEdge = anEdge;
523 }
524 Handle(TColGeom2d_HArray1OfBSplineCurve) concatc2d; //array of the concatenated curves
525 Handle(TColStd_HArray1OfInteger) ArrayOfInd2d; //array of the remining Vertex
526 Geom2dConvert::ConcatC1(tab_c2d,
527 tabtolvertex,
528 ArrayOfInd2d,
529 concatc2d,
530 Standard_False,
531 Precision::Confusion()); //C1 concatenation
532
533 if (concatc2d->Length() > 1)
534 {
535 Geom2dConvert_CompCurveToBSplineCurve Concat2d(concatc2d->Value(concatc2d->Lower()));
536
537 for (i = concatc2d->Lower()+1; i <= concatc2d->Upper(); i++)
538 Concat2d.Add( concatc2d->Value(i), MaxTol, Standard_True );
539
540 concatc2d->SetValue(concatc2d->Lower(), Concat2d.BSplineCurve());
541 }
542 Handle(Geom2d_BSplineCurve) aResPCurve = concatc2d->Value(concatc2d->Lower());
543 ResPCurves.Append(aResPCurve);
544 }
545
546 ResEdge = BRepLib_MakeEdge(ResCurve,
547 FirstVertex, LastVertex,
548 ResCurve->FirstParameter(), ResCurve->LastParameter());
549 BB.SameRange(ResEdge, Standard_False);
550 BB.SameParameter(ResEdge, Standard_False);
551 for (j = 1; j <= ResPCurves.Length(); j++)
552 {
fbfdf61a 553 BB.UpdateEdge(ResEdge, ResPCurves(j), SurfSeq(j), LocSeq(j), MaxTol);
554 BB.Range(ResEdge, SurfSeq(j), LocSeq(j), ResPCurves(j)->FirstParameter(), ResPCurves(j)->LastParameter());
2277323d 555 }
556
557 BRepLib::SameParameter(ResEdge, MaxTol, Standard_True);
558
559 return ResEdge;
560}
561
cef6867c 562//=======================================================================
563//function : MergeSubSeq
564//purpose : Merges a sequence of edges into one edge if possible
565//=======================================================================
566
632175c3 567static Standard_Boolean MergeSubSeq(const TopTools_SequenceOfShape& aChain,
568 TopoDS_Edge& OutEdge,
569 double theAngTol,
570 Standard_Boolean ConcatBSplines,
571 Standard_Boolean isSafeInputMode,
572 Handle(ShapeBuild_ReShape)& theContext)
2277323d 573{
2277323d 574 ShapeAnalysis_Edge sae;
f7d70540 575 BRep_Builder B;
2277323d 576 // union edges in chain
f7d70540 577 int j;
2277323d 578 Standard_Real fp1,lp1,fp2,lp2;
f7d70540 579 Standard_Boolean IsUnionOfLinesPossible = Standard_True;
580 Standard_Boolean IsUnionOfCirclesPossible = Standard_True;
581 Handle(Geom_Curve) c3d1, c3d2;
582 for(j=1; j<aChain.Length(); j++)
583 {
2277323d 584 TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
f7d70540 585 c3d1 = BRep_Tool::Curve(edge1,fp1,lp1);
586
587 TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
588 c3d2 = BRep_Tool::Curve(edge2,fp2,lp2);
589
590 if(c3d1.IsNull() || c3d2.IsNull())
591 return Standard_False;
b4d4dbe8 592
2277323d 593 while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
594 Handle(Geom_TrimmedCurve) tc =
595 Handle(Geom_TrimmedCurve)::DownCast(c3d1);
596 c3d1 = tc->BasisCurve();
597 }
2277323d 598 while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
599 Handle(Geom_TrimmedCurve) tc =
600 Handle(Geom_TrimmedCurve)::DownCast(c3d2);
601 c3d2 = tc->BasisCurve();
602 }
603 if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
2277323d 604 Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
605 Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
606 gp_Dir Dir1 = L1->Position().Direction();
607 gp_Dir Dir2 = L2->Position().Direction();
632175c3 608 if(!Dir1.IsParallel(Dir2,theAngTol))
f7d70540 609 IsUnionOfLinesPossible = Standard_False;
2277323d 610 }
f7d70540 611 else
612 IsUnionOfLinesPossible = Standard_False;
2277323d 613 if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
2277323d 614 Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
615 Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
616 gp_Pnt P01 = C1->Location();
617 gp_Pnt P02 = C2->Location();
b4d4dbe8 618 if (P01.Distance(P02) > Precision::Confusion())
f7d70540 619 IsUnionOfCirclesPossible = Standard_False;
620 }
621 else
622 IsUnionOfCirclesPossible = Standard_False;
623 }
624 if (IsUnionOfLinesPossible && IsUnionOfCirclesPossible)
625 return Standard_False;
626
627 //union of lines is possible
628 if (IsUnionOfLinesPossible)
629 {
632175c3 630 TopoDS_Vertex V[2];
631 V[0] = sae.FirstVertex(TopoDS::Edge(aChain.First()));
632 gp_Pnt PV1 = BRep_Tool::Pnt(V[0]);
633 V[1] = sae.LastVertex(TopoDS::Edge(aChain.Last()));
634 gp_Pnt PV2 = BRep_Tool::Pnt(V[1]);
f7d70540 635 gp_Vec Vec(PV1, PV2);
632175c3 636 if (isSafeInputMode) {
637 for (int k = 0; k < 2; k++) {
638 if (!theContext->IsRecorded(V[k])) {
639 TopoDS_Vertex Vcopy = TopoDS::Vertex(V[k].EmptyCopied());
640 theContext->Replace(V[k], Vcopy);
641 V[k] = Vcopy;
642 }
643 else
644 V[k] = TopoDS::Vertex(theContext->Apply(V[k]));
645 }
646 }
f7d70540 647 Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
648 Standard_Real dist = PV1.Distance(PV2);
649 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
650 TopoDS_Edge E;
651 B.MakeEdge (E, tc ,Precision::Confusion());
632175c3 652 B.Add (E,V[0]); B.Add (E,V[1]);
653 B.UpdateVertex(V[0], 0., E, 0.);
654 B.UpdateVertex(V[1], dist, E, 0.);
f7d70540 655 OutEdge = E;
656 return Standard_True;
657 }
658
659 if (IsUnionOfCirclesPossible)
660 {
661 double f,l;
662 TopoDS_Edge FE = TopoDS::Edge(aChain.First());
663 Handle(Geom_Curve) c3d = BRep_Tool::Curve(FE,f,l);
664
665 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
666 Handle(Geom_TrimmedCurve) tc =
667 Handle(Geom_TrimmedCurve)::DownCast(c3d);
668 c3d = tc->BasisCurve();
669 }
670 Handle(Geom_Circle) Cir = Handle(Geom_Circle)::DownCast(c3d);
671
632175c3 672 TopoDS_Vertex V[2];
673 V[0] = sae.FirstVertex(FE);
674 V[1] = sae.LastVertex(TopoDS::Edge(aChain.Last()));
f7d70540 675 TopoDS_Edge E;
632175c3 676 if (V[0].IsSame(V[1])) {
f7d70540 677 // closed chain
dd2f1b75 678 BRepAdaptor_Curve adef(FE);
679 Handle(Geom_Circle) Cir1;
680 double FP, LP;
681 if ( FE.Orientation() == TopAbs_FORWARD)
682 {
683 FP = adef.FirstParameter();
684 LP = adef.LastParameter();
685 }
686 else
687 {
688 FP = adef.LastParameter();
689 LP = adef.FirstParameter();
690 }
691 if (Abs(FP) < Precision::PConfusion())
692 {
f0144633 693 B.MakeEdge (E,Cir, Precision::Confusion());
632175c3 694 B.Add(E,V[0]);
695 B.Add(E,V[1]);
f0144633 696 E.Orientation(FE.Orientation());
697 }
dd2f1b75 698 else
699 {
700 GC_MakeCircle MC1 (adef.Value(FP), adef.Value((FP + LP) * 0.5), adef.Value(LP));
701 if (MC1.IsDone())
702 Cir1 = MC1.Value();
703 else
704 return Standard_False;
705 B.MakeEdge (E, Cir1, Precision::Confusion());
632175c3 706 B.Add(E,V[0]);
707 B.Add(E,V[1]);
dd2f1b75 708 }
f7d70540 709 }
710 else {
632175c3 711 if (isSafeInputMode) {
712 for (int k = 0; k < 2; k++) {
713 if (!theContext->IsRecorded(V[k])) {
714 TopoDS_Vertex Vcopy = TopoDS::Vertex(V[k].EmptyCopied());
715 theContext->Replace(V[k], Vcopy);
716 V[k] = Vcopy;
717 }
718 else
719 V[k] = TopoDS::Vertex(theContext->Apply(V[k]));
720 }
721 }
722 gp_Pnt PV1 = BRep_Tool::Pnt(V[0]);
723 gp_Pnt PV2 = BRep_Tool::Pnt(V[1]);
dd2f1b75 724 TopoDS_Vertex VM = sae.LastVertex(FE);
725 gp_Pnt PVM = BRep_Tool::Pnt(VM);
726 GC_MakeCircle MC (PV1,PVM,PV2);
f7d70540 727 Handle(Geom_Circle) C = MC.Value();
728 gp_Pnt P0 = C->Location();
729 gp_Dir D1(gp_Vec(P0,PV1));
730 gp_Dir D2(gp_Vec(P0,PV2));
731 Standard_Real fpar = C->XAxis().Direction().Angle(D1);
732 if(fabs(fpar)>Precision::Confusion()) {
733 // check orientation
734 gp_Dir ND = C->XAxis().Direction().Crossed(D1);
735 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
736 fpar = -fpar;
2277323d 737 }
f7d70540 738 }
739 Standard_Real lpar = C->XAxis().Direction().Angle(D2);
740 if(fabs(lpar)>Precision::Confusion()) {
741 // check orientation
742 gp_Dir ND = C->XAxis().Direction().Crossed(D2);
743 if(ND.IsOpposite(C->Axis().Direction(),Precision::Confusion())) {
744 lpar = -lpar;
2277323d 745 }
2277323d 746 }
f7d70540 747 if (lpar < fpar) lpar += 2*M_PI;
748 Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
749 B.MakeEdge (E,tc,Precision::Confusion());
632175c3 750 B.Add(E,V[0]);
751 B.Add(E,V[1]);
752 B.UpdateVertex(V[0], fpar, E, 0.);
753 B.UpdateVertex(V[1], lpar, E, 0.);
2277323d 754 }
f7d70540 755 OutEdge = E;
756 return Standard_True;
2277323d 757 }
758 if (aChain.Length() > 1 && ConcatBSplines) {
759 // second step: union edges with various curves
760 // skl for bug 0020052 from Mantis: perform such unions
761 // only if curves are bspline or bezier
f7d70540 762
763 TopoDS_Vertex VF = sae.FirstVertex(TopoDS::Edge(aChain.First()));
764 TopoDS_Vertex VL = sae.LastVertex(TopoDS::Edge(aChain.Last()));
765 Standard_Boolean NeedUnion = Standard_True;
2277323d 766 for(j=1; j<=aChain.Length(); j++) {
767 TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
b4d4dbe8 768 TopLoc_Location Loc;
2277323d 769 Handle(Geom_Curve) c3d = BRep_Tool::Curve(edge,Loc,fp1,lp1);
770 if(c3d.IsNull()) continue;
771 while(c3d->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
772 Handle(Geom_TrimmedCurve) tc =
773 Handle(Geom_TrimmedCurve)::DownCast(c3d);
774 c3d = tc->BasisCurve();
775 }
776 if( ( c3d->IsKind(STANDARD_TYPE(Geom_BSplineCurve)) ||
777 c3d->IsKind(STANDARD_TYPE(Geom_BezierCurve)) ) ) continue;
f7d70540 778 NeedUnion = Standard_False;
2277323d 779 break;
780 }
781 if(NeedUnion) {
0797d9d3 782#ifdef OCCT_DEBUG
2277323d 783 cout<<"can not make analitical union => make approximation"<<endl;
784#endif
785 TopoDS_Edge E = GlueEdgesWithPCurves(aChain, VF, VL);
f7d70540 786 OutEdge = E;
787 return Standard_True;
2277323d 788 }
789 else {
0797d9d3 790#ifdef OCCT_DEBUG
2277323d 791 cout<<"can not make approximation for such types of curves"<<endl;
792#endif
793 return Standard_False;
794 }
795 }
f7d70540 796 return Standard_False;
797}
798
cef6867c 799//=======================================================================
800//function : IsMergingPossible
801//purpose : Checks if merging of two edges is possible
802//=======================================================================
803
f7d70540 804static Standard_Boolean IsMergingPossible(const TopoDS_Edge& edge1, const TopoDS_Edge& edge2,
1f59dfa9 805 double theAngTol, double theLinTol,
806 const TopTools_MapOfShape& AvoidEdgeVrt, const bool theLineDirectionOk,
807 const gp_Pnt& theFirstPoint, const gp_Vec& theDirectionVec)
f7d70540 808{
809 TopoDS_Vertex CV = TopExp::LastVertex(edge1, Standard_True);
9ed6494b 810 if (CV.IsNull() || AvoidEdgeVrt.Contains(CV))
f7d70540 811 return Standard_False;
812
813 BRepAdaptor_Curve ade1(edge1);
814 BRepAdaptor_Curve ade2(edge2);
815
816 GeomAbs_CurveType t1 = ade1.GetType();
817 GeomAbs_CurveType t2 = ade2.GetType();
818
819 if( t1 == GeomAbs_Circle && t2 == GeomAbs_Circle)
820 {
821 if (ade1.Circle().Location().Distance(ade2.Circle().Location()) > Precision::Confusion())
822 return Standard_False;
823 }
824
825 if( ( (t1 != GeomAbs_BezierCurve && t1 != GeomAbs_BSplineCurve) ||
826 (t2 != GeomAbs_BezierCurve && t2 != GeomAbs_BSplineCurve)) && t1 != t2)
827 return Standard_False;
828
829 gp_Vec Diff1, Diff2;
830 gp_Pnt P1, P2;
831 if (edge1.Orientation() == TopAbs_FORWARD)
832 ade1.D1(ade1.LastParameter(), P1, Diff1);
833 else
834 {
835 ade1.D1(ade1.FirstParameter(), P1, Diff1);
836 Diff1 = -Diff1;
837 }
838
839 if (edge2.Orientation() == TopAbs_FORWARD)
840 ade2.D1(ade2.FirstParameter(), P2, Diff2);
841 else
842 {
843 ade2.D1(ade2.LastParameter(), P2, Diff2);
844 Diff2 = -Diff2;
845 }
846
632175c3 847 if (Diff1.Angle(Diff2) > theAngTol)
f7d70540 848 return Standard_False;
849
1f59dfa9 850 if (theLineDirectionOk && t2 == GeomAbs_Line)
851 {
06084bb8 852 Standard_Real aLast = (edge2.Orientation() == TopAbs_FORWARD) ?
853 ade2.LastParameter() : ade2.FirstParameter();
854 gp_Vec aCurV(theFirstPoint, ade2.Value(aLast));
1f59dfa9 855 Standard_Real aDD = theDirectionVec.CrossSquareMagnitude(aCurV);
856 if (aDD > theLinTol*theLinTol)
857 return Standard_False;
858 }
859
860 return Standard_True;
861}
862
863//=======================================================================
864//function : GetLineEdgePoints
865//purpose :
866//=======================================================================
867static Standard_Boolean GetLineEdgePoints(const TopoDS_Edge& theInpEdge, gp_Pnt& theFirstPoint, gp_Vec& theDirectionVec)
868{
869 double f, l;
870 Handle(Geom_Curve) aCur = BRep_Tool::Curve(theInpEdge, f, l);
871 if(aCur.IsNull())
872 return Standard_False;
873
874 Handle(Geom_TrimmedCurve) aTC = Handle(Geom_TrimmedCurve)::DownCast(aCur);
875 if (!aTC.IsNull())
876 aCur = aTC->BasisCurve();
877
878 if (aCur->DynamicType() != STANDARD_TYPE(Geom_Line))
879 return Standard_False;
880
06084bb8 881 if (theInpEdge.Orientation() == TopAbs_REVERSED) {
882 Standard_Real tmp = f;
883 f = l;
884 l = tmp;
885 }
886 theFirstPoint = aCur->Value(f);
887 gp_Pnt aLP = aCur->Value(l);
1f59dfa9 888 theDirectionVec = aLP.XYZ().Subtracted(theFirstPoint.XYZ());
889 theDirectionVec.Normalize();
f7d70540 890 return Standard_True;
891}
892
cef6867c 893//=======================================================================
894//function : GenerateSubSeq
895//purpose : Generates sub-sequences of edges from sequence of edges
896//Edges from each subsequences can be merged into the one edge
897//=======================================================================
898
f7d70540 899static void GenerateSubSeq (const TopTools_SequenceOfShape& anInpEdgeSeq,
900 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
1f59dfa9 901 Standard_Boolean IsClosed, double theAngTol, double theLinTol,
902 const TopTools_MapOfShape& AvoidEdgeVrt)
f7d70540 903{
904 Standard_Boolean isOk = Standard_False;
905 TopoDS_Edge edge1, edge2;
906
907 SubSequenceOfEdges SubSeq;
1f59dfa9 908 TopoDS_Edge RefEdge = TopoDS::Edge(anInpEdgeSeq(1));
909 SubSeq.SeqsEdges.Append(RefEdge);
f7d70540 910 SeqOfSubSeqOfEdges.Append(SubSeq);
911
1f59dfa9 912 gp_Pnt aFirstPoint;
913 gp_Vec aDirectionVec;
914 Standard_Boolean isLineDirectionOk = GetLineEdgePoints(RefEdge, aFirstPoint, aDirectionVec);
915
f7d70540 916 for (int i = 1; i < anInpEdgeSeq.Length(); i++)
917 {
918 edge1 = TopoDS::Edge(anInpEdgeSeq(i));
919 edge2 = TopoDS::Edge(anInpEdgeSeq(i+1));
1f59dfa9 920 isOk = IsMergingPossible(edge1, edge2, theAngTol, theLinTol, AvoidEdgeVrt, isLineDirectionOk, aFirstPoint, aDirectionVec);
f7d70540 921 if (!isOk)
922 {
51740958 923 SubSequenceOfEdges aSubSeq;
924 aSubSeq.SeqsEdges.Append(edge2);
925 SeqOfSubSeqOfEdges.Append(aSubSeq);
1f59dfa9 926 isLineDirectionOk = GetLineEdgePoints(edge2, aFirstPoint, aDirectionVec);
f7d70540 927 }
928 else
929 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(edge2);
930 }
931 /// check first and last chain segments
932 if (IsClosed && SeqOfSubSeqOfEdges.Length() > 1)
933 {
934 edge1 = TopoDS::Edge(anInpEdgeSeq.Last());
935 edge2 = TopoDS::Edge(anInpEdgeSeq.First());
1f59dfa9 936 if (IsMergingPossible(edge1, edge2, theAngTol, theLinTol, AvoidEdgeVrt, Standard_False, aFirstPoint, aDirectionVec))
f7d70540 937 {
938 SeqOfSubSeqOfEdges.ChangeLast().SeqsEdges.Append(SeqOfSubSeqOfEdges.ChangeFirst().SeqsEdges);
939 SeqOfSubSeqOfEdges.Remove(1);
940 }
941 }
942}
943
f7d70540 944//=======================================================================
945//function : MergeEdges
946//purpose : auxilary
947//=======================================================================
f98965d2 948static Standard_Boolean MergeEdges(TopTools_SequenceOfShape& SeqEdges,
632175c3 949 const Standard_Real theAngTol,
1f59dfa9 950 const Standard_Real theLinTol,
f7d70540 951 const Standard_Boolean ConcatBSplines,
632175c3 952 const Standard_Boolean isSafeInputMode,
953 Handle(ShapeBuild_ReShape)& theContext,
f7d70540 954 NCollection_Sequence<SubSequenceOfEdges>& SeqOfSubSeqOfEdges,
9ed6494b 955 const TopTools_MapOfShape& NonMergVrt)
f7d70540 956{
f98965d2 957 // skip degenerated edges, and forbid merging through them
958 TopTools_IndexedDataMapOfShapeListOfShape aMapVE;
f7d70540 959 Standard_Integer j;
960 TopTools_MapOfShape VerticesToAvoid;
f7d70540 961 for (j = 1; j <= SeqEdges.Length(); j++)
962 {
963 TopoDS_Edge anEdge = TopoDS::Edge(SeqEdges(j));
964 if (BRep_Tool::Degenerated(anEdge))
965 {
966 TopoDS_Vertex V1, V2;
967 TopExp::Vertices(anEdge, V1, V2);
968 VerticesToAvoid.Add(V1);
969 VerticesToAvoid.Add(V2);
f98965d2 970 SeqEdges.Remove(j--);
971 }
972 else
973 {
974 // fill in the map V-E
975 for (TopoDS_Iterator it(anEdge.Oriented(TopAbs_FORWARD)); it.More(); it.Next())
976 {
977 TopoDS_Shape aV = it.Value();
978 if (aV.Orientation() == TopAbs_FORWARD || aV.Orientation() == TopAbs_REVERSED)
979 {
980 if (!aMapVE.Contains(aV))
981 aMapVE.Add(aV, TopTools_ListOfShape());
982 aMapVE.ChangeFromKey(aV).Append(anEdge);
38c2acd4 983 }
f98965d2 984 }
f7d70540 985 }
f7d70540 986 }
f98965d2 987 VerticesToAvoid.Unite(NonMergVrt);
f7d70540 988
f98965d2 989 // do loop while there are unused edges
990 TopTools_MapOfShape aUsedEdges;
991 for (;;)
992 {
993 TopoDS_Edge edge;
994 for(j=1; j <= SeqEdges.Length(); j++)
995 {
996 edge = TopoDS::Edge(SeqEdges.Value(j));
997 if (!aUsedEdges.Contains(edge))
998 break;
999 }
1000 if (j > SeqEdges.Length())
1001 break; // all edges have been used
1002
1003 // make chain for unite
1004 TopTools_SequenceOfShape aChain;
38c2acd4 1005 aChain.Append(edge);
f98965d2 1006 aUsedEdges.Add(edge);
1007 TopoDS_Vertex V[2];
1008 TopExp::Vertices(edge, V[0], V[1], Standard_True);
1009
1010 // connect more edges to the chain in both directions
1011 for (j = 0; j < 2; j++)
1012 {
1013 Standard_Boolean isAdded = Standard_True;
1014 while (isAdded)
1015 {
1016 isAdded = Standard_False;
1017 if (V[j].IsNull())
1018 break;
1019 const TopTools_ListOfShape& aLE = aMapVE.FindFromKey(V[j]);
1020 for (TopTools_ListIteratorOfListOfShape itL(aLE); itL.More(); itL.Next())
1021 {
1022 edge = TopoDS::Edge(itL.Value());
1023 if (!aUsedEdges.Contains(edge))
1024 {
38c2acd4 1025 TopoDS_Vertex V2[2];
1026 TopExp::Vertices(edge, V2[0], V2[1], Standard_True);
1027 // the neighboring edge must have V[j] reversed and located on the opposite end
1028 if (V2[1 - j].IsEqual(V[j].Reversed()))
1029 {
1030 if (j == 0)
1031 aChain.Prepend(edge);
1032 else
1033 aChain.Append(edge);
1034 aUsedEdges.Add(edge);
1035 V[j] = V2[j];
1036 isAdded = Standard_True;
1037 break;
1038 }
1039 }
1040 }
f7d70540 1041 }
1042 }
f7d70540 1043
f98965d2 1044 if (aChain.Length() < 2)
1045 continue;
2277323d 1046
38c2acd4 1047 Standard_Boolean IsClosed = Standard_False;
f98965d2 1048 if (V[0].IsSame ( V[1] ))
38c2acd4 1049 IsClosed = Standard_True;
f98965d2 1050
1051 // split chain by vertices at which merging is not possible
1052 NCollection_Sequence<SubSequenceOfEdges> aOneSeq;
1f59dfa9 1053 GenerateSubSeq(aChain, aOneSeq, IsClosed, theAngTol, theLinTol, VerticesToAvoid);
f98965d2 1054
1055 // put sub-chains in the result
1056 SeqOfSubSeqOfEdges.Append(aOneSeq);
1057 }
f7d70540 1058
1059 for (int i = 1; i <= SeqOfSubSeqOfEdges.Length(); i++)
1060 {
1061 TopoDS_Edge UE;
1062 if (SeqOfSubSeqOfEdges(i).SeqsEdges.Length() < 2)
1063 continue;
632175c3 1064 if (MergeSubSeq(SeqOfSubSeqOfEdges(i).SeqsEdges, UE, theAngTol,
1065 ConcatBSplines, isSafeInputMode, theContext))
f7d70540 1066 SeqOfSubSeqOfEdges(i).UnionEdges = UE;
1067 }
2277323d 1068 return Standard_True;
1069}
1070
cef6867c 1071//=======================================================================
1072//function : MergeSeq
f523acf0 1073//purpose : Tries to unify the sequence of edges with the set of
1074// another edges which lies on the same geometry
cef6867c 1075//=======================================================================
f98965d2 1076static Standard_Boolean MergeSeq (TopTools_SequenceOfShape& SeqEdges,
632175c3 1077 const Standard_Real theAngTol,
1f59dfa9 1078 const Standard_Real theLinTol,
f7d70540 1079 const Standard_Boolean ConcatBSplines,
632175c3 1080 const Standard_Boolean isSafeInputMode,
f7d70540 1081 Handle(ShapeBuild_ReShape)& theContext,
f523acf0 1082 const TopTools_MapOfShape& nonMergVert)
1083{
f7d70540 1084 NCollection_Sequence<SubSequenceOfEdges> SeqOfSubsSeqOfEdges;
1f59dfa9 1085 if (MergeEdges(SeqEdges, theAngTol, theLinTol, ConcatBSplines, isSafeInputMode,
632175c3 1086 theContext, SeqOfSubsSeqOfEdges, nonMergVert))
f7d70540 1087 {
1088 for (Standard_Integer i = 1; i <= SeqOfSubsSeqOfEdges.Length(); i++ )
1089 {
1090 if (SeqOfSubsSeqOfEdges(i).UnionEdges.IsNull())
1091 continue;
654c48b2 1092
1093 theContext->Merge(SeqOfSubsSeqOfEdges(i).SeqsEdges,
1094 SeqOfSubsSeqOfEdges(i).UnionEdges);
f7d70540 1095 }
1096 return Standard_True;
1097 }
f523acf0 1098 return Standard_False;
f7d70540 1099}
1100
cef6867c 1101//=======================================================================
1102//function : CheckSharedVertices
1103//purpose : Checks the sequence of edges on the presence of shared vertex
1104//=======================================================================
1105
f7d70540 1106static void CheckSharedVertices(const TopTools_SequenceOfShape& theSeqEdges,
1107 const TopTools_IndexedDataMapOfShapeListOfShape& theMapEdgesVertex,
9ed6494b 1108 const TopTools_MapOfShape& theMapKeepShape,
f7d70540 1109 TopTools_MapOfShape& theShareVertMap)
1110{
1111 ShapeAnalysis_Edge sae;
1112 TopTools_SequenceOfShape SeqVertexes;
1113 TopTools_MapOfShape MapVertexes;
1114 for (Standard_Integer k = 1; k <= theSeqEdges.Length(); k++ )
1115 {
1116 TopoDS_Vertex aV1 = sae.FirstVertex(TopoDS::Edge(theSeqEdges(k)));
1117 TopoDS_Vertex aV2 = sae.LastVertex(TopoDS::Edge(theSeqEdges(k)));
1118 if (!MapVertexes.Add(aV1))
1119 SeqVertexes.Append(aV1);
1120 if (!MapVertexes.Add(aV2))
1121 SeqVertexes.Append(aV2);
1122 }
1123
1124 for (Standard_Integer k = 1; k <= SeqVertexes.Length()/* && !IsSharedVertexPresent*/; k++ )
1125 {
1126 const TopTools_ListOfShape& ListEdgesV1 = theMapEdgesVertex.FindFromKey(SeqVertexes(k));
f1191d30 1127 if (ListEdgesV1.Extent() > 2 || theMapKeepShape.Contains(SeqVertexes(k)))
f7d70540 1128 theShareVertMap.Add(SeqVertexes(k));
1129 }
1130 //return theShareVertMap.IsEmpty() ? false : true;
1131}
1132
2277323d 1133//=======================================================================
1134//function : ShapeUpgrade_UnifySameDomain
1135//purpose : Constructor
1136//=======================================================================
1137
1138ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain()
2ba9eb30 1139 : myLinTol(Precision::Confusion()),
1140 myAngTol(Precision::Angular()),
1141 myUnifyFaces(Standard_True),
fe1a6e4e 1142 myUnifyEdges (Standard_True),
1143 myConcatBSplines (Standard_False),
632175c3 1144 myAllowInternal (Standard_False),
654c48b2 1145 mySafeInputMode(Standard_True),
1146 myHistory(new BRepTools_History)
2277323d 1147{
2277323d 1148 myContext = new ShapeBuild_ReShape;
1149}
1150
1151//=======================================================================
1152//function : ShapeUpgrade_UnifySameDomain
1153//purpose : Constructor
1154//=======================================================================
1155
1156ShapeUpgrade_UnifySameDomain::ShapeUpgrade_UnifySameDomain(const TopoDS_Shape& aShape,
1157 const Standard_Boolean UnifyEdges,
1158 const Standard_Boolean UnifyFaces,
1159 const Standard_Boolean ConcatBSplines)
fe1a6e4e 1160 : myInitShape (aShape),
2ba9eb30 1161 myLinTol(Precision::Confusion()),
1162 myAngTol(Precision::Angular()),
1163 myUnifyFaces(UnifyFaces),
fe1a6e4e 1164 myUnifyEdges (UnifyEdges),
1165 myConcatBSplines (ConcatBSplines),
1166 myAllowInternal (Standard_False),
632175c3 1167 mySafeInputMode (Standard_True),
654c48b2 1168 myShape (aShape),
1169 myHistory(new BRepTools_History)
2277323d 1170{
2277323d 1171 myContext = new ShapeBuild_ReShape;
1172}
1173
1174//=======================================================================
1175//function : Initialize
1176//purpose :
1177//=======================================================================
1178
1179void ShapeUpgrade_UnifySameDomain::Initialize(const TopoDS_Shape& aShape,
1180 const Standard_Boolean UnifyEdges,
1181 const Standard_Boolean UnifyFaces,
1182 const Standard_Boolean ConcatBSplines)
1183{
1184 myInitShape = aShape;
1185 myShape = aShape;
1186 myUnifyEdges = UnifyEdges;
1187 myUnifyFaces = UnifyFaces;
1188 myConcatBSplines = ConcatBSplines;
1189
1190 myContext->Clear();
9ed6494b 1191 myKeepShapes.Clear();
f523acf0 1192 myHistory->Clear();
fe1a6e4e 1193}
1194
1195//=======================================================================
1196//function : AllowInternalEdges
1197//purpose :
1198//=======================================================================
1199
1200void ShapeUpgrade_UnifySameDomain::AllowInternalEdges (const Standard_Boolean theValue)
1201{
1202 myAllowInternal = theValue;
2277323d 1203}
1204
632175c3 1205//=======================================================================
1206//function : SetSafeInputMode
1207//purpose :
1208//=======================================================================
1209
1210void ShapeUpgrade_UnifySameDomain::SetSafeInputMode(Standard_Boolean theValue)
1211{
1212 mySafeInputMode = theValue;
1213}
1214
9ed6494b 1215//=======================================================================
1216//function : KeepShape
1217//purpose :
1218//=======================================================================
1219
1220void ShapeUpgrade_UnifySameDomain::KeepShape(const TopoDS_Shape& theShape)
1221{
1222 if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_VERTEX)
1223 myKeepShapes.Add(theShape);
1224}
1225
1226//=======================================================================
1227//function : KeepShapes
1228//purpose :
1229//=======================================================================
1230
1231void ShapeUpgrade_UnifySameDomain::KeepShapes(const TopTools_MapOfShape& theShapes)
1232{
1233 for (TopTools_MapIteratorOfMapOfShape it(theShapes); it.More(); it.Next()) {
1234 if (it.Value().ShapeType() == TopAbs_EDGE || it.Value().ShapeType() == TopAbs_VERTEX)
1235 myKeepShapes.Add(it.Value());
1236 }
1237}
1238
2277323d 1239//=======================================================================
1240//function : UnifyFaces
1241//purpose :
1242//=======================================================================
1243
1244void ShapeUpgrade_UnifySameDomain::UnifyFaces()
1245{
fe1a6e4e 1246 // creating map of edge faces for the whole shape
1247 TopTools_IndexedDataMapOfShapeListOfShape aGMapEdgeFaces;
1248 TopExp::MapShapesAndAncestors(myShape, TopAbs_EDGE, TopAbs_FACE, aGMapEdgeFaces);
56091b56 1249
1250 // unify faces in each shell separately
2277323d 1251 TopExp_Explorer exps;
56091b56 1252 for (exps.Init(myShape, TopAbs_SHELL); exps.More(); exps.Next())
977ad983 1253 IntUnifyFaces(exps.Current(), aGMapEdgeFaces);
56091b56 1254
1255 // gather all faces out of shells in one compound and unify them at once
1256 BRep_Builder aBB;
1257 TopoDS_Compound aCmp;
1258 aBB.MakeCompound(aCmp);
1259 Standard_Integer nbf = 0;
1260 for (exps.Init(myShape, TopAbs_FACE, TopAbs_SHELL); exps.More(); exps.Next(), nbf++)
1261 aBB.Add(aCmp, exps.Current());
1262
1263 if (nbf > 0)
977ad983 1264 IntUnifyFaces(aCmp, aGMapEdgeFaces);
56091b56 1265
1266 myShape = myContext->Apply(myShape);
1267}
2277323d 1268
f16a6cc5 1269//=======================================================================
1270//function : SetFixWireModes
1271//purpose :
1272//=======================================================================
1273
1274static void SetFixWireModes(ShapeFix_Face& theSff)
1275{
1276 Handle(ShapeFix_Wire) aFixWire = theSff.FixWireTool();
1277 aFixWire->FixSelfIntersectionMode() = 0;
1278 aFixWire->FixNonAdjacentIntersectingEdgesMode() = 0;
1279 aFixWire->FixLackingMode() = 0;
1280 aFixWire->FixNotchedEdgesMode() = 0;
1281 aFixWire->ModifyTopologyMode() = Standard_False;
1282 aFixWire->ModifyRemoveLoopMode() = 0;
1283 aFixWire->FixGapsByRangesMode() = Standard_False;
1284 aFixWire->FixSmallMode() = 0;
1285}
1286
632175c3 1287//=======================================================================
1288//function : IntUnifyFaces
1289//purpose :
1290//=======================================================================
2277323d 1291
56091b56 1292void ShapeUpgrade_UnifySameDomain::IntUnifyFaces(const TopoDS_Shape& theInpShape,
977ad983 1293 TopTools_IndexedDataMapOfShapeListOfShape& theGMapEdgeFaces)
56091b56 1294{
1295 // creating map of edge faces for the shape
1296 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
1297 TopExp::MapShapesAndAncestors(theInpShape, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
1298
1299 // map of processed shapes
1300 TopTools_MapOfShape aProcessed;
1301
56091b56 1302 // processing each face
f523acf0 1303 TopExp_Explorer exp;
56091b56 1304 for (exp.Init(theInpShape, TopAbs_FACE); exp.More(); exp.Next()) {
10ce3246 1305 const TopoDS_Face& aFaceOriginal = TopoDS::Face(exp.Current());
1306 TopoDS_Face aFace = TopoDS::Face(aFaceOriginal.Oriented(TopAbs_FORWARD));
2277323d 1307
56091b56 1308 if (aProcessed.Contains(aFace))
1309 continue;
2277323d 1310
f523acf0 1311 // Boundary edges for the new face
56091b56 1312 TopTools_SequenceOfShape edges;
2277323d 1313
f523acf0 1314 Standard_Integer dummy;
1315 AddOrdinaryEdges(edges, aFace, dummy);
1316
1317 // Faces to get unified with the current faces
56091b56 1318 TopTools_SequenceOfShape faces;
f523acf0 1319
1320 // Add the current face for unification
56091b56 1321 faces.Append(aFace);
1322
f523acf0 1323 // surface and location to construct result
56091b56 1324 TopLoc_Location aBaseLocation;
1325 Handle(Geom_Surface) aBaseSurface = BRep_Tool::Surface(aFace,aBaseLocation);
1326 aBaseSurface = ClearRts(aBaseSurface);
1327
1328 // find adjacent faces to union
1329 Standard_Integer i;
1330 for (i = 1; i <= edges.Length(); i++) {
1331 TopoDS_Edge edge = TopoDS::Edge(edges(i));
1332 if (BRep_Tool::Degenerated(edge))
1333 continue;
2277323d 1334
56091b56 1335 // get connectivity of the edge in the global shape
1336 const TopTools_ListOfShape& aGList = theGMapEdgeFaces.FindFromKey(edge);
9ed6494b 1337 if (!myAllowInternal && (aGList.Extent() != 2 || myKeepShapes.Contains(edge))) {
f523acf0 1338 // non manifold case is not processed unless myAllowInternal
56091b56 1339 continue;
1340 }
10ce3246 1341 //
f523acf0 1342 // Get the faces connected through the edge in the current shape
1343 const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
1344 if (aList.Extent() < 2) {
1345 continue;
1346 }
1347
f16a6cc5 1348 // for a planar face create and store pcurve of edge on face
1349 // to speed up all operations
1350 if (!mySafeInputMode && aBaseSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
1351 BRepLib::BuildPCurveForEdgeOnPlane(edge, aFace);
1352
10ce3246 1353 // get normal of the face to compare it with normals of other faces
1354 gp_Dir aDN1;
1355 //
1356 // take intermediate point on edge to compute the normal
1357 Standard_Real f, l;
1358 BRep_Tool::Range(edge, f, l);
1359 Standard_Real aTMid = (f + l) * .5;
1360 //
1361 Standard_Boolean bCheckNormals = GetNormalToSurface(aFaceOriginal, edge, aTMid, aDN1);
1362 //
f523acf0 1363 // Process the faces
56091b56 1364 TopTools_ListIteratorOfListOfShape anIter(aList);
1365 for (; anIter.More(); anIter.Next()) {
10ce3246 1366 const TopoDS_Face& aCheckedFaceOriginal = TopoDS::Face(anIter.Value());
1367 TopoDS_Face anCheckedFace = TopoDS::Face(aCheckedFaceOriginal.Oriented(TopAbs_FORWARD));
56091b56 1368 if (anCheckedFace.IsSame(aFace))
fe1a6e4e 1369 continue;
2277323d 1370
56091b56 1371 if (aProcessed.Contains(anCheckedFace))
1372 continue;
2277323d 1373
10ce3246 1374 if (bCheckNormals) {
1375 // get normal of checked face using the same parameter on edge
1376 gp_Dir aDN2;
1377 if (GetNormalToSurface(aCheckedFaceOriginal, edge, aTMid, aDN2)) {
1378 // and check if the adjacent faces are having approximately same normals
1379 Standard_Real anAngle = aDN1.Angle(aDN2);
1380 if (anAngle > myAngTol) {
1381 continue;
1382 }
1383 }
1384 }
1385 //
2ba9eb30 1386 if (IsSameDomain(aFace,anCheckedFace, myLinTol, myAngTol)) {
f0144633 1387
56091b56 1388 // hotfix for 27271: prevent merging along periodic direction.
d3dadd23 1389 if (IsLikeSeam(edge, anCheckedFace, aBaseSurface))
56091b56 1390 continue;
2277323d 1391
f523acf0 1392 if (AddOrdinaryEdges(edges,anCheckedFace,dummy)) {
56091b56 1393 // sequence edges is modified
1394 i = dummy;
2277323d 1395 }
56091b56 1396
1397 faces.Append(anCheckedFace);
1398 aProcessed.Add(anCheckedFace);
1399 break;
2277323d 1400 }
1401 }
56091b56 1402 }
2277323d 1403
56091b56 1404 if (faces.Length() > 1) {
1405 // fill in the connectivity map for selected faces
1406 TopTools_IndexedDataMapOfShapeListOfShape aMapEF;
1407 for (i = 1; i <= faces.Length(); i++) {
1408 TopExp::MapShapesAndAncestors(faces(i), TopAbs_EDGE, TopAbs_FACE, aMapEF);
1409 }
f523acf0 1410 // Collect keep edges and multi-connected edges, i.e. edges that are internal to
56091b56 1411 // the set of selected faces and have connections to other faces.
9ed6494b 1412 TopTools_ListOfShape aKeepEdges;
56091b56 1413 for (i = 1; i <= aMapEF.Extent(); i++) {
1414 const TopTools_ListOfShape& aLF = aMapEF(i);
1415 if (aLF.Extent() == 2) {
1416 const TopoDS_Shape& aE = aMapEF.FindKey(i);
1417 const TopTools_ListOfShape& aGLF = theGMapEdgeFaces.FindFromKey(aE);
9ed6494b 1418 if (aGLF.Extent() > 2 || myKeepShapes.Contains(aE)) {
1419 aKeepEdges.Append(aE);
fe1a6e4e 1420 }
1421 }
9ed6494b 1422 }
1423 if (!aKeepEdges.IsEmpty()) {
1424 if (!myAllowInternal) {
1425 // Remove from the selection the faces which have no other connect edges
f523acf0 1426 // and contain multi-connected edges and/or keep edges.
56091b56 1427 TopTools_MapOfShape anAvoidFaces;
9ed6494b 1428 TopTools_ListIteratorOfListOfShape it(aKeepEdges);
56091b56 1429 for (; it.More(); it.Next()) {
1430 const TopoDS_Shape& aE = it.Value();
1431 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1432 anAvoidFaces.Add(aLF.First());
1433 anAvoidFaces.Add(aLF.Last());
1434 }
9ed6494b 1435 for (i = 1; i <= faces.Length(); i++) {
56091b56 1436 if (anAvoidFaces.Contains(faces(i))) {
1437 // update the boundaries of merged area, for that
1438 // remove from 'edges' the edges of this face and add to 'edges'
1439 // the edges of this face that were not present in 'edges' before
9ed6494b 1440 Standard_Boolean hasConnectAnotherFaces = Standard_False;
56091b56 1441 TopExp_Explorer ex(faces(i), TopAbs_EDGE);
9ed6494b 1442 for (; ex.More() && !hasConnectAnotherFaces; ex.Next()) {
56091b56 1443 TopoDS_Shape aE = ex.Current();
9ed6494b 1444 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1445 if (aLF.Extent() > 1) {
1446 for (it.Init(aLF); it.More() && !hasConnectAnotherFaces; it.Next()) {
1447 if (!anAvoidFaces.Contains(it.Value()))
1448 hasConnectAnotherFaces = Standard_True;
1449 }
1450 }
1451 }
1452 if (!hasConnectAnotherFaces) {
1453 AddOrdinaryEdges(edges, faces(i), dummy);
1454 faces.Remove(i);
1455 i--;
1456 }
1457 }
1458 }
1459 // check if the faces with keep edges contained in
1460 // already updated the boundaries of merged area
1461 if (!faces.IsEmpty()) {
1462 TopTools_MapOfShape aMapFaces;
1463 for (i = 1; i <= faces.Length(); i++) {
1464 aMapFaces.Add(faces(i));
1465 }
1466 for (it.Init(aKeepEdges); it.More(); it.Next()) {
1467 const TopoDS_Shape& aE = it.Value();
1468 const TopTools_ListOfShape& aLF = aMapEF.FindFromKey(aE);
1469 if (aLF.Extent() < 2)
1470 continue;
1471 if (aMapFaces.Contains(aLF.First()) &&
1472 aMapFaces.Contains(aLF.Last())) {
1473 for (i = 1; i <= faces.Length(); i++) {
1474 if (faces(i).IsEqual(aLF.First()) ||
1475 faces(i).IsEqual(aLF.Last())) {
1476 AddOrdinaryEdges(edges, faces(i), dummy);
1477 faces.Remove(i);
1478 i--;
1479 }
cb120537 1480 }
cb120537 1481 }
fe1a6e4e 1482 }
1483 }
56091b56 1484 }
1485 else {
f523acf0 1486 // add multi-connected and keep edges as internal in new face
9ed6494b 1487 TopTools_ListIteratorOfListOfShape it(aKeepEdges);
56091b56 1488 for (; it.More(); it.Next()) {
1489 const TopoDS_Shape& aE = it.Value();
1490 edges.Append(aE.Oriented(TopAbs_INTERNAL));
fe1a6e4e 1491 }
1492 }
1493 }
56091b56 1494 }
fe1a6e4e 1495
56091b56 1496 // all faces collected in the sequence. Perform union of faces
1497 if (faces.Length() > 1) {
56091b56 1498 TopoDS_Face aResult;
1499 BRep_Builder B;
1500 B.MakeFace(aResult,aBaseSurface,aBaseLocation,0);
1501 Standard_Integer nbWires = 0;
1502
f16a6cc5 1503 TopoDS_Face tmpF = TopoDS::Face(faces(1).Oriented(TopAbs_FORWARD));
20aa0d3f 1504
56091b56 1505 // connecting wires
1506 while (edges.Length()>0) {
1507
1508 Standard_Boolean isEdge3d = Standard_False;
1509 nbWires++;
1510 TopTools_MapOfShape aVertices;
1511 TopoDS_Wire aWire;
1512 B.MakeWire(aWire);
1513
1514 TopoDS_Edge anEdge = TopoDS::Edge(edges(1));
1515 edges.Remove(1);
1516 // collect internal edges in separate wires
1517 Standard_Boolean isInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1518
1519 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1520 B.Add(aWire,anEdge);
1521 TopoDS_Vertex V1,V2;
1522 TopExp::Vertices(anEdge,V1,V2);
1523 aVertices.Add(V1);
1524 aVertices.Add(V2);
1525
1526 Standard_Boolean isNewFound = Standard_False;
1527 do {
1528 isNewFound = Standard_False;
1529 for(Standard_Integer j = 1; j <= edges.Length(); j++) {
1530 anEdge = TopoDS::Edge(edges(j));
1531 // check if the current edge orientation corresponds to the first one
1532 Standard_Boolean isCurrInternal = (anEdge.Orientation() == TopAbs_INTERNAL);
1533 if (isCurrInternal != isInternal)
1534 continue;
1535 TopExp::Vertices(anEdge,V1,V2);
1536 if(aVertices.Contains(V1) || aVertices.Contains(V2)) {
1537 isEdge3d |= !BRep_Tool::Degenerated(anEdge);
1538 aVertices.Add(V1);
1539 aVertices.Add(V2);
1540 B.Add(aWire,anEdge);
1541 edges.Remove(j);
1542 j--;
1543 isNewFound = Standard_True;
2277323d 1544 }
2277323d 1545 }
56091b56 1546 } while (isNewFound);
1547
1548 // sorting any type of edges
1549 aWire.Closed (BRep_Tool::IsClosed (aWire));
56091b56 1550
1551 Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire(aWire,tmpF,Precision::Confusion());
632175c3 1552 if (mySafeInputMode)
1553 sfw->SetContext(myContext);
56091b56 1554 sfw->FixReorder();
1555 Standard_Boolean isDegRemoved = Standard_False;
1556 if(!sfw->StatusReorder ( ShapeExtend_FAIL )) {
1557 // clear degenerated edges if at least one with 3d curve exist
2277323d 1558 if(isEdge3d) {
56091b56 1559 Handle(ShapeExtend_WireData) sewd = sfw->WireData();
1560 for(Standard_Integer j = 1; j<=sewd->NbEdges();j++) {
1561 TopoDS_Edge E = sewd->Edge(j);
1562 if(BRep_Tool::Degenerated(E)) {
1563 sewd->Remove(j);
1564 isDegRemoved = Standard_True;
1565 j--;
2277323d 1566 }
2277323d 1567 }
2277323d 1568 }
56091b56 1569 sfw->FixShifted();
1570 if(isDegRemoved)
1571 sfw->FixDegenerated();
2277323d 1572 }
f16a6cc5 1573 aWire = sfw->Wire();
2277323d 1574
56091b56 1575 // add resulting wire
1576 if(isEdge3d) {
f16a6cc5 1577 B.Add(aResult,aWire);
56091b56 1578 }
1579 else {
1580 // sorting edges
1581 Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
1582 Standard_Integer nbEdges = sbwd->NbEdges();
1583 // sort degenerated edges and create one edge instead of several ones
1584 ShapeAnalysis_WireOrder sawo(Standard_False, 0);
1585 ShapeAnalysis_Edge sae;
1586 Standard_Integer aLastEdge = nbEdges;
1587 for(Standard_Integer j = 1; j <= nbEdges; j++) {
1588 Standard_Real f,l;
1589 //smh protection on NULL pcurve
1590 Handle(Geom2d_Curve) c2d;
1591 if(!sae.PCurve(sbwd->Edge(j),tmpF,c2d,f,l)) {
1592 aLastEdge--;
1593 continue;
38c2acd4 1594 }
56091b56 1595 sawo.Add(c2d->Value(f).XY(),c2d->Value(l).XY());
2277323d 1596 }
d3dadd23 1597 if (sawo.NbEdges() == 0)
1598 continue;
56091b56 1599 sawo.Perform();
1600
1601 // constructind one degenerative edge
1602 gp_XY aStart, anEnd, tmp;
1603 Standard_Integer nbFirst = sawo.Ordered(1);
1604 TopoDS_Edge anOrigE = TopoDS::Edge(sbwd->Edge(nbFirst).Oriented(TopAbs_FORWARD));
1605 ShapeBuild_Edge sbe;
1606 TopoDS_Vertex aDummyV;
1607 TopoDS_Edge E = sbe.CopyReplaceVertices(anOrigE,aDummyV,aDummyV);
1608 sawo.XY(nbFirst,aStart,tmp);
1609 sawo.XY(sawo.Ordered(aLastEdge),tmp,anEnd);
1610
1611 gp_XY aVec = anEnd-aStart;
1612 Handle(Geom2d_Line) aLine = new Geom2d_Line(aStart,gp_Dir2d(anEnd-aStart));
1613
1614 B.UpdateEdge(E,aLine,tmpF,0.);
1615 B.Range(E,tmpF,0.,aVec.Modulus());
1616 Handle(Geom_Curve) C3d;
1617 B.UpdateEdge(E,C3d,0.);
1618 B.Degenerated(E,Standard_True);
1619 TopoDS_Wire aW;
1620 B.MakeWire(aW);
1621 B.Add(aW,E);
1622 aW.Closed (Standard_True);
1623 B.Add(aResult,aW);
1624 }
1625 }
2277323d 1626
56091b56 1627 ShapeFix_Face sff (aResult);
f523acf0 1628 //Initializing by tolerances
56091b56 1629 sff.SetPrecision(Precision::Confusion());
f523acf0 1630 sff.SetMinTolerance(Precision::Confusion());
56091b56 1631 sff.SetMaxTolerance(1.);
1632 //Setting modes
f16a6cc5 1633 SetFixWireModes(sff);
1634 if (mySafeInputMode)
1635 sff.SetContext(myContext);
56091b56 1636 // Applying the fixes
1637 sff.Perform();
f16a6cc5 1638 if(!sff.Status(ShapeExtend_FAIL))
1639 {
1640 // perform substitution of faces
1641 aResult = sff.Face();
1642 myContext->Merge(faces, aResult);
2277323d 1643 }
56091b56 1644 }
1645 } // end processing each face
2277323d 1646}
1647
1648//=======================================================================
1649//function : UnifyEdges
1650//purpose :
1651//=======================================================================
2277323d 1652void ShapeUpgrade_UnifySameDomain::UnifyEdges()
1653{
632175c3 1654 TopoDS_Shape aRes = myContext->Apply(myShape);
ce41efde 1655 // creating map of edge faces
1656 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
632175c3 1657 TopExp::MapShapesAndAncestors(aRes, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
ce41efde 1658 // creating map of vertex edges
1659 TopTools_IndexedDataMapOfShapeListOfShape aMapEdgesVertex;
632175c3 1660 TopExp::MapShapesAndUniqueAncestors(aRes, TopAbs_VERTEX, TopAbs_EDGE, aMapEdgesVertex);
ce41efde 1661
632175c3 1662 if (mySafeInputMode)
1663 UpdateMapOfShapes(myKeepShapes, myContext);
1664
12d71ad6 1665 // Sequence of the edges of the shape
1666 TopTools_SequenceOfShape aSeqEdges;
1667 const Standard_Integer aNbE = aMapEdgeFaces.Extent();
1668 for (Standard_Integer i = 1; i <= aNbE; ++i)
1669 aSeqEdges.Append(aMapEdgeFaces.FindKey(i));
1670
1671 // Prepare map of shared vertices (with the number of connected edges greater then 2)
1672 TopTools_MapOfShape aSharedVert;
1673 CheckSharedVertices(aSeqEdges, aMapEdgesVertex, myKeepShapes, aSharedVert);
1674 // Merge the edges avoiding removal of the shared vertices
1675 Standard_Boolean isMerged = MergeSeq(aSeqEdges, myAngTol, myLinTol, myConcatBSplines,
1676 mySafeInputMode, myContext, aSharedVert);
1677 // Collect faces to rebuild
1678 TopTools_IndexedMapOfShape aChangedFaces;
1679 if (isMerged)
f7d70540 1680 {
12d71ad6 1681 for (Standard_Integer i = 1; i <= aNbE; ++i)
f7d70540 1682 {
12d71ad6 1683 const TopoDS_Shape& aE = aMapEdgeFaces.FindKey(i);
1684 if (myContext->IsRecorded(aE))
2277323d 1685 {
12d71ad6 1686 TopTools_ListIteratorOfListOfShape it(aMapEdgeFaces(i));
1687 for (; it.More(); it.Next())
1688 aChangedFaces.Add(it.Value());
2277323d 1689 }
ce41efde 1690 }
12d71ad6 1691 }
ce41efde 1692
1693 // fix changed faces and replace them in the local context
632175c3 1694 Standard_Real aPrec = Precision::Confusion();
12d71ad6 1695 for (Standard_Integer i = 1; i <= aChangedFaces.Extent(); i++) {
1696 TopoDS_Face aFace = TopoDS::Face(myContext->Apply(aChangedFaces.FindKey(i)));
f7d70540 1697 if (aFace.IsNull())
1698 continue;
f16a6cc5 1699
1700 // for a planar face create and store pcurve of edge on face
1701 // to speed up all operations; but this is allowed only when non-safe mode in force
1702 if (!mySafeInputMode)
1703 {
1704 TopLoc_Location aLoc;
1705 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace, aLoc);
1706 aSurface = ClearRts(aSurface);
1707 if (aSurface->IsKind(STANDARD_TYPE(Geom_Plane)))
1708 {
1709 TopTools_ListOfShape aLE;
1710 for (TopExp_Explorer anEx(aFace, TopAbs_EDGE); anEx.More(); anEx.Next())
1711 aLE.Append(anEx.Current());
1712 BRepLib::BuildPCurveForEdgesOnPlane(aLE, aFace);
1713 }
1714 }
1715
1716 ShapeFix_Face sff(aFace);
1717 if (mySafeInputMode)
1718 sff.SetContext(myContext);
1719 sff.SetPrecision(aPrec);
1720 sff.SetMinTolerance(aPrec);
1721 sff.SetMaxTolerance(Max(1., aPrec*1000.));
1722 sff.FixOrientationMode() = 0;
1723 sff.FixAddNaturalBoundMode() = 0;
1724 sff.FixIntersectingWiresMode() = 0;
1725 sff.FixLoopWiresMode() = 0;
1726 sff.FixSplitFaceMode() = 0;
1727 sff.FixPeriodicDegeneratedMode() = 0;
1728 SetFixWireModes(sff);
1729 sff.Perform();
1730 TopoDS_Shape aNewFace = sff.Face();
ce41efde 1731 myContext->Replace(aFace,aNewFace);
ce41efde 1732 }
1733
12d71ad6 1734 if (aChangedFaces.Extent() > 0) {
ce41efde 1735 // fix changed shell and replace it in the local context
ce41efde 1736 TopoDS_Shape aRes1 = myContext->Apply(aRes);
632175c3 1737 Standard_Boolean isChanged = Standard_False;
ce41efde 1738 TopExp_Explorer expsh;
1739 for (expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
1740 TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
1741 Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
1742 sfsh->FixFaceOrientation(aShell);
1743 TopoDS_Shape aNewShell = sfsh->Shell();
632175c3 1744 if (!aNewShell.IsSame(aShell)) {
1745 myContext->Replace(aShell, aNewShell);
1746 isChanged = Standard_True;
2277323d 1747 }
ce41efde 1748 }
632175c3 1749 if (isChanged)
1750 aRes1 = myContext->Apply(aRes1);
1751 myContext->Replace(myShape, aRes1);
ce41efde 1752 }
2277323d 1753
1754 myShape = myContext->Apply(myShape);
1755}
1756
2277323d 1757//=======================================================================
1758//function : Build
1759//purpose : builds the resulting shape
f523acf0 1760//=======================================================================
2277323d 1761void ShapeUpgrade_UnifySameDomain::Build()
1762{
f16a6cc5 1763 if (myUnifyFaces)
2277323d 1764 UnifyFaces();
f16a6cc5 1765 if (myUnifyEdges)
1766 UnifyEdges();
2277323d 1767
f523acf0 1768 // Fill the history of modifications during the operation
1769 FillHistory();
1770}
1771
1772//=======================================================================
1773//function : FillHistory
1774//purpose : Fill the history of modifications during the operation
1775//=======================================================================
1776void ShapeUpgrade_UnifySameDomain::FillHistory()
1777{
1778 if (myHistory.IsNull())
1779 // History is not requested
1780 return;
1781
1782 // Only Vertices, Edges and Faces can be modified during unification.
1783 // Thus, only these kind of shapes should be checked.
1784
1785 // Get history from the context.
1786 // It contains all modifications of the operation. Some of these
1787 // modifications become not relevant and should be filtered.
1788 Handle(BRepTools_History) aCtxHistory = myContext->History();
1789
1790 // Explore the history of the context and fill
1791 // the history of UnifySameDomain algorithm
1792 Handle(BRepTools_History) aUSDHistory = new BRepTools_History();
1793
1794 // Map all Vertices, Edges and Faces in the input shape
1795 TopTools_IndexedMapOfShape aMapInputShape;
1796 TopExp::MapShapes(myInitShape, TopAbs_VERTEX, aMapInputShape);
1797 TopExp::MapShapes(myInitShape, TopAbs_EDGE , aMapInputShape);
1798 TopExp::MapShapes(myInitShape, TopAbs_FACE , aMapInputShape);
1799
1800 // Map all Vertices, Edges and Faces in the result shape
1801 TopTools_IndexedMapOfShape aMapResultShapes;
1802 TopExp::MapShapes(myShape, TopAbs_VERTEX, aMapResultShapes);
1803 TopExp::MapShapes(myShape, TopAbs_EDGE , aMapResultShapes);
1804 TopExp::MapShapes(myShape, TopAbs_FACE , aMapResultShapes);
1805
1806 // Iterate on all input shapes and get their modifications
1807 Standard_Integer i, aNb = aMapInputShape.Extent();
1808 for (i = 1; i <= aNb; ++i)
654c48b2 1809 {
f523acf0 1810 const TopoDS_Shape& aS = aMapInputShape(i);
1811
1812 // Check the shape itself to be present in the result
1813 if (aMapResultShapes.Contains(aS))
1814 {
1815 // The shape is present in the result as is, thus has not been modified
1816 continue;
1817 }
1818
1819 // Check if the shape has been modified during the operation
1820 const TopTools_ListOfShape& aLSImages = aCtxHistory->Modified(aS);
1821 if (aLSImages.IsEmpty())
1822 {
1823 // The shape has not been modified and not present in the result,
1824 // thus it has been removed
1825 aUSDHistory->Remove(aS);
1826 continue;
1827 }
1828
1829 // Check the images of the shape to be present in the result
1830 Standard_Boolean bRemoved = Standard_True;
1831 TopTools_ListIteratorOfListOfShape aItLSIm(aLSImages);
1832 for (; aItLSIm.More(); aItLSIm.Next())
1833 {
1834 if (aMapResultShapes.Contains(aItLSIm.Value()))
1835 {
1836 // Image is found in the result, thus the shape has been modified
1837 aUSDHistory->AddModified(aS, aItLSIm.Value());
1838 bRemoved = Standard_False;
1839 }
1840 }
1841
1842 if (bRemoved)
654c48b2 1843 {
f523acf0 1844 // No images are found in the result, thus the shape has been removed
1845 aUSDHistory->Remove(aS);
654c48b2 1846 }
20aa0d3f 1847 }
f7d70540 1848
f523acf0 1849 // Merge the history of the operation into global history
1850 myHistory->Merge(aUSDHistory);
2277323d 1851}