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_PolylineXY.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 <TopExp_Explorer.hxx>
54 #include <BRep_Builder.hxx>
56 #include <Precision.hxx>
58 #include <Prs3d_ShadingAspect.hxx>
59 #include <Prs3d_LineAspect.hxx>
60 #include <Prs3d_IsoAspect.hxx>
62 #include <SUIT_MessageBox.h>
67 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
68 const Handle(HYDROData_Entity)& theObject )
69 : myContext( theContext ),
70 myObject( theObject ),
71 myIsHighlight( false ),
72 myFillingColor( Qt::transparent ),
73 myBorderColor( Qt::black ),
74 myHighlightColor( Qt::white ),
75 myIsToUpdate( false ),
77 myDisplayMode( AIS_WireFrame )
81 HYDROGUI_Shape::~HYDROGUI_Shape()
85 if ( !myShape.IsNull() )
91 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
93 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
96 myContext->Display( myShape, theIsUpdateViewer );
99 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
101 if ( myContext.IsNull() || myShape.IsNull() )
104 myContext->Erase( myShape, theIsUpdateViewer );
107 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
109 setIsToUpdate( false );
111 if ( myContext.IsNull() )
114 // Try to retrieve information from object
115 if ( !myObject.IsNull() )
117 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
119 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
121 Handle(HYDROData_ImmersibleZone) aZoneObj =
122 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
124 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
125 if ( !aZoneShape.IsNull() ) {
126 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
127 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
128 setFace( aZoneFace, false, false );
130 myTopoShape = aZoneShape;
131 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
134 updateShape( false, false );
138 QColor aFillingColor = aZoneObj->GetFillingColor();
139 QColor aBorderColor = aZoneObj->GetBorderColor();
141 setFillingColor( aFillingColor, false, false );
142 setBorderColor( aBorderColor, false, false );
144 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
146 Handle(HYDROData_PolylineXY) aPolyline =
147 Handle(HYDROData_PolylineXY)::DownCast( myObject );
149 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
151 if ( !aPolylineShape.IsNull() ) {
152 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
153 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
154 setWire( aPolylineWire, false, false );
156 myTopoShape = aPolylineShape;
157 myDisplayMode = AIS_WireFrame;
160 updateShape( false, false );
164 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
166 Handle(HYDROData_Zone) aZone =
167 Handle(HYDROData_Zone)::DownCast( myObject );
169 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
171 // Generate the filling color for zone
172 QStringList aGeomObjectsNames;
174 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
175 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
176 for ( ; anIter.More(); anIter.Next() )
178 Handle(HYDROData_Object) aRefbject =
179 Handle(HYDROData_Object)::DownCast( anIter.Value() );
180 if ( aRefbject.IsNull() )
183 QString aRefObjectName = aRefbject->GetName();
184 if ( aRefObjectName.isEmpty() )
187 aGeomObjectsNames.append( aRefObjectName );
190 setFace( aZoneFace, false, false );
191 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
193 // Red color for a zone with bathymetry conflict
194 setFillingColor( Qt::red );
198 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
201 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
203 Handle(HYDROData_Image) anImageObj =
204 Handle(HYDROData_Image)::DownCast( myObject );
208 QString aTextureFileName = generateTextureFileName( anImageObj );
210 QImage anImage = anImageObj->Image();
211 QString aFilePath = anImageObj->GetFilePath();
212 QTransform aTrsf = anImageObj->Trsf();
214 int aWidth = anImage.width();
215 int aHeight = anImage.height();
217 QTransform anInversion = QTransform::fromScale( -1, -1 );
218 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
220 // temporary optimization, to reduce the saved image size (and the texture quality)
221 QImage anImageToSave = reduceTexture( anImage, 500 );
222 bool isSaved = anImageToSave.save( aTextureFileName );
224 QString aTitle = QObject::tr( "FILE_ERROR" );
225 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
226 SUIT_MessageBox::warning( 0, aTitle, aMessage );
229 QPointF aPoint1( 0, 0 );
230 QPointF aPoint2( aWidth, 0 );
231 QPointF aPoint3( aWidth, aHeight );
232 QPointF aPoint4( 0, aHeight );
234 aPoint1 = aTrsf.map( aPoint1 );
235 aPoint2 = aTrsf.map( aPoint2 );
236 aPoint3 = aTrsf.map( aPoint3 );
237 aPoint4 = aTrsf.map( aPoint4 );
239 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
240 QRectF aRect = aPolygon.boundingRect();
242 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
243 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
244 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
245 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
247 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
248 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
249 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
250 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
252 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
254 setTextureFileName( aTextureFileName, false, false );
255 setFace( aWire, false, false );
257 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
259 Handle(HYDROData_Obstacle) anObstacle =
260 Handle(HYDROData_Obstacle)::DownCast( myObject );
262 //TODO BEGIN of the block of code to be reimplemented
263 //TODO GetTopShape() to be used in future
264 myTopoShape = anObstacle->GetShape3D();
265 myDisplayMode = AIS_Shaded;
267 QColor aFillingColor = anObstacle->GetFillingColor();
268 QColor aBorderColor = anObstacle->GetBorderColor();
270 setFillingColor( aFillingColor, false, false );
271 setBorderColor( aBorderColor, false, false );
274 updateShape( false, false );
275 //TODO END of the block of code to be reimplemented
279 if ( myShape.IsNull() || !isVisible() )
282 myContext->Display( myShape, theIsUpdateViewer );
285 void HYDROGUI_Shape::setVisible( const bool theState,
286 const bool theIsUpdateViewer )
288 if ( myIsVisible == theState )
291 myIsVisible = theState;
293 if ( myShape.IsNull() )
297 myContext->Display( myShape, theIsUpdateViewer );
299 myContext->Erase( myShape, theIsUpdateViewer );
302 void HYDROGUI_Shape::highlight( bool theIsHighlight )
304 if ( myIsHighlight == theIsHighlight )
307 myIsHighlight = theIsHighlight;
309 if ( myContext.IsNull() || myShape.IsNull() )
312 colorShapeBorder( getActiveColor() );
313 myContext->Display( myShape );
316 bool HYDROGUI_Shape::isHighlighted() const
318 return myIsHighlight;
321 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
322 const bool theToDisplay,
323 const bool theIsUpdateViewer )
325 myTopoShape = theWire;
326 myDisplayMode = AIS_WireFrame;
329 updateShape( theToDisplay, theIsUpdateViewer );
332 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
333 const bool theToDisplay,
334 const bool theIsUpdateViewer )
336 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
337 TopoDS_Compound aCompound;
338 BRep_Builder aBuilder;
339 aBuilder.MakeCompound( aCompound );
341 for ( ; anExp.More(); anExp.Next() ) {
342 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
343 if ( aWire.IsNull() ) {
347 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
349 if( aMakeFace.IsDone() ) {
350 aBuilder.Add( aCompound, aMakeFace.Face() );
354 myTopoShape = aCompound;
355 myDisplayMode = AIS_Shaded;
358 updateShape( theToDisplay, theIsUpdateViewer );
361 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
362 const bool theToDisplay,
363 const bool theIsUpdateViewer )
365 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
366 aFaceBuilder.Build();
367 if( aFaceBuilder.IsDone() )
369 TopoDS_Face aFace = aFaceBuilder.Face();
370 setFace( aFace, theToDisplay, theIsUpdateViewer );
374 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
375 const bool theToDisplay,
376 const bool theIsUpdateViewer )
378 myTopoShape = theFace;
379 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
380 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
381 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
384 updateShape( theToDisplay, theIsUpdateViewer );
387 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
388 const bool theToDisplay,
389 const bool theIsUpdateViewer )
391 myTopoShape = theShape;
392 myDisplayMode = AIS_Shaded;
395 updateShape( theToDisplay, theIsUpdateViewer );
398 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
399 const bool theToDisplay,
400 const bool theIsUpdateViewer )
402 myFillingColor = theColor;
403 updateShape( theToDisplay, theIsUpdateViewer );
406 QColor HYDROGUI_Shape::getFillingColor() const
408 return myFillingColor;
411 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
412 const bool theToDisplay,
413 const bool theIsUpdateViewer )
415 myBorderColor = theColor;
416 updateShape( theToDisplay, theIsUpdateViewer );
419 QColor HYDROGUI_Shape::getBorderColor() const
421 return myBorderColor;
424 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
426 myHighlightColor = theColor;
429 QColor HYDROGUI_Shape::getHighlightColor() const
431 return myHighlightColor;
434 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
435 const bool theToDisplay,
436 const bool theIsUpdateViewer )
438 myTextureFileName = theFileName;
439 updateShape( theToDisplay, theIsUpdateViewer );
442 QString HYDROGUI_Shape::getTextureFileName() const
444 return myTextureFileName;
447 void HYDROGUI_Shape::buildShape()
449 // Erase previously created shape
452 if ( myTopoShape.IsNull() )
455 QString aTextureFileName = getTextureFileName();
456 bool anIsTexture = !aTextureFileName.isEmpty();
460 myShape = new AIS_TexturedShape( myTopoShape );
464 myShape = new AIS_Shape( myTopoShape );
467 if ( !myObject.IsNull() )
468 myShape->SetOwner( myObject );
470 myShape->SetTransparency( 0 );
471 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
475 Handle(AIS_TexturedShape) aTexturedShape =
476 Handle(AIS_TexturedShape)::DownCast( myShape );
478 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
479 aTexturedShape->SetTextureMapOn();
480 aTexturedShape->DisableTextureModulate();
483 // Init default params for shape
484 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
485 if ( !anAttributes.IsNull() )
487 if ( myDisplayMode == AIS_Shaded )
489 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
490 if ( !anIsoAspect.IsNull() )
491 anIsoAspect->SetNumber( 0 );
493 anIsoAspect = anAttributes->VIsoAspect();
494 if ( !anIsoAspect.IsNull() )
495 anIsoAspect->SetNumber( 0 );
497 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
498 if ( !aShadingAspect.IsNull() )
500 Graphic3d_MaterialAspect aMatAspect;
501 aMatAspect.SetAmbient( 1 );
502 aMatAspect.SetDiffuse( 0 );
504 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
505 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
508 else if ( myDisplayMode == AIS_WireFrame )
510 anAttributes->SetWireDraw( true );
515 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
516 const bool theIsUpdateViewer )
518 if ( myShape.IsNull() )
521 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
522 if ( !anAttributes.IsNull() )
524 if ( myDisplayMode == AIS_Shaded )
526 // Coloring face filling
527 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
528 if ( !aShadingAspect.IsNull() )
530 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
531 getQuantityColorVal( myFillingColor.green() ),
532 getQuantityColorVal( myFillingColor.blue() ),
535 aShadingAspect->SetColor( aFillingColor );
536 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
539 else if ( myDisplayMode == AIS_WireFrame )
544 colorShapeBorder( getActiveColor() );
547 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
550 myContext->Display( myShape, theIsUpdateViewer );
553 QColor HYDROGUI_Shape::getActiveColor() const
555 return isHighlighted() ? myHighlightColor : myBorderColor;
558 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
560 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
563 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
565 if ( myShape.IsNull() )
568 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
569 if ( anAttributes.IsNull() )
572 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
573 getQuantityColorVal( theColor.green() ),
574 getQuantityColorVal( theColor.blue() ),
576 if ( myDisplayMode == AIS_Shaded )
578 if ( theColor.alpha() == 0 )
580 anAttributes->SetFaceBoundaryDraw( false );
584 anAttributes->SetFaceBoundaryDraw( true );
586 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
587 if ( !aBoundaryAspect.IsNull() )
588 aBoundaryAspect->SetColor( aBorderColor );
591 else if ( myDisplayMode == AIS_WireFrame )
593 myShape->SetColor( aBorderColor );
597 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
600 if( !theImageObj.IsNull() )
602 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
604 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
605 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
607 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
608 anEntry.replace( ':', '_' );
610 QString anExtension = "bmp";
612 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
617 void HYDROGUI_Shape::removeTextureFile() const
619 QFile aFile( getTextureFileName() );
624 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
626 double aSizeLimit = (double)theSizeLimit;
627 double aWidth = (double)theImage.width();
628 double aHeight = (double)theImage.height();
629 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
631 if( aWidth > aHeight )
633 aHeight /= ( aWidth / aSizeLimit );
638 aWidth /= ( aHeight / aSizeLimit );
639 aHeight = aSizeLimit;
642 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );