From 24dfb2d72788ace01a99049a0e9d941680acba85 Mon Sep 17 00:00:00 2001 From: azv Date: Thu, 20 Dec 2018 12:51:14 +0300 Subject: [PATCH] [Code coverage GeomAlgoAPI]: Call static methods of builders --- src/GeomAPI/CMakeLists.txt | 1 + src/GeomAPI/Test/TestBuilders.py | 98 ++++++++++++++++++++ src/GeomAlgoAPI/GeomAlgoAPI.i | 2 + src/GeomAlgoAPI/GeomAlgoAPI_CurveBuilder.cpp | 18 ++-- src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp | 31 ++++--- src/GeomAlgoAPI/GeomAlgoAPI_swig.h | 2 + 6 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 src/GeomAPI/Test/TestBuilders.py diff --git a/src/GeomAPI/CMakeLists.txt b/src/GeomAPI/CMakeLists.txt index 28088abb4..fc20e83df 100644 --- a/src/GeomAPI/CMakeLists.txt +++ b/src/GeomAPI/CMakeLists.txt @@ -162,6 +162,7 @@ INSTALL(FILES ${SWIG_SCRIPTS} DESTINATION ${SHAPER_INSTALL_SWIG}) ADD_UNIT_TESTS( TestAx123.py TestBox.py + TestBuilders.py TestCone.py TestCylinder.py TestEllipse2d.py diff --git a/src/GeomAPI/Test/TestBuilders.py b/src/GeomAPI/Test/TestBuilders.py new file mode 100644 index 000000000..7e6f96ffa --- /dev/null +++ b/src/GeomAPI/Test/TestBuilders.py @@ -0,0 +1,98 @@ +## Copyright (C) 2018-20xx CEA/DEN, EDF R&D +## +## This library is free software; you can redistribute it and/or +## modify it under the terms of the GNU Lesser General Public +## License as published by the Free Software Foundation; either +## version 2.1 of the License, or (at your option) any later version. +## +## This library is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## Lesser General Public License for more details. +## +## You should have received a copy of the GNU Lesser General Public +## License along with this library; if not, write to the Free Software +## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +## +## See http:##www.salome-platform.org/ or +## email : webmaster.salome@opencascade.com +## + +import math + +from GeomAlgoAPI import GeomAlgoAPI_Circ2dBuilder as circBuilder +from GeomAlgoAPI import GeomAlgoAPI_CurveBuilder as curveBuilder +from GeomAlgoAPI import GeomAlgoAPI_EdgeBuilder as edgeBuilder +from GeomAlgoAPI import GeomAlgoAPI_FaceBuilder as faceBuilder +from GeomAlgoAPI import GeomAlgoAPI_ShapeTools as tools +from GeomAlgoAPI import GeomAlgoAPI_MakeVolume as makeVolume +from GeomAPI import GeomAPI_Ax3 as ax3 +from GeomAPI import GeomAPI_Dir as dir +from GeomAPI import GeomAPI_Pnt as pnt +from GeomAPI import GeomAPI_Pnt2d as pnt2d +from GeomAPI import GeomAPI_Circ2d +from GeomAPI import GeomAPI_Shape + +TOLERANCE = 1.e-7 + +center = pnt(10, 0, 0) +normal = dir(0, 0, 1) +xAxis = dir(1, 0, 0) +majorRadius = 10 +minorRadius = 5 + +# Test 1. Elliptical edge +ellipseEdge = edgeBuilder.ellipse(center, normal, xAxis, majorRadius, minorRadius) +ellipse = ellipseEdge.ellipse() +assert(ellipse is not None) +assert(ellipse.center().distance(center) < TOLERANCE) +assert(ellipse.firstFocus().distance(pnt(18.66025404, 0., 0.)) < TOLERANCE) +assert(ellipse.secondFocus().distance(pnt(1.339745962, 0., 0.)) < TOLERANCE) +assert(ellipse.majorRadius() == majorRadius) +assert(ellipse.minorRadius() == minorRadius) + +# Test 2. Square face +size = 100. +squareFace = faceBuilder.squareFace(center, normal, size) +assert(squareFace.middlePoint().distance(center) < TOLERANCE) + +# Test 3. Check are of the face +assert(tools.area(squareFace) == size**2) + +# Test 4. Check properties of wrong shapes +assert(tools.volume(None) == 0) +assert(tools.volume(GeomAPI_Shape()) == 0) +assert(tools.area(None) == 0) +assert(tools.area(GeomAPI_Shape()) == 0) +assert(tools.centreOfMass(None) is None) +assert(tools.centreOfMass(GeomAPI_Shape()) is None) + +# Test 5. Curve with duplicated points +points = [pnt(10, 0, 0), + pnt(20, 10, 0), + pnt(20, 10, 0), + pnt(10, 10, 0), + pnt(10, 0, 0)] +curve = curveBuilder.edge(points, True, True, None, None) +assert(curve is not None) + +# Test 6. Circle by center and radius +center = pnt2d(5, 0) +radius = 5 +plane = ax3(pnt(0, 0, 0), dir(0, 0, 1), dir(1, 0, 0)) +circleBuilder = circBuilder(plane) +circleBuilder.setCenter(center) +circleBuilder.setRadius(radius) +circle = circBuilder.circle(circleBuilder) +assert(circle is not None) +assert(circle.center().distance(center) < TOLERANCE) +assert(circle.radius() == radius) + +# Test 7. Circle by 3 points +p1 = pnt2d(center.x() + radius, center.y()) +p2 = pnt2d(center.x() - radius, center.y()) +p3 = pnt2d(center.x(), center.y() + radius) +circle = circBuilder.circle(p1, p2, p3) +assert(circle is not None) +assert(circle.center().distance(center) < TOLERANCE) +assert(circle.radius() == radius) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI.i b/src/GeomAlgoAPI/GeomAlgoAPI.i index 71c5a05db..a6ce40e6b 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI.i +++ b/src/GeomAlgoAPI/GeomAlgoAPI.i @@ -61,7 +61,9 @@ // all supported interfaces %include "GeomAlgoAPI_MakeShape.h" %include "GeomAlgoAPI_Boolean.h" +%include "GeomAlgoAPI_Circ2dBuilder.h" %include "GeomAlgoAPI_CompoundBuilder.h" +%include "GeomAlgoAPI_CurveBuilder.h" %include "GeomAlgoAPI_DFLoader.h" %include "GeomAlgoAPI_EdgeBuilder.h" %include "GeomAlgoAPI_FaceBuilder.h" diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_CurveBuilder.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_CurveBuilder.cpp index 04966cbd6..ae445a204 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_CurveBuilder.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_CurveBuilder.cpp @@ -35,7 +35,7 @@ #include -static void reorder(Handle(TColgp_HArray1OfPnt) thePoints); +static void reorder(Handle(TColgp_HArray1OfPnt)& thePoints); //================================================================================================= GeomEdgePtr GeomAlgoAPI_CurveBuilder::edge(const std::list& thePoints, @@ -52,17 +52,17 @@ GeomEdgePtr GeomAlgoAPI_CurveBuilder::edge(const std::list& thePoi aPoints->SetValue(i, aPoint->impl()); } - // Reorder points if required - if (theIsToReorder) { - reorder(aPoints); - } - // If the curve to be closed - remove last point if it is too close to the first one bool isClose = aPoints->First().Distance(aPoints->Last()) <= gp::Resolution(); if (isClose && theIsClosed) { aPoints->Resize(aPoints->Lower(), aPoints->Upper() - 1, Standard_True); } + // Reorder points if required + if (theIsToReorder) { + reorder(aPoints); + } + // Initialize interpolator GeomAPI_Interpolate anInterp(aPoints, theIsClosed, gp::Resolution()); @@ -94,7 +94,7 @@ GeomEdgePtr GeomAlgoAPI_CurveBuilder::edge(const std::list& thePoi } //================ Auxiliary functions ======================================================== -void reorder(Handle(TColgp_HArray1OfPnt) thePoints) +void reorder(Handle(TColgp_HArray1OfPnt)& thePoints) { if (thePoints->Length() < 3) { return; @@ -103,7 +103,7 @@ void reorder(Handle(TColgp_HArray1OfPnt) thePoints) int aNbPoints = thePoints->Length(); int aNbDup = 0; gp_Pnt aPrevPnt = thePoints->Value(1); - for (int i = 1; i < aNbPoints - 1; i++) { + for (int i = 1; i < aNbPoints; i++) { gp_Pnt aPnt = thePoints->Value(i); int aNearest = 0; double aMinDist = RealLast(); @@ -118,7 +118,7 @@ void reorder(Handle(TColgp_HArray1OfPnt) thePoints) // Keep given order of points to use it in case of equidistant candidates // .-<---<-. // / \ - // o o o c o->o->o->o->n o o + // o o o c o->o->o->o->n o o // | | | // i i+1 nearest gp_Pnt aNearestPnt = thePoints->Value(aNearest); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp index 598053313..48e3b6cf5 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp @@ -614,23 +614,26 @@ void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr the std::shared_ptr& theV1, std::shared_ptr& theV2) { - if(!theShape.get()) { - std::shared_ptr aVertex(new GeomAPI_Vertex); + static GeomVertexPtr aVertex; + if (!aVertex) { + aVertex = GeomVertexPtr(new GeomAPI_Vertex); aVertex->setImpl(new TopoDS_Vertex()); - theV1 = aVertex; - theV2 = aVertex; - return; } - const TopoDS_Shape& aShape = theShape->impl(); - TopoDS_Vertex aV1, aV2; - ShapeAnalysis::FindBounds(aShape, aV1, aV2); - - std::shared_ptr aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex()); - aGeomV1->setImpl(new TopoDS_Vertex(aV1)); - aGeomV2->setImpl(new TopoDS_Vertex(aV2)); - theV1 = aGeomV1; - theV2 = aGeomV2; + theV1 = aVertex; + theV2 = aVertex; + + if (theShape) { + const TopoDS_Shape& aShape = theShape->impl(); + TopoDS_Vertex aV1, aV2; + ShapeAnalysis::FindBounds(aShape, aV1, aV2); + + std::shared_ptr aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex()); + aGeomV1->setImpl(new TopoDS_Vertex(aV1)); + aGeomV2->setImpl(new TopoDS_Vertex(aV2)); + theV1 = aGeomV1; + theV2 = aGeomV2; + } } //================================================================================================== diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_swig.h b/src/GeomAlgoAPI/GeomAlgoAPI_swig.h index 8c9a49d70..9564d5b74 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_swig.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_swig.h @@ -26,7 +26,9 @@ #include "GeomAlgoAPI.h" #include "GeomAlgoAPI_MakeShape.h" #include "GeomAlgoAPI_Boolean.h" + #include "GeomAlgoAPI_Circ2dBuilder.h" #include "GeomAlgoAPI_CompoundBuilder.h" + #include "GeomAlgoAPI_CurveBuilder.h" #include "GeomAlgoAPI_DFLoader.h" #include "GeomAlgoAPI_EdgeBuilder.h" #include "GeomAlgoAPI_FaceBuilder.h" -- 2.39.2