1 // VISU OBJECT : interactive object for VISU entities implementation
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File: VISU_GaussPoints.cxx
25 // Author: Alexey PETROV
29 #include "VISU_GaussPointsPL.hxx"
30 #include "VISU_DeformedShapePL.hxx"
31 #include "VISU_PipeLineUtils.hxx"
32 #include "SALOME_ExtractGeometry.h"
33 #include "VISU_DeformedShapePL.hxx"
34 #include "VISU_OpenGLPointSpriteMapper.hxx"
35 #include "VTKViewer_PassThroughFilter.h"
37 #include <vtkPointSource.h>
38 #include <vtkElevationFilter.h>
39 #include <vtkImageGaussianSource.h>
40 #include <vtkXMLImageDataReader.h>
41 #include <vtkGeometryFilter.h>
42 #include <vtkImageData.h>
43 #include <vtkWarpVector.h>
44 #include <vtkGlyph3D.h>
45 #include <vtkSphereSource.h>
47 //----------------------------------------------------------------------------
48 vtkStandardNewMacro(VISU_GaussPointsPL);
50 //----------------------------------------------------------------------------
52 ::VISU_GaussPointsPL():
56 myMagnificationIncrement(2)
58 myExtractGeometry->SetExtractInside(0);
60 myPSMapper = VISU_OpenGLPointSpriteMapper::New();
61 myPSMapper->SetColorModeToMapScalars();
62 myPSMapper->ScalarVisibilityOn();
64 myGeomFilter = vtkGeometryFilter::New();
66 myWarpVector = vtkWarpVector::New();
67 myCellDataToPointData = vtkCellDataToPointData::New();
68 myCellDataToPointData->SetPassCellData(true);
70 myGlyph = vtkGlyph3D::New();
71 myGlyph->SetScaleModeToScaleByScalar();
72 myGlyph->SetColorModeToColorByScalar();
73 myGlyph->ClampingOn();
75 myExtractor->SetInput( myExtractGeometry->GetOutput() );
76 myFieldTransform->SetInput( myExtractor->GetOutput() );
78 myCellDataToPointData->SetInput( myFieldTransform->GetUnstructuredGridOutput() );
80 mySphereSource = vtkSphereSource::New();
81 myGlyph->SetSource( mySphereSource->GetOutput() );
83 for(int i = 0; i < 2; i++)
84 myPassFilter.push_back(VTKViewer_PassThroughFilter::New());
88 //----------------------------------------------------------------------------
90 ::~VISU_GaussPointsPL()
94 this->myPSMapper->Delete();
95 this->myPSMapper = NULL;
97 if (this->myGeomFilter)
99 this->myGeomFilter->Delete();
100 this->myGeomFilter = NULL;
103 myWarpVector->Delete();
105 myCellDataToPointData->Delete();
109 mySphereSource->Delete();
111 for(int i = 0; i < 2; i++)
112 myPassFilter[i]->Delete();
116 //----------------------------------------------------------------------------
118 CopyGlyph( vtkGlyph3D* source, vtkGlyph3D* dest )
120 dest->SetRange( source->GetRange() );
121 dest->SetScaling( source->GetScaling() );
122 dest->SetClamping( source->GetClamping() );
123 dest->SetScaleMode( source->GetScaleMode() );
124 dest->SetColorMode( source->GetColorMode() );
125 dest->SetScaleFactor( source->GetScaleFactor() );
129 //----------------------------------------------------------------------------
132 ::ShallowCopy(VISU_PipeLine *thePipeLine)
134 if(VISU_GaussPointsPL *aPipeLine = dynamic_cast<VISU_GaussPointsPL*>(thePipeLine)){
135 // To restore mapper input from pipeline
136 vtkPolyData* aDatsSet = myPSMapper->GetInput();
137 myPSMapper->ShallowCopy(aPipeLine->GetPSMapper());
138 myPSMapper->SetInput(aDatsSet);
140 SetPrimitiveType(aPipeLine->GetPrimitiveType());
141 SetBicolor(aPipeLine->GetBicolor());
142 SetClamp(aPipeLine->GetClamp());
143 SetSize(aPipeLine->GetSize());
144 SetMinSize(aPipeLine->GetMinSize());
145 SetMaxSize(aPipeLine->GetMaxSize());
146 SetMagnification(aPipeLine->GetMagnification());
147 SetMagnificationIncrement(aPipeLine->GetMagnificationIncrement());
148 SetAlphaThreshold(aPipeLine->GetAlphaThreshold());
149 SetResolution(aPipeLine->GetResolution());
151 SetIsDeformed( aPipeLine->GetIsDeformed() );
152 SetScale( aPipeLine->GetScale() );
154 mySphereSource->SetRadius( aPipeLine->mySphereSource->GetRadius() );
155 CopyGlyph( aPipeLine->myGlyph, this->myGlyph );
157 Superclass::ShallowCopy(thePipeLine);
161 //----------------------------------------------------------------------------
162 VISU_PipeLine::TMapper*
166 return GetPSMapper();
169 VISU_OpenGLPointSpriteMapper*
174 if(!myPSMapper->GetInput()){
175 GetInput2()->Update();
179 myPSMapper->Update();
186 ::GetPickableDataSet()
188 return myGeomFilter->GetOutput();
191 //----------------------------------------------------------------------------
198 vtkDataSet* aDataSet = GetParentMesh();
199 float aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
201 float* aScalarRange = GetScalarRange();
202 static double EPS = 1.0 / VTK_LARGE_FLOAT;
203 if(fabs(aScalarRange[1]) > EPS)
204 SetScale( aScaleFactor / aScalarRange[1] );
209 myPassFilter[0]->SetInput(myCellDataToPointData->GetUnstructuredGridOutput());
211 myGeomFilter->SetInput( myPassFilter[0]->GetOutput() );
213 // Geometrical Sphere
214 myPassFilter[1]->SetInput(myGeomFilter->GetOutput());
216 myPSMapper->SetInput( myPassFilter[1]->GetPolyDataOutput() );
219 //----------------------------------------------------------------------------
226 //----------------------------------------------------------------------------
231 //cout << "VISU_GaussPointsPL::Update()" << endl;
232 float* aScalarRange = GetScalarRange();
233 mySourceScalarRange[0] = aScalarRange[0];
234 mySourceScalarRange[1] = aScalarRange[1];
235 myDeltaScalarRange = aScalarRange[1] - aScalarRange[0];
237 SetAverageCellSize( VISU_DeformedShapePL::GetScaleFactor( GetParentMesh() ) );
239 vtkMapper* aMapper = GetMapper();
240 vtkDataSet* aDataSet = aMapper->GetInput();
241 vtkCellData* aCellData = aDataSet->GetCellData();
242 myScalarArray = aCellData->GetScalars();
244 myPSMapper->SetLookupTable( myMapperTable );
245 myPSMapper->SetScalarRange( aScalarRange );
249 VISU_ScalarMapPL::Update();
252 //----------------------------------------------------------------------------
257 //cout << "VISU_GaussPointsPL::UpdateGlyph()" << endl;
259 float* aScalarRange = GetScalarRange();
261 if( myPSMapper->GetPointSpriteMode() == 0 ) // Results
263 //cout << "Results" << endl;
264 myGlyph->ClampingOn();
265 myGlyph->SetScaleModeToScaleByScalar();
266 myGlyph->SetColorModeToColorByScalar();
269 float aMinSize = GetMinSize();
270 float aMaxSize = GetMaxSize();
271 if( fabs( aMaxSize - aMinSize ) > 0.0001 )
272 aRange = ( aScalarRange[1] - aScalarRange[0] ) / ( aMaxSize - aMinSize );
273 float aMinRange = aScalarRange[0] - aMinSize * aRange;
274 float aMaxRange = aMinRange + aRange;
276 myGlyph->SetRange( aMinRange, aMaxRange );
277 myGlyph->SetScaleFactor( 1.0 );
279 else if( myPSMapper->GetPointSpriteMode() == 1 ) // Geometry
281 //cout << "Geometry" << endl;
282 myGlyph->ClampingOff();
283 myGlyph->SetScaleModeToDataScalingOff();
284 myGlyph->SetColorModeToColorByScale();
286 myGlyph->SetScaleFactor( GetSize() );
288 else if( myPSMapper->GetPointSpriteMode() == 2 ) // Outside
290 //cout << "Outside" << endl;
291 myGlyph->ClampingOff();
292 myGlyph->SetScaleModeToDataScalingOff();
293 myGlyph->SetColorModeToColorByScalar();
295 myGlyph->SetScaleFactor( GetSize() );
298 mySphereSource->SetRadius( GetMagnification() * GetAverageCellSize() / 2. );
301 //----------------------------------------------------------------------------
304 ::GetObjID(vtkIdType theID) const
306 return myGaussPtsIDMapper->GetObjID(theID);
311 ::GetNodeCoord(int theObjID)
313 vtkIdType anID = GetNodeVTKID(theObjID);
314 vtkDataSet* aDataSet = myGeomFilter->GetInput();
315 return aDataSet->GetPoint(anID);
320 ::SetGaussPtsIDMapper(const VISU::PGaussPtsIDMapper& theGaussPtsIDMapper)
322 myGaussPtsIDMapper = theGaussPtsIDMapper;
323 SetIDMapper(myGaussPtsIDMapper);
326 const VISU::PGaussPtsIDMapper&
328 ::GetGaussPtsIDMapper() const
330 return myGaussPtsIDMapper;
335 ::GetParentMesh() const
337 VISU::TNamedIDMapper* aNamedIDMapper = myGaussPtsIDMapper->GetParent();
338 return aNamedIDMapper->GetVTKOutput();
341 //----------------------------------------------------------------------------
344 ::SetIsDeformed( bool theIsDeformed )
348 myWarpVector->SetInput( myCellDataToPointData->GetUnstructuredGridOutput() );
349 myPassFilter[0]->SetInput(myWarpVector->GetOutput());
352 myPassFilter[0]->SetInput(myCellDataToPointData->GetUnstructuredGridOutput());
354 myIsDeformed = theIsDeformed;
359 //----------------------------------------------------------------------------
362 ::GetIsDeformed() const
367 //----------------------------------------------------------------------------
370 ::SetBicolor(bool theBicolor)
372 if(GetBicolor() == theBicolor)
375 myMapperTable->SetBicolor( theBicolor );
376 myBarTable->SetBicolor( theBicolor );
381 //----------------------------------------------------------------------------
386 return myMapperTable->GetBicolor();
389 //----------------------------------------------------------------------------
392 ::SetIsColored(bool theIsColored)
394 myPSMapper->SetPointSpriteMode( theIsColored ? 0 : 1 ); // Results / Geometry
398 //----------------------------------------------------------------------------
401 ::SetPrimitiveType(int thePrimitiveType)
403 if( thePrimitiveType == VISU_OpenGLPointSpriteMapper::GeomSphere )
405 myGlyph->SetInput( myGeomFilter->GetOutput() );
406 myPassFilter[1]->SetInput(myGlyph->GetOutput());
409 myPassFilter[1]->SetInput(myGeomFilter->GetOutput());
411 myPSMapper->SetPrimitiveType( thePrimitiveType );
416 //----------------------------------------------------------------------------
421 return myPSMapper->GetPrimitiveType();
424 //----------------------------------------------------------------------------
427 ::GetMaximumSupportedSize()
429 return myPSMapper->GetMaximumSupportedSize();
432 //----------------------------------------------------------------------------
435 ::SetClamp(float theClamp)
437 myPSMapper->SetPointSpriteClamp( theClamp );
441 //----------------------------------------------------------------------------
446 return myPSMapper->GetPointSpriteClamp();
449 //----------------------------------------------------------------------------
452 ::SetSize(float theSize)
454 myPSMapper->SetPointSpriteSize( theSize );
458 //----------------------------------------------------------------------------
463 return myPSMapper->GetPointSpriteSize();
466 //----------------------------------------------------------------------------
469 ::SetMinSize(float theMinSize)
471 myPSMapper->SetPointSpriteMinSize( theMinSize );
475 //----------------------------------------------------------------------------
480 return myPSMapper->GetPointSpriteMinSize();
483 //----------------------------------------------------------------------------
486 ::SetMaxSize(float theMaxSize)
488 myPSMapper->SetPointSpriteMaxSize( theMaxSize );
492 //----------------------------------------------------------------------------
497 return myPSMapper->GetPointSpriteMaxSize();
500 //----------------------------------------------------------------------------
503 ::SetMagnification(float theMagnification)
505 myPSMapper->SetPointSpriteMagnification( theMagnification );
509 //----------------------------------------------------------------------------
514 return myPSMapper->GetPointSpriteMagnification();
517 //----------------------------------------------------------------------------
520 ::SetMagnificationIncrement(float theIncrement)
522 myMagnificationIncrement = theIncrement;
525 //----------------------------------------------------------------------------
528 ::SetAlphaThreshold(float theAlphaThreshold)
530 myPSMapper->SetPointSpriteAlphaThreshold( theAlphaThreshold );
534 //----------------------------------------------------------------------------
537 ::GetAlphaThreshold()
539 return myPSMapper->GetPointSpriteAlphaThreshold();
542 //----------------------------------------------------------------------------
545 ::SetResolution(int theResolution)
547 myResolution = theResolution;
548 mySphereSource->SetThetaResolution( myResolution );
549 mySphereSource->SetPhiResolution( myResolution );
552 //----------------------------------------------------------------------------
555 ::ChangeMagnification( bool up )
557 float anIncrement = up ? myMagnificationIncrement : 1.0 / myMagnificationIncrement;
558 SetMagnification( GetMagnification() * anIncrement );
561 //----------------------------------------------------------------------------
564 ::GetPointSize(vtkIdType theID, vtkDataArray* theScalarArray)
566 float aMaxSize = GetAverageCellSize() * GetMaxSize();
567 float aMinSize = GetAverageCellSize() * GetMinSize();
568 float aDelta = aMaxSize - aMinSize;
569 float aVal = theScalarArray->GetTuple1(theID);
571 return aMinSize + aDelta*(aVal - mySourceScalarRange[0])/myDeltaScalarRange;
574 //----------------------------------------------------------------------------
579 return GetAverageCellSize() * GetMaxSize();
582 //----------------------------------------------------------------------------
585 ::GetPointSize(vtkIdType theID)
587 vtkMapper* aMapper = GetMapper();
588 vtkDataSet* aDataSet = aMapper->GetInput();
589 vtkCellData* aCellData = aDataSet->GetCellData();
590 vtkDataArray* aScalarArray = aCellData->GetScalars();
591 return GetPointSize(theID,aScalarArray);
594 //----------------------------------------------------------------------------
597 ::SetAverageCellSize(float theAverageCellSize)
599 myPSMapper->SetAverageCellSize( theAverageCellSize );
603 //----------------------------------------------------------------------------
606 ::GetAverageCellSize()
608 return myPSMapper->GetAverageCellSize();
611 //----------------------------------------------------------------------------
614 ::SetImageData(vtkImageData* theImageData)
616 myPSMapper->SetImageData( theImageData );
619 //----------------------------------------------------------------------------
620 vtkSmartPointer<vtkImageData>
622 ::MakeTexture( const char* theMainTexture,
623 const char* theAlphaTexture )
625 if( !theMainTexture || !theAlphaTexture )
628 vtkXMLImageDataReader* aMainReader = vtkXMLImageDataReader::New();
629 vtkXMLImageDataReader* anAlphaReader = vtkXMLImageDataReader::New();
631 aMainReader->SetFileName( theMainTexture );
632 anAlphaReader->SetFileName( theAlphaTexture );
634 aMainReader->Update();
635 anAlphaReader->Update();
637 vtkImageData* aMainImageData = aMainReader->GetOutput();
638 vtkImageData* anAlphaImageData = anAlphaReader->GetOutput();
640 int* aMainImageSize = aMainImageData->GetDimensions();
641 int* anAlphaImageSize = anAlphaImageData->GetDimensions();
642 if(aMainImageSize[0] != anAlphaImageSize[0] || aMainImageSize[1] != anAlphaImageSize[1])
645 vtkSmartPointer<vtkImageData> aCompositeImageData = vtkImageData::New();
646 aCompositeImageData->Delete();
648 int aNbCompositeComponents = 4;
649 aCompositeImageData->SetDimensions(aMainImageSize);
650 aCompositeImageData->SetScalarTypeToUnsignedChar();
651 aCompositeImageData->SetNumberOfScalarComponents(aNbCompositeComponents);
652 aCompositeImageData->AllocateScalars();
654 unsigned char* aMainDataPtr = (unsigned char*)aMainImageData->GetScalarPointer();
655 unsigned char* anAlphaDataPtr = (unsigned char*)anAlphaImageData->GetScalarPointer();
656 unsigned char *aCompositeDataPtr = (unsigned char * )aCompositeImageData->GetScalarPointer();
658 int aNbMainComponents = aMainImageData->GetNumberOfScalarComponents();
659 int aNbAlphaComponents = anAlphaImageData->GetNumberOfScalarComponents();
660 int aCompositeSize = aMainImageSize[0] * aMainImageSize[1] * aNbCompositeComponents;
662 int aMainId = 0, anAlphaId = 0, aCompositeId = 0;
663 for(; aCompositeId < aCompositeSize;)
665 aCompositeDataPtr[aCompositeId] = aMainDataPtr[aMainId];
666 aCompositeDataPtr[aCompositeId + 1] = aMainDataPtr[aMainId + 1];
667 aCompositeDataPtr[aCompositeId + 2] = aMainDataPtr[aMainId + 2];
668 aCompositeDataPtr[aCompositeId + 3] = anAlphaDataPtr[anAlphaId];
670 aMainId += aNbMainComponents;
671 anAlphaId += aNbAlphaComponents;
672 aCompositeId += aNbCompositeComponents;
674 aMainReader->Delete();
675 anAlphaReader->Delete();
676 aCompositeImageData->Update();
678 return aCompositeImageData;
681 void VISU_GaussPointsPL::SetScale( float theScale )
683 myWarpVector->SetScaleFactor( theScale );
684 myScaleFactor = GetScale();
688 float VISU_GaussPointsPL::GetScale()
690 return myWarpVector->GetScaleFactor();
693 void VISU_GaussPointsPL::SetMapScale( float theMapScale )
695 VISU_ScalarMapPL::SetMapScale( theMapScale );
697 myWarpVector->SetScaleFactor( myScaleFactor * theMapScale );