# Use vtkBoxWidget to clip point cloud data

**URL:** <https://discourse.vtk.org/t/use-vtkboxwidget-to-clip-point-cloud-data/15445>\
**Category:** Development\
**Created:** [March 30, 2025, 11:15pm UTC](https://discourse.vtk.org/t/use-vtkboxwidget-to-clip-point-cloud-data/15445 "2025-03-30T23:15:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sapvi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sapvi/32/9874_2.png) [@sapvi](https://discourse.vtk.org/u/sapvi)\
**Post date:** [March 30, 2025, 11:15pm UTC](https://discourse.vtk.org/t/use-vtkboxwidget-to-clip-point-cloud-data/15445/1 "2025-03-30T23:15:53Z")

</div>

Hi all, seems I am generating quite high amount of questions lately, but this is one more I couldn’t get past in several hours of trying to go through docs. I managed to make a simpler MRE from it:

```auto
import vtk
import random

points = vtk.vtkPoints()
for _ in range(1000):
    points.InsertNextPoint(
        random.uniform(-5, 5), random.uniform(-5, 5), random.uniform(-5, 5)
    )

point_cloud = vtk.vtkPolyData()
point_cloud.SetPoints(points)

vertex_filter = vtk.vtkVertexGlyphFilter()
vertex_filter.SetInputData(point_cloud)
vertex_filter.Update()

point_cloud_mapper = vtk.vtkPolyDataMapper()
point_cloud_mapper.SetInputConnection(vertex_filter.GetOutputPort())
point_cloud_actor = vtk.vtkActor()
point_cloud_actor.SetMapper(point_cloud_mapper)

renderer = vtk.vtkRenderer()
renderer.AddActor(point_cloud_actor)

renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

renderer.SetBackground(0.1, 0.1, 0.1)

point_cloud_actor.GetProperty().SetPointSize(3)
point_cloud_actor.GetProperty().SetColor(1.0, 0.0, 0.0)

camera = vtk.vtkCamera()
camera.SetPosition(10, 10, 10)
camera.SetFocalPoint(0, 0, 0)
renderer.SetActiveCamera(camera)

box_widget = vtk.vtkBoxWidget()
box_widget.SetInteractor(renderWindowInteractor)
box_widget.SetPlaceFactor(1.5)
box_widget.SetDefaultRenderer(renderer)
box_widget.On()

def update_clip(caller, event):
    planes = vtk.vtkPlanes()
    box_widget.GetPlanes(planes)
    clip_function = vtk.vtkImplicitBoolean()
    for i in range(planes.GetNumberOfPlanes()):
        plane = planes.GetPlane(i)
        clip_function.AddFunction(plane)
    clipper = vtk.vtkClipPolyData()
    clipper.SetInputData(point_cloud)
    clipper.SetClipFunction(clip_function)
    clipper.Update()
    clipped_mapper = vtk.vtkPolyDataMapper()
    clipped_mapper.SetInputData(clipper.GetOutput())
    point_cloud_actor.SetMapper(clipped_mapper)

box_widget.AddObserver("InteractionEvent", update_clip)

renderWindow.Render()
renderWindowInteractor.Start()

```

I have `self.point_cloud_actor` and I want to use `vtkBoxWidget` to clip the point cloud. I am just trying to alter the view of the point cloud, without altering the actor itself. It seems from docs I need to use `vtkClipPolyData` but I do not understand in which way. I tried mixes of pretty much anything that came into my mind, but the results are usually that: a) bounding box have 0 effect at all, b) nothing is visible at all (as in example I have here), c) only a single side of bounding box works (this was when I tried not with clipper but with directly adding clipping planes to actor).

I cannot seem to find any example of doing this, and I am too new to VTK to find my way in docs, apparently. Could anyone point me to correct direction? Thank you!

---

<div class="post-metadata">

