Salome HOME
#662: custom mesh for 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 <BRepMesh_IncrementalMesh.hxx>
24 #include <Graphic3d_ArrayOfTriangles.hxx>
25 #include <Graphic3d_AspectFillArea3d.hxx>
26 #include <Poly_Triangulation.hxx>
27 #include <Prs3d_Root.hxx>
28 #include <Select3D_SensitiveBox.hxx>
29 #include <StdSelect_BRepOwner.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <QColor>
32 #include <QString>
33
34 IMPLEMENT_STANDARD_HANDLE( HYDROGUI_LandCoverMapPrs, AIS_InteractiveObject )
35 IMPLEMENT_STANDARD_RTTIEXT( HYDROGUI_LandCoverMapPrs, AIS_InteractiveObject )
36
37 HYDROGUI_LandCoverMapPrs::HYDROGUI_LandCoverMapPrs( const Handle(HYDROData_LandCoverMap)& theMap )
38   : AIS_InteractiveObject()
39 {
40   SetLandCoverMap( theMap );
41 }
42
43 HYDROGUI_LandCoverMapPrs::~HYDROGUI_LandCoverMapPrs()
44 {
45 }
46
47 Handle(HYDROData_LandCoverMap) HYDROGUI_LandCoverMapPrs::GetLandCoverMap() const
48 {
49   return myLCMap;
50 }
51
52 void HYDROGUI_LandCoverMapPrs::SetLandCoverMap( const Handle(HYDROData_LandCoverMap)& theMap )
53 {
54   myLCMap = theMap;
55 }
56
57 Handle(Aspect_ColorScale) HYDROGUI_LandCoverMapPrs::GetColorScale() const
58 {
59   return myColorScale;
60 }
61
62 void HYDROGUI_LandCoverMapPrs::SetColorScale( const Handle(Aspect_ColorScale)& theColorScale )
63 {
64   myColorScale = theColorScale;
65 }
66
67 Handle(HYDROData_StricklerTable) HYDROGUI_LandCoverMapPrs::GetTable() const
68 {
69   return myTable;
70 }
71
72 void HYDROGUI_LandCoverMapPrs::SetTable( const Handle(HYDROData_StricklerTable)& theTable )
73 {
74   myTable = theTable;
75 }
76
77 Quantity_Color HYDROGUI_LandCoverMapPrs::GetColor( const QString& theStricklerType ) const
78 {
79   Quantity_Color aResult;
80
81   if( !myColorScale.IsNull() && !myTable.IsNull() )
82   {
83     double aCoeff = myTable->Get( theStricklerType, 0.0 );
84     Standard_Boolean isOK = myColorScale->FindColor( aCoeff, aResult );
85     if( isOK )
86       return aResult;
87     else
88       return Quantity_Color();
89   }
90
91   if( !myTable.IsNull() && myTable->HasType( theStricklerType ) )
92   {
93     QColor aColor = myTable->GetColor( theStricklerType );
94     return HYDROData_Tool::toOccColor( aColor );
95   }
96
97   HYDROData_Iterator anIt( HYDROData_Document::Document( myLCMap->Label() ), KIND_STRICKLER_TABLE );
98   for( ; anIt.More(); anIt.Next() )
99   {
100     Handle(HYDROData_StricklerTable) aTable = Handle(HYDROData_StricklerTable)::DownCast( anIt.Current() );
101     if( aTable->HasType( theStricklerType ) )
102     {
103       QColor aColor = myTable->GetColor( theStricklerType );
104       return HYDROData_Tool::toOccColor( aColor );
105     }
106   }
107
108   return Quantity_Color();
109 }
110
111 void DrawTriangulation( const Handle(Prs3d_Presentation)& thePresentation,
112                         const Handle(Poly_Triangulation)& theTriangulation,
113                         const Quantity_Color& theColor )
114 {
115   const TColgp_Array1OfPnt& aNodesArray = theTriangulation->Nodes();
116   int aNbNodes = aNodesArray.Length();
117
118   const Poly_Array1OfTriangle& aTrianglesArray = theTriangulation->Triangles();
119   int aNbTriangles = aTrianglesArray.Length();
120
121   Handle( Graphic3d_ArrayOfTriangles ) anArray = new Graphic3d_ArrayOfTriangles
122     ( aNbNodes, aNbTriangles * 3, Standard_True, Standard_True, Standard_False );
123
124   Prs3d_Root::NewGroup( thePresentation );
125   Handle( Graphic3d_Group ) aGroup = Prs3d_Root::CurrentGroup( thePresentation );
126   Handle( Graphic3d_AspectFillArea3d ) anAspect = new Graphic3d_AspectFillArea3d();
127   Graphic3d_MaterialAspect aMaterialAspect( Graphic3d_NOM_PLASTIC );
128   aMaterialAspect.SetColor( theColor );
129   anAspect->SetFrontMaterial( aMaterialAspect );
130   anAspect->SetBackMaterial( aMaterialAspect );
131
132   Standard_Integer i;
133
134   for( i = aNodesArray.Lower(); i <= aNodesArray.Upper(); i++ )
135   {
136     anArray->AddVertex( aNodesArray( i ), theColor );
137     anArray->SetVertexNormal( i, 0, 0, 1 );
138   }
139
140   Standard_Integer anIndexTriangle[3] = { 0, 0, 0 };
141   for( i = aTrianglesArray.Lower(); i<= aTrianglesArray.Upper(); i++ )
142   {
143     aTrianglesArray( i ).Get( anIndexTriangle[0], anIndexTriangle[1], anIndexTriangle[2] );
144     anArray->AddEdge( anIndexTriangle[0] );
145     anArray->AddEdge( anIndexTriangle[1] );
146     anArray->AddEdge( anIndexTriangle[2] );
147   }
148   aGroup->AddPrimitiveArray( anArray );
149   //aGroup->SetGroupPrimitivesAspect( anAspect );
150 }
151
152 void HYDROGUI_LandCoverMapPrs::Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
153                                         const Handle(Prs3d_Presentation)& thePresentation,
154                                         const Standard_Integer theMode )
155 {
156   if( myLCMap.IsNull() )
157     return;
158
159   thePresentation->Clear();
160   const double aLinearDeflection = 1E-2;
161   BRepMesh_IncrementalMesh aMesh( myLCMap->GetShape(), aLinearDeflection );
162   aMesh.Perform();
163   bool isOK = aMesh.IsDone();
164
165   switch( theMode )
166   {
167   case AIS_Shaded:
168     {
169       HYDROData_LandCoverMap::Iterator anIt( myLCMap );
170       for( ; anIt.More(); anIt.Next() )
171       {
172         TopoDS_Face aFace = anIt.Face();
173         QString aStricklerType = anIt.StricklerType();
174         Quantity_Color aColor = GetColor( aStricklerType );
175
176         TopLoc_Location aLocation;
177         Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation( aFace, aLocation );
178         DrawTriangulation( thePresentation, aTriangulation, aColor );
179       }
180     }
181     break;
182   default:
183     break;
184   }
185 }
186
187 void HYDROGUI_LandCoverMapPrs::ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
188                                                  const Standard_Integer theMode )
189 {
190   if( myLCMap.IsNull() )
191     return;
192
193   if( theMode==0 )
194   {
195     /*theSelection->Clear();
196     Bnd_Box B = BoundingBox();
197     Handle(StdSelect_BRepOwner) anOwner = new StdSelect_BRepOwner( myLCMap->GetShape(), this );
198     Handle(Select3D_SensitiveBox) aSensitiveBox = new Select3D_SensitiveBox( anOwner, B );
199     theSelection->Add( aSensitiveBox );*/
200   }
201   else
202   {
203     //TODO
204   }
205 }