Salome HOME
lots 3,8 - corrections
[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_RTTIEXT(HYDROGUI_ImagePrs, AIS_InteractiveObject)
37
38 HYDROGUI_ImagePrs::HYDROGUI_ImagePrs()
39   : AIS_InteractiveObject()
40 {
41 }
42
43 HYDROGUI_ImagePrs::HYDROGUI_ImagePrs( const Handle(Image_PixMap)& theImage, const QPolygonF& theContour )
44     : AIS_InteractiveObject(),
45     myImage( theImage ),
46     myContour( theContour )
47 {
48 }
49
50 HYDROGUI_ImagePrs::~HYDROGUI_ImagePrs()
51 {
52 }
53
54 QPolygonF HYDROGUI_ImagePrs::GetContour() const
55 {
56     return myContour;
57 }
58
59 void HYDROGUI_ImagePrs::SetContour( const QPolygonF& theRect )
60 {
61     if ( myContour != theRect )
62         SetToUpdate();
63
64     myContour = theRect;
65 }
66
67 Handle(Image_PixMap) HYDROGUI_ImagePrs::GetImage() const
68 {
69     return myImage;
70 }
71
72 void HYDROGUI_ImagePrs::SetImage( const Handle(Image_PixMap)& theImage )
73 {
74     if ( myImage != theImage )
75         SetToUpdate();
76
77     myImage = theImage;
78 }
79
80 void HYDROGUI_ImagePrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection, const Standard_Integer theMode )
81 {
82     if ( myContour.isEmpty() )
83         return;
84
85     if ( theMode == 0 )
86     {
87         Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner( this );
88
89         TColgp_Array1OfPnt aPoints( 0, myContour.size() - 1 );
90         for ( int i = 0; i < myContour.size(); i++ )
91             aPoints.SetValue( aPoints.Lower() + i, convert( myContour[i] ) );
92
93         Handle(Select3D_SensitiveFace) aSensitiveFace = new Select3D_SensitiveFace( anOwner, aPoints, Select3D_TOS_INTERIOR );
94         theSelection->Add( aSensitiveFace );
95     }
96 }
97
98 void HYDROGUI_ImagePrs::Compute( const Handle(PrsMgr_PresentationManager3d)&,
99                                  const Handle(Prs3d_Presentation)& aPrs,
100                                  const Standard_Integer aMode )
101 {
102     aPrs->Clear();
103     Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup( aPrs );
104
105     if( myImage.IsNull() )
106       return;
107
108     if ( aMode == 0 )
109     {
110         Handle(Graphic3d_ArrayOfPolylines) aSegments = new Graphic3d_ArrayOfPolylines( 5 );
111         aSegments->AddVertex( convert( myContour[0] ) );
112         aSegments->AddVertex( convert( myContour[1] ) );
113         aSegments->AddVertex( convert( myContour[2] ) );
114         aSegments->AddVertex( convert( myContour[3] ) );
115         aSegments->AddVertex( convert( myContour[0] ) );
116
117         aGroup->AddPrimitiveArray( aSegments );
118     }
119     else
120     {
121         Graphic3d_MaterialAspect aMat( Graphic3d_NOM_PLASTIC );
122         Handle(Graphic3d_AspectFillArea3d) aFillAspect =
123             new Graphic3d_AspectFillArea3d( Aspect_IS_SOLID, Quantity_NOC_WHITE, Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1.0, aMat, aMat );
124
125         Handle(Graphic3d_TextureMap) aTex = new Graphic3d_Texture2Dmanual( myImage );
126         aTex->DisableModulate();
127         aFillAspect->SetTextureMapOn();
128         aFillAspect->SetTextureMap( aTex );
129
130         aGroup->SetGroupPrimitivesAspect( aFillAspect );
131
132         Handle(Graphic3d_ArrayOfTriangles) aTriangles = new Graphic3d_ArrayOfTriangles( 6, 0, Standard_False, Standard_False, Standard_True );
133
134         aTriangles->AddVertex( convert( myContour[0] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 0 : 1 ) );
135         aTriangles->AddVertex( convert( myContour[1] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 0 : 1 ) );
136         aTriangles->AddVertex( convert( myContour[2] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 1 : 0 ) );
137
138         aTriangles->AddVertex( convert( myContour[2] ), gp_Pnt2d( 1, myImage->IsTopDown() ? 1 : 0 ) );
139         aTriangles->AddVertex( convert( myContour[3] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 1 : 0 ) );
140         aTriangles->AddVertex( convert( myContour[0] ), gp_Pnt2d( 0, myImage->IsTopDown() ? 0 : 1 ) );
141
142         aGroup->AddPrimitiveArray( aTriangles );
143     }
144 }
145
146 gp_Pnt HYDROGUI_ImagePrs::convert( const QPointF& thePoint ) const
147 {
148     return gp_Pnt( thePoint.x(), thePoint.y(), 0 );
149 }