I created a vtkStructuredGrid, set locations for the points, created a vtkDoubleArray containing the distance from the point (50, 0, 0), and tried to make a contour of the points that were 50 units away. I’m sure that I’m missing something silly, but I just can’t seem to figure out what that might be. I tried to trim down the source such that it is the smallest thing that still demonstrates my problem.
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkDoubleArray.h>
#include <vtkNamedColors.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkContourFilter.h>
#include <vtkStructuredGridOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkPointData.h>
const double r_min = 10.0;
const double r_resolution = 10.0;
const double r_max = 100.0;
constexpr double DEGREES = M_PI/180.0;
const double angle_resolution = 2.0*DEGREES;
const double phi_min = -60.0*DEGREES;
const double phi_max = 60.0*DEGREES;
const double theta_min = (90.0 - 40.0)*DEGREES;
const double theta_max = (90 + 40.0)*DEGREES;
vtkSmartPointer<vtkStructuredGrid>
generate_display_volume() {
vtkSmartPointer<vtkStructuredGrid> grid =
vtkSmartPointer<vtkStructuredGrid>::New();
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkDoubleArray> scalars =
vtkSmartPointer<vtkDoubleArray>::New();
scalars->SetNumberOfComponents(1);
scalars->SetName("distance");
for (double e = theta_min; e < theta_max; e += angle_resolution) {
for (double a = phi_min; a < phi_max; a += angle_resolution) {
for (double r = r_min; r < r_max; r += r_resolution) {
double x = r*cos(a)*sin(e);
double y = r*sin(a)*sin(e);
double z = r*cos(e);
points->InsertNextPoint(x, y, z);
// This scalar should be the distance from (50, 0, 0).
double value = sqrt((x - 50)*(x - 50) + y*y + z*z);
scalars->InsertNextTuple(&value);
}
}
}
int r_d = (r_max - r_min)/r_resolution;
int azimuth_d = (phi_max - phi_min)/angle_resolution + 1;
int elevation_d = (theta_max - theta_min)/angle_resolution;
grid->SetDimensions(r_d, azimuth_d, elevation_d);
grid->SetPoints(points);
return grid;
}
//----------------------------------------------------------------------------//
int main(int argc, char *argv[])
{
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkStructuredGrid> grid = generate_display_volume();
// Isosurfaces.
vtkSmartPointer<vtkContourFilter> iso =
vtkSmartPointer<vtkContourFilter>::New();
iso->SetInputData(grid);
iso->ComputeNormalsOn();
iso->SetValue(0, 50.0);
iso->Update();
vtkSmartPointer<vtkPolyDataMapper> iso_mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
iso_mapper->SetInputData(iso->GetOutput());
vtkSmartPointer<vtkActor> iso_actor =
vtkSmartPointer<vtkActor>::New();
iso_actor->SetMapper(iso_mapper);
iso_actor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());
// This shows up as black?! Should show up as gold :(
// Outline of the dataset.
vtkSmartPointer<vtkStructuredGridOutlineFilter> edges =
vtkSmartPointer<vtkStructuredGridOutlineFilter>::New();
edges->SetInputData(grid);
edges->Update();
vtkSmartPointer<vtkDataSetMapper> mapper =
vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(edges->GetOutput());
vtkSmartPointer<vtkActor> wireframe_actor =
vtkSmartPointer<vtkActor>::New();
wireframe_actor->SetMapper(mapper);
wireframe_actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
// Rendering part of the function.
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renwin =
vtkSmartPointer<vtkRenderWindow>::New();
renwin->SetWindowName("StructuredGrid");
renwin->SetSize(600, 600);
renwin->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renwin_interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renwin_interactor->SetRenderWindow(renwin);
renderer->AddActor(iso_actor);
renderer->AddActor(wireframe_actor);
renderer->SetBackground(colors->GetColor3d("Lavender").GetData());
renwin->Render();
renwin_interactor->Start();
return EXIT_SUCCESS;
}