Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into BR_2017_PORTING
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsShape.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_VTKPrsShape.h"
20
21 #include <HYDROData_Entity.h>
22 #include <HYDROData_Channel.h>
23 #include <HYDROData_Document.h>
24 #include <HYDROData_DummyObject3D.h>
25 #include <HYDROData_Image.h>
26 #include <HYDROData_ImmersibleZone.h>
27 #include <HYDROData_Obstacle.h>
28 #include <HYDROData_PolylineXY.h>
29 #include <HYDROData_Polyline3D.h>
30 #include <HYDROData_Profile.h>
31 #include <HYDROData_Region.h>
32 #include <HYDROData_Stream.h>
33 #include <HYDROData_Zone.h>
34
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepBuilderAPI_MakeFace.hxx>
38
39 #include <gp_Pnt.hxx>
40
41 #include <Graphic3d_AspectFillArea3d.hxx>
42 #include <Graphic3d_MaterialAspect.hxx>
43
44 #include <TopoDS.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopoDS_Face.hxx>
47
48 #include <TopExp_Explorer.hxx>
49
50 #include <BRep_Builder.hxx>
51
52 #include <Precision.hxx>
53
54 #include <HYDROGUI_Actor.h>
55 #include <vtkScalarBarActor.h>
56
57 #include <QString>
58
59 // Hard-coded value of shape deflection coefficient for VTK viewer
60 const double VTK_MIN_DEFLECTION = 0.001;
61
62
63 //=======================================================================
64 // name    : HYDROGUI_VTKPrsShape
65 // Purpose : Constructor
66 //=======================================================================
67 HYDROGUI_VTKPrsShape::HYDROGUI_VTKPrsShape( const Handle(HYDROData_Entity)& theObject )
68 : HYDROGUI_VTKPrs( theObject ),
69 myDisplayMode( GEOM_Actor::eWireframe )
70 {
71 }
72
73 //=======================================================================
74 // name    : HYDROGUI_VTKPrsShape
75 // Purpose : Destructor
76 //=======================================================================
77 HYDROGUI_VTKPrsShape::~HYDROGUI_VTKPrsShape()
78 {
79 }
80
81 //================================================================
82 // Function : compute
83 // Purpose  : 
84 //================================================================
85 void HYDROGUI_VTKPrsShape::compute()
86 {
87   HYDROGUI_VTKPrs::compute();
88
89   if ( !getObject().IsNull() )
90   {
91     buildShape();
92
93     if ( !myTopoShape.IsNull() )
94     {
95       HYDROGUI_Actor* anActor = getActor<HYDROGUI_Actor>(this);
96       anActor->SetShape( myTopoShape, VTK_MIN_DEFLECTION );
97       anActor->setDisplayMode( myDisplayMode );
98       anActor->setIO( getIO() );
99     }
100   }
101 }
102
103
104 void HYDROGUI_VTKPrsShape::buildShape()
105 {
106   Handle(HYDROData_Entity) anObject = getObject();
107   // Try to retrieve information from object
108   if ( !anObject.IsNull() )
109   {
110     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( anObject->Label() );
111   
112     if ( anObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
113     {
114       Handle(HYDROData_ImmersibleZone) aZoneObj =
115         Handle(HYDROData_ImmersibleZone)::DownCast( anObject );
116
117       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
118       if ( !aZoneShape.IsNull() ) {
119         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
120           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
121           setFace( aZoneFace, false, false );
122         } else {
123           myTopoShape = aZoneShape;
124         }
125       }
126
127       QColor aFillingColor = aZoneObj->GetFillingColor();
128       QColor aBorderColor = aZoneObj->GetBorderColor();
129
130       //setFillingColor( aFillingColor, false, false );
131       //setBorderColor( aBorderColor, false, false );
132     }
133     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
134     {
135       Handle(HYDROData_PolylineXY) aPolyline =
136         Handle(HYDROData_PolylineXY)::DownCast( anObject );
137
138       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
139
140       if ( !aPolylineShape.IsNull() ) {
141         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
142           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
143           setWire( aPolylineWire, false, false );  
144         } else {
145           myTopoShape = aPolylineShape;
146           myDisplayMode = GEOM_Actor::eWireframe;
147         }
148       }
149
150       QColor aWireColor = aPolyline->GetWireColor();
151       //setBorderColor( aWireColor, false, false );
152     }
153     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
154     {
155       Handle(HYDROData_Polyline3D) aPolyline =
156         Handle(HYDROData_Polyline3D)::DownCast( anObject );
157
158       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
159
160       if ( !aPolylineShape.IsNull() ) {
161         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
162           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
163           setWire( aPolylineWire, false, false );  
164         } else {
165           myTopoShape = aPolylineShape;
166           myDisplayMode = GEOM_Actor::eWireframe;
167         }
168       }
169
170       QColor aWireColor = aPolyline->GetBorderColor();
171       //setBorderColor( aWireColor, false, false );
172     }
173     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
174     {
175       Handle(HYDROData_Zone) aZone =
176         Handle(HYDROData_Zone)::DownCast( anObject );
177
178       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
179
180       setFace( aZoneFace, false, false );
181       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
182       {
183         // Red color for a zone with bathymetry conflict
184         //setFillingColor( Qt::red );
185       }
186       else
187       {
188         // Generate the filling color for zone
189         QStringList anObjectsNames;
190
191         HYDROData_SequenceOfObjects aRefObjects = aZone->GetObjects();
192         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
193         for ( ; anIter.More(); anIter.Next() )
194         {
195           Handle(HYDROData_Entity) aRefbject = anIter.Value();
196           if ( aRefbject.IsNull() )
197             continue;
198
199           QString aRefObjectName = aRefbject->GetName();
200           if ( aRefObjectName.isEmpty() )
201             continue;
202
203           anObjectsNames.append( aRefObjectName );
204         }
205
206         //setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
207       }
208     }
209     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
210     {
211     }
212     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
213     {
214       Handle(HYDROData_Profile) aProfile =
215         Handle(HYDROData_Profile)::DownCast( anObject );
216
217       TopoDS_Wire aProfileWire;
218
219       if ( aProfile->IsValid() ) {
220         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
221
222         if ( !aProfileShape.IsNull() && 
223              aProfileShape.ShapeType() == TopAbs_WIRE ) {
224           aProfileWire = TopoDS::Wire( aProfileShape );
225         }
226       }
227
228       setWire( aProfileWire, false, false );  
229
230       QColor aWireColor = aProfile->GetBorderColor();
231       //setBorderColor( aWireColor, false, false );
232     }
233     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
234               anObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
235               anObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
236     {
237       Handle(HYDROData_Object) aGeomObject =
238         Handle(HYDROData_Object)::DownCast( anObject );
239
240       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
241
242       setShape( anObjShape, false, false );
243
244       QColor aFillingColor = aGeomObject->GetFillingColor();
245       QColor aBorderColor = aGeomObject->GetBorderColor();
246
247       //setFillingColor( aFillingColor, false, false );
248       //setBorderColor( aBorderColor, false, false );
249     }
250     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
251     {
252       Handle(HYDROData_DummyObject3D) anObject3D =
253         Handle(HYDROData_DummyObject3D)::DownCast( anObject );
254       TopoDS_Shape aShape3D = anObject3D->GetShape();
255
256       setShape( aShape3D, false, false );
257
258       QColor aFillingColor = anObject3D->GetFillingColor();
259       QColor aBorderColor = anObject3D->GetBorderColor();
260
261       //setFillingColor( aFillingColor, false, false );
262       //setBorderColor( aBorderColor, false, false );
263     }
264   }
265 }
266
267 void HYDROGUI_VTKPrsShape::setWire( const TopoDS_Wire& theWire,
268                                     const bool         theToDisplay,
269                                     const bool         theIsUpdateViewer )
270 {
271   myTopoShape = theWire;
272   myDisplayMode = GEOM_Actor::eWireframe;
273 }
274
275 void HYDROGUI_VTKPrsShape::setFaces( const TopoDS_Compound& theWires,
276                                      const bool             theToDisplay,
277                                      const bool             theIsUpdateViewer )
278 {
279   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
280   TopoDS_Compound aCompound;
281   BRep_Builder aBuilder;
282     aBuilder.MakeCompound( aCompound );
283
284   for ( ; anExp.More(); anExp.Next() ) {
285     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
286     if ( aWire.IsNull() ) {
287       continue;
288     }
289
290     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
291     aMakeFace.Build();
292     if( aMakeFace.IsDone() ) {
293       aBuilder.Add( aCompound, aMakeFace.Face() );
294     }
295   }
296
297   myTopoShape = aCompound;
298   //myDisplayMode = GEOM_Actor::eShading;
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   //myDisplayMode = GEOM_Actor::eShading;
320 }
321
322 void HYDROGUI_VTKPrsShape::setShape( const TopoDS_Shape& theShape,
323                                      const bool          theToDisplay,
324                                      const bool          theIsUpdateViewer )
325 {
326   myTopoShape = theShape;
327   //myDisplayMode = GEOM_Actor::eShading;
328 }