Salome HOME
381923d88e483bd101664345e9bf874b6d2e7c53
[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 <vtkScalarBarActor.h>
37
38 #include <QString>
39
40 /*! \def Z_MAX
41     \brief Maximum Z value used in bathymetry presentation.
42
43     This value is used instead of invalid values.
44 */
45 #define Z_MAX 1
46
47 //=======================================================================
48 // name    : HYDROGUI_VTKPrsBathymetry
49 // Purpose : Constructor
50 //=======================================================================
51 HYDROGUI_VTKPrsBathymetry::HYDROGUI_VTKPrsBathymetry( const Handle(HYDROData_Bathymetry)& theObject )
52 : HYDROGUI_VTKPrs( theObject )
53 {
54 }
55
56 //=======================================================================
57 // name    : HYDROGUI_VTKPrsBathymetry
58 // Purpose : Destructor
59 //=======================================================================
60 HYDROGUI_VTKPrsBathymetry::~HYDROGUI_VTKPrsBathymetry()
61 {
62 }
63
64 //================================================================
65 // Function : compute
66 // Purpose  : 
67 //================================================================
68 void HYDROGUI_VTKPrsBathymetry::compute()
69 {
70   if ( !getObject().IsNull() )
71   {
72     Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
73     if ( !aBathymetry.IsNull() )
74     {
75       HYDROData_Bathymetry::AltitudePoints anAltPoints = aBathymetry->GetAltitudePoints();
76       int aNbPoints = anAltPoints.length();
77
78       HYDROData_Bathymetry::AltitudePoint anAltPnt;
79       vtkPoints* aPoints = vtkPoints::New();
80       aPoints->SetNumberOfPoints( aNbPoints );
81
82       vtkPolyData* aVertexGrid = vtkPolyData::New();
83       aVertexGrid->Allocate( aNbPoints );
84
85       vtkDoubleArray* aZValues = vtkDoubleArray::New();
86       aZValues->Allocate( aNbPoints );
87
88       vtkVertex* aVertex = vtkVertex::New();
89
90       int aZ;
91       int anInvalidZ = InvalidZValue();
92       for (int i = 0; i < aNbPoints; i++ )
93       {
94         anAltPnt = anAltPoints.at( i );
95         aZ = anAltPnt.Z();
96         if ( ValuesLessEquals( aZ, anInvalidZ ) )
97         {
98           aZ = Z_MAX;
99         }
100         else
101         {
102           aZ = -aZ;
103         }
104         aPoints->InsertPoint( i, anAltPnt.X(), anAltPnt.Y(), aZ );
105         aVertex->GetPointIds()->SetId( 0, i );
106         aVertexGrid->InsertNextCell( aVertex->GetCellType(), aVertex->GetPointIds());
107         aZValues->InsertNextValue( aZ );
108       }
109
110       aVertex->Delete();
111
112       aVertexGrid->SetPoints( aPoints );
113       aVertexGrid->GetPointData()->SetScalars( aZValues );
114       
115       //vtkLookupTable* aLut = vtkLookupTable::New();
116       //aLut->SetHueRange( HUE_START, HUE_END );
117       //aLut->SetSaturationRange( SATURATION_START, SATURATION_END );
118       //aLut->SetTableRange( Z_MIN, Z_MAX );
119       //aLut->SetValueRange( 1.0, 1.0 );
120       //aLut->SetAlphaRange( 1.0, 1.0 );
121       //aLut->SetNumberOfColors( NB_COLORS );
122       //aLut->Build();
123       
124       // Update the lookup table range if this bathymetry is out of it
125       if ( myLookupTable )
126       {
127         aZValues->GetRange( myInternalZRange );
128
129         double* aGlobalRange = myLookupTable->GetRange();
130         // If the global range is not yet initialized or the current one is out of scope then update the global
131         bool anIsUpdated;
132         if (  ValuesEquals( aGlobalRange[0], anInvalidZ ) || ( aGlobalRange[0] > myInternalZRange[0] ) )
133         {
134           aGlobalRange[0] = myInternalZRange[0];
135           anIsUpdated = true;
136         }
137
138         if (  ValuesEquals( aGlobalRange[1], anInvalidZ ) || ( aGlobalRange[1] < myInternalZRange[1] ) )
139         {
140           aGlobalRange[1] = myInternalZRange[1];
141           anIsUpdated = true;
142         }
143
144         if ( anIsUpdated )
145         {
146           myLookupTable->SetRange( aGlobalRange );
147           myLookupTable->Build();
148         }
149
150         myMapper->SetScalarRange( aGlobalRange );
151         myMapper->ScalarVisibilityOn();
152         myMapper->SetScalarModeToUsePointData();
153         myMapper->SetLookupTable( myLookupTable );
154       }
155
156       myMapper->SetInputData( aVertexGrid );
157       
158       SALOME_Actor* anActor = SALOME_Actor::New();
159       anActor->SetMapper( myMapper.GetPointer() );
160       anActor->setIO( getIO() );
161       AddObject( anActor );
162     }
163   }
164 }