1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Shape.h"
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Tool.h"
28 #include <AIS_Drawer.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <Graphic3d_AspectFillArea3d.hxx>
37 #include <Graphic3d_MaterialAspect.hxx>
39 #include <HYDROData_Domain.h>
40 #include <HYDROData_Image.h>
41 #include <HYDROData_Polyline.h>
43 #include <TopoDS_Wire.hxx>
44 #include <TopoDS_Face.hxx>
46 #include <Precision.hxx>
48 #include <Prs3d_ShadingAspect.hxx>
49 #include <Prs3d_LineAspect.hxx>
50 #include <Prs3d_IsoAspect.hxx>
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 ),
65 myDisplayMode( AIS_WireFrame )
69 HYDROGUI_Shape::~HYDROGUI_Shape()
73 if ( !myShape.IsNull() )
79 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
81 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
84 myContext->Display( myShape, theIsUpdateViewer );
87 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
89 if ( myContext.IsNull() || myShape.IsNull() )
92 myContext->Erase( myShape, theIsUpdateViewer );
95 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
97 setIsToUpdate( false );
99 if ( myContext.IsNull() )
102 // Try to retrieve information from object
103 if ( !myObject.IsNull() )
105 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Domain) ) )
107 Handle(HYDROData_Domain) aDomainObj =
108 Handle(HYDROData_Domain)::DownCast( myObject );
110 QColor aFillingColor = aDomainObj->GetFillingColor();
111 QColor aBorderColor = aDomainObj->GetBorderColor();
112 TopoDS_Face aDomainFace = aDomainObj->Face();
114 setFillingColor( aFillingColor, false, false );
115 setBorderColor( aBorderColor, false, false );
116 setFace( aDomainFace, false, false );
118 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline) ) )
120 Handle(HYDROData_Polyline) aPolyline =
121 Handle(HYDROData_Polyline)::DownCast( myObject );
123 TopoDS_Wire aPolylineWire = aPolyline->Wire();
125 setWire( aPolylineWire, false, false );
127 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
129 Handle(HYDROData_Image) anImageObj =
130 Handle(HYDROData_Image)::DownCast( myObject );
134 QString aTextureFileName = generateTextureFileName( anImageObj );
136 QImage anImage = anImageObj->Image();
137 QString aFilePath = anImageObj->GetFilePath();
138 QTransform aTrsf = anImageObj->Trsf();
140 int aWidth = anImage.width();
141 int aHeight = anImage.height();
143 anImage = anImage.transformed( aTrsf, Qt::SmoothTransformation );
145 // temporary optimization, to reduce the saved image size (and the texture quality)
146 QImage anImageToSave = reduceTexture( anImage, 500 );
147 anImageToSave.save( aTextureFileName );
149 QPointF aPoint1( 0, 0 );
150 QPointF aPoint2( aWidth, 0 );
151 QPointF aPoint3( aWidth, aHeight );
152 QPointF aPoint4( 0, aHeight );
154 aPoint1 = aTrsf.map( aPoint1 );
155 aPoint2 = aTrsf.map( aPoint2 );
156 aPoint3 = aTrsf.map( aPoint3 );
157 aPoint4 = aTrsf.map( aPoint4 );
159 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
160 QRectF aRect = aPolygon.boundingRect();
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 );
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();
172 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
174 setTextureFileName( aTextureFileName, false, false );
175 setFace( aWire, false, false );
179 if ( myShape.IsNull() || !isVisible() )
182 myContext->Display( myShape, theIsUpdateViewer );
185 void HYDROGUI_Shape::setVisible( const bool theState,
186 const bool theIsUpdateViewer )
188 if ( myIsVisible == theState )
191 myIsVisible = theState;
193 if ( myShape.IsNull() )
197 myContext->Display( myShape, theIsUpdateViewer );
199 myContext->Erase( myShape, theIsUpdateViewer );
202 void HYDROGUI_Shape::highlight( bool theIsHighlight )
204 if ( myIsHighlight == theIsHighlight )
207 myIsHighlight = theIsHighlight;
209 if ( myContext.IsNull() || myShape.IsNull() )
212 colorShapeBorder( getActiveColor() );
213 myContext->Display( myShape );
216 bool HYDROGUI_Shape::isHighlighted() const
218 return myIsHighlight;
221 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
222 const bool theToDisplay,
223 const bool theIsUpdateViewer )
225 myTopoShape = theWire;
226 myDisplayMode = AIS_WireFrame;
229 updateShape( theToDisplay, theIsUpdateViewer );
232 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
233 const bool theToDisplay,
234 const bool theIsUpdateViewer )
236 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
237 aFaceBuilder.Build();
238 if( aFaceBuilder.IsDone() )
240 TopoDS_Face aFace = aFaceBuilder.Face();
241 setFace( aFace, theToDisplay, theIsUpdateViewer );
245 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
246 const bool theToDisplay,
247 const bool theIsUpdateViewer )
249 myTopoShape = theFace;
250 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_ExactHLR;
253 updateShape( theToDisplay, theIsUpdateViewer );
256 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
257 const bool theToDisplay,
258 const bool theIsUpdateViewer )
260 myFillingColor = theColor;
261 updateShape( theToDisplay, theIsUpdateViewer );
264 QColor HYDROGUI_Shape::getFillingColor() const
266 return myFillingColor;
269 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
270 const bool theToDisplay,
271 const bool theIsUpdateViewer )
273 myBorderColor = theColor;
274 updateShape( theToDisplay, theIsUpdateViewer );
277 QColor HYDROGUI_Shape::getBorderColor() const
279 return myBorderColor;
282 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
284 myHighlightColor = theColor;
287 QColor HYDROGUI_Shape::getHighlightColor() const
289 return myHighlightColor;
292 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
293 const bool theToDisplay,
294 const bool theIsUpdateViewer )
296 myTextureFileName = theFileName;
297 updateShape( theToDisplay, theIsUpdateViewer );
300 QString HYDROGUI_Shape::getTextureFileName() const
302 return myTextureFileName;
305 void HYDROGUI_Shape::buildShape()
307 // Erase previously created shape
310 if ( myTopoShape.IsNull() )
313 myShape = new AIS_TexturedShape( myTopoShape );
315 if ( !myObject.IsNull() )
316 myShape->SetOwner( myObject );
318 myShape->SetTransparency( 0 );
319 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
321 QString aTextureFileName = getTextureFileName();
322 if( !aTextureFileName.isEmpty() )
324 myShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
325 myShape->SetTextureMapOn();
326 myShape->DisableTextureModulate();
329 // Init default params for shape
330 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
331 if ( !anAttributes.IsNull() )
333 if ( myDisplayMode == AIS_Shaded )
335 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
336 if ( !anIsoAspect.IsNull() )
337 anIsoAspect->SetNumber( 0 );
339 anIsoAspect = anAttributes->VIsoAspect();
340 if ( !anIsoAspect.IsNull() )
341 anIsoAspect->SetNumber( 0 );
343 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
344 if ( !aShadingAspect.IsNull() )
346 Graphic3d_MaterialAspect aMatAspect;
347 aMatAspect.SetAmbient( 1 );
348 aMatAspect.SetDiffuse( 0 );
350 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
351 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
354 else if ( myDisplayMode == AIS_WireFrame )
356 anAttributes->SetWireDraw( true );
361 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
362 const bool theIsUpdateViewer )
364 if ( myShape.IsNull() )
367 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
368 if ( !anAttributes.IsNull() )
370 if ( myDisplayMode == AIS_Shaded )
372 // Coloring face filling
373 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
374 if ( !aShadingAspect.IsNull() )
376 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
377 getQuantityColorVal( myFillingColor.green() ),
378 getQuantityColorVal( myFillingColor.blue() ),
381 aShadingAspect->SetColor( aFillingColor );
382 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
385 else if ( myDisplayMode == AIS_WireFrame )
390 colorShapeBorder( getActiveColor() );
393 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
396 myContext->Display( myShape, theIsUpdateViewer );
399 QColor HYDROGUI_Shape::getActiveColor() const
401 return isHighlighted() ? myHighlightColor : myBorderColor;
404 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
406 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
409 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
411 if ( myShape.IsNull() )
414 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
415 if ( anAttributes.IsNull() )
418 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
419 getQuantityColorVal( theColor.green() ),
420 getQuantityColorVal( theColor.blue() ),
422 if ( myDisplayMode == AIS_Shaded )
424 if ( theColor.alpha() == 0 )
426 anAttributes->SetFaceBoundaryDraw( false );
430 anAttributes->SetFaceBoundaryDraw( true );
432 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
433 if ( !aBoundaryAspect.IsNull() )
434 aBoundaryAspect->SetColor( aBorderColor );
437 else if ( myDisplayMode == AIS_WireFrame )
439 myShape->SetColor( aBorderColor );
443 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Object)& theImageObj )
446 if( !theImageObj.IsNull() )
448 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
450 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
451 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
453 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
454 anEntry.replace( ':', '_' );
456 QString anExtension = "bmp";
458 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
463 void HYDROGUI_Shape::removeTextureFile() const
465 QFile aFile( getTextureFileName() );
470 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
472 double aSizeLimit = (double)theSizeLimit;
473 double aWidth = (double)theImage.width();
474 double aHeight = (double)theImage.height();
475 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
477 if( aWidth > aHeight )
479 aHeight /= ( aWidth / aSizeLimit );
484 aWidth /= ( aHeight / aSizeLimit );
485 aHeight = aSizeLimit;
488 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );