[vtkusers] question about mesh ?

Bill Lorensen bill.lorensen at gmail.com
Tue Aug 14 11:28:56 EDT 2012


please include the mailing list in your reply.

On Tue, Aug 14, 2012 at 11:26 AM, Agata Krasoń <agatakrason at gmail.com>wrote:

> Dear Bill,
>
> Thank You for reply.
> I have already resolve this problem.
>
> It was here  :
>
>  for (vtkIdType i = 0; i < number_of_triangles; i++)
>     {
>       vtkIdType a, b, c;
>       infile >> a >> b >> c;
>       polys->InsertNextCell(3);
>       polys->InsertCellPoint(a -1);
>       polys->InsertCellPoint(b - 1);
>       polys->InsertCellPoint(c - 1);
>
>     }
>
>
> But I have another problem with meshes. Sorry for very basic questions. I
> am beginig my adventure working
> with meshes.
> I don't have lots of knowledge  in this area.
> Now I need to display a mesh with attributes in VTK.
> I have already display mesh in vtk, not with attributes.
> Could You help me please ?
>
> My code :
> #include <vtkTable.h>
> #include "vtkPolyData.h"
> #include <vtkPointData.h>
> #include "vtkPoints.h"
> #include "vtkCellArray.h"
> #include <vtkLine.h>
> #include <vtkFloatArray.h>
> #include <vtkLookupTable.h>
> #include <vtkUnstructuredGrid.h>
>
> #include <vtkGeometryFilter.h>
>
>
>
> #include <vtkParticleReader.h>
> #include <vtkPolyDataReader.h>
> #include <vtkPolyDataWriter.h>
> #include <vtkDelimitedTextReader.h>
> #include <vtkUnstructuredGridWriter.h>
> #include <vtkPolyDataWriter.h>
> #include <vtkProperty.h>
>
> #include <vtkPolyDataMapper.h>
> #include <vtkActor.h>
> #include <vtkRenderer.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderWindowInteractor.h>
>
> using namespace std;
>
>
> int main(int argc, char* argv[])
> {
>
> // file with mesh
>     const char* filename = "heartmesh.txt";
>  std::ifstream infile(filename);
>     vtkIdType number_of_points, number_of_triangles;
>     infile >> number_of_points >> number_of_triangles;
>     vtkPoints* points = vtkPoints::New();
>     points->SetNumberOfPoints(number_of_points);
>
> //std::cout<<"number_of_points: "<<number_of_points<<std::endl;
>  //std::cout<<"number_of_triangles: " <<number_of_triangles<<std::endl;
>
>     for (vtkIdType i = 0; i < number_of_points; i++)
>      {
>         double x, y, z;
>         infile >> x >> y >> z;
>         points->SetPoint(i,x, y, z);
>
>      }
>
>     vtkCellArray* polys = vtkCellArray::New();
>
>    for (vtkIdType i = 0; i < number_of_triangles; i++)
>     {
>       vtkIdType a, b, c;
>       infile >> a >> b >> c;
>       polys->InsertNextCell(3);
>       polys->InsertCellPoint(a-1);
>       polys->InsertCellPoint(b-1);
>       polys->InsertCellPoint(c-1);
>
>     }
>
>
>    vtkUnstructuredGrid* grid = vtkUnstructuredGrid::New();
>    grid->SetPoints(points);
>    grid->SetCells(5, polys);
>    grid->Update();
>
>    // Here I read a file with attributes scalars
>    vtkDelimitedTextReader *dread = vtkDelimitedTextReader::New();
>    dread->SetFileName("CSVFile.csv");
>    dread->Update();
>
>    vtkTable *tdata = dread->GetOutput();
>
>    vtkFloatArray *fd = vtkFloatArray::New();
>
>    vtkIdType nr = tdata->GetNumberOfRows();
>
>    fd->SetNumberOfTuples(nr);
>
>    for (vtkIdType i=0; i<nr; i++) {
>     std::cout <<  tdata->GetValue(i, 0).ToFloat() << "\n";
>     fd->SetTuple1(i, tdata->GetValue(i, 0).ToFloat());
>    }
>
>   vtkPointData *pd = grid->GetPointData();
>   pd->SetScalars(fd);
>   pd->Update();
>
>   // Save in file mesh& attributes
>   vtkUnstructuredGridWriter *writer = vtkUnstructuredGridWriter::New();
>   writer->SetInput(grid);
>   writer->SetFileName("outputMeshWithAttributes.vtk");
>   writer->Write();
>
>   vtkGeometryFilter* geometryFilter = vtkGeometryFilter::New();
>   geometryFilter->SetInput(grid);
>   geometryFilter->Update();
>
>   vtkPolyData* polydata = geometryFilter->GetOutput();
>
>   // Here I need to display mesh with attributes !
>   // Visualization & Display
>   vtkSmartPointer<vtkPolyDataMapper> mapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
>   mapper->SetInput(polydata);
>
>   //mapper->SetLookupTable(lut);
>   mapper->SetColorModeToMapScalars();
>   vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
>   actor->SetMapper(mapper);
>
>
>   //Create a renderer, render window, 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);
>   renderer->SetBackground(0, 0, 0);
>   renderWindow->Render();
>   renderWindowInteractor->Start();
>
>
>   /*polydata->Delete();
>   geometryFilter->Delete();
>   writer->Delete();
>   pd->Delete();
>   tdata->Delete();
>   fd->Delete();
>   dread->Delete();
>   polys->Delete();
>   points->Delete();*/
>
>
>
>   return EXIT_SUCCESS;
> }
>
>
>
> I received display mesh&attributes only in paraview. I attach a screen.
>
> agattte
>
>
>
> 2012/8/14 Bill Lorensen <bill.lorensen at gmail.com>
>
>> Sorry, I cannot read the links to your files.
>>
>> On Tue, Aug 14, 2012 at 8:08 AM, agatte <agatakrason at gmail.com> wrote:
>>
>>> Hi ;)
>>> I have a question about a mesh.
>>> I read mesh data from txt file ( I attach this file.)
>>> But I can't receive good structure of mesh (polydata).
>>> I receive  something like this : (I attach a photo screen )
>>> When I use vtkVertexGlyphFilter. I receive a point cloud with good shape
>>> of
>>> heart.
>>> But I need display mesh with triangles/triangleStrips.
>>> Could You look at this code and this file ?
>>> I would appreciate for any help please.
>>> I don't see an error. What is wrong with this code ?
>>>
>>> My code is here :
>>> #include <vtkVersion.h>
>>> #include <vtkSmartPointer.h>
>>> #include <vtkPolyDataMapper.h>
>>> #include <vtkActor.h>
>>> #include <vtkParticleReader.h>
>>> #include <vtkRenderWindow.h>
>>> #include <vtkRenderWindowInteractor.h>
>>> #include <vtkRenderer.h>
>>> #include <vtkVertexGlyphFilter.h>
>>> #include <sstream>
>>> #include "vtkPolyData.h"
>>> #include "vtkPoints.h"
>>> #include "vtkCellArray.h"
>>> #include <iostream>
>>> #include <fstream>
>>> #include <vtkTriangle.h>
>>> #include <vtkTriangleFilter.h>
>>> #include <vtkContourFilter.h>
>>> #include <vtkDelaunay3D.h>
>>> #include <vtkXMLPolyDataWriter.h>
>>> #include <vtkXMLUnstructuredGridWriter.h>
>>> #include <vtkXMLPolyDataWriter.h>
>>> #include <vtkDataSetSurfaceFilter.h>
>>> #include <vtkPolyDataWriter.h>
>>> #include <vtkExtractEdges.h>
>>> #include <vtkTriangleStrip.h>
>>> #include <vtkLine.h>
>>> #include <vtkXMLPolyDataReader.h>
>>> #include <vtkGeometryFilter.h>
>>> #include <vtkSurfaceReconstructionFilter.h>
>>> #include <vtkDelaunay2D.h>
>>> #include <vtkExtractEdges.h>
>>> #include <vtkFloatArray.h>
>>> #include <vtkLookupTable.h>
>>> #include <vtkStripper.h>
>>> #include <iostream>
>>> #include <vector>
>>> #include <string>
>>> #include <fstream>
>>> #include "vtkProperty.h"
>>>
>>> using namespace std;
>>>
>>>
>>> int main(int argc, char* argv[])
>>> {
>>>
>>>     const char* filename = "heartmesh.txt";
>>>         std::ifstream infile(filename);
>>>     vtkIdType number_of_points, number_of_triangles;
>>>     infile >> number_of_points >> number_of_triangles;
>>>     vtkPoints* points = vtkPoints::New();
>>>     points->SetNumberOfPoints(number_of_points);
>>>
>>>         std::cout<<"number_of_points:
>>> "<<number_of_points&lt;&lt;std::endl;
>>>         std::cout&lt;&lt;&quot;number_of_triangles: &quot;
>>> &lt;&lt;number_of_triangles&lt;&lt;std::endl;
>>>
>>>     for (vtkIdType i = 0; i &lt; number_of_points; i++)
>>>      {
>>>         double x, y, z;
>>>         infile >> x >> y >> z;
>>>         points->SetPoint(i, x, y, z);
>>>
>>>      }
>>>
>>>     vtkCellArray* polys = vtkCellArray::New();
>>>
>>>    for (vtkIdType i = 0; i < number_of_triangles; i++)
>>>     {
>>>       vtkIdType a, b, c;
>>>       infile >> a >> b >> c;
>>>       polys->InsertNextCell(3);
>>>       polys->InsertCellPoint(a);
>>>       polys->InsertCellPoint(b);
>>>       polys->InsertCellPoint(c);
>>>
>>>     }
>>>
>>>   vtkPolyData* polydata = vtkPolyData::New();
>>>   polydata->SetPoints(points);
>>>   polydata->SetPolys(polys);
>>>   polydata->Update();
>>>
>>>   vtkVertexGlyphFilter* glyphFilter = vtkVertexGlyphFilter::New();
>>>   glyphFilter->SetInputConnection(polydata->GetProducerPort());
>>>   glyphFilter->Update();
>>>
>>>   vtkDelaunay2D* delaunay = vtkDelaunay2D::New();
>>>   delaunay->SetInputConnection(glyphFilter->GetOutputPort());
>>>   delaunay->Update();
>>>
>>>   vtkDataSetSurfaceFilter* surfaceFilter =
>>> vtkDataSetSurfaceFilter::New();
>>>   surfaceFilter->SetInputConnection(delaunay->GetOutputPort());
>>>   surfaceFilter->Update();
>>>
>>>   vtkTriangleFilter* triangleFilter = vtkTriangleFilter::New();
>>>   triangleFilter->SetInputConnection(surfaceFilter->GetOutputPort());
>>>   triangleFilter->Update();
>>>
>>>   // Visualization
>>>   vtkSmartPointer<vtkPolyDataMapper> mapper =
>>> vtkSmartPointer<vtkPolyDataMapper>::New();
>>>  // mapper->SetInput(polydata);
>>>   mapper->SetInputConnection(triangleFilter->GetOutputPort());
>>>   vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
>>>   actor->SetMapper(mapper);
>>>
>>>
>>>
>>>   //Create a renderer, render window, 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);
>>>   renderer->SetBackground(0, 0, 0);
>>>   renderWindow->Render();
>>>   renderWindowInteractor->Start();
>>>
>>>   polydata->Delete();
>>>   polys->Delete();
>>>   points->Delete();
>>>   glyphFilter->Delete();
>>>   delaunay->Delete();
>>>   triangleFilter->Delete();
>>>   surfaceFilter->Delete();
>>>
>>>
>>>
>>>
>>>   return EXIT_SUCCESS;
>>> }
>>>
>>>
>>>
>>> agatte
>>>
>>> http://vtk.1045678.n5.nabble.com/file/n5715236/heartmesh.txtheartmesh.txt
>>>
>>>
>>>
>>> http://vtk.1045678.n5.nabble.com/file/n5715236/Heart_as_Polydata.png
>>> http://vtk.1045678.n5.nabble.com/file/n5715236/Heart_as_PointCloud.png
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://vtk.1045678.n5.nabble.com/question-about-mesh-tp5715236.html
>>> Sent from the VTK - Users mailing list archive at Nabble.com.
>>> _______________________________________________
>>> Powered by 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
>>
>>
>


-- 
Unpaid intern in BillsBasement at noware dot com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120814/310697e2/attachment.htm>


More information about the vtkusers mailing list