[vtkusers] question ? [Generate the colors for each cell based on color map]

Agata Krasoń agatakrason at gmail.com
Fri Aug 24 08:43:33 EDT 2012


Hi ;)

I have already implemented generation the colors for each point based on
color map.
Now I need to generate the colors for each cell(triangle) based on color
map.


For points : I have like this : It works good ;)

vtkSmartPointer<vtkPointData> pd = grid->GetPointData();
            pd->SetScalars(fd);
            pd->Update();

            vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
            polydata->SetPoints(points);
            polydata->SetPolys(polys);
            polydata->GetPointData()->SetScalars(fd);


            double bounds[6];
            polydata->GetBounds(bounds);

            // Find min and max z
            double minz = bounds[4];
            double maxz = bounds[5];

            // Create the color map
            vtkSmartPointer<vtkLookupTable> colorLookupTable =
vtkSmartPointer<vtkLookupTable>::New();
            colorLookupTable->SetTableRange(minz, maxz);
            colorLookupTable->Build();

            // Generate the colors for each point based on the color map
            vtkSmartPointer<vtkUnsignedCharArray> colors =
vtkSmartPointer<vtkUnsignedCharArray>::New();
            colors->SetNumberOfComponents(3);
            colors->SetName("Colors");



            for(int i = 0; i < polydata->GetNumberOfPoints(); i++)
            {

                double p[3];
                polydata->GetPoint(i,p);

                double dcolor[3];
                colorLookupTable->GetColor(p[2], dcolor);

                unsigned char color[3];
                for(unsigned int j = 0; j < 3; j++)
                {
                    color[j] = static_cast<unsigned char>(255.0 *
dcolor[j]);
                }

                colors->InsertNextTupleValue(color);
            }

            polydata->GetPointData()->SetScalars(colors);
            polydata->Update();

            // Save mesh with attributes
            vtkSmartPointer<vtkPolyDataWriter> pointAttrWriter =
vtkSmartPointer<vtkPolyDataWriter>::New();
            pointAttrWriter->SetInput(polydata);
            pointAttrWriter->SetFileName("meshWithPoints24.08.vtk");
            pointAttrWriter->Write();




*
But for cells, I have made something like this : It doesn't create a
colors  ?*
*There is something wrong with this code ? *

            vtkSmartPointer<vtkCellData> pd = grid->GetCellData();
            pd->SetScalars(fd);
            pd->Update();

            vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
            polydata->SetPoints(points);
            polydata->SetPolys(polys);
            polydata->GetCellData()->SetScalars(fd);


            double bounds[6];
            polydata->GetBounds(bounds);

            // Find min and max z
            double minz = bounds[4];
            double maxz = bounds[5];

            // Create the color map
            vtkSmartPointer<vtkLookupTable> colorLookupTable =
vtkSmartPointer<vtkLookupTable>::New();
            colorLookupTable->SetTableRange(minz, maxz);
            colorLookupTable->Build();

            // Generate the colors for each cell based on color map
            vtkSmartPointer<vtkUnsignedCharArray> attributes =
vtkSmartPointer<vtkUnsignedCharArray>::New();
            attributes->SetNumberOfComponents(3);
            attributes->SetName("Colors");



          *  for(int i = 0; i < polydata->GetNumberOfCells(); i++)  //
here  ???
            {

                double p[3];
                polydata->GetCell(i);    //

                double dcolor[3];
                colorLookupTable->GetColor(p[2], dcolor);

                unsigned char color[3];
                for(unsigned int j = 0; j < 3; j++)
                {
                    color[j] = static_cast<unsigned char>(255.0 *
dcolor[j]);
                }

                attributes->InsertNextTupleValue(color);
            }

            polydata->GetCellData()->SetScalars(attributes);
            polydata->Update();
*
            // Save mesh with attributes - cells
            vtkSmartPointer<vtkPolyDataWriter> cellAtrrWriter =
vtkSmartPointer<vtkPolyDataWriter>::New();
            cellAtrrWriter->SetInput(polydata);
            cellAtrrWriter->SetFileName("Celltest.vtk");
            cellAtrrWriter->Write();



I would appreciate for any help please ;)

------------------------------
agatte
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120824/2ee1fe6a/attachment.htm>


More information about the vtkusers mailing list