Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / OBJECT / VISU_GaussPtsDeviceActor.cxx
1 //  SMESH OBJECT : interactive object for SMESH visualization
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   :
25 //  Author : 
26 //  Module :
27 //  $Header$
28
29
30 #include "VISU_GaussPtsDeviceActor.h"
31
32 #include "VISU_GaussPointsPL.hxx"
33 #include "VISU_OpenGLPointSpriteMapper.hxx"
34
35 #include "VTKViewer_Transform.h"
36 #include "VTKViewer_TransformFilter.h"
37 #include "VTKViewer_PassThroughFilter.h"
38
39 #include <vtkObjectFactory.h>
40 #include <vtkPolyData.h>
41 #include <vtkRenderer.h>
42 #include <vtkTextProperty.h>
43 #include <vtkProperty.h>
44 #include <vtkTexture.h>
45
46 #include "utilities.h"
47
48 #ifdef _DEBUG_
49 static int MYDEBUG = 0;
50 #else
51 static int MYDEBUG = 0;
52 #endif
53
54 using namespace std;
55
56
57 //----------------------------------------------------------------
58 vtkStandardNewMacro(VISU_GaussPtsDeviceActor);
59
60
61 VISU_GaussPtsDeviceActor
62 ::VISU_GaussPtsDeviceActor():
63   myGeomFilter(VTKViewer_GeometryFilter::New()),
64   myTransformFilter(VTKViewer_TransformFilter::New())
65 {
66   if(MYDEBUG) MESSAGE("VISU_GaussPtsDeviceActor - "<<this);
67
68   myGeomFilter->Delete();
69   myTransformFilter->Delete();
70
71   for(int i = 0; i < 3; i++){
72     PPassThroughFilter aFilter(VTKViewer_PassThroughFilter::New());
73     myPassFilter.push_back(aFilter);
74     aFilter->Delete();
75   }
76 }
77
78
79 VISU_GaussPtsDeviceActor
80 ::~VISU_GaussPtsDeviceActor()
81 {
82   if(MYDEBUG) MESSAGE("~VISU_GaussPtsDeviceActor - "<<this);
83 }
84
85
86 //----------------------------------------------------------------
87 void
88 VISU_GaussPtsDeviceActor
89 ::Render(vtkRenderer *ren, vtkMapper *vtkNotUsed(m))
90 {
91   if (this->Mapper == NULL)
92     {
93     vtkErrorMacro("No mapper for actor.");
94     return;
95     }
96   
97   // render the property
98   if (!this->Property)
99     {
100     // force creation of a property
101     this->GetProperty();
102     }
103   this->Property->Render(this, ren);
104   if (this->BackfaceProperty)
105     {
106     this->BackfaceProperty->BackfaceRender(this, ren);
107     this->Device->SetBackfaceProperty(this->BackfaceProperty);
108     }
109   this->Device->SetProperty(this->Property);  
110   
111   // render the texture
112   if (this->Texture)
113     {
114     this->Texture->Render(ren);
115     }
116   
117   // make sure the device has the same matrix
118   vtkMatrix4x4 *matrix = this->Device->GetUserMatrix();
119   this->GetMatrix(matrix);
120   
121   this->Device->Render(ren,Mapper);  
122   this->EstimatedRenderTime = Mapper->GetTimeToDraw();
123 }
124
125
126 //----------------------------------------------------------------
127 void
128 VISU_GaussPtsDeviceActor
129 ::AddToRender(vtkRenderer* theRenderer)
130 {
131   theRenderer->AddActor(this);
132 }
133
134 void 
135 VISU_GaussPtsDeviceActor
136 ::RemoveFromRender(vtkRenderer* theRenderer)
137 {
138   theRenderer->RemoveActor(this);
139 }
140
141 void
142 VISU_GaussPtsDeviceActor
143 ::SetTransform(VTKViewer_Transform* theTransform)
144 {
145   myTransformFilter->SetTransform(theTransform);
146 }
147
148 VISU_OpenGLPointSpriteMapper*
149 VISU_GaussPtsDeviceActor
150 ::GetPSMapper()
151 {
152   return myMapper.GetPointer();
153 }
154
155
156 //----------------------------------------------------------------------------
157 void
158 VISU_GaussPtsDeviceActor
159 ::SetPipeLine(VISU_GaussPointsPL* thePipeLine) 
160 {
161   myPipeLine = thePipeLine;
162   myMapper = thePipeLine->GetPSMapper();
163   vtkPolyData* aDataSet = myMapper->GetInput();
164
165   int anId = 0;
166   myPassFilter[ anId ]->SetInput( aDataSet ); 
167   myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
168   
169   anId++;
170   myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
171   
172   anId++;
173   myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
174   
175   myMapper->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
176   
177   Superclass::SetMapper( myMapper.GetPointer() );
178 }
179
180 VISU_GaussPointsPL* 
181 VISU_GaussPtsDeviceActor
182 ::GetPipeLine() 
183
184   return myPipeLine.GetPointer();
185 }
186
187 void
188 VISU_GaussPtsDeviceActor
189 ::ShallowCopyPL(VISU_GaussPointsPL* thePipeLine)
190 {
191   myPipeLine->ShallowCopy(thePipeLine);
192 }
193
194
195 //============================================================================
196 #include <vtkActor.h>
197 #include <vtkProperty.h>
198 #include <vtkConeSource.h>
199 #include <vtkAppendPolyData.h>
200 #include <vtkPolyDataMapper.h>
201
202
203 //----------------------------------------------------------------------------
204 vtkStandardNewMacro(VISU_CursorPyramid);
205
206 //----------------------------------------------------------------------------
207 VISU_CursorPyramid
208 ::VISU_CursorPyramid():
209   myHeight(10.0),
210   myRadius(5.0),
211   myMagnification(1.0),
212   myClamp(256.0),
213   myNbCones(6),
214   myAppendFilter(vtkAppendPolyData::New()),
215   myMapper(vtkPolyDataMapper::New())
216 {
217   myAppendFilter->Delete();
218   myMapper->Delete();
219
220   myMapper->SetInput(myAppendFilter->GetOutput());
221
222   for(int i = 0; i < myNbCones; i++){
223     vtkConeSource* aConeSource = vtkConeSource::New();
224     myAppendFilter->AddInput(aConeSource->GetOutput());
225     aConeSource->SetResolution(4);
226     mySources[i] = aConeSource;
227     aConeSource->Delete();
228   }
229 }
230
231
232 //----------------------------------------------------------------------------
233 void
234 VISU_CursorPyramid
235 ::Render(vtkRenderer *ren, vtkMapper *vtkNotUsed(m))
236 {
237   if (ren == NULL)
238     {
239     vtkErrorMacro("No mapper for actor.");
240     return;
241     }
242
243   this->SetScale(1.0);
244   float aMRadius = myRadius*myMagnification;
245   Init(myHeight,aMRadius*myCursorSize);
246
247   if(myClamp > 0.0f){
248     float aPoint1[3] = {0.0, 0.0, 0.0};
249     ren->SetDisplayPoint(aPoint1);
250     ren->DisplayToWorld();
251     ren->GetWorldPoint(aPoint1);
252
253     float aPoint2[3] = {0.0, myClamp, 0.0};
254     ren->SetDisplayPoint(aPoint2);
255     ren->DisplayToWorld();
256     ren->GetWorldPoint(aPoint2);
257     
258     float aWorldClamp = 
259       (aPoint2[0] - aPoint1[0])*(aPoint2[0] - aPoint1[0]) + 
260       (aPoint2[1] - aPoint1[1])*(aPoint2[1] - aPoint1[1]) + 
261       (aPoint2[2] - aPoint1[2])*(aPoint2[2] - aPoint1[2]); 
262     
263     aWorldClamp = sqrt(aWorldClamp);
264     float aMDiameter = 2.0 * aMRadius;
265     float aCoeff = aWorldClamp / aMDiameter;
266
267     if(aCoeff < 1.0){
268       this->SetScale(aCoeff);
269       //Init(myHeight/aCoeff,aMRadius*aCoeff);
270     }
271   }
272
273   if (this->Mapper == NULL)
274     {
275     vtkErrorMacro("No mapper for actor.");
276     return;
277     }
278   
279   // render the property
280   if (!this->Property)
281     {
282     // force creation of a property
283     this->GetProperty();
284     }
285   this->Property->Render(this, ren);
286   if (this->BackfaceProperty)
287     {
288     this->BackfaceProperty->BackfaceRender(this, ren);
289     this->Device->SetBackfaceProperty(this->BackfaceProperty);
290     }
291   this->Device->SetProperty(this->Property);  
292   
293   // render the texture
294   if (this->Texture)
295     {
296     this->Texture->Render(ren);
297     }
298   
299   // make sure the device has the same matrix
300   vtkMatrix4x4 *matrix = this->Device->GetUserMatrix();
301   this->GetMatrix(matrix);
302   
303   this->Device->Render(ren,Mapper);  
304   this->EstimatedRenderTime = Mapper->GetTimeToDraw();
305 }
306
307
308 //----------------------------------------------------------------------------
309 void
310 VISU_CursorPyramid
311 ::AddToRender(vtkRenderer* theRenderer)
312
313   theRenderer->AddActor(this);
314 }
315
316 void
317 VISU_CursorPyramid
318 ::RemoveFromRender(vtkRenderer* theRenderer)
319
320   theRenderer->RemoveActor(this);
321 }
322
323 //----------------------------------------------------------------------------
324 void 
325 VISU_CursorPyramid
326 ::SetPreferences(float theHeight,
327                  float theCursorSize)
328 {
329   Init(theHeight, theCursorSize, myRadius, myMagnification, myClamp, GetPosition(), GetProperty()->GetColor());
330 }
331
332 //----------------------------------------------------------------------------
333 void
334 VISU_CursorPyramid
335 ::Init(float theHeight,
336        float theCursorSize,
337        float theRadius,
338        float theMagnification,
339        float theClamp,
340        float thePos[3],
341        float theColor[3])
342 {
343   Init(theHeight,theRadius*theMagnification*theCursorSize);
344   SetPosition(thePos[0],thePos[1],thePos[2]);
345   GetProperty()->SetColor(theColor);
346   SetMapper(myMapper.GetPointer());
347
348   myHeight = theHeight;
349   myCursorSize = theCursorSize;
350
351   myRadius = theRadius;
352   myMagnification = theMagnification;
353
354   myClamp = theClamp;
355 }
356
357
358 void
359 VISU_CursorPyramid
360 ::Init(float theHeight,
361        float theRadius)
362 {
363   for(int i = 0; i < myNbCones; ++i){
364     vtkConeSource* aSource = mySources[i].GetPointer();
365     aSource->SetHeight(theHeight);
366     // Set the angle of the cone. As a side effect, the angle plus height sets 
367     // the base radius of the cone.
368     aSource->SetAngle(20.0);
369   }
370   
371   float aDisplacement = -0.5*theHeight - theRadius;
372   
373   // X
374   mySources[0]->SetDirection(1.0, 0.0, 0.0);
375   mySources[0]->SetCenter(aDisplacement, 0.0, 0.0);
376
377   // It is impossible to inverse direction around X axis (VTK bug)
378   mySources[1]->SetDirection(-VTK_LARGE_FLOAT, 1.0, 0.0); // A workaround
379   mySources[1]->SetCenter(-aDisplacement, 0.0, 0.0);
380   
381   // Y
382   mySources[2]->SetDirection(0.0, 1.0, 0.0);
383   mySources[2]->SetCenter(0.0, aDisplacement, 0.0);
384   
385   mySources[3]->SetDirection(0.0, -1.0, 0.0); 
386   mySources[3]->SetCenter(0.0, -aDisplacement, 0.0);
387   
388   // Z
389   mySources[4]->SetDirection(0.0, 0.0, 1.0); 
390   mySources[4]->SetCenter(0.0, 0.0, aDisplacement);
391   
392   mySources[5]->SetDirection(0.0, 0.0, -1.0); 
393   mySources[5]->SetCenter(0.0, 0.0, -aDisplacement);
394 }
395
396
397 //----------------------------------------------------------------------------
398 #include <vtkViewport.h>
399 #include <vtkWindow.h>
400 #include <vtkProp.h>
401 #include <vtkPolyData.h>
402 #include <vtkPolyDataMapper2D.h>
403 #include <vtkActor2D.h>
404 #include <vtkTimeStamp.h>
405 #include <vtkTextProperty.h>
406 #include <vtkTextActor.h>
407 #include <vtkTextMapper.h>
408 #include <vtkPoints.h>
409 #include <vtkCellArray.h>
410 #include <vtkProperty2D.h>
411 //==================================================================
412 vtkCxxRevisionMacro(VISU_FramedTextActor, "$Revision$");
413 vtkStandardNewMacro(VISU_FramedTextActor);
414
415 //==================================================================
416 // function : VISU_FramedTextActor
417 // purpose  :
418 //==================================================================
419 VISU_FramedTextActor::VISU_FramedTextActor()
420 {
421   PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
422
423   myTransparency=0.;
424   myBar = vtkPolyData::New();
425   myBarMapper = vtkPolyDataMapper2D::New();
426   myBarMapper->SetInput(myBar);
427   myBarActor = vtkActor2D::New();
428   myBarActor->SetMapper(myBarMapper);
429   myBarActor->GetProperty()->SetOpacity(1.-myTransparency);  
430   myBarActor->GetProperty()->SetColor(.5, .5, .5); 
431   //
432   myTextProperty = vtkTextProperty::New();
433   myTextProperty->SetFontSize(12);
434   myTextProperty->SetBold(0);
435   myTextProperty->SetItalic(0);
436   myTextProperty->SetShadow(1);
437   myTextProperty->SetFontFamilyToArial();
438   //
439   myTextMapper=vtkTextMapper::New();
440   myTextMapper->SetInput("some text");
441   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
442   myTextActor=vtkActor2D::New();
443   myTextActor->SetMapper(myTextMapper);
444   //
445   myBarActor->SetVisibility(1);
446   myTextActor->SetVisibility(1);
447   myBarActor->SetPickable(0);
448   myTextActor->SetPickable(0);
449   //----------------------------------------------------------
450   myModePosition=0;// 0 -centered below the point
451                    // 1 -top left corner of the 3D view window
452   //
453   for(int i=0; i<4; i++) {
454     myWorldPoint[i] = 0.;
455   }
456   myDistance=10.;
457   //
458 }
459 //==================================================================
460 // function : ~
461 // purpose  :
462 //==================================================================
463 VISU_FramedTextActor::~VISU_FramedTextActor()
464 {
465   myTextActor->Delete();
466   myTextMapper->Delete();
467   myTextProperty->Delete();
468   myBarActor->Delete();
469   myBarMapper->Delete();
470   myBar->Delete();
471 }
472 //==================================================================
473 // function : SetVisibility
474 // purpose  :
475 //==================================================================
476 void VISU_FramedTextActor::SetVisibility (int theVisibility)
477 {
478   myBarActor->SetVisibility(theVisibility);
479   myTextActor->SetVisibility(theVisibility);
480 }
481 //==================================================================
482 // function : GetVisibility
483 // purpose  :
484 //==================================================================
485 int VISU_FramedTextActor::GetVisibility() 
486 {
487   return myBarActor->GetVisibility();
488 }
489 //==================================================================
490 // function : SetPickable
491 // purpose  :
492 //==================================================================
493 void VISU_FramedTextActor::SetPickable (int thePickability) 
494 {
495   myBarActor->SetPickable(thePickability);
496   myTextActor->SetPickable(thePickability);
497 }
498 //==================================================================
499 // function : GetPickable
500 // purpose  :
501 //==================================================================
502 int VISU_FramedTextActor::GetPickable()
503 {
504   return myBarActor->GetPickable();
505 }
506 //==================================================================
507 // function : SetTransparency
508 // purpose  :
509 //==================================================================
510 void VISU_FramedTextActor::SetTransparency(const float theTransparency)
511 {
512   if (theTransparency>=0.  && theTransparency<=1.){
513     myTransparency=theTransparency;
514     myBarActor->GetProperty()->SetOpacity(1.-myTransparency);  
515     Modified();
516   }
517 }
518 //==================================================================
519 // function : GetTransparency
520 // purpose  :
521 //==================================================================
522 float VISU_FramedTextActor::GetTransparency()const
523 {
524   return myTransparency;
525 }
526 //==================================================================
527 // function : SetText
528 // purpose  :
529 //==================================================================
530 void VISU_FramedTextActor::SetText(const char* theText)
531 {
532   myTextMapper->SetInput(theText); 
533   Modified();
534 }
535 //==================================================================
536 // function : GetText
537 // purpose  :
538 //==================================================================
539 char* VISU_FramedTextActor::GetText()
540 {
541   return myTextMapper->GetInput();
542 }
543 //==================================================================
544 // function : SetModePosition
545 // purpose  :
546 //==================================================================
547 void VISU_FramedTextActor::SetModePosition(const int theMode)
548 {
549   myModePosition=theMode;
550   Modified();
551 }
552 //==================================================================
553 // function : GetModePosition
554 // purpose  :
555 //==================================================================
556 int VISU_FramedTextActor::GetModePosition()const
557 {
558   return myModePosition;
559 }
560 //==================================================================
561 // function : SetWorldPoint
562 // purpose  :
563 //==================================================================
564 void VISU_FramedTextActor::SetWorldPoint(const float theWorldPoint[4])
565 {
566   for(int i = 0; i<4; ++i) {
567     myWorldPoint[i] = theWorldPoint[i];
568   } 
569   Modified();
570 }
571 //==================================================================
572 // function : GetWorldPoint
573 // purpose  :
574 //==================================================================
575 const float* VISU_FramedTextActor::GetWorldPoint()const 
576 {
577   return myWorldPoint;
578 }
579 //==================================================================
580 // function : SetDistance
581 // purpose  :
582 //==================================================================
583 void VISU_FramedTextActor::SetDistance(const float theDistance)
584 {
585   myDistance=theDistance;
586 }
587 //==================================================================
588 // function : GetDistance
589 // purpose  :
590 //==================================================================
591 float VISU_FramedTextActor::GetDistance()const
592 {
593   return myDistance;
594 }
595 //==================================================================
596 // function : ReleaseGraphicsResources
597 // purpose  :
598 //==================================================================
599 void VISU_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
600 {
601   myTextActor->ReleaseGraphicsResources(win);
602   myBarActor->ReleaseGraphicsResources(win);
603 }
604 //==================================================================
605 // function : RenderOverlay
606 // purpose  :
607 //==================================================================
608 int VISU_FramedTextActor::RenderOverlay(vtkViewport *viewport)
609 {
610   int renderedSomething = 0;
611   myBarActor->RenderOverlay(viewport);
612   renderedSomething +=myTextActor->RenderOverlay(viewport);
613   return renderedSomething;
614 }
615 //==================================================================
616 // function : RenderOpaqueGeometry
617 // purpose  :
618 //==================================================================
619 int 
620 VISU_FramedTextActor
621 ::RenderOpaqueGeometry(vtkViewport *theViewport)
622 {
623   int anIsRenderedSomething = 0;
624   int* aViewportSize = theViewport->GetSize();
625   if(aViewportSize[0] == 1 || aViewportSize[1] == 1)
626     return anIsRenderedSomething;
627
628   myBar->Initialize();
629
630   int aNbPoints = 4;
631   vtkPoints *aPoints = vtkPoints::New();
632   aPoints->SetNumberOfPoints(aNbPoints);
633   myBar->SetPoints(aPoints);
634   aPoints->Delete();
635
636   vtkCellArray *aPolys = vtkCellArray::New();
637   aPolys->Allocate(aPolys->EstimateSize(1,4));
638   vtkIdType aPointsIds[4] = {0, 1, 3, 2};
639   aPolys->InsertNextCell(4,aPointsIds);
640   myBar->SetPolys(aPolys);
641   aPolys->Delete(); 
642
643   int aTextSize[2]; 
644   myTextMapper->GetSize(theViewport, aTextSize);
645   int aBarWidth = aTextSize[0];
646   int aBarHeight = aTextSize[1];
647
648   if (myModePosition==0) {
649     theViewport->SetWorldPoint(myWorldPoint);
650     theViewport->WorldToDisplay();
651
652     float aSelectionPoint[3];
653     theViewport->GetDisplayPoint(aSelectionPoint);
654     float u = aSelectionPoint[0];
655     float v = aSelectionPoint[1] - myDistance;
656     theViewport->ViewportToNormalizedViewport(u, v);
657     PositionCoordinate->SetValue(u, v);
658     //
659     myTextProperty->SetJustificationToCentered();
660     myTextProperty->SetVerticalJustificationToTop();
661     //
662     aBarWidth /= 2;
663     aPoints->SetPoint(0, -aBarWidth,         0.0, 0.0);
664     aPoints->SetPoint(1, -aBarWidth, -aBarHeight, 0.0);
665     aPoints->SetPoint(2,  aBarWidth,         0.0, 0.0);
666     aPoints->SetPoint(3,  aBarWidth, -aBarHeight, 0.0);
667   }
668   else {//if (myModePosition==1) {
669     PositionCoordinate->SetValue(0.0, 1.0);
670     myTextProperty->SetJustificationToLeft();
671     myTextProperty->SetVerticalJustificationToTop();  
672     //
673     aPoints->SetPoint(0,        0.0,         0.0, 0.0);
674     aPoints->SetPoint(1,        0.0, -aBarHeight, 0.0);
675     aPoints->SetPoint(2,  aBarWidth,         0.0, 0.0);
676     aPoints->SetPoint(3,  aBarWidth, -aBarHeight, 0.0);
677   }
678   //
679   myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
680   myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
681   myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
682   //
683   myBuildTime.Modified();
684   //
685   return anIsRenderedSomething;
686 }