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>
47 #include <HYDROData_Profile.h>
50 #include <TopoDS_Wire.hxx>
51 #include <TopoDS_Face.hxx>
53 #include <TopExp_Explorer.hxx>
55 #include <BRep_Builder.hxx>
57 #include <Precision.hxx>
59 #include <Prs3d_ShadingAspect.hxx>
60 #include <Prs3d_LineAspect.hxx>
61 #include <Prs3d_IsoAspect.hxx>
63 #include <SUIT_MessageBox.h>
68 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
69 const Handle(HYDROData_Entity)& theObject )
70 : myContext( theContext ),
71 myObject( theObject ),
72 myIsHighlight( false ),
73 myFillingColor( Qt::transparent ),
74 myBorderColor( Qt::black ),
75 myHighlightColor( Qt::white ),
76 myIsToUpdate( false ),
78 myDisplayMode( AIS_WireFrame )
82 HYDROGUI_Shape::~HYDROGUI_Shape()
86 if ( !myShape.IsNull() )
92 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
94 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
97 myContext->Display( myShape, theIsUpdateViewer );
100 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
102 if ( myContext.IsNull() || myShape.IsNull() )
105 myContext->Erase( myShape, theIsUpdateViewer );
108 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
110 setIsToUpdate( false );
112 if ( myContext.IsNull() )
115 // Try to retrieve information from object
116 if ( !myObject.IsNull() )
118 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
120 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
122 Handle(HYDROData_ImmersibleZone) aZoneObj =
123 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
125 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
126 if ( !aZoneShape.IsNull() ) {
127 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
128 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
129 setFace( aZoneFace, false, false );
131 myTopoShape = aZoneShape;
132 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
135 updateShape( false, false );
139 QColor aFillingColor = aZoneObj->GetFillingColor();
140 QColor aBorderColor = aZoneObj->GetBorderColor();
142 setFillingColor( aFillingColor, false, false );
143 setBorderColor( aBorderColor, false, false );
145 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
147 Handle(HYDROData_PolylineXY) aPolyline =
148 Handle(HYDROData_PolylineXY)::DownCast( myObject );
150 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
152 if ( !aPolylineShape.IsNull() ) {
153 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
154 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
155 setWire( aPolylineWire, false, false );
157 myTopoShape = aPolylineShape;
158 myDisplayMode = AIS_WireFrame;
161 updateShape( false, false );
165 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
167 Handle(HYDROData_Zone) aZone =
168 Handle(HYDROData_Zone)::DownCast( myObject );
170 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
172 // Generate the filling color for zone
173 QStringList aGeomObjectsNames;
175 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
176 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
177 for ( ; anIter.More(); anIter.Next() )
179 Handle(HYDROData_Object) aRefbject =
180 Handle(HYDROData_Object)::DownCast( anIter.Value() );
181 if ( aRefbject.IsNull() )
184 QString aRefObjectName = aRefbject->GetName();
185 if ( aRefObjectName.isEmpty() )
188 aGeomObjectsNames.append( aRefObjectName );
191 setFace( aZoneFace, false, false );
192 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
194 // Red color for a zone with bathymetry conflict
195 setFillingColor( Qt::red );
199 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
202 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
204 Handle(HYDROData_Image) anImageObj =
205 Handle(HYDROData_Image)::DownCast( myObject );
209 QString aTextureFileName = generateTextureFileName( anImageObj );
211 QImage anImage = anImageObj->Image();
212 QString aFilePath = anImageObj->GetFilePath();
213 QTransform aTrsf = anImageObj->Trsf();
215 int aWidth = anImage.width();
216 int aHeight = anImage.height();
218 QTransform anInversion = QTransform::fromScale( -1, -1 );
219 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
221 // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
222 int aTrsfWidth = anImage.width();
223 int aDelta = aTrsfWidth % 4;
226 aTrsfWidth += ( 4 - aDelta );
228 anImage = anImage.scaledToWidth( aTrsfWidth );
230 // temporary optimization, to reduce the saved image size (and the texture quality)
231 QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
233 bool isSaved = anImageToSave.save( aTextureFileName );
235 QString aTitle = QObject::tr( "FILE_ERROR" );
236 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
237 SUIT_MessageBox::warning( 0, aTitle, aMessage );
240 QPointF aPoint1( 0, 0 ); // 1: top left
241 QPointF aPoint2( aWidth, 0 ); // 2: top right
242 QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
243 QPointF aPoint4( 0, aHeight ); // 4: bottom left
245 aPoint1 = aTrsf.map( aPoint1 );
246 aPoint2 = aTrsf.map( aPoint2 );
247 aPoint3 = aTrsf.map( aPoint3 );
248 aPoint4 = aTrsf.map( aPoint4 );
250 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
251 QRectF aRect = aPolygon.boundingRect();
253 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
254 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
255 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
256 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
258 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
259 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
260 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
261 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
263 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
264 aWire.Closed( true );
266 setTextureFileName( aTextureFileName, false, false );
267 setFace( aWire, false, false );
269 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
271 Handle(HYDROData_Obstacle) anObstacle =
272 Handle(HYDROData_Obstacle)::DownCast( myObject );
274 //TODO BEGIN of the block of code to be reimplemented
275 //TODO GetTopShape() to be used in future
276 myTopoShape = anObstacle->GetShape3D();
277 myDisplayMode = AIS_Shaded;
279 QColor aFillingColor = anObstacle->GetFillingColor();
280 QColor aBorderColor = anObstacle->GetBorderColor();
282 setFillingColor( aFillingColor, false, false );
283 setBorderColor( aBorderColor, false, false );
286 updateShape( false, false );
287 //TODO END of the block of code to be reimplemented
289 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
291 Handle(HYDROData_Profile) aProfile =
292 Handle(HYDROData_Profile)::DownCast( myObject );
294 if ( aProfile->IsValid() ) {
295 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
297 if ( !aProfileShape.IsNull() ) {
298 if ( aProfileShape.ShapeType() == TopAbs_WIRE ) {
299 TopoDS_Wire aProfileWire = TopoDS::Wire( aProfileShape );
300 setWire( aProfileWire, false, false );
302 myTopoShape = aProfileShape;
303 myDisplayMode = AIS_WireFrame;
306 updateShape( false, false );
313 if ( myShape.IsNull() || !isVisible() )
316 myContext->Display( myShape, theIsUpdateViewer );
319 void HYDROGUI_Shape::setVisible( const bool theState,
320 const bool theIsUpdateViewer )
322 if ( myIsVisible == theState )
325 myIsVisible = theState;
327 if ( myShape.IsNull() )
331 myContext->Display( myShape, theIsUpdateViewer );
333 myContext->Erase( myShape, theIsUpdateViewer );
336 void HYDROGUI_Shape::highlight( bool theIsHighlight )
338 if ( myIsHighlight == theIsHighlight )
341 myIsHighlight = theIsHighlight;
343 if ( myContext.IsNull() || myShape.IsNull() )
346 colorShapeBorder( getActiveColor() );
347 myContext->Display( myShape );
350 bool HYDROGUI_Shape::isHighlighted() const
352 return myIsHighlight;
355 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
356 const bool theToDisplay,
357 const bool theIsUpdateViewer )
359 myTopoShape = theWire;
360 myDisplayMode = AIS_WireFrame;
363 updateShape( theToDisplay, theIsUpdateViewer );
366 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
367 const bool theToDisplay,
368 const bool theIsUpdateViewer )
370 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
371 TopoDS_Compound aCompound;
372 BRep_Builder aBuilder;
373 aBuilder.MakeCompound( aCompound );
375 for ( ; anExp.More(); anExp.Next() ) {
376 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
377 if ( aWire.IsNull() ) {
381 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
383 if( aMakeFace.IsDone() ) {
384 aBuilder.Add( aCompound, aMakeFace.Face() );
388 myTopoShape = aCompound;
389 myDisplayMode = AIS_Shaded;
392 updateShape( theToDisplay, theIsUpdateViewer );
395 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
396 const bool theToDisplay,
397 const bool theIsUpdateViewer )
399 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
400 aFaceBuilder.Build();
401 if( aFaceBuilder.IsDone() )
403 TopoDS_Face aFace = aFaceBuilder.Face();
404 setFace( aFace, theToDisplay, theIsUpdateViewer );
408 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
409 const bool theToDisplay,
410 const bool theIsUpdateViewer )
412 myTopoShape = theFace;
413 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
414 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
415 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
418 updateShape( theToDisplay, theIsUpdateViewer );
421 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
422 const bool theToDisplay,
423 const bool theIsUpdateViewer )
425 myTopoShape = theShape;
426 myDisplayMode = AIS_Shaded;
429 updateShape( theToDisplay, theIsUpdateViewer );
432 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
433 const bool theToDisplay,
434 const bool theIsUpdateViewer )
436 myFillingColor = theColor;
437 updateShape( theToDisplay, theIsUpdateViewer );
440 QColor HYDROGUI_Shape::getFillingColor() const
442 return myFillingColor;
445 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
446 const bool theToDisplay,
447 const bool theIsUpdateViewer )
449 myBorderColor = theColor;
450 updateShape( theToDisplay, theIsUpdateViewer );
453 QColor HYDROGUI_Shape::getBorderColor() const
455 return myBorderColor;
458 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
460 myHighlightColor = theColor;
463 QColor HYDROGUI_Shape::getHighlightColor() const
465 return myHighlightColor;
468 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
469 const bool theToDisplay,
470 const bool theIsUpdateViewer )
472 myTextureFileName = theFileName;
473 updateShape( theToDisplay, theIsUpdateViewer );
476 QString HYDROGUI_Shape::getTextureFileName() const
478 return myTextureFileName;
481 void HYDROGUI_Shape::buildShape()
483 // Erase previously created shape
486 if ( myTopoShape.IsNull() )
489 QString aTextureFileName = getTextureFileName();
490 bool anIsTexture = !aTextureFileName.isEmpty();
494 myShape = new AIS_TexturedShape( myTopoShape );
498 myShape = new AIS_Shape( myTopoShape );
501 if ( !myObject.IsNull() )
502 myShape->SetOwner( myObject );
504 myShape->SetTransparency( 0 );
505 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
509 Handle(AIS_TexturedShape) aTexturedShape =
510 Handle(AIS_TexturedShape)::DownCast( myShape );
512 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
513 aTexturedShape->SetTextureMapOn();
514 // Just use the texture image as is
515 aTexturedShape->DisableTextureModulate();
516 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
519 // Init default params for shape
520 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
521 if ( !anAttributes.IsNull() )
523 if ( myDisplayMode == AIS_Shaded )
525 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
526 if ( !anIsoAspect.IsNull() )
527 anIsoAspect->SetNumber( 0 );
529 anIsoAspect = anAttributes->VIsoAspect();
530 if ( !anIsoAspect.IsNull() )
531 anIsoAspect->SetNumber( 0 );
533 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
534 if ( !aShadingAspect.IsNull() )
536 Graphic3d_MaterialAspect aMatAspect;
537 aMatAspect.SetAmbient( 1 );
538 aMatAspect.SetDiffuse( 0 );
540 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
541 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
544 else if ( myDisplayMode == AIS_WireFrame )
546 anAttributes->SetWireDraw( true );
551 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
552 const bool theIsUpdateViewer )
554 if ( myShape.IsNull() )
557 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
558 if ( !anAttributes.IsNull() )
560 if ( myDisplayMode == AIS_Shaded )
562 // Coloring face filling
563 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
564 if ( !aShadingAspect.IsNull() )
566 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
567 getQuantityColorVal( myFillingColor.green() ),
568 getQuantityColorVal( myFillingColor.blue() ),
571 aShadingAspect->SetColor( aFillingColor );
572 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
575 else if ( myDisplayMode == AIS_WireFrame )
580 colorShapeBorder( getActiveColor() );
583 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
586 myContext->Display( myShape, theIsUpdateViewer );
589 QColor HYDROGUI_Shape::getActiveColor() const
591 return isHighlighted() ? myHighlightColor : myBorderColor;
594 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
596 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
599 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
601 if ( myShape.IsNull() )
604 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
605 if ( anAttributes.IsNull() )
608 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
609 getQuantityColorVal( theColor.green() ),
610 getQuantityColorVal( theColor.blue() ),
612 if ( myDisplayMode == AIS_Shaded )
614 if ( theColor.alpha() == 0 )
616 anAttributes->SetFaceBoundaryDraw( false );
620 anAttributes->SetFaceBoundaryDraw( true );
622 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
623 if ( !aBoundaryAspect.IsNull() )
624 aBoundaryAspect->SetColor( aBorderColor );
627 else if ( myDisplayMode == AIS_WireFrame )
629 myShape->SetColor( aBorderColor );
633 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
636 if( !theImageObj.IsNull() )
638 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
640 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
641 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
643 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
644 anEntry.replace( ':', '_' );
646 QString anExtension = "bmp";
648 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
653 void HYDROGUI_Shape::removeTextureFile() const
655 QFile aFile( getTextureFileName() );
660 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
662 double aSizeLimit = (double)theSizeLimit;
663 double aWidth = (double)theImage.width();
664 double aHeight = (double)theImage.height();
665 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
667 if( aWidth > aHeight )
669 aHeight /= ( aWidth / aSizeLimit );
674 aWidth /= ( aHeight / aSizeLimit );
675 aHeight = aSizeLimit;
678 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );