Salome HOME
debug of DTM object
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImagePrs.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_ImagePrs.h"
20
21 #include <Prs3d_Root.hxx>
22
23 #include <SelectMgr_Selection.hxx>
24 #include <SelectMgr_EntityOwner.hxx>
25 #include <Select3D_SensitiveFace.hxx>
26
27 #include <Graphic3d_Group.hxx>
28 #include <Graphic3d_AspectLine3d.hxx>
29 #include <Graphic3d_Texture2Dmanual.hxx>
30 #include <Graphic3d_ArrayOfPolylines.hxx>
31 #include <Graphic3d_ArrayOfTriangles.hxx>
32 #include <Graphic3d_AspectFillArea3d.hxx>
33
34 #include <TColgp_Array1OfPnt.hxx>
35
36 IMPLEMENT_STANDARD_TYPE(HYDROGUI_ImagePrs)
37     IMPLEMENT_STANDARD_SUPERTYPE_ARRAY()
38     STANDARD_TYPE(AIS_InteractiveObject),
39     STANDARD_TYPE(SelectMgr_SelectableObject),
40     STANDARD_TYPE(PrsMgr_PresentableObject),
41     STANDARD_TYPE(MMgt_TShared),
42     STANDARD_TYPE(Standard_Transient),
43     IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_END()
44     IMPLEMENT_STANDARD_TYPE_END(HYDROGUI_ImagePrs)
45
46     IMPLEMENT_DOWNCAST(HYDROGUI_ImagePrs,Standard_Transient)
47     IMPLEMENT_STANDARD_RTTI(HYDROGUI_ImagePrs)
48
49     HYDROGUI_ImagePrs::HYDROGUI_ImagePrs()
50     : AIS_InteractiveObject()
51 {
52 }
53
54 HYDROGUI_ImagePrs::HYDROGUI_ImagePrs( const Handle(Image_PixMap)& theImage, const QPolygonF& theContour )
55     : AIS_InteractiveObject(),
56     myImage( theImage ),
57     myContour( theContour )
58 {
59 }
60
61 HYDROGUI_ImagePrs::~HYDROGUI_ImagePrs()
62 {
63 }
64
65 QPolygonF HYDROGUI_ImagePrs::GetContour() const
66 {
67     return myContour;
68 }
69
70 void HYDROGUI_ImagePrs::SetContour( const QPolygonF& theRect )
71 {
72     if ( myContour != theRect )
73         SetToUpdate();
74
75     myContour = theRect;
76 }
77
78 Handle(Image_PixMap) HYDROGUI_ImagePrs::GetImage() const
79 {
80     return myImage;
81 }
82
83 void HYDROGUI_ImagePrs::SetImage( const Handle(Image_PixMap)& theImage )
84 {
85     if ( myImage != theImage )
86         SetToUpdate();
87
88     myImage = theImage;
89 }
90
91 void HYDROGUI_ImagePrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer theMode )
92 {
93     if ( myContour.isEmpty() )
94         return;
95
96     if ( theMode == 0 )
97     {
98         Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner( this );
99
100         TColgp_Array1OfPnt aPoints( 0, myContour.size() - 1 );
101         for ( int i = 0; i < myContour.size(); i++ )
102             aPoints.SetValue( aPoints.Lower() + i, convert( myContour[i] ) );
103
104         Handle(Select3D_SensitiveFace) aSensitiveFace = new Select3D_SensitiveFace( anOwner, aPoints, Select3D_TOS_INTERIOR );
105         theSelection->Add( aSensitiveFace );
106     }
107 }
108
109 void HYDROGUI_ImagePrs::Compute( const Handle(PrsMgr_PresentationManager3d)&,
110                                  const Handle(Prs3d_Presentation)& aPrs,
111                                  const Standard_Integer aMode )
112 {
113     aPrs->Clear();
114     Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
115
116     if ( aMode == 0 )
117     {
118         Handle(Graphic3d_ArrayOfPolylines) aSegments = new Graphic3d_ArrayOfPolylines( 5 );
119         aSegments->AddVertex( convert( myContour[0] ) );
120         aSegments->AddVertex( convert( myContour[1] ) );
121         aSegments->AddVertex( convert( myContour[2] ) );
122         aSegments->AddVertex( convert( myContour[3] ) );
123         aSegments->AddVertex( convert( myContour[0] ) );
124
125         aGroup->AddPrimitiveArray( aSegments );
126     }
127     else
128     {
129         Graphic3d_MaterialAspect aMat( Graphic3d_NOM_PLASTIC );
130         Handle(Graphic3d_AspectFillArea3d) aFillAspect =
131             new Graphic3d_AspectFillArea3d( Aspect_IS_SOLID, Quantity_NOC_WHITE, Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0, aMat, aMat );
132
133         Handle(Graphic3d_TextureMap) aTex = new Graphic3d_Texture2Dmanual( myImage );
134         aTex->DisableModulate();
135         aFillAspect->SetTextureMapOn();
136         aFillAspect->SetTextureMap( aTex );
137
138         aGroup->SetGroupPrimitivesAspect( aFillAspect );
139
140         Handle(Graphic3d_ArrayOfTriangles) aTriangles = new Graphic3d_ArrayOfTriangles( 6, 0, Standard_False, Standard_False, Standard_True );
141
142         aTriangles->AddVertex( convert( myContour[0] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 0 : 1 ) );
143         aTriangles->AddVertex( convert( myContour[1] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 0 : 1 ) );
144         aTriangles->AddVertex( convert( myContour[2] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 1 : 0 ) );
145
146         aTriangles->AddVertex( convert( myContour[2] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 1 : 0 ) );
147         aTriangles->AddVertex( convert( myContour[3] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 1 : 0 ) );
148         aTriangles->AddVertex( convert( myContour[0] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 0 : 1 ) );
149
150         aGroup->AddPrimitiveArray( aTriangles );
151     }
152 }
153
154 gp_Pnt HYDROGUI_ImagePrs::convert( const QPointF& thePoint ) const
155 {
156     return gp_Pnt( thePoint.x(), thePoint.y(), 0 );
157 }