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_Channel.h>
41 #include <HYDROData_Document.h>
42 #include <HYDROData_Image.h>
43 #include <HYDROData_ImmersibleZone.h>
44 #include <HYDROData_Obstacle.h>
45 #include <HYDROData_PolylineXY.h>
46 #include <HYDROData_Polyline3D.h>
47 #include <HYDROData_Profile.h>
48 #include <HYDROData_Region.h>
49 #include <HYDROData_Stream.h>
50 #include <HYDROData_Zone.h>
53 #include <TopoDS_Wire.hxx>
54 #include <TopoDS_Face.hxx>
56 #include <TopExp_Explorer.hxx>
58 #include <BRep_Builder.hxx>
60 #include <Precision.hxx>
62 #include <Prs3d_ShadingAspect.hxx>
63 #include <Prs3d_LineAspect.hxx>
64 #include <Prs3d_IsoAspect.hxx>
66 #include <SUIT_MessageBox.h>
71 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
72 const Handle(HYDROData_Entity)& theObject )
73 : myContext( theContext ),
74 myObject( theObject ),
75 myIsHighlight( false ),
76 myFillingColor( Qt::transparent ),
77 myBorderColor( Qt::black ),
78 myHighlightColor( Qt::white ),
79 myIsToUpdate( false ),
81 myDisplayMode( AIS_WireFrame )
85 HYDROGUI_Shape::~HYDROGUI_Shape()
89 if ( !myShape.IsNull() )
95 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
97 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
100 myContext->Display( myShape, theIsUpdateViewer );
103 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
105 if ( myContext.IsNull() || myShape.IsNull() )
108 myContext->Erase( myShape, theIsUpdateViewer );
111 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
113 setIsToUpdate( false );
115 if ( myContext.IsNull() )
118 // Try to retrieve information from object
119 if ( !myObject.IsNull() )
121 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
123 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
125 Handle(HYDROData_ImmersibleZone) aZoneObj =
126 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
128 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
129 if ( !aZoneShape.IsNull() ) {
130 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
131 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
132 setFace( aZoneFace, false, false );
134 myTopoShape = aZoneShape;
135 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
138 updateShape( false, false );
142 QColor aFillingColor = aZoneObj->GetFillingColor();
143 QColor aBorderColor = aZoneObj->GetBorderColor();
145 setFillingColor( aFillingColor, false, false );
146 setBorderColor( aBorderColor, false, false );
148 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
150 Handle(HYDROData_PolylineXY) aPolyline =
151 Handle(HYDROData_PolylineXY)::DownCast( myObject );
153 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
155 if ( !aPolylineShape.IsNull() ) {
156 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
157 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
158 setWire( aPolylineWire, false, false );
160 myTopoShape = aPolylineShape;
161 myDisplayMode = AIS_WireFrame;
164 updateShape( false, false );
168 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
170 Handle(HYDROData_Polyline3D) aPolyline =
171 Handle(HYDROData_Polyline3D)::DownCast( myObject );
173 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
175 if ( !aPolylineShape.IsNull() ) {
176 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
177 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
178 setWire( aPolylineWire, false, false );
180 myTopoShape = aPolylineShape;
181 myDisplayMode = AIS_WireFrame;
184 updateShape( false, false );
188 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
190 Handle(HYDROData_Zone) aZone =
191 Handle(HYDROData_Zone)::DownCast( myObject );
193 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
195 setFace( aZoneFace, false, false );
196 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
198 // Red color for a zone with bathymetry conflict
199 setFillingColor( Qt::red );
203 // Generate the filling color for zone
204 QStringList aGeomObjectsNames;
206 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
207 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
208 for ( ; anIter.More(); anIter.Next() )
210 Handle(HYDROData_Object) aRefbject =
211 Handle(HYDROData_Object)::DownCast( anIter.Value() );
212 if ( aRefbject.IsNull() )
215 QString aRefObjectName = aRefbject->GetName();
216 if ( aRefObjectName.isEmpty() )
219 aGeomObjectsNames.append( aRefObjectName );
222 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
225 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
227 Handle(HYDROData_Image) anImageObj =
228 Handle(HYDROData_Image)::DownCast( myObject );
232 QString aTextureFileName = generateTextureFileName( anImageObj );
234 QImage anImage = anImageObj->Image();
235 QString aFilePath = anImageObj->GetFilePath();
236 QTransform aTrsf = anImageObj->Trsf();
238 int aWidth = anImage.width();
239 int aHeight = anImage.height();
241 QTransform anInversion = QTransform::fromScale( -1, -1 );
242 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
244 // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
245 int aTrsfWidth = anImage.width();
246 int aDelta = aTrsfWidth % 4;
249 aTrsfWidth += ( 4 - aDelta );
251 anImage = anImage.scaledToWidth( aTrsfWidth );
253 // temporary optimization, to reduce the saved image size (and the texture quality)
254 QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
256 bool isSaved = anImageToSave.save( aTextureFileName );
258 QString aTitle = QObject::tr( "FILE_ERROR" );
259 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
260 SUIT_MessageBox::warning( 0, aTitle, aMessage );
263 QPointF aPoint1( 0, 0 ); // 1: top left
264 QPointF aPoint2( aWidth, 0 ); // 2: top right
265 QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
266 QPointF aPoint4( 0, aHeight ); // 4: bottom left
268 aPoint1 = aTrsf.map( aPoint1 );
269 aPoint2 = aTrsf.map( aPoint2 );
270 aPoint3 = aTrsf.map( aPoint3 );
271 aPoint4 = aTrsf.map( aPoint4 );
273 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
274 QRectF aRect = aPolygon.boundingRect();
276 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
277 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
278 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
279 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
281 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
282 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
283 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
284 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
286 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
287 aWire.Closed( true );
289 setTextureFileName( aTextureFileName, false, false );
290 setFace( aWire, false, false );
292 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
294 Handle(HYDROData_Obstacle) anObstacle =
295 Handle(HYDROData_Obstacle)::DownCast( myObject );
297 //TODO BEGIN of the block of code to be reimplemented
298 //TODO GetTopShape() to be used in future
299 myTopoShape = anObstacle->GetShape3D();
300 myDisplayMode = AIS_Shaded;
302 QColor aFillingColor = anObstacle->GetFillingColor();
303 QColor aBorderColor = anObstacle->GetBorderColor();
305 setFillingColor( aFillingColor, false, false );
306 setBorderColor( aBorderColor, false, false );
309 updateShape( false, false );
310 //TODO END of the block of code to be reimplemented
312 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
314 Handle(HYDROData_Profile) aProfile =
315 Handle(HYDROData_Profile)::DownCast( myObject );
317 TopoDS_Wire aProfileWire;
319 if ( aProfile->IsValid() ) {
320 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
322 if ( !aProfileShape.IsNull() &&
323 aProfileShape.ShapeType() == TopAbs_WIRE ) {
324 aProfileWire = TopoDS::Wire( aProfileShape );
328 setWire( aProfileWire, false, false );
330 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) )
332 Handle(HYDROData_Stream) aStream =
333 Handle(HYDROData_Stream)::DownCast( myObject );
335 TopoDS_Shell aStreamShape = TopoDS::Shell( aStream->GetTopShape() );
337 setShape( aStreamShape, false, false );
339 QColor aFillingColor = aStream->GetFillingColor();
340 QColor aBorderColor = aStream->GetBorderColor();
342 setFillingColor( aFillingColor, false, false );
343 setBorderColor( aBorderColor, false, false );
345 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) )
347 Handle(HYDROData_Channel) aChannel =
348 Handle(HYDROData_Channel)::DownCast( myObject );
350 TopoDS_Face aChannelShape = TopoDS::Face( aChannel->GetTopShape() );
352 setShape( aChannelShape, false, false );
354 QColor aFillingColor = aChannel->GetFillingColor();
355 QColor aBorderColor = aChannel->GetBorderColor();
357 setFillingColor( aFillingColor, false, false );
358 setBorderColor( aBorderColor, false, false );
362 if ( myShape.IsNull() || !isVisible() )
365 myContext->Display( myShape, theIsUpdateViewer );
368 void HYDROGUI_Shape::setVisible( const bool theState,
369 const bool theIsUpdateViewer )
371 if ( myIsVisible == theState )
374 myIsVisible = theState;
376 if ( myShape.IsNull() )
380 myContext->Display( myShape, theIsUpdateViewer );
382 myContext->Erase( myShape, theIsUpdateViewer );
385 void HYDROGUI_Shape::highlight( bool theIsHighlight )
387 if ( myIsHighlight == theIsHighlight )
390 myIsHighlight = theIsHighlight;
392 if ( myContext.IsNull() || myShape.IsNull() )
395 colorShapeBorder( getActiveColor() );
396 myContext->Display( myShape );
399 bool HYDROGUI_Shape::isHighlighted() const
401 return myIsHighlight;
404 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
405 const bool theToDisplay,
406 const bool theIsUpdateViewer )
408 myTopoShape = theWire;
409 myDisplayMode = AIS_WireFrame;
412 updateShape( theToDisplay, theIsUpdateViewer );
415 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
416 const bool theToDisplay,
417 const bool theIsUpdateViewer )
419 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
420 TopoDS_Compound aCompound;
421 BRep_Builder aBuilder;
422 aBuilder.MakeCompound( aCompound );
424 for ( ; anExp.More(); anExp.Next() ) {
425 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
426 if ( aWire.IsNull() ) {
430 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
432 if( aMakeFace.IsDone() ) {
433 aBuilder.Add( aCompound, aMakeFace.Face() );
437 myTopoShape = aCompound;
438 myDisplayMode = AIS_Shaded;
441 updateShape( theToDisplay, theIsUpdateViewer );
444 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
445 const bool theToDisplay,
446 const bool theIsUpdateViewer )
448 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
449 aFaceBuilder.Build();
450 if( aFaceBuilder.IsDone() )
452 TopoDS_Face aFace = aFaceBuilder.Face();
453 setFace( aFace, theToDisplay, theIsUpdateViewer );
457 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
458 const bool theToDisplay,
459 const bool theIsUpdateViewer )
461 myTopoShape = theFace;
462 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
463 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
464 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
467 updateShape( theToDisplay, theIsUpdateViewer );
470 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
471 const bool theToDisplay,
472 const bool theIsUpdateViewer )
474 myTopoShape = theShape;
475 myDisplayMode = AIS_Shaded;
478 updateShape( theToDisplay, theIsUpdateViewer );
481 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
482 const bool theToDisplay,
483 const bool theIsUpdateViewer )
485 myFillingColor = theColor;
486 updateShape( theToDisplay, theIsUpdateViewer );
489 QColor HYDROGUI_Shape::getFillingColor() const
491 return myFillingColor;
494 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
495 const bool theToDisplay,
496 const bool theIsUpdateViewer )
498 myBorderColor = theColor;
499 updateShape( theToDisplay, theIsUpdateViewer );
502 QColor HYDROGUI_Shape::getBorderColor() const
504 return myBorderColor;
507 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
509 myHighlightColor = theColor;
512 QColor HYDROGUI_Shape::getHighlightColor() const
514 return myHighlightColor;
517 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
518 const bool theToDisplay,
519 const bool theIsUpdateViewer )
521 myTextureFileName = theFileName;
522 updateShape( theToDisplay, theIsUpdateViewer );
525 QString HYDROGUI_Shape::getTextureFileName() const
527 return myTextureFileName;
530 void HYDROGUI_Shape::buildShape()
532 // Erase previously created shape
535 if ( myTopoShape.IsNull() ) {
540 QString aTextureFileName = getTextureFileName();
541 bool anIsTexture = !aTextureFileName.isEmpty();
545 myShape = new AIS_TexturedShape( myTopoShape );
549 myShape = new AIS_Shape( myTopoShape );
552 if ( !myObject.IsNull() )
553 myShape->SetOwner( myObject );
555 myShape->SetTransparency( 0 );
556 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
560 Handle(AIS_TexturedShape) aTexturedShape =
561 Handle(AIS_TexturedShape)::DownCast( myShape );
563 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
564 aTexturedShape->SetTextureMapOn();
565 // Just use the texture image as is
566 aTexturedShape->DisableTextureModulate();
567 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
570 // Init default params for shape
571 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
572 if ( !anAttributes.IsNull() )
574 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
575 if ( !anIsoAspect.IsNull() ) {
576 anIsoAspect->SetNumber( 0 );
577 anAttributes->SetUIsoAspect( anIsoAspect );
580 anIsoAspect = anAttributes->VIsoAspect();
581 if ( !anIsoAspect.IsNull() ) {
582 anIsoAspect->SetNumber( 0 );
583 anAttributes->SetVIsoAspect( anIsoAspect );
586 if ( myDisplayMode == AIS_Shaded )
588 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
589 if ( !aShadingAspect.IsNull() )
591 Graphic3d_MaterialAspect aMatAspect;
592 aMatAspect.SetAmbient( 1 );
593 aMatAspect.SetDiffuse( 0 );
595 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
596 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
599 else if ( myDisplayMode == AIS_WireFrame )
601 anAttributes->SetWireDraw( true );
606 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
607 const bool theIsUpdateViewer )
609 if ( myShape.IsNull() )
612 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
613 if ( !anAttributes.IsNull() )
615 if ( myDisplayMode == AIS_Shaded )
617 // Coloring face filling
618 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
619 if ( !aShadingAspect.IsNull() )
621 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
622 getQuantityColorVal( myFillingColor.green() ),
623 getQuantityColorVal( myFillingColor.blue() ),
626 aShadingAspect->SetColor( aFillingColor );
627 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
630 else if ( myDisplayMode == AIS_WireFrame )
635 colorShapeBorder( getActiveColor() );
638 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
641 myContext->Display( myShape, theIsUpdateViewer );
644 QColor HYDROGUI_Shape::getActiveColor() const
646 return isHighlighted() ? myHighlightColor : myBorderColor;
649 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
651 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
654 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
656 if ( myShape.IsNull() )
659 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
660 if ( anAttributes.IsNull() )
663 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
664 getQuantityColorVal( theColor.green() ),
665 getQuantityColorVal( theColor.blue() ),
667 if ( myDisplayMode == AIS_Shaded )
669 if ( theColor.alpha() == 0 )
671 anAttributes->SetFaceBoundaryDraw( false );
675 anAttributes->SetFaceBoundaryDraw( true );
677 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
678 if ( !aBoundaryAspect.IsNull() )
679 aBoundaryAspect->SetColor( aBorderColor );
682 else if ( myDisplayMode == AIS_WireFrame )
684 myShape->SetColor( aBorderColor );
688 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
691 if( !theImageObj.IsNull() )
693 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
695 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
696 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
698 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
699 anEntry.replace( ':', '_' );
701 QString anExtension = "bmp";
703 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
708 void HYDROGUI_Shape::removeTextureFile() const
710 QFile aFile( getTextureFileName() );
715 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
717 double aSizeLimit = (double)theSizeLimit;
718 double aWidth = (double)theImage.width();
719 double aHeight = (double)theImage.height();
720 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
722 if( aWidth > aHeight )
724 aHeight /= ( aWidth / aSizeLimit );
729 aWidth /= ( aHeight / aSizeLimit );
730 aHeight = aSizeLimit;
733 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );