Salome HOME
22ee6e03ac80c375ecfec1aae33d18721f026782
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_NormalToFace.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 "GeomAlgoAPI_NormalToFace.h"
21
22 #include <TopoDS.hxx>
23 #include <TopoDS_Face.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS_Shape.hxx>
26
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>
33
34 #include <ShapeAnalysis_Surface.hxx>
35
36 #include <Geom_Plane.hxx>
37 #include <GeomAPI_Edge.h>
38 #include <GeomLProp_SLProps.hxx>
39 #include <GProp_GProps.hxx>
40
41 #include <gp_Ax3.hxx>
42 #include <gp_Pln.hxx>
43
44  /*!
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.
48    */
49 TopAbs_ShapeEnum GetTypeOfSimplePart(const TopoDS_Shape& theShape)
50 {
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);
59     if (It.More()) {
60       return GetTypeOfSimplePart(It.Value());
61     }
62   }
63   return TopAbs_SHAPE;
64 }
65
66  /*!
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.
70    *
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.
74    */
75 gp_Ax3 GetPosition(const TopoDS_Shape& theShape)
76 {
77   gp_Ax3 aResult;
78
79   if (theShape.IsNull())
80     return aResult;
81
82   // Axes
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);
97       }
98     }
99   }
100
101   // Origin
102   gp_Pnt aPnt;
103
104   TopAbs_ShapeEnum aShType = theShape.ShapeType();
105
106   if (aShType == TopAbs_VERTEX) {
107     aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
108   } else {
109     if (aShType == TopAbs_COMPOUND) {
110       aShType = GetTypeOfSimplePart(theShape);
111     }
112
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);
118     else
119       BRepGProp::VolumeProperties(theShape, aSystem);
120
121     aPnt = aSystem.CentreOfMass();
122   }
123
124   aResult.SetLocation(aPnt);
125
126   return aResult;
127 }
128
129 //=================================================================================================
130 bool GeomAlgoAPI_NormalToFace::normal(GeomShapePtr theFace,
131                                       GeomShapePtr thePoint,
132                                       GeomEdgePtr  theNormal,
133                                       std::string& theError)
134 {
135   #ifdef _DEBUG
136   std::cout << "GeomAlgoAPI_NormalToFace::normal" << std::endl;
137   #endif
138
139   if (!theFace.get()) {
140     theError = "Face for normale calculation is null";
141     return false;
142   }
143
144   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
145
146   if (aShape.ShapeType() != TopAbs_FACE) {
147     theError = "Shape for normale calculation is not a face";
148     return false;
149   }
150
151   TopoDS_Face aFace = TopoDS::Face(aShape);
152
153   // Point
154   gp_Pnt p1 (0,0,0);
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);
164
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";
170       return false;
171     }
172     p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
173     pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
174   }
175
176   // Normal direction
177   gp_Vec Vec1,Vec2;
178   BRepAdaptor_Surface SF (aFace);
179   SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
180   if (Vec1.Magnitude() < Precision::Confusion()) {
181     gp_Vec tmpV;
182     gp_Pnt tmpP;
183     SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
184   }
185   else if (Vec2.Magnitude() < Precision::Confusion()) {
186     gp_Vec tmpV;
187     gp_Pnt tmpP;
188     SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
189   }
190
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");
195
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;
206       }
207     } else {
208       if (Abs(radius2) > Precision::Confusion()) {
209         radius = 1.0 / radius2;
210       }
211     }
212   }
213
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()) {
217     Bnd_Box B;
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;
222   }
223
224   if (radius < Precision::Confusion())
225     radius = 1.0;
226
227   V *= radius / mod;
228
229   // consider the face orientation
230   if (aFace.Orientation() == TopAbs_REVERSED ||
231       aFace.Orientation() == TopAbs_INTERNAL) {
232     V = - V;
233   }
234
235   // Edge
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();
241
242   theNormal->setImpl(new TopoDS_Shape(aShape));
243
244   return true;
245 }
246