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