1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_ShapeTools.h"
22 #include "GeomAlgoAPI_SketchBuilder.h"
24 #include <GeomAPI_Ax1.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_Face.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_Pnt.h>
30 #include <GeomAPI_Wire.h>
32 #include <Bnd_Box.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepAlgo.hxx>
36 #include <BRepAlgo_FaceRestrictor.hxx>
37 #include <BRepBndLib.hxx>
38 #include <BRepBuilderAPI_FindPlane.hxx>
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <BRepExtrema_DistShapeShape.hxx>
43 #include <BRepExtrema_ExtCF.hxx>
44 #include <BRepGProp.hxx>
45 #include <BRepTools.hxx>
46 #include <BRepTools_WireExplorer.hxx>
47 #include <BRepTopAdaptor_FClass2d.hxx>
48 #include <BRepClass_FaceClassifier.hxx>
49 #include <Geom2d_Curve.hxx>
50 #include <Geom2d_Curve.hxx>
51 #include <BRepLib_CheckCurveOnSurface.hxx>
52 #include <BRep_Tool.hxx>
53 #include <Geom_CylindricalSurface.hxx>
54 #include <Geom_Line.hxx>
55 #include <Geom_Plane.hxx>
56 #include <GeomAPI_ProjectPointOnCurve.hxx>
57 #include <GeomAPI_ShapeIterator.h>
58 #include <GeomLib_IsPlanarSurface.hxx>
59 #include <GeomLib_Tool.hxx>
60 #include <GeomAPI_IntCS.hxx>
62 #include <GProp_GProps.hxx>
63 #include <IntAna_IntConicQuad.hxx>
64 #include <IntAna_Quadric.hxx>
65 #include <NCollection_Vector.hxx>
66 #include <ShapeAnalysis.hxx>
67 #include <ShapeAnalysis_Surface.hxx>
68 #include <TopoDS_Builder.hxx>
69 #include <TopoDS_Edge.hxx>
70 #include <TopoDS_Face.hxx>
71 #include <TopoDS_Shape.hxx>
72 #include <TopoDS_Shell.hxx>
73 #include <TopoDS_Vertex.hxx>
76 #include <TopExp_Explorer.hxx>
77 #include <TopTools_ListIteratorOfListOfShape.hxx>
80 #include <BOPAlgo_Builder.hxx>
81 #include <BRepBuilderAPI_MakeVertex.hxx>
82 #include <TopoDS_Edge.hxx>
84 //==================================================================================================
85 static GProp_GProps props(const TopoDS_Shape& theShape)
89 if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_WIRE)
91 BRepGProp::LinearProperties(theShape, aGProps);
93 else if (theShape.ShapeType() == TopAbs_FACE || theShape.ShapeType() == TopAbs_SHELL)
95 const Standard_Real anEps = 1.e-6;
96 BRepGProp::SurfaceProperties(theShape, aGProps, anEps);
98 else if (theShape.ShapeType() == TopAbs_SOLID || theShape.ShapeType() == TopAbs_COMPSOLID)
100 BRepGProp::VolumeProperties(theShape, aGProps);
102 else if (theShape.ShapeType() == TopAbs_COMPOUND)
104 for (TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next())
106 aGProps.Add(props(anIt.Value()));
113 //==================================================================================================
114 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
116 if(!theShape.get()) {
119 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
120 if(aShape.IsNull()) {
123 const Standard_Real anEps = 1.e-6;
124 TopExp_Explorer anExp(aShape, TopAbs_SOLID);
125 if (anExp.More()) { // return volume if there is at least one solid
126 double aVolume = 0.0;
127 for (; anExp.More(); anExp.Next()) {
128 GProp_GProps aGProps;
129 BRepGProp::VolumeProperties(anExp.Current(), aGProps, anEps);
130 aVolume += aGProps.Mass();
134 // return surfaces area
135 GProp_GProps aGProps;
136 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
137 return aGProps.Mass();
140 //==================================================================================================
141 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
143 GProp_GProps aGProps;
144 if(!theShape.get()) {
147 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
148 if(aShape.IsNull()) {
151 const Standard_Real anEps = 1.e-6;
153 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
154 return aGProps.Mass();
157 //==================================================================================================
158 std::shared_ptr<GeomAPI_Pnt>
159 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
161 GProp_GProps aGProps;
163 return std::shared_ptr<GeomAPI_Pnt>();
165 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
166 if(aShape.IsNull()) {
167 return std::shared_ptr<GeomAPI_Pnt>();
170 if(aShape.ShapeType() == TopAbs_VERTEX) {
171 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
173 aGProps = props(aShape);
174 aCentre = aGProps.CentreOfMass();
177 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
180 //==================================================================================================
181 double GeomAlgoAPI_ShapeTools::radius(const std::shared_ptr<GeomAPI_Face>& theCylinder)
183 double aRadius = -1.0;
184 if (theCylinder->isCylindrical()) {
185 const TopoDS_Shape& aShape = theCylinder->impl<TopoDS_Shape>();
186 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
187 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
189 aRadius = aCyl->Radius();
194 //==================================================================================================
195 double GeomAlgoAPI_ShapeTools::minimalDistance(const GeomShapePtr& theShape1,
196 const GeomShapePtr& theShape2)
198 const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
199 const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
201 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
203 return aDist.IsDone() ? aDist.Value() : Precision::Infinite();
206 //==================================================================================================
207 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
208 const std::shared_ptr<GeomAPI_Shape> theCompound,
209 const GeomAPI_Shape::ShapeType theType,
210 ListOfShape& theResuts)
213 ListOfShape aResCombinedShapes;
214 ListOfShape aResFreeShapes;
216 GeomShapePtr aResult = theCompound;
218 if(!theCompound.get()) {
222 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
226 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
227 TopAbs_ShapeEnum aTA = TopAbs_FACE;
228 if(theType == GeomAPI_Shape::COMPSOLID) {
233 // map from the resulting shapes to minimal index of the used shape from theCompound list
234 std::map<GeomShapePtr, int> anInputOrder;
235 // map from ancestors-shapes to the index of shapes in theCompound
236 NCollection_DataMap<TopoDS_Shape, int> anAncestorsOrder;
240 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
241 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next(), anOrder++) {
242 const TopoDS_Shape& aShape = anIter.Value();
243 if(aShape.ShapeType() > aTA) {
244 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
245 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
246 aResFreeShapes.push_back(aGeomShape);
247 anInputOrder[aGeomShape] = anOrder;
249 for(TopExp_Explorer anExp(aShape, aTA); anExp.More(); anExp.Next()) {
250 anAncestorsOrder.Bind(anExp.Current(), anOrder);
255 // Map sub-shapes and shapes.
256 TopTools_IndexedDataMapOfShapeListOfShape aMapSA;
257 TopExp::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
258 if(aMapSA.IsEmpty()) {
263 // Get all shapes with common sub-shapes and free shapes.
264 NCollection_Map<TopoDS_Shape> aFreeShapes;
265 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
266 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
267 anIter(aMapSA); anIter.More(); anIter.Next()) {
268 const TopoDS_Shape& aShape = anIter.Key();
269 TopTools_ListOfShape& aListOfShape = anIter.ChangeValue();
270 if(aListOfShape.IsEmpty()) {
273 else if(aListOfShape.Size() == 1) {
274 const TopoDS_Shape& aF = aListOfShape.First();
276 aListOfShape.Clear();
278 NCollection_List<TopoDS_Shape> aTempList;
279 NCollection_Map<TopoDS_Shape> aTempMap;
280 for (TopTools_ListOfShape::Iterator aListIt(aListOfShape); aListIt.More(); aListIt.Next()) {
281 aTempList.Append(aListIt.Value());
282 aTempMap.Add(aListIt.Value());
283 aFreeShapes.Remove(aListIt.Value());
285 aListOfShape.Clear();
286 for(NCollection_List<TopoDS_Shape>::Iterator
287 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
288 const TopoDS_Shape& aTempShape = aTempIter.Value();
289 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
290 anIter(aMapSA); anIter.More(); anIter.Next()) {
291 TopTools_ListOfShape& aTempListOfShape = anIter.ChangeValue();
292 if(aTempListOfShape.IsEmpty()) {
294 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
295 aTempListOfShape.Clear();
296 } else if(aTempListOfShape.Size() > 1) {
297 TopTools_ListOfShape::Iterator anIt1(aTempListOfShape);
298 for (; anIt1.More(); anIt1.Next()) {
299 if (anIt1.Value() == aTempShape) {
300 TopTools_ListOfShape::Iterator anIt2(aTempListOfShape);
301 for (; anIt2.More(); anIt2.Next())
303 if (anIt2.Value() != anIt1.Value()) {
304 if (aTempMap.Add(anIt2.Value())) {
305 aTempList.Append(anIt2.Value());
306 aFreeShapes.Remove(anIt2.Value());
310 aTempListOfShape.Clear();
317 aShapesWithCommonSubshapes.Append(aTempMap);
321 // Combine shapes with common sub-shapes.
322 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
323 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
325 TopoDS_CompSolid aCSolid;
326 TopoDS_Builder aBuilder;
329 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
330 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
331 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
332 const TopoDS_Shape& aShape = anExp.Current();
333 if(aShapesMap.Contains(aShape)) {
335 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
336 aShapesMap.Remove(aShape);
337 int aThisOrder = anAncestorsOrder.Find(aShape);
338 if (anOrder == -1 || aThisOrder < anOrder)
339 anOrder = aThisOrder; // take the minimum order position
342 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
343 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
344 new TopoDS_Shape(aShell);
345 aGeomShape->setImpl<TopoDS_Shape>(aSh);
346 aResCombinedShapes.push_back(aGeomShape);
347 anInputOrder[aGeomShape] = anOrder;
350 // Adding free shapes.
351 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
352 const TopoDS_Shape& aShape = anExp.Current();
353 if(aFreeShapes.Contains(aShape)) {
354 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
355 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
356 aResFreeShapes.push_back(aGeomShape);
357 anInputOrder[aGeomShape] = anAncestorsOrder.Find(aShape);
361 if(aResCombinedShapes.size() == 1 && aResFreeShapes.size() == 0) {
362 aResult = aResCombinedShapes.front();
363 theResuts.push_back(aResult);
364 } else if(aResCombinedShapes.size() == 0 && aResFreeShapes.size() == 1) {
365 aResult = aResFreeShapes.front();
366 theResuts.push_back(aResult);
368 TopoDS_Compound aResultComp;
369 TopoDS_Builder aBuilder;
370 aBuilder.MakeCompound(aResultComp);
371 // put to result compound and result list in accordance to the order numbers
372 std::map<GeomShapePtr, int>::iterator anInputIter = anInputOrder.begin();
373 std::map<int, GeomShapePtr> aNums;
374 for(; anInputIter != anInputOrder.end(); anInputIter++)
375 aNums[anInputIter->second] = anInputIter->first;
376 std::map<int, GeomShapePtr>::iterator aNumsIter = aNums.begin();
377 for(; aNumsIter != aNums.end(); aNumsIter++) {
378 aBuilder.Add(aResultComp, (aNumsIter->second)->impl<TopoDS_Shape>());
379 theResuts.push_back(aNumsIter->second);
381 aResult->setImpl(new TopoDS_Shape(aResultComp));
387 //==================================================================================================
388 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
389 NCollection_List<TopoDS_Shape>& theList)
391 if(theShape.IsNull()) {
395 if(theShape.ShapeType() == TopAbs_COMPOUND) {
396 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
397 addSimpleShapeToList(anIt.Value(), theList);
400 theList.Append(theShape);
404 //==================================================================================================
405 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
407 TopoDS_Compound aCompound;
409 BRep_Builder aBuilder;
410 aBuilder.MakeCompound(aCompound);
412 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
413 aBuilder.Add(aCompound, anIt.Value());
419 //==================================================================================================
420 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
421 const std::shared_ptr<GeomAPI_Shape> theCompound)
423 GeomShapePtr aResult = theCompound;
425 if (!theCompound.get()) {
429 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
430 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
431 addSimpleShapeToList(anInShape, anUngroupedShapes);
433 // Iterate over all shapes and find shapes with shared vertices.
434 TopTools_ListOfShape allVertices;
435 TopTools_DataMapOfShapeListOfShape aVertexShapesMap;
436 for (NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
439 const TopoDS_Shape& aShape = aShapesIt.Value();
440 for (TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
443 const TopoDS_Shape& aVertex = aShapeExp.Current();
444 if (!aVertexShapesMap.IsBound(aVertex)) {
445 NCollection_List<TopoDS_Shape> aList;
446 aList.Append(aShape);
447 allVertices.Append(aVertex);
448 aVertexShapesMap.Bind(aVertex, aList);
451 if (!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
452 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
458 // Iterate over the map and group shapes.
459 NCollection_Vector<TopTools_MapOfShape> aGroups; // groups of shapes connected by vertices
460 while (!allVertices.IsEmpty()) {
461 // Get first group of shapes in map, and then unbind it.
462 const TopoDS_Shape& aKey = allVertices.First();
463 TopTools_ListOfShape aConnectedShapes = aVertexShapesMap.Find(aKey);
464 aVertexShapesMap.UnBind(aKey);
465 allVertices.Remove(aKey);
466 // Iterate over shapes in this group and add to it shapes from groups in map.
467 for (TopTools_ListOfShape::Iterator aConnectedIt(aConnectedShapes);
468 aConnectedIt.More(); aConnectedIt.Next()) {
469 const TopoDS_Shape& aConnected = aConnectedIt.Value();
470 TopTools_ListOfShape aKeysToUnbind;
471 for (TopTools_ListOfShape::Iterator aKeysIt(allVertices);
474 const TopTools_ListOfShape& anOtherConnected = aVertexShapesMap(aKeysIt.Value());
475 if (!anOtherConnected.Contains(aConnected)) {
476 // Other connected group does not contain shape from our connected group
479 // Other is connected to our, so add them to our connected
480 for (TopTools_ListOfShape::Iterator anOtherIt(anOtherConnected);
483 const TopoDS_Shape& aShape = anOtherIt.Value();
484 if (!aConnectedShapes.Contains(aShape)) {
485 aConnectedShapes.Append(aShape);
488 // Save key to unbind from this map.
489 aKeysToUnbind.Append(aKeysIt.Value());
491 // Unbind groups from map that we added to our group.
492 for (TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
495 aVertexShapesMap.UnBind(aKeysIt.Value());
496 allVertices.Remove(aKeysIt.Value());
499 // Sort shapes from the most complicated to the simplest ones
500 TopTools_MapOfShape aSortedGroup;
501 for (int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
502 TopTools_ListOfShape::Iterator anIt(aConnectedShapes);
503 while (anIt.More()) {
504 if (anIt.Value().ShapeType() == aST) {
505 aSortedGroup.Add(anIt.Value());
506 aConnectedShapes.Remove(anIt);
513 aGroups.Append(aSortedGroup);
516 TopoDS_Compound aCompound;
517 BRep_Builder aBuilder;
518 aBuilder.MakeCompound(aCompound);
520 for (NCollection_Vector<TopTools_MapOfShape>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
521 const TopTools_MapOfShape& aGroup = anIt.ChangeValue();
522 GeomShapePtr aGeomShape(new GeomAPI_Shape());
523 if(aGroup.Size() == 1) {
524 TopTools_MapOfShape::Iterator aOneShapeIter(aGroup);
525 aGeomShape->setImpl(new TopoDS_Shape(aOneShapeIter.Value()));
527 // make sub-shapes in the group have order same as in original shape
528 TopTools_ListOfShape anOrderedGoup;
529 NCollection_List<TopoDS_Shape>::Iterator anUngrouped(anUngroupedShapes);
530 for (; anUngrouped.More(); anUngrouped.Next()) {
531 if (aGroup.Contains(anUngrouped.Value()))
532 anOrderedGoup.Append(anUngrouped.Value());
534 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(anOrderedGoup)));
535 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
536 GeomAPI_Shape::COMPSOLID,
539 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
542 if(!aCompound.IsNull()) {
543 aResult->setImpl(new TopoDS_Shape(aCompound));
549 //==================================================================================================
550 bool GeomAlgoAPI_ShapeTools::hasSharedTopology(const ListOfShape& theShapes,
551 const GeomAPI_Shape::ShapeType theShapeType)
553 TopTools_IndexedMapOfShape aSubs;
554 for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt) {
555 TopTools_IndexedMapOfShape aCurSubs;
556 TopExp::MapShapes((*anIt)->impl<TopoDS_Shape>(), (TopAbs_ShapeEnum)theShapeType, aCurSubs);
557 for (TopTools_IndexedMapOfShape::Iterator aSubIt(aCurSubs); aSubIt.More(); aSubIt.Next()) {
558 if (aSubs.Contains(aSubIt.Value()))
561 aSubs.Add(aSubIt.Value());
567 //==================================================================================================
568 std::list<std::shared_ptr<GeomAPI_Pnt> >
569 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
571 // Bounding box of all objects.
575 for (ListOfShape::const_iterator
576 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
577 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
578 BRepBndLib::Add(aShape, aBndBox);
581 if(theEnlarge != 0.0) {
582 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
583 aBndBox.Enlarge(theEnlarge);
586 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
587 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
588 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
589 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
591 for(int i = 0; i < 2; i++) {
592 for(int j = 0; j < 2; j++) {
593 for(int k = 0; k < 2; k++) {
594 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
595 aResultPoints.push_back(aPnt);
600 return aResultPoints;
603 //==================================================================================================
604 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
605 const std::shared_ptr<GeomAPI_Shape> thePlane,
606 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
608 std::shared_ptr<GeomAPI_Face> aResultFace;
610 if(!thePlane.get()) {
614 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
615 if(aShape.ShapeType() != TopAbs_FACE) {
619 TopoDS_Face aFace = TopoDS::Face(aShape);
620 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
625 GeomLib_IsPlanarSurface isPlanar(aSurf);
626 if(!isPlanar.IsPlanar()) {
630 if(thePoints.size() != 8) {
634 const gp_Pln& aFacePln = isPlanar.Plan();
635 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
636 IntAna_Quadric aQuadric(aFacePln);
637 Standard_Real UMin, UMax, VMin, VMax;
638 UMin = UMax = VMin = VMax = 0;
639 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
640 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
641 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
642 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
643 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
644 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
645 Standard_Real aPntU(0), aPntV(0);
646 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
647 if(aPntU < UMin) UMin = aPntU;
648 if(aPntU > UMax) UMax = aPntU;
649 if(aPntV < VMin) VMin = aPntV;
650 if(aPntV > VMax) VMax = aPntV;
652 aResultFace.reset(new GeomAPI_Face());
653 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
658 //==================================================================================================
659 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
660 std::shared_ptr<GeomAPI_Vertex>& theV1,
661 std::shared_ptr<GeomAPI_Vertex>& theV2)
663 static GeomVertexPtr aVertex;
665 aVertex = GeomVertexPtr(new GeomAPI_Vertex);
666 aVertex->setImpl(new TopoDS_Vertex());
673 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
674 TopoDS_Vertex aV1, aV2;
675 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
677 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
678 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
679 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
685 //==================================================================================================
686 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
687 const std::shared_ptr<GeomAPI_Dir> theDirection,
688 const ListOfShape& theWires,
689 ListOfShape& theFaces)
691 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
692 theDirection->impl<gp_Dir>()));
693 TopoDS_Face aFace = aMKFace.Face();
695 BRepAlgo_FaceRestrictor aFRestrictor;
696 aFRestrictor.Init(aFace, Standard_False, Standard_True);
697 for(ListOfShape::const_iterator anIt = theWires.cbegin();
698 anIt != theWires.cend();
700 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
701 aFRestrictor.Add(aWire);
704 aFRestrictor.Perform();
706 if(!aFRestrictor.IsDone()) {
710 for(; aFRestrictor.More(); aFRestrictor.Next()) {
711 GeomShapePtr aShape(new GeomAPI_Shape());
712 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
713 theFaces.push_back(aShape);
717 //==================================================================================================
718 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
720 TopoDS_Compound aCompound;
721 BRep_Builder aBuilder;
722 aBuilder.MakeCompound(aCompound);
724 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
725 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
727 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
729 if(aFindPlane.Found() != Standard_True) {
730 return std::shared_ptr<GeomAPI_Pln>();
733 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
734 gp_Pnt aLoc = aPlane->Location();
735 gp_Dir aDir = aPlane->Axis().Direction();
737 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
738 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
740 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
745 //==================================================================================================
746 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
747 const std::shared_ptr<GeomAPI_Shape> theSubShape,
748 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
750 if(!theSubShape.get() || !theBaseShape.get()) {
754 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
755 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
757 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
758 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
759 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
761 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
764 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
765 if(aBaseShape.ShapeType() == TopAbs_FACE) {
766 // Check that edge is on face surface.
767 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
768 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
769 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
771 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
775 // Check intersections.
776 TopoDS_Vertex aV1, aV2;
777 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
778 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
779 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
780 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
781 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
782 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
784 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
785 // Edge intersect face bound. Check that it is not on edge begin or end.
786 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
787 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
788 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
789 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
796 // No intersections found. Edge is inside or outside face. Check it.
797 BRepAdaptor_Curve aCurveAdaptor(anEdge);
798 gp_Pnt aPointToCheck =
799 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
800 aCurveAdaptor.LastParameter()) / 2.0);
801 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
802 ShapeAnalysis_Surface aSAS(aSurface);
803 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
804 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
805 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
819 //==================================================================================================
820 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
822 if(!theShape.get()) {
826 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
827 return (aChecker.IsValid() == Standard_True);
830 //==================================================================================================
831 std::shared_ptr<GeomAPI_Shape>
832 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
834 GeomShapePtr anOuterWire;
836 if(!theFace.get() || !theFace->isFace()) {
840 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
841 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
843 anOuterWire.reset(new GeomAPI_Shape());
844 anOuterWire->setImpl(new TopoDS_Shape(aWire));
849 //==================================================================================================
850 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
851 const std::shared_ptr<GeomAPI_Face> theFace)
853 if(!theEdge.get() || !theFace.get()) {
857 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
858 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
860 BRepExtrema_ExtCF anExt(anEdge, aFace);
861 return anExt.IsParallel() == Standard_True;
864 //==================================================================================================
865 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
866 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
867 const bool thePointsOutsideFace)
869 std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
870 if(!theEdge.get() || !theFace.get()) {
874 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
875 double aFirstOnCurve, aLastOnCurve;
876 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
878 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
879 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
881 GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
882 if (!anIntAlgo.IsDone())
884 // searching for points-intersection
885 for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
887 if (anIntNum <= anIntAlgo.NbPoints()) {
888 anInt = anIntAlgo.Point(anIntNum);
889 } else { // take the middle point on the segment of the intersection
890 Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
891 anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
893 aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
894 new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
899 //==================================================================================================
900 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
901 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
902 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
904 // to split shape at least one point should be presented in the points container
905 if (thePointsInfo.empty())
908 // General Fuse to split edge by vertices
909 BOPAlgo_Builder aBOP;
910 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
911 // Rebuild closed edge to place vertex to one of split points.
912 // This will prevent edge to be split on same vertex.
913 if (BRep_Tool::IsClosed(aBaseEdge))
915 Standard_Real aFirst, aLast;
916 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
918 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
919 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
920 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
922 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
923 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
924 aBaseEdge.Orientation(anOrientation);
926 aBOP.AddArgument(aBaseEdge);
928 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
929 for (; aPIt != thePointsInfo.end(); ++aPIt) {
930 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
931 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
932 aBOP.AddArgument(aV);
936 if (aBOP.HasErrors())
940 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
941 TopTools_ListIteratorOfListOfShape anIt(aSplits);
942 for (; anIt.More(); anIt.Next()) {
943 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
944 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
945 theShapes.insert(anEdge);
949 //==================================================================================================
950 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
951 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
952 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
954 // General Fuse to split edge by vertices
955 BOPAlgo_Builder aBOP;
956 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
957 // Rebuild closed edge to place vertex to one of split points.
958 // This will prevent edge to be split on seam vertex.
959 if (BRep_Tool::IsClosed(aBaseEdge))
961 Standard_Real aFirst, aLast;
962 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
964 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
965 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
967 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
968 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
969 aBaseEdge.Orientation(anOrientation);
971 aBOP.AddArgument(aBaseEdge);
973 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
974 for (; aPtIt != thePoints.end(); ++aPtIt) {
975 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
976 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
977 aBOP.AddArgument(aV);
981 if (aBOP.HasErrors())
985 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
986 TopTools_ListIteratorOfListOfShape anIt(aSplits);
987 for (; anIt.More(); anIt.Next()) {
988 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
989 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
990 theShapes.insert(anEdge);
994 //==================================================================================================
995 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
996 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
997 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
999 std::shared_ptr<GeomAPI_Shape> aResultShape;
1001 if (thePoints.size() == 2) {
1002 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
1003 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
1005 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
1007 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
1008 aLast = theShapes.end();
1009 for (; anIt != aLast; anIt++) {
1010 GeomShapePtr aShape = *anIt;
1011 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
1013 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
1014 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
1015 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
1016 anEdgeLastPoint->isEqual(aLastPoint))
1017 aResultShape = aShape;
1022 return aResultShape;
1025 //==================================================================================================
1026 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
1027 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
1028 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
1029 const std::shared_ptr<GeomAPI_Ax1> theAxis)
1031 gp_Pnt aCentreOfMassPoint =
1032 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
1033 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
1034 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
1035 gp_Pnt aPoint = aPrjTool.NearestPoint();
1037 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
1038 aCentreOfMassPoint.Y()-aPoint.Y(),
1039 aCentreOfMassPoint.Z()-aPoint.Z()));
1044 //==================================================================================================
1045 static TopoDS_Wire fixParametricGaps(const TopoDS_Wire& theWire)
1047 TopoDS_Wire aFixedWire;
1048 Handle(Geom_Curve) aPrevCurve;
1049 double aPrevLastParam = 0.0;
1051 BRep_Builder aBuilder;
1052 aBuilder.MakeWire(aFixedWire);
1054 BRepTools_WireExplorer aWExp(theWire);
1055 for (; aWExp.More(); aWExp.Next()) {
1056 TopoDS_Edge anEdge = aWExp.Current();
1057 double aFirst, aLast;
1058 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
1059 if (aCurve == aPrevCurve) {
1060 // if parametric gap occurs, create new edge based on the copied curve
1061 aCurve = Handle(Geom_Curve)::DownCast(aCurve->Copy());
1062 TopoDS_Vertex aV1, aV2;
1063 TopExp::Vertices(anEdge, aV1, aV2);
1064 anEdge = TopoDS::Edge(anEdge.EmptyCopied());
1065 aBuilder.UpdateEdge(anEdge, aCurve, BRep_Tool::Tolerance(anEdge));
1066 aBuilder.Add(anEdge, aV1);
1067 aBuilder.Add(anEdge, aV2);
1070 aBuilder.Add(aFixedWire, anEdge);
1072 aPrevCurve = aCurve;
1073 aPrevLastParam = aLast;
1079 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
1080 const std::shared_ptr<GeomAPI_Wire>& theWire)
1084 TopoDS_Wire aWire = theWire->impl<TopoDS_Wire>();
1085 // Workaround: when concatenate a wire consisting of two edges based on the same B-spline curve
1086 // (non-periodic, but having equal start and end points), first of which is placed at the end
1087 // on the curve and second is placed at the start, this workaround copies second curve to avoid
1088 // treating these edges as a single curve by setting trim parameters.
1089 aWire = fixParametricGaps(aWire);
1090 TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
1091 anEdge = GeomEdgePtr(new GeomAPI_Edge);
1092 anEdge->setImpl(new TopoDS_Edge(aNewEdge));
1097 ListOfShape GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(const GeomShapePtr& theShape)
1099 ListOfShape aSubShapes;
1101 if (!theShape->isCompound() && !theShape->isCompSolid() &&
1102 !theShape->isShell() && !theShape->isWire()) {
1106 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
1107 GeomShapePtr aSubShape = anIt.current();
1108 if (aSubShape->isVertex() || aSubShape->isEdge() ||
1109 aSubShape->isFace() || aSubShape->isSolid()) {
1110 aSubShapes.push_back(aSubShape);
1112 aSubShapes.splice(aSubShapes.end(), getLowLevelSubShapes(aSubShape));