# How to preserve the color of vtkPolyData after voxelizing it

**URL:** https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334
**Category:** Support
**Created:** [February 18, 2024, 9:12am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334 "2024-02-18T09:12:44Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 18, 2024, 9:12am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/1 "2024-02-18T09:12:45Z")

</div>

Hi, everyone!  
After voxelizing VtkPolydata, i need to make the corresponding part of the voxel grid model display the color corresponding to the original model.  
The result I want is as shown below:The first picture is the original model, and the second picture is the voxelized mesh model.

 ![img_v3_027r_37d9940f-bd20-4700-8ef6-3a01db8e9b1g](https://discourse.vtk.org/uploads/default/original/2X/9/9ddde7d60744c00b8fb8b42fc0df969f78df7579.jpeg)

 ![img_v3_027r_cf162df6-0b01-4983-9f26-9ab469153b6g](https://discourse.vtk.org/uploads/default/original/2X/d/d8fc9872816a57b357aa4be049b7977f6ff67f96.jpeg)  
Currently, the voxel grid can be obtained through the official website examples [PointOccupancy](https://examples.vtk.org/site/Cxx/Points/PointOccupancy/) and [MarchingCubes](https://examples.vtk.org/site/Cxx/Modelling/MarchingCubes/), but the original corresponding color cannot be obtained.  
To sum up, I have two questions: whether the voxel grid is obtained correctly, and how the color of the original model is mapped to the voxel grid.  
Could you give me some interesting suggestions?  
Thanks for your reply!

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 18, 2024, 9:44pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/2 "2024-02-18T21:44:03Z")

</div>

Hello,

If your output object is `vtkImage`, then you may try `vtkResampleToImage`: [VTK: vtkResampleToImage Class Reference](https://vtk.org/doc/nightly/html/classvtkResampleToImage.html) . If the output object is something else, you may try `vtkResampleWithDataSet`: [VTK: vtkResampleWithDataSet Class Reference](https://vtk.org/doc/nightly/html/classvtkResampleWithDataSet.html) . When reading the documentation, the “Source” object is the original one while the object dubbed “Input” is the one to receive the collocated values of the “Source”.

best,

PC

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 19, 2024, 1:45am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/3 "2024-02-19T01:45:39Z")

</div>

Thanks for your reply!  
I’ll try it!

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 19, 2024, 3:06am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/4 "2024-02-19T03:06:50Z")

</div>

I performed ResampleWithDataSet on paraview, source data Array is model.vtp and destination Mesh is voxel.vtp

 ![image](https://discourse.vtk.org/uploads/default/original/2X/a/af5fac9777aab64346c34b9ef832295a60df7164.png)  
But the result is still no corresponding color.  
I don’t know if what I’m doing is correct.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 19, 2024, 4:21pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/5 "2024-02-19T16:21:45Z")

</div>

> [@Paulo\_Carvalho](#):
>
> If your output object is _ **`vtkImage`** _, then you may try _ **`vtkResampleToImage`** _: [VTK: vtkResampleToImage Class Reference](https://vtk.org/doc/nightly/html/classvtkResampleToImage.html) .

⚠☝

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 21, 2024, 6:12am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/6 "2024-02-21T06:12:54Z")

</div>

Thanks your reply!  
But, I can’t achieve my results based on the information you provided.  
If you have the code or method to implement it, could you share it with me?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 21, 2024, 12:39pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/7 "2024-02-21T12:39:15Z")

</div>

What are you trying? How far have you gone? Sharing the source code of your current attempt is good start.

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 22, 2024, 3:38am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/8 "2024-02-22T03:38:35Z")

</div>

Thank you so much for patiently answering my questions!  
First I tried paraview related Filter.I selected the model.vtp file, which has attribute data displacement. After performing the resampleToImage filtering operation on vtp. The attribute component of the resulting color has displacement. However, Point Gaussian can render colors that are different from the original model phenotype.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/5/561a531ffe9651ee4c51282f22b3e9d20715d696.png)  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/1/1094a38a29ce4daf90798f91d97dccda3ffa6d86.jpeg)

