Salome HOME
527f0aceb7f93fa13e25f4737e187f5d9edcf970
[modules/shaper.git] / src / GeomAPI / GeomAPI_Vertex.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include<GeomAPI_Vertex.h>
21 #include<GeomAPI_Pnt.h>
22
23 #include <TopoDS_Shape.hxx>
24 #include <TopoDS_Edge.hxx>
25 #include <TopoDS.hxx>
26 #include <TopoDS_Vertex.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRep_Builder.hxx>
29 #include <gp_Pnt.hxx>
30 #include <Precision.hxx>
31
32 static TopoDS_Shape* buildVertex(const gp_Pnt& thePoint)
33 {
34   TopoDS_Vertex aVertex;
35   BRep_Builder aBuilder;
36   aBuilder.MakeVertex(aVertex, thePoint, Precision::Confusion());
37   return new TopoDS_Shape(aVertex);
38 }
39
40 GeomAPI_Vertex::GeomAPI_Vertex()
41   : GeomAPI_Shape()
42 {
43 }
44
45 GeomAPI_Vertex::GeomAPI_Vertex(const std::shared_ptr<GeomAPI_Shape>& theShape)
46 {
47   if (!theShape->isNull() && theShape->isVertex()) {
48     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
49   }
50 }
51
52 GeomAPI_Vertex::GeomAPI_Vertex(const std::shared_ptr<GeomAPI_Pnt>& thePoint)
53 {
54   setImpl(buildVertex(thePoint->impl<gp_Pnt>()));
55 }
56
57 GeomAPI_Vertex::GeomAPI_Vertex(double theX, double theY, double theZ)
58 {
59   setImpl(buildVertex(gp_Pnt(theX, theY, theZ)));
60 }
61
62 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Vertex::point()
63 {
64   const TopoDS_Shape& aShape = const_cast<GeomAPI_Vertex*>(this)->impl<TopoDS_Shape>();
65   TopoDS_Vertex aVertex = TopoDS::Vertex(aShape);
66   gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
67   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
68 }
69
70 bool GeomAPI_Vertex::isEqual(const std::shared_ptr<GeomAPI_Shape> theVert) const
71 {
72   if (!theVert.get() || ! theVert->isVertex())
73     return false;
74   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Vertex*>(this)->impl<TopoDS_Shape>();
75   const TopoDS_Shape& aInShape = theVert->impl<TopoDS_Shape>();
76
77   if (aMyShape.ShapeType() != aInShape.ShapeType())
78     return false;
79
80   TopoDS_Vertex aVertex1 = TopoDS::Vertex(aMyShape);
81   gp_Pnt aPoint1 = BRep_Tool::Pnt(aVertex1);
82
83   TopoDS_Vertex aVertex2 = TopoDS::Vertex(aInShape);
84   gp_Pnt aPoint2 = BRep_Tool::Pnt(aVertex2);
85
86   return aPoint1.IsEqual(aPoint2, Precision::Confusion()) == Standard_True;
87 }
88
89
90
91 bool GeomAPI_Vertex::GeometricComparator::operator()(const GeomVertexPtr& theVertex1,
92                                                      const GeomVertexPtr& theVertex2) const
93 {
94   const TopoDS_Vertex& aVertex1 = theVertex1->impl<TopoDS_Vertex>();
95   const TopoDS_Vertex& aVertex2 = theVertex2->impl<TopoDS_Vertex>();
96
97   gp_Pnt aPnt1 = BRep_Tool::Pnt(aVertex1);
98   gp_Pnt aPnt2 = BRep_Tool::Pnt(aVertex2);
99
100   bool isLess = aPnt1.X() + myTolerance < aPnt2.X();
101   if (!isLess && aPnt1.X() <= aPnt2.X() + myTolerance) {
102     isLess = aPnt1.Y() + myTolerance < aPnt2.Y();
103     if (!isLess && aPnt1.Y() <= aPnt2.Y() + myTolerance)
104       isLess = aPnt1.Z() + myTolerance < aPnt2.Z();
105   }
106   return isLess;
107 }