# Is there any way to draw hundreds of balls with different colors?

**URL:** <https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572>\
**Category:** Development\
**Created:** [June 26, 2017, 9:13am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572 "2017-06-26T09:13:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpgao](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wpgao](https://discourse.slicer.org/u/wpgao)\
**Post date:** [June 26, 2017, 9:13am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/1 "2017-06-26T09:13:33Z")

</div>

Is there any way to draw hundreds of balls with different colors more efficiently?  
I had obtained the position information from the sensor and hope to illustrate the data using the ball in 3D View. There is hundreds of balls should be illustrated with different color! How to achieve this in a more efficient way?  
Thanks very much!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 26, 2017, 9:28am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/2 "2017-06-26T09:28:52Z")

</div>

Create a vtkPolyData (using vtkGlyph3D filter), set that into a Model node, and choose a colormap to color it (based on point data stored in the vtkPolyData). Let us know if you stuck at any point.

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 26, 2017, 9:31am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/3 "2017-06-26T09:31:05Z")

</div>

You can check my gist, which includes a minimal example of vtkGlyph3D: [https://gist.github.com/fepegar/bb18198c9dae273e083521db422ec5c0](https://gist.github.com/fepegar/bb18198c9dae273e083521db422ec5c0)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 26, 2017, 9:41am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/4 "2017-06-26T09:41:40Z")

</div>

If you want to visualize positions then I think fiducials could be a good way to do that. You can add fiducials from the toolbar and handle them in the Markups module. They are visualized in 3D as balls by default. The different colors can be set to each fiducial set node.

---

<div class="post-metadata">

**Author:** ![Fernando](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/fernando/32/5640_2.png) [@Fernando](https://discourse.slicer.org/u/Fernando)\
**Post date:** [June 26, 2017, 9:46am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/5 "2017-06-26T09:46:05Z")

</div>

> [@cpinter](#):
>
> If you want to visualize positions then I think fiducials could be a good way to do that. You can add fiducials from the toolbar and handle them in the Markups module.

I think it would be hard for Slicer to handle “hundreds” of fiducials, as discussed in this [issue](https://issues.slicer.org/view.php?id=4306).

---

<div class="post-metadata">

**Author:** ![wpgao](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wpgao](https://discourse.slicer.org/u/wpgao)\
**Post date:** [June 27, 2017, 7:32am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/6 "2017-06-27T07:32:51Z")

</div>

Hi Lasso,

I had created a vtkPolyData which store the point data and scalars (color), and set it into a Model node. How can I choose the scalars stored in vtkPolyData as the colormap?  
Thanks very much!

```
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
for(iter=path->begin();iter!=path->end();iter++)
	points->InsertNextPoint(iter->pos.x,iter->pos.y,iter->pos.z);

vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData->SetPoints(points);

vtkSmartPointer<vtkVertexGlyphFilter> vertexFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
vertexFilter->SetInputData(polyData);
vertexFilter->Update();

vtkSmartPointer<vtkUnsignedCharArray> Colors=vtkSmartPointer<vtkUnsignedCharArray>::New();
Colors->SetName("Color");
Colors->SetNumberOfComponents(3);
std::vector<Point3f>::iterator it1;
for(it1=color->begin();it1!=color->end();it1++)
	Colors->InsertNextTuple3(it1->x,it1->y,it1->z);	

vtkSmartPointer<vtkPolyData> polyData1 = vtkSmartPointer<vtkPolyData>::New();
polyData1->ShallowCopy(vertexFilter->GetOutput());
polyData1->GetPointData()->SetScalars(Colors);

//create model node
m_PtsModelNode[which] = vtkSmartPointer<vtkMRMLModelNode>::New();
m_PtsModelNode[which].Take(vtkMRMLModelNode::SafeDownCast(m_scene->CreateNodeByClass("vtkMRMLModelNode")));
m_PtsModelNode[which]->SetName(m_scene->GetUniqueNameByString(m_strModelName[which].c_str()));
m_PtsModelNode[which]->SetAndObservePolyData(polyData1);

```

Best,  
Wenpeng

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 27, 2017, 8:54pm UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/7 "2017-06-27T20:54:21Z")

</div>

Example:

```
modelNode.GetDisplayNode().SetActiveScalarName('Distance')
modelNode.GetDisplayNode().SetAndObserveColorNodeID('vtkMRMLFreeSurferProceduralColorNodeBlueRed')
modelNode.GetDisplayNode().ScalarVisibilityOn()
#modelNode.GetDisplayNode().AutoScalarRangeOn()
modelNode.GetDisplayNode().SetScalarRangeFlag(slicer.vtkMRMLDisplayNode.UseColorNodeScalarRange) 

```

See API description here: [http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html](http://apidocs.slicer.org/master/classvtkMRMLModelDisplayNode.html)

---

<div class="post-metadata">

**Author:** ![wpgao](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wpgao](https://discourse.slicer.org/u/wpgao)\
**Post date:** [June 30, 2017, 5:11am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/8 "2017-06-30T05:11:25Z")

</div>

Hi Fernando,

Thanks for your reply. It works!

Best,  
Wenpeng

---

<div class="post-metadata">

**Author:** ![wpgao](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wpgao](https://discourse.slicer.org/u/wpgao)\
**Post date:** [June 30, 2017, 5:12am UTC](https://discourse.slicer.org/t/is-there-any-way-to-draw-hundreds-of-balls-with-different-colors/572/9 "2017-06-30T05:12:10Z")

</div>

Hi Lasso,

Thanks for your reply. It works!

Best,  
Wenpeng
