1 // Copyright (C) 2014-2024 CEA, EDF
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_NormalToFace.h"
23 #include <TopoDS_Face.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS_Shape.hxx>
27 #include <BRep_Tool.hxx>
28 #include <BRepAdaptor_Surface.hxx>
29 #include <BRepBndLib.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRepGProp.hxx>
32 #include <BRepTools.hxx>
34 #include <ShapeAnalysis_Surface.hxx>
36 #include <Geom_Plane.hxx>
37 #include <GeomAPI_Edge.h>
38 #include <GeomLProp_SLProps.hxx>
39 #include <GProp_GProps.hxx>
45 * \brief Return type of shape for explode. In case of compound it will be a type of its first sub shape.
46 * \param theShape The shape to get type of.
47 * \retval TopAbs_ShapeEnum Return type of shape for explode.
49 TopAbs_ShapeEnum GetTypeOfSimplePart(const TopoDS_Shape& theShape)
51 TopAbs_ShapeEnum aType = theShape.ShapeType();
52 if (aType == TopAbs_VERTEX) return TopAbs_VERTEX;
53 else if (aType == TopAbs_EDGE || aType == TopAbs_WIRE) return TopAbs_EDGE;
54 else if (aType == TopAbs_FACE || aType == TopAbs_SHELL) return TopAbs_FACE;
55 else if (aType == TopAbs_SOLID || aType == TopAbs_COMPSOLID) return TopAbs_SOLID;
56 else if (aType == TopAbs_COMPOUND) {
57 // Only the iType of the first shape in the compound is taken into account
58 TopoDS_Iterator It (theShape, Standard_False, Standard_False);
60 return GetTypeOfSimplePart(It.Value());
67 * \brief Get Local Coordinate System, corresponding to the given shape.
68 * \param theShape The shape to get type of.
69 * \retval gp_Ax3 Return axis.
71 * Origin of the LCS is situated at the shape's center of mass.
72 * Axes of the LCS are obtained from shape's location or,
73 * if the shape is a planar face, from position of its plane.
75 gp_Ax3 GetPosition(const TopoDS_Shape& theShape)
79 if (theShape.IsNull())
83 aResult.Transform(theShape.Location().Transformation());
84 if (theShape.ShapeType() == TopAbs_FACE) {
85 Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(theShape));
86 if (!aGS.IsNull() && aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
87 Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast(aGS);
88 gp_Pln aPln = aGPlane->Pln();
89 aResult = aPln.Position();
90 // In case of reverse orinetation of the face invert the plane normal
91 // (the face's normal does not mathc the plane's normal in this case)
92 if (theShape.Orientation() == TopAbs_REVERSED) {
93 gp_Dir Vx = aResult.XDirection();
94 gp_Dir N = aResult.Direction().Mirrored(Vx);
95 gp_Pnt P = aResult.Location();
96 aResult = gp_Ax3(P, N, Vx);
104 TopAbs_ShapeEnum aShType = theShape.ShapeType();
106 if (aShType == TopAbs_VERTEX) {
107 aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
109 if (aShType == TopAbs_COMPOUND) {
110 aShType = GetTypeOfSimplePart(theShape);
113 GProp_GProps aSystem;
114 if (aShType == TopAbs_EDGE || aShType == TopAbs_WIRE)
115 BRepGProp::LinearProperties(theShape, aSystem);
116 else if (aShType == TopAbs_FACE || aShType == TopAbs_SHELL)
117 BRepGProp::SurfaceProperties(theShape, aSystem);
119 BRepGProp::VolumeProperties(theShape, aSystem);
121 aPnt = aSystem.CentreOfMass();
124 aResult.SetLocation(aPnt);
129 //=================================================================================================
130 bool GeomAlgoAPI_NormalToFace::normal(GeomShapePtr theFace,
131 GeomShapePtr thePoint,
132 GeomEdgePtr theNormal,
133 std::string& theError)
136 std::cout << "GeomAlgoAPI_NormalToFace::normal" << std::endl;
139 if (!theFace.get()) {
140 theError = "Face for normale calculation is null";
144 TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
146 if (aShape.ShapeType() != TopAbs_FACE) {
147 theError = "Shape for normale calculation is not a face";
151 TopoDS_Face aFace = TopoDS::Face(aShape);
155 // Point parameters on surface
156 double u1, u2, v1, v2;
157 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
158 Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
159 gp_Ax3 aPos = GetPosition(aFace);
160 p1 = aPos.Location();
161 // Set default starting point using UV bounds
162 BRepTools::UVBounds(aFace, u1, u2, v1, v2);
163 gp_Pnt2d pUV ((u2 + u1) * 0.5, (v2 + v1) * 0.5);
165 // Change to Vertex coord if selected
166 if (thePoint.get()) {
167 TopoDS_Shape anOptPnt = thePoint->impl<TopoDS_Shape>();
168 if (anOptPnt.IsNull()) {
169 theError = "Invalid shape given for point argument";
172 p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
173 pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
178 BRepAdaptor_Surface SF (aFace);
179 SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
180 if (Vec1.Magnitude() < Precision::Confusion()) {
183 SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
185 else if (Vec2.Magnitude() < Precision::Confusion()) {
188 SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
191 gp_Vec V = Vec1.Crossed(Vec2);
192 Standard_Real mod = V.Magnitude();
193 if (mod < Precision::Confusion())
194 Standard_NullObject::Raise("Normal vector of a face has null magnitude");
196 // Set length of normal vector to average radius of curvature
197 Standard_Real radius = 0.0;
198 GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
199 if (aProperties.IsCurvatureDefined()) {
200 Standard_Real radius1 = Abs(aProperties.MinCurvature());
201 Standard_Real radius2 = Abs(aProperties.MaxCurvature());
202 if (Abs(radius1) > Precision::Confusion()) {
203 radius = 1.0 / radius1;
204 if (Abs(radius2) > Precision::Confusion()) {
205 radius = (radius + 1.0 / radius2) / 2.0;
208 if (Abs(radius2) > Precision::Confusion()) {
209 radius = 1.0 / radius2;
214 // Set length of normal vector to average dimension of the face
215 // (only if average radius of curvature is not appropriate)
216 if (radius < Precision::Confusion()) {
218 Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
219 BRepBndLib::Add(aFace, B);
220 B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
221 radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
224 if (radius < Precision::Confusion())
229 // consider the face orientation
230 if (aFace.Orientation() == TopAbs_REVERSED ||
231 aFace.Orientation() == TopAbs_INTERNAL) {
236 gp_Pnt p2 = p1.Translated(V);
237 BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
238 if (!aBuilder.IsDone())
239 Standard_NullObject::Raise("Vector construction failed");
240 aShape = aBuilder.Shape();
242 theNormal->setImpl(new TopoDS_Shape(aShape));