[vtk-developers] Problem with dynamic colormap
weifeng0715
weifeng0715 at gmail.com
Sat Mar 15 21:24:42 EDT 2014
Hi David,
Could u help me with the following code? thanks. I want to move the
downmodel and color it dynamically. But it does not work.
#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->SetPosition(this->TimerCount, this->TimerCount,0);
//actor_DownModel->SetOrientation(50.0*cos((double)this->TimerCount),10.0*sin((double)this->TimerCount),1.0);
actor_DownModel->SetPosition(this->TimerCount,
this->TimerCount,0);
vtkDataSet *vtkDownModelPoly_DataMotion =
actor_DownModel->GetMapper()->GetInput();
vtkDownModelPoly_DataMotion->Update();
//double * range =
vtkDownModelPoly_DataMotion->GetScalarRange();
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);
int numPts_UpperModel =
vtkUpperModelPoly_DataMotion->GetNumberOfPoints();
double dist_min = MAX_DOUBLE, dist_max = MIN_DOUBLE;
//double *distance = new double[numPts_DownModel];
for( int iDownModel = 0; iDownModel < numPts_DownModel;
++iDownModel )
{
double *coord_Codyle =
vtkDownModelPoly_DataMotion->GetPoint(iDownModel);
double DistMin_TMJ = MAX_DOUBLE;
for (int iUpperModel = 0; iUpperModel <
numPts_UpperModel; ++iUpperModel)
{
//double *coord_UpperModel = new double[3];
//coord_UpperModel =
poly_UpperModel->GetPoint(iUpperModel);
double *coord_UpperModel =
vtkUpperModelPoly_DataMotion->GetPoint(iUpperModel);
double dist_tmp =
sqrt(pow(coord_Codyle[0]-coord_UpperModel[0],2)
+pow(coord_Codyle[1]-coord_UpperModel[1],2)+pow(coord_Codyle[2]-coord_UpperModel[2],2));
if (dist_tmp<DistMin_TMJ)
{
DistMin_TMJ = dist_tmp;
}
}
scalars_DownModel->SetValue(iDownModel,DistMin_TMJ);
if (DistMin_TMJ>dist_max)
{
dist_max = DistMin_TMJ;
}
if (DistMin_TMJ<dist_min)
{
dist_min = DistMin_TMJ;
}
}
//vtkDownModelPoly_DataMotion->GetPointData()->SetScalars(scalars_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);// >SetInput();
//mapper_UpperModel->SetInput(vtkUpperModelPoly_DataMotion);
#else
mapper_DownModel->SetInputData(scalars_DownModel);
//mapper_UpperModel->SetInputData(vtkUpperModelPoly_DataMotion);
#endif
actor_DownModel->SetMapper(mapper_DownModel);
//actor_UpperModel->SetMapper(mapper_UpperModel);
actor_UpperModel->GetProperty()->SetOpacity(0.18);
vtkSmartPointer<vtkAssembly> stlAssembly =
vtkSmartPointer<vtkAssembly>::New();
stlAssembly->AddPart(actor_DownModel);
//stlAssembly->AddPart(actor_UpperModel);
mapper_DownModel->SetScalarRange(dist_min,dist_max);
//mapper_DownModel->SetScalarRange(dist_min, dist_min*4.0);
//mapper_DownModel->GetLookupTable()->SetRange(dist_min,
dist_min*4.0);
vtkSmartPointer<vtkScalarBarActor> actor_ScalarBar =
vtkSmartPointer<vtkScalarBarActor>::New();
actor_ScalarBar->SetUseOpacity(0.2);
actor_ScalarBar->SetLookupTable(mapper_DownModel->GetLookupTable());
actor_ScalarBar->SetTitle("Distance\n(unit: mm)");
actor_ScalarBar->SetNumberOfLabels(10);
actor_ScalarBar->SetHeight(1.0*(actor_ScalarBar->GetHeight()));
actor_ScalarBar->SetWidth(0.5*(actor_ScalarBar->GetWidth()));
actor_ScalarBar->SetPosition(0.9,0.1);
actor_ScalarBar->SetPickable(1);
actor_ScalarBar->GetTitleTextProperty()->SetFontFamilyToArial();
actor_ScalarBar->GetTitleTextProperty()->BoldOn();
actor_ScalarBar->GetTitleTextProperty()->SetJustificationToCentered();
actor_ScalarBar->GetLabelTextProperty()->ItalicOff();
//actor_ScalarBar->GetLabelTextProperty()->ShadowOff();
actor_ScalarBar->GetLabelTextProperty()->BoldOn();
actor_ScalarBar->GetLabelTextProperty()->SetColor(0.7,0.7,0.7);
/*vtkSmartPointer<vtkActor> map_DownModelDataMotion =
vtkSmartPointer<vtkActor>::New();
vtkSmartPointer<vtkActor> actor_DownModelDataMotion =
vtkSmartPointer<vtkActor>::New();
actor_DownModelDataMotion->SetMapper(mapper_DownModelDataMotion);*/
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();
//renderCollections->GetFirstRenderer()->RemoveActor(actor);
renderCurrent->AddActor(stlAssembly);
//renderCurrent->AddActor(actor_UpperModel);
renderCurrent->AddActor2D(actor_ScalarBar);
iren->GetRenderWindow()->AddRenderer(renderCurrent);
iren->GetRenderWindow()->Render();
}
private:
int TimerCount;
public:
vtkSmartPointer<vtkActor> actor_DownModel;
vtkSmartPointer<vtkActor> actor_UpperModel;
//vtkSmartPointer<vtkScalarBarActor> actor_ScalarBar;
vtkSmartPointer<vtkPolyData> poly_DownModel;
//vtkSmartPointer<vtkPolyData> poly_UpperModel;
};
int main (int, char *[])
{
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);
int numPts_UpperModel =
poly_UpperModel->GetPoints()->GetNumberOfPoints();
double dist_min = MAX_DOUBLE, dist_max = MIN_DOUBLE;
for( int iDownModel = 0; iDownModel < numPts_DownModel; ++iDownModel
)
{
double *coord_DownModel =
poly_DownModel->GetPoint(iDownModel);
double DistMin_TMJ = MAX_DOUBLE;
for (int iUpperModel = 0; iUpperModel < numPts_UpperModel;
++iUpperModel)
{
double *coord_UpperModel =
poly_UpperModel->GetPoint(iUpperModel);
double dist_tmp =
sqrt(pow(coord_DownModel[0]-coord_UpperModel[0],2)
+pow(coord_DownModel[1]-coord_UpperModel[1],2)+pow(coord_DownModel[2]-coord_UpperModel[2],2));
if (dist_tmp<DistMin_TMJ)
{
DistMin_TMJ = dist_tmp;
}
}
scalars_DownModel->SetValue(iDownModel,DistMin_TMJ);
if (DistMin_TMJ>dist_max)
{
dist_max = DistMin_TMJ;
}
if (DistMin_TMJ<dist_min)
{
dist_min = DistMin_TMJ;
}
}
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
mapper_DownModel->SetScalarMaterialModeToDiffuse();
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);
//mapper_DownModel->SetScalarRange(dist_min, dist_min*4.0);
//mapper_DownModel->GetLookupTable()->SetRange(dist_min,
dist_min*4.0);
scalarBar->SetUseOpacity(0.2);
scalarBar->SetLookupTable(mapper_DownModel->GetLookupTable());
scalarBar->SetTitle("Distance\n(unit: mm)");
scalarBar->SetNumberOfLabels(10);
scalarBar->SetHeight(1.0*(scalarBar->GetHeight()));
scalarBar->SetWidth(0.5*(scalarBar->GetWidth()));
scalarBar->SetPosition(0.9,0.1);
scalarBar->GetTitleTextProperty()->SetFontFamilyToArial();
scalarBar->GetTitleTextProperty()->BoldOn();
scalarBar->GetTitleTextProperty()->SetJustificationToCentered();
scalarBar->GetLabelTextProperty()->ItalicOff();
scalarBar->GetLabelTextProperty()->BoldOn();
scalarBar->GetLabelTextProperty()->SetColor(0.7,0.7,0.7);
// Create a renderer and render window
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("TMJ 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-tp5726322.html
Sent from the VTK - Dev mailing list archive at Nabble.com.
More information about the vtk-developers
mailing list