Salome HOME
3a841ee33cbc9c67aabb2437ab98256d1321a0a6
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsShape.cxx
1 // Copyright (C) 2007-2013  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 "HYDROGUI_VTKPrsShape.h"
24
25 #include <HYDROData_Entity.h>
26 #include <HYDROData_Channel.h>
27 #include <HYDROData_Document.h>
28 #include <HYDROData_DummyObject3D.h>
29 #include <HYDROData_Image.h>
30 #include <HYDROData_ImmersibleZone.h>
31 #include <HYDROData_Obstacle.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Polyline3D.h>
34 #include <HYDROData_Profile.h>
35 #include <HYDROData_Region.h>
36 #include <HYDROData_Stream.h>
37 #include <HYDROData_Zone.h>
38
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeWire.hxx>
41 #include <BRepBuilderAPI_MakeFace.hxx>
42
43 #include <gp_Pnt.hxx>
44
45 #include <Graphic3d_AspectFillArea3d.hxx>
46 #include <Graphic3d_MaterialAspect.hxx>
47
48 #include <TopoDS.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Face.hxx>
51
52 #include <TopExp_Explorer.hxx>
53
54 #include <BRep_Builder.hxx>
55
56 #include <Precision.hxx>
57
58 #include <GEOM_Actor.h>
59 #include <vtkScalarBarActor.h>
60
61 #include <QString>
62
63 // Hard-coded value of shape deflection coefficient for VTK viewer
64 const double VTK_MIN_DEFLECTION = 0.001;
65
66
67 //=======================================================================
68 // name    : HYDROGUI_VTKPrsShape
69 // Purpose : Constructor
70 //=======================================================================
71 HYDROGUI_VTKPrsShape::HYDROGUI_VTKPrsShape( const Handle(HYDROData_Entity)& theObject )
72 : HYDROGUI_VTKPrs( theObject ),
73 myDisplayMode( GEOM_Actor::eWireframe )
74 {
75 }
76
77 //=======================================================================
78 // name    : HYDROGUI_VTKPrsShape
79 // Purpose : Destructor
80 //=======================================================================
81 HYDROGUI_VTKPrsShape::~HYDROGUI_VTKPrsShape()
82 {
83 }
84
85 //================================================================
86 // Function : compute
87 // Purpose  : 
88 //================================================================
89 void HYDROGUI_VTKPrsShape::compute()
90 {
91   if ( !getObject().IsNull() )
92   {
93     buildShape();
94
95     if ( !myTopoShape.IsNull() )
96     {
97       GEOM_Actor* anActor = GEOM_Actor::New();
98       anActor->SetShape( myTopoShape, VTK_MIN_DEFLECTION );
99       anActor->setDisplayMode( myDisplayMode );
100       anActor->setIO( getIO() );
101       AddObject( anActor );
102
103       anActor->Delete();
104     }
105   }
106 }
107
108
109 void HYDROGUI_VTKPrsShape::buildShape()
110 {
111   Handle(HYDROData_Entity) anObject = getObject();
112   // Try to retrieve information from object
113   if ( !anObject.IsNull() )
114   {
115     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( anObject->Label() );
116   
117     if ( anObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
118     {
119       Handle(HYDROData_ImmersibleZone) aZoneObj =
120         Handle(HYDROData_ImmersibleZone)::DownCast( anObject );
121
122       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
123       if ( !aZoneShape.IsNull() ) {
124         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
125           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
126           setFace( aZoneFace, false, false );
127         } else {
128           myTopoShape = aZoneShape;
129         }
130       }
131
132       QColor aFillingColor = aZoneObj->GetFillingColor();
133       QColor aBorderColor = aZoneObj->GetBorderColor();
134
135       //setFillingColor( aFillingColor, false, false );
136       //setBorderColor( aBorderColor, false, false );
137     }
138     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
139     {
140       Handle(HYDROData_PolylineXY) aPolyline =
141         Handle(HYDROData_PolylineXY)::DownCast( anObject );
142
143       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
144
145       if ( !aPolylineShape.IsNull() ) {
146         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
147           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
148           setWire( aPolylineWire, false, false );  
149         } else {
150           myTopoShape = aPolylineShape;
151           myDisplayMode = GEOM_Actor::eWireframe;
152         }
153       }
154
155       QColor aWireColor = aPolyline->GetWireColor();
156       //setBorderColor( aWireColor, false, false );
157     }
158     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
159     {
160       Handle(HYDROData_Polyline3D) aPolyline =
161         Handle(HYDROData_Polyline3D)::DownCast( anObject );
162
163       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
164
165       if ( !aPolylineShape.IsNull() ) {
166         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
167           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
168           setWire( aPolylineWire, false, false );  
169         } else {
170           myTopoShape = aPolylineShape;
171           myDisplayMode = GEOM_Actor::eWireframe;
172         }
173       }
174
175       QColor aWireColor = aPolyline->GetBorderColor();
176       //setBorderColor( aWireColor, false, false );
177     }
178     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
179     {
180       Handle(HYDROData_Zone) aZone =
181         Handle(HYDROData_Zone)::DownCast( anObject );
182
183       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
184
185       setFace( aZoneFace, false, false );
186       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
187       {
188         // Red color for a zone with bathymetry conflict
189         //setFillingColor( Qt::red );
190       }
191       else
192       {
193         // Generate the filling color for zone
194         QStringList aGeomObjectsNames;
195
196         HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
197         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
198         for ( ; anIter.More(); anIter.Next() )
199         {
200           Handle(HYDROData_Object) aRefbject = 
201             Handle(HYDROData_Object)::DownCast( anIter.Value() );
202           if ( aRefbject.IsNull() )
203             continue;
204
205           QString aRefObjectName = aRefbject->GetName();
206           if ( aRefObjectName.isEmpty() )
207             continue;
208
209           aGeomObjectsNames.append( aRefObjectName );
210         }
211
212         //setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
213       }
214     }
215     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
216     {
217     }
218     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
219     {
220       Handle(HYDROData_Profile) aProfile =
221         Handle(HYDROData_Profile)::DownCast( anObject );
222
223       TopoDS_Wire aProfileWire;
224
225       if ( aProfile->IsValid() ) {
226         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
227
228         if ( !aProfileShape.IsNull() && 
229              aProfileShape.ShapeType() == TopAbs_WIRE ) {
230           aProfileWire = TopoDS::Wire( aProfileShape );
231         }
232       }
233
234       setWire( aProfileWire, false, false );  
235
236       QColor aWireColor = aProfile->GetBorderColor();
237       //setBorderColor( aWireColor, false, false );
238     }
239     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
240               anObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
241               anObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
242     {
243       Handle(HYDROData_Object) aGeomObject =
244         Handle(HYDROData_Object)::DownCast( anObject );
245
246       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
247
248       setShape( anObjShape, false, false );
249
250       QColor aFillingColor = aGeomObject->GetFillingColor();
251       QColor aBorderColor = aGeomObject->GetBorderColor();
252
253       //setFillingColor( aFillingColor, false, false );
254       //setBorderColor( aBorderColor, false, false );
255     }
256     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
257     {
258       Handle(HYDROData_DummyObject3D) anObject3D =
259         Handle(HYDROData_DummyObject3D)::DownCast( anObject );
260       TopoDS_Shape aShape3D = anObject3D->GetShape();
261
262       setShape( aShape3D, false, false );
263
264       QColor aFillingColor = anObject3D->GetFillingColor();
265       QColor aBorderColor = anObject3D->GetBorderColor();
266
267       //setFillingColor( aFillingColor, false, false );
268       //setBorderColor( aBorderColor, false, false );
269     }
270   }
271 }
272
273 void HYDROGUI_VTKPrsShape::setWire( const TopoDS_Wire& theWire,
274                                     const bool         theToDisplay,
275                                     const bool         theIsUpdateViewer )
276 {
277   myTopoShape = theWire;
278   myDisplayMode = GEOM_Actor::eWireframe;
279 }
280
281 void HYDROGUI_VTKPrsShape::setFaces( const TopoDS_Compound& theWires,
282                                      const bool             theToDisplay,
283                                      const bool             theIsUpdateViewer )
284 {
285   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
286   TopoDS_Compound aCompound;
287   BRep_Builder aBuilder;
288     aBuilder.MakeCompound( aCompound );
289
290   for ( ; anExp.More(); anExp.Next() ) {
291     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
292     if ( aWire.IsNull() ) {
293       continue;
294     }
295
296     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
297     aMakeFace.Build();
298     if( aMakeFace.IsDone() ) {
299       aBuilder.Add( aCompound, aMakeFace.Face() );
300     }
301   }
302
303   myTopoShape = aCompound;
304   myDisplayMode = GEOM_Actor::eShading;
305 }
306
307 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Wire& theWire,
308                                     const bool         theToDisplay,
309                                     const bool         theIsUpdateViewer )
310 {
311   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
312   aFaceBuilder.Build();
313   if( aFaceBuilder.IsDone() )
314   {
315     TopoDS_Face aFace = aFaceBuilder.Face();
316     setFace( aFace, theToDisplay, theIsUpdateViewer );
317   }
318 }
319
320 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Face& theFace,
321                                     const bool         theToDisplay,
322                                     const bool         theIsUpdateViewer )
323 {
324   myTopoShape = theFace;
325   myDisplayMode = GEOM_Actor::eShading;
326 }
327
328 void HYDROGUI_VTKPrsShape::setShape( const TopoDS_Shape& theShape,
329                                      const bool          theToDisplay,
330                                      const bool          theIsUpdateViewer )
331 {
332   myTopoShape = theShape;
333   myDisplayMode = GEOM_Actor::eShading;
334 }