Salome HOME
9d430e4c7728c9b862069e524bd5d96d26e2975f
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MeasureDriver.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_MeasureDriver.hxx>
26 #include <GEOMImpl_IMeasure.hxx>
27 #include <GEOMImpl_Types.hxx>
28
29 #include <GEOM_Function.hxx>
30
31 #include <GEOMUtils.hxx>
32
33 #include <BRep_Tool.hxx>
34 #include <BRepTools.hxx>
35 #include <BRepGProp.hxx>
36 #include <BRepBuilderAPI_Copy.hxx>
37 #include <BRepBuilderAPI_MakeEdge.hxx>
38 #include <BRepBuilderAPI_MakeVertex.hxx>
39 #include <BRepPrimAPI_MakeBox.hxx>
40
41 #include <TopAbs.hxx>
42 #include <TopoDS.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopExp.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49
50 #include <GProp_GProps.hxx>
51 #include <GeomLProp_SLProps.hxx>
52 #include <Geom_Surface.hxx>
53
54 #include <Bnd_Box.hxx>
55 #include <BRepBndLib.hxx>
56 #include <BRepAdaptor_Surface.hxx>
57 #include <ShapeAnalysis_Surface.hxx>
58
59 #include <gp_Pnt.hxx>
60 #include <Precision.hxx>
61 #include <Standard_NullObject.hxx>
62 #include <StdFail_NotDone.hxx>
63
64 //=======================================================================
65 //function : GetID
66 //purpose  :
67 //======================================================================= 
68 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
69 {
70   static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
71   return aMeasureDriver; 
72 }
73
74
75 //=======================================================================
76 //function : GEOMImpl_MeasureDriver
77 //purpose  : 
78 //=======================================================================
79 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver() 
80 {
81 }
82
83 //=======================================================================
84 //function : Execute
85 //purpose  :
86 //======================================================================= 
87 Standard_Integer GEOMImpl_MeasureDriver::Execute(Handle(TFunction_Logbook)& log) const
88 {
89   if (Label().IsNull()) return 0;    
90   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
91
92   GEOMImpl_IMeasure aCI (aFunction);
93   Standard_Integer aType = aFunction->GetType();
94
95   TopoDS_Shape aShape;
96
97   if (aType == CDG_MEASURE)
98   {
99     Handle(GEOM_Function) aRefBase = aCI.GetBase();
100     TopoDS_Shape aShapeBase = aRefBase->GetValue();
101     if (aShapeBase.IsNull())
102       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
103
104     gp_Ax3 aPos = GEOMUtils::GetPosition(aShapeBase);
105     gp_Pnt aCenterMass = aPos.Location();
106     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
107   }
108   else if (aType == BND_BOX_MEASURE || aType == BND_BOX_MEASURE_PRECISE)
109   {
110     Handle(GEOM_Function) aRefBase = aCI.GetBase();
111     TopoDS_Shape aShapeBase = aRefBase->GetValue();
112     if (aShapeBase.IsNull())
113       Standard_NullObject::Raise("Shape for bounding box calculation is null");
114
115     BRepBuilderAPI_Copy aCopyTool (aShapeBase);
116     if (!aCopyTool.IsDone())
117       Standard_NullObject::Raise("Shape for bounding box calculation is bad");
118
119     aShapeBase = aCopyTool.Shape();
120
121     // remove triangulation to obtain more exact boundaries
122     BRepTools::Clean(aShapeBase);
123
124     Bnd_Box B;
125     BRepBndLib::Add(aShapeBase, B);
126
127     if (aType == BND_BOX_MEASURE_PRECISE) {
128       if (!GEOMUtils::PreciseBoundingBox(aShapeBase, B)) {
129         Standard_NullObject::Raise("Bounding box cannot be precised");
130       }
131     }
132
133     Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
134     B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
135
136     if (Xmax - Xmin < Precision::Confusion()) Xmax += Precision::Confusion();
137     if (Ymax - Ymin < Precision::Confusion()) Ymax += Precision::Confusion();
138     if (Zmax - Zmin < Precision::Confusion()) Zmax += Precision::Confusion();
139
140     gp_Pnt P1 (Xmin, Ymin, Zmin);
141     gp_Pnt P2 (Xmax, Ymax, Zmax);
142
143     BRepPrimAPI_MakeBox MB (P1, P2);
144     MB.Build();
145     if (!MB.IsDone()) StdFail_NotDone::Raise("Bounding box cannot be computed from the given shape");
146     aShape = MB.Shape();
147   }
148   else if (aType == VERTEX_BY_INDEX)
149   {
150     Handle(GEOM_Function) aRefBase = aCI.GetBase();
151     TopoDS_Shape aShapeBase = aRefBase->GetValue();
152     if (aShapeBase.IsNull()) {
153       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
154     }
155
156     int index = aCI.GetIndex();
157     bool useOri = aCI.GetUseOri();
158     gp_Pnt aVertex;
159
160     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
161       if ( index != 1 )
162         Standard_NullObject::Raise("Vertex index is out of range");
163       else
164         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
165     }
166     else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
167       if (index < 0 || index > 1)
168         Standard_NullObject::Raise("Vertex index is out of range");
169
170       TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
171       if ( anEdgeE.Orientation() != TopAbs_FORWARD && 
172            anEdgeE.Orientation() != TopAbs_REVERSED )
173         anEdgeE.Orientation( TopAbs_FORWARD );
174
175       TopoDS_Vertex aV[2];
176       TopExp::Vertices(anEdgeE, aV[0], aV[1], useOri );
177       aVertex = BRep_Tool::Pnt( aV[ index ]);
178     }
179     else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
180       TopTools_IndexedMapOfShape anEdgeShapes;
181       TopTools_IndexedMapOfShape aVertexShapes;
182       TopoDS_Vertex aV1, aV2;
183       TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
184       TopExp_Explorer exp (aWire, TopAbs_EDGE);
185       for (; exp.More(); exp.Next()) {
186         anEdgeShapes.Add(exp.Current());
187         TopoDS_Edge E = TopoDS::Edge(exp.Current());
188         TopExp::Vertices(E, aV1, aV2);
189         if ( aVertexShapes.Extent() == 0)
190           aVertexShapes.Add(aV1);
191         if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
192           aVertexShapes.Add(aV1);
193         if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
194           aVertexShapes.Add(aV2);
195       }
196
197       if (index < 0 || index > aVertexShapes.Extent())
198         Standard_NullObject::Raise("Vertex index is out of range");
199
200       if ( aWire.Orientation() == TopAbs_FORWARD || !useOri )
201         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
202       else
203         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
204     }
205     else {
206       Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
207     }
208
209     aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
210   }
211   else if (aType == VECTOR_FACE_NORMALE)
212   {
213     // Face
214     Handle(GEOM_Function) aRefBase = aCI.GetBase();
215     TopoDS_Shape aShapeBase = aRefBase->GetValue();
216     if (aShapeBase.IsNull()) {
217       Standard_NullObject::Raise("Face for normale calculation is null");
218     }
219     if (aShapeBase.ShapeType() != TopAbs_FACE) {
220       Standard_NullObject::Raise("Shape for normale calculation is not a face");
221     }
222     TopoDS_Face aFace = TopoDS::Face(aShapeBase);
223
224     // Point
225     gp_Pnt p1 (0,0,0);
226
227     Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
228     if (!aPntFunc.IsNull())
229     {
230       TopoDS_Shape anOptPnt = aPntFunc->GetValue();
231       if (anOptPnt.IsNull())
232         Standard_NullObject::Raise("Invalid shape given for point argument");
233       p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
234     }
235     else
236     {
237       gp_Ax3 aPos = GEOMUtils::GetPosition(aFace);
238       p1 = aPos.Location();
239     }
240
241     // Point parameters on surface
242     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
243     Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
244     gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
245
246     // Normal direction
247     gp_Vec Vec1,Vec2;
248     BRepAdaptor_Surface SF (aFace);
249     SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
250     if (Vec1.Magnitude() < Precision::Confusion()) {
251       gp_Vec tmpV;
252       gp_Pnt tmpP;
253       SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
254     }
255     else if (Vec2.Magnitude() < Precision::Confusion()) {
256       gp_Vec tmpV;
257       gp_Pnt tmpP;
258       SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
259     }
260
261     gp_Vec V = Vec1.Crossed(Vec2);
262     Standard_Real mod = V.Magnitude();
263     if (mod < Precision::Confusion())
264       Standard_NullObject::Raise("Normal vector of a face has null magnitude");
265
266     // Set length of normal vector to average radius of curvature
267     Standard_Real radius = 0.0;
268     GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
269     if (aProperties.IsCurvatureDefined()) {
270       Standard_Real radius1 = Abs(aProperties.MinCurvature());
271       Standard_Real radius2 = Abs(aProperties.MaxCurvature());
272       if (Abs(radius1) > Precision::Confusion()) {
273         radius = 1.0 / radius1;
274         if (Abs(radius2) > Precision::Confusion()) {
275           radius = (radius + 1.0 / radius2) / 2.0;
276         }
277       }
278       else {
279         if (Abs(radius2) > Precision::Confusion()) {
280           radius = 1.0 / radius2;
281         }
282       }
283     }
284
285     // Set length of normal vector to average dimension of the face
286     // (only if average radius of curvature is not appropriate)
287     if (radius < Precision::Confusion()) {
288         Bnd_Box B;
289         Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
290         BRepBndLib::Add(aFace, B);
291         B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
292         radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
293     }
294
295     if (radius < Precision::Confusion())
296       radius = 1.0;
297
298     V *= radius / mod;
299
300     // consider the face orientation
301     if (aFace.Orientation() == TopAbs_REVERSED ||
302         aFace.Orientation() == TopAbs_INTERNAL) {
303       V = - V;
304     }
305
306     // Edge
307     gp_Pnt p2 = p1.Translated(V);
308     BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
309     if (!aBuilder.IsDone())
310       Standard_NullObject::Raise("Vector construction failed");
311     aShape = aBuilder.Shape();
312   }
313   else {
314   }
315
316   if (aShape.IsNull()) return 0;
317
318   aFunction->SetValue(aShape);
319
320   log->SetTouched(Label());
321
322   return 1;
323 }
324
325 //================================================================================
326 /*!
327  * \brief Returns a name of creation operation and names and values of creation parameters
328  */
329 //================================================================================
330
331 bool GEOMImpl_MeasureDriver::
332 GetCreationInformation(std::string&             theOperationName,
333                        std::vector<GEOM_Param>& theParams)
334 {
335   if (Label().IsNull()) return 0;
336   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
337
338   GEOMImpl_IMeasure aCI( function );
339   Standard_Integer aType = function->GetType();
340
341   switch ( aType ) {
342   case CDG_MEASURE:
343     theOperationName = "MASS_CENTER";
344     AddParam( theParams, "Object", aCI.GetBase() );
345     break;
346   case BND_BOX_MEASURE:
347   case BND_BOX_MEASURE_PRECISE:
348     theOperationName = "BND_BOX";
349     AddParam( theParams, "Object", aCI.GetBase() );
350     break;
351   case VERTEX_BY_INDEX:
352     theOperationName = "GetVertexByIndex";
353     AddParam( theParams, "Object", aCI.GetBase() );
354     AddParam( theParams, "Index", aCI.GetIndex() );
355     AddParam( theParams, "Oriented", aCI.GetUseOri() );
356     break;
357   case VECTOR_FACE_NORMALE:
358     theOperationName = "NORMALE";
359     AddParam( theParams, "Face", aCI.GetBase() );
360     AddParam( theParams, "Point", aCI.GetPoint(), "face center" );
361     break;
362   default:
363     return false;
364   }
365
366   return true;
367 }
368
369 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_MeasureDriver,GEOM_BaseDriver)