generated from yuecideng/PrimitivesFittingLib
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from yuecideng/yuecideng/RayCastRenderer
Add RayRastRenderer
- Loading branch information
Showing
9 changed files
with
399 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <iostream> | ||
#include <memory> | ||
|
||
#include <misc3d/pose_estimation/ray_cast_renderer.h> | ||
#include <misc3d/utils.h> | ||
#include <misc3d/vis/vis_utils.h> | ||
#include <open3d/camera/PinholeCameraIntrinsic.h> | ||
#include <open3d/geometry/PointCloud.h> | ||
#include <open3d/geometry/TriangleMesh.h> | ||
#include <open3d/io/TriangleMeshIO.h> | ||
#include <open3d/visualization/utility/DrawGeometry.h> | ||
|
||
int main(int argc, char *argv[]) { | ||
const open3d::camera::PinholeCameraIntrinsic camera_intrinsic( | ||
640, 480, 572.4114, 573.5704, 325.2611, 242.0489); | ||
|
||
misc3d::pose_estimation::RayCastRenderer renderer(camera_intrinsic); | ||
|
||
open3d::geometry::TriangleMesh mesh; | ||
bool ret = open3d::io::ReadTriangleMesh( | ||
"../examples/data/pose_estimation/model/obj.ply", mesh); | ||
mesh.Scale(0.001, Eigen::Vector3d::Zero()); | ||
const open3d::geometry::TriangleMesh mesh2 = mesh; | ||
|
||
Eigen::Matrix<double, 4, 4, Eigen::RowMajor> pose, pose2; | ||
pose << 0.29493218, 0.95551309, 0.00312103, -0.14527225, 0.89692822, | ||
-0.27572004, -0.34568516, 0.12533501, -0.32944616, 0.10475302, | ||
-0.93834537, 0.99371838, 0., 0., 0., 1.; | ||
pose2 << 0.29493218, 0.95551309, 0.00312103, -0.04527225, 0.89692822, | ||
-0.27572004, -0.34568516, 0.02533501, -0.32944616, 0.10475302, | ||
-0.93834537, 0.99371838, 0., 0., 0., 1.; | ||
|
||
misc3d::Timer timer; | ||
timer.Start(); | ||
ret = renderer.CastRays({mesh, mesh2}, {pose, pose2}); | ||
std::cout << "Ray cast time: " << timer.Stop() << std::endl; | ||
|
||
auto instance_pcds = renderer.GetInstancePointCloud(); | ||
instance_pcds[0].PaintUniformColor({1, 0, 0}); | ||
instance_pcds[1].PaintUniformColor({0, 1, 0}); | ||
const auto axis = open3d::geometry::TriangleMesh::CreateCoordinateFrame( | ||
0.1, Eigen::Vector3d::Zero()); | ||
|
||
open3d::visualization::DrawGeometries( | ||
{std::make_shared<const open3d::geometry::PointCloud>(instance_pcds[0]), | ||
std::make_shared<const open3d::geometry::PointCloud>(instance_pcds[1]), | ||
axis}, | ||
"Ray Cast Rendering"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
import open3d as o3d | ||
from torch import embedding | ||
import misc3d as m3d | ||
from matplotlib import pyplot as plt | ||
import time | ||
|
||
# Create a ray cast renderer. | ||
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic(640, 480, 572.4114, | ||
573.5704, 325.2611, | ||
242.0489) | ||
renderer = m3d.pose_estimation.RayCastRenderer(camera_intrinsic) | ||
|
||
# Load mesh and create a copy. | ||
mesh = o3d.io.read_triangle_mesh('../data/pose_estimation/model/obj.ply') | ||
mesh = mesh.scale(0.001, np.array([0, 0, 0])) | ||
mesh2 = mesh | ||
|
||
# Create mesh pose relative to camera. | ||
pose = np.array([[0.29493218, 0.95551309, 0.00312103, -0.14527225], | ||
[0.89692822, -0.27572004, -0.34568516, 0.12533501], | ||
[-0.32944616, 0.10475302, -0.93834537, 0.99371838], | ||
[0., 0., 0., 1.]]) | ||
pose2 = np.array([[0.29493218, 0.95551309, 0.00312103, -0.04527225], | ||
[0.89692822, -0.27572004, -0.34568516, 0.02533501], | ||
[-0.32944616, 0.10475302, -0.93834537, 0.99371838], | ||
[0., 0., 0., 1.]]) | ||
|
||
# Cast rays for meshes in the scene. | ||
t0 = time.time() | ||
ret = renderer.cast_rays([mesh, mesh2], [pose, pose2]) | ||
print('cast_rays:', time.time() - t0) | ||
|
||
# Visualize the depth and instance map. Both of them are open3d.core.Tensor type, | ||
# which can be converted into numpy array. | ||
depth = renderer.get_depth_map().numpy() | ||
instance_map = renderer.get_instance_map().numpy() | ||
instance_pcds = renderer.get_instance_point_cloud() | ||
instance_pcds[0].paint_uniform_color([1, 0, 0]) | ||
instance_pcds[1].paint_uniform_color([0, 1, 0]) | ||
|
||
plt.subplot(1, 2, 1) | ||
plt.imshow(depth) | ||
plt.subplot(1, 2, 2) | ||
plt.imshow(instance_map, vmax=2) | ||
plt.show() | ||
|
||
axis = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.1, | ||
origin=[0, 0, 0]) | ||
o3d.visualization.draw_geometries([instance_pcds[0], instance_pcds[1], axis], | ||
"Ray Cast Rendering") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include <open3d/camera/PinholeCameraIntrinsic.h> | ||
#include <open3d/geometry/Image.h> | ||
#include <open3d/t/geometry/RaycastingScene.h> | ||
#include <open3d/t/geometry/TriangleMesh.h> | ||
#include <Eigen/Dense> | ||
|
||
namespace misc3d { | ||
|
||
namespace pose_estimation { | ||
|
||
/** | ||
* @brief Ray cast renderer for depth map, instance map, point clouds and | ||
* normals map generation. | ||
* Currently only support CPU. | ||
* TODO: Add CUDA suppot. | ||
*/ | ||
class RayCastRenderer { | ||
public: | ||
/** | ||
* @brief Construct a new Ray Cast Renderer defined by a camera intrinsic. | ||
* The rays start from the origin [0, 0, 0] will be created based on the | ||
* intrinsic inside constructor. | ||
* | ||
* @param intrinsic | ||
*/ | ||
explicit RayCastRenderer( | ||
const open3d::camera::PinholeCameraIntrinsic &intrinsic); | ||
|
||
virtual ~RayCastRenderer() {} | ||
|
||
/** | ||
* @brief Compute the first intersection of the rays with the scene. | ||
* The result is stored in a map with the following keys: | ||
* \b t_hit: the distance of each hit | ||
* \b geometry_ids: the instance id of the geometries | ||
* \b primitive_normals: the normals of the geometries | ||
* | ||
* @param mesh_list | ||
* @param pose_list | ||
* @return true | ||
* @return false | ||
*/ | ||
bool CastRays(const std::vector<open3d::geometry::TriangleMesh> &mesh_list, | ||
const std::vector<Eigen::Matrix4d> &pose_list); | ||
|
||
/** | ||
* @brief Get Depth Map | ||
* | ||
* @return open3d::core::Tensor | ||
*/ | ||
open3d::core::Tensor GetDepthMap() const; | ||
|
||
/** | ||
* @brief Get Instance Map | ||
* The id is from 0 to num_instances - 1. | ||
* | ||
* @return open3d::core::Tensor | ||
*/ | ||
open3d::core::Tensor GetInstanceMap() const; | ||
|
||
/** | ||
* @brief Get PointCloud from depth map with valid value. | ||
* | ||
* @return open3d::geometry::PointCloud | ||
*/ | ||
open3d::geometry::PointCloud GetPointCloud() const; | ||
|
||
/** | ||
* @brief Get Instance PointCloud individully. | ||
* | ||
* @return std::vector<open3d::geometry::PointCloud> | ||
*/ | ||
std::vector<open3d::geometry::PointCloud> GetInstancePointCloud() const; | ||
|
||
private: | ||
std::unordered_map<std::string, open3d::core::Tensor> ray_cast_results_; | ||
int width_; | ||
int height_; | ||
open3d::core::Tensor rays_; | ||
size_t num_instance_; | ||
}; | ||
|
||
} // namespace pose_estimation | ||
|
||
} // namespace misc3d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.