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_Zone) ) )
136 Handle(HYDROData_Zone) aZone =
137 Handle(HYDROData_Zone)::DownCast( myObject );
139 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
141 // Generate the filling color for zone
142 QStringList aGeomObjectsNames;
144 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
145 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
146 for ( ; anIter.More(); anIter.Next() )
148 Handle(HYDROData_Object) aRefbject =
149 Handle(HYDROData_Object)::DownCast( anIter.Value() );
150 if ( aRefbject.IsNull() )
153 QString aRefObjectName = aRefbject->GetName();
154 if ( aRefObjectName.isEmpty() )
157 aGeomObjectsNames.append( aRefObjectName );
160 setFace( aZoneFace, false, false );
161 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
163 // Red color for a zone with bathymetry conflict
164 setFillingColor( Qt::red );
168 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
171 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
173 Handle(HYDROData_Image) anImageObj =
174 Handle(HYDROData_Image)::DownCast( myObject );
178 QString aTextureFileName = generateTextureFileName( anImageObj );
180 QImage anImage = anImageObj->Image();
181 QString aFilePath = anImageObj->GetFilePath();
182 QTransform aTrsf = anImageObj->Trsf();
184 int aWidth = anImage.width();
185 int aHeight = anImage.height();
187 QTransform anInversion = QTransform::fromScale( -1, -1 );
188 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
190 // temporary optimization, to reduce the saved image size (and the texture quality)
191 QImage anImageToSave = reduceTexture( anImage, 500 );
192 anImageToSave.save( aTextureFileName );
194 QPointF aPoint1( 0, 0 );
195 QPointF aPoint2( aWidth, 0 );
196 QPointF aPoint3( aWidth, aHeight );
197 QPointF aPoint4( 0, aHeight );
199 aPoint1 = aTrsf.map( aPoint1 );
200 aPoint2 = aTrsf.map( aPoint2 );
201 aPoint3 = aTrsf.map( aPoint3 );
202 aPoint4 = aTrsf.map( aPoint4 );
204 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
205 QRectF aRect = aPolygon.boundingRect();
207 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
208 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
209 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
210 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
212 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
213 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
214 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
215 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
217 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
219 setTextureFileName( aTextureFileName, false, false );
220 setFace( aWire, false, false );
224 if ( myShape.IsNull() || !isVisible() )
227 myContext->Display( myShape, theIsUpdateViewer );
230 void HYDROGUI_Shape::setVisible( const bool theState,
231 const bool theIsUpdateViewer )
233 if ( myIsVisible == theState )
236 myIsVisible = theState;
238 if ( myShape.IsNull() )
242 myContext->Display( myShape, theIsUpdateViewer );
244 myContext->Erase( myShape, theIsUpdateViewer );
247 void HYDROGUI_Shape::highlight( bool theIsHighlight )
249 if ( myIsHighlight == theIsHighlight )
252 myIsHighlight = theIsHighlight;
254 if ( myContext.IsNull() || myShape.IsNull() )
257 colorShapeBorder( getActiveColor() );
258 myContext->Display( myShape );
261 bool HYDROGUI_Shape::isHighlighted() const
263 return myIsHighlight;
266 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
267 const bool theToDisplay,
268 const bool theIsUpdateViewer )
270 myTopoShape = theWire;
271 myDisplayMode = AIS_WireFrame;
274 updateShape( theToDisplay, theIsUpdateViewer );
277 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
278 const bool theToDisplay,
279 const bool theIsUpdateViewer )
281 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
282 aFaceBuilder.Build();
283 if( aFaceBuilder.IsDone() )
285 TopoDS_Face aFace = aFaceBuilder.Face();
286 setFace( aFace, theToDisplay, theIsUpdateViewer );
290 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
291 const bool theToDisplay,
292 const bool theIsUpdateViewer )
294 myTopoShape = theFace;
295 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_ExactHLR;
298 updateShape( theToDisplay, theIsUpdateViewer );
301 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
302 const bool theToDisplay,
303 const bool theIsUpdateViewer )
305 myFillingColor = theColor;
306 updateShape( theToDisplay, theIsUpdateViewer );
309 QColor HYDROGUI_Shape::getFillingColor() const
311 return myFillingColor;
314 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
315 const bool theToDisplay,
316 const bool theIsUpdateViewer )
318 myBorderColor = theColor;
319 updateShape( theToDisplay, theIsUpdateViewer );
322 QColor HYDROGUI_Shape::getBorderColor() const
324 return myBorderColor;
327 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
329 myHighlightColor = theColor;
332 QColor HYDROGUI_Shape::getHighlightColor() const
334 return myHighlightColor;
337 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
338 const bool theToDisplay,
339 const bool theIsUpdateViewer )
341 myTextureFileName = theFileName;
342 updateShape( theToDisplay, theIsUpdateViewer );
345 QString HYDROGUI_Shape::getTextureFileName() const
347 return myTextureFileName;
350 void HYDROGUI_Shape::buildShape()
352 // Erase previously created shape
355 if ( myTopoShape.IsNull() )
358 QString aTextureFileName = getTextureFileName();
359 bool anIsTexture = !aTextureFileName.isEmpty();
363 myShape = new AIS_TexturedShape( myTopoShape );
367 myShape = new AIS_Shape( myTopoShape );
370 if ( !myObject.IsNull() )
371 myShape->SetOwner( myObject );
373 myShape->SetTransparency( 0 );
374 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
378 Handle(AIS_TexturedShape) aTexturedShape =
379 Handle(AIS_TexturedShape)::DownCast( myShape );
381 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
382 aTexturedShape->SetTextureMapOn();
383 aTexturedShape->DisableTextureModulate();
386 // Init default params for shape
387 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
388 if ( !anAttributes.IsNull() )
390 if ( myDisplayMode == AIS_Shaded )
392 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
393 if ( !anIsoAspect.IsNull() )
394 anIsoAspect->SetNumber( 0 );
396 anIsoAspect = anAttributes->VIsoAspect();
397 if ( !anIsoAspect.IsNull() )
398 anIsoAspect->SetNumber( 0 );
400 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
401 if ( !aShadingAspect.IsNull() )
403 Graphic3d_MaterialAspect aMatAspect;
404 aMatAspect.SetAmbient( 1 );
405 aMatAspect.SetDiffuse( 0 );
407 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
408 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
411 else if ( myDisplayMode == AIS_WireFrame )
413 anAttributes->SetWireDraw( true );
418 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
419 const bool theIsUpdateViewer )
421 if ( myShape.IsNull() )
424 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
425 if ( !anAttributes.IsNull() )
427 if ( myDisplayMode == AIS_Shaded )
429 // Coloring face filling
430 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
431 if ( !aShadingAspect.IsNull() )
433 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
434 getQuantityColorVal( myFillingColor.green() ),
435 getQuantityColorVal( myFillingColor.blue() ),
438 aShadingAspect->SetColor( aFillingColor );
439 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
442 else if ( myDisplayMode == AIS_WireFrame )
447 colorShapeBorder( getActiveColor() );
450 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
453 myContext->Display( myShape, theIsUpdateViewer );
456 QColor HYDROGUI_Shape::getActiveColor() const
458 return isHighlighted() ? myHighlightColor : myBorderColor;
461 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
463 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
466 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
468 if ( myShape.IsNull() )
471 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
472 if ( anAttributes.IsNull() )
475 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
476 getQuantityColorVal( theColor.green() ),
477 getQuantityColorVal( theColor.blue() ),
479 if ( myDisplayMode == AIS_Shaded )
481 if ( theColor.alpha() == 0 )
483 anAttributes->SetFaceBoundaryDraw( false );
487 anAttributes->SetFaceBoundaryDraw( true );
489 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
490 if ( !aBoundaryAspect.IsNull() )
491 aBoundaryAspect->SetColor( aBorderColor );
494 else if ( myDisplayMode == AIS_WireFrame )
496 myShape->SetColor( aBorderColor );
500 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
503 if( !theImageObj.IsNull() )
505 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
507 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
508 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
510 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
511 anEntry.replace( ':', '_' );
513 QString anExtension = "bmp";
515 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
520 void HYDROGUI_Shape::removeTextureFile() const
522 QFile aFile( getTextureFileName() );
527 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
529 double aSizeLimit = (double)theSizeLimit;
530 double aWidth = (double)theImage.width();
531 double aHeight = (double)theImage.height();
532 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
534 if( aWidth > aHeight )
536 aHeight /= ( aWidth / aSizeLimit );
541 aWidth /= ( aHeight / aSizeLimit );
542 aHeight = aSizeLimit;
545 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );