Salome HOME
lot 3 - coloring of section in OCC view see also refs #1838
[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_BCPolygon.h>
28 #include <HYDROData_Obstacle.h>
29 #include <HYDROData_PolylineXY.h>
30 #include <HYDROData_Polyline3D.h>
31 #include <HYDROData_Profile.h>
32 #include <HYDROData_Region.h>
33 #include <HYDROData_Stream.h>
34 #include <HYDROData_Zone.h>
35
36 #include <BRepBuilderAPI_MakeEdge.hxx>
37 #include <BRepBuilderAPI_MakeWire.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39
40 #include <gp_Pnt.hxx>
41
42 #include <Graphic3d_AspectFillArea3d.hxx>
43 #include <Graphic3d_MaterialAspect.hxx>
44
45 #include <TopoDS.hxx>
46 #include <TopoDS_Wire.hxx>
47 #include <TopoDS_Face.hxx>
48
49 #include <TopExp_Explorer.hxx>
50
51 #include <BRep_Builder.hxx>
52
53 #include <Precision.hxx>
54
55 #include <HYDROGUI_Actor.h>
56 #include <vtkScalarBarActor.h>
57
58 #include <QString>
59
60 // Hard-coded value of shape deflection coefficient for VTK viewer
61 const double VTK_MIN_DEFLECTION = 0.001;
62
63
64 //=======================================================================
65 // name    : HYDROGUI_VTKPrsShape
66 // Purpose : Constructor
67 //=======================================================================
68 HYDROGUI_VTKPrsShape::HYDROGUI_VTKPrsShape( const Handle(HYDROData_Entity)& theObject )
69 : HYDROGUI_VTKPrs( theObject ),
70 myDisplayMode( GEOM_Actor::eWireframe )
71 {
72 }
73
74 //=======================================================================
75 // name    : HYDROGUI_VTKPrsShape
76 // Purpose : Destructor
77 //=======================================================================
78 HYDROGUI_VTKPrsShape::~HYDROGUI_VTKPrsShape()
79 {
80 }
81
82 //================================================================
83 // Function : compute
84 // Purpose  : 
85 //================================================================
86 void HYDROGUI_VTKPrsShape::compute()
87 {
88   HYDROGUI_VTKPrs::compute();
89
90   if ( !getObject().IsNull() )
91   {
92     buildShape();
93
94     if ( !myTopoShape.IsNull() )
95     {
96       HYDROGUI_Actor* anActor = getActor<HYDROGUI_Actor>(this);
97       anActor->SetShape( myTopoShape, VTK_MIN_DEFLECTION );
98       anActor->setDisplayMode( myDisplayMode );
99       anActor->setIO( getIO() );
100     }
101   }
102 }
103
104
105 void HYDROGUI_VTKPrsShape::buildShape()
106 {
107   Handle(HYDROData_Entity) anObject = getObject();
108   // Try to retrieve information from object
109   if ( !anObject.IsNull() )
110   {
111     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( anObject->Label() );
112   
113     if ( anObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
114     {
115       Handle(HYDROData_ImmersibleZone) aZoneObj =
116         Handle(HYDROData_ImmersibleZone)::DownCast( anObject );
117
118       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
119       if ( !aZoneShape.IsNull() ) {
120         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
121           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
122           setFace( aZoneFace, false, false );
123         } else {
124           myTopoShape = aZoneShape;
125         }
126       }
127
128       QColor aFillingColor = aZoneObj->GetFillingColor();
129       QColor aBorderColor = aZoneObj->GetBorderColor();
130
131       //setFillingColor( aFillingColor, false, false );
132       //setBorderColor( aBorderColor, false, false );
133     }
134     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
135     {
136       Handle(HYDROData_PolylineXY) aPolyline =
137         Handle(HYDROData_PolylineXY)::DownCast( anObject );
138
139       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
140
141       if ( !aPolylineShape.IsNull() ) {
142         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
143           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
144           setWire( aPolylineWire, false, false );  
145         } else {
146           myTopoShape = aPolylineShape;
147           myDisplayMode = GEOM_Actor::eWireframe;
148         }
149       }
150
151       //QColor aWireColor = aPolyline->GetWireColor();
152       //setBorderColor( aWireColor, false, false );
153     }
154     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
155     {
156       Handle(HYDROData_Polyline3D) aPolyline =
157         Handle(HYDROData_Polyline3D)::DownCast( anObject );
158
159       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
160
161       if ( !aPolylineShape.IsNull() ) {
162         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
163           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
164           setWire( aPolylineWire, false, false );  
165         } else {
166           myTopoShape = aPolylineShape;
167           myDisplayMode = GEOM_Actor::eWireframe;
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 anObjectsNames;
191
192         HYDROData_SequenceOfObjects aRefObjects = aZone->GetObjects();
193         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
194         for ( ; anIter.More(); anIter.Next() )
195         {
196           Handle(HYDROData_Entity) aRefbject = 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     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_BCPolygon) ) )
266     {
267       Handle(HYDROData_BCPolygon) aBCObj =
268         Handle(HYDROData_BCPolygon)::DownCast( anObject );
269
270       TopoDS_Shape aBCShape = aBCObj->GetTopShape();
271       if ( !aBCShape.IsNull() ) 
272       {
273         if ( aBCShape.ShapeType() == TopAbs_FACE )
274         {
275           TopoDS_Face aFace = TopoDS::Face( aBCShape );
276           setFace( aFace, false, false );
277         }
278         else 
279         {
280           myTopoShape = aBCShape;
281         }
282       }
283
284       QColor aFillingColor = aBCObj->GetFillingColor();
285       QColor aBorderColor = aBCObj->GetBorderColor();
286     }
287  
288   }
289 }
290
291 void HYDROGUI_VTKPrsShape::setWire( const TopoDS_Wire& theWire,
292                                     const bool         theToDisplay,
293                                     const bool         theIsUpdateViewer )
294 {
295   myTopoShape = theWire;
296   myDisplayMode = GEOM_Actor::eWireframe;
297 }
298
299 void HYDROGUI_VTKPrsShape::setFaces( const TopoDS_Compound& theWires,
300                                      const bool             theToDisplay,
301                                      const bool             theIsUpdateViewer )
302 {
303   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
304   TopoDS_Compound aCompound;
305   BRep_Builder aBuilder;
306     aBuilder.MakeCompound( aCompound );
307
308   for ( ; anExp.More(); anExp.Next() ) {
309     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
310     if ( aWire.IsNull() ) {
311       continue;
312     }
313
314     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
315     aMakeFace.Build();
316     if( aMakeFace.IsDone() ) {
317       aBuilder.Add( aCompound, aMakeFace.Face() );
318     }
319   }
320
321   myTopoShape = aCompound;
322   //myDisplayMode = GEOM_Actor::eShading;
323 }
324
325 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Wire& theWire,
326                                     const bool         theToDisplay,
327                                     const bool         theIsUpdateViewer )
328 {
329   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
330   aFaceBuilder.Build();
331   if( aFaceBuilder.IsDone() )
332   {
333     TopoDS_Face aFace = aFaceBuilder.Face();
334     setFace( aFace, theToDisplay, theIsUpdateViewer );
335   }
336 }
337
338 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Face& theFace,
339                                     const bool         theToDisplay,
340                                     const bool         theIsUpdateViewer )
341 {
342   myTopoShape = theFace;
343   //myDisplayMode = GEOM_Actor::eShading;
344 }
345
346 void HYDROGUI_VTKPrsShape::setShape( const TopoDS_Shape& theShape,
347                                      const bool          theToDisplay,
348                                      const bool          theIsUpdateViewer )
349 {
350   myTopoShape = theShape;
351   //myDisplayMode = GEOM_Actor::eShading;
352 }