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>
46 #include <HYDROData_Obstacle.h>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Face.hxx>
52 #include <Precision.hxx>
54 #include <Prs3d_ShadingAspect.hxx>
55 #include <Prs3d_LineAspect.hxx>
56 #include <Prs3d_IsoAspect.hxx>
61 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
62 const Handle(HYDROData_Entity)& theObject )
63 : myContext( theContext ),
64 myObject( theObject ),
65 myIsHighlight( false ),
66 myFillingColor( Qt::transparent ),
67 myBorderColor( Qt::black ),
68 myHighlightColor( Qt::white ),
69 myIsToUpdate( false ),
71 myDisplayMode( AIS_WireFrame )
75 HYDROGUI_Shape::~HYDROGUI_Shape()
79 if ( !myShape.IsNull() )
85 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
87 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
90 myContext->Display( myShape, theIsUpdateViewer );
93 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
95 if ( myContext.IsNull() || myShape.IsNull() )
98 myContext->Erase( myShape, theIsUpdateViewer );
101 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
103 setIsToUpdate( false );
105 if ( myContext.IsNull() )
108 // Try to retrieve information from object
109 if ( !myObject.IsNull() )
111 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
113 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
115 Handle(HYDROData_ImmersibleZone) aZoneObj =
116 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
118 QColor aFillingColor = aZoneObj->GetFillingColor();
119 QColor aBorderColor = aZoneObj->GetBorderColor();
120 TopoDS_Face aZoneFace = TopoDS::Face( aZoneObj->GetTopShape() );
122 setFillingColor( aFillingColor, false, false );
123 setBorderColor( aBorderColor, false, false );
124 setFace( aZoneFace, false, false );
126 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline) ) )
128 Handle(HYDROData_Polyline) aPolyline =
129 Handle(HYDROData_Polyline)::DownCast( myObject );
131 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolyline->GetTopShape() );
133 setWire( aPolylineWire, false, false );
135 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
137 Handle(HYDROData_Zone) aZone =
138 Handle(HYDROData_Zone)::DownCast( myObject );
140 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
142 // Generate the filling color for zone
143 QStringList aGeomObjectsNames;
145 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
146 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
147 for ( ; anIter.More(); anIter.Next() )
149 Handle(HYDROData_Object) aRefbject =
150 Handle(HYDROData_Object)::DownCast( anIter.Value() );
151 if ( aRefbject.IsNull() )
154 QString aRefObjectName = aRefbject->GetName();
155 if ( aRefObjectName.isEmpty() )
158 aGeomObjectsNames.append( aRefObjectName );
161 setFace( aZoneFace, false, false );
162 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
164 // Red color for a zone with bathymetry conflict
165 setFillingColor( Qt::red );
169 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
172 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
174 Handle(HYDROData_Image) anImageObj =
175 Handle(HYDROData_Image)::DownCast( myObject );
179 QString aTextureFileName = generateTextureFileName( anImageObj );
181 QImage anImage = anImageObj->Image();
182 QString aFilePath = anImageObj->GetFilePath();
183 QTransform aTrsf = anImageObj->Trsf();
185 int aWidth = anImage.width();
186 int aHeight = anImage.height();
188 QTransform anInversion = QTransform::fromScale( -1, -1 );
189 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
191 // temporary optimization, to reduce the saved image size (and the texture quality)
192 QImage anImageToSave = reduceTexture( anImage, 500 );
193 anImageToSave.save( aTextureFileName );
195 QPointF aPoint1( 0, 0 );
196 QPointF aPoint2( aWidth, 0 );
197 QPointF aPoint3( aWidth, aHeight );
198 QPointF aPoint4( 0, aHeight );
200 aPoint1 = aTrsf.map( aPoint1 );
201 aPoint2 = aTrsf.map( aPoint2 );
202 aPoint3 = aTrsf.map( aPoint3 );
203 aPoint4 = aTrsf.map( aPoint4 );
205 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
206 QRectF aRect = aPolygon.boundingRect();
208 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
209 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
210 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
211 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
213 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
214 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
215 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
216 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
218 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
220 setTextureFileName( aTextureFileName, false, false );
221 setFace( aWire, false, false );
223 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
225 Handle(HYDROData_Obstacle) anObstacle =
226 Handle(HYDROData_Obstacle)::DownCast( myObject );
228 //TODO BEGIN of the block of code to be reimplemented
229 //TODO GetTopShape() to be used in future
230 myTopoShape = anObstacle->GetShape3D();
231 myDisplayMode = AIS_Shaded;
233 QColor aFillingColor = anObstacle->GetFillingColor();
234 QColor aBorderColor = anObstacle->GetBorderColor();
236 setFillingColor( aFillingColor, false, false );
237 setBorderColor( aBorderColor, false, false );
240 updateShape( false, false );
241 //TODO END of the block of code to be reimplemented
245 if ( myShape.IsNull() || !isVisible() )
248 myContext->Display( myShape, theIsUpdateViewer );
251 void HYDROGUI_Shape::setVisible( const bool theState,
252 const bool theIsUpdateViewer )
254 if ( myIsVisible == theState )
257 myIsVisible = theState;
259 if ( myShape.IsNull() )
263 myContext->Display( myShape, theIsUpdateViewer );
265 myContext->Erase( myShape, theIsUpdateViewer );
268 void HYDROGUI_Shape::highlight( bool theIsHighlight )
270 if ( myIsHighlight == theIsHighlight )
273 myIsHighlight = theIsHighlight;
275 if ( myContext.IsNull() || myShape.IsNull() )
278 colorShapeBorder( getActiveColor() );
279 myContext->Display( myShape );
282 bool HYDROGUI_Shape::isHighlighted() const
284 return myIsHighlight;
287 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
288 const bool theToDisplay,
289 const bool theIsUpdateViewer )
291 myTopoShape = theWire;
292 myDisplayMode = AIS_WireFrame;
295 updateShape( theToDisplay, theIsUpdateViewer );
298 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
299 const bool theToDisplay,
300 const bool theIsUpdateViewer )
302 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
303 aFaceBuilder.Build();
304 if( aFaceBuilder.IsDone() )
306 TopoDS_Face aFace = aFaceBuilder.Face();
307 setFace( aFace, theToDisplay, theIsUpdateViewer );
311 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
312 const bool theToDisplay,
313 const bool theIsUpdateViewer )
315 myTopoShape = theFace;
316 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
317 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
318 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
321 updateShape( theToDisplay, theIsUpdateViewer );
324 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
325 const bool theToDisplay,
326 const bool theIsUpdateViewer )
328 myFillingColor = theColor;
329 updateShape( theToDisplay, theIsUpdateViewer );
332 QColor HYDROGUI_Shape::getFillingColor() const
334 return myFillingColor;
337 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
338 const bool theToDisplay,
339 const bool theIsUpdateViewer )
341 myBorderColor = theColor;
342 updateShape( theToDisplay, theIsUpdateViewer );
345 QColor HYDROGUI_Shape::getBorderColor() const
347 return myBorderColor;
350 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
352 myHighlightColor = theColor;
355 QColor HYDROGUI_Shape::getHighlightColor() const
357 return myHighlightColor;
360 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
361 const bool theToDisplay,
362 const bool theIsUpdateViewer )
364 myTextureFileName = theFileName;
365 updateShape( theToDisplay, theIsUpdateViewer );
368 QString HYDROGUI_Shape::getTextureFileName() const
370 return myTextureFileName;
373 void HYDROGUI_Shape::buildShape()
375 // Erase previously created shape
378 if ( myTopoShape.IsNull() )
381 QString aTextureFileName = getTextureFileName();
382 bool anIsTexture = !aTextureFileName.isEmpty();
386 myShape = new AIS_TexturedShape( myTopoShape );
390 myShape = new AIS_Shape( myTopoShape );
393 if ( !myObject.IsNull() )
394 myShape->SetOwner( myObject );
396 myShape->SetTransparency( 0 );
397 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
401 Handle(AIS_TexturedShape) aTexturedShape =
402 Handle(AIS_TexturedShape)::DownCast( myShape );
404 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
405 aTexturedShape->SetTextureMapOn();
406 aTexturedShape->DisableTextureModulate();
409 // Init default params for shape
410 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
411 if ( !anAttributes.IsNull() )
413 if ( myDisplayMode == AIS_Shaded )
415 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
416 if ( !anIsoAspect.IsNull() )
417 anIsoAspect->SetNumber( 0 );
419 anIsoAspect = anAttributes->VIsoAspect();
420 if ( !anIsoAspect.IsNull() )
421 anIsoAspect->SetNumber( 0 );
423 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
424 if ( !aShadingAspect.IsNull() )
426 Graphic3d_MaterialAspect aMatAspect;
427 aMatAspect.SetAmbient( 1 );
428 aMatAspect.SetDiffuse( 0 );
430 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
431 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
434 else if ( myDisplayMode == AIS_WireFrame )
436 anAttributes->SetWireDraw( true );
441 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
442 const bool theIsUpdateViewer )
444 if ( myShape.IsNull() )
447 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
448 if ( !anAttributes.IsNull() )
450 if ( myDisplayMode == AIS_Shaded )
452 // Coloring face filling
453 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
454 if ( !aShadingAspect.IsNull() )
456 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
457 getQuantityColorVal( myFillingColor.green() ),
458 getQuantityColorVal( myFillingColor.blue() ),
461 aShadingAspect->SetColor( aFillingColor );
462 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
465 else if ( myDisplayMode == AIS_WireFrame )
470 colorShapeBorder( getActiveColor() );
473 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
476 myContext->Display( myShape, theIsUpdateViewer );
479 QColor HYDROGUI_Shape::getActiveColor() const
481 return isHighlighted() ? myHighlightColor : myBorderColor;
484 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
486 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
489 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
491 if ( myShape.IsNull() )
494 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
495 if ( anAttributes.IsNull() )
498 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
499 getQuantityColorVal( theColor.green() ),
500 getQuantityColorVal( theColor.blue() ),
502 if ( myDisplayMode == AIS_Shaded )
504 if ( theColor.alpha() == 0 )
506 anAttributes->SetFaceBoundaryDraw( false );
510 anAttributes->SetFaceBoundaryDraw( true );
512 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
513 if ( !aBoundaryAspect.IsNull() )
514 aBoundaryAspect->SetColor( aBorderColor );
517 else if ( myDisplayMode == AIS_WireFrame )
519 myShape->SetColor( aBorderColor );
523 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
526 if( !theImageObj.IsNull() )
528 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
530 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
531 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
533 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
534 anEntry.replace( ':', '_' );
536 QString anExtension = "bmp";
538 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
543 void HYDROGUI_Shape::removeTextureFile() const
545 QFile aFile( getTextureFileName() );
550 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
552 double aSizeLimit = (double)theSizeLimit;
553 double aWidth = (double)theImage.width();
554 double aHeight = (double)theImage.height();
555 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
557 if( aWidth > aHeight )
559 aHeight /= ( aWidth / aSizeLimit );
564 aWidth /= ( aHeight / aSizeLimit );
565 aHeight = aSizeLimit;
568 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );