skytaker
skytaker

Reputation: 4489

VTK: Orient and Scale Many Planes

I'm creating and displaying thousands of vtkPlaneSources but the graphics are sluggish once displayed. I am trying to use vtkGlyph3D or vtkGlyph3DMapper to overcome this issue but there are problems with the plane orientation. I suspect it is due to the way the planes are defined. A vtkPlaneSource is defined by one of these 2 methods:

1) origin, point1 & point2

2) center & normal

The code below scales properly but I do not understand the resulting plane orientation based on the normals I have specified. I assumed if I defined a normal vector, I would have a plane orthogonal to the normal. It appears to me the planes may be oriented along each axis. Both vtkGlyph3D and vtkGlyph3DMapper have the same behavior.

How do I properly orient using the plane normal in this case?

vtkSmartPointer<vtkPoints> glyphPoints = 
        vtkSmartPointer<vtkPoints>::New();
    glyphPoints->InsertNextPoint(0, 0, 0);
    glyphPoints->InsertNextPoint(2, 0, 0);
    glyphPoints->InsertNextPoint(4, 0, 0);

    vtkSmartPointer<vtkPolyData> polydata = 
        vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(glyphPoints);

    vtkSmartPointer<vtkDoubleArray> planeNormals = 
        vtkSmartPointer<vtkDoubleArray>::New();
    planeNormals->SetName("orientArray"); 
    planeNormals->SetNumberOfComponents(3); //3d normals (ie x,y,z)
    planeNormals->SetNumberOfTuples(polydata->GetNumberOfPoints());
    // Construct the normal vectors
    double pN1[3] = { 1.0,0.0,0.0 };
    double pN2[3] = { 0.0,1.0,0.0 };
    double pN3[3] = { 0.0,0.0,1.0 };
    // Add the data to the normals array
    planeNormals->SetTuple(0, pN1);
    planeNormals->SetTuple(1, pN2);
    planeNormals->SetTuple(2, pN3);
    polydata->GetPointData()->SetNormals(planeNormals);

    vtkSmartPointer<vtkDoubleArray> scaleVectors = 
        vtkSmartPointer<vtkDoubleArray>::New();
    scaleVectors->SetName("scaleArray"); //3d scaling 
    scaleVectors->SetNumberOfComponents(3); //3d scaling (ie x,y,z)
    scaleVectors->SetNumberOfTuples(polydata->GetNumberOfPoints());
    // Construct the scale vectors
    double sV1[3] = { 1.0,2.0,1.0 };
    double sV2[3] = { 1.0,3.0,1.0 };
    double sV3[3] = { 1.5,4.0,1.0};
    // Add the data to the vector array
    scaleVectors->SetTuple(0, sV1);
    scaleVectors->SetTuple(1, sV2);
    scaleVectors->SetTuple(2, sV3);
    polydata->GetPointData()->SetVectors(scaleVectors);

    vtkSmartPointer<vtkPlaneSource> planeSource = 
        vtkSmartPointer<vtkPlaneSource>::New();

    // Visualize
    vtkSmartPointer<vtkGlyph3DMapper> glyph3Dmapper = 
        vtkSmartPointer<vtkGlyph3DMapper>::New();
    glyph3Dmapper->SetSourceConnection(planeSource->GetOutputPort());
    glyph3Dmapper->SetInputData(polydata);
    glyph3Dmapper->SetScaleArray("scaleArray");
    glyph3Dmapper->SetScaleModeToScaleByVectorComponents();
    glyph3Dmapper->SetOrientationArray("orientArray");
    glyph3Dmapper->Update();

    vtkSmartPointer<vtkActor> actor = 
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(glyph3Dmapper);

    vtkSmartPointer<vtkRenderer> renderer = 
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = 
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3); // Background color green

    renderWindow->Render();
    renderWindowInteractor->Start();

Attached are images of (1) planes scaled only:

planes scaled only

(2) planes scaled and "oriented" (result of code above):

planes scaled and "oriented"

Thanks for your help.

Upvotes: 1

Views: 1114

Answers (1)

skytaker
skytaker

Reputation: 4489

I found an alternate method using vtkProgrammableGlyphFilter thanks to the examples here and here. I am rotating the 3 coordinates (origin, point1, point2) of each plane then adding to the polydata arrays. The result is a much more responsive graphics display.

#include <vtkSmartPointer.h>
#include <vtkPlaneSource.h>
#include <vtkProgrammableFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPoints.h>
#include <vtkPolyData>
#include <vtkDoubleArray>

void Glyph(void *arg){

    vtkProgrammableGlyphFilter *glyphFilter = (vtkProgrammableGlyphFilter*)arg;
    double origin[3];
    double point1[3];
    double point2[3];
    double center[3];
    int pid = glyphFilter->GetPointId();
    glyphFilter->GetPointData()->GetArray("originArray")->GetTuple(pid, origin);
    glyphFilter->GetPointData()->GetArray("point1Array")->GetTuple(pid, point1);
    glyphFilter->GetPointData()->GetArray("point2Array")->GetTuple(pid, point2);
    glyphFilter->GetPointData()->GetArray("centerArray")->GetTuple(pid, center);

    std::cout << endl << "point id: "   << pid << std::endl;
    std::cout << "origin: " << origin[0] << " " << origin[1] << " " << origin[2] << std::endl;
    std::cout << "point1: " << point1[0] << " " << point1[1] << " " << point1[2] << std::endl;
    std::cout << "point2: " << point2[0] << " " << point2[1] << " " << point2[2] << std::endl;
    std::cout << "center: " << center[0] << " " << center[1] << " " << center[2] << std::endl;

    vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New();
    plane->SetOrigin(origin);
    plane->SetPoint1(point1);
    plane->SetPoint2(point2);
    plane->SetCenter(center);
    plane->Update();
    glyphFilter->SetSourceData(plane->GetOutput());
}


