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>
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepAlgo.hxx>
37 #include <BRepAlgo_FaceRestrictor.hxx>
38 #include <BRepAdaptor_Curve.hxx>
39 #include <BRepBndLib.hxx>
40 #include <BRepBuilderAPI_FindPlane.hxx>
41 #include <BRepBuilderAPI_MakeEdge.hxx>
42 #include <BRepBuilderAPI_MakeFace.hxx>
43 #include <BRepBuilderAPI_MakeVertex.hxx>
44 #include <BRepCheck_Analyzer.hxx>
45 #include <BRepExtrema_DistShapeShape.hxx>
46 #include <BRepExtrema_ExtCF.hxx>
47 #include <BRepGProp.hxx>
48 #include <BRepTools.hxx>
49 #include <BRepTools_WireExplorer.hxx>
50 #include <BRepTopAdaptor_FClass2d.hxx>
51 #include <BRepClass_FaceClassifier.hxx>
52 #include <BRepLib_CheckCurveOnSurface.hxx>
53 #include <BRepLProp.hxx>
55 #include <BOPAlgo_Builder.hxx>
57 #include <Geom2d_Curve.hxx>
58 #include <Geom2d_Curve.hxx>
60 #include <Geom_CylindricalSurface.hxx>
61 #include <Geom_Line.hxx>
62 #include <Geom_Plane.hxx>
63 #include <Geom_RectangularTrimmedSurface.hxx>
65 #include <GeomAPI_ProjectPointOnCurve.hxx>
66 #include <GeomAPI_ShapeIterator.h>
68 #include <GeomLib_IsPlanarSurface.hxx>
69 #include <GeomLib_Tool.hxx>
70 #include <GeomAPI_IntCS.hxx>
73 #include <GProp_GProps.hxx>
75 #include <IntAna_IntConicQuad.hxx>
76 #include <IntAna_Quadric.hxx>
78 #include <ShapeAnalysis.hxx>
79 #include <ShapeAnalysis_Surface.hxx>
82 #include <TopoDS_Edge.hxx>
83 #include <TopoDS_Face.hxx>
84 #include <TopoDS_Shape.hxx>
85 #include <TopoDS_Shell.hxx>
86 #include <TopoDS_Vertex.hxx>
87 #include <TopoDS_Builder.hxx>
90 #include <TopExp_Explorer.hxx>
92 #include <TopTools_ListIteratorOfListOfShape.hxx>
94 #include <NCollection_Vector.hxx>
96 //==================================================================================================
97 static GProp_GProps props(const TopoDS_Shape& theShape)
101 if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_WIRE)
103 BRepGProp::LinearProperties(theShape, aGProps);
105 else if (theShape.ShapeType() == TopAbs_FACE || theShape.ShapeType() == TopAbs_SHELL)
107 const Standard_Real anEps = 1.e-6;
108 BRepGProp::SurfaceProperties(theShape, aGProps, anEps);
110 else if (theShape.ShapeType() == TopAbs_SOLID || theShape.ShapeType() == TopAbs_COMPSOLID)
112 BRepGProp::VolumeProperties(theShape, aGProps);
114 else if (theShape.ShapeType() == TopAbs_COMPOUND)
116 for (TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next())
118 aGProps.Add(props(anIt.Value()));
125 //==================================================================================================
126 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
128 if(!theShape.get()) {
131 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
132 if(aShape.IsNull()) {
135 const Standard_Real anEps = 1.e-6;
136 TopExp_Explorer anExp(aShape, TopAbs_SOLID);
137 if (anExp.More()) { // return volume if there is at least one solid
138 double aVolume = 0.0;
139 for (; anExp.More(); anExp.Next()) {
140 GProp_GProps aGProps;
141 BRepGProp::VolumeProperties(anExp.Current(), aGProps, anEps);
142 aVolume += aGProps.Mass();
146 // return surfaces area
147 GProp_GProps aGProps;
148 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
149 return aGProps.Mass();
152 //==================================================================================================
153 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
155 GProp_GProps aGProps;
156 if(!theShape.get()) {
159 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
160 if(aShape.IsNull()) {
163 const Standard_Real anEps = 1.e-6;
165 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
166 return aGProps.Mass();
169 //==================================================================================================
170 std::shared_ptr<GeomAPI_Pnt>
171 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
173 GProp_GProps aGProps;
175 return std::shared_ptr<GeomAPI_Pnt>();
177 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
178 if(aShape.IsNull()) {
179 return std::shared_ptr<GeomAPI_Pnt>();
182 if(aShape.ShapeType() == TopAbs_VERTEX) {
183 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
185 aGProps = props(aShape);
186 aCentre = aGProps.CentreOfMass();
189 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
192 //==================================================================================================
193 double GeomAlgoAPI_ShapeTools::radius(const std::shared_ptr<GeomAPI_Face>& theCylinder)
195 double aRadius = -1.0;
196 if (theCylinder->isCylindrical()) {
197 const TopoDS_Shape& aShape = theCylinder->impl<TopoDS_Shape>();
198 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
199 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
201 aRadius = aCyl->Radius();
206 //==================================================================================================
207 double GeomAlgoAPI_ShapeTools::minimalDistance(const GeomShapePtr& theShape1,
208 const GeomShapePtr& theShape2)
210 const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
211 const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
213 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
215 return aDist.IsDone() ? aDist.Value() : Precision::Infinite();
218 //==================================================================================================
219 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
220 const std::shared_ptr<GeomAPI_Shape> theCompound,
221 const GeomAPI_Shape::ShapeType theType,
222 ListOfShape& theResuts)
225 ListOfShape aResCombinedShapes;
226 ListOfShape aResFreeShapes;
228 GeomShapePtr aResult = theCompound;
230 if(!theCompound.get()) {
234 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
238 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
239 TopAbs_ShapeEnum aTA = TopAbs_FACE;
240 if(theType == GeomAPI_Shape::COMPSOLID) {
245 // map from the resulting shapes to minimal index of the used shape from theCompound list
246 std::map<GeomShapePtr, int> anInputOrder;
247 // map from ancestors-shapes to the index of shapes in theCompound
248 NCollection_DataMap<TopoDS_Shape, int> anAncestorsOrder;
252 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
253 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next(), anOrder++) {
254 const TopoDS_Shape& aShape = anIter.Value();
255 if(aShape.ShapeType() > aTA) {
256 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
257 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
258 aResFreeShapes.push_back(aGeomShape);
259 anInputOrder[aGeomShape] = anOrder;
261 for(TopExp_Explorer anExp(aShape, aTA); anExp.More(); anExp.Next()) {
262 anAncestorsOrder.Bind(anExp.Current(), anOrder);
267 // Map sub-shapes and shapes.
268 TopTools_IndexedDataMapOfShapeListOfShape aMapSA;
269 TopExp::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
270 if(aMapSA.IsEmpty()) {
275 // Get all shapes with common sub-shapes and free shapes.
276 NCollection_Map<TopoDS_Shape> aFreeShapes;
277 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
278 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
279 anIter(aMapSA); anIter.More(); anIter.Next()) {
280 const TopoDS_Shape& aShape = anIter.Key();
281 TopTools_ListOfShape& aListOfShape = anIter.ChangeValue();
282 if(aListOfShape.IsEmpty()) {
285 else if(aListOfShape.Size() == 1) {
286 const TopoDS_Shape& aF = aListOfShape.First();
288 aListOfShape.Clear();
290 NCollection_List<TopoDS_Shape> aTempList;
291 NCollection_Map<TopoDS_Shape> aTempMap;
292 for (TopTools_ListOfShape::Iterator aListIt(aListOfShape); aListIt.More(); aListIt.Next()) {
293 aTempList.Append(aListIt.Value());
294 aTempMap.Add(aListIt.Value());
295 aFreeShapes.Remove(aListIt.Value());
297 aListOfShape.Clear();
298 for(NCollection_List<TopoDS_Shape>::Iterator
299 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
300 const TopoDS_Shape& aTempShape = aTempIter.Value();
301 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
302 anIter(aMapSA); anIter.More(); anIter.Next()) {
303 TopTools_ListOfShape& aTempListOfShape = anIter.ChangeValue();
304 if(aTempListOfShape.IsEmpty()) {
306 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
307 aTempListOfShape.Clear();
308 } else if(aTempListOfShape.Size() > 1) {
309 TopTools_ListOfShape::Iterator anIt1(aTempListOfShape);
310 for (; anIt1.More(); anIt1.Next()) {
311 if (anIt1.Value() == aTempShape) {
312 TopTools_ListOfShape::Iterator anIt2(aTempListOfShape);
313 for (; anIt2.More(); anIt2.Next())
315 if (anIt2.Value() != anIt1.Value()) {
316 if (aTempMap.Add(anIt2.Value())) {
317 aTempList.Append(anIt2.Value());
318 aFreeShapes.Remove(anIt2.Value());
322 aTempListOfShape.Clear();
329 aShapesWithCommonSubshapes.Append(aTempMap);
333 // Combine shapes with common sub-shapes.
334 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
335 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
337 TopoDS_CompSolid aCSolid;
338 TopoDS_Builder aBuilder;
341 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
342 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
343 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
344 const TopoDS_Shape& aShape = anExp.Current();
345 if(aShapesMap.Contains(aShape)) {
347 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
348 aShapesMap.Remove(aShape);
349 int aThisOrder = anAncestorsOrder.Find(aShape);
350 if (anOrder == -1 || aThisOrder < anOrder)
351 anOrder = aThisOrder; // take the minimum order position
354 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
355 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
356 new TopoDS_Shape(aShell);
357 aGeomShape->setImpl<TopoDS_Shape>(aSh);
358 aResCombinedShapes.push_back(aGeomShape);
359 anInputOrder[aGeomShape] = anOrder;
362 // Adding free shapes.
363 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
364 const TopoDS_Shape& aShape = anExp.Current();
365 if(aFreeShapes.Contains(aShape)) {
366 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
367 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
368 aResFreeShapes.push_back(aGeomShape);
369 anInputOrder[aGeomShape] = anAncestorsOrder.Find(aShape);
373 if(aResCombinedShapes.size() == 1 && aResFreeShapes.size() == 0) {
374 aResult = aResCombinedShapes.front();
375 theResuts.push_back(aResult);
376 } else if(aResCombinedShapes.size() == 0 && aResFreeShapes.size() == 1) {
377 aResult = aResFreeShapes.front();
378 theResuts.push_back(aResult);
380 TopoDS_Compound aResultComp;
381 TopoDS_Builder aBuilder;
382 aBuilder.MakeCompound(aResultComp);
383 // put to result compound and result list in accordance to the order numbers
384 std::map<GeomShapePtr, int>::iterator anInputIter = anInputOrder.begin();
385 std::map<int, GeomShapePtr> aNums;
386 for(; anInputIter != anInputOrder.end(); anInputIter++)
387 aNums[anInputIter->second] = anInputIter->first;
388 std::map<int, GeomShapePtr>::iterator aNumsIter = aNums.begin();
389 for(; aNumsIter != aNums.end(); aNumsIter++) {
390 aBuilder.Add(aResultComp, (aNumsIter->second)->impl<TopoDS_Shape>());
391 theResuts.push_back(aNumsIter->second);
393 aResult->setImpl(new TopoDS_Shape(aResultComp));
399 //==================================================================================================
400 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
401 NCollection_List<TopoDS_Shape>& theList)
403 if(theShape.IsNull()) {
407 if(theShape.ShapeType() == TopAbs_COMPOUND) {
408 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
409 addSimpleShapeToList(anIt.Value(), theList);
412 theList.Append(theShape);
416 //==================================================================================================
417 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
419 TopoDS_Compound aCompound;
421 BRep_Builder aBuilder;
422 aBuilder.MakeCompound(aCompound);
424 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
425 aBuilder.Add(aCompound, anIt.Value());
431 //==================================================================================================
432 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
433 const std::shared_ptr<GeomAPI_Shape> theCompound)
435 GeomShapePtr aResult = theCompound;
437 if (!theCompound.get()) {
441 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
442 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
443 addSimpleShapeToList(anInShape, anUngroupedShapes);
445 // Iterate over all shapes and find shapes with shared vertices.
446 TopTools_ListOfShape allVertices;
447 TopTools_DataMapOfShapeListOfShape aVertexShapesMap;
448 for (NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
451 const TopoDS_Shape& aShape = aShapesIt.Value();
452 for (TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
455 const TopoDS_Shape& aVertex = aShapeExp.Current();
456 if (!aVertexShapesMap.IsBound(aVertex)) {
457 NCollection_List<TopoDS_Shape> aList;
458 aList.Append(aShape);
459 allVertices.Append(aVertex);
460 aVertexShapesMap.Bind(aVertex, aList);
463 if (!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
464 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
470 // Iterate over the map and group shapes.
471 NCollection_Vector<TopTools_MapOfShape> aGroups; // groups of shapes connected by vertices
472 while (!allVertices.IsEmpty()) {
473 // Get first group of shapes in map, and then unbind it.
474 const TopoDS_Shape& aKey = allVertices.First();
475 TopTools_ListOfShape aConnectedShapes = aVertexShapesMap.Find(aKey);
476 aVertexShapesMap.UnBind(aKey);
477 allVertices.Remove(aKey);
478 // Iterate over shapes in this group and add to it shapes from groups in map.
479 for (TopTools_ListOfShape::Iterator aConnectedIt(aConnectedShapes);
480 aConnectedIt.More(); aConnectedIt.Next()) {
481 const TopoDS_Shape& aConnected = aConnectedIt.Value();
482 TopTools_ListOfShape aKeysToUnbind;
483 for (TopTools_ListOfShape::Iterator aKeysIt(allVertices);
486 const TopTools_ListOfShape& anOtherConnected = aVertexShapesMap(aKeysIt.Value());
487 if (!anOtherConnected.Contains(aConnected)) {
488 // Other connected group does not contain shape from our connected group
491 // Other is connected to our, so add them to our connected
492 for (TopTools_ListOfShape::Iterator anOtherIt(anOtherConnected);
495 const TopoDS_Shape& aShape = anOtherIt.Value();
496 if (!aConnectedShapes.Contains(aShape)) {
497 aConnectedShapes.Append(aShape);
500 // Save key to unbind from this map.
501 aKeysToUnbind.Append(aKeysIt.Value());
503 // Unbind groups from map that we added to our group.
504 for (TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
507 aVertexShapesMap.UnBind(aKeysIt.Value());
508 allVertices.Remove(aKeysIt.Value());
511 // Sort shapes from the most complicated to the simplest ones
512 TopTools_MapOfShape aSortedGroup;
513 for (int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
514 TopTools_ListOfShape::Iterator anIt(aConnectedShapes);
515 while (anIt.More()) {
516 if (anIt.Value().ShapeType() == aST) {
517 aSortedGroup.Add(anIt.Value());
518 aConnectedShapes.Remove(anIt);
525 aGroups.Append(aSortedGroup);
528 TopoDS_Compound aCompound;
529 BRep_Builder aBuilder;
530 aBuilder.MakeCompound(aCompound);
532 for (NCollection_Vector<TopTools_MapOfShape>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
533 const TopTools_MapOfShape& aGroup = anIt.ChangeValue();
534 GeomShapePtr aGeomShape(new GeomAPI_Shape());
535 if(aGroup.Size() == 1) {
536 TopTools_MapOfShape::Iterator aOneShapeIter(aGroup);
537 aGeomShape->setImpl(new TopoDS_Shape(aOneShapeIter.Value()));
539 // make sub-shapes in the group have order same as in original shape
540 TopTools_ListOfShape anOrderedGoup;
541 NCollection_List<TopoDS_Shape>::Iterator anUngrouped(anUngroupedShapes);
542 for (; anUngrouped.More(); anUngrouped.Next()) {
543 if (aGroup.Contains(anUngrouped.Value()))
544 anOrderedGoup.Append(anUngrouped.Value());
546 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(anOrderedGoup)));
547 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
548 GeomAPI_Shape::COMPSOLID,
551 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
554 if(!aCompound.IsNull()) {
555 aResult->setImpl(new TopoDS_Shape(aCompound));
561 //==================================================================================================
562 bool GeomAlgoAPI_ShapeTools::hasSharedTopology(const ListOfShape& theShapes,
563 const GeomAPI_Shape::ShapeType theShapeType)
565 TopTools_IndexedMapOfShape aSubs;
566 for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt) {
567 TopTools_IndexedMapOfShape aCurSubs;
568 TopExp::MapShapes((*anIt)->impl<TopoDS_Shape>(), (TopAbs_ShapeEnum)theShapeType, aCurSubs);
569 for (TopTools_IndexedMapOfShape::Iterator aSubIt(aCurSubs); aSubIt.More(); aSubIt.Next()) {
570 if (aSubs.Contains(aSubIt.Value()))
573 aSubs.Add(aSubIt.Value());
579 //==================================================================================================
580 std::list<std::shared_ptr<GeomAPI_Pnt> >
581 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
583 // Bounding box of all objects.
587 for (ListOfShape::const_iterator
588 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
589 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
590 BRepBndLib::Add(aShape, aBndBox);
593 if(theEnlarge != 0.0) {
594 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
595 aBndBox.Enlarge(theEnlarge);
598 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
599 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
600 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
601 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
603 for(int i = 0; i < 2; i++) {
604 for(int j = 0; j < 2; j++) {
605 for(int k = 0; k < 2; k++) {
606 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
607 aResultPoints.push_back(aPnt);
612 return aResultPoints;
615 //==================================================================================================
616 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
617 const std::shared_ptr<GeomAPI_Shape> thePlane,
618 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
620 std::shared_ptr<GeomAPI_Face> aResultFace;
622 if(!thePlane.get()) {
626 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
627 if(aShape.ShapeType() != TopAbs_FACE) {
631 TopoDS_Face aFace = TopoDS::Face(aShape);
632 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
637 GeomLib_IsPlanarSurface isPlanar(aSurf);
638 if(!isPlanar.IsPlanar()) {
642 if(thePoints.size() != 8) {
646 const gp_Pln& aFacePln = isPlanar.Plan();
647 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
648 IntAna_Quadric aQuadric(aFacePln);
649 Standard_Real UMin, UMax, VMin, VMax;
650 UMin = UMax = VMin = VMax = 0;
651 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
652 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
653 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
654 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
655 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
656 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
657 Standard_Real aPntU(0), aPntV(0);
658 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
659 if(aPntU < UMin) UMin = aPntU;
660 if(aPntU > UMax) UMax = aPntU;
661 if(aPntV < VMin) VMin = aPntV;
662 if(aPntV > VMax) VMax = aPntV;
664 aResultFace.reset(new GeomAPI_Face());
665 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
670 //==================================================================================================
671 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
672 std::shared_ptr<GeomAPI_Vertex>& theV1,
673 std::shared_ptr<GeomAPI_Vertex>& theV2)
675 static GeomVertexPtr aVertex;
677 aVertex = GeomVertexPtr(new GeomAPI_Vertex);
678 aVertex->setImpl(new TopoDS_Vertex());
685 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
686 TopoDS_Vertex aV1, aV2;
687 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
689 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
690 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
691 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
697 //==================================================================================================
698 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
699 const std::shared_ptr<GeomAPI_Dir> theDirection,
700 const ListOfShape& theWires,
701 ListOfShape& theFaces)
703 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
704 theDirection->impl<gp_Dir>()));
705 TopoDS_Face aFace = aMKFace.Face();
707 BRepAlgo_FaceRestrictor aFRestrictor;
708 aFRestrictor.Init(aFace, Standard_False, Standard_True);
709 for(ListOfShape::const_iterator anIt = theWires.cbegin();
710 anIt != theWires.cend();
712 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
713 aFRestrictor.Add(aWire);
716 aFRestrictor.Perform();
718 if(!aFRestrictor.IsDone()) {
722 for(; aFRestrictor.More(); aFRestrictor.Next()) {
723 GeomShapePtr aShape(new GeomAPI_Shape());
724 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
725 theFaces.push_back(aShape);
729 //==================================================================================================
730 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
732 TopoDS_Compound aCompound;
733 BRep_Builder aBuilder;
734 aBuilder.MakeCompound(aCompound);
736 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
737 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
739 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
741 if(aFindPlane.Found() != Standard_True) {
742 return std::shared_ptr<GeomAPI_Pln>();
745 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
746 gp_Pnt aLoc = aPlane->Location();
747 gp_Dir aDir = aPlane->Axis().Direction();
749 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
750 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
752 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
757 //==================================================================================================
758 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
759 const std::shared_ptr<GeomAPI_Shape> theSubShape,
760 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
762 if(!theSubShape.get() || !theBaseShape.get()) {
766 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
767 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
769 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
770 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
771 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
773 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
776 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
777 if(aBaseShape.ShapeType() == TopAbs_FACE) {
778 // Check that edge is on face surface.
779 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
780 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
781 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
783 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
787 // Check intersections.
788 TopoDS_Vertex aV1, aV2;
789 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
790 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
791 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
792 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
793 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
794 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
796 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
797 // Edge intersect face bound. Check that it is not on edge begin or end.
798 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
799 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
800 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
801 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
808 // No intersections found. Edge is inside or outside face. Check it.
809 BRepAdaptor_Curve aCurveAdaptor(anEdge);
810 gp_Pnt aPointToCheck =
811 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
812 aCurveAdaptor.LastParameter()) / 2.0);
813 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
814 ShapeAnalysis_Surface aSAS(aSurface);
815 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
816 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
817 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
831 //==================================================================================================
832 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
834 if(!theShape.get()) {
838 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
839 return (aChecker.IsValid() == Standard_True);
842 //==================================================================================================
843 std::shared_ptr<GeomAPI_Shape>
844 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
846 GeomShapePtr anOuterWire;
848 if(!theFace.get() || !theFace->isFace()) {
852 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
853 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
855 anOuterWire.reset(new GeomAPI_Shape());
856 anOuterWire->setImpl(new TopoDS_Shape(aWire));
861 //==================================================================================================
862 static bool boundaryOfEdge(const std::shared_ptr<GeomAPI_Edge> theEdge,
863 const std::shared_ptr<GeomAPI_Vertex> theVertex,
866 GeomPointPtr aPoint = theVertex->point();
867 GeomPointPtr aFirstPnt = theEdge->firstPoint();
868 double aFirstPntTol = theEdge->firstPointTolerance();
869 GeomPointPtr aLastPnt = theEdge->lastPoint();
870 double aLastPntTol = theEdge->lastPointTolerance();
872 double aFirst, aLast;
873 theEdge->getRange(aFirst, aLast);
875 bool isFirst = aPoint->distance(aFirstPnt) <= aFirstPntTol;
876 bool isLast = aPoint->distance(aLastPnt) <= aLastPntTol;
882 return isFirst != isLast;
885 bool GeomAlgoAPI_ShapeTools::isTangent(const std::shared_ptr<GeomAPI_Edge> theEdge1,
886 const std::shared_ptr<GeomAPI_Edge> theEdge2,
887 const std::shared_ptr<GeomAPI_Vertex> theTgPoint)
889 double aParE1 = 0, aParE2 = 0;
890 if (!boundaryOfEdge(theEdge1, theTgPoint, aParE1) ||
891 !boundaryOfEdge(theEdge2, theTgPoint, aParE2))
894 BRepAdaptor_Curve aC1(theEdge1->impl<TopoDS_Edge>());
895 BRepAdaptor_Curve aC2(theEdge2->impl<TopoDS_Edge>());
896 return BRepLProp::Continuity(aC1, aC2, aParE1, aParE2) >= GeomAbs_G1;
899 //==================================================================================================
900 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
901 const std::shared_ptr<GeomAPI_Face> theFace)
903 if(!theEdge.get() || !theFace.get()) {
907 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
908 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
910 BRepExtrema_ExtCF anExt(anEdge, aFace);
911 return anExt.IsParallel() == Standard_True;
914 //==================================================================================================
915 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
916 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
917 const bool thePointsOutsideFace)
919 std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
920 if(!theEdge.get() || !theFace.get()) {
924 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
925 double aFirstOnCurve, aLastOnCurve;
926 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
928 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
929 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
931 GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
932 if (!anIntAlgo.IsDone())
934 // searching for points-intersection
935 for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
937 if (anIntNum <= anIntAlgo.NbPoints()) {
938 anInt = anIntAlgo.Point(anIntNum);
939 } else { // take the middle point on the segment of the intersection
940 Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
941 anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
943 aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
944 new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
949 //==================================================================================================
950 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
951 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
952 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
954 // to split shape at least one point should be presented in the points container
955 if (thePointsInfo.empty())
958 // General Fuse to split edge by vertices
959 BOPAlgo_Builder aBOP;
960 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
961 // Rebuild closed edge to place vertex to one of split points.
962 // This will prevent edge to be split on same vertex.
963 if (BRep_Tool::IsClosed(aBaseEdge))
965 Standard_Real aFirst, aLast;
966 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
968 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
969 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
970 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
972 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
973 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
974 aBaseEdge.Orientation(anOrientation);
976 aBOP.AddArgument(aBaseEdge);
978 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
979 for (; aPIt != thePointsInfo.end(); ++aPIt) {
980 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
981 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
982 aBOP.AddArgument(aV);
986 if (aBOP.HasErrors())
990 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
991 TopTools_ListIteratorOfListOfShape anIt(aSplits);
992 for (; anIt.More(); anIt.Next()) {
993 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
994 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
995 theShapes.insert(anEdge);
999 //==================================================================================================
1000 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
1001 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
1002 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
1004 // General Fuse to split edge by vertices
1005 BOPAlgo_Builder aBOP;
1006 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
1007 // Rebuild closed edge to place vertex to one of split points.
1008 // This will prevent edge to be split on seam vertex.
1009 if (BRep_Tool::IsClosed(aBaseEdge))
1011 Standard_Real aFirst, aLast;
1012 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
1014 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
1015 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
1017 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
1018 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
1019 aBaseEdge.Orientation(anOrientation);
1021 aBOP.AddArgument(aBaseEdge);
1023 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
1024 for (; aPtIt != thePoints.end(); ++aPtIt) {
1025 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
1026 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
1027 aBOP.AddArgument(aV);
1031 if (aBOP.HasErrors())
1035 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
1036 TopTools_ListIteratorOfListOfShape anIt(aSplits);
1037 for (; anIt.More(); anIt.Next()) {
1038 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
1039 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
1040 theShapes.insert(anEdge);
1044 //==================================================================================================
1045 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
1046 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
1047 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
1049 std::shared_ptr<GeomAPI_Shape> aResultShape;
1051 if (thePoints.size() == 2) {
1052 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
1053 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
1055 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
1057 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
1058 aLast = theShapes.end();
1059 for (; anIt != aLast; anIt++) {
1060 GeomShapePtr aShape = *anIt;
1061 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
1063 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
1064 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
1065 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
1066 anEdgeLastPoint->isEqual(aLastPoint))
1067 aResultShape = aShape;
1072 return aResultShape;
1075 //==================================================================================================
1076 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
1077 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
1078 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
1079 const std::shared_ptr<GeomAPI_Ax1> theAxis)
1081 gp_Pnt aCentreOfMassPoint =
1082 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
1083 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
1084 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
1085 gp_Pnt aPoint = aPrjTool.NearestPoint();
1087 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
1088 aCentreOfMassPoint.Y()-aPoint.Y(),
1089 aCentreOfMassPoint.Z()-aPoint.Z()));
1094 //==================================================================================================
1095 static TopoDS_Wire fixParametricGaps(const TopoDS_Wire& theWire)
1097 TopoDS_Wire aFixedWire;
1098 Handle(Geom_Curve) aPrevCurve;
1099 double aPrevLastParam = 0.0;
1101 BRep_Builder aBuilder;
1102 aBuilder.MakeWire(aFixedWire);
1104 BRepTools_WireExplorer aWExp(theWire);
1105 for (; aWExp.More(); aWExp.Next()) {
1106 TopoDS_Edge anEdge = aWExp.Current();
1107 double aFirst, aLast;
1108 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
1109 if (aCurve == aPrevCurve) {
1110 // if parametric gap occurs, create new edge based on the copied curve
1111 aCurve = Handle(Geom_Curve)::DownCast(aCurve->Copy());
1112 TopoDS_Vertex aV1, aV2;
1113 TopExp::Vertices(anEdge, aV1, aV2);
1114 anEdge = TopoDS::Edge(anEdge.EmptyCopied());
1115 aBuilder.UpdateEdge(anEdge, aCurve, BRep_Tool::Tolerance(anEdge));
1116 aBuilder.Add(anEdge, aV1);
1117 aBuilder.Add(anEdge, aV2);
1120 aBuilder.Add(aFixedWire, anEdge);
1122 aPrevCurve = aCurve;
1123 aPrevLastParam = aLast;
1129 //==================================================================================================
1130 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
1131 const std::shared_ptr<GeomAPI_Wire>& theWire)
1135 TopoDS_Wire aWire = theWire->impl<TopoDS_Wire>();
1136 // Workaround: when concatenate a wire consisting of two edges based on the same B-spline curve
1137 // (non-periodic, but having equal start and end points), first of which is placed at the end
1138 // on the curve and second is placed at the start, this workaround copies second curve to avoid
1139 // treating these edges as a single curve by setting trim parameters.
1140 aWire = fixParametricGaps(aWire);
1141 TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
1142 anEdge = GeomEdgePtr(new GeomAPI_Edge);
1143 anEdge->setImpl(new TopoDS_Edge(aNewEdge));
1148 //==================================================================================================
1149 ListOfShape GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(const GeomShapePtr& theShape)
1151 ListOfShape aSubShapes;
1153 if (!theShape->isCompound() && !theShape->isCompSolid() &&
1154 !theShape->isShell() && !theShape->isWire()) {
1158 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
1159 GeomShapePtr aSubShape = anIt.current();
1160 if (aSubShape->isVertex() || aSubShape->isEdge() ||
1161 aSubShape->isFace() || aSubShape->isSolid()) {
1162 aSubShapes.push_back(aSubShape);
1164 aSubShapes.splice(aSubShapes.end(), getLowLevelSubShapes(aSubShape));
1171 //==================================================================================================
1172 static void getMinMaxPointsOnLine(const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
1173 const gp_Dir theDir,
1174 double& theMin, double& theMax)
1176 theMin = RealLast();
1177 theMax = RealFirst();
1178 // Project bounding points on theDir
1179 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
1180 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
1181 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
1182 gp_Dir aPntDir (aPnt.XYZ());
1183 Standard_Real proj = (theDir*aPntDir) * aPnt.XYZ().Modulus();
1184 if (proj < theMin) theMin = proj;
1185 if (proj > theMax) theMax = proj;
1189 //==================================================================================================
1190 void GeomAlgoAPI_ShapeTools::computeThroughAll(const ListOfShape& theObjects,
1191 const ListOfShape& theBaseShapes,
1192 const std::shared_ptr<GeomAPI_Dir> theDir,
1193 double& theToSize, double& theFromSize)
1195 // Bounding box of objects
1196 std::list<std::shared_ptr<GeomAPI_Pnt> > aBndObjs =
1197 GeomAlgoAPI_ShapeTools::getBoundingBox(theObjects);
1198 if (aBndObjs.size() != 8) {
1204 // One direction for all prisms
1205 gp_Dir aDir = theDir->impl<gp_Dir>();
1207 // Bounding box of the base
1208 std::list<std::shared_ptr<GeomAPI_Pnt> > aBndBases =
1209 GeomAlgoAPI_ShapeTools::getBoundingBox(theBaseShapes);
1210 if (aBndBases.size() != 8) {
1215 Standard_Real lowBnd, upperBnd;
1216 getMinMaxPointsOnLine(aBndObjs, aDir, lowBnd, upperBnd);
1219 Standard_Real lowBase, upperBase;
1220 getMinMaxPointsOnLine(aBndBases, aDir, lowBase, upperBase);
1222 // ----------.-----.---------.--------------.-----------> theDir
1223 // lowBnd lowBase upperBase upperBnd
1225 theToSize = upperBnd - lowBase;
1226 theFromSize = upperBase - lowBnd;
1228 // Direction is a normal to each base shape (different normals to bases)
1229 // So we calculate own sizes for each base shape
1233 for (ListOfShape::const_iterator anIt = theBaseShapes.begin();
1234 anIt != theBaseShapes.end(); ++anIt) {
1235 const GeomShapePtr& aBaseShape_i = (*anIt);
1236 ListOfShape aBaseShapes_i;
1237 aBaseShapes_i.push_back(aBaseShape_i);
1239 // Bounding box of the base
1240 std::list<std::shared_ptr<GeomAPI_Pnt> > aBndBases =
1241 GeomAlgoAPI_ShapeTools::getBoundingBox(aBaseShapes_i);
1242 if (aBndBases.size() != 8) {
1246 // Direction (normal to aBaseShapes_i)
1247 // Code like in GeomAlgoAPI_Prism
1249 const TopoDS_Shape& aBaseShape = aBaseShape_i->impl<TopoDS_Shape>();
1250 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
1251 if (aFindPlane.Found() == Standard_True) {
1252 Handle(Geom_Plane) aPlane;
1253 if (aBaseShape.ShapeType() == TopAbs_FACE || aBaseShape.ShapeType() == TopAbs_SHELL) {
1254 TopExp_Explorer anExp(aBaseShape, TopAbs_FACE);
1255 const TopoDS_Shape& aFace = anExp.Current();
1256 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(TopoDS::Face(aFace));
1257 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1258 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
1259 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
1260 aSurface = aTrimSurface->BasisSurface();
1262 if(aSurface->DynamicType() != STANDARD_TYPE(Geom_Plane)) {
1265 aPlane = Handle(Geom_Plane)::DownCast(aSurface);
1267 aPlane = aFindPlane.Plane();
1269 aDir = aPlane->Axis().Direction();
1275 Standard_Real lowBnd, upperBnd;
1276 getMinMaxPointsOnLine(aBndObjs, aDir, lowBnd, upperBnd);
1279 Standard_Real lowBase, upperBase;
1280 getMinMaxPointsOnLine(aBndBases, aDir, lowBase, upperBase);
1282 // ----------.-----.---------.--------------.-----------> theDir
1283 // lowBnd lowBase upperBase upperBnd
1285 double aToSize_i = upperBnd - lowBase;
1286 double aFromSize_i = upperBase - lowBnd;
1288 if (aToSize_i > theToSize) theToSize = aToSize_i;
1289 if (aFromSize_i > theFromSize) theFromSize = aFromSize_i;