Salome HOME
refs #497: redesign of HYDRO main menu.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImagePrs.cxx
1 #include "HYDROGUI_ImagePrs.h"
2
3 #include <Prs3d_Root.hxx>
4
5 #include <SelectMgr_Selection.hxx>
6 #include <SelectMgr_EntityOwner.hxx>
7 #include <Select3D_SensitiveFace.hxx>
8
9 #include <Graphic3d_Group.hxx>
10 #include <Graphic3d_AspectLine3d.hxx>
11 #include <Graphic3d_Texture2Dmanual.hxx>
12 #include <Graphic3d_ArrayOfPolylines.hxx>
13 #include <Graphic3d_ArrayOfTriangles.hxx>
14 #include <Graphic3d_AspectFillArea3d.hxx>
15
16 #include <TColgp_Array1OfPnt.hxx>
17
18 IMPLEMENT_STANDARD_TYPE(HYDROGUI_ImagePrs)
19     IMPLEMENT_STANDARD_SUPERTYPE_ARRAY()
20     STANDARD_TYPE(AIS_InteractiveObject),
21     STANDARD_TYPE(SelectMgr_SelectableObject),
22     STANDARD_TYPE(PrsMgr_PresentableObject),
23     STANDARD_TYPE(MMgt_TShared),
24     STANDARD_TYPE(Standard_Transient),
25     IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_END()
26     IMPLEMENT_STANDARD_TYPE_END(HYDROGUI_ImagePrs)
27
28     IMPLEMENT_DOWNCAST(HYDROGUI_ImagePrs,Standard_Transient)
29     IMPLEMENT_STANDARD_RTTI(HYDROGUI_ImagePrs)
30
31     HYDROGUI_ImagePrs::HYDROGUI_ImagePrs()
32     : AIS_InteractiveObject()
33 {
34 }
35
36 HYDROGUI_ImagePrs::HYDROGUI_ImagePrs( const Handle(Image_PixMap)& theImage, const QPolygonF& theContour )
37     : AIS_InteractiveObject(),
38     myImage( theImage ),
39     myContour( theContour )
40 {
41 }
42
43 HYDROGUI_ImagePrs::~HYDROGUI_ImagePrs()
44 {
45 }
46
47 QPolygonF HYDROGUI_ImagePrs::GetContour() const
48 {
49     return myContour;
50 }
51
52 void HYDROGUI_ImagePrs::SetContour( const QPolygonF& theRect )
53 {
54     if ( myContour != theRect )
55         SetToUpdate();
56
57     myContour = theRect;
58 }
59
60 Handle(Image_PixMap) HYDROGUI_ImagePrs::GetImage() const
61 {
62     return myImage;
63 }
64
65 void HYDROGUI_ImagePrs::SetImage( const Handle(Image_PixMap)& theImage )
66 {
67     if ( myImage != theImage )
68         SetToUpdate();
69
70     myImage = theImage;
71 }
72
73 void HYDROGUI_ImagePrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer theMode )
74 {
75     if ( myContour.isEmpty() )
76         return;
77
78     if ( theMode == 0 )
79     {
80         Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner( this );
81
82         TColgp_Array1OfPnt aPoints( 0, myContour.size() - 1 );
83         for ( int i = 0; i < myContour.size(); i++ )
84             aPoints.SetValue( aPoints.Lower() + i, convert( myContour[i] ) );
85
86         Handle(Select3D_SensitiveFace) aSensitiveFace = new Select3D_SensitiveFace( anOwner, aPoints );
87         theSelection->Add( aSensitiveFace );
88     }
89 }
90
91 void HYDROGUI_ImagePrs::Compute( const Handle(PrsMgr_PresentationManager3d)&,
92                                  const Handle(Prs3d_Presentation)& aPrs,
93                                  const Standard_Integer aMode )
94 {
95     aPrs->Clear();
96     Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
97
98     if ( aMode == 0 )
99     {
100         Handle(Graphic3d_ArrayOfPolylines) aSegments = new Graphic3d_ArrayOfPolylines( 5 );
101         aSegments->AddVertex( convert( myContour[0] ) );
102         aSegments->AddVertex( convert( myContour[1] ) );
103         aSegments->AddVertex( convert( myContour[2] ) );
104         aSegments->AddVertex( convert( myContour[3] ) );
105         aSegments->AddVertex( convert( myContour[0] ) );
106
107         aGroup->AddPrimitiveArray( aSegments );
108     }
109     else
110     {
111         Graphic3d_MaterialAspect aMat( Graphic3d_NOM_PLASTIC );
112         Handle(Graphic3d_AspectFillArea3d) aFillAspect =
113             new Graphic3d_AspectFillArea3d( Aspect_IS_SOLID, Quantity_NOC_WHITE, Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0, aMat, aMat );
114
115         Handle(Graphic3d_TextureMap) aTex = new Graphic3d_Texture2Dmanual( myImage );
116         aTex->DisableModulate();
117         aFillAspect->SetTextureMapOn();
118         aFillAspect->SetTextureMap( aTex );
119
120         aGroup->SetGroupPrimitivesAspect( aFillAspect );
121
122         Handle(Graphic3d_ArrayOfTriangles) aTriangles = new Graphic3d_ArrayOfTriangles( 6, 0, Standard_False, Standard_False, Standard_True );
123
124         aTriangles->AddVertex( convert( myContour[0] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 0 : 1 ) );
125         aTriangles->AddVertex( convert( myContour[1] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 0 : 1 ) );
126         aTriangles->AddVertex( convert( myContour[2] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 1 : 0 ) );
127
128         aTriangles->AddVertex( convert( myContour[2] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 1 : 0 ) );
129         aTriangles->AddVertex( convert( myContour[3] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 1 : 0 ) );
130         aTriangles->AddVertex( convert( myContour[0] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 0 : 1 ) );
131
132         aGroup->AddPrimitiveArray( aTriangles );
133     }
134 }
135
136 gp_Pnt HYDROGUI_ImagePrs::convert( const QPointF& thePoint ) const
137 {
138     return gp_Pnt( thePoint.x(), thePoint.y(), 0 );
139 }