1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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"
22 #include <TopoDS_Shape.hxx>
23 #include <TopoDS_Face.hxx>
25 #include <BRep_Tool.hxx>
27 #include <Geom_Plane.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>
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.
45 TopAbs_ShapeEnum GetTypeOfSimplePart(const TopoDS_Shape& theShape)
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);
56 return GetTypeOfSimplePart(It.Value());
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.
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.
71 gp_Ax3 GetPosition(const TopoDS_Shape& theShape)
75 if (theShape.IsNull())
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);
100 TopAbs_ShapeEnum aShType = theShape.ShapeType();
102 if (aShType == TopAbs_VERTEX) {
103 aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
105 if (aShType == TopAbs_COMPOUND) {
106 aShType = GetTypeOfSimplePart(theShape);
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);
115 BRepGProp::VolumeProperties(theShape, aSystem);
117 aPnt = aSystem.CentreOfMass();
120 aResult.SetLocation(aPnt);
125 //=================================================================================================
126 bool GeomAlgoAPI_NormalToFace::normal(GeomShapePtr theFace,
127 GeomShapePtr thePoint,
128 GeomEdgePtr theNormal,
129 std::string& theError)
132 std::cout << "GeomAlgoAPI_NormalToFace::normal" << std::endl;
135 if (!theFace.get()) {
136 theError = "Face for normale calculation is null";
140 TopoDS_Shape aShape = theFace->impl<TopoDS_Shape>();
142 if (aShape.ShapeType() != TopAbs_FACE) {
143 theError = "Shape for normale calculation is not a face";
147 TopoDS_Face aFace = TopoDS::Face(aShape);
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);
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";
168 p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
169 pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
174 BRepAdaptor_Surface SF (aFace);
175 SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
176 if (Vec1.Magnitude() < Precision::Confusion()) {
179 SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
181 else if (Vec2.Magnitude() < Precision::Confusion()) {
184 SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
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");
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;
204 if (Abs(radius2) > Precision::Confusion()) {
205 radius = 1.0 / radius2;
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()) {
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;
220 if (radius < Precision::Confusion())
225 // consider the face orientation
226 if (aFace.Orientation() == TopAbs_REVERSED ||
227 aFace.Orientation() == TopAbs_INTERNAL) {
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();
238 theNormal->setImpl(new TopoDS_Shape(aShape));