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