1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // VISU OBJECT : interactive object for VISU entities implementation
23 // File: VISU_GaussPoints.cxx
24 // Author: Alexey PETROV
27 #include "VISU_GaussPointsPL.hxx"
28 #include "VISU_PointSpriteMapperHolder.hxx"
29 #include "VISU_OpenGLPointSpriteMapper.hxx"
30 #include "VISU_DeformedShapePL.hxx"
31 #include "VISU_FieldTransform.hxx"
32 #include "VISU_LookupTable.hxx"
34 #include "VISU_PipeLineUtils.hxx"
35 #include "VISU_AppendFilter.hxx"
36 #include "VISU_GaussMergeFilter.hxx"
38 #include <vtkPointSource.h>
39 #include <vtkElevationFilter.h>
40 #include <vtkImageGaussianSource.h>
41 #include <vtkXMLImageDataReader.h>
42 #include <vtkGeometryFilter.h>
43 #include <vtkImageData.h>
44 #include <vtkWarpVector.h>
45 #include <vtkGlyph3D.h>
46 #include <vtkSphereSource.h>
47 #include <vtkPassThroughFilter.h>
49 //----------------------------------------------------------------------------
50 vtkStandardNewMacro(VISU_GaussPointsPL);
52 //----------------------------------------------------------------------------
54 ::VISU_GaussPointsPL():
56 myMagnificationIncrement(2),
57 myAppendFilter(VISU_AppendFilter::New()),
58 myMergeFilter(VISU_GaussMergeFilter::New())
60 SetIsShrinkable(false);
61 SetIsFeatureEdgesAllowed(false);
63 myWarpVector = vtkWarpVector::New();
65 myGlyph = vtkGlyph3D::New();
66 myGlyph->SetScaleModeToScaleByScalar();
67 myGlyph->SetColorModeToColorByScalar();
68 myGlyph->ClampingOn();
70 mySphereSource = vtkSphereSource::New();
71 mySphereSource->SetThetaResolution( 8 );
72 mySphereSource->SetPhiResolution( 8 );
73 myGlyph->SetSource( mySphereSource->GetOutput() );
75 for(int i = 0; i < 3; i++)
76 myPassFilter.push_back(vtkPassThroughFilter::New());
78 myPrimitiveType = VISU_OpenGLPointSpriteMapper::PointSprite;
80 myAppendFilter->SetMergingInputs(true);
81 myAppendFilter->Delete();
83 myMergeFilter->SetMergingInputs(true);
84 myMergeFilter->Delete();
88 //----------------------------------------------------------------------------
90 ::~VISU_GaussPointsPL()
92 myWarpVector->Delete();
96 mySphereSource->Delete();
98 for(int i = 0; i < 3; i++)
99 myPassFilter[i]->Delete();
103 //----------------------------------------------------------------------------
108 unsigned long int aTime = Superclass::GetMTime();
110 aTime = std::max(aTime, myWarpVector->GetMTime());
111 aTime = std::max(aTime, myGlyph->GetMTime());
112 aTime = std::max(aTime, mySphereSource->GetMTime());
113 aTime = std::max(aTime, myAppendFilter->GetMTime());
114 aTime = std::max(aTime, myMergeFilter->GetMTime());
116 for(int i = 0; i < 3; i++)
117 aTime = std::max(aTime, myPassFilter[i]->GetMTime());
123 //----------------------------------------------------------------------------
126 ::OnCreateMapperHolder()
128 myPointSpriteMapperHolder = VISU_PointSpriteMapperHolder::New();
129 myPointSpriteMapperHolder->Delete();
131 SetMapperHolder(myPointSpriteMapperHolder.GetPointer());
135 //----------------------------------------------------------------------------
138 ::SetGaussPtsIDMapper(const VISU::PGaussPtsIDMapper& theIDMapper)
140 GetPointSpriteMapperHolder()->SetGaussPtsIDMapper(theIDMapper);
144 //----------------------------------------------------------------------------
145 const VISU::PGaussPtsIDMapper&
147 ::GetGaussPtsIDMapper()
149 return GetPointSpriteMapperHolder()->GetGaussPtsIDMapper();
153 //----------------------------------------------------------------------------
154 VISU_PointSpriteMapperHolder*
156 ::GetPointSpriteMapperHolder()
160 return myPointSpriteMapperHolder.GetPointer();
163 //----------------------------------------------------------------------------
168 VISU::TNamedIDMapper* aNamedIDMapper = GetGaussPtsIDMapper()->GetParent();
169 return aNamedIDMapper->GetOutput();
172 //----------------------------------------------------------------------------
174 CopyGlyph( vtkGlyph3D* theSource, vtkGlyph3D* theDestination )
176 vtkFloatingPointType* aSourceRange = theSource->GetRange();
177 vtkFloatingPointType* aDestinationRange = theDestination->GetRange();
178 if(!VISU::CheckIsSameRange(aDestinationRange, aSourceRange))
179 theDestination->SetRange( aSourceRange );
181 theDestination->SetScaling( theSource->GetScaling() );
182 theDestination->SetClamping( theSource->GetClamping() );
183 theDestination->SetScaleMode( theSource->GetScaleMode() );
184 theDestination->SetColorMode( theSource->GetColorMode() );
185 theDestination->SetScaleFactor( theSource->GetScaleFactor() );
189 //----------------------------------------------------------------------------
192 ::DoShallowCopy(VISU_PipeLine *thePipeLine,
195 Superclass::DoShallowCopy(thePipeLine, theIsCopyInput);
196 VISU_MergedPL::DoShallowCopy(thePipeLine, theIsCopyInput);
198 if(VISU_GaussPointsPL *aPipeLine = dynamic_cast<VISU_GaussPointsPL*>(thePipeLine)){
199 SetPrimitiveType(aPipeLine->GetPrimitiveType());
200 SetBicolor(aPipeLine->GetBicolor());
201 SetClamp(aPipeLine->GetClamp());
202 SetSize(aPipeLine->GetSize());
203 SetMinSize(aPipeLine->GetMinSize());
204 SetMaxSize(aPipeLine->GetMaxSize());
205 SetMagnification(aPipeLine->GetMagnification());
206 SetMagnificationIncrement(aPipeLine->GetMagnificationIncrement());
207 SetAlphaThreshold(aPipeLine->GetAlphaThreshold());
208 SetResolution(aPipeLine->GetResolution());
210 SetIsDeformed( aPipeLine->GetIsDeformed() );
211 SetScale( aPipeLine->GetScale() );
213 vtkFloatingPointType aRadius = aPipeLine->mySphereSource->GetRadius();
214 if(!VISU::CheckIsSameValue(mySphereSource->GetRadius(), aRadius))
215 mySphereSource->SetRadius( aRadius );
217 CopyGlyph( aPipeLine->myGlyph, this->myGlyph );
222 //----------------------------------------------------------------------------
223 VISU_OpenGLPointSpriteMapper*
225 ::GetPointSpriteMapper()
227 return GetPointSpriteMapperHolder()->GetPointSpriteMapper();
231 //----------------------------------------------------------------------------
234 ::GetPickableDataSet()
236 return myPassFilter[1]->GetPolyDataOutput();
240 //----------------------------------------------------------------------------
247 //SetExtractInside(false);
249 vtkDataSet* aDataSet = GetParentMesh();
250 vtkFloatingPointType aScaleFactor = VISU_DeformedShapePL::GetScaleFactor( aDataSet );
252 vtkFloatingPointType* aScalarRange = GetScalarRange();
253 static double EPS = 1.0 / VTK_LARGE_FLOAT;
254 if(fabs(aScalarRange[1]) > EPS)
255 SetScale( aScaleFactor / aScalarRange[1] );
261 //----------------------------------------------------------------------------
270 vtkDataSet* aDataSet = GetFieldTransformFilter()->GetOutput();
272 myMergeFilter->SetGeometry(myAppendFilter->GetOutput());
274 myMergeFilter->SetScalars(aDataSet);
275 myMergeFilter->SetVectors(aDataSet);
277 myMergeFilter->RemoveFields();
278 myMergeFilter->AddField("VISU_FIELD", aDataSet);
279 myMergeFilter->AddField("VISU_POINTS_MAPPER", aDataSet);
280 myMergeFilter->AddField("VISU_INPUTS_MAPPER", aDataSet);
281 myMergeFilter->AddField("VISU_CELLS_MAPPER", aDataSet);
283 myMergeFilter->SetGaussPtsIDMapper(GetGaussPtsIDMapper());
285 myPassFilter[0]->SetInput(InsertCustomPL());
287 myPassFilter[1]->SetInput(myPassFilter[0]->GetOutput());
289 // Geometrical Sphere
290 myPassFilter[2]->SetInput(myPassFilter[1]->GetOutput());
292 GetPointSpriteMapper()->SetInput( myPassFilter[2]->GetPolyDataOutput() );
294 // Update according the current state
295 SetIsDeformed(GetIsDeformed());
297 SetPrimitiveType(GetPrimitiveType());
301 //----------------------------------------------------------------------------
306 if(IsExternalGeometryUsed() || GetNumberOfGeometry() > 1 ){
307 myMergeFilter->Update();
308 myPassFilter[0]->SetInput(myMergeFilter->GetOutput());
311 myPassFilter[0]->SetInput(GetFieldTransformFilter()->GetOutput());
314 SetAverageCellSize( VISU_DeformedShapePL::GetScaleFactor( GetParentMesh() ) );
318 Superclass::Update();
323 //----------------------------------------------------------------------------
328 unsigned long int aSize = Superclass::GetMemorySize();
331 if(vtkDataSet* aDataSet = myWarpVector->GetOutput())
332 aSize += aDataSet->GetActualMemorySize() * 1024;
334 if(GetPrimitiveType() == VISU_OpenGLPointSpriteMapper::GeomSphere)
335 if(vtkDataSet* aDataSet = myGlyph->GetOutput())
336 aSize += aDataSet->GetActualMemorySize() * 1024;
342 //----------------------------------------------------------------------------
347 vtkFloatingPointType* aScalarRange = GetScalarRange();
349 if( GetPointSpriteMapper()->GetPointSpriteMode() == 0 ) // Results
351 myGlyph->ClampingOn();
352 myGlyph->SetScaleModeToScaleByScalar();
353 myGlyph->SetColorModeToColorByScalar();
355 vtkFloatingPointType aScaleFactor = 0.0;
356 vtkFloatingPointType aMinSize = GetMinSize();
357 vtkFloatingPointType aMaxSize = GetMaxSize();
358 if(!VISU::CheckIsSameValue(aMaxSize, aMinSize))
359 aScaleFactor = ( aScalarRange[1] - aScalarRange[0] ) / ( aMaxSize - aMinSize );
361 vtkFloatingPointType aMinRange = aScalarRange[0] - aMinSize * aScaleFactor;
362 vtkFloatingPointType aMaxRange = aMinRange + aScaleFactor;
363 vtkFloatingPointType aRange[2] = {aMinRange, aMaxRange};
365 if(!VISU::CheckIsSameRange(myGlyph->GetRange(), aRange))
366 myGlyph->SetRange( aRange );
368 if(!VISU::CheckIsSameValue(myGlyph->GetScaleFactor(), 1.0))
369 myGlyph->SetScaleFactor( 1.0 );
371 else if( GetPointSpriteMapper()->GetPointSpriteMode() == 1 ) // Geometry
373 myGlyph->ClampingOff();
374 myGlyph->SetScaleModeToDataScalingOff();
375 myGlyph->SetColorModeToColorByScale();
377 vtkFloatingPointType aScaleFactor = GetSize();
378 if(!VISU::CheckIsSameValue(myGlyph->GetScaleFactor(), aScaleFactor))
379 myGlyph->SetScaleFactor( aScaleFactor );
381 else if( GetPointSpriteMapper()->GetPointSpriteMode() == 2 ) // Outside
383 myGlyph->ClampingOff();
384 myGlyph->SetScaleModeToDataScalingOff();
385 myGlyph->SetColorModeToColorByScalar();
387 vtkFloatingPointType aScaleFactor = GetSize();
388 if(!VISU::CheckIsSameValue(myGlyph->GetScaleFactor(), aScaleFactor))
389 myGlyph->SetScaleFactor( aScaleFactor );
392 vtkFloatingPointType aRadius = GetMagnification() * GetAverageCellSize() / 2.0;
393 if(!VISU::CheckIsSameValue(mySphereSource->GetRadius(), aRadius))
394 mySphereSource->SetRadius( aRadius );
398 //----------------------------------------------------------------------------
401 ::GetObjID(vtkIdType theID)
403 return GetGaussPtsIDMapper()->GetObjID(theID);
407 //----------------------------------------------------------------------------
410 ::SetIsDeformed( bool theIsDeformed )
413 myWarpVector->SetInput( myPassFilter[0]->GetPolyDataOutput() );
414 myPassFilter[1]->SetInput(myWarpVector->GetOutput());
416 myPassFilter[1]->SetInput(myPassFilter[0]->GetOutput());
419 //----------------------------------------------------------------------------
424 return myPassFilter[1]->GetInput() != myPassFilter[0]->GetOutput();
427 //----------------------------------------------------------------------------
430 ::SetBicolor(bool theBicolor)
432 if(GetBicolor() == theBicolor)
435 GetMapperTable()->SetBicolor( theBicolor );
436 GetBarTable()->SetBicolor( theBicolor );
439 //----------------------------------------------------------------------------
444 return GetMapperTable()->GetBicolor();
447 //----------------------------------------------------------------------------
450 ::SetIsColored(bool theIsColored)
452 GetPointSpriteMapper()->SetPointSpriteMode( theIsColored ? 0 : 1 ); // Results / Geometry
455 //----------------------------------------------------------------------------
460 return GetPointSpriteMapper()->GetPointSpriteMode() == 0;
463 //----------------------------------------------------------------------------
466 ::SetPrimitiveType(int thePrimitiveType)
468 if( thePrimitiveType == VISU_OpenGLPointSpriteMapper::GeomSphere )
470 myGlyph->SetInput( myPassFilter[1]->GetOutput() );
471 myPassFilter[2]->SetInput(myGlyph->GetOutput());
474 myPassFilter[2]->SetInput(myPassFilter[1]->GetOutput());
476 GetPointSpriteMapper()->SetPrimitiveType( thePrimitiveType );
478 myPrimitiveType = thePrimitiveType;
482 //----------------------------------------------------------------------------
487 return myPrimitiveType;
491 //----------------------------------------------------------------------------
494 ::GetMaximumSupportedSize()
496 return GetPointSpriteMapper()->GetMaximumSupportedSize();
500 //----------------------------------------------------------------------------
503 ::SetClamp(vtkFloatingPointType theClamp)
505 GetPointSpriteMapper()->SetPointSpriteClamp( theClamp );
509 //----------------------------------------------------------------------------
514 return GetPointSpriteMapper()->GetPointSpriteClamp();
517 //----------------------------------------------------------------------------
520 ::SetSize(vtkFloatingPointType theSize)
522 GetPointSpriteMapper()->SetPointSpriteSize( theSize );
526 //----------------------------------------------------------------------------
531 return GetPointSpriteMapper()->GetPointSpriteSize();
535 //----------------------------------------------------------------------------
538 ::SetMinSize(vtkFloatingPointType theMinSize)
540 GetPointSpriteMapper()->SetPointSpriteMinSize( theMinSize );
544 //----------------------------------------------------------------------------
549 return GetPointSpriteMapper()->GetPointSpriteMinSize();
553 //----------------------------------------------------------------------------
556 ::SetMaxSize(vtkFloatingPointType theMaxSize)
558 GetPointSpriteMapper()->SetPointSpriteMaxSize( theMaxSize );
562 //----------------------------------------------------------------------------
567 return GetPointSpriteMapper()->GetPointSpriteMaxSize();
571 //----------------------------------------------------------------------------
574 ::SetMagnification(vtkFloatingPointType theMagnification)
576 GetPointSpriteMapper()->SetPointSpriteMagnification( theMagnification );
580 //----------------------------------------------------------------------------
585 return GetPointSpriteMapper()->GetPointSpriteMagnification();
589 //----------------------------------------------------------------------------
592 ::SetMagnificationIncrement(vtkFloatingPointType theIncrement)
594 if(VISU::CheckIsSameValue(myMagnificationIncrement, theIncrement))
597 myMagnificationIncrement = theIncrement;
602 //----------------------------------------------------------------------------
605 ::SetAlphaThreshold(vtkFloatingPointType theAlphaThreshold)
607 GetPointSpriteMapper()->SetPointSpriteAlphaThreshold( theAlphaThreshold );
611 //----------------------------------------------------------------------------
614 ::GetAlphaThreshold()
616 return GetPointSpriteMapper()->GetPointSpriteAlphaThreshold();
620 //----------------------------------------------------------------------------
623 ::SetResolution(int theResolution)
625 mySphereSource->SetThetaResolution( theResolution );
626 mySphereSource->SetPhiResolution( theResolution );
630 //----------------------------------------------------------------------------
635 return mySphereSource->GetThetaResolution();
639 //----------------------------------------------------------------------------
642 ::ChangeMagnification( bool up )
644 vtkFloatingPointType anIncrement = up ? myMagnificationIncrement : 1.0 / myMagnificationIncrement;
645 SetMagnification( GetMagnification() * anIncrement );
649 //----------------------------------------------------------------------------
652 ::GetPointSize(vtkIdType theID, vtkDataArray* theScalarArray)
654 vtkFloatingPointType aMaxSize = GetAverageCellSize() * GetMaxSize();
655 vtkFloatingPointType aMinSize = GetAverageCellSize() * GetMinSize();
656 vtkFloatingPointType aDelta = aMaxSize - aMinSize;
657 vtkFloatingPointType aVal = theScalarArray->GetTuple1(theID);
659 vtkFloatingPointType* aScalarRange = GetScalarRange();
660 vtkFloatingPointType aDeltaScalarRange = aScalarRange[1] - aScalarRange[0];
662 return aMinSize + aDelta*(aVal - aScalarRange[0]) / aDeltaScalarRange;
666 //----------------------------------------------------------------------------
671 return GetAverageCellSize() * GetMaxSize();
675 //----------------------------------------------------------------------------
678 ::GetPointSize(vtkIdType theID)
680 vtkMapper* aMapper = GetMapper();
681 vtkDataSet* aDataSet = aMapper->GetInput();
682 vtkPointData* aPointData = aDataSet->GetPointData();
683 vtkDataArray* aScalarArray = aPointData->GetScalars();
684 return GetPointSize(theID, aScalarArray);
688 //----------------------------------------------------------------------------
691 ::SetAverageCellSize(vtkFloatingPointType theAverageCellSize)
693 GetPointSpriteMapper()->SetAverageCellSize( theAverageCellSize );
697 //----------------------------------------------------------------------------
700 ::GetAverageCellSize()
702 return GetPointSpriteMapper()->GetAverageCellSize();
706 //----------------------------------------------------------------------------
709 ::SetImageData(vtkImageData* theImageData)
711 GetPointSpriteMapper()->SetImageData( theImageData );
715 //----------------------------------------------------------------------------
716 vtkSmartPointer<vtkImageData>
718 ::MakeTexture( const char* theMainTexture,
719 const char* theAlphaTexture )
721 if( !theMainTexture || !theAlphaTexture )
724 vtkXMLImageDataReader* aMainReader = vtkXMLImageDataReader::New();
725 vtkXMLImageDataReader* anAlphaReader = vtkXMLImageDataReader::New();
727 aMainReader->SetFileName( theMainTexture );
728 anAlphaReader->SetFileName( theAlphaTexture );
730 aMainReader->Update();
731 anAlphaReader->Update();
733 vtkImageData* aMainImageData = aMainReader->GetOutput();
734 vtkImageData* anAlphaImageData = anAlphaReader->GetOutput();
736 int* aMainImageSize = aMainImageData->GetDimensions();
737 int* anAlphaImageSize = anAlphaImageData->GetDimensions();
738 if(aMainImageSize[0] != anAlphaImageSize[0] || aMainImageSize[1] != anAlphaImageSize[1])
741 vtkSmartPointer<vtkImageData> aCompositeImageData = vtkImageData::New();
742 aCompositeImageData->Delete();
744 int aNbCompositeComponents = 4;
745 aCompositeImageData->SetDimensions(aMainImageSize);
746 aCompositeImageData->SetScalarTypeToUnsignedChar();
747 aCompositeImageData->SetNumberOfScalarComponents(aNbCompositeComponents);
748 aCompositeImageData->AllocateScalars();
750 unsigned char* aMainDataPtr = (unsigned char*)aMainImageData->GetScalarPointer();
751 unsigned char* anAlphaDataPtr = (unsigned char*)anAlphaImageData->GetScalarPointer();
752 unsigned char *aCompositeDataPtr = (unsigned char * )aCompositeImageData->GetScalarPointer();
754 int aNbMainComponents = aMainImageData->GetNumberOfScalarComponents();
755 int aNbAlphaComponents = anAlphaImageData->GetNumberOfScalarComponents();
756 int aCompositeSize = aMainImageSize[0] * aMainImageSize[1] * aNbCompositeComponents;
758 int aMainId = 0, anAlphaId = 0, aCompositeId = 0;
759 for(; aCompositeId < aCompositeSize;)
761 aCompositeDataPtr[aCompositeId] = aMainDataPtr[aMainId];
762 aCompositeDataPtr[aCompositeId + 1] = aMainDataPtr[aMainId + 1];
763 aCompositeDataPtr[aCompositeId + 2] = aMainDataPtr[aMainId + 2];
764 aCompositeDataPtr[aCompositeId + 3] = anAlphaDataPtr[anAlphaId];
766 aMainId += aNbMainComponents;
767 anAlphaId += aNbAlphaComponents;
768 aCompositeId += aNbCompositeComponents;
770 aMainReader->Delete();
771 anAlphaReader->Delete();
772 aCompositeImageData->Update();
774 return aCompositeImageData;
780 ::SetScale( vtkFloatingPointType theScale )
782 if(VISU::CheckIsSameValue(myWarpVector->GetScaleFactor(), theScale))
785 myWarpVector->SetScaleFactor( theScale );
786 myScaleFactor = theScale;
794 return myWarpVector->GetScaleFactor();
800 ::SetMapScale( vtkFloatingPointType theMapScale )
802 Superclass::SetMapScale( theMapScale );
804 vtkFloatingPointType aMapScale = myScaleFactor * theMapScale;
805 if(VISU::CheckIsSameValue(myWarpVector->GetScaleFactor(), aMapScale))
808 myWarpVector->SetScaleFactor( aMapScale );
813 ::SetSourceGeometry()
815 if(IsExternalGeometryUsed()){
817 myAppendFilter->AddInput(GetFieldTransformFilter()->GetOutput());
825 return GetMergedInput();
830 ::AddGeometry(vtkDataSet* theGeometry)
832 myAppendFilter->AddInput(theGeometry);
833 return GetNumberOfGeometry();
838 ::GetGeometry(int theGeomNumber)
840 return vtkDataSet::SafeDownCast(myAppendFilter->GetInput(theGeomNumber));
845 ::GetNumberOfGeometry()
847 return myAppendFilter->GetNumberOfInputConnections(0);
852 ::IsExternalGeometryUsed()
854 return myAppendFilter->GetInput() != GetFieldTransformFilter()->GetOutput();
861 myAppendFilter->RemoveAllInputs();
868 if(myMergeFilter->GetInput())
869 myMergeFilter->Update();
870 return myMergeFilter->GetOutput();