Salome HOME
refs #1330: draft preferences implementation for polyline arrow
[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 QList<Handle(AIS_InteractiveObject)> HYDROGUI_ShapeBathymetry::createShape() const
62 {
63   QList<Handle(AIS_InteractiveObject)> shapes;
64
65   Handle(AIS_InteractiveObject) aPntCloud;
66
67   Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( getObject() );
68   if( !aBath.IsNull() )
69   {
70     aPntCloud = new HYDROGUI_BathymetryPrs();
71     aPntCloud->SetHilightMode( AIS_PointCloud::DM_BndBox );
72     aPntCloud->Attributes()->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_WHITE, 2.0));
73
74     const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
75     int aLower = 0;
76     int anUpper = (int)aBathPoints.size()-1;
77
78     HYDROGUI_ShapeBathymetry* aThat = const_cast<HYDROGUI_ShapeBathymetry*>( this );
79     aThat->myCoords = new TColgp_HArray1OfPnt( aLower, anUpper );
80     aThat->myColors = new Quantity_HArray1OfColor( aLower, anUpper );
81     for( int i=aLower; i<=anUpper; i++ )
82       aThat->myCoords->SetValue( i, gp_Pnt( aBathPoints[i].X, aBathPoints[i].Y, aBathPoints[i].Z ) );
83
84     shapes.append( aPntCloud );
85   }
86
87   return shapes;
88 }
89
90 void HYDROGUI_ShapeBathymetry::GetRange( double& theMin, double& theMax ) const
91 {
92   theMin = 0;
93   theMax = 0;
94   if( myCoords.IsNull() )
95     return;
96
97   bool isFirst = true;
98   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
99   {
100     double aValue = myCoords->Value( i ).Z();
101     if( isFirst || aValue < theMin )
102       theMin = aValue;
103     if( isFirst || aValue > theMax )
104       theMax = aValue;
105     isFirst = false;
106   }
107 }
108
109 void HYDROGUI_ShapeBathymetry::UpdateWithColorScale( const Handle(AIS_ColorScale)& theColorScale )
110 {
111   if (!myCoords)
112     return;
113   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
114   {
115     double z = myCoords->Value( i ).Z();
116     Quantity_Color aColor;
117     theColorScale->FindColor( z, aColor );
118     myColors->SetValue( i, aColor );
119   }
120   Handle(AIS_PointCloud) aPntCloud = Handle(AIS_PointCloud)::DownCast( getAISObjects()[0] );
121   aPntCloud->SetPoints( myCoords, myColors );
122   getContext()->Redisplay( aPntCloud, Standard_False );
123 }
124
125 void HYDROGUI_ShapeBathymetry::setVisible( const bool theState,
126                                            const bool theIsUpdateViewer )
127 {
128   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
129   bool isChanged = ( isShown != theState );
130   HYDROGUI_Shape::setVisible( theState, theIsUpdateViewer );
131   setToUpdateColorScale( isChanged );
132 }
133
134 void HYDROGUI_ShapeBathymetry::displayShape( const bool theIsUpdateViewer )
135 {
136   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
137   bool isChanged = ( !isShown  );
138   HYDROGUI_Shape::displayShape( theIsUpdateViewer );
139   setToUpdateColorScale( isChanged );
140 }
141
142 void HYDROGUI_ShapeBathymetry::display( const bool theIsUpdateViewer )
143 {
144   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
145   bool isChanged = ( !isShown  );
146   HYDROGUI_Shape::display( theIsUpdateViewer );
147   setToUpdateColorScale( isChanged );
148 }
149
150 void HYDROGUI_ShapeBathymetry::erase( const bool theIsUpdateViewer )
151 {
152   bool isShown = getContext()->IsDisplayed( getAISObjects()[0] );
153   bool isChanged = ( isShown  );
154   HYDROGUI_Shape::erase( theIsUpdateViewer );
155   setToUpdateColorScale( isChanged );
156 }
157
158 void HYDROGUI_ShapeBathymetry::setToUpdateColorScale( bool isChanged )
159 {
160 #ifndef LIGHT_MODE
161   if( isChanged && myDisplayer )
162     myDisplayer->SetToUpdateColorScale();
163 #endif
164 }