[vtkusers] Problem with dynamic colormap
weifeng0715
weifeng0715 at gmail.com
Wed Mar 12 22:33:06 EDT 2014
Hi all,
I have done with the first part of my work: create a static colormap. I
want to move one of the model so as to dynamically create colormap. I have
done with the movement but failed to dynamically colormap. Could anyone help
me? THanks.
Following is the code:
#include <vtkVersion.h>
#include <vtkActor.h>
#include <vtkFloatArray.h>
#include <vtkLookupTable.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkScalarBarActor.h>
#include <vtkSmartPointer.h>
#include "vtkSTLReader.h"
#include "vtkCellArray.h"
#include "vtkScalarsToColors.h"
#include "vtkAxesActor.h"
#include "vtkOrientationMarkerWidget.h"
#include "vtkTextActor.h"
#include "vtkTextProperty.h"
#include "vtkVectorText.h"
#include "vtkFollower.h"
#include "vtkProperty.h"
#include "vtkAssembly.h"
#include "vtkPointSet.h"
#include "vtkPolyDataAlgorithm.h"
#include "vtkPoints.h"
#include "vtkCommand.h"
#include <math.h>
#include "vtkRendererCollection.h"
#include "vtkDataSetMapper.h"
#include "vtkDataSet.h"
#define MAX_DOUBLE 999.99
#define MIN_DOUBLE -9999.99
class vtkTimerCallback2 : public vtkCommand
{
public:
static vtkTimerCallback2 *New()
{
vtkTimerCallback2 *cb = new vtkTimerCallback2;
cb->TimerCount = 0;
return cb;
}
virtual void Execute(vtkObject *caller, unsigned long eventId, void
* vtkNotUsed(callData))
{
if (vtkCommand::TimerEvent == eventId)
{
++this->TimerCount;
}
std::cout << this->TimerCount << std::endl;
actor_DownModel->SetPosition(this->TimerCount,
this->TimerCount,0);
vtkDataSet *vtkDownModelPoly_DataMotion =
actor_DownModel->GetMapper()->GetInput();
vtkDownModelPoly_DataMotion->Update();
vtkDataSet * vtkUpperModelPoly_DataMotion =
actor_UpperModel->GetMapper()->GetInput();
vtkUpperModelPoly_DataMotion->Update();
int numPts_DownModel =
vtkDownModelPoly_DataMotion->GetNumberOfPoints();
vtkSmartPointer<vtkFloatArray> scalars_DownModel =
vtkSmartPointer<vtkFloatArray>::New();
scalars_DownModel->SetNumberOfValues(numPts_DownModel);
poly_DownModel->GetPointData()->SetScalars(scalars_DownModel);
vtkSmartPointer<vtkPolyDataMapper> mapper_DownModel =
vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper_DownModel->SetInput(poly_DownModel);// >SetInput();
#else
mapper_DownModel->SetInputData(scalars_DownModel);
#endif
actor_DownModel->SetMapper(mapper_DownModel);
actor_UpperModel->GetProperty()->SetOpacity(0.18);
vtkSmartPointer<vtkAssembly> stlAssembly =
vtkSmartPointer<vtkAssembly>::New();
stlAssembly->AddPart(actor_DownModel);
mapper_DownModel->SetScalarRange(dist_min,dist_max);
vtkSmartPointer<vtkScalarBarActor> actor_ScalarBar =
vtkSmartPointer<vtkScalarBarActor>::New();
actor_ScalarBar->SetUseOpacity(0.2);
vtkRenderWindowInteractor *iren =
vtkRenderWindowInteractor::SafeDownCast(caller);
vtkSmartPointer<vtkRendererCollection> renderCollections =
vtkSmartPointer<vtkRendererCollection>::New();
renderCollections = iren->GetRenderWindow()->GetRenderers();
vtkSmartPointer<vtkRenderer> renderCurrent=
vtkSmartPointer<vtkRenderer>::New();
renderCurrent = renderCollections->GetFirstRenderer();
renderCurrent->RemoveAllViewProps();
renderCurrent->AddActor(stlAssembly);
renderCurrent->AddActor2D(actor_ScalarBar);
iren->GetRenderWindow()->AddRenderer(renderCurrent);
iren->GetRenderWindow()->Render();
}
private:
int TimerCount;
public:
vtkSmartPointer<vtkActor> actor_DownModel;
vtkSmartPointer<vtkActor> actor_UpperModel;
vtkSmartPointer<vtkPolyData> poly_DownModel;
};
int main (int, char *[])
{
vtkSmartPointer<vtkSTLReader> stlReader_DownModel =
vtkSmartPointer<vtkSTLReader>::New();
stlReader_DownModel->SetFileName("DownModel.stl");
stlReader_DownModel->Update();
vtkSmartPointer<vtkSTLReader> stlReader_UpperModel =
vtkSmartPointer<vtkSTLReader>::New();
stlReader_UpperModel->SetFileName("UpperModel.stl");
stlReader_UpperModel->Update();
vtkSmartPointer<vtkPolyData> poly_DownModel =
vtkSmartPointer<vtkPolyData>::New();
poly_DownModel= stlReader_DownModel->GetOutput();
vtkSmartPointer<vtkPolyData> poly_UpperModel =
vtkSmartPointer<vtkPolyData>::New();
poly_UpperModel= stlReader_UpperModel->GetOutput();
int numPts_DownModel =
poly_DownModel->GetPoints()->GetNumberOfPoints();
vtkSmartPointer<vtkFloatArray> scalars_DownModel =
vtkSmartPointer<vtkFloatArray>::New();
scalars_DownModel->SetNumberOfValues(numPts_DownModel);
poly_DownModel->GetPointData()->SetScalars(scalars_DownModel);
vtkSmartPointer<vtkPolyDataMapper> mapper_DownModel =
vtkSmartPointer<vtkPolyDataMapper>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper_UpperModel =
vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
mapper_DownModel->SetInput(poly_DownModel);
mapper_UpperModel->SetInput(poly_UpperModel);
#else
mapper_DownModel->SetInputData(poly_DownModel);
mapper_UpperModel->SetInputData(poly_UpperModel);
#endif
vtkSmartPointer<vtkActor> actor_DownModel =
vtkSmartPointer<vtkActor>::New();
actor_DownModel->SetMapper(mapper_DownModel);
vtkSmartPointer<vtkActor> actor_UpperModel =
vtkSmartPointer<vtkActor>::New();
actor_UpperModel->GetProperty()->SetOpacity(0.18);
actor_UpperModel->SetMapper(mapper_UpperModel);
vtkSmartPointer<vtkAssembly> stlAssembly =
vtkSmartPointer<vtkAssembly>::New();
stlAssembly->AddPart(actor_DownModel);
stlAssembly->AddPart(actor_UpperModel);
vtkSmartPointer<vtkScalarBarActor> scalarBar =
vtkSmartPointer<vtkScalarBarActor>::New();
mapper_DownModel->SetScalarRange(dist_min,dist_max);
scalarBar->SetUseOpacity(0.2);
scalarBar->SetLookupTable(mapper_DownModel->GetLookupTable());
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->SetInteractive(1);
renderer->GradientBackgroundOn();
renderer->SetBackground(1,1,1);
renderer->SetBackground2(0.,0.0,0.);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
int * iScreenSize = renderWindow->GetScreenSize();
renderWindow->SetSize(iScreenSize[0]/2,iScreenSize[1]/2);
renderWindow->SetPosition(2*iScreenSize[0]/4,2*iScreenSize[1]/4);
iScreenSize = NULL;
delete iScreenSize;
renderWindow->AddRenderer(renderer);
// Create an interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Render the scene (lights and cameras are created automatically)
renderer->AddActor(stlAssembly);
renderer->AddActor2D(scalarBar);
renderWindow->Render();
renderWindow->SetWindowName("WholeModel Display Window");
//// Initialize must be called prior to creating timer events.
renderWindowInteractor->Initialize();
vtkSmartPointer<vtkTimerCallback2> cb =
vtkSmartPointer<vtkTimerCallback2>::New();
cb->actor_DownModel = actor_DownModel;
cb->actor_UpperModel = actor_UpperModel;
cb->poly_DownModel = poly_DownModel;
renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
int timerId = renderWindowInteractor->CreateRepeatingTimer(50);
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
--
View this message in context: http://vtk.1045678.n5.nabble.com/Problem-with-dynamic-colormap-tp5726283.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list