[vtkusers] computing the volume of a simple multi triangles polydata

Michele Conconi michele.conconi at unibo.it
Thu Jun 6 14:26:12 EDT 2013


Bill,

thank you so much for the prompt and effective reply.
I uniformed the normals in the triangles and now it works.

I must admit however that the graphical effect of the original version was interesting :-)

Thanks again

Michele
Il 06/06/2013 18:09, Bill Lorensen ha scritto:
Thanks for providing a complete example. It makes it much easrie to help.

Your triangles are not ordered consistently. To see this set the backface property to a different color.

#include "vtkProperty.h"


...


  vtkSmartPointer<vtkProperty> back =
    vtkSmartPointer<vtkProperty>::New();
  back->SetColor(1,0,0);

  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->SetBackfaceProperty(back);




On Thu, Jun 6, 2013 at 10:33 AM, Michele <michele.conconi at unibo.it<mailto:michele.conconi at unibo.it>> wrote:
Hi everybody,

I am tring to build a polydata starting from an ordered set of points in
space in order to evaluate its volume.

I built an exapmple (see below) which generate a correct visual
representation of the polydata, but the computed volume is wrong (6.8 rather
then 48 mm^3).

I tried aldo the vtkDelaunay2D, with no better results.

What am I doing wrong?
Thanks in advance for the help

Michele

------------------------------


#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkPoints.h>
#include <vtkPolyLine.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkMassProperties.h>
#include <vtkTriangle.h>



int main( int argc, char *argv[] )
{
        // build the points set to be triangulated
        int Np = 5;
        double **P1, **P2, **P3;

        P1 = new double*[Np];
        P2 = new double*[Np];
        P3 = new double*[Np];

        for(int i = 0; i < Np; i++)
        {
                P1[i] = new double[4];
                P2[i] = new double[4];
                P3[i] = new double[4];
        }

        for(int i=0;i<Np;i++)
        {
                P1[i][0]        = 0+i;
                P1[i][1]        = 1;
                P1[i][2]        = 0;

                P2[i][0]        = 0+i;
                P2[i][1]        = 3;
                P2[i][2]        = 2;

                P3[i][0]        = 0+i;
                P3[i][1]        = 5;
                P3[i][2]        = 4;
        }


        vtkSmartPointer&lt;vtkPoints> points = vtkSmartPointer<vtkPoints>::New();

        for(int i=0;i<Np;i++)
                points->InsertNextPoint(P1[i]);

        for(int i=0;i<Np;i++)
                points->InsertNextPoint(P2[i]);


        for(int i=0;i<Np;i++)
                points->InsertNextPoint(P3[i]);

        for(int i=0;i<Np;i++)
                points->InsertNextPoint(P1[i][0],0,P1[i][2]);


        for(int i=0;i<Np;i++)
                points->InsertNextPoint(P2[i][0],0,P2[i][2]);

        for(int i=0;i<Np;i++)
                points->InsertNextPoint(P3[i][0],0,P3[i][2]);


        //build manually the triangle

        vtkSmartPointer<vtkCellArray> triangles =
vtkSmartPointer<vtkCellArray>::New();
        vtkSmartPointer<vtkTriangle> triangle =
vtkSmartPointer<vtkTriangle>::New();

        // ------------------------------------- base
        for(int j=3;j<5;j++)
                for(int i = (j*Np)+1; i < (j+1)*Np; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i-1);
                        triangle->GetPointIds()->SetId(1,i);
                        triangle->GetPointIds()->SetId(2,Np+i-1);
                        triangles ->InsertNextCell(triangle);
                }

        for(int j=4;j<6;j++)
                for(int i = j*Np; i < (j+1)*Np-1; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i);
                        triangle->GetPointIds()->SetId(1,i+1);
                        triangle->GetPointIds()->SetId(2,i-Np+1);
                        triangles ->InsertNextCell(triangle);
                }


        // ---------------------------------------- side 1

        for(int i = 1; i < Np; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i-1);
                        triangle->GetPointIds()->SetId(1,i);
                        triangle->GetPointIds()->SetId(2,3*Np+i-1);
                        triangles ->InsertNextCell(triangle);
                }

        for(int i = 3*Np; i < 4*Np-1; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i);
                        triangle->GetPointIds()->SetId(1,i+1);
                        triangle->GetPointIds()->SetId(2,i-3*Np+1);
                        triangles ->InsertNextCell(triangle);
                }

        // ---------------------------------------- side 2
        for(int i = 2*Np+1; i < 3*Np; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i-1);
                        triangle->GetPointIds()->SetId(1,i);
                        triangle->GetPointIds()->SetId(2,3*Np+i-1);
                        triangles ->InsertNextCell(triangle);
                }

        for(int i = 5*Np; i < 6*Np-1; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i);
                        triangle->GetPointIds()->SetId(1,i+1);
                        triangle->GetPointIds()->SetId(2,i-3*Np+1);
                        triangles ->InsertNextCell(triangle);
                }

        // ---------------------------------------- side 3

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,0);
                        triangle->GetPointIds()->SetId(1,3*Np);
                        triangle->GetPointIds()->SetId(2,4*Np);
                        triangles ->InsertNextCell(triangle);

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,Np);
                        triangle->GetPointIds()->SetId(1,4*Np);
                        triangle->GetPointIds()->SetId(2,5*Np);
                        triangles ->InsertNextCell(triangle);

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,4*Np);
                        triangle->GetPointIds()->SetId(1,Np);
                        triangle->GetPointIds()->SetId(2,0);
                        triangles ->InsertNextCell(triangle);

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,5*Np);
                        triangle->GetPointIds()->SetId(1,2*Np);
                        triangle->GetPointIds()->SetId(2,Np);
                        triangles ->InsertNextCell(triangle);

        // ---------------------------------------- side 4

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,Np-1);
                        triangle->GetPointIds()->SetId(1,4*Np-1);
                        triangle->GetPointIds()->SetId(2,5*Np-1);
                        triangles ->InsertNextCell(triangle);

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,2*Np-1);
                        triangle->GetPointIds()->SetId(1,5*Np-1);
                        triangle->GetPointIds()->SetId(2,6*Np-1);
                        triangles ->InsertNextCell(triangle);

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,5*Np-1);
                        triangle->GetPointIds()->SetId(1,2*Np-1);
                        triangle->GetPointIds()->SetId(2,Np-1);
                        triangles ->InsertNextCell(triangle);

                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,6*Np-1);
                        triangle->GetPointIds()->SetId(1,3*Np-1);
                        triangle->GetPointIds()->SetId(2,2*Np-1);
                        triangles ->InsertNextCell(triangle);


        // -------------------- top

        for(int j=0;j<2;j++)
                for(int i = (j*Np)+1; i < (j+1)*Np; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i-1);
                        triangle->GetPointIds()->SetId(1,i);
                        triangle->GetPointIds()->SetId(2,Np+i-1);
                        triangles ->InsertNextCell(triangle);
                }

        for(int j=1;j<3;j++)
                for(int i = j*Np; i < (j+1)*Np-1; i++)
                {
                        triangle->Initialize();
                        triangle->GetPointIds()->SetId(0,i);
                        triangle->GetPointIds()->SetId(1,i+1);
                        triangle->GetPointIds()->SetId(2,i-Np+1);
                        triangles ->InsertNextCell(triangle);
                }


        // build the polydata

        vtkPolyData *polyData = vtkPolyData::New();

        polyData->SetPoints(points);
        polyData->SetPolys(triangles );


        //evaluate the volume : expected outcome for the current points set 48

        vtkMassProperties *mass = vtkMassProperties::New();

        mass->SetInput(polyData);
        cout<<"volume "<<mass->GetVolume()<<endl;

          // Setup actor and mapper
          vtkSmartPointer&lt;vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
          mapper->SetInput(polyData);


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

          // Setup render window, renderer, and interactor
          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);

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

