Salome HOME
#662: test for land cover map presentation
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapPrs.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_LandCoverMapPrs.h>
20 #include <HYDROData_Iterator.h>
21 #include <HYDROData_Tool.h>
22 #include <AIS_DisplayMode.hxx>
23 #include <Prs3d.hxx>
24 #include <Prs3d_IsoAspect.hxx>
25 #include <Prs3d_LineAspect.hxx>
26 #include <Prs3d_ShadingAspect.hxx>
27 #include <Graphic3d_AspectFillArea3d.hxx>
28 #include <Select3D_SensitiveBox.hxx>
29 #include <StdPrs_ShadedShape.hxx>
30 #include <StdPrs_WFShape.hxx>
31 #include <StdSelect.hxx>
32 #include <StdSelect_BRepOwner.hxx>
33 #include <StdSelect_BRepSelectionTool.hxx>
34 #include <TopoDS_Face.hxx>
35 #include <QColor>
36 #include <QString>
37
38 IMPLEMENT_STANDARD_HANDLE( HYDROGUI_LandCoverMapPrs, AIS_Shape )
39 IMPLEMENT_STANDARD_RTTIEXT( HYDROGUI_LandCoverMapPrs, AIS_Shape )
40
41 HYDROGUI_LandCoverMapPrs::HYDROGUI_LandCoverMapPrs( const Handle(HYDROData_LandCoverMap)& theMap )
42   : AIS_Shape( TopoDS_Shape() )
43 {
44   SetLandCoverMap( theMap );
45 }
46
47 HYDROGUI_LandCoverMapPrs::~HYDROGUI_LandCoverMapPrs()
48 {
49 }
50
51 Handle(HYDROData_LandCoverMap) HYDROGUI_LandCoverMapPrs::GetLandCoverMap() const
52 {
53   return myLCMap;
54 }
55
56 void HYDROGUI_LandCoverMapPrs::SetLandCoverMap( const Handle(HYDROData_LandCoverMap)& theMap )
57 {
58   myLCMap = theMap;
59   Set( theMap->GetShape() );
60 }
61
62 Handle(Aspect_ColorScale) HYDROGUI_LandCoverMapPrs::GetColorScale() const
63 {
64   return myColorScale;
65 }
66
67 void HYDROGUI_LandCoverMapPrs::SetColorScale( const Handle(Aspect_ColorScale)& theColorScale )
68 {
69   myColorScale = theColorScale;
70 }
71
72 Handle(HYDROData_StricklerTable) HYDROGUI_LandCoverMapPrs::GetTable() const
73 {
74   return myTable;
75 }
76
77 void HYDROGUI_LandCoverMapPrs::SetTable( const Handle(HYDROData_StricklerTable)& theTable )
78 {
79   myTable = theTable;
80 }
81
82 Quantity_Color HYDROGUI_LandCoverMapPrs::GetColor( const QString& theStricklerType ) const
83 {
84   Quantity_Color aResult;
85
86   if( !myColorScale.IsNull() && !myTable.IsNull() )
87   {
88     double aCoeff = myTable->Get( theStricklerType, 0.0 );
89     Standard_Boolean isOK = myColorScale->FindColor( aCoeff, aResult );
90     if( isOK )
91       return aResult;
92     else
93       return Quantity_Color();
94   }
95
96   if( !myTable.IsNull() && myTable->HasType( theStricklerType ) )
97   {
98     QColor aColor = myTable->GetColor( theStricklerType );
99     return HYDROData_Tool::toOccColor( aColor );
100   }
101
102   HYDROData_Iterator anIt( HYDROData_Document::Document( myLCMap->Label() ), KIND_STRICKLER_TABLE );
103   for( ; anIt.More(); anIt.Next() )
104   {
105     Handle(HYDROData_StricklerTable) aTable = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
106     if( aTable->HasType( theStricklerType ) )
107     {
108       QColor aColor = myTable->GetColor( theStricklerType );
109       return HYDROData_Tool::toOccColor( aColor );
110     }
111   }
112
113   return Quantity_Color();
114 }
115
116 void HYDROGUI_LandCoverMapPrs::Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
117                                         const Handle(Prs3d_Presentation)& thePresentation,
118                                         const Standard_Integer theMode )
119 {
120   if( myLCMap.IsNull() )
121     return;
122
123   thePresentation->Clear();
124   SetMaterial( Graphic3d_NOM_PLASTIC );
125
126   Quantity_Color anEdgeColor = Quantity_NOC_WHITE;
127   myDrawer->LineAspect()->SetColor( anEdgeColor );
128   myDrawer->FaceBoundaryAspect()->SetColor( anEdgeColor );
129   myDrawer->FreeBoundaryAspect()->SetColor( anEdgeColor );
130   myDrawer->UIsoAspect()->SetNumber( 0 );
131   myDrawer->VIsoAspect()->SetNumber( 0 );
132
133   switch( theMode )
134   {
135   case AIS_WireFrame:
136   case AIS_Shaded:
137     {
138       bool isShaded = theMode==AIS_Shaded;
139       HYDROData_LandCoverMap::Iterator anIt( myLCMap );
140       for( ; anIt.More(); anIt.Next() )
141       {
142         TopoDS_Face aFace = anIt.Face();
143         if( isShaded )
144         {
145           QString aStricklerType = anIt.StricklerType();
146           Quantity_Color aColor = GetColor( aStricklerType );
147           Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
148           Graphic3d_MaterialAspect aMaterialAspect( Graphic3d_NOM_PLASTIC );
149           aMaterialAspect.SetColor( aColor );
150           anAspect->SetFrontMaterial( aMaterialAspect );
151           anAspect->SetBackMaterial( aMaterialAspect );
152
153           Handle(Graphic3d_AspectFillArea3d) aGlobalAspect = myDrawer->ShadingAspect()->Aspect();
154           myDrawer->ShadingAspect()->SetAspect( anAspect );
155           StdPrs_ShadedShape::Add( thePresentation, aFace, myDrawer );
156           myDrawer->ShadingAspect()->SetAspect( aGlobalAspect );
157
158           StdPrs_WFShape::Add( thePresentation, aFace, myDrawer );
159         }
160         else
161         {
162           StdPrs_WFShape::Add( thePresentation, aFace, myDrawer );
163         }
164       }
165     }
166     break;
167   default:
168     break;
169   }
170 }
171
172 void HYDROGUI_LandCoverMapPrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
173                                                  const Standard_Integer theMode )
174 {
175   if( myLCMap.IsNull() )
176     return;
177
178   if( theMode==0 )
179   {
180     theSelection->Clear();
181     Bnd_Box B = BoundingBox();
182     Handle(StdSelect_BRepOwner) anOwner = new StdSelect_BRepOwner( myLCMap->GetShape(), this );
183     Handle(Select3D_SensitiveBox) aSensitiveBox = new Select3D_SensitiveBox( anOwner, B );
184     theSelection->Add( aSensitiveBox );
185   }
186   else
187   {
188     Standard_Real aDeflection = Prs3d::GetDeflection( myLCMap->GetShape(), myDrawer );
189     HYDROData_LandCoverMap::Iterator anIt( myLCMap );
190     for( ; anIt.More(); anIt.Next() )
191     {
192       StdSelect_BRepSelectionTool::Load( theSelection,
193                                          this,
194                                          anIt.Face(),
195                                          TopAbs_FACE,
196                                          aDeflection,
197                                          myDrawer->HLRAngle(),
198                                          myDrawer->IsAutoTriangulation());
199     }
200     StdSelect::SetDrawerForBRepOwner( theSelection, myDrawer );
201   }
202 }