Salome HOME
Initial merge of branch 'BR_HYDRO_IMPS_2016' into BR_PORTING_OCCT_7
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ShapeBathymetry.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_ShapeBathymetry.h>
20 #include <HYDROGUI_OCCDisplayer.h>
21 #include <HYDROGUI_BathymetryPrs.h>
22 #include <HYDROData_Bathymetry.h>
23
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_ColorScale.hxx>
26 #include <Prs3d_PointAspect.hxx>
27
28 HYDROGUI_ShapeBathymetry::HYDROGUI_ShapeBathymetry( HYDROGUI_OCCDisplayer*                theDisplayer,
29                                                     const Handle(AIS_InteractiveContext)& theContext,
30                                                     const Handle(HYDROData_Bathymetry)&   theBathymetry,
31                                                     const int                             theZLayer )
32 : HYDROGUI_Shape( theContext, theBathymetry, theZLayer ),
33   myDisplayer( theDisplayer )
34 {
35   setDisplayMode( AIS_PointCloud::DM_Points );
36 }
37
38 HYDROGUI_ShapeBathymetry::~HYDROGUI_ShapeBathymetry()
39 {
40   setToUpdateColorScale( true );
41 }
42
43 void HYDROGUI_ShapeBathymetry::update( bool theIsUpdateViewer, bool isDeactivateSelection )
44 {
45   setIsToUpdate( false );
46
47   // Try to retrieve information from object
48
49   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
50
51   if ( !aBath.IsNull() && !aBath->GetAltitudePoints().empty())
52   {
53     buildShape();
54     updateShape( false, false );
55   }
56
57   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
58 }
59
60
61 Handle(AIS_InteractiveObject) HYDROGUI_ShapeBathymetry::createShape() const
62 {
63   Handle(AIS_InteractiveObject) aPntCloud;
64
65   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
66   if( !aBath.IsNull() )
67   {
68     aPntCloud = new HYDROGUI_BathymetryPrs();
69     aPntCloud->SetHilightMode( AIS_PointCloud::DM_BndBox );
70     aPntCloud->Attributes()->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_WHITE, 2.0));
71
72     const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
73     int aLower = 0;
74     int anUpper = (int)aBathPoints.size()-1;
75
76     HYDROGUI_ShapeBathymetry* aThat = const_cast<HYDROGUI_ShapeBathymetry*>( this );
77     aThat->myCoords = new TColgp_HArray1OfPnt( aLower, anUpper );
78     aThat->myColors = new Quantity_HArray1OfColor( aLower, anUpper );
79     for( int i=aLower; i<=anUpper; i++ )
80       aThat->myCoords->SetValue( i, gp_Pnt( aBathPoints[i].X, aBathPoints[i].Y, aBathPoints[i].Z ) );
81   }
82
83   return aPntCloud;
84 }
85
86 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
87 {
88   theMin = 0;
89   theMax = 0;
90   if( myCoords.IsNull() )
91     return;
92
93   bool isFirst = true;
94   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
95   {
96     double aValue = myCoords->Value( i ).Z();
97     if( isFirst || aValue < theMin )
98       theMin = aValue;
99     if( isFirst || aValue > theMax )
100       theMax = aValue;
101     isFirst = false;
102   }
103 }
104
105 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
106 {
107   if (!myCoords)
108     return;
109   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
110   {
111     double z = myCoords->Value( i ).Z();
112     Quantity_Color aColor;
113     theColorScale->FindColor( z, aColor );
114     myColors->SetValue( i, aColor );
115   }
116   Handle(AIS_PointCloud) aPntCloud = Handle(AIS_PointCloud)::DownCast( getAISObject() );
117   aPntCloud->SetPoints( myCoords, myColors );
118   getContext()->Redisplay( aPntCloud, Standard_False );
119 }
120
121 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
122                                            const bool theIsUpdateViewer )
123 {
124   bool isShown = getContext()->IsDisplayed( getAISObject() );
125   bool isChanged = ( isShown != theState );
126   HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
127   setToUpdateColorScale( isChanged );
128 }
129
130 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
131 {
132   bool isShown = getContext()->IsDisplayed( getAISObject() );
133   bool isChanged = ( !isShown  );
134   HYDROGUI_Shape::displayShape( theIsUpdateViewer );
135   setToUpdateColorScale( isChanged );
136 }
137
138 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
139 {
140   bool isShown = getContext()->IsDisplayed( getAISObject() );
141   bool isChanged = ( !isShown  );
142   HYDROGUI_Shape::display( theIsUpdateViewer );
143   setToUpdateColorScale( isChanged );
144 }
145
146 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
147 {
148   bool isShown = getContext()->IsDisplayed( getAISObject() );
149   bool isChanged = ( isShown  );
150   HYDROGUI_Shape::erase( theIsUpdateViewer );
151   setToUpdateColorScale( isChanged );
152 }
153
154 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
155 {
156 #ifndef LIGHT_MODE
157   if( isChanged && myDisplayer )
158     myDisplayer->SetToUpdateColorScale();
159 #endif
160 }