ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
rpc.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <memory>
9 
22 #include "pybind/docstring.h"
23 
24 namespace cloudViewer {
25 namespace io {
26 
27 void pybind_rpc(py::module& m_io) {
28  py::module m_rpc = m_io.def_submodule("rpc");
29  // this is to cleanly shutdown the zeromq context on windows.
30  auto atexit = py::module::import("atexit");
31  atexit.attr("register")(
32  py::cpp_function([]() { rpc::DestroyZMQContext(); }));
33 
34  py::class_<rpc::ConnectionBase, std::shared_ptr<rpc::ConnectionBase>>(
35  m_rpc, "_ConnectionBase");
36 
37  py::class_<rpc::Connection, std::shared_ptr<rpc::Connection>,
38  rpc::ConnectionBase>(m_rpc, "Connection", R"doc(
39  The default connection class which uses a ZeroMQ socket.
40  )doc")
41  .def(py::init([](std::string address, int connect_timeout,
42  int timeout) {
43  return std::shared_ptr<rpc::Connection>(
44  new rpc::Connection(address, connect_timeout,
45  timeout));
46  }),
47  "Creates a connection object",
48  "address"_a = "tcp://127.0.0.1:51454",
49  "connect_timeout"_a = 5000, "timeout"_a = 10000);
50 
51  py::class_<rpc::BufferConnection, std::shared_ptr<rpc::BufferConnection>,
52  rpc::ConnectionBase>(m_rpc, "BufferConnection", R"doc(
53  A connection writing to a memory buffer.
54  )doc")
55  .def(py::init<>())
56  .def(
57  "get_buffer",
58  [](const rpc::BufferConnection& self) {
59  return py::bytes(self.buffer().str());
60  },
61  "Returns a copy of the buffer.");
62 
63  py::class_<rpc::DummyReceiver, std::shared_ptr<rpc::DummyReceiver>>(
64  m_rpc, "_DummyReceiver",
65  "Dummy receiver for the server side receiving requests from a "
66  "client.")
67  .def(py::init([](const std::string& address, int timeout) {
68  return std::shared_ptr<rpc::DummyReceiver>(
69  new rpc::DummyReceiver(address, timeout));
70  }),
71  "Creates the receiver object which can be used for testing "
72  "connections.",
73  "address"_a = "tcp://127.0.0.1:51454", "timeout"_a = 10000)
74  .def("start", &rpc::DummyReceiver::Start,
75  "Starts the receiver mainloop in a new thread.")
76  .def("stop", &rpc::DummyReceiver::Stop,
77  "Stops the receiver mainloop and joins the thread. This "
78  "function blocks until the mainloop is done with processing "
79  "messages that have already been received.");
80 
81  m_rpc.def("destroy_zmq_context", &rpc::DestroyZMQContext,
82  "Destroys the ZMQ context.");
83 
84  m_rpc.def("set_point_cloud", &rpc::SetPointCloud, "pcd"_a, "path"_a = "",
85  "time"_a = 0, "layer"_a = "",
86  "connection"_a = std::shared_ptr<rpc::Connection>(),
87  "Sends a point cloud message to a viewer.");
89  m_rpc, "set_point_cloud",
90  {
91  {"pcd", "Point cloud object."},
92  {"path", "A path descriptor, e.g., 'mygroup/points'."},
93  {"time", "The time associated with this data."},
94  {"layer", "The layer associated with this data."},
95  {"connection",
96  "A Connection object. Use None to automatically create "
97  "the connection."},
98  });
99 
100  m_rpc.def("set_triangle_mesh",
101  py::overload_cast<const ccMesh&, const std::string&, int,
102  const std::string&,
103  std::shared_ptr<rpc::ConnectionBase>>(
105  "mesh"_a, "path"_a = "", "time"_a = 0, "layer"_a = "",
106  "connection"_a = std::shared_ptr<rpc::ConnectionBase>(),
107  R"doc(Sends a triangle mesh to a viewer.
108  Args:
109  mesh (ccMesh): The triangle mesh.
110  path (str): The path in the scene graph.
111  time (int): The time associated with the data.
112  layer (str): A layer name that can be used by receivers that support layers.
113  connection (cv3d.io.rpc.Connection): A connection object that will be used for sending the data.
114 
115  Returns:
116  Returns True if the data was successfully received.
117  )doc");
118 
119  m_rpc.def("set_triangle_mesh",
120  py::overload_cast<const t::geometry::TriangleMesh&,
121  const std::string&, int, const std::string&,
122  std::shared_ptr<rpc::ConnectionBase>>(
124  "mesh"_a, "path"_a = "", "time"_a = 0, "layer"_a = "",
125  "connection"_a = std::shared_ptr<rpc::ConnectionBase>(),
126  R"doc(Sends a triangle mesh to a viewer.
127 Args:
128 mesh (cv3d.t.geometry.TriangleMesh): The triangle mesh.
129 path (str): The path in the scene graph.
130 time (int): The time associated with the data.
131 layer (str): A layer name that can be used by receivers that support layers.
132 connection (cv3d.io.rpc.Connection): A connection object that will be used for sending the data.
133 
134 Returns:
135 Returns True if the data was successfully received.
136 )doc");
137 
138  m_rpc.def("set_mesh_data", &rpc::SetMeshData, "path"_a = "", "time"_a = 0,
139  "layer"_a = "", "vertices"_a = core::Tensor({0}, core::Float32),
140  "vertex_attributes"_a = std::map<std::string, core::Tensor>(),
141  "faces"_a = core::Tensor({0}, core::Int32),
142  "face_attributes"_a = std::map<std::string, core::Tensor>(),
143  "lines"_a = core::Tensor({0}, core::Int32),
144  "line_attributes"_a = std::map<std::string, core::Tensor>(),
145  "material"_a = "",
146  "material_scalar_attributes"_a = std::map<std::string, float>(),
147  "material_vector_attributes"_a =
148  std::map<std::string, Eigen::Vector4f>(),
149  "texture_maps"_a = std::map<std::string, t::geometry::Image>(),
150  "o3d_type"_a = "",
151  "connection"_a = std::shared_ptr<rpc::ConnectionBase>(),
152  "Sends a set_mesh_data message.");
154  m_rpc, "set_mesh_data",
155  {
156  {"path", "A path descriptor, e.g., 'mygroup/points'."},
157  {"time", "The time associated with this data."},
158  {"layer", "The layer associated with this data."},
159  {"vertices", "Tensor defining the vertices."},
160  {"vertex_attributes",
161  "dict of Tensors with vertex attributes."},
162  {"faces", "Tensor defining the faces with vertex indices."},
163  {"face_attributes",
164  "dict of Tensors with face attributes."},
165  {"lines", "Tensor defining lines with vertex indices."},
166  {"line_attributes",
167  "dict of Tensors with line attributes."},
168  {"material",
169  "Basic Material for geometry drawing. Must be non-empty "
170  "if any material attributes or texture maps are "
171  "provided."},
172  {"material_scalar_attributes",
173  "dict of material scalar attributes for geometry drawing "
174  "(e.g. ``point_size``, ``line_width`` or "
175  "``base_reflectance``)."},
176  {"material_vector_attributes",
177  "dict of material Vector4f attributes for geometry "
178  "drawing (e.g. ``base_color`` or ``absorption_color``)"},
179  {"texture_maps", "dict of Images with textures."},
180  {"o3d_type", R"doc(The type of the geometry. This is one of
181  ``PointCloud``, ``LineSet``, ``TriangleMesh``. This argument should be
182  specified for partial data that has no primary key data, e.g., a
183  triangle mesh without vertices but with other attribute tensors.)doc"},
184  {"connection",
185  "A Connection object. Use None to automatically create "
186  "the connection."},
187  });
188 
189  m_rpc.def("set_legacy_camera", &rpc::SetLegacyCamera, "camera"_a,
190  "path"_a = "", "time"_a = 0, "layer"_a = "",
191  "connection"_a = std::shared_ptr<rpc::ConnectionBase>(),
192  "Sends a PinholeCameraParameters object.");
194  m_rpc, "set_legacy_camera",
195  {
196  {"path", "A path descriptor, e.g., 'mygroup/camera'."},
197  {"time", "The time associated with this data."},
198  {"layer", "The layer associated with this data."},
199  {"connection",
200  "A Connection object. Use None to automatically create "
201  "the connection."},
202  });
203 
204  m_rpc.def("set_time", &rpc::SetTime, "time"_a,
205  "connection"_a = std::shared_ptr<rpc::ConnectionBase>(),
206  "Sets the time in the external visualizer.");
208  m_rpc, "set_time",
209  {
210  {"time", "The time value to set."},
211  {"connection",
212  "A Connection object. Use None to automatically create "
213  "the connection."},
214  });
215 
216  m_rpc.def("set_active_camera", &rpc::SetActiveCamera, "path"_a,
217  "connection"_a = std::shared_ptr<rpc::ConnectionBase>(),
218  "Sets the object with the specified path as the active camera.");
220  m_rpc, "set_active_camera",
221  {
222  {"path", "A path descriptor, e.g., 'mygroup/camera'."},
223  {"connection",
224  "A Connection object. Use None to automatically create "
225  "the connection."},
226  });
227 
228  m_rpc.def("data_buffer_to_meta_geometry", &rpc::DataBufferToMetaGeometry,
229  "data"_a, R"doc(
230  This function returns the geometry, the path and the time stored in a
231  SetMeshData message. data must contain the Request header message followed
232  by the SetMeshData message. The function returns None for the geometry if not
233  successful.
234  )doc");
235 }
236 
237 } // namespace io
238 } // namespace cloudViewer
void * bytes
Triangular mesh.
Definition: ecvMesh.h:35
Implements a connection writing to a buffer.
Base class for all connections.
void Start()
Starts the receiver mainloop in a new thread.
Definition: ZMQReceiver.cpp:45
A triangle mesh contains vertices and triangles.
Definition: TriangleMesh.h:98
const Dtype Int32
Definition: Dtype.cpp:46
const Dtype Float32
Definition: Dtype.cpp:42
void FunctionDocInject(py::module &pybind_module, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:76
bool SetTriangleMesh(const ccMesh &mesh, const std::string &path, int time, const std::string &layer, std::shared_ptr< ConnectionBase > connection)
std::tuple< std::string, double, std::shared_ptr< t::geometry::Geometry > > DataBufferToMetaGeometry(std::string &data)
bool SetPointCloud(const ccPointCloud &pcd, const std::string &path, int time, const std::string &layer, std::shared_ptr< ConnectionBase > connection)
bool SetActiveCamera(const std::string &path, std::shared_ptr< ConnectionBase > connection)
bool SetLegacyCamera(const camera::PinholeCameraParameters &camera, const std::string &path, int time, const std::string &layer, std::shared_ptr< ConnectionBase > connection)
bool SetMeshData(const std::string &path, int time, const std::string &layer, const core::Tensor &vertices, const std::map< std::string, core::Tensor > &vertex_attributes, const core::Tensor &faces, const std::map< std::string, core::Tensor > &face_attributes, const core::Tensor &lines, const std::map< std::string, core::Tensor > &line_attributes, const std::string &material, const std::map< std::string, float > &material_scalar_attributes, const std::map< std::string, std::array< float, 4 >> &material_vector_attributes, const std::map< std::string, t::geometry::Image > &texture_maps, const std::string &o3d_type, std::shared_ptr< ConnectionBase > connection)
bool SetTime(int time, std::shared_ptr< ConnectionBase > connection)
void pybind_rpc(py::module &m)
Definition: rpc.cpp:27
Generic file read and write utility for python interface.