]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISU_ScalarBarActor.cxx
Salome HOME
Issue 0019818: EDF 703 SMESH VISU : Display Mesh Groups names in viewer (as a caption)
[modules/visu.git] / src / PIPELINE / VISU_ScalarBarActor.cxx
1 //  Copyright (C) 2007-2008  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 //  VISU OBJECT : interactive object for VISU entities implementation
23 // File:    VISU_PipeLine.hxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26 //
27 #include "VISU_ScalarBarActor.hxx"
28
29 #include <vtkPolyDataMapper2D.h>
30 #include <vtkCellArray.h>
31 #include <vtkCellData.h>
32 #include <vtkObjectFactory.h>
33 #include <vtkPolyData.h>
34 #include <vtkPolyDataMapper2D.h>
35 #include <vtkScalarsToColors.h>
36 #include <vtkTextMapper.h>
37 #include <vtkTextProperty.h>
38 #include <vtkViewport.h>
39 #include <vtkWindow.h>
40 #include <vtkLogLookupTable.h>
41 #include <vtkProperty2D.h> // RKV
42 #include <vtkAxisActor2D.h> // RKV
43
44 using namespace std;
45
46 vtkCxxRevisionMacro(VISU_ScalarBarActor, "$Revision$");
47
48 vtkCxxSetObjectMacro(VISU_ScalarBarActor,LookupTable,VISU_LookupTable);
49 vtkCxxSetObjectMacro(VISU_ScalarBarActor,LabelTextProperty,vtkTextProperty);
50 vtkCxxSetObjectMacro(VISU_ScalarBarActor,TitleTextProperty,vtkTextProperty);
51
52 //------------------------------------------------------------------------------
53 VISU_ScalarBarActor* VISU_ScalarBarActor::New(){
54   vtkObject* ret = vtkObjectFactory::CreateInstance("VISU_ScalarBarActor");
55   if(ret)
56     return (VISU_ScalarBarActor*)ret;
57   return new VISU_ScalarBarActor;
58 }
59
60 VISU_ScalarBarActor::VISU_ScalarBarActor()
61 {
62   this->LookupTable = NULL;
63   this->Position2Coordinate->SetValue(0.17, 0.8);
64   
65   this->PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
66   this->PositionCoordinate->SetValue(0.82,0.1);
67   
68   this->MaximumNumberOfColors = 64;
69   this->NumberOfLabels = 5;
70   this->NumberOfLabelsBuilt = 0;
71   this->Orientation = VTK_ORIENT_VERTICAL;
72   this->Title = NULL;
73
74   this->LabelTextProperty = vtkTextProperty::New();
75   this->LabelTextProperty->SetFontSize(12);
76   this->LabelTextProperty->SetBold(1);
77   this->LabelTextProperty->SetItalic(1);
78   this->LabelTextProperty->SetShadow(1);
79   this->LabelTextProperty->SetFontFamilyToArial();
80
81   this->TitleTextProperty = vtkTextProperty::New();
82   this->TitleTextProperty->ShallowCopy(this->LabelTextProperty);
83
84   this->LabelFormat = new char[8]; 
85   sprintf(this->LabelFormat,"%s","%-#6.3g");
86
87   this->TitleMapper = vtkTextMapper::New();
88   this->TitleActor = vtkActor2D::New();
89   this->TitleActor->SetMapper(this->TitleMapper);
90   this->TitleActor->GetPositionCoordinate()->
91     SetReferenceCoordinate(this->PositionCoordinate);
92   
93   this->TextMappers = NULL;
94   this->TextActors = NULL;
95
96   this->ScalarBar = vtkPolyData::New();
97   this->ScalarBarMapper = vtkPolyDataMapper2D::New();
98   this->ScalarBarMapper->SetInput(this->ScalarBar);
99   this->ScalarBarActor = vtkActor2D::New();
100   this->ScalarBarActor->SetMapper(this->ScalarBarMapper);
101   this->ScalarBarActor->GetPositionCoordinate()->
102     SetReferenceCoordinate(this->PositionCoordinate);
103   this->LastOrigin[0] = 0;
104   this->LastOrigin[1] = 0;
105   this->LastSize[0] = 0;
106   this->LastSize[1] = 0;
107
108   this->TitleRatioSize = 0;
109   this->LabelRatioWidth = 0;
110   this->BarRatioWidth = 0;
111   this->BarRatioHeight = 0;
112   
113   // RKV : Begin
114   this->Distribution = vtkDoubleArray::New();
115   this->DistributionObj = vtkDataObject::New();
116   this->DistributionActor = VISU_XYPlotActor::New();
117   this->DistributionActor->SetTitle("");
118   this->DistributionActor->SetXTitle("");
119   this->DistributionActor->SetYTitle("");
120   this->DistributionActor->GetXAxisActor2D()->LabelVisibilityOff();
121   this->DistributionActor->GetXAxisActor2D()->TitleVisibilityOff();
122   this->DistributionActor->GetXAxisActor2D()->TickVisibilityOff();
123 //  this->DistributionActor->GetXAxisActor2D()->SetFontFactor(0.);
124   this->DistributionActor->SetNumberOfYLabels(1);
125 //  this->DistributionActor->SetNumberOfXLabels(2);
126   this->DistributionActor->GetXAxisActor2D()->AdjustLabelsOff();
127   this->DistributionActor->GetYAxisActor2D()->AdjustLabelsOff();
128   this->DistributionActor->LegendOff();
129   this->DistributionActor->SetLabelFormat("%4.3f");
130   this->DistributionActor->SetXValuesToIndex();
131 //  this->DistributionActor->GetPositionCoordinate()->SetValue(0.0, 0.67, 0);
132 //  this->DistributionActor->GetPosition2Coordinate()->SetValue(1.0, 0.33, 0); // #relative to Position
133   this->DistributionActor->GetPositionCoordinate()->
134     SetReferenceCoordinate(this->PositionCoordinate);
135   this->DistributionVisibilityOff(); // Don't show the distribution curve by default
136   
137   // RKV : End
138 }
139
140 void VISU_ScalarBarActor::ReleaseGraphicsResources(vtkWindow *win)
141 {
142   this->TitleActor->ReleaseGraphicsResources(win);
143   if (this->TextMappers != NULL )
144     {
145     for (int i=0; i < this->NumberOfLabelsBuilt; i++)
146       {
147       this->TextActors[i]->ReleaseGraphicsResources(win);
148       }
149     }
150   this->ScalarBarActor->ReleaseGraphicsResources(win);
151   this->DistributionActor->ReleaseGraphicsResources(win); // RKV
152 }
153
154 VISU_ScalarBarActor::~VISU_ScalarBarActor()
155 {
156   if (this->LabelFormat) 
157     {
158     delete [] this->LabelFormat;
159     this->LabelFormat = NULL;
160     }
161
162   this->TitleMapper->Delete();
163   this->TitleActor->Delete();
164
165   if (this->TextMappers != NULL )
166     {
167     for (int i=0; i < this->NumberOfLabelsBuilt; i++)
168       {
169       this->TextMappers[i]->Delete();
170       this->TextActors[i]->Delete();
171       }
172     delete [] this->TextMappers;
173     delete [] this->TextActors;
174     }
175
176   // RKV : Begin
177   this->DistributionActor->Delete();
178   this->DistributionObj->Delete();
179   this->SetDistribution(NULL);
180   // RKV : End
181
182   this->ScalarBar->Delete();
183   this->ScalarBarMapper->Delete();
184   this->ScalarBarActor->Delete();
185   
186   if (this->Title)
187     {
188     delete [] this->Title;
189     this->Title = NULL;
190     }
191   
192   this->SetLookupTable(NULL);
193   this->SetLabelTextProperty(NULL);
194   this->SetTitleTextProperty(NULL);
195 }
196
197 int VISU_ScalarBarActor::RenderOverlay(vtkViewport *viewport)
198 {
199   int renderedSomething = 0;
200   int i;
201   
202   // Everything is built, just have to render
203   if (this->Title != NULL)
204     {
205     renderedSomething += this->TitleActor->RenderOverlay(viewport);
206     }
207   this->ScalarBarActor->RenderOverlay(viewport);
208   // RKV : Begin
209   if (this->DistributionVisibility)
210     this->DistributionActor->RenderOverlay(viewport);
211   // RKV : End
212   if( this->TextActors == NULL)
213     {
214      vtkWarningMacro(<<"Need a mapper to render a scalar bar");
215      return renderedSomething;
216     }
217   
218   for (i=0; i<this->NumberOfLabels; i++)
219     {
220     renderedSomething += this->TextActors[i]->RenderOverlay(viewport);
221     }
222
223   renderedSomething = (renderedSomething > 0)?(1):(0);
224
225   return renderedSomething;
226 }
227
228 int VISU_ScalarBarActor::RenderOpaqueGeometry(vtkViewport *viewport)
229 {
230   int renderedSomething = 0;
231   int i;
232   int size[2];
233   
234   if (!this->LookupTable)
235     {
236     vtkWarningMacro(<<"Need a mapper to render a scalar bar");
237     return 0;
238     }
239
240   if (!this->TitleTextProperty)
241     {
242     vtkErrorMacro(<<"Need title text property to render a scalar bar");
243     return 0;
244     }
245
246   if (!this->LabelTextProperty)
247     {
248     vtkErrorMacro(<<"Need label text property to render a scalar bar");
249     return 0;
250     }
251
252   // Check to see whether we have to rebuild everything
253   int positionsHaveChanged = 0;
254   if (viewport->GetMTime() > this->BuildTime || 
255       (viewport->GetVTKWindow() && 
256        viewport->GetVTKWindow()->GetMTime() > this->BuildTime))
257     {
258     // if the viewport has changed we may - or may not need
259     // to rebuild, it depends on if the projected coords chage
260     int *barOrigin;
261     barOrigin = this->PositionCoordinate->GetComputedViewportValue(viewport);
262     size[0] = 
263       this->Position2Coordinate->GetComputedViewportValue(viewport)[0] -
264       barOrigin[0];
265     size[1] = 
266       this->Position2Coordinate->GetComputedViewportValue(viewport)[1] -
267       barOrigin[1];
268     if (this->LastSize[0] != size[0] || 
269         this->LastSize[1] != size[1] ||
270         this->LastOrigin[0] != barOrigin[0] || 
271         this->LastOrigin[1] != barOrigin[1])
272       {
273       positionsHaveChanged = 1;
274       }
275     }
276   
277   // Check to see whether we have to rebuild everything
278   // RKV : Begin
279   if (positionsHaveChanged ||
280       this->GetMTime() > this->BuildTime || 
281       this->LookupTable->GetMTime() > this->BuildTime ||
282       this->LabelTextProperty->GetMTime() > this->BuildTime ||
283       this->TitleTextProperty->GetMTime() > this->BuildTime ||
284       this->Distribution->GetMTime() > this->BuildTime)
285   // RKV : End
286 /* RKV  if (positionsHaveChanged ||
287       this->GetMTime() > this->BuildTime || 
288       this->LookupTable->GetMTime() > this->BuildTime ||
289       this->LabelTextProperty->GetMTime() > this->BuildTime ||
290       this->TitleTextProperty->GetMTime() > this->BuildTime)*/
291     {
292
293     // Delete previously constructed objects
294     //
295     if (this->TextMappers != NULL )
296       {
297       for (i=0; i < this->NumberOfLabelsBuilt; i++)
298         {
299         this->TextMappers[i]->Delete();
300         this->TextActors[i]->Delete();
301         }
302       delete [] this->TextMappers;
303       delete [] this->TextActors;
304       }
305
306     // Build scalar bar object; determine its type
307     //
308     VISU_LookupTable *lut = this->LookupTable; //SALOME specific
309     int isLogTable = lut->GetScale() == VTK_SCALE_LOG10;
310     
311     // we hard code how many steps to display
312     int numColors = this->MaximumNumberOfColors;
313     vtkFloatingPointType *range = lut->GetRange();
314
315     int numPts = 2*(numColors + 1);
316     vtkPoints *pts = vtkPoints::New();
317     pts->SetNumberOfPoints(numPts);
318     vtkCellArray *polys = vtkCellArray::New();
319     polys->Allocate(polys->EstimateSize(numColors,4));
320     vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
321     colors->SetNumberOfComponents(3);
322     colors->SetNumberOfTuples(numColors);
323
324 // RKV : Begin
325     // If the distribution is changed then recalculate the total
326     if (this->Distribution->GetMTime() > this->BuildTime) {
327           int aNbVals = this->Distribution->GetNumberOfTuples();
328       double range[2];
329       this->Distribution->GetRange(range);
330       this->DistributionActor->SetYRange(0, range[1]);
331 /*        int total = 0;
332           for(vtkIdType aValId = 0; aValId < aNbVals; aValId++){
333                 total += this->Distribution->GetValue(aValId);
334           }
335           this->DistributionActor->SetYRange(0, total);
336 */
337       }
338   
339     this->DistributionActor->SetProperty(this->GetProperty());
340     this->DistributionActor->GetProperty()->SetColor(1, 1, 1);
341     this->DistributionActor->GetProperty()->SetLineWidth(2);
342     this->DistributionActor->GetProperty()->SetDisplayLocationToForeground();
343     vtkTextProperty* tprop;
344     tprop = this->DistributionActor->GetTitleTextProperty();
345     tprop->SetColor(this->DistributionActor->GetProperty()->GetColor());
346 //  this->DistributionActor->SetAxisTitleTextProperty(tprop);
347 //  this->DistributionActor->SetAxisLabelTextProperty(tprop);
348     
349 // RKV : End
350     this->ScalarBarActor->SetProperty(this->GetProperty());
351     this->ScalarBar->Initialize();
352     this->ScalarBar->SetPoints(pts);
353     this->ScalarBar->SetPolys(polys);
354     this->ScalarBar->GetCellData()->SetScalars(colors);
355     pts->Delete(); polys->Delete(); colors->Delete();
356
357     // get the viewport size in display coordinates
358     int *barOrigin, barWidth, barHeight;
359     barOrigin = this->PositionCoordinate->GetComputedViewportValue(viewport);
360     size[0] = 
361       this->Position2Coordinate->GetComputedViewportValue(viewport)[0] -
362       barOrigin[0];
363     size[1] = 
364       this->Position2Coordinate->GetComputedViewportValue(viewport)[1] -
365       barOrigin[1];
366     this->LastOrigin[0] = barOrigin[0];
367     this->LastOrigin[1] = barOrigin[1];
368     this->LastSize[0] = size[0];
369     this->LastSize[1] = size[1];
370     
371     // Update all the composing objects
372     this->TitleActor->SetProperty(this->GetProperty());
373     this->TitleMapper->SetInput(this->Title);
374     if (this->TitleTextProperty->GetMTime() > this->BuildTime)
375       {
376       // Shallow copy here so that the size of the title prop is not affected
377       // by the automatic adjustment of its text mapper's size (i.e. its
378       // mapper's text property is identical except for the font size
379       // which will be modified later). This allows text actors to
380       // share the same text property, and in that case specifically allows
381       // the title and label text prop to be the same.
382       this->TitleMapper->GetTextProperty()->ShallowCopy(this->TitleTextProperty);
383       this->TitleMapper->GetTextProperty()->SetJustificationToCentered();
384       }
385     
386     // find the best size for the title font
387     int titleSize[2];
388     this->SizeTitle(titleSize, size, viewport);
389     
390     // find the best size for the ticks
391     int labelSize[2];
392     this->AllocateAndSizeLabels(labelSize, size, viewport, range);
393     this->NumberOfLabelsBuilt = this->NumberOfLabels;
394     
395     this->SizeBar(barWidth, barHeight, size, viewport, range);
396
397     // generate points
398     vtkFloatingPointType x[3]; x[2] = 0.0;
399     vtkFloatingPointType delta, val;
400     if ( this->Orientation == VTK_ORIENT_VERTICAL )
401       {
402       delta=(vtkFloatingPointType)barHeight/numColors;
403       for (i=0; i<numPts/2; i++)
404         {
405         x[0] = 0;
406         x[1] = i*delta;
407         pts->SetPoint(2*i,x);
408         x[0] = barWidth;
409         pts->SetPoint(2*i+1,x);
410         }
411       }
412     else
413       {
414       delta=(vtkFloatingPointType)barWidth/numColors;
415       for (i=0; i<numPts/2; i++)
416         {
417         x[0] = i*delta;
418         x[1] = barHeight;
419         pts->SetPoint(2*i,x);
420         x[1] = 0;
421         pts->SetPoint(2*i+1,x);
422         }
423       }
424
425     //polygons & cell colors
426     unsigned char *rgba, *rgb;
427     vtkIdType ptIds[4];
428     for (i=0; i<numColors; i++)
429       {
430       ptIds[0] = 2*i;
431       ptIds[1] = ptIds[0] + 1;
432       ptIds[2] = ptIds[1] + 2;
433       ptIds[3] = ptIds[0] + 2;
434       polys->InsertNextCell(4,ptIds);
435
436       if ( isLogTable ){ //SALOME specific
437         vtkFloatingPointType rgbval = log10(range[0]) + 
438           i*(log10(range[1])-log10(range[0]))/(numColors -1);
439         rgba = lut->MapValue(rgbval);
440       }else{
441         rgba = lut->MapValue(range[0] + (range[1] - range[0])*
442                              ((vtkFloatingPointType)i /(numColors-1.0)));
443       }
444
445       rgb = colors->GetPointer(3*i); //write into array directly
446       rgb[0] = rgba[0];
447       rgb[1] = rgba[1];
448       rgb[2] = rgba[2];
449     }
450
451     // Now position everything properly
452     //
453     if (this->Orientation == VTK_ORIENT_VERTICAL)
454       {
455       int sizeTextData[2];
456       
457       // center the title
458       this->TitleActor->SetPosition(size[0]/2, 0.9*size[1]);
459       
460       for (i=0; i < this->NumberOfLabels; i++)
461         {
462         val = (vtkFloatingPointType)i/(this->NumberOfLabels-1) *barHeight;
463         this->TextMappers[i]->GetSize(viewport,sizeTextData);
464         this->TextMappers[i]->GetTextProperty()->SetJustificationToLeft();
465         this->TextActors[i]->SetPosition(barWidth+3,
466                                          val - sizeTextData[1]/2);
467         }
468         
469       }
470     else
471       {
472       this->TitleActor->SetPosition(size[0]/2, 
473                                     barHeight + labelSize[1] + 0.1*size[1]);
474       for (i=0; i < this->NumberOfLabels; i++)
475         {
476         this->TextMappers[i]->GetTextProperty()->SetJustificationToCentered();
477         val = (vtkFloatingPointType)i/(this->NumberOfLabels-1) * barWidth;
478         this->TextActors[i]->SetPosition(val, barHeight + 0.05*size[1]);
479         }
480         
481       }
482       
483     // Compute the position of the distribution curve
484     this->PlaceDistribution(viewport, barWidth, barHeight); // RKV
485
486     this->BuildTime.Modified();
487     }
488
489   // Everything is built, just have to render
490   if (this->Title != NULL)
491     {
492     renderedSomething += this->TitleActor->RenderOpaqueGeometry(viewport);
493     }
494   this->ScalarBarActor->RenderOpaqueGeometry(viewport);
495   // RKV : Begin
496   if (this->DistributionVisibility)
497     this->DistributionActor->RenderOpaqueGeometry(viewport);
498   // RKV : End
499   for (i=0; i<this->NumberOfLabels; i++)
500     {
501     renderedSomething += this->TextActors[i]->RenderOpaqueGeometry(viewport);
502     }
503
504   renderedSomething = (renderedSomething > 0)?(1):(0);
505
506   return renderedSomething;
507 }
508
509 void VISU_ScalarBarActor::PrintSelf(ostream& os, vtkIndent indent)
510 {
511   this->Superclass::PrintSelf(os,indent);
512
513   if ( this->LookupTable )
514     {
515     os << indent << "Lookup Table:\n";
516     this->LookupTable->PrintSelf(os,indent.GetNextIndent());
517     }
518   else
519     {
520     os << indent << "Lookup Table: (none)\n";
521     }
522
523   if (this->TitleTextProperty)
524     {
525     os << indent << "Title Text Property:\n";
526     this->TitleTextProperty->PrintSelf(os,indent.GetNextIndent());
527     }
528   else
529     {
530     os << indent << "Title Text Property: (none)\n";
531     }
532
533   if (this->LabelTextProperty)
534     {
535     os << indent << "Label Text Property:\n";
536     this->LabelTextProperty->PrintSelf(os,indent.GetNextIndent());
537     }
538   else
539     {
540     os << indent << "Label Text Property: (none)\n";
541     }
542
543   os << indent << "Title: " << (this->Title ? this->Title : "(none)") << "\n";
544   os << indent << "Maximum Number Of Colors: " 
545      << this->MaximumNumberOfColors << "\n";
546   os << indent << "Number Of Labels: " << this->NumberOfLabels << "\n";
547   os << indent << "Number Of Labels Built: " << this->NumberOfLabelsBuilt << "\n";
548
549   os << indent << "Orientation: ";
550   if ( this->Orientation == VTK_ORIENT_HORIZONTAL )
551     {
552     os << "Horizontal\n";
553     }
554   else
555     {
556     os << "Vertical\n";
557     }
558
559   os << indent << "Label Format: " << this->LabelFormat << "\n";
560 }
561
562 void VISU_ScalarBarActor::ShallowCopy(vtkProp *prop)
563 {
564   VISU_ScalarBarActor *a = VISU_ScalarBarActor::SafeDownCast(prop);
565   if ( a != NULL )
566     {
567     this->SetPosition2(a->GetPosition2());
568     this->SetLookupTable(a->GetLookupTable());
569     this->SetMaximumNumberOfColors(a->GetMaximumNumberOfColors());
570     this->SetOrientation(a->GetOrientation());
571     this->SetLabelTextProperty(a->GetLabelTextProperty());
572     this->SetTitleTextProperty(a->GetTitleTextProperty());
573     this->SetLabelFormat(a->GetLabelFormat());
574     this->SetTitle(a->GetTitle());
575     this->GetPositionCoordinate()->SetCoordinateSystem(
576       a->GetPositionCoordinate()->GetCoordinateSystem());    
577     this->GetPositionCoordinate()->SetValue(
578       a->GetPositionCoordinate()->GetValue());
579     this->GetPosition2Coordinate()->SetCoordinateSystem(
580       a->GetPosition2Coordinate()->GetCoordinateSystem());    
581     this->GetPosition2Coordinate()->SetValue(
582       a->GetPosition2Coordinate()->GetValue());
583     }
584
585   // Now do superclass
586   this->vtkActor2D::ShallowCopy(prop);
587 }
588
589 void VISU_ScalarBarActor::AllocateAndSizeLabels(int *labelSize, int *size,
590                                               vtkViewport *viewport,
591                                               vtkFloatingPointType *range)
592 {
593   labelSize[0] = labelSize[1] = 0;
594
595   this->TextMappers = new vtkTextMapper * [this->NumberOfLabels];
596   this->TextActors = new vtkActor2D * [this->NumberOfLabels];
597
598   char string[512];
599
600   vtkFloatingPointType val;
601   int i;
602   
603   // TODO: this should be optimized, maybe by keeping a list of
604   // allocated mappers, in order to avoid creation/destruction of
605   // their underlying text properties (i.e. each time a mapper is
606   // created, text properties are created and shallow-assigned a font size
607   // which value might be "far" from the target font size).
608
609   VISU_LookupTable *lut = this->LookupTable; //SALOME specific
610   int isLogTable = lut->GetScale() == VTK_SCALE_LOG10;
611
612   for (i=0; i < this->NumberOfLabels; i++)
613     {
614     this->TextMappers[i] = vtkTextMapper::New();
615
616     if(isLogTable && 0 < i && i < this->NumberOfLabels - 1){ // SALOME specific
617       vtkFloatingPointType lval = log10(range[0]) + (vtkFloatingPointType)i/(this->NumberOfLabels-1) *
618         (log10(range[1])-log10(range[0]));
619       val = pow((double)10,(double)lval);
620     }else{
621       val = range[0] + (vtkFloatingPointType)i/(this->NumberOfLabels-1) * (range[1]-range[0]);
622     }
623     sprintf(string, this->LabelFormat, val);
624     this->TextMappers[i]->SetInput(string);
625
626     // Shallow copy here so that the size of the label prop is not affected
627     // by the automatic adjustment of its text mapper's size (i.e. its
628     // mapper's text property is identical except for the font size
629     // which will be modified later). This allows text actors to
630     // share the same text property, and in that case specifically allows
631     // the title and label text prop to be the same.
632     this->TextMappers[i]->GetTextProperty()->ShallowCopy(
633       this->LabelTextProperty);
634
635     this->TextActors[i] = vtkActor2D::New();
636     this->TextActors[i]->SetMapper(this->TextMappers[i]);
637     this->TextActors[i]->SetProperty(this->GetProperty());
638     this->TextActors[i]->GetPositionCoordinate()->
639       SetReferenceCoordinate(this->PositionCoordinate);
640     }
641
642   if (this->NumberOfLabels)
643     {
644     int targetWidth, targetHeight;
645
646     if(LabelRatioWidth == 0)
647       if ( this->Orientation == VTK_ORIENT_VERTICAL )
648           targetWidth = (int)(0.6*size[0]);
649         else
650           targetWidth = (int)(size[0]*0.8/this->NumberOfLabels);
651     else
652       targetWidth = (int)(0.01*LabelRatioWidth*size[0]);
653
654     if ( this->Orientation == VTK_ORIENT_VERTICAL )
655       targetHeight = (int)(0.86*size[1]/this->NumberOfLabels);
656     else
657       targetHeight = (int)(0.25*size[1]);
658
659     vtkTextMapper::SetMultipleConstrainedFontSize(viewport, 
660                                                   targetWidth, 
661                                                   targetHeight,
662                                                   this->TextMappers,
663                                                   this->NumberOfLabels,
664                                                   labelSize);
665     }
666 }
667
668 void VISU_ScalarBarActor::SizeTitle(int *titleSize, int *size, 
669                                   vtkViewport *viewport)
670 {
671   titleSize[0] = titleSize[1] = 0;
672
673   if (this->Title == NULL || !strlen(this->Title))
674     {
675     return;
676     }
677
678   int targetWidth, targetHeight;
679   
680   if(TitleRatioSize == 0)
681     targetWidth = size[0];
682   else
683     targetWidth = (int)(0.01*TitleRatioSize*size[0]);
684
685   if ( this->Orientation == VTK_ORIENT_VERTICAL )
686     targetHeight = (int)(0.1*size[1]);
687   else
688     targetHeight = (int)(0.25*size[1]);
689
690   this->TitleMapper->SetConstrainedFontSize(
691     viewport, targetWidth, targetHeight);
692
693   this->TitleMapper->GetSize(viewport, titleSize);
694 }
695
696 // RKV : Begin
697 void VISU_ScalarBarActor::SetDistributionVisibility(int v) 
698 {
699   this->DistributionVisibility = v;
700   if (v) {
701         this->DistributionActor->VisibilityOn();
702   } else {
703         this->DistributionActor->VisibilityOff();
704   }
705 }
706
707 void VISU_ScalarBarActor::SetDistribution(vtkDoubleArray *distr) 
708 {
709   this->Distribution = distr;
710   if (distr == NULL) return;
711   
712   this->DistributionObj->Initialize();
713   this->DistributionObj->GetFieldData()->AddArray(this->Distribution);
714   this->DistributionActor->AddDataObjectInput(this->DistributionObj);
715   // Set ranges of axes for the distribution curve
716   this->DistributionActor->SetXRange(0, this->Distribution->GetNumberOfTuples()-1);
717   double range[2];
718   this->Distribution->GetRange(range);
719   int aNbVals = this->Distribution->GetNumberOfTuples();
720 //  int total = 0;
721   if (this->GetDebug()) {
722   for(vtkIdType aValId = 0; aValId < aNbVals; aValId++){
723 //      if (this->GetDebug()) {
724                 if (this->Distribution->GetValue(aValId) > 0)
725               vtkDebugMacro(<< "D(" << aValId << ") = " << this->Distribution->GetValue(aValId));
726 //      }
727 //      total += this->Distribution->GetValue(aValId);
728   }
729   }
730 //  this->DistributionActor->SetYRange(0, total);
731   this->DistributionActor->SetYRange(0, range[1]);
732   vtkDebugMacro(<< "max X = " << this->Distribution->GetNumberOfTuples());
733   vtkDebugMacro(<< "Y = (" << range[0] << ", " << range[1] << ")");
734 //  vtkDebugMacro(<< "total = " << total);
735 }
736
737 void VISU_ScalarBarActor::DebugOn() {
738         this->DistributionActor->DebugOn();
739         Superclass::DebugOn();
740
741
742 void VISU_ScalarBarActor::DebugOff() {
743         this->DistributionActor->DebugOff();
744         Superclass::DebugOff();
745
746 // RKV : End
747
748 void VISU_ScalarBarActor::SetRatios(int titleRatioSize, int labelRatioWidth, 
749                                     int barRatioWidth, int barRatioHeight)
750 {
751   TitleRatioSize=titleRatioSize;
752   if(TitleRatioSize>100)
753     TitleRatioSize=100;
754   else if(TitleRatioSize<0)
755     TitleRatioSize=0;
756
757   LabelRatioWidth=labelRatioWidth;
758   if(LabelRatioWidth>100)
759     LabelRatioWidth=100;
760   else if(LabelRatioWidth<0)
761     LabelRatioWidth=0;
762
763   BarRatioWidth=barRatioWidth;
764   if(BarRatioWidth>100)
765     BarRatioWidth=100;
766   else if(BarRatioWidth<0)
767     BarRatioWidth=0;
768
769   BarRatioHeight=barRatioHeight;
770   if(BarRatioHeight>100)
771     BarRatioHeight=100;
772   else if(BarRatioHeight<0)
773     BarRatioHeight=0;
774 }
775
776 void VISU_ScalarBarActor::GetRatios(int& titleRatioSize, int& labelRatioWidth, 
777                                     int& barRatioWidth, int& barRatioHeight)
778 {
779   titleRatioSize=TitleRatioSize;
780   labelRatioWidth=LabelRatioWidth;
781   barRatioWidth=BarRatioWidth;
782   barRatioHeight=BarRatioHeight;
783 }
784
785 void VISU_ScalarBarActor::SizeBar(int& barSizeWidth, int& barSizeHeight, int *size,
786                                   vtkViewport *viewport, vtkFloatingPointType *range)
787 {
788   if(BarRatioWidth == 0)
789     if ( this->Orientation == VTK_ORIENT_VERTICAL )
790       {
791         int labelSize[2];
792         this->AllocateAndSizeLabels(labelSize, size, viewport,range);
793         barSizeWidth = size[0] - 4 - labelSize[0];
794       } else
795         barSizeWidth = size[0];
796   else
797     barSizeWidth = (int)(0.01*BarRatioWidth*size[0]);
798
799   if(BarRatioHeight == 0)
800     if ( this->Orientation == VTK_ORIENT_VERTICAL )
801       barSizeHeight = (int)(0.86*size[1]);
802     else
803       barSizeHeight = (int)(0.4*size[1]);
804   else
805     barSizeHeight = (int)(0.01*BarRatioHeight*size[1]);
806 }
807 // RKV : Begin
808 //------------------------------------------------------------------------------
809 /** Place the distribution plot actor in the viewport according to the 
810  * scalar bar location and orientation */
811 void VISU_ScalarBarActor::PlaceDistribution(vtkViewport *viewport, const int barWidth, const int barHeight) {
812     vtkDebugMacro(<< "barOrigin[0]=" << this->LastOrigin[0] << "; barOrigin[1]=" << this->LastOrigin[1]);
813     // Detect the side of the viewport where the curve should be placed by the bar origin.
814     double u = (double)(this->LastOrigin[0]), v = (double)(this->LastOrigin[1]), z=0;
815     viewport->ViewportToNormalizedViewport(u, v);
816     
817     if ( this->Orientation == VTK_ORIENT_VERTICAL ) {
818       // Position the distribution curve vertically
819       if (u > 0.5) {
820         // X - UP, Y - TO THE LEFT
821         this->DistributionActor->SetPlotLocation(VISU_XYPLOT_RIGHT);
822         // Curve to be placed on the left side of the bar
823         vtkDebugMacro(<< "Curve to be placed on the left side of the bar");
824         // relative to the bar origin
825         u = 0;
826         v = 0;
827         viewport->ViewportToNormalizedViewport(u, v);
828         vtkDebugMacro(<< "u=" << u << "; v=" << v);
829         this->DistributionActor->GetPositionCoordinate()->SetValue(u, v, 0);
830         // relative to Position
831         u = - barWidth;
832         v = barHeight;
833         viewport->ViewportToNormalizedViewport(u, v);
834         vtkDebugMacro("u2=" << u << "; v2=" << v);
835         this->DistributionActor->GetPosition2Coordinate()->SetValue(u, v, 0);
836       } else {
837         // X - UP, Y - TO THE RIGHT
838         this->DistributionActor->SetPlotLocation(VISU_XYPLOT_LEFT);
839         // Curve to be placed on the right side of the bar
840         vtkDebugMacro(<< "Curve to be placed on the right side of the bar");
841         // relative to the bar origin
842         u = barWidth;
843         v = 0;
844         viewport->ViewportToNormalizedViewport(u, v);
845         vtkDebugMacro(<< "u=" << u << "; v=" << v);
846         this->DistributionActor->GetPositionCoordinate()->SetValue(u, v, 0);
847         // relative to Position
848         u = barWidth;
849         v = barHeight;
850         viewport->ViewportToNormalizedViewport(u, v);
851         vtkDebugMacro("u2=" << u << "; v2=" << v);
852         this->DistributionActor->GetPosition2Coordinate()->SetValue(u, v, 0);
853       }
854     } else {
855       // Position the distribution curve horizontally
856       if (v > 0.5) {
857         // X - TO THE LEFT, Y - DOWN
858         this->DistributionActor->SetPlotLocation(VISU_XYPLOT_TOP);
859         // Curve to be placed below the bar
860         vtkDebugMacro(<< "Curve to be placed below the bar");
861         // relative to the bar origin
862         u = 0;
863         v = 0;
864         viewport->ViewportToNormalizedViewport(u, v);
865         vtkDebugMacro(<< "u=" << u << "; v=" << v);
866         this->DistributionActor->GetPositionCoordinate()->SetValue(u, v, 0);
867         // relative to Position
868         u = barWidth;
869         v = - barHeight;
870         viewport->ViewportToNormalizedViewport(u, v);
871         vtkDebugMacro("u2=" << u << "; v2=" << v);
872         this->DistributionActor->GetPosition2Coordinate()->SetValue(u, v, 0);
873       } else {
874         // X - TO THE RIGHT, Y - UP
875         this->DistributionActor->SetPlotLocation(VISU_XYPLOT_BOTTOM);
876         // Curve to be placed on the top of the bar
877         vtkDebugMacro(<< "Curve to be placed on the top of the bar");
878         // relative to the bar origin
879         u = 0;
880         v = barHeight;
881         viewport->ViewportToNormalizedViewport(u, v);
882         vtkDebugMacro(<< "u=" << u << "; v=" << v);
883         this->DistributionActor->GetPositionCoordinate()->SetValue(u, v, 0);
884         // relative to Position
885         u = barWidth;
886         v = barHeight;
887         viewport->ViewportToNormalizedViewport(u, v);
888         vtkDebugMacro("u2=" << u << "; v2=" << v);
889         this->DistributionActor->GetPosition2Coordinate()->SetValue(u, v, 0);
890       }
891     }
892 }
893 // RKV : End
894