Salome HOME
- Bathymethries are colored now
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsBathymetry.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_VTKPrsBathymetry.h"
24
25 #include <HYDROData_Entity.h>
26 #include <HYDROData_Tool.h>
27
28 #include <SALOME_Actor.h>
29 #include <gp_XYZ.hxx>
30 #include <vtkDoubleArray.h>
31 #include <vtkPoints.h>
32 #include <vtkPolyData.h>
33 #include <vtkPointData.h>
34 #include <vtkPolyDataMapper.h>
35 #include <vtkVertex.h>
36 #include <vtkLookupTable.h>
37
38 #include <QString>
39
40 #define NB_COLORS 32
41 #define Z_MIN -100
42 #define Z_MAX 1
43 #define HUE_START 0.69 
44 #define HUE_END   0.41
45 #define SATURATION_START 1.0 
46 #define SATURATION_END   0.4
47
48 //=======================================================================
49 // name    : HYDROGUI_VTKPrsBathymetry
50 // Purpose : Constructor
51 //=======================================================================
52 HYDROGUI_VTKPrsBathymetry::HYDROGUI_VTKPrsBathymetry( const Handle(HYDROData_Bathymetry)& theObject )
53 : HYDROGUI_VTKPrs( theObject )
54 {
55 }
56
57 //=======================================================================
58 // name    : HYDROGUI_VTKPrsBathymetry
59 // Purpose : Destructor
60 //=======================================================================
61 HYDROGUI_VTKPrsBathymetry::~HYDROGUI_VTKPrsBathymetry()
62 {
63 }
64
65 //================================================================
66 // Function : compute
67 // Purpose  : 
68 //================================================================
69 void HYDROGUI_VTKPrsBathymetry::compute()
70 {
71   if ( !getObject().IsNull() )
72   {
73     Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
74     if ( !aBathymetry.IsNull() )
75     {
76       HYDROData_Bathymetry::AltitudePoints anAltPoints = aBathymetry->GetAltitudePoints();
77       int aNbPoints = anAltPoints.length();
78
79       HYDROData_Bathymetry::AltitudePoint anAltPnt;
80       vtkPoints* aPoints = vtkPoints::New();
81       aPoints->SetNumberOfPoints( aNbPoints );
82
83       vtkPolyData* aVertexGrid = vtkPolyData::New();
84       aVertexGrid->Allocate( aNbPoints );
85
86       vtkDoubleArray* aZValues = vtkDoubleArray::New();
87       aZValues->Allocate( aNbPoints );
88
89       vtkVertex* aVertex = vtkVertex::New();
90
91       int aZ;
92       int anInvalidZ = aBathymetry->GetInvalidAltitude();
93       for (int i = 0; i < aNbPoints; i++ )
94       {
95         anAltPnt = anAltPoints.at( i );
96         aZ = anAltPnt.Z();
97         if ( ValuesLessEquals( aZ, anInvalidZ ) )
98         {
99           aZ = Z_MAX;
100         }
101         else
102         {
103           aZ = -aZ;
104         }
105         aPoints->InsertPoint( i, anAltPnt.X(), anAltPnt.Y(), aZ );
106         aVertex->GetPointIds()->SetId( 0, i );
107         aVertexGrid->InsertNextCell( aVertex->GetCellType(), aVertex->GetPointIds());
108         aZValues->InsertNextValue( aZ );
109       }
110
111       aVertex->Delete();
112
113       aVertexGrid->SetPoints( aPoints );
114       aVertexGrid->GetPointData()->SetScalars( aZValues );
115       
116       vtkLookupTable* aLut = vtkLookupTable::New();
117       aLut->SetHueRange( HUE_START, HUE_END );
118       aLut->SetSaturationRange( SATURATION_START, SATURATION_END );
119       aLut->SetTableRange( Z_MIN, Z_MAX );
120       aLut->SetValueRange( 1.0, 1.0 );
121       aLut->SetAlphaRange( 1.0, 1.0 );
122       aLut->SetNumberOfColors( NB_COLORS );
123       aLut->Build();
124
125       vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
126       aMapper->SetScalarRange( Z_MIN, Z_MAX );
127       aMapper->ScalarVisibilityOn();
128       aMapper->SetScalarModeToUsePointData();
129       aMapper->SetLookupTable( aLut );
130       aMapper->SetInputData( aVertexGrid );
131       
132       SALOME_Actor* anActor = SALOME_Actor::New();
133       anActor->SetMapper( aMapper );
134       anActor->setIO( getIO() );
135       AddObject( anActor );
136     }
137   }
138 }