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