0024267: Exception in Visual3d_ViewManager::Redraw() when color scale is displayed
[occt.git] / src / V3d / V3d_Viewer.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 /***********************************************************************
19  
20      FONCTION :
21      ----------
22         Classe V3d_Viewer :
23  
24      HISTORIQUE DES MODIFICATIONS   :
25      --------------------------------
26       00-09-92 : GG  ; Creation.
27       15-11-97 : FMN ; Ajout texture mapping
28       02-02-98 : FMN ; Mise a niveau WNT
29       23-02-98 : FMN ; Remplacement PI par Standard_PI
30       16-07-98 : CAL ; S3892. Ajout grilles 3d.
31
32 ************************************************************************/
33
34 //GER61351      //GG_15/12/99 Adds SetDefaultBackgroundColor() and DefaultBackgroundColor() methods
35
36 #define IMP240100       //GG 
37 //                      Initalize grid echo fields
38
39 /*----------------------------------------------------------------------*/
40 /*
41  * Includes
42  */
43
44 #include <V3d.hxx>
45 #include <Visual3d_View.hxx>
46 #include <Visual3d_Light.hxx>
47 #include <V3d_Viewer.ixx>
48 #include <V3d_View.hxx>
49 #include <V3d_BadValue.hxx>
50 #include <V3d_OrthographicView.hxx>
51 #include <V3d_PerspectiveView.hxx>
52
53 /*----------------------------------------------------------------------*/
54
55 //-Constructor:
56 V3d_Viewer::V3d_Viewer (const Handle(Graphic3d_GraphicDriver)& theDriver,
57                         const Standard_ExtString      theName,
58                         const Standard_CString        theDomain,
59                         const Standard_Real           theViewSize,
60                         const V3d_TypeOfOrientation   theViewProj,
61                         const Quantity_NameOfColor    theViewBackground,
62                         const V3d_TypeOfVisualization theVisualization,
63                         const V3d_TypeOfShadingModel  theShadingModel,
64                         const V3d_TypeOfUpdate        theUpdateMode,
65                         const Standard_Boolean        theComputedMode,
66                         const Standard_Boolean        theDefaultComputedMode,
67                         const V3d_TypeOfSurfaceDetail theSurfaceDetail)  
68 :myNextCount (-1),
69 myDriver (theDriver),
70 myName (TCollection_ExtendedString (theName)),
71 myDomain (TCollection_AsciiString (theDomain)),
72 MyDefinedViews(),
73 MyActiveViews(),
74 MyDefinedLights(),
75 MyActiveLights(),
76 myActiveViewsIterator(),
77 myDefinedViewsIterator(),
78 myActiveLightsIterator(),
79 myDefinedLightsIterator(),
80 myComputedMode (theComputedMode),
81 myDefaultComputedMode (theDefaultComputedMode),
82 myPrivilegedPlane (gp_Ax3 (gp_Pnt (0.,0.,0), gp_Dir (0.,0.,1.), gp_Dir (1.,0.,0.))),
83 myDisplayPlane (Standard_False),
84 myDisplayPlaneLength (theViewSize)
85 #ifdef IMP240100
86 ,myGridEcho (Standard_True), myGridEchoStructure(), myGridEchoGroup()
87 #endif 
88 {
89   MyViewer = new Visual3d_ViewManager (theDriver);
90   // san (16/09/2010): It has been decided to turn depth test ON
91   // by default, as this is important for new font rendering
92   // (without it, there are numerous texture rendering artefacts)
93   MyViewer->SetZBufferAuto (Standard_False);
94   SetUpdateMode (theUpdateMode);
95   SetDefaultViewSize (theViewSize);
96   SetDefaultViewProj (theViewProj);
97   SetDefaultBackgroundColor (theViewBackground);
98   SetDefaultVisualization (theVisualization);
99   SetDefaultShadingModel (theShadingModel);
100   SetDefaultSurfaceDetail (theSurfaceDetail); 
101   SetDefaultAngle (M_PI / 2.);
102   SetDefaultTypeOfView (V3d_ORTHOGRAPHIC);
103
104   Quantity_Color Color1 (Quantity_NOC_GRAY50);
105   Quantity_Color Color2 (Quantity_NOC_GRAY70);
106 //  Quantity_Color White (Quantity_NOC_WHITE);
107   myRGrid = new V3d_RectangularGrid (this, Color1, Color2);
108   myCGrid = new V3d_CircularGrid (this, Color1, Color2);
109   myGridType = Aspect_GT_Rectangular;
110 }
111
112 //-Methods, in order
113
114
115 Handle(V3d_View) V3d_Viewer::CreateView () {
116   if (MyDefaultTypeOfView == V3d_ORTHOGRAPHIC) 
117     return new V3d_OrthographicView(this);
118   else
119     return new V3d_PerspectiveView(this);
120 }
121
122 void V3d_Viewer::SetViewOn( ) {
123
124   for (InitDefinedViews();MoreDefinedViews();NextDefinedViews()){
125     SetViewOn(ActiveView());};
126 }
127
128 void V3d_Viewer::SetViewOff( ) {
129
130   for (InitDefinedViews();MoreDefinedViews();NextDefinedViews()){
131     SetViewOff(ActiveView());};
132 }
133
134 void V3d_Viewer::SetViewOn( const Handle(V3d_View)& TheView ) {
135
136   Handle(Visual3d_View) MyView = TheView->View() ;
137   if( MyView->IsDefined() && !IsActive(TheView)) {
138     MyActiveViews.Append(TheView) ;
139     MyView->Activate();
140     for (InitActiveLights();MoreActiveLights();NextActiveLights()){
141       TheView->SetLightOn(ActiveLight());}
142     // Grid
143     TheView->SetGrid (myPrivilegedPlane, Grid ());
144     TheView->SetGridActivity (Grid ()->IsActive ());
145     // Update
146     MyView->Redraw() ;
147   }
148 }
149
150 void V3d_Viewer::SetViewOff( const Handle(V3d_View)& TheView ) {
151
152   Handle(Visual3d_View) MyView =TheView->View(); 
153   if( MyView->IsDefined() && IsActive(TheView) ) {
154     MyActiveViews.Remove(TheView);
155     MyView->Deactivate() ;
156   }
157 }
158
159 Standard_Boolean V3d_Viewer::ComputedMode() const {
160   return myComputedMode;
161 }
162
163 Standard_Boolean V3d_Viewer::DefaultComputedMode() const {
164   return myDefaultComputedMode;
165 }
166
167 void V3d_Viewer::Update() {
168
169   MyViewer->Update();
170 }
171
172 void V3d_Viewer::Redraw()const  {
173
174   MyViewer->Redraw();
175 }
176
177 void V3d_Viewer::Remove() {
178
179   MyViewer->Remove();
180 }
181
182 void V3d_Viewer::Erase() const {
183   
184   MyViewer->Erase();
185 }
186
187 void V3d_Viewer::UnHighlight() const {
188
189   //FMN MyViewer->UnHighlight();
190 }
191
192 void V3d_Viewer::SetDefaultBackgroundColor(const Quantity_TypeOfColor Type, const Standard_Real v1, const Standard_Real v2, const Standard_Real v3) {
193   Standard_Real V1 = v1 ;
194   Standard_Real V2 = v2 ;
195   Standard_Real V3 = v3 ;
196
197   if( V1 < 0. ) V1 = 0. ; else if( V1 > 1. ) V1 = 1. ;
198   if( V2 < 0. ) V2 = 0. ; else if( V2 > 1. ) V2 = 1. ;
199   if( V3 < 0. ) V3 = 0. ; else if( V3 > 1. ) V3 = 1. ;
200
201   Quantity_Color C(V1,V2,V3,Type) ;
202   SetDefaultBackgroundColor(C);
203 }
204
205 void V3d_Viewer::SetDefaultBackgroundColor(const Quantity_NameOfColor Name)
206 {
207   Quantity_Color C(Name) ;
208   SetDefaultBackgroundColor(C);
209 }
210
211 void V3d_Viewer::SetDefaultBackgroundColor(const Quantity_Color &Color)
212 {
213   MyBackground.SetColor(Color) ;
214 }
215
216 void V3d_Viewer::SetDefaultBgGradientColors( const Quantity_NameOfColor Name1,
217                                              const Quantity_NameOfColor Name2,
218                                              const Aspect_GradientFillMethod FillStyle){
219
220   Quantity_Color C1(Name1) ;
221   Quantity_Color C2(Name2) ;
222   MyGradientBackground.SetColors(C1, C2, FillStyle);
223
224
225
226 void V3d_Viewer::SetDefaultBgGradientColors( const Quantity_Color& Color1,
227                                              const Quantity_Color& Color2,
228                                              const Aspect_GradientFillMethod FillStyle ){
229
230   MyGradientBackground.SetColors(Color1, Color2, FillStyle);
231
232 }  
233
234
235 void V3d_Viewer::SetDefaultViewSize(const Standard_Real Size) {
236
237   V3d_BadValue_Raise_if( Size <= 0. ,"V3d_Viewer::SetDefaultViewSize, bad size");
238   MyViewSize = Size ;
239 }
240
241 void V3d_Viewer::SetDefaultViewProj(const V3d_TypeOfOrientation Orientation) {
242
243   MyViewProj = Orientation ;
244 }
245
246 void V3d_Viewer::SetDefaultVisualization(const V3d_TypeOfVisualization Type) {
247
248   MyVisualization = Type ;
249 }
250 void V3d_Viewer::SetZBufferManagment(const Standard_Boolean Automatic) {
251   MyViewer->SetZBufferAuto (Automatic);
252 }
253 Standard_Boolean V3d_Viewer::ZBufferManagment() const {
254   return MyViewer->ZBufferAuto();
255 }
256
257 void V3d_Viewer::SetDefaultShadingModel(const V3d_TypeOfShadingModel Type) {
258
259   MyShadingModel = Type ;
260 }
261
262 void V3d_Viewer::SetDefaultSurfaceDetail(const V3d_TypeOfSurfaceDetail Type) {
263
264   MySurfaceDetail = Type ;
265 }
266
267 void V3d_Viewer::SetDefaultAngle(const Quantity_PlaneAngle Angle) {
268   MyDefaultAngle = Angle;
269 }
270
271 void V3d_Viewer::SetDefaultTypeOfView(const V3d_TypeOfView Type) {
272   MyDefaultTypeOfView = Type;}
273
274
275 void V3d_Viewer::SetUpdateMode(const V3d_TypeOfUpdate Mode) {
276   
277   MyViewer->SetUpdateMode((Aspect_TypeOfUpdate)Mode) ;
278 }
279
280 void V3d_Viewer::DefaultBackgroundColor(const Quantity_TypeOfColor Type,Standard_Real &V1,Standard_Real &V2,Standard_Real &V3) const
281 {
282   Quantity_Color C = DefaultBackgroundColor();
283   C.Values(V1,V2,V3,Type) ;
284 }
285
286 Quantity_Color V3d_Viewer::DefaultBackgroundColor() const
287 {
288   return MyBackground.Color() ;
289 }
290
291 void V3d_Viewer::DefaultBgGradientColors(Quantity_Color& Color1,Quantity_Color& Color2) const
292 {
293   MyGradientBackground.Colors(Color1,Color2);     
294 }
295
296 Standard_Real V3d_Viewer::DefaultViewSize() const {
297   return MyViewSize ;
298 }
299
300 V3d_TypeOfOrientation V3d_Viewer::DefaultViewProj() const {
301   return MyViewProj ;
302 }
303
304 V3d_TypeOfVisualization V3d_Viewer::DefaultVisualization() const {
305   return MyVisualization ;
306 }
307
308 V3d_TypeOfShadingModel V3d_Viewer::DefaultShadingModel() const {
309   return MyShadingModel ;
310 }
311
312 V3d_TypeOfSurfaceDetail V3d_Viewer::DefaultSurfaceDetail() const {
313   return MySurfaceDetail ;
314 }
315
316 Quantity_PlaneAngle V3d_Viewer::DefaultAngle() const {
317   return MyDefaultAngle;
318 }
319
320 V3d_TypeOfUpdate V3d_Viewer::UpdateMode() const {
321
322   V3d_TypeOfUpdate Mode = (V3d_TypeOfUpdate) MyViewer->UpdateMode() ;
323   return Mode ;
324 }
325
326 Standard_Boolean V3d_Viewer::IfMoreViews() const {
327   Standard_Boolean TheStatus = Standard_False ;
328
329 #ifdef NEW
330   if( MyActiveViews->Length() < Visual3d_View::Limit() )
331 #endif /*NEW*/
332     TheStatus = Standard_True ;
333   return TheStatus ;
334 }
335
336 Handle(Visual3d_ViewManager) V3d_Viewer::Viewer() const {
337   return MyViewer ;
338 }
339
340 Aspect_Background V3d_Viewer::GetBackgroundColor() const {
341   return MyBackground ;
342 }
343
344 Aspect_GradientBackground V3d_Viewer::GetGradientBackground() const {
345   return MyGradientBackground;
346 }   
347
348 void V3d_Viewer::AddView( const Handle(V3d_View)& TheView ) {
349
350   MyDefinedViews.Append(TheView);
351   IncrCount();
352 }
353
354 void V3d_Viewer::DelView( const Handle(V3d_View)& TheView ) {
355
356   MyActiveViews.Remove(TheView);
357   MyDefinedViews.Remove(TheView);
358 }
359
360 //=======================================================================
361 //function : AddZLayer
362 //purpose  :
363 //=======================================================================
364
365 Standard_Boolean V3d_Viewer::AddZLayer (Standard_Integer& theLayerId)
366 {
367   return MyViewer->AddZLayer (theLayerId);
368 }
369
370 //=======================================================================
371 //function : RemoveZLayer
372 //purpose  : 
373 //=======================================================================
374
375 Standard_Boolean V3d_Viewer::RemoveZLayer (const Standard_Integer theLayerId)
376 {
377   return MyViewer->RemoveZLayer (theLayerId);
378 }
379
380 //=======================================================================
381 //function : GetAllZLayers
382 //purpose  :
383 //=======================================================================
384
385 void V3d_Viewer::GetAllZLayers (TColStd_SequenceOfInteger& theLayerSeq) const
386 {
387   MyViewer->GetAllZLayers (theLayerSeq);
388 }
389
390 //=======================================================================
391 //function : Domain
392 //purpose  :
393 //=======================================================================
394
395 Standard_CString V3d_Viewer::Domain() const
396 {
397   return myDomain.ToCString();
398 }
399
400 //=======================================================================
401 //function : Driver
402 //purpose  :
403 //=======================================================================
404
405 const Handle(Graphic3d_GraphicDriver)& V3d_Viewer::Driver() const
406 {
407   return myDriver;
408 }
409
410 //=======================================================================
411 //function : NextName
412 //purpose  :
413 //=======================================================================
414
415 Standard_ExtString V3d_Viewer::NextName() const
416 {
417   TCollection_ExtendedString aNextName = TCollection_ExtendedString (myName.ToExtString());
418   aNextName.AssignCat (TCollection_ExtendedString (myNextCount));
419   
420   return aNextName.ToExtString();
421 }
422
423 //=======================================================================
424 //function : IncrCount
425 //purpose  :
426 //=======================================================================
427
428 void V3d_Viewer::IncrCount()
429 {
430   myNextCount++;
431 }