Salome HOME
The curve creator for Profile object first implementation.
[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
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <BRepBuilderAPI_MakeFace.hxx>
34
35 #include <gp_Pnt.hxx>
36
37 #include <Graphic3d_AspectFillArea3d.hxx>
38 #include <Graphic3d_MaterialAspect.hxx>
39
40 #include <HYDROData_Document.h>
41 #include <HYDROData_Image.h>
42 #include <HYDROData_ImmersibleZone.h>
43 #include <HYDROData_PolylineXY.h>
44 #include <HYDROData_Region.h>
45 #include <HYDROData_Zone.h>
46 #include <HYDROData_Obstacle.h>
47
48 #include <TopoDS.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Face.hxx>
51
52 #include <TopExp_Explorer.hxx>
53
54 #include <BRep_Builder.hxx>
55
56 #include <Precision.hxx>
57
58 #include <Prs3d_ShadingAspect.hxx>
59 #include <Prs3d_LineAspect.hxx>
60 #include <Prs3d_IsoAspect.hxx>
61
62 #include <SUIT_MessageBox.h>
63
64 #include <QColor>
65 #include <QFile>
66
67 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
68                                 const Handle(HYDROData_Entity)&       theObject )
69 : myContext( theContext ),
70   myObject( theObject ),
71   myIsHighlight( false ),
72   myFillingColor( Qt::transparent ),
73   myBorderColor( Qt::black ),
74   myHighlightColor( Qt::white ),
75   myIsToUpdate( false ),
76   myIsVisible( true ),
77   myDisplayMode( AIS_WireFrame )
78 {
79 }
80
81 HYDROGUI_Shape::~HYDROGUI_Shape()
82 {
83   erase();
84
85   if ( !myShape.IsNull() )
86     myShape.Nullify();
87
88   removeTextureFile();
89 }
90
91 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
92 {
93   if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
94     return;
95
96   myContext->Display( myShape, theIsUpdateViewer );
97 }
98
99 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
100 {
101   if ( myContext.IsNull() || myShape.IsNull() )
102     return;
103
104   myContext->Erase( myShape, theIsUpdateViewer );
105 }
106
107 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
108 {
109   setIsToUpdate( false );
110
111   if ( myContext.IsNull() )
112     return;
113
114   // Try to retrieve information from object
115   if ( !myObject.IsNull() )
116   {
117     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
118   
119     if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
120     {
121       Handle(HYDROData_ImmersibleZone) aZoneObj =
122         Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
123
124       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
125       if ( !aZoneShape.IsNull() ) {
126         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
127           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
128           setFace( aZoneFace, false, false );
129         } else {
130           myTopoShape = aZoneShape;
131           myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
132
133           buildShape();
134           updateShape( false, false );
135         }
136       }
137
138       QColor aFillingColor = aZoneObj->GetFillingColor();
139       QColor aBorderColor = aZoneObj->GetBorderColor();
140
141       setFillingColor( aFillingColor, false, false );
142       setBorderColor( aBorderColor, false, false );
143     }
144     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
145     {
146       Handle(HYDROData_PolylineXY) aPolyline =
147         Handle(HYDROData_PolylineXY)::DownCast( myObject );
148
149       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
150
151       if ( !aPolylineShape.IsNull() ) {
152         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
153           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
154           setWire( aPolylineWire, false, false );  
155         } else {
156           myTopoShape = aPolylineShape;
157           myDisplayMode = AIS_WireFrame;
158
159           buildShape();
160           updateShape( false, false );
161         }
162       }
163     }
164     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
165     {
166       Handle(HYDROData_Zone) aZone =
167         Handle(HYDROData_Zone)::DownCast( myObject );
168
169       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
170
171       // Generate the filling color for zone
172       QStringList aGeomObjectsNames;
173
174       HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
175       HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
176       for ( ; anIter.More(); anIter.Next() )
177       {
178         Handle(HYDROData_Object) aRefbject = 
179           Handle(HYDROData_Object)::DownCast( anIter.Value() );
180         if ( aRefbject.IsNull() )
181           continue;
182
183         QString aRefObjectName = aRefbject->GetName();
184         if ( aRefObjectName.isEmpty() )
185           continue;
186
187         aGeomObjectsNames.append( aRefObjectName );
188       }
189        
190       setFace( aZoneFace, false, false );
191       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
192       {
193         // Red color for a zone with bathymetry conflict
194         setFillingColor( Qt::red );
195       }
196       else
197       {
198         setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
199       }
200     }
201     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
202     {
203       Handle(HYDROData_Image) anImageObj =
204         Handle(HYDROData_Image)::DownCast( myObject );
205
206       removeTextureFile();
207
208       QString aTextureFileName = generateTextureFileName( anImageObj );
209
210       QImage anImage = anImageObj->Image();
211       QString aFilePath = anImageObj->GetFilePath();
212       QTransform aTrsf = anImageObj->Trsf();
213
214       int aWidth = anImage.width();
215       int aHeight = anImage.height();
216
217       QTransform anInversion = QTransform::fromScale( -1, -1 );
218       anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
219
220       // temporary optimization, to reduce the saved image size (and the texture quality)
221       QImage anImageToSave = reduceTexture( anImage, 500 );
222       bool isSaved = anImageToSave.save( aTextureFileName );
223       if ( !isSaved ) {
224         QString aTitle = QObject::tr( "FILE_ERROR" );
225         QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
226         SUIT_MessageBox::warning( 0, aTitle, aMessage );
227       }
228
229       QPointF aPoint1( 0, 0 );
230       QPointF aPoint2( aWidth, 0 );
231       QPointF aPoint3( aWidth, aHeight );
232       QPointF aPoint4( 0, aHeight );
233
234       aPoint1 = aTrsf.map( aPoint1 );
235       aPoint2 = aTrsf.map( aPoint2 );
236       aPoint3 = aTrsf.map( aPoint3 );
237       aPoint4 = aTrsf.map( aPoint4 );
238
239       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
240       QRectF aRect = aPolygon.boundingRect();
241
242       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
243       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
244       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
245       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
246
247       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
248       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
249       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
250       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
251
252       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
253
254       setTextureFileName( aTextureFileName, false, false );
255       setFace( aWire, false, false );
256     }
257     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
258     {
259       Handle(HYDROData_Obstacle) anObstacle =
260         Handle(HYDROData_Obstacle)::DownCast( myObject );
261
262       //TODO BEGIN of the block of code to be reimplemented
263       //TODO GetTopShape() to be used in future
264       myTopoShape = anObstacle->GetShape3D();
265       myDisplayMode = AIS_Shaded;
266
267       QColor aFillingColor = anObstacle->GetFillingColor();
268       QColor aBorderColor = anObstacle->GetBorderColor();
269
270       setFillingColor( aFillingColor, false, false );
271       setBorderColor( aBorderColor, false, false );
272
273       buildShape();
274       updateShape( false, false );
275       //TODO END of the block of code to be reimplemented
276     }
277   }
278  
279   if ( myShape.IsNull() || !isVisible() )
280     return;
281
282   myContext->Display( myShape, theIsUpdateViewer );
283 }
284
285 void HYDROGUI_Shape::setVisible( const bool theState,
286                                  const bool theIsUpdateViewer )
287 {
288   if ( myIsVisible == theState )
289     return;
290
291   myIsVisible = theState;
292
293   if ( myShape.IsNull() )
294     return;
295
296   if ( myIsVisible )
297     myContext->Display( myShape, theIsUpdateViewer );
298   else
299     myContext->Erase( myShape, theIsUpdateViewer );
300 }
301
302 void HYDROGUI_Shape::highlight( bool theIsHighlight )
303 {
304   if ( myIsHighlight == theIsHighlight )
305     return;
306
307   myIsHighlight = theIsHighlight;
308
309   if ( myContext.IsNull() || myShape.IsNull() )
310     return;
311
312   colorShapeBorder( getActiveColor() );
313   myContext->Display( myShape );
314 }
315
316 bool HYDROGUI_Shape::isHighlighted() const
317 {
318   return myIsHighlight;
319 }
320
321 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
322                               const bool         theToDisplay,
323                               const bool         theIsUpdateViewer )
324 {
325   myTopoShape = theWire;
326   myDisplayMode = AIS_WireFrame;
327
328   buildShape();
329   updateShape( theToDisplay, theIsUpdateViewer );
330 }
331
332 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
333                                const bool             theToDisplay,
334                                const bool             theIsUpdateViewer )
335 {
336   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
337   TopoDS_Compound aCompound;
338   BRep_Builder aBuilder;
339     aBuilder.MakeCompound( aCompound );
340
341   for ( ; anExp.More(); anExp.Next() ) {
342     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
343     if ( aWire.IsNull() ) {
344       continue;
345     }
346
347     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
348     aMakeFace.Build();
349     if( aMakeFace.IsDone() ) {
350       aBuilder.Add( aCompound, aMakeFace.Face() );
351     }
352   }
353
354   myTopoShape = aCompound;
355   myDisplayMode = AIS_Shaded;
356
357   buildShape();
358   updateShape( theToDisplay, theIsUpdateViewer );
359 }
360
361 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
362                               const bool         theToDisplay,
363                               const bool         theIsUpdateViewer )
364 {
365   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
366   aFaceBuilder.Build();
367   if( aFaceBuilder.IsDone() )
368   {
369     TopoDS_Face aFace = aFaceBuilder.Face();
370     setFace( aFace, theToDisplay, theIsUpdateViewer );
371   }
372 }
373
374 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
375                               const bool         theToDisplay,
376                               const bool         theIsUpdateViewer )
377 {
378   myTopoShape = theFace;
379   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
380   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
381   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
382
383   buildShape();
384   updateShape( theToDisplay, theIsUpdateViewer );
385 }
386
387 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
388                                const bool          theToDisplay,
389                                const bool          theIsUpdateViewer )
390 {
391   myTopoShape = theShape;
392   myDisplayMode = AIS_Shaded;
393
394   buildShape();
395   updateShape( theToDisplay, theIsUpdateViewer );
396 }
397
398 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
399                                       const bool    theToDisplay,
400                                       const bool    theIsUpdateViewer )
401 {
402   myFillingColor = theColor;
403   updateShape( theToDisplay, theIsUpdateViewer );
404 }
405
406 QColor HYDROGUI_Shape::getFillingColor() const
407 {
408   return myFillingColor;
409 }
410
411 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
412                                      const bool    theToDisplay,
413                                      const bool    theIsUpdateViewer )
414 {
415   myBorderColor = theColor;
416   updateShape( theToDisplay, theIsUpdateViewer );
417 }
418
419 QColor HYDROGUI_Shape::getBorderColor() const
420 {
421   return myBorderColor;
422 }
423
424 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
425 {
426   myHighlightColor = theColor;
427 }
428
429 QColor HYDROGUI_Shape::getHighlightColor() const
430 {
431   return myHighlightColor;
432 }
433
434 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
435                                          const bool     theToDisplay,
436                                          const bool     theIsUpdateViewer )
437 {
438   myTextureFileName = theFileName;
439   updateShape( theToDisplay, theIsUpdateViewer );
440 }
441
442 QString HYDROGUI_Shape::getTextureFileName() const
443 {
444   return myTextureFileName;
445 }
446
447 void HYDROGUI_Shape::buildShape()
448 {
449   // Erase previously created shape
450   erase();
451
452   if ( myTopoShape.IsNull() )
453     return;
454
455   QString aTextureFileName = getTextureFileName();
456   bool anIsTexture = !aTextureFileName.isEmpty();
457
458   if ( anIsTexture )
459   {
460     myShape = new AIS_TexturedShape( myTopoShape );
461   }
462   else
463   {
464     myShape = new AIS_Shape( myTopoShape );
465   }
466
467   if ( !myObject.IsNull() )
468     myShape->SetOwner( myObject );
469
470   myShape->SetTransparency( 0 );
471   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
472
473   if( anIsTexture )
474   {
475     Handle(AIS_TexturedShape) aTexturedShape = 
476       Handle(AIS_TexturedShape)::DownCast( myShape );
477
478     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
479     aTexturedShape->SetTextureMapOn();
480     aTexturedShape->DisableTextureModulate();
481   }
482
483     // Init default params for shape
484   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
485   if ( !anAttributes.IsNull() )
486   {
487     if ( myDisplayMode == AIS_Shaded )
488     {
489       Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
490       if ( !anIsoAspect.IsNull() )
491         anIsoAspect->SetNumber( 0 );
492       
493       anIsoAspect = anAttributes->VIsoAspect();
494       if ( !anIsoAspect.IsNull() )
495         anIsoAspect->SetNumber( 0 );
496
497       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
498       if ( !aShadingAspect.IsNull() )
499       {
500         Graphic3d_MaterialAspect aMatAspect;
501         aMatAspect.SetAmbient( 1 );
502         aMatAspect.SetDiffuse( 0 );
503
504         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
505         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
506       }
507     }
508     else if ( myDisplayMode == AIS_WireFrame )
509     {
510       anAttributes->SetWireDraw( true );
511     }
512   }
513 }
514
515 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
516                                   const bool theIsUpdateViewer )
517 {
518   if ( myShape.IsNull() )
519     return;
520
521   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
522   if ( !anAttributes.IsNull() )
523   {
524     if ( myDisplayMode == AIS_Shaded )
525     {
526       // Coloring face filling
527       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
528       if ( !aShadingAspect.IsNull() )
529       {
530         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
531                                       getQuantityColorVal( myFillingColor.green() ),
532                                       getQuantityColorVal( myFillingColor.blue() ),
533                                       Quantity_TOC_RGB );
534
535         aShadingAspect->SetColor( aFillingColor );
536         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
537       }
538     }
539     else if ( myDisplayMode == AIS_WireFrame )
540     {
541     }
542
543     // Coloring borders
544     colorShapeBorder( getActiveColor() );
545   }
546
547   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
548     return;
549   
550   myContext->Display( myShape, theIsUpdateViewer );
551 }
552
553 QColor HYDROGUI_Shape::getActiveColor() const
554 {
555   return isHighlighted() ? myHighlightColor : myBorderColor;
556 }
557
558 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
559 {
560   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
561 }
562
563 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
564 {
565   if ( myShape.IsNull() )
566     return;
567
568   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
569   if ( anAttributes.IsNull() )
570     return;
571
572   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
573                                getQuantityColorVal( theColor.green() ),
574                                getQuantityColorVal( theColor.blue() ),
575                                Quantity_TOC_RGB );
576   if ( myDisplayMode == AIS_Shaded )
577   {
578     if ( theColor.alpha() == 0 )
579     {
580       anAttributes->SetFaceBoundaryDraw( false );
581     }
582     else
583     {
584       anAttributes->SetFaceBoundaryDraw( true );
585
586       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
587       if ( !aBoundaryAspect.IsNull() )
588         aBoundaryAspect->SetColor( aBorderColor );
589     }
590   }
591   else if ( myDisplayMode == AIS_WireFrame )
592   {
593     myShape->SetColor( aBorderColor );
594   }
595 }
596
597 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
598 {
599   QString aResult;
600   if( !theImageObj.IsNull() )
601   {
602     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
603
604     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
605     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
606
607     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
608     anEntry.replace( ':', '_' );
609
610     QString anExtension = "bmp";
611
612     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
613   }
614   return aResult;
615 }
616
617 void HYDROGUI_Shape::removeTextureFile() const
618 {
619   QFile aFile( getTextureFileName() );
620   if( aFile.exists() )
621     aFile.remove();
622 }
623
624 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
625 {
626   double aSizeLimit = (double)theSizeLimit;
627   double aWidth = (double)theImage.width();
628   double aHeight = (double)theImage.height();
629   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
630   {
631     if( aWidth > aHeight )
632     {
633       aHeight /= ( aWidth / aSizeLimit );
634       aWidth = aSizeLimit;
635     }
636     else
637     {
638       aWidth /= ( aHeight / aSizeLimit );
639       aHeight = aSizeLimit;
640     }
641   }
642   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
643 }