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