**Author:** ![sapvi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sapvi/32/9874_2.png) [@sapvi](https://discourse.vtk.org/u/sapvi)\
**Post date:** [March 31, 2025, 8:04am UTC](https://discourse.vtk.org/t/use-vtkboxwidget-to-clip-point-cloud-data/15445/2 "2025-03-31T08:04:49Z")

</div>

I managed to make a callback that seems working, but it is so painfully slow that it is pretty much unusable and I cannot test it properly as app just freezes most of the time. It seems that this is how the clipper is supposed to be used, but how to make it usable for some bigger point clouds?

```auto
def update_clipping(self, caller, event):
        if self.poly_data:
            planes = vtk.vtkPlanes()
            self.box_widget.GetPlanes(planes)
            self.clipper.SetClipFunction(planes)
            self.clipper.Update()
            clipped_mapper = vtk.vtkPolyDataMapper()
            clipped_mapper.SetInputConnection(self.clipper.GetOutputPort())
            self.point_cloud_actor.SetMapper(clipped_mapper)
            self.render_window.Render()

```

---

<div class="post-metadata">

**Author:** ![sapvi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sapvi/32/9874_2.png) [@sapvi](https://discourse.vtk.org/u/sapvi)\
**Post date:** [March 31, 2025, 11:23am UTC](https://discourse.vtk.org/t/use-vtkboxwidget-to-clip-point-cloud-data/15445/3 "2025-03-31T11:23:09Z")

</div>

Update whole MRE with code that works but is still painfully slow to use in a real app.

Note that in the real app I am also using `vtkPoints`, not meshes etc. Maybe this info can be somehow used for the speed up?

```auto
import vtk
import random

vtk_points = vtk.vtkPoints()
for _ in range(10_000_000):
    vtk_points.InsertNextPoint(
        random.uniform(-5, 5), random.uniform(-5, 5), random.uniform(-5, 5)
    )

poly_data = vtk.vtkPolyData()
poly_data.SetPoints(vtk_points)

vertex_filter = vtk.vtkVertexGlyphFilter()
vertex_filter.SetInputData(poly_data)
vertex_filter.Update()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(vertex_filter.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(3)

renderer = vtk.vtkRenderer()
renderer.AddActor(actor)

render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(render_window)
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())

renderer.SetBackground(0.1, 0.1, 0.1)

camera = vtk.vtkCamera()
camera.SetPosition(10, 10, 10)
camera.SetFocalPoint(0, 0, 0)
renderer.SetActiveCamera(camera)

box_widget = vtk.vtkBoxWidget()
box_widget.SetInteractor(interactor)
box_widget.SetPlaceFactor(1.0)
box_widget.SetProp3D(actor)
box_widget.PlaceWidget()
box_widget.SetEnabled(True)

clipper = vtk.vtkClipPolyData()
clipper.SetInputConnection(vertex_filter.GetOutputPort())
clipper.InsideOutOn()

clipped_mapper = vtk.vtkPolyDataMapper()

def update_clipping(caller, event):
    planes = vtk.vtkPlanes()
    box_widget.GetPlanes(planes)
    clipper.SetClipFunction(planes)
    clipper.Update()

    clipped_mapper.SetInputConnection(clipper.GetOutputPort())
    actor.SetMapper(clipped_mapper)

box_widget.AddObserver("InteractionEvent", update_clipping)

render_window.Render()
interactor.Start()

```

---

<div class="post-metadata">

**Author:** ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)\
**Post date:** [April 3, 2025, 2:20pm UTC](https://discourse.vtk.org/t/use-vtkboxwidget-to-clip-point-cloud-data/15445/4 "2025-04-03T14:20:16Z")

</div>

Hi @sapvi, Some of the performance overhead could be because you’re glyphing the points as well as clipping on the CPU. Try removing the VertexGlyphFilter and instead use [`vtkProperty::SetRenderPointsAsSpheres`](https://vtk.org/doc/nightly/html/classvtkProperty.html#a299ff684fcb7cdd2c53c68567e7f7e82) to make points _appear_ as spheres.  
Similarly, the clip can happen on the GPU via [`vtkAbstractMapper::SetClippingPlanes`](https://vtk.org/doc/nightly/html/classvtkAbstractMapper.html#aa2709b1b99666839d7c3e1ac9e141f6a). This is unless, you want to use the clipped data for further processing.
