1 // Created on: 2016-06-23
2 // Copyright (c) 2016 OPEN CASCADE SAS
3 // Created by: Oleg AGASHIN
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <BRepMesh_ModelHealer.hxx>
17 #include <BRepMesh_Deflection.hxx>
18 #include <BRepMesh_ShapeTool.hxx>
19 #include <BRepMesh_FaceChecker.hxx>
20 #include <BRepMesh_EdgeDiscret.hxx>
21 #include <IMeshData_Face.hxx>
22 #include <IMeshData_Wire.hxx>
23 #include <IMeshData_Edge.hxx>
24 #include <IMeshData_PCurve.hxx>
25 #include <OSD_Parallel.hxx>
27 #include <TopoDS_Vertex.hxx>
30 #include <BRepBuilderAPI_MakePolygon.hxx>
31 #include <BRepTools.hxx>
32 #include <BRep_Builder.hxx>
33 #include <TopoDS_Compound.hxx>
38 //! Decreases deflection of the given edge and tries to update discretization.
43 EdgeAmplifier(const IMeshTools_Parameters& theParameters)
44 : myParameters(theParameters)
49 void operator()(const IMeshData::IEdgePtr& theDEdge) const
51 const IMeshData::IEdgeHandle aDEdge = theDEdge;
52 aDEdge->Clear(Standard_True);
53 aDEdge->SetDeflection(Max(aDEdge->GetDeflection() / 3., Precision::Confusion()));
55 const IMeshData::IPCurveHandle& aPCurve = aDEdge->GetPCurve(0);
56 const IMeshData::IFaceHandle aDFace = aPCurve->GetFace();
57 Handle(IMeshTools_CurveTessellator) aTessellator =
58 BRepMesh_EdgeDiscret::CreateEdgeTessellator(
59 aDEdge, aPCurve->GetOrientation(), aDFace, myParameters);
61 BRepMesh_EdgeDiscret::Tessellate3d(aDEdge, aTessellator, Standard_False);
62 BRepMesh_EdgeDiscret::Tessellate2d(aDEdge, Standard_False);
67 EdgeAmplifier (const EdgeAmplifier& theOther);
69 void operator=(const EdgeAmplifier& theOther);
72 const IMeshTools_Parameters& myParameters;
75 //! Returns True if some of two vertcies is same with reference one.
76 inline Standard_Boolean isSameWithSomeOf(
77 const TopoDS_Vertex& theRefVertex,
78 const TopoDS_Vertex& theVertex1,
79 const TopoDS_Vertex& theVertex2)
81 return (theRefVertex.IsSame(theVertex1) ||
82 theRefVertex.IsSame(theVertex2));
85 //! Returns True if some of two vertcies is within tolerance of reference one.
86 inline Standard_Boolean isInToleranceWithSomeOf(
87 const gp_Pnt& theRefPoint,
88 const gp_Pnt& thePoint1,
89 const gp_Pnt& thePoint2,
90 const Standard_Real theTol)
92 const Standard_Real aSqTol = theTol * theTol;
93 return (theRefPoint.SquareDistance(thePoint1) < aSqTol ||
94 theRefPoint.SquareDistance(thePoint2) < aSqTol);
98 //=======================================================================
99 // Function: Constructor
101 //=======================================================================
102 BRepMesh_ModelHealer::BRepMesh_ModelHealer()
106 //=======================================================================
107 // Function: Destructor
109 //=======================================================================
110 BRepMesh_ModelHealer::~BRepMesh_ModelHealer()
114 //=======================================================================
117 //=======================================================================
118 Standard_Boolean BRepMesh_ModelHealer::performInternal(
119 const Handle(IMeshData_Model)& theModel,
120 const IMeshTools_Parameters& theParameters)
123 myParameters = theParameters;
124 if (myModel.IsNull())
126 return Standard_False;
129 // MinSize is made as a constant. It is connected with
130 // the fact that too rude discretisation can lead to
131 // self-intersecting polygon, which cannot be fixed.
132 // As result the face will not be triangulated at all.
133 // E.g. see "Test mesh standard_mesh C7", the face #17.
134 myParameters.MinSize = Precision::Confusion();
136 myFaceIntersectingEdges = new IMeshData::DMapOfIFacePtrsMapOfIEdgePtrs;
137 for (Standard_Integer aFaceIt = 0; aFaceIt < myModel->FacesNb(); ++aFaceIt)
139 myFaceIntersectingEdges->Bind(myModel->GetFace(aFaceIt).get(), Handle(IMeshData::MapOfIEdgePtr)());
142 // TODO: Here we can process edges in order to remove close discrete points.
143 OSD_Parallel::For(0, myModel->FacesNb(), *this, !isParallel());
146 IMeshData::DMapOfIFacePtrsMapOfIEdgePtrs::Iterator aFaceIt(*myFaceIntersectingEdges);
147 for (; aFaceIt.More(); aFaceIt.Next())
149 if (!aFaceIt.Value().IsNull())
151 const IMeshData::IFaceHandle aDFace = aFaceIt.Key();
152 aDFace->SetStatus(IMeshData_SelfIntersectingWire);
153 aDFace->SetStatus(IMeshData_Failure);
157 myFaceIntersectingEdges.Nullify();
158 myModel.Nullify(); // Do not hold link to model.
159 return Standard_True;
162 //=======================================================================
163 // Function: amplifyEdges
165 //=======================================================================
166 void BRepMesh_ModelHealer::amplifyEdges()
168 Handle(NCollection_IncAllocator) aTmpAlloc =
169 new NCollection_IncAllocator(IMeshData::MEMORY_BLOCK_SIZE_HUGE);
171 Standard_Integer aAmpIt = 0;
172 const Standard_Real aIterNb = 5;
173 IMeshData::MapOfIEdgePtr aEdgesToUpdate(1, aTmpAlloc);
174 while (aAmpIt++ < aIterNb && popEdgesToUpdate(aEdgesToUpdate))
176 // Try to update discretization by decreasing deflection of problematic edges.
177 OSD_Parallel::ForEach(aEdgesToUpdate.cbegin(), aEdgesToUpdate.cend(),
178 EdgeAmplifier(myParameters),
179 !(myParameters.InParallel && aEdgesToUpdate.Size() > 1),
180 aEdgesToUpdate.Size());
182 IMeshData::MapOfIFacePtr aFacesToCheck(1, aTmpAlloc);
183 IMeshData::MapOfIEdgePtr::Iterator aEdgeIt(aEdgesToUpdate);
184 for (; aEdgeIt.More(); aEdgeIt.Next())
186 const IMeshData::IEdgeHandle aDEdge = aEdgeIt.Value();
187 for (Standard_Integer aPCurveIt = 0; aPCurveIt < aDEdge->PCurvesNb(); ++aPCurveIt)
189 aFacesToCheck.Add(aDEdge->GetPCurve(aPCurveIt)->GetFace());
193 OSD_Parallel::ForEach(aFacesToCheck.cbegin(), aFacesToCheck.cend(),
194 *this, !(myParameters.InParallel && aFacesToCheck.Size() > 1),
195 aFacesToCheck.Size());
197 aEdgesToUpdate.Clear();
198 aTmpAlloc->Reset(Standard_False);
202 //=======================================================================
203 // Function: popEdgesToUpdate
205 //=======================================================================
206 Standard_Boolean BRepMesh_ModelHealer::popEdgesToUpdate(
207 IMeshData::MapOfIEdgePtr& theEdgesToUpdate)
209 IMeshData::DMapOfIFacePtrsMapOfIEdgePtrs::Iterator aFaceIt(*myFaceIntersectingEdges);
210 for (; aFaceIt.More(); aFaceIt.Next())
212 Handle(IMeshData::MapOfIEdgePtr)& aIntersections = aFaceIt.ChangeValue();
213 if (!aIntersections.IsNull())
215 theEdgesToUpdate.Unite(*aIntersections);
216 aIntersections.Nullify();
220 return !theEdgesToUpdate.IsEmpty();
223 //=======================================================================
226 //=======================================================================
227 void BRepMesh_ModelHealer::process(const IMeshData::IFaceHandle& theDFace) const
233 Handle(IMeshData::MapOfIEdgePtr)& aIntersections = myFaceIntersectingEdges->ChangeFind(theDFace.get());
234 aIntersections.Nullify();
236 fixFaceBoundaries(theDFace);
238 if (!theDFace->IsSet(IMeshData_Failure))
240 BRepMesh_FaceChecker aChecker(theDFace, myParameters);
241 if (!aChecker.Perform())
244 std::cout << "Failed : #" << aChecker.GetIntersectingEdges()->Size() << std::endl;
246 aIntersections = aChecker.GetIntersectingEdges();
250 if (theDFace->WiresNb () == 1)
252 const IMeshData::IWireHandle& aDWire = theDFace->GetWire (0);
254 if (aDWire->EdgesNb () == 2)
256 const IMeshData::IEdgePtr& aDEdge0 = aDWire->GetEdge (0);
257 const IMeshData::IEdgePtr& aDEdge1 = aDWire->GetEdge (1);
259 const IMeshData::IPCurveHandle& aPCurve0 = aDEdge0->GetPCurve (theDFace.get (), aDWire->GetEdgeOrientation (0));
260 const IMeshData::IPCurveHandle& aPCurve1 = aDEdge1->GetPCurve (theDFace.get (), aDWire->GetEdgeOrientation (1));
262 if (aPCurve0->ParametersNb () == 2 && aPCurve1->ParametersNb () == 2)
264 aIntersections = new IMeshData::MapOfIEdgePtr;
265 // a kind of degenerated face - 1 wire, 2 edges and both edges are very small
266 aIntersections->Add (aDEdge0);
267 aIntersections->Add (aDEdge1);
274 catch (Standard_Failure const&)
276 theDFace->SetStatus (IMeshData_Failure);
280 //=======================================================================
281 // Function: fixFaceBoundaries
283 //=======================================================================
284 void BRepMesh_ModelHealer::fixFaceBoundaries(const IMeshData::IFaceHandle& theDFace) const
287 TopoDS_Compound aComp;
288 BRep_Builder aBuilder;
289 aBuilder.MakeCompound(aComp);
292 for (int aWireIt = 0; aWireIt < theDFace->WiresNb(); ++aWireIt)
294 const IMeshData::IWireHandle& aDWire = theDFace->GetWire(aWireIt);
295 BRepMesh_Deflection::ComputeDeflection(aDWire, myParameters);
296 for (int aEdgeIt = 0; aEdgeIt < aDWire->EdgesNb(); ++aEdgeIt)
298 const int aPrevEdgeIt = (aEdgeIt + aDWire->EdgesNb() - 1) % aDWire->EdgesNb();
299 const int aNextEdgeIt = (aEdgeIt + 1) % aDWire->EdgesNb();
301 const IMeshData::IEdgeHandle aPrevEdge = aDWire->GetEdge(aPrevEdgeIt);
302 const IMeshData::IEdgeHandle aCurrEdge = aDWire->GetEdge(aEdgeIt);
303 const IMeshData::IEdgeHandle aNextEdge = aDWire->GetEdge(aNextEdgeIt);
305 Standard_Boolean isConnected = !getCommonVertex(aCurrEdge, aNextEdge).IsNull() &&
306 !getCommonVertex(aPrevEdge, aCurrEdge).IsNull();
310 const IMeshData::IPCurveHandle& aPrevPCurve =
311 aPrevEdge->GetPCurve(theDFace.get(), aDWire->GetEdgeOrientation(aPrevEdgeIt));
313 const IMeshData::IPCurveHandle& aCurrPCurve =
314 aCurrEdge->GetPCurve(theDFace.get(), aDWire->GetEdgeOrientation(aEdgeIt));
316 const IMeshData::IPCurveHandle& aNextPCurve =
317 aNextEdge->GetPCurve(theDFace.get(), aDWire->GetEdgeOrientation(aNextEdgeIt));
319 isConnected = connectClosestPoints(aPrevPCurve, aCurrPCurve, aNextPCurve);
322 BRepBuilderAPI_MakePolygon aPoly;
323 for (int i = 0; i < aCurrPCurve->ParametersNb(); ++i)
325 const gp_Pnt2d& aPnt = aCurrPCurve->GetPoint(i);
326 aPoly.Add(gp_Pnt(aPnt.X(), aPnt.Y(), 0.));
331 aBuilder.Add(aComp, aPoly.Shape());
333 TCollection_AsciiString aName("face_discr.brep");
334 BRepTools::Write(aComp, aName.ToCString());
338 if (!isConnected || aCurrEdge->IsSet(IMeshData_Outdated))
340 // We have to clean face from triangulation.
341 theDFace->SetStatus(IMeshData_Outdated);
345 // Just mark wire as open, but continue fixing other inconsistencies
346 // in hope that this data could be suitable to build mesh somehow.
347 aDWire->SetStatus(IMeshData_OpenWire);
354 TCollection_AsciiString aName ("face_discr.brep");
355 TCollection_AsciiString aFaceName("face_geom.brep");
356 BRepTools::Write(aComp, aName.ToCString());
357 BRepTools::Write(theDFace->GetFace(), aFaceName.ToCString());
360 BRepMesh_Deflection::ComputeDeflection(theDFace, myParameters);
363 //=======================================================================
364 // Function: hasCommonVertex
366 //=======================================================================
367 TopoDS_Vertex BRepMesh_ModelHealer::getCommonVertex(
368 const IMeshData::IEdgeHandle& theEdge1,
369 const IMeshData::IEdgeHandle& theEdge2) const
371 TopoDS_Vertex aVertex1_1, aVertex1_2;
372 TopExp::Vertices(theEdge1->GetEdge(), aVertex1_1, aVertex1_2);
374 //Test bugs moddata_2 bug428.
375 // restore [locate_data_file OCC428.brep] rr
379 // nbshapes rr_91_2_2
380 // # 0 vertices; 1 edge
382 //This shape is invalid and can lead to exception in this code.
384 if (aVertex1_1.IsNull() || aVertex1_2.IsNull())
385 return TopoDS_Vertex();
387 if (theEdge1->GetEdge().IsSame(theEdge2->GetEdge()))
389 return aVertex1_1.IsSame(aVertex1_2) ? aVertex1_1 : TopoDS_Vertex();
392 TopoDS_Vertex aVertex2_1, aVertex2_2;
393 TopExp::Vertices(theEdge2->GetEdge(), aVertex2_1, aVertex2_2);
395 if (aVertex2_1.IsNull() || aVertex2_2.IsNull())
396 return TopoDS_Vertex();
398 if (isSameWithSomeOf(aVertex1_1, aVertex2_1, aVertex2_2))
402 else if (isSameWithSomeOf(aVertex1_2, aVertex2_1, aVertex2_2))
407 const gp_Pnt aPnt1_1 = BRep_Tool::Pnt(aVertex1_1);
408 const gp_Pnt aPnt1_2 = BRep_Tool::Pnt(aVertex1_2);
409 const Standard_Real aTol1_1 = BRep_Tool::Tolerance(aVertex1_1);
410 const Standard_Real aTol1_2 = BRep_Tool::Tolerance(aVertex1_2);
412 const gp_Pnt aPnt2_1 = BRep_Tool::Pnt(aVertex2_1);
413 const gp_Pnt aPnt2_2 = BRep_Tool::Pnt(aVertex2_2);
414 const Standard_Real aTol2_1 = BRep_Tool::Tolerance(aVertex2_1);
415 const Standard_Real aTol2_2 = BRep_Tool::Tolerance(aVertex2_2);
417 if (isInToleranceWithSomeOf(aPnt1_1, aPnt2_1, aPnt2_2, aTol1_1 + Max(aTol2_1, aTol2_2)))
421 else if (isInToleranceWithSomeOf(aPnt1_2, aPnt2_1, aPnt2_2, aTol1_2 + Max(aTol2_1, aTol2_2)))
426 return TopoDS_Vertex();
429 //=======================================================================
430 // Function: connectClosestPoints
432 //=======================================================================
433 Standard_Boolean BRepMesh_ModelHealer::connectClosestPoints(
434 const IMeshData::IPCurveHandle& thePrevDEdge,
435 const IMeshData::IPCurveHandle& theCurrDEdge,
436 const IMeshData::IPCurveHandle& theNextDEdge) const
438 if (thePrevDEdge->IsInternal() ||
439 theCurrDEdge->IsInternal() ||
440 theNextDEdge->IsInternal())
442 return Standard_True;
445 gp_Pnt2d& aPrevFirstUV = thePrevDEdge->GetPoint(0);
446 gp_Pnt2d& aPrevLastUV = thePrevDEdge->GetPoint(thePrevDEdge->ParametersNb() - 1);
448 if (thePrevDEdge == theCurrDEdge)
450 // Wire consists of a single edge.
451 aPrevFirstUV = aPrevLastUV;
452 return Standard_True;
455 gp_Pnt2d& aCurrFirstUV = theCurrDEdge->GetPoint(0);
456 gp_Pnt2d& aCurrLastUV = theCurrDEdge->GetPoint(theCurrDEdge->ParametersNb() - 1);
458 gp_Pnt2d *aPrevUV = NULL, *aCurrPrevUV = NULL;
459 const Standard_Real aPrevSqDist = closestPoints(aPrevFirstUV, aPrevLastUV,
460 aCurrFirstUV, aCurrLastUV,
461 aPrevUV, aCurrPrevUV);
463 gp_Pnt2d *aNextUV = NULL, *aCurrNextUV = NULL;
464 if (thePrevDEdge == theNextDEdge)
466 // Wire consists of two edges. Connect both ends.
467 aNextUV = (aPrevUV == &aPrevFirstUV) ? &aPrevLastUV : &aPrevFirstUV;
468 aCurrNextUV = (aCurrPrevUV == &aCurrFirstUV) ? &aCurrLastUV : &aCurrFirstUV;
470 *aNextUV = *aCurrNextUV;
471 *aPrevUV = *aCurrPrevUV;
472 return Standard_True;
475 gp_Pnt2d& aNextFirstUV = theNextDEdge->GetPoint(0);
476 gp_Pnt2d& aNextLastUV = theNextDEdge->GetPoint(theNextDEdge->ParametersNb() - 1);
478 const Standard_Real aNextSqDist = closestPoints(aNextFirstUV, aNextLastUV,
479 aCurrFirstUV, aCurrLastUV,
480 aNextUV, aCurrNextUV);
483 std::cout << "PrevSqDist = " << aPrevSqDist << std::endl;
484 std::cout << "NextSqDist = " << aNextSqDist << std::endl;
487 // Connect closest points first. This can help to identify
488 // which ends should be connected in case of gap.
489 if (aPrevSqDist - aNextSqDist > gp::Resolution())
491 adjustSamePoints(aCurrNextUV, aNextUV, aCurrPrevUV, aPrevUV, aCurrFirstUV, aCurrLastUV, aPrevFirstUV, aPrevLastUV);
495 adjustSamePoints(aCurrPrevUV, aPrevUV, aCurrNextUV, aNextUV, aCurrFirstUV, aCurrLastUV, aNextFirstUV, aNextLastUV);
498 return Standard_True;