# Represent surface voxels using vtkcubesource

**URL:** https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045
**Category:** Support
**Created:** [July 27, 2023, 9:31pm UTC](https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045 "2023-07-27T21:31:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Trainee](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@Trainee](https://discourse.vtk.org/u/Trainee)
#### Post date: [July 27, 2023, 9:31pm UTC](https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045/1 "2023-07-27T21:31:29Z")

</div>

Hi,

I am trying to represent voxels using vtkcubesource and find it very slow in rendering. The number of cubes are approximately around 100k. here is the code snippet.

for( auto k = 0; k\<zsteps; ++k)  
for(auto j = 0; j\<jsteps; ++j)  
for(auto i = 0; i\<isteps; ++i)  
{

```
        vtkNew<vtkCubeSource> cubesource;
        cubesource->setcenter( center.x, center.y, center.z);
        cubesource->SetXLength(GetStepX());
        cubesource->SetYLength(GetStepY());
        cubesource->SetZLength(GetStepZ());

        vtkNew<vtkPolyDataMapper> icube;
        icube->SetInputConnection(cubesource->GetOutputPort());

        vtkNew<vtkActor> cubeActor;
        cubeActor->SetMapper(icube);   
        
        renderer->AddActor(cubeActor);

```

}

renderer-\>ResetCamera();  
renderWindow()-\>AddRenderer(renderer);

This displays the 3d profile but is terribly slow to the point where it is unusable. It takes nearly 5 minutes to render the structure and any further interactions (such as rotate or zoom in/out) with the graph takes about 45s up to a minute.

Since this approach uses one actor and mapper per each cubesource i tried using a single actor and mapper with glyph3d object which also yielded the same result. The rendering was still very slow. Here is the code with single actor and mapper

```
vtkNew<vtkPoints> points;
vtkNew<vtkPolyData> polydata;
vtkNew<vtkIntArray> scalars;
vtkNew<vtkGlyph3D> glyph3D;

```

for( auto k = 0; k\<zsteps; ++k)  
for(auto j = 0; j\<jsteps; ++j)  
for(auto i = 0; i\<isteps; ++i)  
{

```
        points->InsertNextPoint(pcenter.GetX(), pcenter.GetY(), pcenter.GetZ());
        scalars->InsertNextValue(iMaterial);

```

}

```
        polydata->SetPoints(points);
            polydata->GetPointData()->SetScalars(scalars);

            vtkNew<vtkCubeSource> cubesource;

            cubesource->SetXLength(GetStepX());
            cubesource->SetYLength(GetStepY());
             cubesource->SetZLength(GetStepZ());

            glyph3D->OrientOff(); // disable orientation
            //glyph3D->SetScaleModeToScaleByVectorComponents(); // sacle along each axis
            glyph3D->SetSourceConnection(cubesource->GetOutputPort());
            glyph3D->SetInputData(polydata);
            glyph3D->Update();

            vtkNew<vtkPolyDataMapper> icube;
            icube->SetInputConnection(glyph3D->GetOutputPort());

            vtkNew<vtkActor> cubeActor;
            cubeActor->SetMapper(icube);

            icube->Update();
            renderer->AddActor(cubeActor);

```

renderer-\>ResetCamera();  
renderWindow()-\>AddRenderer(renderer);

The dataset is structured points with unform spacing in 3 dimensions. The dataset is too large for a volumetric rendering. Is there any other datastructure in vtk better suited to our usecase and to help with the rendering slowness in particular. I have looked into vtkimagedata and vtkstructuredpoints but am not entirely sure.

Any help is appreciated.

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [July 27, 2023, 10:55pm UTC](https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045/2 "2023-07-27T22:55:24Z")

</div>

vtkimagedata with geometry rendering

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [July 28, 2023, 12:08pm UTC](https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045/3 "2023-07-28T12:08:41Z")

</div>

> The dataset is too large for a volumetric rendering.

100K voxels is a tiny volume, volume rendering can easily handle that.

You also might want to try vtkGlyph3DMapper.

Also, as Sebastien suggested, extracting a surface from the volume followed by rendering should be fast.

---

<div class="post-metadata">

### Author: ![Trainee](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/22d042/32.png) [@Trainee](https://discourse.vtk.org/u/Trainee)
#### Post date: [July 31, 2023, 4:24pm UTC](https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045/4 "2023-07-31T16:24:47Z")

</div>

Can you please elaborate a bit or point me to an example

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [August 2, 2023, 12:04pm UTC](https://discourse.vtk.org/t/represent-surface-voxels-using-vtkcubesource/12045/5 "2023-08-02T12:04:21Z")

</div>

It would be helpful to have data, and also provide more information about what you are trying to do; e.g., a sample image etc.
