tango_3d_reconstruction_helper.cpp
Go to the documentation of this file.
1 // Copyright 2017 Intermodalics All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 
17 #include <glog/logging.h>
18 
21  const ros::NodeHandle& node_handle, double* t3dr_resolution,
22  Tango3DR_ReconstructionContext* t3dr_context,
23  Tango3DR_CameraCalibration* t3dr_color_camera_intrinsics) {
24  const char* function_name = "TangoRosNodelet::TangoSetup3DRConfig()";
25 
26  Tango3DR_Config t3dr_config =
28  Tango3DR_Status result;
29  const char* resolution = "resolution";
30  double t3dr_resolution_param;
32  t3dr_resolution_param, TANGO_3DR_DEFAULT_RESOLUTION);
33  *t3dr_resolution = t3dr_resolution_param;
34  result = Tango3DR_Config_setDouble(t3dr_config, resolution, t3dr_resolution_param);
35  if (result != TANGO_3DR_SUCCESS) {
36  LOG(ERROR) << function_name << ", Tango3DR_Config_setDouble "
37  << resolution << " error: " << result;
38  return result;
39  }
40  const char* generate_color = "generate_color";
41  result = Tango3DR_Config_setBool(t3dr_config, generate_color, true);
42  if (result != TANGO_3DR_SUCCESS) {
43  LOG(ERROR) << function_name << ", Tango3DR_Config_setBool "
44  << generate_color << " error: " << result;
45  return result;
46  }
47  const char* use_floorplan = "use_floorplan";
48  result = Tango3DR_Config_setBool(t3dr_config, use_floorplan, true);
49  if (result != TANGO_3DR_SUCCESS) {
50  LOG(ERROR) << function_name << ", Tango3DR_Config_setBool "
51  << use_floorplan << " error: " << result;
52  return result;
53  }
54  const char* use_space_clearing = "use_space_clearing";
55  bool t3dr_use_space_clearing;
57  t3dr_use_space_clearing, TANGO_3DR_DEFAULT_USE_SPACE_CLEARING);
58  result = Tango3DR_Config_setBool(t3dr_config, use_space_clearing,
59  t3dr_use_space_clearing);
60  if (result != TANGO_3DR_SUCCESS) {
61  LOG(ERROR) << function_name << ", Tango3DR_Config_setBool "
62  << use_space_clearing << " error: " << result;
63  return result;
64  }
65  const char* min_num_vertices = "min_num_vertices";
66  int t3dr_min_num_vertices;
68  t3dr_min_num_vertices, TANGO_3DR_DEFAULT_MIN_NUM_VERTICES);
69  result = Tango3DR_Config_setInt32(t3dr_config, min_num_vertices,
70  t3dr_min_num_vertices);
71  if (result != TANGO_3DR_SUCCESS) {
72  LOG(ERROR) << function_name << ", Tango3DR_Config_setInt32 "
73  << min_num_vertices << " error: " << result;
74  return result;
75  }
76  const char* update_method = "update_method";
77  int t3dr_update_method;
79  t3dr_update_method, TANGO_3DR_DEFAULT_UPDATE_METHOD);
80  result = Tango3DR_Config_setInt32(t3dr_config, update_method,
81  t3dr_update_method);
82  if (result != TANGO_3DR_SUCCESS) {
83  LOG(ERROR) << function_name << ", Tango3DR_Config_setInt32 "
84  << update_method << " error: " << result;
85  return result;
86  }
87  const char* max_voxel_weight = "max_voxel_weight";
88  int t3dr_max_voxel_weight;
90  t3dr_max_voxel_weight, TANGO_3DR_DEFAULT_MAX_VOXEL_WEIGHT);
91  result = Tango3DR_Config_setInt32(t3dr_config, max_voxel_weight,
92  t3dr_max_voxel_weight);
93  if (result != TANGO_3DR_SUCCESS) {
94  LOG(ERROR) << function_name << ", Tango3DR_Config_setInt32 "
95  << max_voxel_weight << " error: " << result;
96  return result;
97  }
98  const char* floorplan_max_error = "floorplan_max_error";
99  double t3dr_floorplan_max_error;
101  t3dr_floorplan_max_error, TANGO_3DR_DEFAULT_FLOORPLAN_MAX_ERROR);
102  result = Tango3DR_Config_setDouble(t3dr_config, floorplan_max_error,
103  t3dr_floorplan_max_error);
104  if (result != TANGO_3DR_SUCCESS) {
105  LOG(ERROR) << function_name << ", Tango3DR_Config_setDouble "
106  << floorplan_max_error << " error: " << result;
107  return result;
108  }
109 
110  *t3dr_context = Tango3DR_ReconstructionContext_create(t3dr_config);
111  if (*t3dr_context == nullptr) {
112  LOG(ERROR) << function_name << ", Tango3DR_ReconstructionContext_create error: "
113  "Unable to create 3DR context.";
114  return TANGO_3DR_ERROR;
115  }
116  // Configure the color camera intrinsics to be used with updates to the mesh.
118  *t3dr_context, t3dr_color_camera_intrinsics);
119  if (result != TANGO_3DR_SUCCESS) {
120  LOG(ERROR) << function_name << ", Unable to set color calibration.";
121  return TANGO_3DR_ERROR;
122  }
123  return Tango3DR_Config_destroy(t3dr_config);
124 }
125 
127  TangoSupport_PointCloudManager* point_cloud_manager,
128  TangoSupport_ImageBufferManager* image_buffer_manager,
129  Tango3DR_Pose* last_camera_depth_pose,
130  Tango3DR_Pose* last_camera_color_pose,
131  Tango3DR_GridIndexArray* t3dr_updated_indices) {
132  // Get latest point cloud.
133  TangoPointCloud* last_point_cloud;
134  TangoSupport_getLatestPointCloud(point_cloud_manager, &last_point_cloud);
135  Tango3DR_PointCloud t3dr_depth;
136  t3dr_depth.timestamp = last_point_cloud->timestamp;
137  t3dr_depth.num_points = last_point_cloud->num_points;
138  t3dr_depth.points = reinterpret_cast<Tango3DR_Vector4*>(
139  last_point_cloud->points);
140  // Get latest image.
141  TangoImageBuffer* last_color_image_buffer;
142  if (image_buffer_manager != nullptr) {
143  TangoSupport_getLatestImageBuffer(image_buffer_manager,
144  &last_color_image_buffer);
145  Tango3DR_ImageBuffer t3dr_image;
146  t3dr_image.width = last_color_image_buffer->width;
147  t3dr_image.height = last_color_image_buffer->height;
148  t3dr_image.stride = last_color_image_buffer->stride;
149  t3dr_image.timestamp = last_color_image_buffer->timestamp;
150  t3dr_image.format = static_cast<Tango3DR_ImageFormatType>(
151  last_color_image_buffer->format);
152  t3dr_image.data = last_color_image_buffer->data;
153  // Get updated mesh segment indices.
154  Tango3DR_Status result =
155  Tango3DR_updateFromPointCloud(t3dr_context, &t3dr_depth, last_camera_depth_pose,
156  &t3dr_image, last_camera_color_pose,
157  t3dr_updated_indices);
158  if (result != TANGO_3DR_SUCCESS) {
159  LOG(ERROR) << "Tango3DR_updateFromPointCloud failed with error code "
160  << result;
161  }
162  }
163 }
164 
166  const Tango3DR_ReconstructionContext& t3dr_context,
167  const Tango3DR_GridIndexArray& t3dr_updated_indices,
168  double time_offset, const std::string& base_frame_id,
169  visualization_msgs::MarkerArray* mesh_marker_array) {
170  for (size_t i = 0; i < t3dr_updated_indices.num_indices; ++i) {
171  // Extract Tango mesh from updated index.
172  Tango3DR_Mesh tango_mesh;
174  t3dr_context, t3dr_updated_indices.indices[i], &tango_mesh);
175  if(result != TANGO_3DR_SUCCESS) {
176  LOG(ERROR) << "Tango3DR_extractMeshSegment failed.";
177  continue;
178  }
179  if (tango_mesh.num_faces == 0) {
180  LOG(INFO) << "Empty mesh extracted.";
181  continue;
182  }
183  // Make mesh marker from tango mesh.
184  visualization_msgs::Marker mesh_marker;
185  tango_ros_conversions_helper::toMeshMarker(t3dr_updated_indices.indices[i],
186  tango_mesh, time_offset, base_frame_id, &mesh_marker);
187  // Free tango mesh once we are finished with it.
188  result = Tango3DR_Mesh_destroy(&tango_mesh);
189  if (result != TANGO_3DR_SUCCESS) {
190  LOG(ERROR) << "Tango3DR_Mesh_destroy failed with error code: "
191  << result;
192  }
193  if (mesh_marker.points.empty()) {
194  LOG(INFO) << "Empty mesh marker.";
195  continue;
196  }
197  mesh_marker_array->markers.push_back(mesh_marker);
198  }
199 }
200 
202  const Tango3DR_ReconstructionContext& t3dr_context,
203  double time_offset, const std::string& base_frame_id,
204  double t3dr_resolution, uint8_t threshold,
205  nav_msgs::OccupancyGrid* occupancy_grid) {
206  Tango3DR_Status result = Tango3DR_updateFullFloorplan(t3dr_context);
207  if (result == TANGO_3DR_SUCCESS) {
208  Tango3DR_Vector2 origin;
209  Tango3DR_ImageBuffer image_grid;
211  t3dr_context, TANGO_3DR_LAYER_SPACE, &origin, &image_grid);
212  if (result == TANGO_3DR_SUCCESS) {
214  time_offset, base_frame_id, t3dr_resolution, threshold, occupancy_grid);
215  } else {
216  LOG(ERROR) << "Tango3DR_extractFullFloorplanImage failed with error"
217  "code: " << result;
218  return false;
219  }
220  result = Tango3DR_ImageBuffer_destroy(&image_grid);
221  if (result != TANGO_3DR_SUCCESS) {
222  LOG(ERROR) << "Tango3DR_ImageBuffer_destroy failed with error "
223  "code: " << result;
224  return false;
225  }
226  } else {
227  LOG(ERROR) << "Tango3DR_updateFullFloorplan failed with error "
228  "code: " << result;
229  return false;
230  }
231  return true;
232 }
233 } // namespace tango_3d_reconstruction_helper
struct _Tango3DR_Config * Tango3DR_Config
float Tango3DR_Vector4[4]
void ExtractMeshAndConvertToMarkerArray(const Tango3DR_ReconstructionContext &t3dr_context, const Tango3DR_GridIndexArray &t3dr_updated_indices, double time_offset, const std::string &base_frame_id, visualization_msgs::MarkerArray *mesh_marker_array)
TANGO_3DR_ERROR
Tango3DR_Status Tango3DR_Config_destroy(Tango3DR_Config config)
Tango3DR_Status Tango3DR_extractFullFloorplanImage(const Tango3DR_ReconstructionContext context, Tango3DR_FloorplanLayer layer, Tango3DR_Vector2 *origin, Tango3DR_ImageBuffer *image)
Tango3DR_Status
Tango3DR_Status Tango3DR_extractMeshSegment(const Tango3DR_ReconstructionContext context, const Tango3DR_GridIndex grid_index, Tango3DR_Mesh *mesh)
uint32_t num_faces
Tango3DR_Status TangoSetup3DRConfig(const ros::NodeHandle &node_handle, double *t3dr_resolution, Tango3DR_ReconstructionContext *t3dr_context, Tango3DR_CameraCalibration *t3dr_color_camera_intrinsics)
TANGO_3DR_LAYER_SPACE
Tango3DR_Status Tango3DR_Config_setInt32(Tango3DR_Config config, const char *key, int32_t value)
float Tango3DR_Vector2[2]
Tango3DR_Status Tango3DR_updateFullFloorplan(const Tango3DR_ReconstructionContext context)
Tango3DR_GridIndex * indices
Tango3DR_Status Tango3DR_updateFromPointCloud(Tango3DR_ReconstructionContext context, const Tango3DR_PointCloud *cloud, const Tango3DR_Pose *cloud_pose, const Tango3DR_ImageBuffer *color_image, const Tango3DR_Pose *color_image_pose, Tango3DR_GridIndexArray *updated_indices)
Tango3DR_Status Tango3DR_Config_setDouble(Tango3DR_Config config, const char *key, double value)
TangoErrorType TangoSupport_getLatestImageBuffer(TangoSupport_ImageBufferManager *manager, TangoImageBuffer **image_buffer)
TANGO_3DR_SUCCESS
Tango3DR_Status Tango3DR_ReconstructionContext_setColorCalibration(const Tango3DR_ReconstructionContext context, const Tango3DR_CameraCalibration *calibration)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Tango3DR_Status Tango3DR_Mesh_destroy(Tango3DR_Mesh *mesh)
TangoErrorType TangoSupport_getLatestPointCloud(TangoSupport_PointCloudManager *manager, TangoPointCloud **latest_point_cloud)
TangoImageFormatType format
bool ExtractFloorPlanImageAndConvertToOccupancyGrid(const Tango3DR_ReconstructionContext &t3dr_context, double time_offset, const std::string &base_frame_id, double t3dr_resolution, uint8_t threshold, nav_msgs::OccupancyGrid *occupancy_grid)
uint32_t num_points
Tango3DR_ImageFormatType format
Tango3DR_Config Tango3DR_Config_create(Tango3DR_ConfigType config_type)
Tango3DR_Status Tango3DR_ImageBuffer_destroy(Tango3DR_ImageBuffer *image)
Tango3DR_Status Tango3DR_Config_setBool(Tango3DR_Config config, const char *key, bool value)
struct _Tango3DR_ReconstructionContext * Tango3DR_ReconstructionContext
Tango3DR_Vector4 * points
void toOccupancyGrid(const Tango3DR_ImageBuffer &image_grid, const Tango3DR_Vector2 &origin, double time_offset, const std::string &base_frame_id, double resolution, uint8_t threshold, nav_msgs::OccupancyGrid *occupancy_grid)
world coordinates (x: right, y: up).
void toMeshMarker(const Tango3DR_GridIndex &grid_index, const Tango3DR_Mesh &tango_mesh, double time_offset, const std::string &base_frame_id, visualization_msgs::Marker *mesh_marker)
Tango3DR_ImageFormatType
void UpdateMesh(const Tango3DR_ReconstructionContext &t3dr_context, TangoSupport_PointCloudManager *point_cloud_manager, TangoSupport_ImageBufferManager *image_buffer_manager, Tango3DR_Pose *last_camera_depth_pose, Tango3DR_Pose *last_camera_color_pose, Tango3DR_GridIndexArray *t3dr_updated_indices)
Tango3DR_ReconstructionContext Tango3DR_ReconstructionContext_create(const Tango3DR_Config context_config)
float(* points)[4]
TANGO_3DR_CONFIG_RECONSTRUCTION


tango_ros_native
Author(s):
autogenerated on Mon Jun 10 2019 15:37:51