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