int main(int, char *[])
{

    vtkSmartPointer<vtkPoints> glyphPoints = 
        vtkSmartPointer<vtkPoints>::New();
    glyphPoints->SetDataTypeToDouble();
    glyphPoints->SetNumberOfPoints(table->GetNumberOfRows());

    vtkSmartPointer<vtkPolyData> polydata = 
        vtkSmartPointer<vtkPolyData>::New();
    polydata->SetPoints(glyphPoints);

    vtkSmartPointer<vtkDoubleArray> originArray = 
        vtkSmartPointer<vtkDoubleArray>::New();
    originArray->SetName("originArray");
    originArray->SetNumberOfComponents(3);
    originArray->SetNumberOfTuples(table->GetNumberOfRows());
    // Construct the plane origin points
    double o1[3] = { -1.0, -1.0, 1.0 };
    double o2[3] = { -1.0, -1.0, 1.0 };
    double o3[3] = { -1.0, -1.0, 1.0 };
    // Add the data to the array
    originArray->SetTuple(0, o1);
    originArray->SetTuple(1, o2);
    originArray->SetTuple(2, o3);
    polydata->GetPointData()->AddArray(originArray);

    vtkSmartPointer<vtkDoubleArray> point1Array = 
        vtkSmartPointer<vtkDoubleArray>::New();
    point1Array->SetName("point1Array"); 
    point1Array->SetNumberOfComponents(3);
    point1Array->SetNumberOfTuples(table->GetNumberOfRows());
    // Construct the points in 1st direction
    double p11[3] = { -1.0, 1.0, 1.0 };
    double p12[3] = { -1.0, 1.0, 1.0 };
    double p13[3] = { -1.0, 1.0, 1.0 };
    // Add the data to the array
    point1Array->SetTuple(0, p11);
    point1Array->SetTuple(1, p12);
    point1Array->SetTuple(2, p13);
    polydata->GetPointData()->AddArray(point1Array);

    vtkSmartPointer<vtkDoubleArray> point2Array = 
        vtkSmartPointer<vtkDoubleArray>::New();
    point2Array->SetName("point2Array");
    point2Array->SetNumberOfComponents(3);
    point2Array->SetNumberOfTuples(table->GetNumberOfRows());
    // Construct the points in 2nd direction
    double p21[3] = { -1.0, -1.0, -1.0 };
    double p22[3] = { -1.0, -1.0, -1.0 };
    double p23[3] = { -1.0, -1.0, -1.0 };
    // Add the data to the array
    point2Array->SetTuple(0, p21);
    point2Array->SetTuple(1, p22);
    point2Array->SetTuple(2, p23);
    polydata->GetPointData()->AddArray(point2Array);

    vtkSmartPointer<vtkDoubleArray> centerArray = 
        vtkSmartPointer<vtkDoubleArray>::New();
    centerArray->SetName("centerArray"); 
    centerArray->SetNumberOfComponents(3); 
    centerArray->SetNumberOfTuples(table->GetNumberOfRows());
    // Construct the new plane center (translate to this location original center)
    double c1[3] = { 1.0, 0.0, 0.0 };
    double c2[3] = { 3.0, 0.0, 0.0 };
    double c3[3] = { 5.0, 0.0, 0.0 };
    // Add the data to the array
    centerArray->SetTuple(0, c1);
    centerArray->SetTuple(1, c2);
    centerArray->SetTuple(2, c3);
    polydata->GetPointData()->AddArray(centerArray);

    vtkSmartPointer<vtkPlaneSource> planeSource = 
        vtkSmartPointer<vtkPlaneSource>::New();
    planeSource->SetOutputPointsPrecision(vtkAlgorithm::DOUBLE_PRECISION);
    planeSource->SetCenter(0, 0, 0);
    planeSource->Update();

    vtkSmartPointer<vtkProgrammableGlyphFilter> glypher = 
        vtkSmartPointer<vtkProgrammableGlyphFilter>::New();
    glypher->SetInputData(polydata);
    glypher->SetSourceData(planeSource->GetOutput());
    glypher->SetGlyphMethod(Glyph, glypher);
    glypher->Update();

    vtkSmartPointer<vtkPolyDataMapper> glyphMapper = 
        vtkSmartPointer<vtkPolyDataMapper>::New();
    glyphMapper->SetInputConnection(glypher->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = 
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(glyphMapper);

    vtkSmartPointer<vtkRenderer> renderer = 
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = 
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3); // Background color green

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

Apologies if not a complete example. I am running this code within a much larger program.

Upvotes: 1

Related Questions