]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_MeasureDriver.cxx
Salome HOME
f454de9264fe83558bbf86a09c5d4f8c5ce4ee10
[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 #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     gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aShapeBase);
99     gp_Pnt aCenterMass = aPos.Location();
100     aShape = BRepBuilderAPI_MakeVertex(aCenterMass).Shape();
101   }
102   else if (aType == VERTEX_BY_INDEX)
103   {
104     Handle(GEOM_Function) aRefBase = aCI.GetBase();
105     TopoDS_Shape aShapeBase = aRefBase->GetValue();
106     if (aShapeBase.IsNull()) {
107       Standard_NullObject::Raise("Shape for centre of mass calculation is null");
108     }
109
110     int index = aCI.GetIndex();
111     gp_Pnt aVertex;
112
113     if (aShapeBase.ShapeType() == TopAbs_VERTEX) {
114       if ( index != 1 )
115         Standard_NullObject::Raise("Vertex index is out of range");
116       else
117         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aShapeBase));
118     } else if (aShapeBase.ShapeType() == TopAbs_EDGE) {
119       TopoDS_Vertex aV1, aV2;
120       TopoDS_Edge anEdgeE = TopoDS::Edge(aShapeBase);
121       
122       TopExp::Vertices(anEdgeE, aV1, aV2);
123       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
124       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
125
126       if (index < 0 || index > 1)
127         Standard_NullObject::Raise("Vertex index is out of range");
128
129       if ( ( anEdgeE.Orientation() == TopAbs_FORWARD && index == 0 ) ||
130            ( anEdgeE.Orientation() == TopAbs_REVERSED && index == 1 ) )
131         aVertex = aP1;
132       else
133       aVertex = aP2;
134     } else if (aShapeBase.ShapeType() == TopAbs_WIRE) {
135       TopTools_IndexedMapOfShape anEdgeShapes;
136       TopTools_IndexedMapOfShape aVertexShapes;
137       TopoDS_Vertex aV1, aV2;
138       TopoDS_Wire aWire = TopoDS::Wire(aShapeBase);
139       TopExp_Explorer exp (aWire, TopAbs_EDGE);
140       for (; exp.More(); exp.Next()) {
141         anEdgeShapes.Add(exp.Current());
142         TopoDS_Edge E = TopoDS::Edge(exp.Current());
143         TopExp::Vertices(E, aV1, aV2);
144         if ( aVertexShapes.Extent() == 0)
145           aVertexShapes.Add(aV1);
146         if ( !aV1.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
147           aVertexShapes.Add(aV1);
148         if ( !aV2.IsSame( aVertexShapes(aVertexShapes.Extent()) ) )
149           aVertexShapes.Add(aV2);
150       }
151
152       if (index < 0 || index > aVertexShapes.Extent())
153         Standard_NullObject::Raise("Vertex index is out of range");
154
155       if (aWire.Orientation() == TopAbs_FORWARD)
156         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(index+1)));
157       else
158         aVertex = BRep_Tool::Pnt(TopoDS::Vertex(aVertexShapes(aVertexShapes.Extent() - index)));
159     } else {
160       Standard_NullObject::Raise("Shape for vertex calculation is not an edge or wire");
161     }
162
163     aShape = BRepBuilderAPI_MakeVertex(aVertex).Shape();
164   }
165   else if (aType == VECTOR_FACE_NORMALE)
166   {
167     // Face
168     Handle(GEOM_Function) aRefBase = aCI.GetBase();
169     TopoDS_Shape aShapeBase = aRefBase->GetValue();
170     if (aShapeBase.IsNull()) {
171       Standard_NullObject::Raise("Face for normale calculation is null");
172     }
173     if (aShapeBase.ShapeType() != TopAbs_FACE) {
174       Standard_NullObject::Raise("Shape for normale calculation is not a face");
175     }
176     TopoDS_Face aFace = TopoDS::Face(aShapeBase);
177
178     // Point
179     gp_Pnt p1 (0,0,0);
180
181     Handle(GEOM_Function) aPntFunc = aCI.GetPoint();
182     if (!aPntFunc.IsNull())
183     {
184       TopoDS_Shape anOptPnt = aPntFunc->GetValue();
185       if (anOptPnt.IsNull())
186         Standard_NullObject::Raise("Invalid shape given for point argument");
187       p1 = BRep_Tool::Pnt(TopoDS::Vertex(anOptPnt));
188     }
189     else
190     {
191       gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(aFace);
192       p1 = aPos.Location();
193     }
194
195     // Point parameters on surface
196     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
197     Handle(ShapeAnalysis_Surface) aSurfAna = new ShapeAnalysis_Surface (aSurf);
198     gp_Pnt2d pUV = aSurfAna->ValueOfUV(p1, Precision::Confusion());
199
200     // Normal direction
201     gp_Vec Vec1,Vec2;
202     BRepAdaptor_Surface SF (aFace);
203     SF.D1(pUV.X(), pUV.Y(), p1, Vec1, Vec2);
204     if (Vec1.Magnitude() < Precision::Confusion()) {
205       gp_Vec tmpV;
206       gp_Pnt tmpP;
207       SF.D1(pUV.X(), pUV.Y()-0.1, tmpP, Vec1, tmpV);
208     }
209     else if (Vec2.Magnitude() < Precision::Confusion()) {
210       gp_Vec tmpV;
211       gp_Pnt tmpP;
212       SF.D1(pUV.X()-0.1, pUV.Y(), tmpP, tmpV, Vec2);
213     }
214
215     gp_Vec V = Vec1.Crossed(Vec2);
216     Standard_Real mod = V.Magnitude();
217     if (mod < Precision::Confusion())
218       Standard_NullObject::Raise("Normal vector of a face has null magnitude");
219
220     // Set length of normal vector to average radius of curvature
221     Standard_Real radius = 0.0;
222     GeomLProp_SLProps aProperties (aSurf, pUV.X(), pUV.Y(), 2, Precision::Confusion());
223     if (aProperties.IsCurvatureDefined()) {
224       Standard_Real radius1 = Abs(aProperties.MinCurvature());
225       Standard_Real radius2 = Abs(aProperties.MaxCurvature());
226       if (Abs(radius1) > Precision::Confusion()) {
227         radius = 1.0 / radius1;
228         if (Abs(radius2) > Precision::Confusion()) {
229           radius = (radius + 1.0 / radius2) / 2.0;
230         }
231       }
232       else {
233         if (Abs(radius2) > Precision::Confusion()) {
234           radius = 1.0 / radius2;
235         }
236       }
237     }
238
239     // Set length of normal vector to average dimension of the face
240     // (only if average radius of curvature is not appropriate)
241     if (radius < Precision::Confusion()) {
242         Bnd_Box B;
243         Standard_Real Xmin, Xmax, Ymin, Ymax, Zmin, Zmax;
244         BRepBndLib::Add(aFace, B);
245         B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
246         radius = ((Xmax - Xmin) + (Ymax - Ymin) + (Zmax - Zmin)) / 3.0;
247     }
248
249     if (radius < Precision::Confusion())
250       radius = 1.0;
251
252     V *= radius / mod;
253
254     // consider the face orientation
255     if (aFace.Orientation() == TopAbs_REVERSED ||
256         aFace.Orientation() == TopAbs_INTERNAL) {
257       V = - V;
258     }
259
260     // Edge
261     gp_Pnt p2 = p1.Translated(V);
262     BRepBuilderAPI_MakeEdge aBuilder (p1, p2);
263     if (!aBuilder.IsDone())
264       Standard_NullObject::Raise("Vector construction failed");
265     aShape = aBuilder.Shape();
266   }
267   else {
268   }
269
270   if (aShape.IsNull()) return 0;
271
272   aFunction->SetValue(aShape);
273
274   log.SetTouched(Label()); 
275
276   return 1;
277 }
278
279
280 //=======================================================================
281 //function :  GEOMImpl_MeasureDriver_Type_
282 //purpose  :
283 //======================================================================= 
284 Standard_EXPORT Handle_Standard_Type& GEOMImpl_MeasureDriver_Type_()
285 {
286
287   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
288   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
289   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
290   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared); 
291   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
292   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
293  
294
295   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
296   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_MeasureDriver",
297                                                          sizeof(GEOMImpl_MeasureDriver),
298                                                          1,
299                                                          (Standard_Address)_Ancestors,
300                                                          (Standard_Address)NULL);
301
302   return _aType;
303 }
304
305 //=======================================================================
306 //function : DownCast
307 //purpose  :
308 //======================================================================= 
309 const Handle(GEOMImpl_MeasureDriver) Handle(GEOMImpl_MeasureDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
310 {
311   Handle(GEOMImpl_MeasureDriver) _anOtherObject;
312
313   if (!AnObject.IsNull()) {
314      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_MeasureDriver))) {
315        _anOtherObject = Handle(GEOMImpl_MeasureDriver)((Handle(GEOMImpl_MeasureDriver)&)AnObject);
316      }
317   }
318
319   return _anOtherObject ;
320 }