@@ -197,26 +197,30 @@ Tensor PyHandleToTensor(const py::handle& handle,
197197 // 5) tuple
198198 // 6) numpy.ndarray (value will be copied)
199199 // 7) Tensor (value will be copied)
200- std::string class_name (py::str (handle.get_type ()));
201- if (class_name == " <class 'bool'>" ) {
202- return BoolToTensor (static_cast <bool >(handle.cast <py::bool_>()), dtype,
203- device);
204- } else if (class_name == " <class 'int'>" ) {
205- return IntToTensor (static_cast <int64_t >(handle.cast <py::int_>()), dtype,
206- device);
207- } else if (class_name == " <class 'float'>" ) {
208- return DoubleToTensor (static_cast <double >(handle.cast <py::float_>()),
209- dtype, device);
210- } else if (class_name == " <class 'list'>" ) {
211- return PyListToTensor (handle.cast <py::list>(), dtype, device);
212- } else if (class_name == " <class 'tuple'>" ) {
213- return PyTupleToTensor (handle.cast <py::tuple>(), dtype, device);
214- } else if (class_name == " <class 'numpy.ndarray'>" ) {
215- return CastOptionalDtypeDevice (PyArrayToTensor (handle.cast <py::array>(),
216- /* inplace=*/ !force_copy),
217- dtype, device);
218- } else if (class_name.find (" open3d" ) != std::string::npos &&
219- class_name.find (" Tensor" ) != std::string::npos) {
200+ if (py::isinstance<py::bool_>(handle)) {
201+ return BoolToTensor (
202+ static_cast <bool >(py::reinterpret_borrow<py::bool_>(handle)),
203+ dtype, device);
204+ } else if (py::isinstance<py::int_>(handle)) {
205+ return IntToTensor (
206+ static_cast <int64_t >(py::reinterpret_borrow<py::int_>(handle)),
207+ dtype, device);
208+ } else if (py::isinstance<py::float_>(handle)) {
209+ return DoubleToTensor (
210+ static_cast <double >(py::reinterpret_borrow<py::float_>(handle)),
211+ dtype, device);
212+ } else if (py::isinstance<py::list>(handle)) {
213+ return PyListToTensor (py::reinterpret_borrow<py::list>(handle), dtype,
214+ device);
215+ } else if (py::isinstance<py::tuple>(handle)) {
216+ return PyTupleToTensor (py::reinterpret_borrow<py::tuple>(handle), dtype,
217+ device);
218+ } else if (py::isinstance<py::array>(handle)) {
219+ return CastOptionalDtypeDevice (
220+ PyArrayToTensor (py::reinterpret_borrow<py::array>(handle),
221+ /* inplace=*/ !force_copy),
222+ dtype, device);
223+ } else if (py::isinstance<Tensor>(handle)) {
220224 try {
221225 Tensor* tensor = handle.cast <Tensor*>();
222226 if (force_copy) {
@@ -228,8 +232,9 @@ Tensor PyHandleToTensor(const py::handle& handle,
228232 utility::LogError (" Cannot cast index to Tensor." );
229233 }
230234 } else {
231- utility::LogError (" PyHandleToTensor has invalid input type {}." ,
232- class_name);
235+ utility::LogError (
236+ " PyHandleToTensor has invalid input type {}." ,
237+ static_cast <std::string>(py::str (py::type::of (handle))));
233238 }
234239}
235240
0 commit comments