]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Shape.cxx
Salome HOME
f18e3870720d810cd0af10f59c0e7e2086752a01
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Shape.h"
24
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <AIS_Drawer.hxx>
29 #include <AIS_TexturedShape.hxx>
30 #include <AIS_PointCloud.hxx>
31 #include <V3d_Viewer.hxx>
32
33 #include <BRepBuilderAPI_MakeEdge.hxx>
34 #include <BRepBuilderAPI_MakeWire.hxx>
35 #include <BRepBuilderAPI_MakeFace.hxx>
36
37 #include <gp_Pnt.hxx>
38
39 #include <Graphic3d_AspectFillArea3d.hxx>
40 #include <Graphic3d_MaterialAspect.hxx>
41
42 #include <Aspect_ColorScale.hxx>
43
44 #include <HYDROData_Channel.h>
45 #include <HYDROData_Document.h>
46 #include <HYDROData_Bathymetry.h>
47 #include <HYDROData_DummyObject3D.h>
48 #include <HYDROData_Image.h>
49 #include <HYDROData_ImmersibleZone.h>
50 #include <HYDROData_Obstacle.h>
51 #include <HYDROData_PolylineXY.h>
52 #include <HYDROData_Polyline3D.h>
53 #include <HYDROData_Profile.h>
54 #include <HYDROData_Region.h>
55 #include <HYDROData_ShapesGroup.h>
56 #include <HYDROData_Stream.h>
57 #include <HYDROData_Zone.h>
58
59 #include <TopoDS.hxx>
60 #include <TopoDS_Wire.hxx>
61 #include <TopoDS_Face.hxx>
62
63 #include <TopExp_Explorer.hxx>
64
65 #include <BRep_Builder.hxx>
66
67 #include <Precision.hxx>
68
69 #include <Prs3d_ShadingAspect.hxx>
70 #include <Prs3d_LineAspect.hxx>
71 #include <Prs3d_IsoAspect.hxx>
72 #include <Prs3d_PointAspect.hxx>
73 #include <SUIT_MessageBox.h>
74
75 #include <QColor>
76 #include <QFile>
77
78 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
79                                 const Handle(HYDROData_Entity)&       theObject,
80                                 const int                             theZLayer )
81 : myContext( theContext ),
82   myObject( theObject ),
83   myZLayer( theZLayer ),
84   myIsHighlight( false ),
85   myFillingColor( Qt::transparent ),
86   myBorderColor( Qt::black ),
87   myHighlightColor( Qt::white ),
88   myIsToUpdate( false ),
89   myIsVisible( true ),
90   myDisplayMode( AIS_WireFrame )
91 {
92 }
93
94 HYDROGUI_Shape::~HYDROGUI_Shape()
95 {
96   erase( false );
97
98   if ( !myShape.IsNull() )
99     myShape.Nullify();
100
101   removeTextureFile();
102 }
103
104 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
105 {
106   if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
107     return;
108
109   displayShape( theIsUpdateViewer );
110 }
111
112 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
113 {
114   if ( myContext.IsNull() || myShape.IsNull() )
115     return;
116
117   myContext->Erase( myShape, theIsUpdateViewer );
118 }
119
120 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
121 {
122   setIsToUpdate( false );
123
124   if ( myContext.IsNull() )
125     return;
126
127   bool isDeactivateSelection = false;
128   // Try to retrieve information from object
129   if ( !myObject.IsNull() )
130   {
131     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
132   
133     if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
134     {
135       Handle(HYDROData_ImmersibleZone) aZoneObj =
136         Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
137
138       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
139       if ( !aZoneShape.IsNull() ) {
140         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
141           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
142           setFace( aZoneFace, false, false );
143         } else {
144           myTopoShape = aZoneShape;
145           myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
146
147           buildShape();
148           updateShape( false, false );
149         }
150       }
151
152       QColor aFillingColor = aZoneObj->GetFillingColor();
153       QColor aBorderColor = aZoneObj->GetBorderColor();
154
155       setFillingColor( aFillingColor, false, false );
156       setBorderColor( aBorderColor, false, false );
157     }
158     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
159     {
160       Handle(HYDROData_PolylineXY) aPolyline =
161         Handle(HYDROData_PolylineXY)::DownCast( myObject );
162
163       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
164
165       if ( !aPolylineShape.IsNull() ) {
166         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
167           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
168           setWire( aPolylineWire, false, false );  
169         } else {
170           myTopoShape = aPolylineShape;
171           // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
172           // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
173           myDisplayMode = AIS_Shaded;
174
175           buildShape();
176           updateShape( false, false );
177         }
178       }
179
180       QColor aWireColor = aPolyline->GetWireColor();
181       setBorderColor( aWireColor, false, false );
182     }
183     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
184     {
185       Handle(HYDROData_Polyline3D) aPolyline =
186         Handle(HYDROData_Polyline3D)::DownCast( myObject );
187
188       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
189
190       if ( !aPolylineShape.IsNull() ) {
191         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
192           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
193           setWire( aPolylineWire, false, false );  
194         } else {
195           myTopoShape = aPolylineShape;
196           // Set shading mode to avoid that hilight presentation is equal to "normal" object presentation.
197           // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
198           myDisplayMode = AIS_Shaded;
199
200           buildShape();
201           updateShape( false, false );
202         }
203       }
204
205       QColor aWireColor = aPolyline->GetBorderColor();
206       setBorderColor( aWireColor, false, false );
207     }
208     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
209     {
210       Handle(HYDROData_Zone) aZone =
211         Handle(HYDROData_Zone)::DownCast( myObject );
212
213       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
214
215       setFace( aZoneFace, false, false );
216       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
217       {
218         // Red color for a zone with bathymetry conflict
219         setFillingColor( Qt::red );
220       }
221       else
222       {
223         // Generate the filling color for zone
224         QStringList aGeomObjectsNames;
225
226         HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
227         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
228         for ( ; anIter.More(); anIter.Next() )
229         {
230           Handle(HYDROData_Object) aRefbject = 
231             Handle(HYDROData_Object)::DownCast( anIter.Value() );
232           if ( aRefbject.IsNull() )
233             continue;
234
235           QString aRefObjectName = aRefbject->GetName();
236           if ( aRefObjectName.isEmpty() )
237             continue;
238
239           aGeomObjectsNames.append( aRefObjectName );
240         }
241
242         setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
243       }
244     }
245     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
246     {
247       Handle(HYDROData_Image) anImageObj =
248         Handle(HYDROData_Image)::DownCast( myObject );
249
250       removeTextureFile();
251
252       QString aTextureFileName = generateTextureFileName( anImageObj );
253
254       QImage anImage = anImageObj->Image();
255       QString aFilePath = anImageObj->GetFilePath();
256       QTransform aTrsf = anImageObj->Trsf();
257
258       int aWidth = anImage.width();
259       int aHeight = anImage.height();
260
261       QString anImageError = "";
262
263       QTransform anInversion = QTransform::fromScale( -1, -1 );
264       anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
265
266       if ( anImage.isNull() )
267         anImageError = QObject::tr( "IMAGE_TRANSFORMATION_CAN_NOT_BE_APPLYED" );
268       else
269       {
270         // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
271         int aTrsfWidth = anImage.width();
272         int aDelta = aTrsfWidth % 4;
273         if ( aDelta > 0 )
274         {
275           aTrsfWidth += ( 4 - aDelta );
276         }
277         anImage = anImage.scaledToWidth( aTrsfWidth );
278
279         // temporary optimization, to reduce the saved image size (and the texture quality)
280         QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
281
282         bool isSaved = anImageToSave.save( aTextureFileName );
283         if ( !isSaved )
284           anImageError = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
285         else
286           QFile::setPermissions( aTextureFileName, (QFile::Permissions)0x4FFFF );
287       }
288
289       if ( !anImageError.isEmpty() )
290       {
291         SUIT_MessageBox::warning( 0, QObject::tr( "SHAPE_IMAGE_ERROR" ),
292                                   QObject::tr( "IMAGE_CAN_NOT_BE_CREATED" ) + anImageError );
293       }
294
295       QPointF aPoint1( 0, 0 );            // 1: top left
296       QPointF aPoint2( aWidth, 0 );       // 2: top right
297       QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
298       QPointF aPoint4( 0, aHeight );      // 4: bottom left
299
300       aPoint1 = aTrsf.map( aPoint1 );
301       aPoint2 = aTrsf.map( aPoint2 );
302       aPoint3 = aTrsf.map( aPoint3 );
303       aPoint4 = aTrsf.map( aPoint4 );
304
305       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
306       QRectF aRect = aPolygon.boundingRect();
307
308       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
309       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
310       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
311       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
312
313       Handle_HYDROData_Document aDoc = HYDROData_Document::Document( anImageObj->Label() );
314       aDoc->Transform( aPnt1, true );
315       aDoc->Transform( aPnt2, true );
316       aDoc->Transform( aPnt3, true );
317       aDoc->Transform( aPnt4, true );
318
319       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
320       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
321       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
322       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
323
324       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
325       aWire.Closed( true );
326
327       setTextureFileName( aTextureFileName, false, false );
328       setFace( aWire, false, false );
329       isDeactivateSelection = true;
330     }
331     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
332     {
333       Handle(HYDROData_Profile) aProfile =
334         Handle(HYDROData_Profile)::DownCast( myObject );
335
336       TopoDS_Wire aProfileWire;
337
338       if ( aProfile->IsValid() ) {
339         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
340
341         if ( !aProfileShape.IsNull() && 
342              aProfileShape.ShapeType() == TopAbs_WIRE ) {
343           aProfileWire = TopoDS::Wire( aProfileShape );
344         }
345       }
346
347       setWire( aProfileWire, false, false );  
348
349       QColor aWireColor = aProfile->GetBorderColor();
350       setBorderColor( aWireColor, false, false );
351     }
352     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
353               myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
354               myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
355     {
356       Handle(HYDROData_Object) aGeomObject =
357         Handle(HYDROData_Object)::DownCast( myObject );
358
359       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
360
361       setShape( anObjShape, false, false );
362
363       QColor aFillingColor = aGeomObject->GetFillingColor();
364       QColor aBorderColor = aGeomObject->GetBorderColor();
365
366       setFillingColor( aFillingColor, false, false );
367       setBorderColor( aBorderColor, false, false );
368     }
369     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
370     {
371       Handle(HYDROData_DummyObject3D) anObject3D =
372         Handle(HYDROData_DummyObject3D)::DownCast( myObject );
373       TopoDS_Shape aShape3D = anObject3D->GetShape();
374
375       setShape( aShape3D, false, false );
376
377       QColor aFillingColor = anObject3D->GetFillingColor();
378       QColor aBorderColor = anObject3D->GetBorderColor();
379
380       setFillingColor( aFillingColor, false, false );
381       setBorderColor( aBorderColor, false, false );
382     }
383     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ShapesGroup) ) )
384     {
385       Handle(HYDROData_ShapesGroup) aShapesGroup =
386         Handle(HYDROData_ShapesGroup)::DownCast( myObject );
387
388       TopTools_SequenceOfShape aShapes;
389       aShapesGroup->GetShapes( aShapes );
390
391       TopoDS_Compound aCompound;
392       BRep_Builder aCompoundBuilder;
393       aCompoundBuilder.MakeCompound( aCompound );
394
395       for ( int i = 1, n = aShapes.Length(); i <= n; ++i )
396       {
397         const TopoDS_Shape& aShape = aShapes.Value( i );
398         aCompoundBuilder.Add( aCompound, aShape );
399       }
400
401       setShape( aCompound, false, false );  
402     }
403     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Bathymetry) ) )
404     {
405       buildShape();
406       updateShape( false, false );
407     }
408   }
409  
410   if ( myShape.IsNull() || !isVisible() )
411     return;
412
413   displayShape( theIsUpdateViewer );
414
415   if (isDeactivateSelection)
416     myContext->Deactivate(myShape);
417 }
418
419 void HYDROGUI_Shape::setVisible( const bool theState,
420                                  const bool theIsUpdateViewer )
421 {
422   myIsVisible = theState;
423
424   if ( myShape.IsNull() )
425     return;
426
427   if ( ( myIsVisible && myContext->IsDisplayed( myShape ) ) ||
428        ( !myIsVisible && !myContext->IsDisplayed( myShape ) ) )
429     return;
430
431   if ( myIsVisible ) {
432     displayShape( theIsUpdateViewer );
433   }
434   else
435     myContext->Erase( myShape, theIsUpdateViewer );
436 }
437
438 void HYDROGUI_Shape::highlight( bool theIsHighlight, bool isUpdateViewer )
439 {
440   if ( myIsHighlight == theIsHighlight )
441     return;
442
443   myIsHighlight = theIsHighlight;
444
445   if ( myContext.IsNull() || myShape.IsNull() )
446     return;
447
448   colorShapeBorder( getActiveColor() );
449   displayShape( isUpdateViewer );
450 }
451
452 bool HYDROGUI_Shape::isHighlighted() const
453 {
454   return myIsHighlight;
455 }
456
457 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
458                               const bool         theToDisplay,
459                               const bool         theIsUpdateViewer )
460 {
461   myTopoShape = theWire;
462   // To avoid that hilight presentation is equal to "normal" object presentation.
463   // Note that hilight presentation is always to be on top ( i.e. in the top Z layer ).
464   myDisplayMode = AIS_Shaded;
465
466   buildShape();
467   updateShape( theToDisplay, theIsUpdateViewer );
468 }
469
470 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
471                                const bool             theToDisplay,
472                                const bool             theIsUpdateViewer )
473 {
474   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
475   TopoDS_Compound aCompound;
476   BRep_Builder aBuilder;
477     aBuilder.MakeCompound( aCompound );
478
479   for ( ; anExp.More(); anExp.Next() ) {
480     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
481     if ( aWire.IsNull() ) {
482       continue;
483     }
484
485     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
486     aMakeFace.Build();
487     if( aMakeFace.IsDone() ) {
488       aBuilder.Add( aCompound, aMakeFace.Face() );
489     }
490   }
491
492   myTopoShape = aCompound;
493   myDisplayMode = AIS_Shaded;
494
495   buildShape();
496   updateShape( theToDisplay, theIsUpdateViewer );
497 }
498
499 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
500                               const bool         theToDisplay,
501                               const bool         theIsUpdateViewer )
502 {
503   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
504   aFaceBuilder.Build();
505   if( aFaceBuilder.IsDone() )
506   {
507     TopoDS_Face aFace = aFaceBuilder.Face();
508     setFace( aFace, theToDisplay, theIsUpdateViewer );
509   }
510 }
511
512 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
513                               const bool         theToDisplay,
514                               const bool         theIsUpdateViewer )
515 {
516   myTopoShape = theFace;
517   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
518   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
519   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
520
521   buildShape();
522   updateShape( theToDisplay, theIsUpdateViewer );
523 }
524
525 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
526                                const bool          theToDisplay,
527                                const bool          theIsUpdateViewer )
528 {
529   myTopoShape = theShape;
530   myDisplayMode = AIS_Shaded;
531
532   buildShape();
533   updateShape( theToDisplay, theIsUpdateViewer );
534 }
535
536 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
537                                       const bool    theToDisplay,
538                                       const bool    theIsUpdateViewer )
539 {
540   myFillingColor = theColor;
541   updateShape( theToDisplay, theIsUpdateViewer );
542 }
543
544 QColor HYDROGUI_Shape::getFillingColor() const
545 {
546   return myFillingColor;
547 }
548
549 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
550                                      const bool    theToDisplay,
551                                      const bool    theIsUpdateViewer )
552 {
553   myBorderColor = theColor;
554   updateShape( theToDisplay, theIsUpdateViewer );
555 }
556
557 QColor HYDROGUI_Shape::getBorderColor() const
558 {
559   return myBorderColor;
560 }
561
562 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
563 {
564   myHighlightColor = theColor;
565 }
566
567 QColor HYDROGUI_Shape::getHighlightColor() const
568 {
569   return myHighlightColor;
570 }
571
572 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
573                                          const bool     theToDisplay,
574                                          const bool     theIsUpdateViewer )
575 {
576   myTextureFileName = theFileName;
577   updateShape( theToDisplay, theIsUpdateViewer );
578 }
579
580 QString HYDROGUI_Shape::getTextureFileName() const
581 {
582   return myTextureFileName;
583 }
584
585 void HYDROGUI_Shape::setZLayer( const int theZLayer )
586 {
587   if ( myZLayer == theZLayer )
588     return;
589
590   myZLayer = theZLayer;
591   if ( !myShape.IsNull() && isVisible() && !myContext.IsNull() && myZLayer >= 0 )
592     myContext->SetZLayer( myShape, myZLayer );
593 }
594
595 #include <NCollection_Sequence.hxx>
596 void HYDROGUI_Shape::buildShape()
597 {
598   // Erase previously created shape
599   erase();
600
601   Handle_HYDROData_Bathymetry aBath = Handle_HYDROData_Bathymetry::DownCast( myObject );
602   bool isBath = !aBath.IsNull();
603
604   if( !isBath && myTopoShape.IsNull() ) {
605     myShape.Nullify();
606     return;
607   }
608
609   QString aTextureFileName = getTextureFileName();
610   bool anIsTexture = !aTextureFileName.isEmpty();
611   if ( anIsTexture )
612   {
613     myShape = new AIS_TexturedShape( myTopoShape );
614   }
615   else if( isBath )
616   {
617     Handle_AIS_PointCloud aPntCloud = new AIS_PointCloud();
618     aPntCloud->Attributes()->SetPointAspect (new Prs3d_PointAspect (Aspect_TOM_POINT, Quantity_NOC_WHITE, 1.0));
619
620     myShape = aPntCloud;
621     const HYDROData_Bathymetry::AltitudePoints& aBathPoints = aBath->GetAltitudePoints();
622     int aLower = aBathPoints.Lower();
623     int anUpper = aBathPoints.Upper();
624     myCoords = new TColgp_HArray1OfPnt( aLower, anUpper );
625     myColors = new Quantity_HArray1OfColor( aLower, anUpper );
626     for( int i=aLower; i<=anUpper; i++ )
627       myCoords->SetValue( i, aBathPoints.Value( i ) );
628   }
629   else
630   {
631     myShape = new AIS_Shape( myTopoShape );
632   }
633
634   Handle_AIS_Shape anAISShape = Handle_AIS_Shape::DownCast( myShape );
635   if( !anAISShape.IsNull() )
636     anAISShape ->SetHLRAngleAndDeviation( 0.001 );
637
638   if ( !myObject.IsNull() )
639     myShape->SetOwner( myObject );
640
641   myShape->SetTransparency( 0 );
642   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
643
644   if( anIsTexture )
645   {
646     Handle(AIS_TexturedShape) aTexturedShape = 
647       Handle(AIS_TexturedShape)::DownCast( myShape );
648
649     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
650     aTexturedShape->SetTextureMapOn();
651     // Just use the texture image as is
652     aTexturedShape->DisableTextureModulate();
653     aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
654   }
655
656     // Init default params for shape
657   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
658   if ( !anAttributes.IsNull() )
659   {
660     Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
661     if ( !anIsoAspect.IsNull() ) {
662       anIsoAspect->SetNumber( 0 );
663       anAttributes->SetUIsoAspect( anIsoAspect );
664     }
665       
666     anIsoAspect = anAttributes->VIsoAspect();
667     if ( !anIsoAspect.IsNull() ) {
668       anIsoAspect->SetNumber( 0 );
669       anAttributes->SetVIsoAspect( anIsoAspect );
670     }
671
672     if ( myDisplayMode == AIS_Shaded )
673     {
674       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
675       if ( !aShadingAspect.IsNull() )
676       {
677         Graphic3d_MaterialAspect aMatAspect( Graphic3d_NOM_PLASTIC );
678         //aMatAspect.SetAmbient( 1 );
679         //aMatAspect.SetDiffuse( 0 );
680
681         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
682         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
683       }
684     }
685     else if ( myDisplayMode == AIS_WireFrame )
686     {
687       anAttributes->SetWireDraw( true );
688     }
689   }
690 }
691
692 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
693                                   const bool theIsUpdateViewer )
694 {
695   if ( myShape.IsNull() )
696     return;
697
698   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
699   if ( !anAttributes.IsNull() )
700   {
701     if ( myDisplayMode == AIS_Shaded )
702     {
703       // Coloring face filling
704       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
705       if ( !aShadingAspect.IsNull() )
706       {
707         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
708                                       getQuantityColorVal( myFillingColor.green() ),
709                                       getQuantityColorVal( myFillingColor.blue() ),
710                                       Quantity_TOC_RGB );
711
712         aShadingAspect->SetColor( aFillingColor );
713         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
714       }
715     }
716     else if ( myDisplayMode == AIS_WireFrame )
717     {
718     }
719
720     // Coloring borders
721     colorShapeBorder( getActiveColor() );
722   }
723
724   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
725     return;
726   
727   displayShape( theIsUpdateViewer );
728 }
729
730 void HYDROGUI_Shape::displayShape( const bool theIsUpdateViewer )
731 {
732   myContext->Display( myShape, Standard_False );
733
734   if ( myZLayer >= 0 )
735     myContext->SetZLayer( myShape, myZLayer );
736
737   myContext->UpdateCurrentViewer();
738 }
739
740 QColor HYDROGUI_Shape::getActiveColor() const
741 {
742   return isHighlighted() ? myHighlightColor : myBorderColor;
743 }
744
745 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
746 {
747   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
748 }
749
750 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
751 {
752   if ( myShape.IsNull() )
753     return;
754
755   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
756   if ( anAttributes.IsNull() )
757     return;
758
759   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
760                                getQuantityColorVal( theColor.green() ),
761                                getQuantityColorVal( theColor.blue() ),
762                                Quantity_TOC_RGB );
763   
764   if( !myTopoShape.IsNull() )
765   {
766     if ( myTopoShape.ShapeType() == TopAbs_WIRE ) // Note that we display polylines in shaded mode
767     {
768       myShape->SetColor( aBorderColor );
769     }
770     else if ( myDisplayMode == AIS_Shaded )
771     {
772       if ( theColor.alpha() == 0 )
773       {
774         anAttributes->SetFaceBoundaryDraw( false );
775       }
776       else
777       {
778         anAttributes->SetFaceBoundaryDraw( true );
779   
780         Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
781         if ( !aBoundaryAspect.IsNull() )
782         {
783           aBoundaryAspect->SetColor( aBorderColor );
784           anAttributes->SetFaceBoundaryAspect( aBoundaryAspect );
785         }
786         Handle(Prs3d_LineAspect) aWireAspect = anAttributes->WireAspect();
787         if ( !aWireAspect.IsNull() )
788         {
789           aWireAspect->SetColor( aBorderColor );
790           anAttributes->SetWireAspect( aWireAspect );
791         }
792       }
793     }
794     else if ( myDisplayMode == AIS_WireFrame )
795     {
796       myShape->SetColor( aBorderColor );
797     }
798   }
799 }
800
801 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
802 {
803   QString aResult;
804   if( !theImageObj.IsNull() )
805   {
806     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
807
808     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
809     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
810
811     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
812     anEntry.replace( ':', '_' );
813
814     QString anExtension = "bmp";
815
816     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
817   }
818   return aResult;
819 }
820
821 void HYDROGUI_Shape::removeTextureFile() const
822 {
823   QFile aFile( getTextureFileName() );
824   if( aFile.exists() )
825     aFile.remove();
826 }
827
828 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
829 {
830   double aSizeLimit = (double)theSizeLimit;
831   double aWidth = (double)theImage.width();
832   double aHeight = (double)theImage.height();
833   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
834   {
835     if( aWidth > aHeight )
836     {
837       aHeight /= ( aWidth / aSizeLimit );
838       aWidth = aSizeLimit;
839     }
840     else
841     {
842       aWidth /= ( aHeight / aSizeLimit );
843       aHeight = aSizeLimit;
844     }
845   }
846   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
847 }
848
849 void HYDROGUI_Shape::GetRange( double& theMin, double& theMax ) const
850 {
851   theMin = 0;
852   theMax = 0;
853   if( myCoords.IsNull() )
854     return;
855
856   bool isFirst = true;
857   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
858   {
859     double aValue = myCoords->Value( i ).Z();
860     if( isFirst || aValue < theMin )
861       theMin = aValue;
862     if( isFirst || aValue > theMax )
863       theMax = aValue;
864     isFirst = false;
865   }
866 }
867
868 void HYDROGUI_Shape::UpdateWithColorScale( const Handle(Aspect_ColorScale)& theColorScale )
869 {
870   for( int i=myCoords->Lower(), n=myCoords->Upper(); i<=n; i++ )
871   {
872     double z = myCoords->Value( i ).Z();
873     Quantity_Color aColor;
874     theColorScale->FindColor( z, aColor );
875     myColors->SetValue( i, aColor );
876   }
877   Handle_AIS_PointCloud aPntCloud = Handle_AIS_PointCloud::DownCast( myShape );
878   aPntCloud->SetPoints( myCoords, myColors );
879   myContext->Redisplay( aPntCloud, Standard_False );
880 }