Salome HOME
refs #1327: implementation of the scaling operations
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_BathymetryPrs.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_BathymetryPrs.h>
20 #include <Prs3d_Root.hxx>
21 #include <Prs3d_LineAspect.hxx>
22 #include <Prs3d_Text.hxx>
23 #include <Prs3d_TextAspect.hxx>
24 #include <Select3D_SensitiveBox.hxx>
25 #include <Select3D_SensitivePoint.hxx>
26 #include <Graphic3d_ArrayOfPolylines.hxx>
27 #include <Graphic3d_ArrayOfPoints.hxx>
28
29 const int BATH_HIGHLIGHT_MODE = 10;
30
31 HYDROGUI_BathymetryPrs::HYDROGUI_BathymetryPrs( const HYDROGUI_ShapeBathymetry* theShape )
32   : myShape( theShape )
33 {
34   SetHilightMode( BATH_HIGHLIGHT_MODE );
35   SetAutoHilight( Standard_True );
36 }
37
38 HYDROGUI_BathymetryPrs::~HYDROGUI_BathymetryPrs()
39 {
40 }
41
42 HYDROGUI_ShapeBathymetry* HYDROGUI_BathymetryPrs::GetShape() const
43 {
44   return const_cast<HYDROGUI_ShapeBathymetry*>( myShape );
45 }
46
47 void HYDROGUI_BathymetryPrs::UpdateBound()
48 {
49   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
50   myBound.SetVoid();
51   if( !points.IsNull() )
52   {
53     int aLower = 1;
54     int anUpper = points->VertexNumber();
55
56     for( int i = aLower; i <= anUpper; i++ )
57     {
58       gp_Pnt p = points->Vertice( i );
59       if( i==aLower )
60         myBound.Set( p );
61       else
62         myBound.Update( p.X(), p.Y(), p.Z() );
63     }
64   }
65 }
66
67 void HYDROGUI_BathymetryPrs::SetPoints( const Handle(TColgp_HArray1OfPnt)&     theCoords,
68                                         const Handle(Quantity_HArray1OfColor)& theColors )
69 {
70   AIS_PointCloud::SetPoints( theCoords, theColors );
71   UpdateBound();
72 }
73
74 void HYDROGUI_BathymetryPrs::Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75                                       const Handle(Prs3d_Presentation)& thePresentation,
76                                       const Standard_Integer theMode )
77 {
78   thePresentation->Clear();
79   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
80
81   if( theMode==BATH_HIGHLIGHT_MODE )
82   {
83     if( myBound.IsVoid() )
84       UpdateBound();
85
86     if( myBound.IsVoid() || 
87         myBound.IsOpenXmin() || myBound.IsOpenXmax() ||
88         myBound.IsOpenYmin() || myBound.IsOpenYmax() ||
89         myBound.IsOpenZmin() || myBound.IsOpenZmax() )
90       return;
91
92     Standard_Real xmin, xmax, ymin, ymax, zmin, zmax;
93     myBound.Get( xmin, ymin, zmin, xmax, ymax, zmax );
94
95     Handle(Prs3d_LineAspect) aWireAspect = new Prs3d_LineAspect( Quantity_NOC_RED, Aspect_TOL_SOLID, 1.0 );
96     aGroup->SetPrimitivesAspect( aWireAspect->Aspect() );
97
98     Handle(Graphic3d_ArrayOfPolylines) aLines = new Graphic3d_ArrayOfPolylines( 18, 6 );
99     
100     aLines->AddBound( 5 );
101     aLines->AddVertex( xmin, ymin, zmin );
102     aLines->AddVertex( xmax, ymin, zmin );
103     aLines->AddVertex( xmax, ymax, zmin );
104     aLines->AddVertex( xmin, ymax, zmin );
105     aLines->AddVertex( xmin, ymin, zmin );
106     aLines->AddBound( 5 );
107     aLines->AddVertex( xmin, ymin, zmax );
108     aLines->AddVertex( xmax, ymin, zmax );
109     aLines->AddVertex( xmax, ymax, zmax );
110     aLines->AddVertex( xmin, ymax, zmax );
111     aLines->AddVertex( xmin, ymin, zmax );
112     aLines->AddBound( 2 );
113     aLines->AddVertex( xmin, ymin, zmin );
114     aLines->AddVertex( xmin, ymin, zmax );
115     aLines->AddBound( 2 );
116     aLines->AddVertex( xmax, ymin, zmin );
117     aLines->AddVertex( xmax, ymin, zmax );
118     aLines->AddBound( 2 );
119     aLines->AddVertex( xmax, ymax, zmin );
120     aLines->AddVertex( xmax, ymax, zmax );
121     aLines->AddBound( 2 );
122     aLines->AddVertex( xmin, ymax, zmin );
123     aLines->AddVertex( xmin, ymax, zmax );
124     aGroup->AddPrimitiveArray( aLines );
125   }
126   else
127   {
128     AIS_PointCloud::Compute( thePresentationManager, thePresentation, theMode );
129
130     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
131     if( !myTextIndices.empty() && !points.IsNull() )
132     {
133       char aBuf[1024];
134       Handle(Prs3d_TextAspect) anAspect = new Prs3d_TextAspect();
135
136       foreach( int index, myTextIndices )
137       {
138         gp_Pnt p = points->Vertice( index );
139         sprintf( aBuf, "%.2f", p.Z() );
140         Prs3d_Text::Draw( aGroup, anAspect, aBuf, p );
141       }
142     }
143   }
144 }
145
146 void HYDROGUI_BathymetryPrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
147                                                const Standard_Integer theMode )
148 {
149   if( theMode == 0 )
150   {
151     if( myBound.IsVoid() )
152       UpdateBound();
153
154     if( myBound.IsVoid() || 
155         myBound.IsOpenXmin() || myBound.IsOpenXmax() ||
156         myBound.IsOpenYmin() || myBound.IsOpenYmax() ||
157         myBound.IsOpenZmin() || myBound.IsOpenZmax() )
158       return;
159
160     Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner( this );
161     Handle(Select3D_SensitiveBox) aSensitiveBox = new Select3D_SensitiveBox( anOwner, myBound );
162     theSelection->Add( aSensitiveBox );
163   }
164   if( theMode == 1 )
165   {
166     Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
167     int n = points.IsNull() ? 0 : points->VertexNumber();
168     for( int i=1; i<=n; i++ )
169     {
170       gp_Pnt p = points->Vertice( i );
171       Handle(HYDROGUI_BathymetryPointOwner) anOwner = new HYDROGUI_BathymetryPointOwner( this, i );
172       Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint( anOwner, p );
173       theSelection->Add( aSensitivePoint );
174     }
175   }
176 }
177
178 int HYDROGUI_BathymetryPrs::NbPoints() const
179 {
180   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
181   int n = points.IsNull() ? 0 : points->VertexNumber();
182   return n;
183 }
184
185 gp_Pnt HYDROGUI_BathymetryPrs::GetPoint( int theIndex ) const
186 {
187   Handle(Graphic3d_ArrayOfPoints) points = GetPoints();
188   if( points.IsNull() )
189     return gp_Pnt();
190   else
191     return points->Vertice( theIndex );
192 }
193
194 void HYDROGUI_BathymetryPrs::AddPoint( const Handle(Graphic3d_ArrayOfPoints)& thePoints,
195                                        const Handle(SelectMgr_EntityOwner)& theOwner )
196 {
197   Handle(HYDROGUI_BathymetryPointOwner) anOwner = Handle(HYDROGUI_BathymetryPointOwner)::DownCast( theOwner );
198   if( anOwner.IsNull() )
199     return;
200   gp_Pnt p = GetPoint( anOwner->GetIndex() );
201   thePoints->AddVertex( p );
202 }
203
204 void HYDROGUI_BathymetryPrs::HilightOwnerWithColor( const Handle(PrsMgr_PresentationManager3d)& thePM,
205                                                     const Handle(Graphic3d_HighlightStyle)& theColor,
206                                                     const Handle(SelectMgr_EntityOwner)& theOwner )
207 {
208   Handle(Prs3d_Presentation) aHilightPrs = GetHilightPresentation( thePM );
209   aHilightPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
210   aHilightPrs->Clear();
211   Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( 1 );
212   AddPoint( points, theOwner );
213
214   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aHilightPrs );
215   Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
216   aGroup->SetGroupPrimitivesAspect( anAspect );
217   aGroup->AddPrimitiveArray( points );
218
219   if( thePM->IsImmediateModeOn() )
220     thePM->AddToImmediateList( aHilightPrs );
221 }
222
223 void HYDROGUI_BathymetryPrs::HilightSelected( const Handle(PrsMgr_PresentationManager3d)& thePM,
224                                               const SelectMgr_SequenceOfOwner& theOwners )
225 {
226   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( thePM );
227   aSelectPrs->SetZLayer( Graphic3d_ZLayerId_Topmost );
228   aSelectPrs->Clear();
229
230   Handle(Graphic3d_ArrayOfPoints) points = new Graphic3d_ArrayOfPoints( theOwners.Size() );
231   for( int i=theOwners.Lower(); i<=theOwners.Upper(); i++ )
232     AddPoint( points, theOwners.Value( i ) );
233
234   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aSelectPrs );
235   Handle(Graphic3d_AspectMarker3d) anAspect = new Graphic3d_AspectMarker3d( Aspect_TOM_X, Quantity_NOC_WHITE, 1.0 );
236   aGroup->SetGroupPrimitivesAspect( anAspect );
237   aGroup->AddPrimitiveArray( points );
238
239   aSelectPrs->SetDisplayPriority(9);
240   aSelectPrs->Display();
241 }
242
243 void HYDROGUI_BathymetryPrs::ClearSelected()
244 {
245   Handle(Prs3d_Presentation) aSelectPrs = GetSelectPresentation( NULL );  
246   if( !aSelectPrs.IsNull() )
247     aSelectPrs->Clear(); 
248 }
249
250 void HYDROGUI_BathymetryPrs::SetTextLabels( const QList<int>& theTextIndices )
251 {
252   myTextIndices = theTextIndices;
253 }
254
255
256
257
258
259
260
261 HYDROGUI_BathymetryPointOwner::HYDROGUI_BathymetryPointOwner
262   ( const Handle(HYDROGUI_BathymetryPrs)& theBathymetry, int theIndex )
263   : SelectMgr_EntityOwner( Handle(SelectMgr_SelectableObject)::DownCast( theBathymetry ), 0 ),
264     myIndex( theIndex )
265 {
266 }
267
268 HYDROGUI_BathymetryPointOwner::~HYDROGUI_BathymetryPointOwner()
269 {
270 }
271
272 Standard_Boolean HYDROGUI_BathymetryPointOwner::IsAutoHilight() const
273 {
274   return Standard_False;
275 }
276
277 int HYDROGUI_BathymetryPointOwner::GetIndex() const
278 {
279   return myIndex;
280 }