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 QTransform anInversion = QTransform::fromScale( -1, -1 );
144 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
146 // temporary optimization, to reduce the saved image size (and the texture quality)
147 QImage anImageToSave = reduceTexture( anImage, 500 );
148 anImageToSave.save( aTextureFileName );
150 QPointF aPoint1( 0, 0 );
151 QPointF aPoint2( aWidth, 0 );
152 QPointF aPoint3( aWidth, aHeight );
153 QPointF aPoint4( 0, aHeight );
155 aPoint1 = aTrsf.map( aPoint1 );
156 aPoint2 = aTrsf.map( aPoint2 );
157 aPoint3 = aTrsf.map( aPoint3 );
158 aPoint4 = aTrsf.map( aPoint4 );
160 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
161 QRectF aRect = aPolygon.boundingRect();
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 );
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();
173 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
175 setTextureFileName( aTextureFileName, false, false );
176 setFace( aWire, false, false );
180 if ( myShape.IsNull() || !isVisible() )
183 myContext->Display( myShape, theIsUpdateViewer );
186 void HYDROGUI_Shape::setVisible( const bool theState,
187 const bool theIsUpdateViewer )
189 if ( myIsVisible == theState )
192 myIsVisible = theState;
194 if ( myShape.IsNull() )
198 myContext->Display( myShape, theIsUpdateViewer );
200 myContext->Erase( myShape, theIsUpdateViewer );
203 void HYDROGUI_Shape::highlight( bool theIsHighlight )
205 if ( myIsHighlight == theIsHighlight )
208 myIsHighlight = theIsHighlight;
210 if ( myContext.IsNull() || myShape.IsNull() )
213 colorShapeBorder( getActiveColor() );
214 myContext->Display( myShape );
217 bool HYDROGUI_Shape::isHighlighted() const
219 return myIsHighlight;
222 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
223 const bool theToDisplay,
224 const bool theIsUpdateViewer )
226 myTopoShape = theWire;
227 myDisplayMode = AIS_WireFrame;
230 updateShape( theToDisplay, theIsUpdateViewer );
233 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
234 const bool theToDisplay,
235 const bool theIsUpdateViewer )
237 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
238 aFaceBuilder.Build();
239 if( aFaceBuilder.IsDone() )
241 TopoDS_Face aFace = aFaceBuilder.Face();
242 setFace( aFace, theToDisplay, theIsUpdateViewer );
246 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
247 const bool theToDisplay,
248 const bool theIsUpdateViewer )
250 myTopoShape = theFace;
251 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_ExactHLR;
254 updateShape( theToDisplay, theIsUpdateViewer );
257 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
258 const bool theToDisplay,
259 const bool theIsUpdateViewer )
261 myFillingColor = theColor;
262 updateShape( theToDisplay, theIsUpdateViewer );
265 QColor HYDROGUI_Shape::getFillingColor() const
267 return myFillingColor;
270 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
271 const bool theToDisplay,
272 const bool theIsUpdateViewer )
274 myBorderColor = theColor;
275 updateShape( theToDisplay, theIsUpdateViewer );
278 QColor HYDROGUI_Shape::getBorderColor() const
280 return myBorderColor;
283 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
285 myHighlightColor = theColor;
288 QColor HYDROGUI_Shape::getHighlightColor() const
290 return myHighlightColor;
293 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
294 const bool theToDisplay,
295 const bool theIsUpdateViewer )
297 myTextureFileName = theFileName;
298 updateShape( theToDisplay, theIsUpdateViewer );
301 QString HYDROGUI_Shape::getTextureFileName() const
303 return myTextureFileName;
306 void HYDROGUI_Shape::buildShape()
308 // Erase previously created shape
311 if ( myTopoShape.IsNull() )
314 myShape = new AIS_TexturedShape( myTopoShape );
316 if ( !myObject.IsNull() )
317 myShape->SetOwner( myObject );
319 myShape->SetTransparency( 0 );
320 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
322 QString aTextureFileName = getTextureFileName();
323 if( !aTextureFileName.isEmpty() )
325 myShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
326 myShape->SetTextureMapOn();
327 myShape->DisableTextureModulate();
330 // Init default params for shape
331 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
332 if ( !anAttributes.IsNull() )
334 if ( myDisplayMode == AIS_Shaded )
336 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
337 if ( !anIsoAspect.IsNull() )
338 anIsoAspect->SetNumber( 0 );
340 anIsoAspect = anAttributes->VIsoAspect();
341 if ( !anIsoAspect.IsNull() )
342 anIsoAspect->SetNumber( 0 );
344 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
345 if ( !aShadingAspect.IsNull() )
347 Graphic3d_MaterialAspect aMatAspect;
348 aMatAspect.SetAmbient( 1 );
349 aMatAspect.SetDiffuse( 0 );
351 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
352 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
355 else if ( myDisplayMode == AIS_WireFrame )
357 anAttributes->SetWireDraw( true );
362 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
363 const bool theIsUpdateViewer )
365 if ( myShape.IsNull() )
368 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
369 if ( !anAttributes.IsNull() )
371 if ( myDisplayMode == AIS_Shaded )
373 // Coloring face filling
374 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
375 if ( !aShadingAspect.IsNull() )
377 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
378 getQuantityColorVal( myFillingColor.green() ),
379 getQuantityColorVal( myFillingColor.blue() ),
382 aShadingAspect->SetColor( aFillingColor );
383 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
386 else if ( myDisplayMode == AIS_WireFrame )
391 colorShapeBorder( getActiveColor() );
394 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
397 myContext->Display( myShape, theIsUpdateViewer );
400 QColor HYDROGUI_Shape::getActiveColor() const
402 return isHighlighted() ? myHighlightColor : myBorderColor;
405 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
407 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
410 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
412 if ( myShape.IsNull() )
415 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
416 if ( anAttributes.IsNull() )
419 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
420 getQuantityColorVal( theColor.green() ),
421 getQuantityColorVal( theColor.blue() ),
423 if ( myDisplayMode == AIS_Shaded )
425 if ( theColor.alpha() == 0 )
427 anAttributes->SetFaceBoundaryDraw( false );
431 anAttributes->SetFaceBoundaryDraw( true );
433 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
434 if ( !aBoundaryAspect.IsNull() )
435 aBoundaryAspect->SetColor( aBorderColor );
438 else if ( myDisplayMode == AIS_WireFrame )
440 myShape->SetColor( aBorderColor );
444 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Object)& theImageObj )
447 if( !theImageObj.IsNull() )
449 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
451 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
452 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
454 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
455 anEntry.replace( ':', '_' );
457 QString anExtension = "bmp";
459 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
464 void HYDROGUI_Shape::removeTextureFile() const
466 QFile aFile( getTextureFileName() );
471 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
473 double aSizeLimit = (double)theSizeLimit;
474 double aWidth = (double)theImage.width();
475 double aHeight = (double)theImage.height();
476 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
478 if( aWidth > aHeight )
480 aHeight /= ( aWidth / aSizeLimit );
485 aWidth /= ( aHeight / aSizeLimit );
486 aHeight = aSizeLimit;
489 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );