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_Profile.h>
47 #include <HYDROData_Region.h>
48 #include <HYDROData_Stream.h>
49 #include <HYDROData_Zone.h>
52 #include <TopoDS_Wire.hxx>
53 #include <TopoDS_Face.hxx>
55 #include <TopExp_Explorer.hxx>
57 #include <BRep_Builder.hxx>
59 #include <Precision.hxx>
61 #include <Prs3d_ShadingAspect.hxx>
62 #include <Prs3d_LineAspect.hxx>
63 #include <Prs3d_IsoAspect.hxx>
65 #include <SUIT_MessageBox.h>
70 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
71 const Handle(HYDROData_Entity)& theObject )
72 : myContext( theContext ),
73 myObject( theObject ),
74 myIsHighlight( false ),
75 myFillingColor( Qt::transparent ),
76 myBorderColor( Qt::black ),
77 myHighlightColor( Qt::white ),
78 myIsToUpdate( false ),
80 myDisplayMode( AIS_WireFrame )
84 HYDROGUI_Shape::~HYDROGUI_Shape()
88 if ( !myShape.IsNull() )
94 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
96 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
99 myContext->Display( myShape, theIsUpdateViewer );
102 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
104 if ( myContext.IsNull() || myShape.IsNull() )
107 myContext->Erase( myShape, theIsUpdateViewer );
110 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
112 setIsToUpdate( false );
114 if ( myContext.IsNull() )
117 // Try to retrieve information from object
118 if ( !myObject.IsNull() )
120 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
122 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
124 Handle(HYDROData_ImmersibleZone) aZoneObj =
125 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
127 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
128 if ( !aZoneShape.IsNull() ) {
129 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
130 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
131 setFace( aZoneFace, false, false );
133 myTopoShape = aZoneShape;
134 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
137 updateShape( false, false );
141 QColor aFillingColor = aZoneObj->GetFillingColor();
142 QColor aBorderColor = aZoneObj->GetBorderColor();
144 setFillingColor( aFillingColor, false, false );
145 setBorderColor( aBorderColor, false, false );
147 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
149 Handle(HYDROData_PolylineXY) aPolyline =
150 Handle(HYDROData_PolylineXY)::DownCast( myObject );
152 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
154 if ( !aPolylineShape.IsNull() ) {
155 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
156 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
157 setWire( aPolylineWire, false, false );
159 myTopoShape = aPolylineShape;
160 myDisplayMode = AIS_WireFrame;
163 updateShape( false, false );
167 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
169 Handle(HYDROData_Zone) aZone =
170 Handle(HYDROData_Zone)::DownCast( myObject );
172 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
174 setFace( aZoneFace, false, false );
175 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
177 // Red color for a zone with bathymetry conflict
178 setFillingColor( Qt::red );
182 // Generate the filling color for zone
183 QStringList aGeomObjectsNames;
185 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
186 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
187 for ( ; anIter.More(); anIter.Next() )
189 Handle(HYDROData_Object) aRefbject =
190 Handle(HYDROData_Object)::DownCast( anIter.Value() );
191 if ( aRefbject.IsNull() )
194 QString aRefObjectName = aRefbject->GetName();
195 if ( aRefObjectName.isEmpty() )
198 aGeomObjectsNames.append( aRefObjectName );
201 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
204 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
206 Handle(HYDROData_Image) anImageObj =
207 Handle(HYDROData_Image)::DownCast( myObject );
211 QString aTextureFileName = generateTextureFileName( anImageObj );
213 QImage anImage = anImageObj->Image();
214 QString aFilePath = anImageObj->GetFilePath();
215 QTransform aTrsf = anImageObj->Trsf();
217 int aWidth = anImage.width();
218 int aHeight = anImage.height();
220 QTransform anInversion = QTransform::fromScale( -1, -1 );
221 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
223 // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
224 int aTrsfWidth = anImage.width();
225 int aDelta = aTrsfWidth % 4;
228 aTrsfWidth += ( 4 - aDelta );
230 anImage = anImage.scaledToWidth( aTrsfWidth );
232 // temporary optimization, to reduce the saved image size (and the texture quality)
233 QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
235 bool isSaved = anImageToSave.save( aTextureFileName );
237 QString aTitle = QObject::tr( "FILE_ERROR" );
238 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
239 SUIT_MessageBox::warning( 0, aTitle, aMessage );
242 QPointF aPoint1( 0, 0 ); // 1: top left
243 QPointF aPoint2( aWidth, 0 ); // 2: top right
244 QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
245 QPointF aPoint4( 0, aHeight ); // 4: bottom left
247 aPoint1 = aTrsf.map( aPoint1 );
248 aPoint2 = aTrsf.map( aPoint2 );
249 aPoint3 = aTrsf.map( aPoint3 );
250 aPoint4 = aTrsf.map( aPoint4 );
252 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
253 QRectF aRect = aPolygon.boundingRect();
255 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
256 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
257 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
258 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
260 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
261 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
262 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
263 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
265 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
266 aWire.Closed( true );
268 setTextureFileName( aTextureFileName, false, false );
269 setFace( aWire, false, false );
271 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
273 Handle(HYDROData_Obstacle) anObstacle =
274 Handle(HYDROData_Obstacle)::DownCast( myObject );
276 //TODO BEGIN of the block of code to be reimplemented
277 //TODO GetTopShape() to be used in future
278 myTopoShape = anObstacle->GetShape3D();
279 myDisplayMode = AIS_Shaded;
281 QColor aFillingColor = anObstacle->GetFillingColor();
282 QColor aBorderColor = anObstacle->GetBorderColor();
284 setFillingColor( aFillingColor, false, false );
285 setBorderColor( aBorderColor, false, false );
288 updateShape( false, false );
289 //TODO END of the block of code to be reimplemented
291 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
293 Handle(HYDROData_Profile) aProfile =
294 Handle(HYDROData_Profile)::DownCast( myObject );
296 TopoDS_Wire aProfileWire;
298 if ( aProfile->IsValid() ) {
299 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
301 if ( !aProfileShape.IsNull() &&
302 aProfileShape.ShapeType() == TopAbs_WIRE ) {
303 aProfileWire = TopoDS::Wire( aProfileShape );
307 setWire( aProfileWire, false, false );
309 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) )
311 Handle(HYDROData_Stream) aStream =
312 Handle(HYDROData_Stream)::DownCast( myObject );
314 TopoDS_Shell aStreamShape = TopoDS::Shell( aStream->GetTopShape() );
316 setShape( aStreamShape, false, false );
318 QColor aFillingColor = aStream->GetFillingColor();
319 QColor aBorderColor = aStream->GetBorderColor();
321 setFillingColor( aFillingColor, false, false );
322 setBorderColor( aBorderColor, false, false );
324 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) )
326 Handle(HYDROData_Channel) aChannel =
327 Handle(HYDROData_Channel)::DownCast( myObject );
329 TopoDS_Face aChannelShape = TopoDS::Face( aChannel->GetTopShape() );
331 setShape( aChannelShape, false, false );
333 QColor aFillingColor = aChannel->GetFillingColor();
334 QColor aBorderColor = aChannel->GetBorderColor();
336 setFillingColor( aFillingColor, false, false );
337 setBorderColor( aBorderColor, false, false );
341 if ( myShape.IsNull() || !isVisible() )
344 myContext->Display( myShape, theIsUpdateViewer );
347 void HYDROGUI_Shape::setVisible( const bool theState,
348 const bool theIsUpdateViewer )
350 if ( myIsVisible == theState )
353 myIsVisible = theState;
355 if ( myShape.IsNull() )
359 myContext->Display( myShape, theIsUpdateViewer );
361 myContext->Erase( myShape, theIsUpdateViewer );
364 void HYDROGUI_Shape::highlight( bool theIsHighlight )
366 if ( myIsHighlight == theIsHighlight )
369 myIsHighlight = theIsHighlight;
371 if ( myContext.IsNull() || myShape.IsNull() )
374 colorShapeBorder( getActiveColor() );
375 myContext->Display( myShape );
378 bool HYDROGUI_Shape::isHighlighted() const
380 return myIsHighlight;
383 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
384 const bool theToDisplay,
385 const bool theIsUpdateViewer )
387 myTopoShape = theWire;
388 myDisplayMode = AIS_WireFrame;
391 updateShape( theToDisplay, theIsUpdateViewer );
394 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
395 const bool theToDisplay,
396 const bool theIsUpdateViewer )
398 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
399 TopoDS_Compound aCompound;
400 BRep_Builder aBuilder;
401 aBuilder.MakeCompound( aCompound );
403 for ( ; anExp.More(); anExp.Next() ) {
404 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
405 if ( aWire.IsNull() ) {
409 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
411 if( aMakeFace.IsDone() ) {
412 aBuilder.Add( aCompound, aMakeFace.Face() );
416 myTopoShape = aCompound;
417 myDisplayMode = AIS_Shaded;
420 updateShape( theToDisplay, theIsUpdateViewer );
423 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
424 const bool theToDisplay,
425 const bool theIsUpdateViewer )
427 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
428 aFaceBuilder.Build();
429 if( aFaceBuilder.IsDone() )
431 TopoDS_Face aFace = aFaceBuilder.Face();
432 setFace( aFace, theToDisplay, theIsUpdateViewer );
436 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
437 const bool theToDisplay,
438 const bool theIsUpdateViewer )
440 myTopoShape = theFace;
441 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
442 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
443 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
446 updateShape( theToDisplay, theIsUpdateViewer );
449 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
450 const bool theToDisplay,
451 const bool theIsUpdateViewer )
453 myTopoShape = theShape;
454 myDisplayMode = AIS_Shaded;
457 updateShape( theToDisplay, theIsUpdateViewer );
460 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
461 const bool theToDisplay,
462 const bool theIsUpdateViewer )
464 myFillingColor = theColor;
465 updateShape( theToDisplay, theIsUpdateViewer );
468 QColor HYDROGUI_Shape::getFillingColor() const
470 return myFillingColor;
473 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
474 const bool theToDisplay,
475 const bool theIsUpdateViewer )
477 myBorderColor = theColor;
478 updateShape( theToDisplay, theIsUpdateViewer );
481 QColor HYDROGUI_Shape::getBorderColor() const
483 return myBorderColor;
486 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
488 myHighlightColor = theColor;
491 QColor HYDROGUI_Shape::getHighlightColor() const
493 return myHighlightColor;
496 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
497 const bool theToDisplay,
498 const bool theIsUpdateViewer )
500 myTextureFileName = theFileName;
501 updateShape( theToDisplay, theIsUpdateViewer );
504 QString HYDROGUI_Shape::getTextureFileName() const
506 return myTextureFileName;
509 void HYDROGUI_Shape::buildShape()
511 // Erase previously created shape
514 if ( myTopoShape.IsNull() ) {
519 QString aTextureFileName = getTextureFileName();
520 bool anIsTexture = !aTextureFileName.isEmpty();
524 myShape = new AIS_TexturedShape( myTopoShape );
528 myShape = new AIS_Shape( myTopoShape );
531 if ( !myObject.IsNull() )
532 myShape->SetOwner( myObject );
534 myShape->SetTransparency( 0 );
535 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
539 Handle(AIS_TexturedShape) aTexturedShape =
540 Handle(AIS_TexturedShape)::DownCast( myShape );
542 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
543 aTexturedShape->SetTextureMapOn();
544 // Just use the texture image as is
545 aTexturedShape->DisableTextureModulate();
546 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
549 // Init default params for shape
550 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
551 if ( !anAttributes.IsNull() )
553 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
554 if ( !anIsoAspect.IsNull() ) {
555 anIsoAspect->SetNumber( 0 );
556 anAttributes->SetUIsoAspect( anIsoAspect );
559 anIsoAspect = anAttributes->VIsoAspect();
560 if ( !anIsoAspect.IsNull() ) {
561 anIsoAspect->SetNumber( 0 );
562 anAttributes->SetVIsoAspect( anIsoAspect );
565 if ( myDisplayMode == AIS_Shaded )
567 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
568 if ( !aShadingAspect.IsNull() )
570 Graphic3d_MaterialAspect aMatAspect;
571 aMatAspect.SetAmbient( 1 );
572 aMatAspect.SetDiffuse( 0 );
574 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
575 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
578 else if ( myDisplayMode == AIS_WireFrame )
580 anAttributes->SetWireDraw( true );
585 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
586 const bool theIsUpdateViewer )
588 if ( myShape.IsNull() )
591 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
592 if ( !anAttributes.IsNull() )
594 if ( myDisplayMode == AIS_Shaded )
596 // Coloring face filling
597 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
598 if ( !aShadingAspect.IsNull() )
600 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
601 getQuantityColorVal( myFillingColor.green() ),
602 getQuantityColorVal( myFillingColor.blue() ),
605 aShadingAspect->SetColor( aFillingColor );
606 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
609 else if ( myDisplayMode == AIS_WireFrame )
614 colorShapeBorder( getActiveColor() );
617 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
620 myContext->Display( myShape, theIsUpdateViewer );
623 QColor HYDROGUI_Shape::getActiveColor() const
625 return isHighlighted() ? myHighlightColor : myBorderColor;
628 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
630 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
633 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
635 if ( myShape.IsNull() )
638 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
639 if ( anAttributes.IsNull() )
642 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
643 getQuantityColorVal( theColor.green() ),
644 getQuantityColorVal( theColor.blue() ),
646 if ( myDisplayMode == AIS_Shaded )
648 if ( theColor.alpha() == 0 )
650 anAttributes->SetFaceBoundaryDraw( false );
654 anAttributes->SetFaceBoundaryDraw( true );
656 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
657 if ( !aBoundaryAspect.IsNull() )
658 aBoundaryAspect->SetColor( aBorderColor );
661 else if ( myDisplayMode == AIS_WireFrame )
663 myShape->SetColor( aBorderColor );
667 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
670 if( !theImageObj.IsNull() )
672 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
674 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
675 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
677 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
678 anEntry.replace( ':', '_' );
680 QString anExtension = "bmp";
682 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
687 void HYDROGUI_Shape::removeTextureFile() const
689 QFile aFile( getTextureFileName() );
694 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
696 double aSizeLimit = (double)theSizeLimit;
697 double aWidth = (double)theImage.width();
698 double aHeight = (double)theImage.height();
699 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
701 if( aWidth > aHeight )
703 aHeight /= ( aWidth / aSizeLimit );
708 aWidth /= ( aHeight / aSizeLimit );
709 aHeight = aSizeLimit;
712 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );