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