[vtkusers] Normal vector of the surface of ellipstic cylinder

arwtyxouymz arw.tyx-ouy_mz at ezweb.ne.jp
Thu Jul 27 22:45:24 EDT 2017


Thank you Girish Lande!

I tried to implement your code, but result is something wrong.
Why?
My code is below and the result is in an attachment picture.
I want to calculat normal vectors on the side of the cylinder.

please help!

result.png <http://vtk.1045678.n5.nabble.com/file/n5744183/result.png>  

#include "vtkPoints.h"
#include "vtkSmartPointer.h"
#include "vtkLinearExtrusionFilter.h"
#include "vtkDataSetMapper.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkPolyLine.h"
#include "vtkLineSource.h"


#include "vtkSphereSource.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkCellData.h"
#include "vtkPolyDataNormals.h"
#include "vtkPointData.h"
#include "vtkBrownianPoints.h"
#include "vtkArrowSource.h"
#include "vtkGlyph3D.h"


bool GetPointNormals(vtkPolyData* polydata);
void TestPointNormals(vtkPolyData* polydata);

int main(int argc, char *argv[])
{
    // 2次元楕円
    double angle = 0;
    double r1, r2;
    int id = 0;
    double CenterX, CenterY;
    r1 = 50;
    r2 = 30;
    double z_length = 50.0;
    CenterX = 0.0; CenterY = 0.0;

    vtkSmartPointer<vtkPoints> points =
            vtkSmartPointer<vtkPoints>::New();
    points->SetNumberOfPoints(720);
    while (angle <= 2.0 * vtkMath::Pi() + (vtkMath::Pi() / 360.0))
    {
        points->InsertPoint(id, r1 * cos(angle) + CenterX, r2 * sin(angle) +
CenterY, 0);
        angle = angle + (vtkMath::Pi() / 360.0);
        id++;
    }


    vtkSmartPointer<vtkPolyLine> line =
            vtkSmartPointer<vtkPolyLine>::New();
    line->GetPointIds()->SetNumberOfIds(id);
    for (int k = 0; k < id; ++k) {
        line->GetPointIds()->SetId(k, k);
    }

    vtkSmartPointer<vtkCellArray> lines =
            vtkSmartPointer<vtkCellArray>::New();
    lines->InsertNextCell(line);

    vtkSmartPointer<vtkPolyData> polyData =
            vtkSmartPointer<vtkPolyData>::New();
    polyData->GlobalReleaseDataFlagOff();
    polyData->Allocate(1, 1);
    polyData->SetPoints(points);
    polyData->SetLines(lines);

    // 3次元化
    vtkSmartPointer<vtkLinearExtrusionFilter> extrude =
            vtkSmartPointer<vtkLinearExtrusionFilter>::New();
    extrude->SetInputData(polyData);
    extrude->SetExtrusionTypeToNormalExtrusion();
    extrude->SetVector(0, 0, -z_length);
    extrude->SetScaleFactor(1.0);
    extrude->Update();

    vtkSmartPointer<vtkPolyDataMapper> elliptic_mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    elliptic_mapper->SetInputConnection(extrude->GetOutputPort());

    vtkSmartPointer<vtkActor> elliptic_actor =
            vtkSmartPointer<vtkActor>::New();
    elliptic_actor->SetMapper(elliptic_mapper);
    elliptic_actor->GetProperty()->SetColor(0.8900, 0.8100, 0.3400);

    // 法線ベクトル
    vtkPolyData* elliptic_cylinder = extrude->GetOutput();
    TestPointNormals(elliptic_cylinder);

    vtkMath::RandomSeed(100);
    vtkSmartPointer<vtkBrownianPoints> brownianPoints =
            vtkSmartPointer<vtkBrownianPoints>::New();
    brownianPoints->SetInputConnection(extrude->GetOutputPort());

    vtkSmartPointer<vtkArrowSource> arrowSource =
            vtkSmartPointer<vtkArrowSource>::New();
    vtkSmartPointer<vtkGlyph3D> glyph3D =
            vtkSmartPointer<vtkGlyph3D>::New();
    glyph3D->SetSourceConnection(arrowSource->GetOutputPort());
    glyph3D->SetInputConnection(brownianPoints->GetOutputPort());
    glyph3D->SetVectorModeToUseNormal();
    glyph3D->SetScaleFactor(10.0);

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

    vtkSmartPointer<vtkActor> glyphActor =
            vtkSmartPointer<vtkActor>::New();
    glyphActor->GetProperty()->SetColor(.0, 1.0, .0);
    glyphActor->SetMapper(glyphMapper);


    // 腫瘍
    double TARGET_X, TARGET_Y, TARGET_Z;
    TARGET_X = -sqrt(pow(r1, 2.0) - pow(r2, 2.0));
    TARGET_Y = 0.0;
    TARGET_Z = -z_length/2.0;

    vtkSmartPointer<vtkSphereSource> target =
            vtkSmartPointer<vtkSphereSource>::New();
    target->SetRadius(1.0);
    target->SetCenter(TARGET_X, TARGET_Y, TARGET_Z);

    vtkSmartPointer<vtkPolyDataMapper> target_mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    target_mapper->SetInputConnection(target->GetOutputPort());

    vtkSmartPointer<vtkActor> target_actor =
            vtkSmartPointer<vtkActor>::New();
    target_actor->SetMapper(target_mapper);
    target_actor->GetProperty()->SetColor(1.0, 0.0, 0.0);

    // ベクトル
    for (vtkIdType id= 0; id <= 720; id++)
    {
        double p[3];
        points->GetPoint(id, p);
        // 方向ベクトル
        vtkVector3d v(p[0] - TARGET_X,
                      p[1] - TARGET_Y,
                      p[2] - TARGET_Z);
    }

    // 直線
    vtkSmartPointer<vtkLineSource> lineSource =
            vtkSmartPointer<vtkLineSource>::New();
    lineSource->SetPoint1(TARGET_X, TARGET_Y, TARGET_Z);
    lineSource->SetPoint2(0.0, 0.0, 0.0);
    lineSource->Update();

    vtkSmartPointer<vtkPolyDataMapper> line_mapper =
            vtkSmartPointer<vtkPolyDataMapper>::New();
    line_mapper->SetInputData(lineSource->GetOutput());

    vtkSmartPointer<vtkActor> line_actor =
            vtkSmartPointer<vtkActor>::New();
    line_actor->SetMapper(line_mapper);
    line_actor->GetProperty()->SetLineWidth(0.5);


    // 描画
    vtkSmartPointer<vtkRenderer> ren =
            vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin =
            vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(ren);
    renWin->SetSize(600, 600);

    vtkSmartPointer<vtkRenderWindowInteractor> iren =
            vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    //ren->AddActor(line_actor);
    //ren->AddActor(target_actor);
    ren->AddActor(glyphActor);
    ren->AddActor(elliptic_actor);
    ren->SetBackground(.3, .6, .3);

    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
            vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    iren->SetInteractorStyle(style);

    renWin->Render();
    iren->Initialize();
    iren->Start();

    return EXIT_SUCCESS;
}

