Salome HOME
Bathymetry prs corrected according to new API.
[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   HYDROGUI_VTKPrs::compute();
71
72   if ( !getObject().IsNull() )
73   {
74     Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
75     if ( !aBathymetry.IsNull() )
76     {
77       HYDROData_Bathymetry::AltitudePoints anAltPoints = aBathymetry->GetAltitudePoints();
78       int aNbPoints = anAltPoints.Length();
79
80       HYDROData_Bathymetry::AltitudePoint anAltPnt;
81       vtkPoints* aPoints = vtkPoints::New();
82       aPoints->SetNumberOfPoints( aNbPoints );
83
84       vtkPolyData* aVertexGrid = vtkPolyData::New();
85       aVertexGrid->Allocate( aNbPoints );
86
87       vtkDoubleArray* aZValues = vtkDoubleArray::New();
88       aZValues->Allocate( aNbPoints );
89
90       vtkVertex* aVertex = vtkVertex::New();
91
92       double aZ;
93       int anInvalidZ = InvalidZValue();
94       for (int i = 1; i <= aNbPoints; i++ )
95       {
96         anAltPnt = anAltPoints.Value( i );
97         aZ = anAltPnt.Z();
98         if ( ValuesLessEquals( aZ, anInvalidZ ) )
99         {
100           aZ = Z_MAX; // If Z value is invalid then use Z_MAX
101         }
102         aPoints->InsertPoint( i, anAltPnt.X(), anAltPnt.Y(), aZ );
103         aVertex->GetPointIds()->SetId( 0, i );
104         aVertexGrid->InsertNextCell( aVertex->GetCellType(), aVertex->GetPointIds());
105         aZValues->InsertNextValue( aZ );
106       }
107
108       aVertex->Delete();
109
110       aVertexGrid->SetPoints( aPoints );
111       aVertexGrid->GetPointData()->SetScalars( aZValues );
112       
113       // Update the lookup table range if this bathymetry is out of it
114       if ( myLookupTable )
115       {
116         aZValues->GetRange( myInternalZRange );
117         double* aGlobalRange = myLookupTable->GetRange();
118         // If the global range is not yet initialized or the current one is out of scope then update the global
119         bool anIsUpdated = false;
120         if (  ValuesEquals( aGlobalRange[0], anInvalidZ ) || ( aGlobalRange[0] > myInternalZRange[0] ) )
121         {
122           aGlobalRange[0] = myInternalZRange[0];
123           anIsUpdated = true;
124         }
125
126         if (  ValuesEquals( aGlobalRange[1], anInvalidZ ) || ( aGlobalRange[1] < myInternalZRange[1] ) )
127         {
128           aGlobalRange[1] = myInternalZRange[1];
129           anIsUpdated = true;
130         }
131
132         if ( anIsUpdated )
133         {
134           myLookupTable->SetRange( aGlobalRange );
135           myLookupTable->Build();
136         }
137
138         myMapper->SetScalarRange( aGlobalRange );
139         myMapper->ScalarVisibilityOn();
140         myMapper->SetScalarModeToUsePointData();
141         myMapper->SetLookupTable( myLookupTable );
142       }
143
144       myMapper->SetInputData( aVertexGrid );
145       
146       SALOME_Actor* anActor = getActor<SALOME_Actor>(this);
147       anActor->SetMapper( myMapper.GetPointer() );
148       anActor->setIO( getIO() );
149       AddObject( anActor );
150
151       aVertexGrid->Delete();
152       aZValues->Delete();
153     }
154   }
155 }