# ----------------------------------------------------------------------------
# - CloudViewer: www.cloudViewer.org -
# ----------------------------------------------------------------------------
# Copyright (c) 2018-2024 www.cloudViewer.org
# SPDX-License-Identifier: MIT
# ----------------------------------------------------------------------------
import cloudViewer as cv3d
__all__ = ['ExternalVisualizer', 'EV']
[docs]class ExternalVisualizer:
"""This class allows to send data to an external Visualizer
Example:
This example sends a point cloud to the visualizer::
import cloudViewer as cv3d
import numpy as np
ev = cv3d.visualization.ExternalVisualizer()
pcd = cv3d.geometry.ccPointCloud(cv3d.utility.Vector3dVector(np.random.rand(100,3)))
ev.set(pcd)
Args:
address: The address where the visualizer is running.
The default is localhost.
timeout: The timeout for sending data in milliseconds.
"""
[docs] def __init__(self, address='tcp://127.0.0.1:51454', timeout=10000):
self.address = address
self.timeout = timeout
[docs] def set(self, obj=None, path='', time=0, layer='', connection=None):
"""Send CloudViewer objects for visualization to the visualizer.
Example:
To quickly send a single object just write::
ev.set(point_cloud)
To place the object at a specific location in the scene tree do::
ev.set(point_cloud, path='group/mypoints', time=42, layer='')
Note that depending on the visualizer some arguments like time or
layer may not be supported and will be ignored.
To set multiple objects use a list to pass multiple objects::
ev.set([point_cloud, mesh, camera])
Each entry in the list can be a tuple specifying all or some of the
location parameters::
ev.set(objs=[(point_cloud,'group/mypoints', 1, 'layer1'),
(mesh, 'group/mymesh'),
camera
]
Args:
obj: A geometry or camera object or a list of objects. See the
example seection for usage instructions.
path: A path describing a location in the scene tree.
time: An integer time value associated with the object.
layer: The layer associated with the object.
connection: A connection object to use for sending data. This
parameter can be used to override the default object.
"""
if connection is None:
connection = cv3d.io.rpc.Connection(address=self.address,
timeout=self.timeout)
result = []
if isinstance(obj, (tuple, list)):
# item can be just an object or a tuple with path, time, layer, e.g.,
# set(obj=[point_cloud, mesh, camera])
# set(obj=[(point_cloud,'group/mypoints', 1, 'layer1'),
# (mesh, 'group/mymesh'),
# camera
# ]
for item in obj:
if isinstance(item, (tuple, list)):
if len(item) in range(1, 5):
result.append(self.set(*item, connection=connection))
else:
result.append(self.set(item, connection=connection))
elif isinstance(obj, cv3d.geometry.ccPointCloud):
status = cv3d.io.rpc.set_point_cloud(obj,
path=path,
time=time,
layer=layer,
connection=connection)
result.append(status)
elif isinstance(obj,
(cv3d.t.geometry.TriangleMesh, cv3d.geometry.ccMesh)):
status = cv3d.io.rpc.set_triangle_mesh(obj,
path=path,
time=time,
layer=layer,
connection=connection)
result.append(status)
elif isinstance(obj, cv3d.camera.PinholeCameraParameters):
status = cv3d.io.rpc.set_legacy_camera(obj,
path=path,
time=time,
layer=layer,
connection=connection)
result.append(status)
else:
raise Exception("Unsupported object type '{}'".format(str(
type(obj))))
return all(result)
[docs] def set_time(self, time):
"""Sets the time in the external visualizer
Note that this function is a placeholder for future functionality and
not yet supported by the receiving visualizer.
Args:
time: The time value
"""
connection = cv3d.io.rpc.Connection(address=self.address,
timeout=self.timeout)
return cv3d.io.rpc.set_time(time, connection)
[docs] def set_active_camera(self, path):
"""Sets the active camera in the external visualizer
Note that this function is a placeholder for future functionality and
not yet supported by the receiving visualizer.
Args:
path: A path describing a location in the scene tree.
"""
connection = cv3d.io.rpc.Connection(address=self.address,
timeout=self.timeout)
return cv3d.io.rpc.set_active_camera(path, connection)
[docs] def draw(self, geometry=None, *args, **kwargs):
"""This function has the same functionality as 'set'.
This function is compatible with the standalone 'draw' function and can
be used to redirect calls to the external visualizer. Note that only
the geometry argument is supported, all other arguments will be
ignored.
Example:
Here we use draw with the default external visualizer::
import cloudViewer as cv3d
torus = cv3d.geometry.ccMesh.create_torus()
sphere = cv3d.geometry.ccMesh.create_sphere()
draw = cv3d.visualization.EV.draw
draw([ {'geometry': sphere, 'name': 'sphere'},
{'geometry': torus, 'name': 'torus', 'time': 1} ])
# now use the standard draw function as comparison
draw = cv3d.visualization.draw
draw([ {'geometry': sphere, 'name': 'sphere'},
{'geometry': torus, 'name': 'torus', 'time': 1} ])
Args:
geometry: The geometry to draw. This can be a geometry object, a
list of geometries. To pass additional information along with the
geometry we can use a dictionary. Supported keys for the dictionary
are 'geometry', 'name', and 'time'.
"""
if args or kwargs:
import warnings
warnings.warn(
"ExternalVisualizer.draw() does only support the 'geometry' argument",
Warning)
def add(g):
if isinstance(g, dict):
obj = g['geometry']
path = g.get('name', '')
time = g.get('time', 0)
self.set(obj=obj, path=path, time=time)
else:
self.set(g)
if isinstance(geometry, (tuple, list)):
for g in geometry:
add(g)
elif geometry is not None:
add(geometry)
# convenience default external visualizer
EV = ExternalVisualizer()