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_DummyObject3D.h>
43 #include <HYDROData_Image.h>
44 #include <HYDROData_ImmersibleZone.h>
45 #include <HYDROData_Obstacle.h>
46 #include <HYDROData_PolylineXY.h>
47 #include <HYDROData_Polyline3D.h>
48 #include <HYDROData_Profile.h>
49 #include <HYDROData_Region.h>
50 #include <HYDROData_Stream.h>
51 #include <HYDROData_Zone.h>
54 #include <TopoDS_Wire.hxx>
55 #include <TopoDS_Face.hxx>
57 #include <TopExp_Explorer.hxx>
59 #include <BRep_Builder.hxx>
61 #include <Precision.hxx>
63 #include <Prs3d_ShadingAspect.hxx>
64 #include <Prs3d_LineAspect.hxx>
65 #include <Prs3d_IsoAspect.hxx>
67 #include <SUIT_MessageBox.h>
72 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
73 const Handle(HYDROData_Entity)& theObject )
74 : myContext( theContext ),
75 myObject( theObject ),
76 myIsHighlight( false ),
77 myFillingColor( Qt::transparent ),
78 myBorderColor( Qt::black ),
79 myHighlightColor( Qt::white ),
80 myIsToUpdate( false ),
82 myDisplayMode( AIS_WireFrame )
86 HYDROGUI_Shape::~HYDROGUI_Shape()
90 if ( !myShape.IsNull() )
96 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
98 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
101 myContext->Display( myShape, theIsUpdateViewer );
104 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
106 if ( myContext.IsNull() || myShape.IsNull() )
109 myContext->Erase( myShape, theIsUpdateViewer );
112 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
114 setIsToUpdate( false );
116 if ( myContext.IsNull() )
119 // Try to retrieve information from object
120 if ( !myObject.IsNull() )
122 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
124 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
126 Handle(HYDROData_ImmersibleZone) aZoneObj =
127 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
129 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
130 if ( !aZoneShape.IsNull() ) {
131 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
132 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
133 setFace( aZoneFace, false, false );
135 myTopoShape = aZoneShape;
136 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
139 updateShape( false, false );
143 QColor aFillingColor = aZoneObj->GetFillingColor();
144 QColor aBorderColor = aZoneObj->GetBorderColor();
146 setFillingColor( aFillingColor, false, false );
147 setBorderColor( aBorderColor, false, false );
149 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
151 Handle(HYDROData_PolylineXY) aPolyline =
152 Handle(HYDROData_PolylineXY)::DownCast( myObject );
154 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
156 if ( !aPolylineShape.IsNull() ) {
157 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
158 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
159 setWire( aPolylineWire, false, false );
161 myTopoShape = aPolylineShape;
162 myDisplayMode = AIS_WireFrame;
165 updateShape( false, false );
169 QColor aWireColor = aPolyline->GetWireColor();
170 setBorderColor( aWireColor, false, false );
172 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
174 Handle(HYDROData_Polyline3D) aPolyline =
175 Handle(HYDROData_Polyline3D)::DownCast( myObject );
177 TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
179 if ( !aPolylineShape.IsNull() ) {
180 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
181 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
182 setWire( aPolylineWire, false, false );
184 myTopoShape = aPolylineShape;
185 myDisplayMode = AIS_WireFrame;
188 updateShape( false, false );
192 QColor aWireColor = aPolyline->GetBorderColor();
193 setBorderColor( aWireColor, false, false );
195 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
197 Handle(HYDROData_Zone) aZone =
198 Handle(HYDROData_Zone)::DownCast( myObject );
200 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
202 setFace( aZoneFace, false, false );
203 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
205 // Red color for a zone with bathymetry conflict
206 setFillingColor( Qt::red );
210 // Generate the filling color for zone
211 QStringList aGeomObjectsNames;
213 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
214 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
215 for ( ; anIter.More(); anIter.Next() )
217 Handle(HYDROData_Object) aRefbject =
218 Handle(HYDROData_Object)::DownCast( anIter.Value() );
219 if ( aRefbject.IsNull() )
222 QString aRefObjectName = aRefbject->GetName();
223 if ( aRefObjectName.isEmpty() )
226 aGeomObjectsNames.append( aRefObjectName );
229 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
232 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
234 Handle(HYDROData_Image) anImageObj =
235 Handle(HYDROData_Image)::DownCast( myObject );
239 QString aTextureFileName = generateTextureFileName( anImageObj );
241 QImage anImage = anImageObj->Image();
242 QString aFilePath = anImageObj->GetFilePath();
243 QTransform aTrsf = anImageObj->Trsf();
245 int aWidth = anImage.width();
246 int aHeight = anImage.height();
248 QTransform anInversion = QTransform::fromScale( -1, -1 );
249 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
251 // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
252 int aTrsfWidth = anImage.width();
253 int aDelta = aTrsfWidth % 4;
256 aTrsfWidth += ( 4 - aDelta );
258 anImage = anImage.scaledToWidth( aTrsfWidth );
260 // temporary optimization, to reduce the saved image size (and the texture quality)
261 QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
263 bool isSaved = anImageToSave.save( aTextureFileName );
265 QString aTitle = QObject::tr( "FILE_ERROR" );
266 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
267 SUIT_MessageBox::warning( 0, aTitle, aMessage );
270 QPointF aPoint1( 0, 0 ); // 1: top left
271 QPointF aPoint2( aWidth, 0 ); // 2: top right
272 QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
273 QPointF aPoint4( 0, aHeight ); // 4: bottom left
275 aPoint1 = aTrsf.map( aPoint1 );
276 aPoint2 = aTrsf.map( aPoint2 );
277 aPoint3 = aTrsf.map( aPoint3 );
278 aPoint4 = aTrsf.map( aPoint4 );
280 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
281 QRectF aRect = aPolygon.boundingRect();
283 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
284 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
285 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
286 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
288 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
289 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
290 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
291 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
293 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
294 aWire.Closed( true );
296 setTextureFileName( aTextureFileName, false, false );
297 setFace( aWire, false, false );
299 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
301 Handle(HYDROData_Profile) aProfile =
302 Handle(HYDROData_Profile)::DownCast( myObject );
304 TopoDS_Wire aProfileWire;
306 if ( aProfile->IsValid() ) {
307 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
309 if ( !aProfileShape.IsNull() &&
310 aProfileShape.ShapeType() == TopAbs_WIRE ) {
311 aProfileWire = TopoDS::Wire( aProfileShape );
315 setWire( aProfileWire, false, false );
317 QColor aWireColor = aProfile->GetBorderColor();
318 setBorderColor( aWireColor, false, false );
320 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
321 myObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
322 myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
324 Handle(HYDROData_Object) aGeomObject =
325 Handle(HYDROData_Object)::DownCast( myObject );
327 TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
329 setShape( anObjShape, false, false );
331 QColor aFillingColor = aGeomObject->GetFillingColor();
332 QColor aBorderColor = aGeomObject->GetBorderColor();
334 setFillingColor( aFillingColor, false, false );
335 setBorderColor( aBorderColor, false, false );
337 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
339 Handle(HYDROData_DummyObject3D) anObject3D =
340 Handle(HYDROData_DummyObject3D)::DownCast( myObject );
342 TopoDS_Shape aShape3D = anObject3D->GetShape();
344 setShape( aShape3D, false, false );
346 QColor aFillingColor = anObject3D->GetFillingColor();
347 QColor aBorderColor = anObject3D->GetBorderColor();
349 setFillingColor( aFillingColor, false, false );
350 setBorderColor( aBorderColor, false, false );
354 if ( myShape.IsNull() || !isVisible() )
357 myContext->Display( myShape, theIsUpdateViewer );
360 void HYDROGUI_Shape::setVisible( const bool theState,
361 const bool theIsUpdateViewer )
363 if ( myIsVisible == theState )
366 myIsVisible = theState;
368 if ( myShape.IsNull() )
372 myContext->Display( myShape, theIsUpdateViewer );
374 myContext->Erase( myShape, theIsUpdateViewer );
377 void HYDROGUI_Shape::highlight( bool theIsHighlight )
379 if ( myIsHighlight == theIsHighlight )
382 myIsHighlight = theIsHighlight;
384 if ( myContext.IsNull() || myShape.IsNull() )
387 colorShapeBorder( getActiveColor() );
388 myContext->Display( myShape );
391 bool HYDROGUI_Shape::isHighlighted() const
393 return myIsHighlight;
396 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
397 const bool theToDisplay,
398 const bool theIsUpdateViewer )
400 myTopoShape = theWire;
401 myDisplayMode = AIS_WireFrame;
404 updateShape( theToDisplay, theIsUpdateViewer );
407 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
408 const bool theToDisplay,
409 const bool theIsUpdateViewer )
411 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
412 TopoDS_Compound aCompound;
413 BRep_Builder aBuilder;
414 aBuilder.MakeCompound( aCompound );
416 for ( ; anExp.More(); anExp.Next() ) {
417 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
418 if ( aWire.IsNull() ) {
422 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
424 if( aMakeFace.IsDone() ) {
425 aBuilder.Add( aCompound, aMakeFace.Face() );
429 myTopoShape = aCompound;
430 myDisplayMode = AIS_Shaded;
433 updateShape( theToDisplay, theIsUpdateViewer );
436 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
437 const bool theToDisplay,
438 const bool theIsUpdateViewer )
440 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
441 aFaceBuilder.Build();
442 if( aFaceBuilder.IsDone() )
444 TopoDS_Face aFace = aFaceBuilder.Face();
445 setFace( aFace, theToDisplay, theIsUpdateViewer );
449 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
450 const bool theToDisplay,
451 const bool theIsUpdateViewer )
453 myTopoShape = theFace;
454 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
455 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
456 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
459 updateShape( theToDisplay, theIsUpdateViewer );
462 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
463 const bool theToDisplay,
464 const bool theIsUpdateViewer )
466 myTopoShape = theShape;
467 myDisplayMode = AIS_Shaded;
470 updateShape( theToDisplay, theIsUpdateViewer );
473 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
474 const bool theToDisplay,
475 const bool theIsUpdateViewer )
477 myFillingColor = theColor;
478 updateShape( theToDisplay, theIsUpdateViewer );
481 QColor HYDROGUI_Shape::getFillingColor() const
483 return myFillingColor;
486 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
487 const bool theToDisplay,
488 const bool theIsUpdateViewer )
490 myBorderColor = theColor;
491 updateShape( theToDisplay, theIsUpdateViewer );
494 QColor HYDROGUI_Shape::getBorderColor() const
496 return myBorderColor;
499 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
501 myHighlightColor = theColor;
504 QColor HYDROGUI_Shape::getHighlightColor() const
506 return myHighlightColor;
509 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
510 const bool theToDisplay,
511 const bool theIsUpdateViewer )
513 myTextureFileName = theFileName;
514 updateShape( theToDisplay, theIsUpdateViewer );
517 QString HYDROGUI_Shape::getTextureFileName() const
519 return myTextureFileName;
522 void HYDROGUI_Shape::buildShape()
524 // Erase previously created shape
527 if ( myTopoShape.IsNull() ) {
532 QString aTextureFileName = getTextureFileName();
533 bool anIsTexture = !aTextureFileName.isEmpty();
537 myShape = new AIS_TexturedShape( myTopoShape );
541 myShape = new AIS_Shape( myTopoShape );
544 if ( !myObject.IsNull() )
545 myShape->SetOwner( myObject );
547 myShape->SetTransparency( 0 );
548 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
552 Handle(AIS_TexturedShape) aTexturedShape =
553 Handle(AIS_TexturedShape)::DownCast( myShape );
555 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
556 aTexturedShape->SetTextureMapOn();
557 // Just use the texture image as is
558 aTexturedShape->DisableTextureModulate();
559 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
562 // Init default params for shape
563 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
564 if ( !anAttributes.IsNull() )
566 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
567 if ( !anIsoAspect.IsNull() ) {
568 anIsoAspect->SetNumber( 0 );
569 anAttributes->SetUIsoAspect( anIsoAspect );
572 anIsoAspect = anAttributes->VIsoAspect();
573 if ( !anIsoAspect.IsNull() ) {
574 anIsoAspect->SetNumber( 0 );
575 anAttributes->SetVIsoAspect( anIsoAspect );
578 if ( myDisplayMode == AIS_Shaded )
580 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
581 if ( !aShadingAspect.IsNull() )
583 Graphic3d_MaterialAspect aMatAspect;
584 aMatAspect.SetAmbient( 1 );
585 aMatAspect.SetDiffuse( 0 );
587 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
588 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
591 else if ( myDisplayMode == AIS_WireFrame )
593 anAttributes->SetWireDraw( true );
598 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
599 const bool theIsUpdateViewer )
601 if ( myShape.IsNull() )
604 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
605 if ( !anAttributes.IsNull() )
607 if ( myDisplayMode == AIS_Shaded )
609 // Coloring face filling
610 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
611 if ( !aShadingAspect.IsNull() )
613 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
614 getQuantityColorVal( myFillingColor.green() ),
615 getQuantityColorVal( myFillingColor.blue() ),
618 aShadingAspect->SetColor( aFillingColor );
619 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
622 else if ( myDisplayMode == AIS_WireFrame )
627 colorShapeBorder( getActiveColor() );
630 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
633 myContext->Display( myShape, theIsUpdateViewer );
636 QColor HYDROGUI_Shape::getActiveColor() const
638 return isHighlighted() ? myHighlightColor : myBorderColor;
641 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
643 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
646 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
648 if ( myShape.IsNull() )
651 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
652 if ( anAttributes.IsNull() )
655 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
656 getQuantityColorVal( theColor.green() ),
657 getQuantityColorVal( theColor.blue() ),
659 if ( myDisplayMode == AIS_Shaded )
661 if ( theColor.alpha() == 0 )
663 anAttributes->SetFaceBoundaryDraw( false );
667 anAttributes->SetFaceBoundaryDraw( true );
669 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
670 if ( !aBoundaryAspect.IsNull() )
671 aBoundaryAspect->SetColor( aBorderColor );
674 else if ( myDisplayMode == AIS_WireFrame )
676 myShape->SetColor( aBorderColor );
680 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
683 if( !theImageObj.IsNull() )
685 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
687 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
688 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
690 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
691 anEntry.replace( ':', '_' );
693 QString anExtension = "bmp";
695 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
700 void HYDROGUI_Shape::removeTextureFile() const
702 QFile aFile( getTextureFileName() );
707 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
709 double aSizeLimit = (double)theSizeLimit;
710 double aWidth = (double)theImage.width();
711 double aHeight = (double)theImage.height();
712 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
714 if( aWidth > aHeight )
716 aHeight /= ( aWidth / aSizeLimit );
721 aWidth /= ( aHeight / aSizeLimit );
722 aHeight = aSizeLimit;
725 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );