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>
29 #include <AIS_TexturedShape.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <BRepBuilderAPI_MakeFace.hxx>
37 #include <Graphic3d_AspectFillArea3d.hxx>
38 #include <Graphic3d_MaterialAspect.hxx>
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>
48 #include <TopoDS_Wire.hxx>
49 #include <TopoDS_Face.hxx>
51 #include <Precision.hxx>
53 #include <Prs3d_ShadingAspect.hxx>
54 #include <Prs3d_LineAspect.hxx>
55 #include <Prs3d_IsoAspect.hxx>
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 ),
70 myDisplayMode( AIS_WireFrame )
74 HYDROGUI_Shape::~HYDROGUI_Shape()
78 if ( !myShape.IsNull() )
84 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
86 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
89 myContext->Display( myShape, theIsUpdateViewer );
92 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
94 if ( myContext.IsNull() || myShape.IsNull() )
97 myContext->Erase( myShape, theIsUpdateViewer );
100 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
102 setIsToUpdate( false );
104 if ( myContext.IsNull() )
107 // Try to retrieve information from object
108 if ( !myObject.IsNull() )
110 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
112 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
114 Handle(HYDROData_ImmersibleZone) aZoneObj =
115 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
117 QColor aFillingColor = aZoneObj->GetFillingColor();
118 QColor aBorderColor = aZoneObj->GetBorderColor();
119 TopoDS_Face aZoneFace = TopoDS::Face( aZoneObj->GetTopShape() );
121 setFillingColor( aFillingColor, false, false );
122 setBorderColor( aBorderColor, false, false );
123 setFace( aZoneFace, false, false );
125 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline) ) )
127 Handle(HYDROData_Polyline) aPolyline =
128 Handle(HYDROData_Polyline)::DownCast( myObject );
130 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolyline->GetTopShape() );
132 setWire( aPolylineWire, false, false );
134 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Region) ) )
136 Handle(HYDROData_Region) aRegion =
137 Handle(HYDROData_Region)::DownCast( myObject );
139 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
141 Handle(HYDROData_Zone) aZone =
142 Handle(HYDROData_Zone)::DownCast( myObject );
144 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
146 // Generate the filling color for zone
147 QStringList aGeomObjectsNames;
149 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
150 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
151 for ( ; anIter.More(); anIter.Next() )
153 Handle(HYDROData_Object) aRefbject =
154 Handle(HYDROData_Object)::DownCast( anIter.Value() );
155 if ( aRefbject.IsNull() )
158 QString aRefObjectName = aRefbject->GetName();
159 if ( aRefObjectName.isEmpty() )
162 aGeomObjectsNames.append( aRefObjectName );
165 setFace( aZoneFace, false, false );
166 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
168 // Red color for a zone with bathymetry conflict
169 setFillingColor( Qt::red );
173 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
176 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
178 Handle(HYDROData_Image) anImageObj =
179 Handle(HYDROData_Image)::DownCast( myObject );
183 QString aTextureFileName = generateTextureFileName( anImageObj );
185 QImage anImage = anImageObj->Image();
186 QString aFilePath = anImageObj->GetFilePath();
187 QTransform aTrsf = anImageObj->Trsf();
189 int aWidth = anImage.width();
190 int aHeight = anImage.height();
192 QTransform anInversion = QTransform::fromScale( -1, -1 );
193 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
195 // temporary optimization, to reduce the saved image size (and the texture quality)
196 QImage anImageToSave = reduceTexture( anImage, 500 );
197 anImageToSave.save( aTextureFileName );
199 QPointF aPoint1( 0, 0 );
200 QPointF aPoint2( aWidth, 0 );
201 QPointF aPoint3( aWidth, aHeight );
202 QPointF aPoint4( 0, aHeight );
204 aPoint1 = aTrsf.map( aPoint1 );
205 aPoint2 = aTrsf.map( aPoint2 );
206 aPoint3 = aTrsf.map( aPoint3 );
207 aPoint4 = aTrsf.map( aPoint4 );
209 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
210 QRectF aRect = aPolygon.boundingRect();
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 );
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();
222 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
224 setTextureFileName( aTextureFileName, false, false );
225 setFace( aWire, false, false );
229 if ( myShape.IsNull() || !isVisible() )
232 myContext->Display( myShape, theIsUpdateViewer );
235 void HYDROGUI_Shape::setVisible( const bool theState,
236 const bool theIsUpdateViewer )
238 if ( myIsVisible == theState )
241 myIsVisible = theState;
243 if ( myShape.IsNull() )
247 myContext->Display( myShape, theIsUpdateViewer );
249 myContext->Erase( myShape, theIsUpdateViewer );
252 void HYDROGUI_Shape::highlight( bool theIsHighlight )
254 if ( myIsHighlight == theIsHighlight )
257 myIsHighlight = theIsHighlight;
259 if ( myContext.IsNull() || myShape.IsNull() )
262 colorShapeBorder( getActiveColor() );
263 myContext->Display( myShape );
266 bool HYDROGUI_Shape::isHighlighted() const
268 return myIsHighlight;
271 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
272 const bool theToDisplay,
273 const bool theIsUpdateViewer )
275 myTopoShape = theWire;
276 myDisplayMode = AIS_WireFrame;
279 updateShape( theToDisplay, theIsUpdateViewer );
282 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
283 const bool theToDisplay,
284 const bool theIsUpdateViewer )
286 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
287 aFaceBuilder.Build();
288 if( aFaceBuilder.IsDone() )
290 TopoDS_Face aFace = aFaceBuilder.Face();
291 setFace( aFace, theToDisplay, theIsUpdateViewer );
295 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
296 const bool theToDisplay,
297 const bool theIsUpdateViewer )
299 myTopoShape = theFace;
300 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_ExactHLR;
303 updateShape( theToDisplay, theIsUpdateViewer );
306 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
307 const bool theToDisplay,
308 const bool theIsUpdateViewer )
310 myFillingColor = theColor;
311 updateShape( theToDisplay, theIsUpdateViewer );
314 QColor HYDROGUI_Shape::getFillingColor() const
316 return myFillingColor;
319 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
320 const bool theToDisplay,
321 const bool theIsUpdateViewer )
323 myBorderColor = theColor;
324 updateShape( theToDisplay, theIsUpdateViewer );
327 QColor HYDROGUI_Shape::getBorderColor() const
329 return myBorderColor;
332 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
334 myHighlightColor = theColor;
337 QColor HYDROGUI_Shape::getHighlightColor() const
339 return myHighlightColor;
342 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
343 const bool theToDisplay,
344 const bool theIsUpdateViewer )
346 myTextureFileName = theFileName;
347 updateShape( theToDisplay, theIsUpdateViewer );
350 QString HYDROGUI_Shape::getTextureFileName() const
352 return myTextureFileName;
355 void HYDROGUI_Shape::buildShape()
357 // Erase previously created shape
360 if ( myTopoShape.IsNull() )
363 QString aTextureFileName = getTextureFileName();
364 bool anIsTexture = !aTextureFileName.isEmpty();
368 myShape = new AIS_TexturedShape( myTopoShape );
372 myShape = new AIS_Shape( myTopoShape );
375 if ( !myObject.IsNull() )
376 myShape->SetOwner( myObject );
378 myShape->SetTransparency( 0 );
379 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
383 Handle(AIS_TexturedShape) aTexturedShape =
384 Handle(AIS_TexturedShape)::DownCast( myShape );
386 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
387 aTexturedShape->SetTextureMapOn();
388 aTexturedShape->DisableTextureModulate();
391 // Init default params for shape
392 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
393 if ( !anAttributes.IsNull() )
395 if ( myDisplayMode == AIS_Shaded )
397 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
398 if ( !anIsoAspect.IsNull() )
399 anIsoAspect->SetNumber( 0 );
401 anIsoAspect = anAttributes->VIsoAspect();
402 if ( !anIsoAspect.IsNull() )
403 anIsoAspect->SetNumber( 0 );
405 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
406 if ( !aShadingAspect.IsNull() )
408 Graphic3d_MaterialAspect aMatAspect;
409 aMatAspect.SetAmbient( 1 );
410 aMatAspect.SetDiffuse( 0 );
412 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
413 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
416 else if ( myDisplayMode == AIS_WireFrame )
418 anAttributes->SetWireDraw( true );
423 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
424 const bool theIsUpdateViewer )
426 if ( myShape.IsNull() )
429 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
430 if ( !anAttributes.IsNull() )
432 if ( myDisplayMode == AIS_Shaded )
434 // Coloring face filling
435 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
436 if ( !aShadingAspect.IsNull() )
438 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
439 getQuantityColorVal( myFillingColor.green() ),
440 getQuantityColorVal( myFillingColor.blue() ),
443 aShadingAspect->SetColor( aFillingColor );
444 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
447 else if ( myDisplayMode == AIS_WireFrame )
452 colorShapeBorder( getActiveColor() );
455 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
458 myContext->Display( myShape, theIsUpdateViewer );
461 QColor HYDROGUI_Shape::getActiveColor() const
463 return isHighlighted() ? myHighlightColor : myBorderColor;
466 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
468 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
471 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
473 if ( myShape.IsNull() )
476 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
477 if ( anAttributes.IsNull() )
480 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
481 getQuantityColorVal( theColor.green() ),
482 getQuantityColorVal( theColor.blue() ),
484 if ( myDisplayMode == AIS_Shaded )
486 if ( theColor.alpha() == 0 )
488 anAttributes->SetFaceBoundaryDraw( false );
492 anAttributes->SetFaceBoundaryDraw( true );
494 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
495 if ( !aBoundaryAspect.IsNull() )
496 aBoundaryAspect->SetColor( aBorderColor );
499 else if ( myDisplayMode == AIS_WireFrame )
501 myShape->SetColor( aBorderColor );
505 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
508 if( !theImageObj.IsNull() )
510 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
512 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
513 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
515 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
516 anEntry.replace( ':', '_' );
518 QString anExtension = "bmp";
520 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
525 void HYDROGUI_Shape::removeTextureFile() const
527 QFile aFile( getTextureFileName() );
532 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
534 double aSizeLimit = (double)theSizeLimit;
535 double aWidth = (double)theImage.width();
536 double aHeight = (double)theImage.height();
537 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
539 if( aWidth > aHeight )
541 aHeight /= ( aWidth / aSizeLimit );
546 aWidth /= ( aHeight / aSizeLimit );
547 aHeight = aSizeLimit;
550 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );