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 aChannelShape2d = TopoDS::Face( aChannel->GetTopShape() );
351 TopoDS_Shape aChannelShape = aChannel->GetShape3D();
353 setShape( aChannelShape, false, false );
355 QColor aFillingColor = aChannel->GetFillingColor();
356 QColor aBorderColor = aChannel->GetBorderColor();
358 setFillingColor( aFillingColor, false, false );
359 setBorderColor( aBorderColor, false, false );
363 if ( myShape.IsNull() || !isVisible() )
366 myContext->Display( myShape, theIsUpdateViewer );
369 void HYDROGUI_Shape::setVisible( const bool theState,
370 const bool theIsUpdateViewer )
372 if ( myIsVisible == theState )
375 myIsVisible = theState;
377 if ( myShape.IsNull() )
381 myContext->Display( myShape, theIsUpdateViewer );
383 myContext->Erase( myShape, theIsUpdateViewer );
386 void HYDROGUI_Shape::highlight( bool theIsHighlight )
388 if ( myIsHighlight == theIsHighlight )
391 myIsHighlight = theIsHighlight;
393 if ( myContext.IsNull() || myShape.IsNull() )
396 colorShapeBorder( getActiveColor() );
397 myContext->Display( myShape );
400 bool HYDROGUI_Shape::isHighlighted() const
402 return myIsHighlight;
405 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
406 const bool theToDisplay,
407 const bool theIsUpdateViewer )
409 myTopoShape = theWire;
410 myDisplayMode = AIS_WireFrame;
413 updateShape( theToDisplay, theIsUpdateViewer );
416 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
417 const bool theToDisplay,
418 const bool theIsUpdateViewer )
420 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
421 TopoDS_Compound aCompound;
422 BRep_Builder aBuilder;
423 aBuilder.MakeCompound( aCompound );
425 for ( ; anExp.More(); anExp.Next() ) {
426 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
427 if ( aWire.IsNull() ) {
431 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
433 if( aMakeFace.IsDone() ) {
434 aBuilder.Add( aCompound, aMakeFace.Face() );
438 myTopoShape = aCompound;
439 myDisplayMode = AIS_Shaded;
442 updateShape( theToDisplay, theIsUpdateViewer );
445 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
446 const bool theToDisplay,
447 const bool theIsUpdateViewer )
449 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
450 aFaceBuilder.Build();
451 if( aFaceBuilder.IsDone() )
453 TopoDS_Face aFace = aFaceBuilder.Face();
454 setFace( aFace, theToDisplay, theIsUpdateViewer );
458 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
459 const bool theToDisplay,
460 const bool theIsUpdateViewer )
462 myTopoShape = theFace;
463 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
464 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
465 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
468 updateShape( theToDisplay, theIsUpdateViewer );
471 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
472 const bool theToDisplay,
473 const bool theIsUpdateViewer )
475 myTopoShape = theShape;
476 myDisplayMode = AIS_Shaded;
479 updateShape( theToDisplay, theIsUpdateViewer );
482 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
483 const bool theToDisplay,
484 const bool theIsUpdateViewer )
486 myFillingColor = theColor;
487 updateShape( theToDisplay, theIsUpdateViewer );
490 QColor HYDROGUI_Shape::getFillingColor() const
492 return myFillingColor;
495 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
496 const bool theToDisplay,
497 const bool theIsUpdateViewer )
499 myBorderColor = theColor;
500 updateShape( theToDisplay, theIsUpdateViewer );
503 QColor HYDROGUI_Shape::getBorderColor() const
505 return myBorderColor;
508 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
510 myHighlightColor = theColor;
513 QColor HYDROGUI_Shape::getHighlightColor() const
515 return myHighlightColor;
518 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
519 const bool theToDisplay,
520 const bool theIsUpdateViewer )
522 myTextureFileName = theFileName;
523 updateShape( theToDisplay, theIsUpdateViewer );
526 QString HYDROGUI_Shape::getTextureFileName() const
528 return myTextureFileName;
531 void HYDROGUI_Shape::buildShape()
533 // Erase previously created shape
536 if ( myTopoShape.IsNull() ) {
541 QString aTextureFileName = getTextureFileName();
542 bool anIsTexture = !aTextureFileName.isEmpty();
546 myShape = new AIS_TexturedShape( myTopoShape );
550 myShape = new AIS_Shape( myTopoShape );
553 if ( !myObject.IsNull() )
554 myShape->SetOwner( myObject );
556 myShape->SetTransparency( 0 );
557 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
561 Handle(AIS_TexturedShape) aTexturedShape =
562 Handle(AIS_TexturedShape)::DownCast( myShape );
564 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
565 aTexturedShape->SetTextureMapOn();
566 // Just use the texture image as is
567 aTexturedShape->DisableTextureModulate();
568 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
571 // Init default params for shape
572 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
573 if ( !anAttributes.IsNull() )
575 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
576 if ( !anIsoAspect.IsNull() ) {
577 anIsoAspect->SetNumber( 0 );
578 anAttributes->SetUIsoAspect( anIsoAspect );
581 anIsoAspect = anAttributes->VIsoAspect();
582 if ( !anIsoAspect.IsNull() ) {
583 anIsoAspect->SetNumber( 0 );
584 anAttributes->SetVIsoAspect( anIsoAspect );
587 if ( myDisplayMode == AIS_Shaded )
589 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
590 if ( !aShadingAspect.IsNull() )
592 Graphic3d_MaterialAspect aMatAspect;
593 aMatAspect.SetAmbient( 1 );
594 aMatAspect.SetDiffuse( 0 );
596 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
597 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
600 else if ( myDisplayMode == AIS_WireFrame )
602 anAttributes->SetWireDraw( true );
607 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
608 const bool theIsUpdateViewer )
610 if ( myShape.IsNull() )
613 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
614 if ( !anAttributes.IsNull() )
616 if ( myDisplayMode == AIS_Shaded )
618 // Coloring face filling
619 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
620 if ( !aShadingAspect.IsNull() )
622 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
623 getQuantityColorVal( myFillingColor.green() ),
624 getQuantityColorVal( myFillingColor.blue() ),
627 aShadingAspect->SetColor( aFillingColor );
628 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
631 else if ( myDisplayMode == AIS_WireFrame )
636 colorShapeBorder( getActiveColor() );
639 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
642 myContext->Display( myShape, theIsUpdateViewer );
645 QColor HYDROGUI_Shape::getActiveColor() const
647 return isHighlighted() ? myHighlightColor : myBorderColor;
650 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
652 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
655 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
657 if ( myShape.IsNull() )
660 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
661 if ( anAttributes.IsNull() )
664 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
665 getQuantityColorVal( theColor.green() ),
666 getQuantityColorVal( theColor.blue() ),
668 if ( myDisplayMode == AIS_Shaded )
670 if ( theColor.alpha() == 0 )
672 anAttributes->SetFaceBoundaryDraw( false );
676 anAttributes->SetFaceBoundaryDraw( true );
678 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
679 if ( !aBoundaryAspect.IsNull() )
680 aBoundaryAspect->SetColor( aBorderColor );
683 else if ( myDisplayMode == AIS_WireFrame )
685 myShape->SetColor( aBorderColor );
689 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
692 if( !theImageObj.IsNull() )
694 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
696 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
697 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
699 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
700 anEntry.replace( ':', '_' );
702 QString anExtension = "bmp";
704 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
709 void HYDROGUI_Shape::removeTextureFile() const
711 QFile aFile( getTextureFileName() );
716 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
718 double aSizeLimit = (double)theSizeLimit;
719 double aWidth = (double)theImage.width();
720 double aHeight = (double)theImage.height();
721 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
723 if( aWidth > aHeight )
725 aHeight /= ( aWidth / aSizeLimit );
730 aWidth /= ( aHeight / aSizeLimit );
731 aHeight = aSizeLimit;
734 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );