Salome HOME
Remove warnings
[modules/geom.git] / src / GEOMImpl / GEOMImpl_MeasureDriver.cxx
1 //  Copyright (C) 2007-2010  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.
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_IMeasureOperations.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOM_Function.hxx>
30
31 #include <BRep_Tool.hxx>
32 #include <BRepGProp.hxx>
33 #include <BRepBuilderAPI_MakeVertex.hxx>
34 #include <BRepBuilderAPI_MakeEdge.hxx>
35
36 #include <TopAbs.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Wire.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_IndexedMapOfShape.hxx>
44
45 #include <GProp_GProps.hxx>
46 #include <GeomLProp_SLProps.hxx>
47 #include <Geom_Surface.hxx>
48
49 #include <Bnd_Box.hxx>
50 #include <BRepBndLib.hxx>
51 #include <BRepAdaptor_Surface.hxx>
52 #include <ShapeAnalysis_Surface.hxx>
53
54 #include <gp_Pnt.hxx>
55 #include <Precision.hxx>
56 #include <Standard_NullObject.hxx>
57
58 //=======================================================================
59 //function : GetID
60 //purpose  :
61 //======================================================================= 
62 const Standard_GUID& GEOMImpl_MeasureDriver::GetID()
63 {
64   static Standard_GUID aMeasureDriver("FF1BBB65-5D14-4df2-980B-3A668264EA16");
65   return aMeasureDriver; 
66 }
67
68
69 //=======================================================================
70 //function : GEOMImpl_MeasureDriver
71 //purpose  : 
72 //=======================================================================
73 GEOMImpl_MeasureDriver::GEOMImpl_MeasureDriver() 
74 {
75 }
76
77 //=======================================================================
78 //function : Execute
79 //purpose  :
80 //======================================================================= 
81 Standard_Integer GEOMImpl_MeasureDriver::Execute(TFunction_Logbook& log) const
82 {
83   if (Label().IsNull()) return 0;    
84   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
85
86   GEOMImpl_IMeasure aCI (aFunction);
87   Standard_Integer aType = aFunction->GetType();
88
89   TopoDS_Shape aShape;
90
91   if (aType == CDG_MEASURE)
92   {
93     Handle(GEOM_Function) aRefBase = aCI.GetBase();
94     TopoDS_Shape aShapeBase = aRefBase->GetValue();
95     if (aShapeBase.IsNull()) {
96       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
97     }
98
99     GProp_GProps aSystem;
100     gp_Pnt aCenterMass;
101
102     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
103       aCenterMass = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
104     } else if (aShapeBase.ShapeType() == TopAbs_EDGE || aShapeBase.ShapeType() == TopAbs_WIRE) {
105       BRepGProp::LinearProperties(aShapeBase, aSystem);
106       aCenterMass = aSystem.CentreOfMass();
107     } else if (aShapeBase.ShapeType() == TopAbs_FACE || aShapeBase.ShapeType() == TopAbs_SHELL) {
108       BRepGProp::SurfaceProperties(aShapeBase, aSystem);
109       aCenterMass = aSystem.CentreOfMass();
110     } else {
111       BRepGProp::VolumeProperties(aShapeBase, aSystem);
112       aCenterMass = aSystem.CentreOfMass();
113     }
114
115     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
116   }
117   else if (aType == VERTEX_BY_INDEX)
118   {
119     Handle(GEOM_Function) aRefBase = aCI.GetBase();
120     TopoDS_Shape aShapeBase = aRefBase->GetValue();
121     if (aShapeBase.IsNull()) {
122       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
123     }
124
125     int index = aCI.GetIndex();
126     gp_Pnt aVertex;
127
128     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
129       if ( index != 1 )
130         Standard_NullObject::Raise("Vertex index is out of range");
131       else
132         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
133     } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
134       TopoDS_Vertex aV1, aV2;
135       TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
136       
137       TopExp::Vertices(anEdgeE, aV1, aV2);
138       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
139       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
140
141       if (index < 0 || index > 1)
142         Standard_NullObject::Raise("Vertex index is out of range");
143
144       if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
145            ( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
146         aVertex = aP1;
147       else
148       aVertex = aP2;
149     } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
150       TopTools_IndexedMapOfShape anEdgeShapes;
151       TopTools_IndexedMapOfShape aVertexShapes;
152       TopoDS_Vertex aV1, aV2;
153       TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
154       TopExp_Explorer exp (aWire, TopAbs_EDGE);
155       for (; exp.More(); exp.Next()) {
156         anEdgeShapes.Add(exp.Current());
157         TopoDS_Edge E = TopoDS::Edge(exp.Current());
158         TopExp::Vertices(E, aV1, aV2);
159         if ( aVertexShapes.Extent() == 0)
160           aVertexShapes.Add(aV1);
161         if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
162           aVertexShapes.Add(aV1);
163         if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
164           aVertexShapes.Add(aV2);
165       }
166
167       if (index < 0 || index > aVertexShapes.Extent())
168         Standard_NullObject::Raise("Vertex index is out of range");
169
170       if (aWire.Orientation() == TopAbs_FORWARD)
171         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
172       else
173         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
174     } else {
175       Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
176     }
177
178     aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
179   }
180   else if (aType == VECTOR_FACE_NORMALE)
181   {
182     // Face
183     Handle(GEOM_Function) aRefBase = aCI.GetBase();
184     TopoDS_Shape aShapeBase = aRefBase->GetValue();
185     if (aShapeBase.IsNull()) {
186       Standard_NullObject::Raise("Face for normale calculation is null");
187     }
188     if (aShapeBase.ShapeType() != TopAbs_FACE) {
189       Standard_NullObject::Raise("Shape for normale calculation is not a face");
190     }
191     TopoDS_Face aFace = TopoDS::Face(aShapeBase);
192
193     // Point
194     gp_Pnt p1 (0,0,0);
195
196     Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
197     if (!aPntFunc.IsNull())
198     {
199       TopoDS_Shape anOptPnt = aPntFunc->GetValue();
200       if (anOptPnt.IsNull())
201         Standard_NullObject::Raise("Invalid shape given for point argument");
202       p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
203     }
204     else
205     {
206       gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
207       p1 = aPos.Location();
208     }
209
210     // Point parameters on surface
211     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
212     Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
213     gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
214
215     // Normal direction
216     gp_Vec Vec1,Vec2;
217     BRepAdaptor_Surface SF (aFace);
218     SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
219     if (Vec1.Magnitude() < Precision::Confusion()) {
220       gp_Vec tmpV;
221       gp_Pnt tmpP;
222       SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
223     }
224     else if (Vec2.Magnitude() < Precision::Confusion()) {
225       gp_Vec tmpV;
226       gp_Pnt tmpP;
227       SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
228     }
229
230     gp_Vec V = Vec1.Crossed(Vec2);
231     Standard_Real mod = V.Magnitude();
232     if (mod < Precision::Confusion())
233       Standard_NullObject::Raise("Normal vector of a face has null magnitude");
234
235     // Set length of normal vector to average radius of curvature
236     Standard_Real radius = 0.0;
237     GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
238     if (aProperties.IsCurvatureDefined()) {
239       Standard_Real radius1 = Abs(aProperties.MinCurvature());
240       Standard_Real radius2 = Abs(aProperties.MaxCurvature());
241       if (Abs(radius1) > Precision::Confusion()) {
242         radius = 1.0 / radius1;
243         if (Abs(radius2) > Precision::Confusion()) {
244           radius = (radius + 1.0 / radius2) / 2.0;
245         }
246       }
247       else {
248         if (Abs(radius2) > Precision::Confusion()) {
249           radius = 1.0 / radius2;
250         }
251       }
252     }
253
254     // Set length of normal vector to average dimension of the face
255     // (only if average radius of curvature is not appropriate)
256     if (radius < Precision::Confusion()) {
257         Bnd_Box B;
258         Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
259         BRepBndLib::Add(aFace, B);
260         B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
261         radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
262     }
263
264     if (radius < Precision::Confusion())
265       radius = 1.0;
266
267     V *= radius / mod;
268
269     // consider the face orientation
270     if (aFace.Orientation() == TopAbs_REVERSED ||
271         aFace.Orientation() == TopAbs_INTERNAL) {
272       V = - V;
273     }
274
275     // Edge
276     gp_Pnt p2 = p1.Translated(V);
277     BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
278     if (!aBuilder.IsDone())
279       Standard_NullObject::Raise("Vector construction failed");
280     aShape = aBuilder.Shape();
281   }
282   else {
283   }
284
285   if (aShape.IsNull()) return 0;
286
287   aFunction->SetValue(aShape);
288
289   log.SetTouched(Label()); 
290
291   return 1;
292 }
293
294
295 //=======================================================================
296 //function :  GEOMImpl_MeasureDriver_Type_
297 //purpose  :
298 //======================================================================= 
299 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
300 {
301
302   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
303   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
304   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
305   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
306   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
307   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
308  
309
310   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
311   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
312                                                          sizeof(GEOMImpl_MeasureDriver),
313                                                          1,
314                                                          (Standard_Address)_Ancestors,
315                                                          (Standard_Address)NULL);
316
317   return _aType;
318 }
319
320 //=======================================================================
321 //function : DownCast
322 //purpose  :
323 //======================================================================= 
324 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
325 {
326   Handle(GEOMImpl_MeasureDriver) _anOtherObject;
327
328   if (!AnObject.IsNull()) {
329      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
330        _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
331      }
332   }
333
334   return _anOtherObject ;
335 }