Salome HOME
4b78aae4c5b7f3b550be93306509c71440716011
[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       anImage = anImage.transformed( aTrsf, Qt::SmoothTransformation );
144
145       // temporary optimization, to reduce the saved image size (and the texture quality)
146       QImage anImageToSave = reduceTexture( anImage, 500 );
147       anImageToSave.save( aTextureFileName );
148
149       QPointF aPoint1( 0, 0 );
150       QPointF aPoint2( aWidth, 0 );
151       QPointF aPoint3( aWidth, aHeight );
152       QPointF aPoint4( 0, aHeight );
153
154       aPoint1 = aTrsf.map( aPoint1 );
155       aPoint2 = aTrsf.map( aPoint2 );
156       aPoint3 = aTrsf.map( aPoint3 );
157       aPoint4 = aTrsf.map( aPoint4 );
158
159       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
160       QRectF aRect = aPolygon.boundingRect();
161
162       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
163       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
164       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
165       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
166
167       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
168       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
169       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
170       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
171
172       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
173
174       setTextureFileName( aTextureFileName, false, false );
175       setFace( aWire, false, false );
176     }
177   }
178
179   if ( myShape.IsNull() || !isVisible() )
180     return;
181
182   myContext->Display( myShape, theIsUpdateViewer );
183 }
184
185 void HYDROGUI_Shape::setVisible( const bool theState,
186                                  const bool theIsUpdateViewer )
187 {
188   if ( myIsVisible == theState )
189     return;
190
191   myIsVisible = theState;
192
193   if ( myShape.IsNull() )
194     return;
195
196   if ( myIsVisible )
197     myContext->Display( myShape, theIsUpdateViewer );
198   else
199     myContext->Erase( myShape, theIsUpdateViewer );
200 }
201
202 void HYDROGUI_Shape::highlight( bool theIsHighlight )
203 {
204   if ( myIsHighlight == theIsHighlight )
205     return;
206
207   myIsHighlight = theIsHighlight;
208
209   if ( myContext.IsNull() || myShape.IsNull() )
210     return;
211
212   colorShapeBorder( getActiveColor() );
213   myContext->Display( myShape );
214 }
215
216 bool HYDROGUI_Shape::isHighlighted() const
217 {
218   return myIsHighlight;
219 }
220
221 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
222                               const bool         theToDisplay,
223                               const bool         theIsUpdateViewer )
224 {
225   myTopoShape = theWire;
226   myDisplayMode = AIS_WireFrame;
227
228   buildShape();
229   updateShape( theToDisplay, theIsUpdateViewer );
230 }
231
232 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
233                               const bool         theToDisplay,
234                               const bool         theIsUpdateViewer )
235 {
236   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
237   aFaceBuilder.Build();
238   if( aFaceBuilder.IsDone() )
239   {
240     TopoDS_Face aFace = aFaceBuilder.Face();
241     setFace( aFace, theToDisplay, theIsUpdateViewer );
242   }
243 }
244
245 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
246                               const bool         theToDisplay,
247                               const bool         theIsUpdateViewer )
248 {
249   myTopoShape = theFace;
250   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_ExactHLR;
251
252   buildShape();
253   updateShape( theToDisplay, theIsUpdateViewer );
254 }
255
256 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
257                                       const bool    theToDisplay,
258                                       const bool    theIsUpdateViewer )
259 {
260   myFillingColor = theColor;
261   updateShape( theToDisplay, theIsUpdateViewer );
262 }
263
264 QColor HYDROGUI_Shape::getFillingColor() const
265 {
266   return myFillingColor;
267 }
268
269 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
270                                      const bool    theToDisplay,
271                                      const bool    theIsUpdateViewer )
272 {
273   myBorderColor = theColor;
274   updateShape( theToDisplay, theIsUpdateViewer );
275 }
276
277 QColor HYDROGUI_Shape::getBorderColor() const
278 {
279   return myBorderColor;
280 }
281
282 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
283 {
284   myHighlightColor = theColor;
285 }
286
287 QColor HYDROGUI_Shape::getHighlightColor() const
288 {
289   return myHighlightColor;
290 }
291
292 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
293                                          const bool     theToDisplay,
294                                          const bool     theIsUpdateViewer )
295 {
296   myTextureFileName = theFileName;
297   updateShape( theToDisplay, theIsUpdateViewer );
298 }
299
300 QString HYDROGUI_Shape::getTextureFileName() const
301 {
302   return myTextureFileName;
303 }
304
305 void HYDROGUI_Shape::buildShape()
306 {
307   // Erase previously created shape
308   erase();
309
310   if ( myTopoShape.IsNull() )
311     return;
312
313   myShape = new AIS_TexturedShape( myTopoShape );
314
315   if ( !myObject.IsNull() )
316     myShape->SetOwner( myObject );
317
318   myShape->SetTransparency( 0 );
319   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
320
321   QString aTextureFileName = getTextureFileName();
322   if( !aTextureFileName.isEmpty() )
323   {
324     myShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
325     myShape->SetTextureMapOn();
326     myShape->DisableTextureModulate();
327   }
328
329     // Init default params for shape
330   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
331   if ( !anAttributes.IsNull() )
332   {
333     if ( myDisplayMode == AIS_Shaded )
334     {
335       Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
336       if ( !anIsoAspect.IsNull() )
337         anIsoAspect->SetNumber( 0 );
338       
339       anIsoAspect = anAttributes->VIsoAspect();
340       if ( !anIsoAspect.IsNull() )
341         anIsoAspect->SetNumber( 0 );
342
343       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
344       if ( !aShadingAspect.IsNull() )
345       {
346         Graphic3d_MaterialAspect aMatAspect;
347         aMatAspect.SetAmbient( 1 );
348         aMatAspect.SetDiffuse( 0 );
349
350         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
351         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
352       }
353     }
354     else if ( myDisplayMode == AIS_WireFrame )
355     {
356       anAttributes->SetWireDraw( true );
357     }
358   }
359 }
360
361 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
362                                   const bool theIsUpdateViewer )
363 {
364   if ( myShape.IsNull() )
365     return;
366
367   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
368   if ( !anAttributes.IsNull() )
369   {
370     if ( myDisplayMode == AIS_Shaded )
371     {
372       // Coloring face filling
373       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
374       if ( !aShadingAspect.IsNull() )
375       {
376         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
377                                       getQuantityColorVal( myFillingColor.green() ),
378                                       getQuantityColorVal( myFillingColor.blue() ),
379                                       Quantity_TOC_RGB );
380
381         aShadingAspect->SetColor( aFillingColor );
382         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
383       }
384     }
385     else if ( myDisplayMode == AIS_WireFrame )
386     {
387     }
388
389     // Coloring borders
390     colorShapeBorder( getActiveColor() );
391   }
392
393   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
394     return;
395   
396   myContext->Display( myShape, theIsUpdateViewer );
397 }
398
399 QColor HYDROGUI_Shape::getActiveColor() const
400 {
401   return isHighlighted() ? myHighlightColor : myBorderColor;
402 }
403
404 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
405 {
406   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
407 }
408
409 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
410 {
411   if ( myShape.IsNull() )
412     return;
413
414   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
415   if ( anAttributes.IsNull() )
416     return;
417
418   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
419                                getQuantityColorVal( theColor.green() ),
420                                getQuantityColorVal( theColor.blue() ),
421                                Quantity_TOC_RGB );
422   if ( myDisplayMode == AIS_Shaded )
423   {
424     if ( theColor.alpha() == 0 )
425     {
426       anAttributes->SetFaceBoundaryDraw( false );
427     }
428     else
429     {
430       anAttributes->SetFaceBoundaryDraw( true );
431
432       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
433       if ( !aBoundaryAspect.IsNull() )
434         aBoundaryAspect->SetColor( aBorderColor );
435     }
436   }
437   else if ( myDisplayMode == AIS_WireFrame )
438   {
439     myShape->SetColor( aBorderColor );
440   }
441 }
442
443 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Object)& theImageObj )
444 {
445   QString aResult;
446   if( !theImageObj.IsNull() )
447   {
448     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
449
450     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
451     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
452
453     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
454     anEntry.replace( ':', '_' );
455
456     QString anExtension = "bmp";
457
458     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
459   }
460   return aResult;
461 }
462
463 void HYDROGUI_Shape::removeTextureFile() const
464 {
465   QFile aFile( getTextureFileName() );
466   if( aFile.exists() )
467     aFile.remove();
468 }
469
470 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
471 {
472   double aSizeLimit = (double)theSizeLimit;
473   double aWidth = (double)theImage.width();
474   double aHeight = (double)theImage.height();
475   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
476   {
477     if( aWidth > aHeight )
478     {
479       aHeight /= ( aWidth / aSizeLimit );
480       aWidth = aSizeLimit;
481     }
482     else
483     {
484       aWidth /= ( aHeight / aSizeLimit );
485       aHeight = aSizeLimit;
486     }
487   }
488   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
489 }