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_Obstacle.h>
44 #include <HYDROData_PolylineXY.h>
45 #include <HYDROData_Profile.h>
46 #include <HYDROData_Region.h>
47 #include <HYDROData_Stream.h>
48 #include <HYDROData_Zone.h>
51 #include <TopoDS_Wire.hxx>
52 #include <TopoDS_Face.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <BRep_Builder.hxx>
58 #include <Precision.hxx>
60 #include <Prs3d_ShadingAspect.hxx>
61 #include <Prs3d_LineAspect.hxx>
62 #include <Prs3d_IsoAspect.hxx>
64 #include <SUIT_MessageBox.h>
69 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
70 const Handle(HYDROData_Entity)& theObject )
71 : myContext( theContext ),
72 myObject( theObject ),
73 myIsHighlight( false ),
74 myFillingColor( Qt::transparent ),
75 myBorderColor( Qt::black ),
76 myHighlightColor( Qt::white ),
77 myIsToUpdate( false ),
79 myDisplayMode( AIS_WireFrame )
83 HYDROGUI_Shape::~HYDROGUI_Shape()
87 if ( !myShape.IsNull() )
93 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
95 if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
98 myContext->Display( myShape, theIsUpdateViewer );
101 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
103 if ( myContext.IsNull() || myShape.IsNull() )
106 myContext->Erase( myShape, theIsUpdateViewer );
109 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
111 setIsToUpdate( false );
113 if ( myContext.IsNull() )
116 // Try to retrieve information from object
117 if ( !myObject.IsNull() )
119 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
121 if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
123 Handle(HYDROData_ImmersibleZone) aZoneObj =
124 Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
126 TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
127 if ( !aZoneShape.IsNull() ) {
128 if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
129 TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
130 setFace( aZoneFace, false, false );
132 myTopoShape = aZoneShape;
133 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
136 updateShape( false, false );
140 QColor aFillingColor = aZoneObj->GetFillingColor();
141 QColor aBorderColor = aZoneObj->GetBorderColor();
143 setFillingColor( aFillingColor, false, false );
144 setBorderColor( aBorderColor, false, false );
146 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
148 Handle(HYDROData_PolylineXY) aPolyline =
149 Handle(HYDROData_PolylineXY)::DownCast( myObject );
151 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
153 if ( !aPolylineShape.IsNull() ) {
154 if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
155 TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
156 setWire( aPolylineWire, false, false );
158 myTopoShape = aPolylineShape;
159 myDisplayMode = AIS_WireFrame;
162 updateShape( false, false );
166 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
168 Handle(HYDROData_Zone) aZone =
169 Handle(HYDROData_Zone)::DownCast( myObject );
171 TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
173 setFace( aZoneFace, false, false );
174 if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
176 // Red color for a zone with bathymetry conflict
177 setFillingColor( Qt::red );
181 // Generate the filling color for zone
182 QStringList aGeomObjectsNames;
184 HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
185 HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
186 for ( ; anIter.More(); anIter.Next() )
188 Handle(HYDROData_Object) aRefbject =
189 Handle(HYDROData_Object)::DownCast( anIter.Value() );
190 if ( aRefbject.IsNull() )
193 QString aRefObjectName = aRefbject->GetName();
194 if ( aRefObjectName.isEmpty() )
197 aGeomObjectsNames.append( aRefObjectName );
200 setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
203 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
205 Handle(HYDROData_Image) anImageObj =
206 Handle(HYDROData_Image)::DownCast( myObject );
210 QString aTextureFileName = generateTextureFileName( anImageObj );
212 QImage anImage = anImageObj->Image();
213 QString aFilePath = anImageObj->GetFilePath();
214 QTransform aTrsf = anImageObj->Trsf();
216 int aWidth = anImage.width();
217 int aHeight = anImage.height();
219 QTransform anInversion = QTransform::fromScale( -1, -1 );
220 anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
222 // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
223 int aTrsfWidth = anImage.width();
224 int aDelta = aTrsfWidth % 4;
227 aTrsfWidth += ( 4 - aDelta );
229 anImage = anImage.scaledToWidth( aTrsfWidth );
231 // temporary optimization, to reduce the saved image size (and the texture quality)
232 QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
234 bool isSaved = anImageToSave.save( aTextureFileName );
236 QString aTitle = QObject::tr( "FILE_ERROR" );
237 QString aMessage = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
238 SUIT_MessageBox::warning( 0, aTitle, aMessage );
241 QPointF aPoint1( 0, 0 ); // 1: top left
242 QPointF aPoint2( aWidth, 0 ); // 2: top right
243 QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
244 QPointF aPoint4( 0, aHeight ); // 4: bottom left
246 aPoint1 = aTrsf.map( aPoint1 );
247 aPoint2 = aTrsf.map( aPoint2 );
248 aPoint3 = aTrsf.map( aPoint3 );
249 aPoint4 = aTrsf.map( aPoint4 );
251 QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
252 QRectF aRect = aPolygon.boundingRect();
254 gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
255 gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
256 gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
257 gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
259 TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
260 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
261 TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
262 TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
264 TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
265 aWire.Closed( true );
267 setTextureFileName( aTextureFileName, false, false );
268 setFace( aWire, false, false );
270 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
272 Handle(HYDROData_Obstacle) anObstacle =
273 Handle(HYDROData_Obstacle)::DownCast( myObject );
275 //TODO BEGIN of the block of code to be reimplemented
276 //TODO GetTopShape() to be used in future
277 myTopoShape = anObstacle->GetShape3D();
278 myDisplayMode = AIS_Shaded;
280 QColor aFillingColor = anObstacle->GetFillingColor();
281 QColor aBorderColor = anObstacle->GetBorderColor();
283 setFillingColor( aFillingColor, false, false );
284 setBorderColor( aBorderColor, false, false );
287 updateShape( false, false );
288 //TODO END of the block of code to be reimplemented
290 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
292 Handle(HYDROData_Profile) aProfile =
293 Handle(HYDROData_Profile)::DownCast( myObject );
295 TopoDS_Wire aProfileWire;
297 if ( aProfile->IsValid() ) {
298 TopoDS_Shape aProfileShape = aProfile->GetShape3D();
300 if ( !aProfileShape.IsNull() &&
301 aProfileShape.ShapeType() == TopAbs_WIRE ) {
302 aProfileWire = TopoDS::Wire( aProfileShape );
306 setWire( aProfileWire, false, false );
308 else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) )
310 Handle(HYDROData_Stream) aStream =
311 Handle(HYDROData_Stream)::DownCast( myObject );
313 TopoDS_Shell aStreamShape = TopoDS::Shell( aStream->GetTopShape() );
315 setShape( aStreamShape, false, false );
319 if ( myShape.IsNull() || !isVisible() )
322 myContext->Display( myShape, theIsUpdateViewer );
325 void HYDROGUI_Shape::setVisible( const bool theState,
326 const bool theIsUpdateViewer )
328 if ( myIsVisible == theState )
331 myIsVisible = theState;
333 if ( myShape.IsNull() )
337 myContext->Display( myShape, theIsUpdateViewer );
339 myContext->Erase( myShape, theIsUpdateViewer );
342 void HYDROGUI_Shape::highlight( bool theIsHighlight )
344 if ( myIsHighlight == theIsHighlight )
347 myIsHighlight = theIsHighlight;
349 if ( myContext.IsNull() || myShape.IsNull() )
352 colorShapeBorder( getActiveColor() );
353 myContext->Display( myShape );
356 bool HYDROGUI_Shape::isHighlighted() const
358 return myIsHighlight;
361 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
362 const bool theToDisplay,
363 const bool theIsUpdateViewer )
365 myTopoShape = theWire;
366 myDisplayMode = AIS_WireFrame;
369 updateShape( theToDisplay, theIsUpdateViewer );
372 void HYDROGUI_Shape::setFaces( const TopoDS_Compound& theWires,
373 const bool theToDisplay,
374 const bool theIsUpdateViewer )
376 TopExp_Explorer anExp( theWires, TopAbs_WIRE );
377 TopoDS_Compound aCompound;
378 BRep_Builder aBuilder;
379 aBuilder.MakeCompound( aCompound );
381 for ( ; anExp.More(); anExp.Next() ) {
382 TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
383 if ( aWire.IsNull() ) {
387 BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
389 if( aMakeFace.IsDone() ) {
390 aBuilder.Add( aCompound, aMakeFace.Face() );
394 myTopoShape = aCompound;
395 myDisplayMode = AIS_Shaded;
398 updateShape( theToDisplay, theIsUpdateViewer );
401 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
402 const bool theToDisplay,
403 const bool theIsUpdateViewer )
405 BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
406 aFaceBuilder.Build();
407 if( aFaceBuilder.IsDone() )
409 TopoDS_Face aFace = aFaceBuilder.Face();
410 setFace( aFace, theToDisplay, theIsUpdateViewer );
414 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
415 const bool theToDisplay,
416 const bool theIsUpdateViewer )
418 myTopoShape = theFace;
419 myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
420 //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
421 //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
424 updateShape( theToDisplay, theIsUpdateViewer );
427 void HYDROGUI_Shape::setShape( const TopoDS_Shape& theShape,
428 const bool theToDisplay,
429 const bool theIsUpdateViewer )
431 myTopoShape = theShape;
432 myDisplayMode = AIS_Shaded;
435 updateShape( theToDisplay, theIsUpdateViewer );
438 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
439 const bool theToDisplay,
440 const bool theIsUpdateViewer )
442 myFillingColor = theColor;
443 updateShape( theToDisplay, theIsUpdateViewer );
446 QColor HYDROGUI_Shape::getFillingColor() const
448 return myFillingColor;
451 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
452 const bool theToDisplay,
453 const bool theIsUpdateViewer )
455 myBorderColor = theColor;
456 updateShape( theToDisplay, theIsUpdateViewer );
459 QColor HYDROGUI_Shape::getBorderColor() const
461 return myBorderColor;
464 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
466 myHighlightColor = theColor;
469 QColor HYDROGUI_Shape::getHighlightColor() const
471 return myHighlightColor;
474 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
475 const bool theToDisplay,
476 const bool theIsUpdateViewer )
478 myTextureFileName = theFileName;
479 updateShape( theToDisplay, theIsUpdateViewer );
482 QString HYDROGUI_Shape::getTextureFileName() const
484 return myTextureFileName;
487 void HYDROGUI_Shape::buildShape()
489 // Erase previously created shape
492 if ( myTopoShape.IsNull() ) {
497 QString aTextureFileName = getTextureFileName();
498 bool anIsTexture = !aTextureFileName.isEmpty();
502 myShape = new AIS_TexturedShape( myTopoShape );
506 myShape = new AIS_Shape( myTopoShape );
509 if ( !myObject.IsNull() )
510 myShape->SetOwner( myObject );
512 myShape->SetTransparency( 0 );
513 myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
517 Handle(AIS_TexturedShape) aTexturedShape =
518 Handle(AIS_TexturedShape)::DownCast( myShape );
520 aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
521 aTexturedShape->SetTextureMapOn();
522 // Just use the texture image as is
523 aTexturedShape->DisableTextureModulate();
524 aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
527 // Init default params for shape
528 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
529 if ( !anAttributes.IsNull() )
531 if ( myDisplayMode == AIS_Shaded )
533 Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
534 if ( !anIsoAspect.IsNull() )
535 anIsoAspect->SetNumber( 0 );
537 anIsoAspect = anAttributes->VIsoAspect();
538 if ( !anIsoAspect.IsNull() )
539 anIsoAspect->SetNumber( 0 );
541 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
542 if ( !aShadingAspect.IsNull() )
544 Graphic3d_MaterialAspect aMatAspect;
545 aMatAspect.SetAmbient( 1 );
546 aMatAspect.SetDiffuse( 0 );
548 aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
549 aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
552 else if ( myDisplayMode == AIS_WireFrame )
554 anAttributes->SetWireDraw( true );
559 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
560 const bool theIsUpdateViewer )
562 if ( myShape.IsNull() )
565 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
566 if ( !anAttributes.IsNull() )
568 if ( myDisplayMode == AIS_Shaded )
570 // Coloring face filling
571 Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
572 if ( !aShadingAspect.IsNull() )
574 Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ),
575 getQuantityColorVal( myFillingColor.green() ),
576 getQuantityColorVal( myFillingColor.blue() ),
579 aShadingAspect->SetColor( aFillingColor );
580 aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
583 else if ( myDisplayMode == AIS_WireFrame )
588 colorShapeBorder( getActiveColor() );
591 if ( !theToDisplay || !isVisible() || myContext.IsNull() )
594 myContext->Display( myShape, theIsUpdateViewer );
597 QColor HYDROGUI_Shape::getActiveColor() const
599 return isHighlighted() ? myHighlightColor : myBorderColor;
602 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
604 return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
607 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
609 if ( myShape.IsNull() )
612 const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
613 if ( anAttributes.IsNull() )
616 Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ),
617 getQuantityColorVal( theColor.green() ),
618 getQuantityColorVal( theColor.blue() ),
620 if ( myDisplayMode == AIS_Shaded )
622 if ( theColor.alpha() == 0 )
624 anAttributes->SetFaceBoundaryDraw( false );
628 anAttributes->SetFaceBoundaryDraw( true );
630 Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
631 if ( !aBoundaryAspect.IsNull() )
632 aBoundaryAspect->SetColor( aBorderColor );
635 else if ( myDisplayMode == AIS_WireFrame )
637 myShape->SetColor( aBorderColor );
641 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
644 if( !theImageObj.IsNull() )
646 QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
648 int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
649 QString aPrefix = QString( "image_%1" ).arg( aStudyId );
651 QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
652 anEntry.replace( ':', '_' );
654 QString anExtension = "bmp";
656 aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
661 void HYDROGUI_Shape::removeTextureFile() const
663 QFile aFile( getTextureFileName() );
668 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
670 double aSizeLimit = (double)theSizeLimit;
671 double aWidth = (double)theImage.width();
672 double aHeight = (double)theImage.height();
673 if( aWidth > aSizeLimit || aHeight > aSizeLimit )
675 if( aWidth > aHeight )
677 aHeight /= ( aWidth / aSizeLimit );
682 aWidth /= ( aHeight / aSizeLimit );
683 aHeight = aSizeLimit;
686 return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );