Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[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 #include <vtkLookupTable.h>
38
39 #include <QString>
40
41 /*! \def Z_MAX
42     \brief Maximum Z value used in bathymetry presentation.
43
44     This value is used instead of invalid values.
45 */
46 #define Z_MAX 1
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 = InvalidZValue();
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       // Update the lookup table range if this bathymetry is out of it
117       if ( myLookupTable )
118       {
119         double* aRange = aZValues->GetRange();
120         myInternalZRange[0] = -aRange[1];
121         myInternalZRange[1] = -aRange[0];
122
123         double* aGlobalRange = myLookupTable->GetRange();
124         // If the global range is not yet initialized or the current one is out of scope then update the global
125         bool anIsUpdated = false;
126         if (  ValuesEquals( aGlobalRange[0], anInvalidZ ) || ( aGlobalRange[0] > myInternalZRange[0] ) )
127         {
128           aGlobalRange[0] = myInternalZRange[0];
129           anIsUpdated = true;
130         }
131
132         if (  ValuesEquals( aGlobalRange[1], anInvalidZ ) || ( aGlobalRange[1] < myInternalZRange[1] ) )
133         {
134           aGlobalRange[1] = myInternalZRange[1];
135           anIsUpdated = true;
136         }
137
138         if ( anIsUpdated )
139         {
140           myLookupTable->SetRange( aGlobalRange );
141           myLookupTable->Build();
142         }
143
144         myMapper->SetScalarRange( -aGlobalRange[1], -aGlobalRange[0] );
145         myMapper->ScalarVisibilityOn();
146         myMapper->SetScalarModeToUsePointData();
147
148
149         vtkLookupTable* aTable = vtkLookupTable::New();
150         vtkLookupTable* aGlobalTable = vtkLookupTable::SafeDownCast( myLookupTable );
151         double* aHueRange = aGlobalTable->GetHueRange();
152         // Revert hue range to keep it the same as for appropriate positive Z values in scalar bar
153         aTable->SetHueRange( aHueRange[1], aHueRange[0] );
154         aTable->SetSaturationRange( aGlobalTable->GetSaturationRange() );
155         // Revert the Z values range from positive to negative to show them as depth
156         aTable->SetTableRange( -aGlobalRange[1], -aGlobalRange[0] );
157         aTable->SetValueRange( aGlobalTable->GetValueRange() );
158         aTable->SetAlphaRange( aGlobalTable->GetAlphaRange() );
159         aTable->SetNumberOfColors( aGlobalTable->GetNumberOfColors() );
160         aTable->Build();
161
162         myMapper->SetLookupTable( aTable );
163         aTable->Delete();
164 //        myMapper->SetLookupTable( myLookupTable );
165       }
166
167       myMapper->SetInputData( aVertexGrid );
168       
169       SALOME_Actor* anActor = SALOME_Actor::New();
170       anActor->SetMapper( myMapper.GetPointer() );
171       anActor->setIO( getIO() );
172       AddObject( anActor );
173
174       anActor->Delete();
175       aVertexGrid->Delete();
176       aZValues->Delete();
177     }
178   }
179 }