Second,I tried the code, the reference example is [MarchingCubes](https://examples.vtk.org/site/Cxx/Modelling/MarchingCubes/), the following is the code content.

```auto
        double bounds[6];
        vtkSmartPointer<vtkResampleToImage> resampler = 
        vtkSmartPointer<vtkResampleToImage>::New();
        resampler->SetInputDataObject(polyData);
        resampler->SetSamplingDimensions(32, 32, 32); 
        resampler->Update();
        resampler->GetOutput()->GetBounds(bounds);

        vtkNew<vtkVoxelModeller> voxelModeller;
        voxelModeller->SetSampleDimensions(32, 32, 32);
        voxelModeller->SetModelBounds(bounds);
        voxelModeller->SetScalarTypeToFloat();
        voxelModeller->SetMaximumDistance(1);

        voxelModeller->SetInputData(resampler->GetOutput());
        voxelModeller->Update();
        vtkNew<vtkImageData> volume;
        volume->DeepCopy(voxelModeller->GetOutput());

        vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = 
        vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
        surfaceFilter->SetInputData(voxelModeller->GetOutput());
        surfaceFilter->Update();

        //vtkNew<vtkFlyingEdges3D> surface;
        vtkNew<vtkMarchingCubes> surface;
        double isoValue = 0.5;
        surface->SetInputData(volume);
        surface->ComputeNormalsOn();
        surface->SetValue(0, isoValue);

        vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
        writer->SetInputConnection(surface->GetOutputPort());
        const std::string fileName = (yj_output_file_base + "voxel.vtp");
        writer->SetFileName(fileName.c_str());
        writer->Write();
        writer->Delete();`

```

Finally, the size of the vtp file output by the above code is only 3KB, and there is no relevant displacement-related attribute component.  
I will keep the latest updates here!  
I‘m looking forwad for your reply.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 22, 2024, 2:56pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/9 "2024-02-22T14:56:38Z")

</div>

Why don’t you simply render `resampler`’s output? Simplify your pipeline. Once you get a minimum working example, improve the pipeline.

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 22, 2024, 3:16pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/10 "2024-02-22T15:16:45Z")

</div>

Thanks your reply!  
First, Today I have new progress, I can achieve the effect I need in paraView.  
Before mapping, I need to process model.vtp through the Delaunay3D1 filter to obtain the data information of Delaunay3D1, and then use resampleToDataSet to map the attribute data on Delaunay3D1 (as the source mesh) to the voxel grid ( as the destination mesh).  
The following is a simple mind map.

 ![img_v3_028a_844b231b-c701-495f-bff9-8d36b29b1f4g](https://discourse.vtk.org/uploads/default/original/2X/2/2777a56ad35e52b6f71b25ca8378672db2705387.jpeg)  
Below is the rendering  
 ![img_v3_028a_ca907a8c-d73d-465a-9fc1-2eb561b8f6eg](https://discourse.vtk.org/uploads/default/original/2X/0/0187b7bac9703c88c36ee64f486af45a0e1910da.jpeg)  
I may need to choose better voxelization to achieve what I want.

Second, The voxel grid can also be obtained through resampleToImage and mapped to the correct data, but I found a problem. Some models cannot obtain the voxel grid through the resampleToImage filter, just like the model above I cannot obtain. But I can get it by switching to another model. When I adjust the Sampling Dimensions of the model, the model will gradually become incomplete. As shown in Figure a, Sampling Dimensions= 100 \* 100 \* 100, and Figure B is Sampling Dimensions= 32. _32_32  
a:  
 ![img_v3_028a_970918bb-2ae1-4e93-a272-7dc95a11dbeg](https://discourse.vtk.org/uploads/default/original/2X/7/70e84f91abcfffd597573cdb4bfebedc1b1a0a30.jpeg)  
b:

 ![img_v3_028a_d325f8ce-96f3-4b71-a768-82cd93ac65dg](https://discourse.vtk.org/uploads/default/original/2X/2/2996490853cd652a041cebcd13c381e3a35c6624.jpeg)

Last, I don’t know what causes this effect, but if you know why, I’d love to hear it. At present, I will choose to use resampleToDataSet to achieve my effect, and make relevant improvements based on your latest suggestions.  
I‘m looking forwad for your reply!

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 22, 2024, 5:14pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/11 "2024-02-22T17:14:08Z")

</div>

> [@sunlin0oo](#):
>
> Last, I don’t know what causes this effect, but if you know why,

I believe this is self-evident. If you sample your model in a 100x100x100 grid, you get a raster much more detailed than a 32x32x32 grid. At some point you will loose information.

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 23, 2024, 1:47am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/12 "2024-02-23T01:47:34Z")

</div>

Thanks your reply!  
I have another question, how did you know that using vtkResampleToImage and vtkResampleWithDataSet might achieve the desired effect? If possible, can you share your thought process?  
I‘m looking forwad for your reply!

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [February 23, 2024, 1:11pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/13 "2024-02-23T13:11:09Z")

</div>

`vtkImage` is a somewhat different class because it is optimized for large, regularly spaced datasets (Cartesian grids). Hence, it makes sense to use anything related to it when it comes to regular grids. Of course, you can have a volume as a `vtkStructuredGrid` or even a `vtkUnstructureGrid` but these result in far greater memory footprint and lower rendering performance unnecessarily.

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [February 23, 2024, 2:06pm UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/14 "2024-02-23T14:06:41Z")

</div>

Thanks your reply!  
I will keep the latest updates here!

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [March 5, 2024, 8:44am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/15 "2024-03-05T08:44:40Z")

</div>

I have the latest progress now.  
I found that the reason why some samples could not be sampled before was because the spatial position of the voxel vertices was outside the original model.  
Now I am using [vtkProbeFilter](https://www.plasma-matters.nl/vtk/9.1/html/classvtkFindCellStrategy.html) and want to change to a more advanced search strategy through the SetFindCellStrategy class, such as selecting the nearest unit or vertex as attribute data.  
I have not seen similar examples. You Can you provide me with some help?

```auto
vtkSmartPointer<vtkProbeFilter> probe_filter = vtkSmartPointer<vtkProbeFilter>::New();
        probe_filter->SetInputConnection(threshold->GetOutputPort());
        probe_filter->SetSourceData(reader->GetOutput());
        vtkSmartPointer<vtkFindCellStrategy> findCellStrategy = vtkSmartPointer<vtkFindCellStrategy>::New(); // ？？？
        findCellStrategy->Initialize(pointSet);// ？？？
        probe_filter->SetFindCellStrategy(findCellStrategy);
        probe_filter->Update();

```

I‘m looking forwad for your reply!

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 7, 2024, 11:18am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/16 "2024-03-07T11:18:13Z")

</div>

So, I believe the original question has been answered. If you have further questions, please, start new ones.

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [March 7, 2024, 11:21am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/17 "2024-03-07T11:21:48Z")

</div>

Thanks your reply!  
At present, the problem has not been solved, but I have some ideas for solving it.  
I will open a new problem based on your suggestion.  
Thank you for your help during this time!

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [March 7, 2024, 11:27am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/18 "2024-03-07T11:27:15Z")

</div>

> [@sunlin0oo](#):
>
> Below is the rendering
> 
> ![img_v3_028a_ca907a8c-d73d-465a-9fc1-2eb561b8f6eg](https://discourse.vtk.org/uploads/default/original/2X/0/0187b7bac9703c88c36ee64f486af45a0e1910da.jpeg)

This looks good to me.

---

<div class="post-metadata">

### Author: ![sunlin0oo](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sunlin0oo/32/5607_2.png) [@sunlin0oo](https://discourse.vtk.org/u/sunlin0oo)
#### Post date: [March 7, 2024, 11:31am UTC](https://discourse.vtk.org/t/how-to-preserve-the-color-of-vtkpolydata-after-voxelizing-it/13334/19 "2024-03-07T11:31:07Z")

</div>

Yes! But it still has problems, and for me there is still some time, I should be able to do better! 😀
