Salome HOME
debug of DTM object
[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 = 
196             Handle(HYDROData_Entity)::DownCast( anIter.Value() );
197           if ( aRefbject.IsNull() )
198             continue;
199
200           QString aRefObjectName = aRefbject->GetName();
201           if ( aRefObjectName.isEmpty() )
202             continue;
203
204           anObjectsNames.append( aRefObjectName );
205         }
206
207         //setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
208       }
209     }
210     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
211     {
212     }
213     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
214     {
215       Handle(HYDROData_Profile) aProfile =
216         Handle(HYDROData_Profile)::DownCast( anObject );
217
218       TopoDS_Wire aProfileWire;
219
220       if ( aProfile->IsValid() ) {
221         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
222
223         if ( !aProfileShape.IsNull() && 
224              aProfileShape.ShapeType() == TopAbs_WIRE ) {
225           aProfileWire = TopoDS::Wire( aProfileShape );
226         }
227       }
228
229       setWire( aProfileWire, false, false );  
230
231       QColor aWireColor = aProfile->GetBorderColor();
232       //setBorderColor( aWireColor, false, false );
233     }
234     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
235               anObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
236               anObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
237     {
238       Handle(HYDROData_Object) aGeomObject =
239         Handle(HYDROData_Object)::DownCast( anObject );
240
241       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
242
243       setShape( anObjShape, false, false );
244
245       QColor aFillingColor = aGeomObject->GetFillingColor();
246       QColor aBorderColor = aGeomObject->GetBorderColor();
247
248       //setFillingColor( aFillingColor, false, false );
249       //setBorderColor( aBorderColor, false, false );
250     }
251     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
252     {
253       Handle(HYDROData_DummyObject3D) anObject3D =
254         Handle(HYDROData_DummyObject3D)::DownCast( anObject );
255       TopoDS_Shape aShape3D = anObject3D->GetShape();
256
257       setShape( aShape3D, false, false );
258
259       QColor aFillingColor = anObject3D->GetFillingColor();
260       QColor aBorderColor = anObject3D->GetBorderColor();
261
262       //setFillingColor( aFillingColor, false, false );
263       //setBorderColor( aBorderColor, false, false );
264     }
265   }
266 }
267
268 void HYDROGUI_VTKPrsShape::setWire( const TopoDS_Wire& theWire,
269                                     const bool         theToDisplay,
270                                     const bool         theIsUpdateViewer )
271 {
272   myTopoShape = theWire;
273   myDisplayMode = GEOM_Actor::eWireframe;
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   //myDisplayMode = GEOM_Actor::eShading;
300 }
301
302 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Wire& theWire,
303                                     const bool         theToDisplay,
304                                     const bool         theIsUpdateViewer )
305 {
306   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
307   aFaceBuilder.Build();
308   if( aFaceBuilder.IsDone() )
309   {
310     TopoDS_Face aFace = aFaceBuilder.Face();
311     setFace( aFace, theToDisplay, theIsUpdateViewer );
312   }
313 }
314
315 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Face& theFace,
316                                     const bool         theToDisplay,
317                                     const bool         theIsUpdateViewer )
318 {
319   myTopoShape = theFace;
320   //myDisplayMode = GEOM_Actor::eShading;
321 }
322
323 void HYDROGUI_VTKPrsShape::setShape( const TopoDS_Shape& theShape,
324                                      const bool          theToDisplay,
325                                      const bool          theIsUpdateViewer )
326 {
327   myTopoShape = theShape;
328   //myDisplayMode = GEOM_Actor::eShading;
329 }