[vtkusers] iteratively update the vertices positions.

Meisam Aliroteh meisam.aliroteh at gmail.com
Tue Aug 14 10:07:33 EDT 2007


Hi Dongqing,

I know I'm getting bck to you with a bit of delay but I'm writting my thesis
now a days and I'm pretty busy with that. I still don't know what is causing
the problem since I haven't seen your code but here is a sample code for
modifying a polydata. Note that this code is not very effecient and I've
pasted it here only as an example:

#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkProperty.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkPointData.h"
#include "vtkPolyDataNormals.h"
#include "vtkCamera.h"

vtkPolyData *MakeUnitCube()
{
        vtkPolyData *cube = vtkPolyData::New();
        vtkPoints *pnts = vtkPoints::New(); // vertices of the cube
        vtkCellArray *faces = vtkCellArray::New(); // faces of the cube

        double points[8][3] = { {-0.5,-0.5,0.5}, {-0.5,0.5,0.5}, {0.5,0.5,
0.5}, {0.5,-0.5,0.5},
                                         {-0.5,-0.5,-0.5}, {-0.5,0.5,-0.5},
{0.5,0.5,-0.5}, {0.5,-0.5,-0.5} };
        int faceIds[6][4] = { {3,2,1,0}, {0,1,5,4}, {6,5,1,2}, {3,7,6,2},
{4,7,3,0}, {4,5,6,7} };
        int i;

        for(i=0; i<8; i++)
        {
                pnts->InsertNextPoint( points[i] );
                if (i < 6)
                {
                        faces->InsertNextCell(4, faceIds[i]);
                }
        }

        cube->SetPoints( pnts );
        cube->SetPolys( faces );
        return cube;
}

void BuildPolyDataNormals(vtkPolyData *pd)
{
        vtkPolyDataNormals *normals = vtkPolyDataNormals::New();
        normals->SetInput( pd );
        normals->ComputePointNormalsOn();
        normals->ComputeCellNormalsOff();
        normals->SplittingOff();
        normals->Update();
        pd->GetPointData()->SetNormals(
normals->GetOutput()->GetPointData()->GetNormals() );
        normals->Delete();
}

void DeformPoly(vtkPolyData *poly, vtkPolyData *polyNew)
{
        int numPoints = poly->GetNumberOfPoints();
        int i;
        double pnt[3];

        polyNew->CopyStructure(poly);
        for(i=0; i<numPoints; i++)
        {
                poly->GetPoint(i, pnt);
                polyNew->GetPoints()->SetPoint(i, pnt[0]*1.02, pnt[1]*1.04,
pnt[2]*1.06);
        }
}

void main()
{
        vtkRenderer *ren = vtkRenderer::New();
        vtkRenderWindow *renWin = vtkRenderWindow::New();
        renWin->SetSize(600,400);
        renWin->AddRenderer(ren);

        vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
        iren->SetInteractorStyle( vtkInteractorStyleTrackballCamera::New()
);
        iren->SetRenderWindow(renWin);

        vtkPolyData *poly = MakeUnitCube(); // create a cube
        BuildPolyDataNormals(poly); // create it's point normals

        vtkPolyData *newPoly = vtkPolyData::New();
        int i=0;
        for(i=0; i<10; i++)
        {
                DeformPoly( poly, newPoly ); // call the function to deform
model based on curvature etc.
                BuildPolyDataNormals( newPoly ); // build normals of the new
shape
                poly->DeepCopy( newPoly ); // assign it back to poly to be
used in the next iteration
        }
        newPoly->Delete();
        /* I commented the following line because I'm going to render poly
onto screen. If you
          don't need to rendere it, then in your program you can delete poly
here ant it should work */
        // poly->Delete();

        vtkProperty *prop = vtkProperty::New();
        prop->SetAmbientColor(0.488566, 0.394212, 0.147212);
        prop->SetAmbient(1);
        prop->SetDiffuseColor(0.488566, 0.394212, 0.147212);
        prop->SetDiffuse(1);
        prop->SetSpecularColor(0.488566, 0.394212, 0.147212);
        prop->SetSpecular(1);
        prop->SetSpecularPower(51.2);

        vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
        mapper->SetInput( poly );
        mapper->GlobalImmediateModeRenderingOn();

        vtkActor *actor = vtkActor::New();
        actor->SetMapper( mapper );
        actor->SetProperty( prop );

        ren->AddActor(actor);
        ren->GetActiveCamera()->SetFocalPoint(0,0,0);
        ren->GetActiveCamera()->SetPosition(3,3,3);
        ren->GetActiveCamera()->OrthogonalizeViewUp();

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

        prop->Delete();
        mapper->Delete();
        actor->Delete();
        poly->Delete();
        ren->Delete();
        renWin->Delete();
        iren->Delete();
}



On 8/13/07, Dongqing Chen <dqchen at cvip.louisville.edu> wrote:
>
>  Hi, Meisam:
>
>    Thanks for your replying.
>
>    Actually, since I need to update all the new vertex positions based on
> their previous ones and some curvature flow criteria, during each iteration,
> I keep the previous polydate dataset structure, (say vtkPolypData
> *poly=vtkPolyData::New()), and assign the new vertex position to a newly
> generated polydata dataset polyNew. Finally during each iteration, I
> regenerate all the normals of polyNew, then I delete the poly and polyNew.
> Starting from a new interation, I transfer the normals to the poly which is
> generated again.
>
>   To be simple, let me summarize what I did:
>  1) create poly and polyNew;  poly takes the input dataset, while polyNew
> is designed to take the updated vertex positions during each iteration.
>     I am also thinking a question: how to keep the exact structures of
> poly and polyNew?
> 2). Based on its old position and curature flow criteria, I iteratively
> change the vertice position.
> 3). At the end of each iteration, I generate all the new normals of
> polyNew, which will be transfered to the poly generated in next iteration.
> 4). keep doing until all iterations are finished.
> 5). delete poly or polyNew during each iteration or after all the
> iterations, which really depends on where I create them, during
> each iteration or before all the iterations?
>
>  Hope it is more clear this time. Otherwise, I will post some patches of
> my code.
>
>
> Best Wishes,
> -----------------------------------------------------------------
> Dongqing Chen
> Ph.D Candidate
> Rm. 07, Paul C. Lutz Hall
> Computer Vision & Image Processing (CVIP) Lab
> Department of Electrical & Computer Engineering
> Speed School of Engineering
> University of Louisville
> Louisville, KY. 40292
> U.S.A
> Email: dqchen at cvip.louisville.edu
> Phone:+1-502-852-2789 (Lab)
>             +1-502-852-6130 (Office)
> -----------------------------------------------------------------
>
> ----- Original Message -----
> *From:* Meisam Aliroteh <meisam.aliroteh at gmail.com>
> *To:* Dongqing Chen <dqchen at cairo.spd.louisville.edu>
> *Cc:* vtkusers at vtk.org
> *Sent:* Sunday, August 12, 2007 8:07 PM
> *Subject:* Re: [vtkusers] iteratively update the vertices positions.
>
>
> Hi Dongqing,
>
> I have implemented programs where I update position of vertices in a
> for-loop and it never crashed. Basically the Delete() method will not crash
> just because you iteratively change vertex positions. If your program
> crashes, it is because of something else. From your explanation it is not
> clear what is the cause of the problem. Maybe you can give more details!?
> Also including parts of your code that is causing the problem can be very
> helpful.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070814/488ca5bd/attachment.htm>


More information about the vtkusers mailing list