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