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>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Face.hxx>
52 #include <TopExp_Explorer.hxx>
54 #include <BRep_Builder.hxx>
56 #include <Precision.hxx>
58 #include <Prs3d_ShadingAspect.hxx>
59 #include <Prs3d_LineAspect.hxx>
60 #include <Prs3d_IsoAspect.hxx>
62 #include <SUIT_MessageBox.h>
67 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
68 const Handle(HYDROData_Entity)& theObject )
69 : myContext( theContext ),
70 myObject( theObject ),
71 myIsHighlight( false ),
72 myFillingColor( Qt::transparent ),
73 myBorderColor( Qt::black ),
74 myHighlightColor( Qt::white ),
75 myIsToUpdate( false ),
77 myDisplayMode( AIS_WireFrame )
81 HYDROGUI_Shape::~HYDROGUI_Shape()
85 if ( !myShape.IsNull() )
91 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
93 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
96 myContext->Display( myShape, theIsUpdateViewer );
99 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
101 if ( myContext.IsNull() || myShape.IsNull() )
104 myContext->Erase( myShape, theIsUpdateViewer );
107 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
109 setIsToUpdate( false );
111 if ( myContext.IsNull() )
114 // Try to retrieve information from object
115 if ( !myObject.IsNull() )
117 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
119 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
121 Handle(HYDROData_ImmersibleZone) aZoneObj =
122 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
124 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
125 if ( !aZoneShape.IsNull() ) {
126 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
127 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
128 setFace( aZoneFace, false, false );
130 myTopoShape = aZoneShape;
131 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
134 updateShape( false, false );
138 QColor aFillingColor = aZoneObj->GetFillingColor();
139 QColor aBorderColor = aZoneObj->GetBorderColor();
141 setFillingColor( aFillingColor, false, false );
142 setBorderColor( aBorderColor, false, false );
144 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
146 Handle(HYDROData_PolylineXY) aPolyline =
147 Handle(HYDROData_PolylineXY)::DownCast( myObject );
149 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
151 if ( !aPolylineShape.IsNull() ) {
152 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
153 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
154 setWire( aPolylineWire, false, false );
156 myTopoShape = aPolylineShape;
157 myDisplayMode = AIS_WireFrame;
160 updateShape( false, false );
164 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
166 Handle(HYDROData_Zone) aZone =
167 Handle(HYDROData_Zone)::DownCast( myObject );
169 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
171 // Generate the filling color for zone
172 QStringList aGeomObjectsNames;
174 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
175 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
176 for ( ; anIter.More(); anIter.Next() )
178 Handle(HYDROData_Object) aRefbject =
179 Handle(HYDROData_Object)::DownCast( anIter.Value() );
180 if ( aRefbject.IsNull() )
183 QString aRefObjectName = aRefbject->GetName();
184 if ( aRefObjectName.isEmpty() )
187 aGeomObjectsNames.append( aRefObjectName );
190 setFace( aZoneFace, false, false );
191 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
193 // Red color for a zone with bathymetry conflict
194 setFillingColor( Qt::red );
198 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
201 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
203 Handle(HYDROData_Image) anImageObj =
204 Handle(HYDROData_Image)::DownCast( myObject );
208 QString aTextureFileName = generateTextureFileName( anImageObj );
210 QImage anImage = anImageObj->Image();
211 QString aFilePath = anImageObj->GetFilePath();
212 QTransform aTrsf = anImageObj->Trsf();
214 int aWidth = anImage.width();
215 int aHeight = anImage.height();
217 QTransform anInversion = QTransform::fromScale( -1, -1 );
218 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
220 // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
221 int aTrsfWidth = anImage.width();
222 int aDelta = aTrsfWidth % 4;
225 aTrsfWidth += ( 4 - aDelta );
227 anImage = anImage.scaledToWidth( aTrsfWidth );
229 // temporary optimization, to reduce the saved image size (and the texture quality)
230 QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
232 bool isSaved = anImageToSave.save( aTextureFileName );
234 QString aTitle = QObject::tr( "FILE_ERROR" );
235 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
236 SUIT_MessageBox::warning( 0, aTitle, aMessage );
239 QPointF aPoint1( 0, 0 ); // 1: top left
240 QPointF aPoint2( aWidth, 0 ); // 2: top right
241 QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
242 QPointF aPoint4( 0, aHeight ); // 4: bottom left
244 aPoint1 = aTrsf.map( aPoint1 );
245 aPoint2 = aTrsf.map( aPoint2 );
246 aPoint3 = aTrsf.map( aPoint3 );
247 aPoint4 = aTrsf.map( aPoint4 );
249 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
250 QRectF aRect = aPolygon.boundingRect();
252 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
253 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
254 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
255 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
257 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
258 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
259 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
260 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
262 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
263 aWire.Closed( true );
265 setTextureFileName( aTextureFileName, false, false );
266 setFace( aWire, false, false );
268 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
270 Handle(HYDROData_Obstacle) anObstacle =
271 Handle(HYDROData_Obstacle)::DownCast( myObject );
273 //TODO BEGIN of the block of code to be reimplemented
274 //TODO GetTopShape() to be used in future
275 myTopoShape = anObstacle->GetShape3D();
276 myDisplayMode = AIS_Shaded;
278 QColor aFillingColor = anObstacle->GetFillingColor();
279 QColor aBorderColor = anObstacle->GetBorderColor();
281 setFillingColor( aFillingColor, false, false );
282 setBorderColor( aBorderColor, false, false );
285 updateShape( false, false );
286 //TODO END of the block of code to be reimplemented
290 if ( myShape.IsNull() || !isVisible() )
293 myContext->Display( myShape, theIsUpdateViewer );
296 void HYDROGUI_Shape::setVisible( const bool theState,
297 const bool theIsUpdateViewer )
299 if ( myIsVisible == theState )
302 myIsVisible = theState;
304 if ( myShape.IsNull() )
308 myContext->Display( myShape, theIsUpdateViewer );
310 myContext->Erase( myShape, theIsUpdateViewer );
313 void HYDROGUI_Shape::highlight( bool theIsHighlight )
315 if ( myIsHighlight == theIsHighlight )
318 myIsHighlight = theIsHighlight;
320 if ( myContext.IsNull() || myShape.IsNull() )
323 colorShapeBorder( getActiveColor() );
324 myContext->Display( myShape );
327 bool HYDROGUI_Shape::isHighlighted() const
329 return myIsHighlight;
332 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
333 const bool theToDisplay,
334 const bool theIsUpdateViewer )
336 myTopoShape = theWire;
337 myDisplayMode = AIS_WireFrame;
340 updateShape( theToDisplay, theIsUpdateViewer );
343 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
344 const bool theToDisplay,
345 const bool theIsUpdateViewer )
347 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
348 TopoDS_Compound aCompound;
349 BRep_Builder aBuilder;
350 aBuilder.MakeCompound( aCompound );
352 for ( ; anExp.More(); anExp.Next() ) {
353 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
354 if ( aWire.IsNull() ) {
358 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
360 if( aMakeFace.IsDone() ) {
361 aBuilder.Add( aCompound, aMakeFace.Face() );
365 myTopoShape = aCompound;
366 myDisplayMode = AIS_Shaded;
369 updateShape( theToDisplay, theIsUpdateViewer );
372 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
373 const bool theToDisplay,
374 const bool theIsUpdateViewer )
376 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
377 aFaceBuilder.Build();
378 if( aFaceBuilder.IsDone() )
380 TopoDS_Face aFace = aFaceBuilder.Face();
381 setFace( aFace, theToDisplay, theIsUpdateViewer );
385 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
386 const bool theToDisplay,
387 const bool theIsUpdateViewer )
389 myTopoShape = theFace;
390 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
391 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
392 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
395 updateShape( theToDisplay, theIsUpdateViewer );
398 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
399 const bool theToDisplay,
400 const bool theIsUpdateViewer )
402 myTopoShape = theShape;
403 myDisplayMode = AIS_Shaded;
406 updateShape( theToDisplay, theIsUpdateViewer );
409 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
410 const bool theToDisplay,
411 const bool theIsUpdateViewer )
413 myFillingColor = theColor;
414 updateShape( theToDisplay, theIsUpdateViewer );
417 QColor HYDROGUI_Shape::getFillingColor() const
419 return myFillingColor;
422 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
423 const bool theToDisplay,
424 const bool theIsUpdateViewer )
426 myBorderColor = theColor;
427 updateShape( theToDisplay, theIsUpdateViewer );
430 QColor HYDROGUI_Shape::getBorderColor() const
432 return myBorderColor;
435 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
437 myHighlightColor = theColor;
440 QColor HYDROGUI_Shape::getHighlightColor() const
442 return myHighlightColor;
445 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
446 const bool theToDisplay,
447 const bool theIsUpdateViewer )
449 myTextureFileName = theFileName;
450 updateShape( theToDisplay, theIsUpdateViewer );
453 QString HYDROGUI_Shape::getTextureFileName() const
455 return myTextureFileName;
458 void HYDROGUI_Shape::buildShape()
460 // Erase previously created shape
463 if ( myTopoShape.IsNull() )
466 QString aTextureFileName = getTextureFileName();
467 bool anIsTexture = !aTextureFileName.isEmpty();
471 myShape = new AIS_TexturedShape( myTopoShape );
475 myShape = new AIS_Shape( myTopoShape );
478 if ( !myObject.IsNull() )
479 myShape->SetOwner( myObject );
481 myShape->SetTransparency( 0 );
482 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
486 Handle(AIS_TexturedShape) aTexturedShape =
487 Handle(AIS_TexturedShape)::DownCast( myShape );
489 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
490 aTexturedShape->SetTextureMapOn();
491 // Just use the texture image as is
492 aTexturedShape->DisableTextureModulate();
493 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
496 // Init default params for shape
497 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
498 if ( !anAttributes.IsNull() )
500 if ( myDisplayMode == AIS_Shaded )
502 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
503 if ( !anIsoAspect.IsNull() )
504 anIsoAspect->SetNumber( 0 );
506 anIsoAspect = anAttributes->VIsoAspect();
507 if ( !anIsoAspect.IsNull() )
508 anIsoAspect->SetNumber( 0 );
510 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
511 if ( !aShadingAspect.IsNull() )
513 Graphic3d_MaterialAspect aMatAspect;
514 aMatAspect.SetAmbient( 1 );
515 aMatAspect.SetDiffuse( 0 );
517 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
518 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
521 else if ( myDisplayMode == AIS_WireFrame )
523 anAttributes->SetWireDraw( true );
528 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
529 const bool theIsUpdateViewer )
531 if ( myShape.IsNull() )
534 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
535 if ( !anAttributes.IsNull() )
537 if ( myDisplayMode == AIS_Shaded )
539 // Coloring face filling
540 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
541 if ( !aShadingAspect.IsNull() )
543 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
544 getQuantityColorVal( myFillingColor.green() ),
545 getQuantityColorVal( myFillingColor.blue() ),
548 aShadingAspect->SetColor( aFillingColor );
549 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
552 else if ( myDisplayMode == AIS_WireFrame )
557 colorShapeBorder( getActiveColor() );
560 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
563 myContext->Display( myShape, theIsUpdateViewer );
566 QColor HYDROGUI_Shape::getActiveColor() const
568 return isHighlighted() ? myHighlightColor : myBorderColor;
571 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
573 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
576 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
578 if ( myShape.IsNull() )
581 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
582 if ( anAttributes.IsNull() )
585 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
586 getQuantityColorVal( theColor.green() ),
587 getQuantityColorVal( theColor.blue() ),
589 if ( myDisplayMode == AIS_Shaded )
591 if ( theColor.alpha() == 0 )
593 anAttributes->SetFaceBoundaryDraw( false );
597 anAttributes->SetFaceBoundaryDraw( true );
599 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
600 if ( !aBoundaryAspect.IsNull() )
601 aBoundaryAspect->SetColor( aBorderColor );
604 else if ( myDisplayMode == AIS_WireFrame )
606 myShape->SetColor( aBorderColor );
610 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
613 if( !theImageObj.IsNull() )
615 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
617 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
618 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
620 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
621 anEntry.replace( ':', '_' );
623 QString anExtension = "bmp";
625 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
630 void HYDROGUI_Shape::removeTextureFile() const
632 QFile aFile( getTextureFileName() );
637 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
639 double aSizeLimit = (double)theSizeLimit;
640 double aWidth = (double)theImage.width();
641 double aHeight = (double)theImage.height();
642 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
644 if( aWidth > aHeight )
646 aHeight /= ( aWidth / aSizeLimit );
651 aWidth /= ( aHeight / aSizeLimit );
652 aHeight = aSizeLimit;
655 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );