Salome HOME
2028b990249786635697660a4d2bbf4782fbef26
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_NormalToFace.cpp
1 // Copyright (C) 2014-2021  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 "GeomAlgoAPI_NormalToFace.h"
21
22 #include <TopoDS_Shape.hxx>
23 #include <TopoDS_Face.hxx>
24 #include <TopoDS.hxx>
25 #include <BRep_Tool.hxx>
26 #include <gp_Ax3.hxx>
27 #include <Geom_Plane.hxx>
28 #include <gp_Pln.hxx>
29 #include <TopoDS_Iterator.hxx>
30 #include <GProp_GProps.hxx>
31 #include <BRepGProp.hxx>
32 #include <ShapeAnalysis.hxx>
33 #include <ShapeAnalysis_Surface.hxx>
34 #include <BRepAdaptor_Surface.hxx>
35 #include <BRepBndLib.hxx>
36 #include <GeomLProp_SLProps.hxx>
37 #include <BRepBuilderAPI_MakeEdge.hxx>
38 #include <GeomAPI_Edge.h>
39
40  /*!
41    * \brief Return type of shape for explode. In case of compound it will be a type of its first sub shape.
42    * \param theShape The shape to get type of.
43    * \retval TopAbs_ShapeEnum Return type of shape for explode.
44    */
45 TopAbs_ShapeEnum GetTypeOfSimplePart(const TopoDS_Shape& theShape)
46 {
47   TopAbs_ShapeEnum aType = theShape.ShapeType();
48   if      (aType == TopAbs_VERTEX)                             return TopAbs_VERTEX;
49   else if (aType == TopAbs_EDGE  || aType == TopAbs_WIRE)      return TopAbs_EDGE;
50   else if (aType == TopAbs_FACE  || aType == TopAbs_SHELL)     return TopAbs_FACE;
51   else if (aType == TopAbs_SOLID || aType == TopAbs_COMPSOLID) return TopAbs_SOLID;
52   else if (aType == TopAbs_COMPOUND) {
53     // Only the iType of the first shape in the compound is taken into account
54     TopoDS_Iterator It (theShape, Standard_False, Standard_False);
55     if (It.More()) {
56       return GetTypeOfSimplePart(It.Value());
57     }
58   }
59   return TopAbs_SHAPE;
60 }
61
62  /*!
63    * \brief Get Local Coordinate System, corresponding to the given shape.
64    * \param theShape The shape to get type of.
65    * \retval gp_Ax3 Return axis.
66    *
67    * Origin of the LCS is situated at the shape's center of mass.
68    * Axes of the LCS are obtained from shape's location or,
69    * if the shape is a planar face, from position of its plane.
70    */
71 gp_Ax3 GetPosition(const TopoDS_Shape& theShape)
72 {
73   gp_Ax3 aResult;
74
75   if (theShape.IsNull())
76     return aResult;
77
78   // Axes
79   aResult.Transform(theShape.Location().Transformation());
80   if (theShape.ShapeType() == TopAbs_FACE) {
81     Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(theShape));
82     if (!aGS.IsNull() && aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
83       Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast(aGS);
84       gp_Pln aPln = aGPlane->Pln();
85       aResult = aPln.Position();
86       // In case of reverse orinetation of the face invert the plane normal
87       // (the face's normal does not mathc the plane's normal in this case)
88       if (theShape.Orientation() == TopAbs_REVERSED) {
89         gp_Dir Vx =  aResult.XDirection();
90         gp_Dir N  =  aResult.Direction().Mirrored(Vx);
91         gp_Pnt P  =  aResult.Location();
92         aResult = gp_Ax3(P, N, Vx);
93       }
94     }
95   }
96
97   // Origin
98   gp_Pnt aPnt;
99
100   TopAbs_ShapeEnum aShType = theShape.ShapeType();
101
102   if (aShType == TopAbs_VERTEX) {
103     aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
104   } else {
105     if (aShType == TopAbs_COMPOUND) {
106       aShType = GetTypeOfSimplePart(theShape);
107     }
108
109     GProp_GProps aSystem;
110     if (aShType == TopAbs_EDGE || aShType == TopAbs_WIRE)
111       BRepGProp::LinearProperties(theShape, aSystem);
112     else if (aShType == TopAbs_FACE || aShType == TopAbs_SHELL)
113       BRepGProp::SurfaceProperties(theShape, aSystem);
114     else
115       BRepGProp::VolumeProperties(theShape, aSystem);
116
117     aPnt = aSystem.CentreOfMass();
118   }
119
120   aResult.SetLocation(aPnt);
121
122   return aResult;
123 }
124
125 //=================================================================================================
126 bool GeomAlgoAPI_NormalToFace::normal(GeomShapePtr theFace,
127                                       GeomShapePtr thePoint,
128                                       GeomEdgePtr  theNormal,
129                                       std::string& theError)
130 {
131   #ifdef _DEBUG
132   std::cout << "GeomAlgoAPI_NormalToFace::normal" << std::endl;
133   #endif
134
135   if (!theFace.get()) {
136     theError = "Face for normale calculation is null";
137     return false;
138   }
139
140   TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
141
142   if (aShape.ShapeType() != TopAbs_FACE) {
143     theError = "Shape for normale calculation is not a face";
144     return false;
145   }
146
147   TopoDS_Face aFace = TopoDS::Face(aShape);
148
149   // Point
150   gp_Pnt p1 (0,0,0);
151   // Point parameters on surface
152   double u1, u2, v1, v2;
153   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
154   Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
155   gp_Ax3 aPos = GetPosition(aFace);
156   p1 = aPos.Location();
157   // Set default starting point using UV bounds
158   ShapeAnalysis::GetFaceUVBounds(aFace, u1, u2, v1, v2);
159   gp_Pnt2d pUV ((u2 + u1) * 0.5, (v2 + v1) * 0.5);
160
161   // Change to Vertex coord if selected
162   if (thePoint.get()) {
163     TopoDS_Shape anOptPnt = thePoint->impl<TopoDS_Shape>();
164     if (anOptPnt.IsNull()) {
165       theError = "Invalid shape given for point argument";
166       return false;
167     }
168     p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
169     pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
170   }
171
172   // Normal direction
173   gp_Vec Vec1,Vec2;
174   BRepAdaptor_Surface SF (aFace);
175   SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
176   if (Vec1.Magnitude() < Precision::Confusion()) {
177     gp_Vec tmpV;
178     gp_Pnt tmpP;
179     SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
180   }
181   else if (Vec2.Magnitude() < Precision::Confusion()) {
182     gp_Vec tmpV;
183     gp_Pnt tmpP;
184     SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
185   }
186
187   gp_Vec V = Vec1.Crossed(Vec2);
188   Standard_Real mod = V.Magnitude();
189   if (mod < Precision::Confusion())
190     Standard_NullObject::Raise("Normal vector of a face has null magnitude");
191
192   // Set length of normal vector to average radius of curvature
193   Standard_Real radius = 0.0;
194   GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
195   if (aProperties.IsCurvatureDefined()) {
196     Standard_Real radius1 = Abs(aProperties.MinCurvature());
197     Standard_Real radius2 = Abs(aProperties.MaxCurvature());
198     if (Abs(radius1) > Precision::Confusion()) {
199       radius = 1.0 / radius1;
200       if (Abs(radius2) > Precision::Confusion()) {
201         radius = (radius + 1.0 / radius2) / 2.0;
202       }
203     } else {
204       if (Abs(radius2) > Precision::Confusion()) {
205         radius = 1.0 / radius2;
206       }
207     }
208   }
209
210   // Set length of normal vector to average dimension of the face
211   // (only if average radius of curvature is not appropriate)
212   if (radius < Precision::Confusion()) {
213     Bnd_Box B;
214     Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
215     BRepBndLib::Add(aFace, B);
216     B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
217     radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
218   }
219
220   if (radius < Precision::Confusion())
221     radius = 1.0;
222
223   V *= radius / mod;
224
225   // consider the face orientation
226   if (aFace.Orientation() == TopAbs_REVERSED ||
227       aFace.Orientation() == TopAbs_INTERNAL) {
228     V = - V;
229   }
230
231   // Edge
232   gp_Pnt p2 = p1.Translated(V);
233   BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
234   if (!aBuilder.IsDone())
235     Standard_NullObject::Raise("Vector construction failed");
236   aShape = aBuilder.Shape();
237
238   theNormal->setImpl(new TopoDS_Shape(aShape));
239
240   return true;
241 }
242