Salome HOME
Adding HYDROData_Lambert93 source and header.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Shape.h"
24
25 #include <AIS_Drawer.hxx>
26
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30
31 #include <gp_Pnt.hxx>
32
33 #include <Graphic3d_AspectFillArea3d.hxx>
34 #include <Graphic3d_MaterialAspect.hxx>
35
36 #include <TopoDS_Wire.hxx>
37 #include <TopoDS_Face.hxx>
38
39 #include <Precision.hxx>
40
41 #include <Prs3d_ShadingAspect.hxx>
42 #include <Prs3d_LineAspect.hxx>
43 #include <Prs3d_IsoAspect.hxx>
44
45 #include <QColor>
46
47 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext )
48 : myContext( theContext ),
49   myIsHighlight( false ),
50   myFillingColor( Qt::green ),
51   myBorderColor( Qt::black ),
52   myHighlightColor( Qt::white )
53 {
54 }
55
56 HYDROGUI_Shape::~HYDROGUI_Shape()
57 {
58   erase();
59
60   if ( !myShape.IsNull() )
61     myShape.Nullify();
62 }
63
64 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
65 {
66   if ( !myContext || myShape.IsNull() )
67     return;
68
69   myContext->Display( myShape, theIsUpdateViewer );
70 }
71
72 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
73 {
74   if ( !myContext || myShape.IsNull() )
75     return;
76
77   myContext->Erase( myShape, theIsUpdateViewer );
78 }
79
80 void HYDROGUI_Shape::highlight( bool theIsHighlight )
81 {
82   if ( myIsHighlight == theIsHighlight )
83     return;
84
85   myIsHighlight = theIsHighlight;
86
87   if ( !myContext || myShape.IsNull() )
88     return;
89
90   colorShapeBorder( getActiveColor() );
91   myContext->Display( myShape );
92 }
93
94 bool HYDROGUI_Shape::isHighlighted() const
95 {
96   return myIsHighlight;
97 }
98
99 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
100                               const bool         theToDisplay )
101 {
102   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
103   aFaceBuilder.Build();
104   if( aFaceBuilder.IsDone() )
105   {
106     TopoDS_Face aFace = aFaceBuilder.Face();
107     setFace( aFace, theToDisplay );
108   }
109 }
110
111 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
112                               const bool         theToDisplay )
113 {
114   myFace = theFace;
115   buildShape();
116   updateShape( theToDisplay );
117 }
118
119 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
120                                       const bool    theToDisplay )
121 {
122   myFillingColor = theColor;
123   updateShape( theToDisplay );
124 }
125
126 QColor HYDROGUI_Shape::getFillingColor() const
127 {
128   return myFillingColor;
129 }
130
131 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
132                                      const bool    theToDisplay )
133 {
134   myBorderColor = theColor;
135   updateShape( theToDisplay );
136 }
137
138 QColor HYDROGUI_Shape::getBorderColor() const
139 {
140   return myBorderColor;
141 }
142
143 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
144 {
145   myHighlightColor = theColor;
146 }
147
148 QColor HYDROGUI_Shape::getHighlightColor() const
149 {
150   return myHighlightColor;
151 }
152
153 void HYDROGUI_Shape::buildShape()
154 {
155   // Erase previously created shape
156   erase();
157
158   if( myFace.IsNull() )
159     return;
160
161   myShape = new AIS_Shape( myFace );
162
163   myShape->SetTransparency( 0 );
164   myShape->SetDisplayMode( AIS_Shaded );
165
166   // Init default params for shape
167   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
168   if ( !anAttributes.IsNull() )
169   {
170     Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
171     if ( !anIsoAspect.IsNull() )
172       anIsoAspect->SetNumber( 0 );
173     
174     anIsoAspect = anAttributes->VIsoAspect();
175     if ( !anIsoAspect.IsNull() )
176       anIsoAspect->SetNumber( 0 );
177
178     Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
179     if ( !aShadingAspect.IsNull() )
180     {
181       Graphic3d_MaterialAspect aMatAspect;
182       aMatAspect.SetAmbient( 1 );
183       aMatAspect.SetDiffuse( 0 );
184
185       aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
186       aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
187     }
188   }
189 }
190
191 void HYDROGUI_Shape::updateShape( const bool theIsForce )
192 {
193   if ( myShape.IsNull() )
194     return;
195
196   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
197   if ( !anAttributes.IsNull() )
198   {
199     // Coloring face filling
200     Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
201     if ( !aShadingAspect.IsNull() )
202     {
203       Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
204                                     getQuantityColorVal( myFillingColor.green() ),
205                                     getQuantityColorVal( myFillingColor.blue() ),
206                                     Quantity_TOC_RGB );
207
208       aShadingAspect->SetColor( aFillingColor );
209       aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
210     }
211
212     // Coloring borders
213     colorShapeBorder( getActiveColor() );
214   }
215
216   if ( !theIsForce || !myContext )
217     return;
218   
219   myContext->Display( myShape, theIsForce );
220 }
221
222 QColor HYDROGUI_Shape::getActiveColor() const
223 {
224   return isHighlighted() ? myHighlightColor : myBorderColor;
225 }
226
227 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
228 {
229   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
230 }
231
232 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
233 {
234   if ( myShape.IsNull() )
235     return;
236
237   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
238   if ( anAttributes.IsNull() )
239     return;
240
241   if ( theColor.alpha() == 0 )
242   {
243     anAttributes->SetFaceBoundaryDraw( false );
244   }
245   else
246   {
247     Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
248                                  getQuantityColorVal( theColor.green() ),
249                                  getQuantityColorVal( theColor.blue() ),
250                                  Quantity_TOC_RGB );
251
252     anAttributes->SetFaceBoundaryDraw( true );
253
254     Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
255     if ( !aBoundaryAspect.IsNull() )
256     {
257       aBoundaryAspect->SetColor( aBorderColor );
258     }
259   }
260 }
261
262