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 TopoDS_Wire aProfileWire;
296 if ( aProfile->IsValid() ) {
297 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
299 if ( !aProfileShape.IsNull() &&
300 aProfileShape.ShapeType() == TopAbs_WIRE ) {
301 aProfileWire = TopoDS::Wire( aProfileShape );
305 setWire( aProfileWire, false, false );
309 if ( myShape.IsNull() || !isVisible() )
312 myContext->Display( myShape, theIsUpdateViewer );
315 void HYDROGUI_Shape::setVisible( const bool theState,
316 const bool theIsUpdateViewer )
318 if ( myIsVisible == theState )
321 myIsVisible = theState;
323 if ( myShape.IsNull() )
327 myContext->Display( myShape, theIsUpdateViewer );
329 myContext->Erase( myShape, theIsUpdateViewer );
332 void HYDROGUI_Shape::highlight( bool theIsHighlight )
334 if ( myIsHighlight == theIsHighlight )
337 myIsHighlight = theIsHighlight;
339 if ( myContext.IsNull() || myShape.IsNull() )
342 colorShapeBorder( getActiveColor() );
343 myContext->Display( myShape );
346 bool HYDROGUI_Shape::isHighlighted() const
348 return myIsHighlight;
351 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
352 const bool theToDisplay,
353 const bool theIsUpdateViewer )
355 myTopoShape = theWire;
356 myDisplayMode = AIS_WireFrame;
359 updateShape( theToDisplay, theIsUpdateViewer );
362 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
363 const bool theToDisplay,
364 const bool theIsUpdateViewer )
366 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
367 TopoDS_Compound aCompound;
368 BRep_Builder aBuilder;
369 aBuilder.MakeCompound( aCompound );
371 for ( ; anExp.More(); anExp.Next() ) {
372 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
373 if ( aWire.IsNull() ) {
377 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
379 if( aMakeFace.IsDone() ) {
380 aBuilder.Add( aCompound, aMakeFace.Face() );
384 myTopoShape = aCompound;
385 myDisplayMode = AIS_Shaded;
388 updateShape( theToDisplay, theIsUpdateViewer );
391 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
392 const bool theToDisplay,
393 const bool theIsUpdateViewer )
395 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
396 aFaceBuilder.Build();
397 if( aFaceBuilder.IsDone() )
399 TopoDS_Face aFace = aFaceBuilder.Face();
400 setFace( aFace, theToDisplay, theIsUpdateViewer );
404 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
405 const bool theToDisplay,
406 const bool theIsUpdateViewer )
408 myTopoShape = theFace;
409 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
410 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
411 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
414 updateShape( theToDisplay, theIsUpdateViewer );
417 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
418 const bool theToDisplay,
419 const bool theIsUpdateViewer )
421 myTopoShape = theShape;
422 myDisplayMode = AIS_Shaded;
425 updateShape( theToDisplay, theIsUpdateViewer );
428 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
429 const bool theToDisplay,
430 const bool theIsUpdateViewer )
432 myFillingColor = theColor;
433 updateShape( theToDisplay, theIsUpdateViewer );
436 QColor HYDROGUI_Shape::getFillingColor() const
438 return myFillingColor;
441 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
442 const bool theToDisplay,
443 const bool theIsUpdateViewer )
445 myBorderColor = theColor;
446 updateShape( theToDisplay, theIsUpdateViewer );
449 QColor HYDROGUI_Shape::getBorderColor() const
451 return myBorderColor;
454 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
456 myHighlightColor = theColor;
459 QColor HYDROGUI_Shape::getHighlightColor() const
461 return myHighlightColor;
464 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
465 const bool theToDisplay,
466 const bool theIsUpdateViewer )
468 myTextureFileName = theFileName;
469 updateShape( theToDisplay, theIsUpdateViewer );
472 QString HYDROGUI_Shape::getTextureFileName() const
474 return myTextureFileName;
477 void HYDROGUI_Shape::buildShape()
479 // Erase previously created shape
482 if ( myTopoShape.IsNull() ) {
487 QString aTextureFileName = getTextureFileName();
488 bool anIsTexture = !aTextureFileName.isEmpty();
492 myShape = new AIS_TexturedShape( myTopoShape );
496 myShape = new AIS_Shape( myTopoShape );
499 if ( !myObject.IsNull() )
500 myShape->SetOwner( myObject );
502 myShape->SetTransparency( 0 );
503 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
507 Handle(AIS_TexturedShape) aTexturedShape =
508 Handle(AIS_TexturedShape)::DownCast( myShape );
510 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
511 aTexturedShape->SetTextureMapOn();
512 // Just use the texture image as is
513 aTexturedShape->DisableTextureModulate();
514 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
517 // Init default params for shape
518 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
519 if ( !anAttributes.IsNull() )
521 if ( myDisplayMode == AIS_Shaded )
523 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
524 if ( !anIsoAspect.IsNull() )
525 anIsoAspect->SetNumber( 0 );
527 anIsoAspect = anAttributes->VIsoAspect();
528 if ( !anIsoAspect.IsNull() )
529 anIsoAspect->SetNumber( 0 );
531 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
532 if ( !aShadingAspect.IsNull() )
534 Graphic3d_MaterialAspect aMatAspect;
535 aMatAspect.SetAmbient( 1 );
536 aMatAspect.SetDiffuse( 0 );
538 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
539 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
542 else if ( myDisplayMode == AIS_WireFrame )
544 anAttributes->SetWireDraw( true );
549 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
550 const bool theIsUpdateViewer )
552 if ( myShape.IsNull() )
555 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
556 if ( !anAttributes.IsNull() )
558 if ( myDisplayMode == AIS_Shaded )
560 // Coloring face filling
561 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
562 if ( !aShadingAspect.IsNull() )
564 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
565 getQuantityColorVal( myFillingColor.green() ),
566 getQuantityColorVal( myFillingColor.blue() ),
569 aShadingAspect->SetColor( aFillingColor );
570 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
573 else if ( myDisplayMode == AIS_WireFrame )
578 colorShapeBorder( getActiveColor() );
581 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
584 myContext->Display( myShape, theIsUpdateViewer );
587 QColor HYDROGUI_Shape::getActiveColor() const
589 return isHighlighted() ? myHighlightColor : myBorderColor;
592 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
594 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
597 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
599 if ( myShape.IsNull() )
602 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
603 if ( anAttributes.IsNull() )
606 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
607 getQuantityColorVal( theColor.green() ),
608 getQuantityColorVal( theColor.blue() ),
610 if ( myDisplayMode == AIS_Shaded )
612 if ( theColor.alpha() == 0 )
614 anAttributes->SetFaceBoundaryDraw( false );
618 anAttributes->SetFaceBoundaryDraw( true );
620 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
621 if ( !aBoundaryAspect.IsNull() )
622 aBoundaryAspect->SetColor( aBorderColor );
625 else if ( myDisplayMode == AIS_WireFrame )
627 myShape->SetColor( aBorderColor );
631 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
634 if( !theImageObj.IsNull() )
636 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
638 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
639 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
641 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
642 anEntry.replace( ':', '_' );
644 QString anExtension = "bmp";
646 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
651 void HYDROGUI_Shape::removeTextureFile() const
653 QFile aFile( getTextureFileName() );
658 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
660 double aSizeLimit = (double)theSizeLimit;
661 double aWidth = (double)theImage.width();
662 double aHeight = (double)theImage.height();
663 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
665 if( aWidth > aHeight )
667 aHeight /= ( aWidth / aSizeLimit );
672 aWidth /= ( aHeight / aSizeLimit );
673 aHeight = aSizeLimit;
676 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );