Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized loops implementation in conversion method to reduce CPU load #47

Open
wants to merge 2 commits into
base: melodic-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions include/depthimage_to_laserscan/DepthImageToLaserScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,34 @@ namespace depthimage_to_laserscan
const double unit_scaling = depthimage_to_laserscan::DepthTraits<T>::toMeters( T(1) );
const float constant_x = unit_scaling / cam_model.fx();

const T* depth_row = reinterpret_cast<const T*>(&depth_msg->data[0]);
const T* depth_row0 = reinterpret_cast<const T*>(&depth_msg->data[0]);
const int row_step = depth_msg->step / sizeof(T);

const int offset = (int)(center_y - scan_height/2);
depth_row += offset*row_step; // Offset to center of image

for(int v = offset; v < offset+scan_height_; ++v, depth_row += row_step){
for (int u = 0; u < (int)depth_msg->width; ++u) // Loop over each pixel in row
{
const int depth_row_offset_start = offset*row_step;
const int depth_row_offset_end = (offset+scan_height_)*row_step;

double z_coef = depthimage_to_laserscan::DepthTraits<T>::toMeters(1.0);

for (int u = 0; u < (int)depth_msg->width; ++u) {
const double th = -atan2((double)(u - center_x) * constant_x, unit_scaling); // Atan2(x, z), but depth divides out
const int index = (th - scan_msg->angle_min) / scan_msg->angle_increment;

double x_coef = (u - center_x) * constant_x;
double r_coef = hypot(x_coef, z_coef);

for(const T* depth_row = depth_row0 + depth_row_offset_start;
depth_row < depth_row0 + depth_row_offset_end;
depth_row += row_step) {

const T depth = depth_row[u];

double r = depth; // Assign to pass through NaNs and Infs
const double th = -atan2((double)(u - center_x) * constant_x, unit_scaling); // Atan2(x, z), but depth divides out
const int index = (th - scan_msg->angle_min) / scan_msg->angle_increment;
double r = depth;

if (depthimage_to_laserscan::DepthTraits<T>::valid(depth)){ // Not NaN or Inf
// Calculate in XYZ
double x = (u - center_x) * depth * constant_x;
double z = depthimage_to_laserscan::DepthTraits<T>::toMeters(depth);

// Calculate actual distance
r = hypot(x, z);
r = r_coef * depth;
}

// Determine if this point should be used.
Expand Down
5 changes: 1 addition & 4 deletions src/DepthImageToLaserScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ bool DepthImageToLaserScan::use_point(const float new_value, const float old_val

// Infs are preferable over NaNs (more information)
if(!new_finite && !old_finite){ // Both are not NaN or Inf.
if(!std::isnan(new_value)){ // new is not NaN, so use it's +-Inf value.
return true;
}
return false; // Do not replace old_value
return !std::isnan(new_value); // new is not NaN, so use it's +-Inf value.
}

// If not in range, don't bother
Expand Down