bool GetPointNormals(vtkPolyData* polydata)
{
    std::cout << "In GetPointNormals: " << polydata <<
polydata->GetNumberOfPoints() << std::endl;
    std::cout << "Looking for point normals..." << std::endl;

    vtkIdType numPoints = polydata->GetNumberOfPoints();
    std::cout << "There are " << numPoints << " points." << std::endl;

    vtkIdType numPolys = polydata->GetNumberOfPolys();
    std::cout << "There are " << numPolys << " polys." << std::endl;

    // Double normals in an array
    vtkDoubleArray* normalDataDouble =
           
vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Normals"));

    if (normalDataDouble)
    {
        vtkIdType nc = normalDataDouble->GetNumberOfTuples();
        std::cout << "There are " << nc << " components in normalDataDouble"
<< std::endl;
        return true;
    }

    // Float normals in an array
    vtkFloatArray* normalDataFloat =
           
vtkFloatArray::SafeDownCast(polydata->GetPointData()->GetArray("Normals"));

    if (normalDataFloat)
    {
        vtkIdType nc = normalDataFloat->GetNumberOfTuples();
        std::cout << "There are " << nc << " components in normalDataFloat"
<< std::endl;
        return true;
    }

    // Point normals
    vtkDoubleArray* normalsDouble =
           
vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetNormals());

    if (normalsDouble)
    {
        std::cout << "There are " << normalsDouble->GetNumberOfComponents()
                  << " components in normalsDouble" << std::endl;
        return true;
    }

    // Point normals
    vtkFloatArray* normalsFloat =
           
vtkFloatArray::SafeDownCast(polydata->GetPointData()->GetNormals());

    if (normalsFloat)
    {
        std::cout << "There are " << normalsFloat->GetNumberOfComponents()
                  << " components in normalsFloat" << std::endl;
        return true;
    }

    // Generic type point normals
    vtkDataArray* normalsGeneric = polydata->GetPointData()->GetNormals();

    if (normalsGeneric)
    {
        std::cout << "There are " << normalsGeneric->GetNumberOfTuples()
                  << " normals in normalsGeneric" << std::endl;

        double testDouble[3];
        normalsGeneric->GetTuple(0, testDouble);

        std::cout << "Double: " << testDouble[0] << " "
                 << testDouble[1] << " " << testDouble[2] << std::endl;
        return true;
    }

    std::cout << "Normals not found!" << std::endl;
    return false;
}


void TestPointNormals(vtkPolyData* polydata)
{
    std::cout << "In TestPointNormals: " << polydata->GetNumberOfPoints() <<
std::endl;
    bool hasPointNormals = GetPointNormals(polydata);

    if (!hasPointNormals)
    {
        std::cout << "No point normals were found. Computing normals..." <<
std::endl;

        vtkSmartPointer<vtkPolyDataNormals> normalGenerator =
                vtkSmartPointer<vtkPolyDataNormals>::New();
        normalGenerator->SetInputData(polydata);
        normalGenerator->ComputePointNormalsOn();
        normalGenerator->Update();

        polydata = normalGenerator->GetOutput();
        hasPointNormals = GetPointNormals(polydata);

        std::cout << "On the second try, has point normals? "
                  << hasPointNormals << std::endl;
    }
    else
    {
        std::cout << "Point normals were found!" << std::endl;
    }
}



--
View this message in context: http://vtk.1045678.n5.nabble.com/Normal-vector-of-the-surface-of-ellipstic-cylinder-tp5744171p5744183.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list