return 0;
}



--
View this message in context: http://vtk.1045678.n5.nabble.com/computing-the-volume-of-a-simple-multi-triangles-polydata-tp5721243.html
Sent from the VTK - Users mailing list archive at Nabble.com.
_______________________________________________
Powered by www.kitware.com<http://www.kitware.com>

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers



--
Unpaid intern in BillsBasement at noware dot com

--
Eng. Michele Conconi, Ph.D.
Health Sciences and Technologies - Interdepartmental Center for Industrial Research (HST - ICIR)
Alma Mater Studiorum - University of Bologna
Viale Risorgimento 2, 40136, Bologna, Italy
Email: michele.conconi at unibo.it<mailto:michele.conconi at unibo.it>
Website: http://grab.diem.unibo.it/
Office: (+39) 051 20 93451
Mobile: (+39) 329 0287996



INFORMAZIONE RISERVATA E CONFIDENZIALE
Questo messaggio contiene informazioni riservate e confidenziali.
Se il lettore non fosse il destinatario del messaggio, inoltrato e ricevuto per errore,
il testo dovrà essere immediatamente cancellato dal computer del ricevente.
E' assolutamente proibita qualunque circolazione, disseminazione o
copia del messaggio spedito e/o ricevuto per errore.

CONFIDENTIALITY and WARNING NOTICE
This message may contain legally privileged or confidential information.
If the reader is not the intended recipient, you received this message in error
and it should therefore be deleted from your computer at once.
Any dissemination, distribution and copying of a message
mistakenly sent or received is strictly forbidden.

5x1000 AI GIOVANI RICERCATORI
DELL'UNIVERSITÀ DI BOLOGNA
Codice Fiscale: 80007010376
www.unibo.it/Vademecum5permille

Questa informativa è inserita in automatico dal sistema al fine esclusivo della realizzazione dei fini istituzionali dell’ente.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130606/e90ba7a0/attachment.htm>


More information about the vtkusers mailing list