1 // SMESH OBJECT : interactive object for SMESH visualization
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
30 #include "VISU_GaussPtsDeviceActor.h"
32 #include "VISU_GaussPointsPL.hxx"
33 #include "VISU_OpenGLPointSpriteMapper.hxx"
35 #include "VTKViewer_Transform.h"
36 #include "VTKViewer_TransformFilter.h"
38 #include <vtkObjectFactory.h>
39 #include <vtkPolyData.h>
40 #include <vtkRenderer.h>
41 #include <vtkTextProperty.h>
42 #include <vtkProperty.h>
43 #include <vtkTexture.h>
44 #include <vtkPassThroughFilter.h>
45 #include <vtkImageData.h>
47 #include <qfileinfo.h>
49 #include "utilities.h"
50 #include "VISU_PipeLineUtils.hxx"
53 static int MYDEBUG = 0;
55 static int MYDEBUG = 0;
59 //----------------------------------------------------------------
64 Image2VTI(const std::string& theImageFileName)
66 QFileInfo aFileInfo(theImageFileName.c_str());
67 QString aFormat = aFileInfo.extension(FALSE);
69 QString aTmpDir = getenv( "TEMP" );
71 QString aTmpDir = QString( "/tmp/" ) + getenv("USER");
73 QString aVTIName = aTmpDir + "-" + aFileInfo.baseName(TRUE) + ".vti";
74 QString aCommand = QString( "VISU_img2vti " ) + aFormat + " " + theImageFileName + " " + aVTIName;
76 if(system( aCommand.latin1() ) == 0)
77 return aVTIName.latin1();
84 RemoveFile(const std::string& theFileName)
86 if( theFileName != "" ){
88 QString aCommand = QString( "rm -fr " ) + theFileName.c_str();
90 QString aCommand = QString( "del /F " ) + theFileName.c_str();
92 system( aCommand.latin1() );
98 GetTexture(const std::string& theMainTexture,
99 const std::string& theAlphaTexture)
101 typedef std::pair<std::string,std::string> TTextureKey;
102 typedef std::map<TTextureKey,TTextureValue> TTextureMap;
104 static TTextureMap aTextureMap;
106 TTextureValue aTextureValue;
107 TTextureKey aTextureKey( theMainTexture.c_str(), theAlphaTexture.c_str() );
108 TTextureMap::const_iterator anIter = aTextureMap.find( aTextureKey );
109 if ( anIter != aTextureMap.end() ) {
110 aTextureValue = anIter->second;
112 QString aMainTextureVTI = Image2VTI(theMainTexture);
113 QString anAlphaTextureVTI = Image2VTI(theAlphaTexture);
115 if( !aMainTextureVTI.isNull() && !anAlphaTextureVTI.isNull() ){
117 VISU_GaussPointsPL::MakeTexture( aMainTextureVTI.latin1(),
118 anAlphaTextureVTI.latin1());
120 if( aTextureValue.GetPointer() )
121 aTextureMap[aTextureKey] = aTextureValue;
124 RemoveFile(aMainTextureVTI);
125 RemoveFile(anAlphaTextureVTI);
128 return aTextureValue;
133 //----------------------------------------------------------------
134 vtkStandardNewMacro(VISU_GaussDeviceActorBase);
137 VISU_GaussDeviceActorBase
138 ::VISU_GaussDeviceActorBase():
139 myTransformFilter(VTKViewer_TransformFilter::New())
141 if(MYDEBUG) MESSAGE("VISU_GaussDeviceActorBase - "<<this);
143 myTransformFilter->Delete();
145 for(int i = 0; i < 3; i++){
146 PPassThroughFilter aFilter(vtkPassThroughFilter::New());
147 myPassFilter.push_back(aFilter);
153 VISU_GaussDeviceActorBase
154 ::~VISU_GaussDeviceActorBase()
156 if(MYDEBUG) MESSAGE("~VISU_GaussDeviceActorBase - "<<this);
160 //----------------------------------------------------------------
162 VISU_GaussDeviceActorBase
163 ::Render(vtkRenderer *ren, vtkMapper *vtkNotUsed(m))
165 if (this->Mapper == NULL)
167 vtkErrorMacro("No mapper for actor.");
171 // render the property
174 // force creation of a property
177 this->Property->Render(this, ren);
178 if (this->BackfaceProperty)
180 this->BackfaceProperty->BackfaceRender(this, ren);
181 this->Device->SetBackfaceProperty(this->BackfaceProperty);
183 this->Device->SetProperty(this->Property);
185 // render the texture
188 this->Texture->Render(ren);
191 // make sure the device has the same matrix
192 vtkMatrix4x4 *matrix = this->Device->GetUserMatrix();
193 this->GetMatrix(matrix);
195 this->Device->Render(ren,Mapper);
196 this->EstimatedRenderTime = Mapper->GetTimeToDraw();
200 //----------------------------------------------------------------
202 VISU_GaussDeviceActorBase
203 ::SetTransform(VTKViewer_Transform* theTransform)
205 myTransformFilter->SetTransform(theTransform);
208 //----------------------------------------------------------------
210 VISU_GaussDeviceActorBase
211 ::SetPointSpriteMapper(VISU_OpenGLPointSpriteMapper* theMapper)
213 vtkPolyData* aDataSet = theMapper->GetInput();
214 myMapper = theMapper;
217 myPassFilter[ anId ]->SetInput( aDataSet );
218 myPassFilter[ anId + 1 ]->SetInput( myPassFilter[ anId ]->GetOutput() );
221 myTransformFilter->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
224 myPassFilter[ anId ]->SetInput( myTransformFilter->GetOutput() );
226 myMapper->SetInput( myPassFilter[ anId ]->GetPolyDataOutput() );
228 Superclass::SetMapper( theMapper );
232 VISU_GaussDeviceActorBase
233 ::DoMapperShallowCopy( vtkMapper* theMapper,
234 bool theIsCopyInput )
236 VISU::CopyMapper( GetMapper(), theMapper, theIsCopyInput );
239 VISU_OpenGLPointSpriteMapper*
240 VISU_GaussDeviceActorBase
241 ::GetPointSpriteMapper()
243 return myMapper.GetPointer();
246 //----------------------------------------------------------------------------
248 VISU_GaussDeviceActorBase
251 vtkDataSet* aDataSet = GetMapper()->GetInput();
252 return aDataSet->GetActualMemorySize() * 1024;
257 //----------------------------------------------------------------
258 vtkStandardNewMacro(VISU_GaussPtsDeviceActor);
261 VISU_GaussPtsDeviceActor
262 ::VISU_GaussPtsDeviceActor()
266 VISU_GaussPtsDeviceActor
267 ::~VISU_GaussPtsDeviceActor()
271 //----------------------------------------------------------------------------
273 VISU_GaussPtsDeviceActor
274 ::AddToRender(vtkRenderer* theRenderer)
276 theRenderer->AddActor(this);
280 VISU_GaussPtsDeviceActor
281 ::RemoveFromRender(vtkRenderer* theRenderer)
283 theRenderer->RemoveActor(this);
287 //----------------------------------------------------------------------------
289 VISU_GaussPtsDeviceActor
290 ::SetPipeLine(VISU_GaussPointsPL* thePipeLine)
292 SetPointSpriteMapper( thePipeLine->GetPointSpriteMapper() );
294 myPipeLine = thePipeLine;
298 VISU_GaussPtsDeviceActor
301 return myPipeLine.GetPointer();
305 VISU_GaussPtsDeviceActor
306 ::ShallowCopyPL(VISU_GaussPointsPL* thePipeLine)
308 myPipeLine->ShallowCopy(thePipeLine, true);
312 //----------------------------------------------------------------------------
314 VISU_GaussPtsDeviceActor
317 if(Superclass::GetPickable()){
318 if(vtkMapper* aMapper = GetMapper()){
319 if(vtkDataSet* aDataSet= aMapper->GetInput()){
321 return aDataSet->GetNumberOfCells() > 0;
330 //----------------------------------------------------------------------------
332 VISU_GaussPtsDeviceActor
335 unsigned long int aSize = Superclass::GetMemorySize();
337 aSize += GetPipeLine()->GetMemorySize();
344 //============================================================================
345 #include <vtkActor.h>
346 #include <vtkProperty.h>
347 #include <vtkConeSource.h>
348 #include <vtkAppendPolyData.h>
349 #include <vtkPolyDataMapper.h>
352 //----------------------------------------------------------------------------
353 vtkStandardNewMacro(VISU_CursorPyramid);
355 //----------------------------------------------------------------------------
357 ::VISU_CursorPyramid():
360 myMagnification(1.0),
363 myAppendFilter(vtkAppendPolyData::New()),
364 myMapper(vtkPolyDataMapper::New())
366 myAppendFilter->Delete();
369 myMapper->SetInput(myAppendFilter->GetOutput());
371 for(int i = 0; i < myNbCones; i++){
372 vtkConeSource* aConeSource = vtkConeSource::New();
373 myAppendFilter->AddInput(aConeSource->GetOutput());
374 aConeSource->SetResolution(4);
375 mySources[i] = aConeSource;
376 aConeSource->Delete();
381 //----------------------------------------------------------------------------
384 ::Render(vtkRenderer *ren, vtkMapper *vtkNotUsed(m))
388 vtkErrorMacro("No mapper for actor.");
393 vtkFloatingPointType aMRadius = myRadius*myMagnification;
394 Init(myHeight,aMRadius*myCursorSize);
397 vtkFloatingPointType aPoint1[3] = {0.0, 0.0, 0.0};
398 ren->SetDisplayPoint(aPoint1);
399 ren->DisplayToWorld();
400 ren->GetWorldPoint(aPoint1);
402 vtkFloatingPointType aPoint2[3] = {0.0, myClamp, 0.0};
403 ren->SetDisplayPoint(aPoint2);
404 ren->DisplayToWorld();
405 ren->GetWorldPoint(aPoint2);
407 vtkFloatingPointType aWorldClamp =
408 (aPoint2[0] - aPoint1[0])*(aPoint2[0] - aPoint1[0]) +
409 (aPoint2[1] - aPoint1[1])*(aPoint2[1] - aPoint1[1]) +
410 (aPoint2[2] - aPoint1[2])*(aPoint2[2] - aPoint1[2]);
412 aWorldClamp = sqrt(aWorldClamp);
413 vtkFloatingPointType aMDiameter = 2.0 * aMRadius;
414 vtkFloatingPointType aCoeff = aWorldClamp / aMDiameter;
417 this->SetScale(aCoeff);
418 //Init(myHeight/aCoeff,aMRadius*aCoeff);
422 if (this->Mapper == NULL)
424 vtkErrorMacro("No mapper for actor.");
428 // render the property
431 // force creation of a property
434 this->Property->Render(this, ren);
435 if (this->BackfaceProperty)
437 this->BackfaceProperty->BackfaceRender(this, ren);
438 this->Device->SetBackfaceProperty(this->BackfaceProperty);
440 this->Device->SetProperty(this->Property);
442 // render the texture
445 this->Texture->Render(ren);
448 // make sure the device has the same matrix
449 vtkMatrix4x4 *matrix = this->Device->GetUserMatrix();
450 this->GetMatrix(matrix);
452 this->Device->Render(ren,Mapper);
453 this->EstimatedRenderTime = Mapper->GetTimeToDraw();
457 //----------------------------------------------------------------------------
460 ::AddToRender(vtkRenderer* theRenderer)
462 theRenderer->AddActor(this);
467 ::RemoveFromRender(vtkRenderer* theRenderer)
469 theRenderer->RemoveActor(this);
472 //----------------------------------------------------------------------------
475 ::SetPreferences(vtkFloatingPointType theHeight,
476 vtkFloatingPointType theCursorSize)
478 Init(theHeight, theCursorSize, myRadius, myMagnification, myClamp, GetPosition(), GetProperty()->GetColor());
481 //----------------------------------------------------------------------------
484 ::Init(vtkFloatingPointType theHeight,
485 vtkFloatingPointType theCursorSize,
486 vtkFloatingPointType theRadius,
487 vtkFloatingPointType theMagnification,
488 vtkFloatingPointType theClamp,
489 vtkFloatingPointType thePos[3],
490 vtkFloatingPointType theColor[3])
492 Init(theHeight,theRadius*theMagnification*theCursorSize);
493 SetPosition(thePos[0],thePos[1],thePos[2]);
494 GetProperty()->SetColor(theColor);
495 SetMapper(myMapper.GetPointer());
497 myHeight = theHeight;
498 myCursorSize = theCursorSize;
500 myRadius = theRadius;
501 myMagnification = theMagnification;
509 ::Init(vtkFloatingPointType theHeight,
510 vtkFloatingPointType theRadius)
512 for(int i = 0; i < myNbCones; ++i){
513 vtkConeSource* aSource = mySources[i].GetPointer();
514 aSource->SetHeight(theHeight);
515 // Set the angle of the cone. As a side effect, the angle plus height sets
516 // the base radius of the cone.
517 aSource->SetAngle(20.0);
520 vtkFloatingPointType aDisplacement = -0.5*theHeight - theRadius;
523 mySources[0]->SetDirection(1.0, 0.0, 0.0);
524 mySources[0]->SetCenter(aDisplacement, 0.0, 0.0);
526 // It is impossible to inverse direction around X axis (VTK bug)
527 mySources[1]->SetDirection(-VTK_LARGE_FLOAT, 1.0, 0.0); // A workaround
528 mySources[1]->SetCenter(-aDisplacement, 0.0, 0.0);
531 mySources[2]->SetDirection(0.0, 1.0, 0.0);
532 mySources[2]->SetCenter(0.0, aDisplacement, 0.0);
534 mySources[3]->SetDirection(0.0, -1.0, 0.0);
535 mySources[3]->SetCenter(0.0, -aDisplacement, 0.0);
538 mySources[4]->SetDirection(0.0, 0.0, 1.0);
539 mySources[4]->SetCenter(0.0, 0.0, aDisplacement);
541 mySources[5]->SetDirection(0.0, 0.0, -1.0);
542 mySources[5]->SetCenter(0.0, 0.0, -aDisplacement);
546 //----------------------------------------------------------------------------
547 #include <vtkViewport.h>
548 #include <vtkWindow.h>
550 #include <vtkPolyData.h>
551 #include <vtkPolyDataMapper2D.h>
552 #include <vtkActor2D.h>
553 #include <vtkTimeStamp.h>
554 #include <vtkTextProperty.h>
555 #include <vtkTextActor.h>
556 #include <vtkTextMapper.h>
557 #include <vtkPoints.h>
558 #include <vtkCellArray.h>
559 #include <vtkProperty2D.h>
560 //==================================================================
561 vtkCxxRevisionMacro(VISU_FramedTextActor, "$Revision$");
562 vtkStandardNewMacro(VISU_FramedTextActor);
564 //==================================================================
565 // function : VISU_FramedTextActor
567 //==================================================================
568 VISU_FramedTextActor::VISU_FramedTextActor()
570 PositionCoordinate->SetCoordinateSystemToNormalizedViewport();
573 myBar = vtkPolyData::New();
574 myBarMapper = vtkPolyDataMapper2D::New();
575 myBarMapper->SetInput(myBar);
576 myBarActor = vtkActor2D::New();
577 myBarActor->SetMapper(myBarMapper);
578 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
579 myBarActor->GetProperty()->SetColor(.5, .5, .5);
581 myTextProperty = vtkTextProperty::New();
582 myTextProperty->SetFontSize(12);
583 myTextProperty->SetBold(0);
584 myTextProperty->SetItalic(0);
585 myTextProperty->SetShadow(1);
586 myTextProperty->SetFontFamilyToArial();
588 myTextMapper=vtkTextMapper::New();
589 myTextMapper->SetInput("some text");
590 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
591 myTextActor=vtkActor2D::New();
592 myTextActor->SetMapper(myTextMapper);
594 myBarActor->SetVisibility(1);
595 myTextActor->SetVisibility(1);
596 myBarActor->SetPickable(0);
597 myTextActor->SetPickable(0);
598 //----------------------------------------------------------
599 myModePosition=0;// 0 -centered below the point
600 // 1 -top left corner of the 3D view window
602 for(int i=0; i<4; i++) {
603 myWorldPoint[i] = 0.;
608 //==================================================================
611 //==================================================================
612 VISU_FramedTextActor::~VISU_FramedTextActor()
614 myTextActor->Delete();
615 myTextMapper->Delete();
616 myTextProperty->Delete();
617 myBarActor->Delete();
618 myBarMapper->Delete();
621 //==================================================================
622 // function : SetVisibility
624 //==================================================================
625 void VISU_FramedTextActor::SetVisibility (int theVisibility)
627 myBarActor->SetVisibility(theVisibility);
628 myTextActor->SetVisibility(theVisibility);
630 //==================================================================
631 // function : GetVisibility
633 //==================================================================
634 int VISU_FramedTextActor::GetVisibility()
636 return myBarActor->GetVisibility();
638 //==================================================================
639 // function : SetPickable
641 //==================================================================
642 void VISU_FramedTextActor::SetPickable (int thePickability)
644 myBarActor->SetPickable(thePickability);
645 myTextActor->SetPickable(thePickability);
647 //==================================================================
648 // function : GetPickable
650 //==================================================================
651 int VISU_FramedTextActor::GetPickable()
653 return myBarActor->GetPickable();
655 //==================================================================
656 // function : SetTransparency
658 //==================================================================
659 void VISU_FramedTextActor::SetTransparency(const vtkFloatingPointType theTransparency)
661 if (theTransparency>=0. && theTransparency<=1.){
662 myTransparency=theTransparency;
663 myBarActor->GetProperty()->SetOpacity(1.-myTransparency);
667 //==================================================================
668 // function : GetTransparency
670 //==================================================================
671 vtkFloatingPointType VISU_FramedTextActor::GetTransparency()const
673 return myTransparency;
675 //==================================================================
676 // function : SetText
678 //==================================================================
679 void VISU_FramedTextActor::SetText(const char* theText)
681 myTextMapper->SetInput(theText);
684 //==================================================================
685 // function : GetText
687 //==================================================================
688 char* VISU_FramedTextActor::GetText()
690 return myTextMapper->GetInput();
692 //==================================================================
693 // function : SetModePosition
695 //==================================================================
696 void VISU_FramedTextActor::SetModePosition(const int theMode)
698 myModePosition=theMode;
701 //==================================================================
702 // function : GetModePosition
704 //==================================================================
705 int VISU_FramedTextActor::GetModePosition()const
707 return myModePosition;
709 //==================================================================
710 // function : SetWorldPoint
712 //==================================================================
713 void VISU_FramedTextActor::SetWorldPoint(const vtkFloatingPointType theWorldPoint[4])
715 for(int i = 0; i<4; ++i) {
716 myWorldPoint[i] = theWorldPoint[i];
720 //==================================================================
721 // function : GetWorldPoint
723 //==================================================================
724 const vtkFloatingPointType* VISU_FramedTextActor::GetWorldPoint()const
728 //==================================================================
729 // function : SetDistance
731 //==================================================================
732 void VISU_FramedTextActor::SetDistance(const vtkFloatingPointType theDistance)
734 myDistance=theDistance;
736 //==================================================================
737 // function : GetDistance
739 //==================================================================
740 vtkFloatingPointType VISU_FramedTextActor::GetDistance()const
744 //==================================================================
745 // function : ReleaseGraphicsResources
747 //==================================================================
748 void VISU_FramedTextActor::ReleaseGraphicsResources(vtkWindow *win)
750 myTextActor->ReleaseGraphicsResources(win);
751 myBarActor->ReleaseGraphicsResources(win);
753 //==================================================================
754 // function : RenderOverlay
756 //==================================================================
757 int VISU_FramedTextActor::RenderOverlay(vtkViewport *viewport)
759 int renderedSomething = 0;
760 myBarActor->RenderOverlay(viewport);
761 renderedSomething +=myTextActor->RenderOverlay(viewport);
762 return renderedSomething;
764 //==================================================================
765 // function : RenderOpaqueGeometry
767 //==================================================================
770 ::RenderOpaqueGeometry(vtkViewport *theViewport)
772 int anIsRenderedSomething = 0;
773 int* aViewportSize = theViewport->GetSize();
774 if(aViewportSize[0] == 1 || aViewportSize[1] == 1)
775 return anIsRenderedSomething;
780 vtkPoints *aPoints = vtkPoints::New();
781 aPoints->SetNumberOfPoints(aNbPoints);
782 myBar->SetPoints(aPoints);
785 vtkCellArray *aPolys = vtkCellArray::New();
786 aPolys->Allocate(aPolys->EstimateSize(1,4));
787 vtkIdType aPointsIds[4] = {0, 1, 3, 2};
788 aPolys->InsertNextCell(4,aPointsIds);
789 myBar->SetPolys(aPolys);
793 myTextMapper->GetSize(theViewport, aTextSize);
794 int aBarWidth = aTextSize[0];
795 int aBarHeight = aTextSize[1];
797 if (myModePosition==0) {
798 theViewport->SetWorldPoint(myWorldPoint);
799 theViewport->WorldToDisplay();
801 vtkFloatingPointType aSelectionPoint[3];
802 theViewport->GetDisplayPoint(aSelectionPoint);
803 vtkFloatingPointType u = aSelectionPoint[0];
804 vtkFloatingPointType v = aSelectionPoint[1] - myDistance;
805 theViewport->ViewportToNormalizedViewport(u, v);
806 PositionCoordinate->SetValue(u, v);
808 myTextProperty->SetJustificationToCentered();
809 myTextProperty->SetVerticalJustificationToTop();
812 aPoints->SetPoint(0, -aBarWidth, 0.0, 0.0);
813 aPoints->SetPoint(1, -aBarWidth, -aBarHeight, 0.0);
814 aPoints->SetPoint(2, aBarWidth, 0.0, 0.0);
815 aPoints->SetPoint(3, aBarWidth, -aBarHeight, 0.0);
817 else {//if (myModePosition==1) {
818 PositionCoordinate->SetValue(0.0, 1.0);
819 myTextProperty->SetJustificationToLeft();
820 myTextProperty->SetVerticalJustificationToTop();
822 aPoints->SetPoint(0, 0.0, 0.0, 0.0);
823 aPoints->SetPoint(1, 0.0, -aBarHeight, 0.0);
824 aPoints->SetPoint(2, aBarWidth, 0.0, 0.0);
825 aPoints->SetPoint(3, aBarWidth, -aBarHeight, 0.0);
828 myTextMapper->GetTextProperty()->ShallowCopy(myTextProperty);
829 myBarActor ->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
830 myTextActor->GetPositionCoordinate()->SetReferenceCoordinate(PositionCoordinate);
832 myBuildTime.Modified();
834 return anIsRenderedSomething;