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