Salome HOME
Merge remote-tracking branch 'origin/BR_SHAPE_RECOGNITION' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsBathymetry.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_VTKPrsBathymetry.h"
20
21 #include <HYDROData_Entity.h>
22 #include <HYDROData_Tool.h>
23
24 #include <SALOME_Actor.h>
25 #include <gp_Pnt.hxx>
26 #include <vtkDoubleArray.h>
27 #include <vtkPoints.h>
28 #include <vtkPolyData.h>
29 #include <vtkPointData.h>
30 #include <vtkPolyDataMapper.h>
31 #include <vtkVertex.h>
32 #include <vtkScalarBarActor.h>
33
34 #include <QString>
35
36 /*! \def Z_MAX
37     \brief Maximum Z value used in bathymetry presentation.
38
39     This value is used instead of invalid values.
40 */
41 #define Z_MAX 1
42
43 //=======================================================================
44 // name    : HYDROGUI_VTKPrsBathymetry
45 // Purpose : Constructor
46 //=======================================================================
47 HYDROGUI_VTKPrsBathymetry::HYDROGUI_VTKPrsBathymetry( const Handle(HYDROData_Bathymetry)& theObject )
48 : HYDROGUI_VTKPrs( theObject )
49 {
50 }
51
52 //=======================================================================
53 // name    : HYDROGUI_VTKPrsBathymetry
54 // Purpose : Destructor
55 //=======================================================================
56 HYDROGUI_VTKPrsBathymetry::~HYDROGUI_VTKPrsBathymetry()
57 {
58 }
59
60 //================================================================
61 // Function : compute
62 // Purpose  : 
63 //================================================================
64 void HYDROGUI_VTKPrsBathymetry::compute()
65 {
66   HYDROGUI_VTKPrs::compute();
67
68   if ( !getObject().IsNull() )
69   {
70     Handle(HYDROData_Bathymetry) aBathymetry = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
71     if ( !aBathymetry.IsNull() )
72     {
73       HYDROData_Bathymetry::AltitudePoints anAltPoints = aBathymetry->GetAltitudePoints();
74       int aNbPoints = anAltPoints.Length();
75
76       HYDROData_Bathymetry::AltitudePoint anAltPnt;
77       vtkPoints* aPoints = vtkPoints::New();
78       aPoints->SetNumberOfPoints( aNbPoints );
79
80       vtkPolyData* aVertexGrid = vtkPolyData::New();
81       aVertexGrid->Allocate( aNbPoints );
82
83       vtkDoubleArray* aZValues = vtkDoubleArray::New();
84       aZValues->Allocate( aNbPoints );
85
86       vtkVertex* aVertex = vtkVertex::New();
87
88       double aZ;
89       int anInvalidZ = InvalidZValue();
90       for (int i = 1; i <= aNbPoints; i++ )
91       {
92         anAltPnt = anAltPoints.Value( i );
93         aZ = anAltPnt.Z();
94         if ( ValuesLessEquals( aZ, anInvalidZ ) )
95         {
96           aZ = Z_MAX; // If Z value is invalid then use Z_MAX
97         }
98         aPoints->InsertPoint( i - 1, anAltPnt.X(), anAltPnt.Y(), aZ );
99         aVertex->GetPointIds()->SetId( 0, i - 1 );
100         aVertexGrid->InsertNextCell( aVertex->GetCellType(), aVertex->GetPointIds());
101         aZValues->InsertNextValue( aZ );
102       }
103
104       aVertex->Delete();
105
106       aVertexGrid->SetPoints( aPoints );
107       aVertexGrid->GetPointData()->SetScalars( aZValues );
108       
109       // Update the lookup table range if this bathymetry is out of it
110       if ( myLookupTable )
111       {
112         aZValues->GetRange( myInternalZRange );
113         double* aGlobalRange = myLookupTable->GetRange();
114         // If the global range is not yet initialized or the current one is out of scope then update the global
115         bool anIsUpdated = false;
116         if (  ValuesEquals( aGlobalRange[0], anInvalidZ ) || ( aGlobalRange[0] > myInternalZRange[0] ) )
117         {
118           aGlobalRange[0] = myInternalZRange[0];
119           anIsUpdated = true;
120         }
121
122         if (  ValuesEquals( aGlobalRange[1], anInvalidZ ) || ( aGlobalRange[1] < myInternalZRange[1] ) )
123         {
124           aGlobalRange[1] = myInternalZRange[1];
125           anIsUpdated = true;
126         }
127
128         if ( anIsUpdated )
129         {
130           myLookupTable->SetRange( aGlobalRange );
131           myLookupTable->Build();
132         }
133
134         myMapper->SetScalarRange( aGlobalRange );
135         myMapper->ScalarVisibilityOn();
136         myMapper->SetScalarModeToUsePointData();
137         myMapper->SetLookupTable( myLookupTable );
138       }
139
140       myMapper->SetInputData( aVertexGrid );
141       
142       SALOME_Actor* anActor = getActor<SALOME_Actor>(this);
143       anActor->SetMapper( myMapper.GetPointer() );
144       anActor->setIO( getIO() );
145       AddObject( anActor );
146
147       aVertexGrid->Delete();
148       aZValues->Delete();
149     }
150   }
151 }