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

Fixed getColor method for HSV to RGB conversion #772

Merged
merged 1 commit into from
Jun 26, 2014
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pcl::visualization::PointCloudColorHandlerHSVField<PointT>::PointCloudColorHandl
}

///////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT> bool
template <typename PointT> bool
pcl::visualization::PointCloudColorHandlerHSVField<PointT>::getColor (vtkSmartPointer<vtkDataArray> &scalars) const
{
if (!capable_ || !cloud_)
Expand All @@ -224,10 +224,10 @@ pcl::visualization::PointCloudColorHandlerHSVField<PointT>::getColor (vtkSmartPo
reinterpret_cast<vtkUnsignedCharArray*>(&(*scalars))->SetNumberOfTuples (nr_points);
unsigned char* colors = reinterpret_cast<vtkUnsignedCharArray*>(&(*scalars))->GetPointer (0);

int j = 0;
int idx = 0;
// If XYZ present, check if the points are invalid
int x_idx = -1;

for (size_t d = 0; d < fields_.size (); ++d)
if (fields_[d].name == "x")
x_idx = static_cast<int> (d);
Expand All @@ -239,80 +239,109 @@ pcl::visualization::PointCloudColorHandlerHSVField<PointT>::getColor (vtkSmartPo
{
// Copy the value at the specified field
if (!pcl_isfinite (cloud_->points[cp].x) ||
!pcl_isfinite (cloud_->points[cp].y) ||
!pcl_isfinite (cloud_->points[cp].y) ||
!pcl_isfinite (cloud_->points[cp].z))
continue;

int idx = j * 3;

///@todo do this with the point_types_conversion in common, first template it!

float h = cloud_->points[cp].h;
float v = cloud_->points[cp].v;
float s = cloud_->points[cp].s;

// Fill color data with HSV here:
if (cloud_->points[cp].s == 0)
// restrict the hue value to [0,360[
h = h < 0.0f ? h - (((int)h)/360 - 1)*360 : h - (((int)h)/360)*360;

// restrict s and v to [0,1]
if (s > 1.0f) s = 1.0f;
if (s < 0.0f) s = 0.0f;
if (v > 1.0f) v = 1.0f;
if (v < 0.0f) v = 0.0f;

if (s == 0.0f)
{
colors[idx] = colors[idx+1] = colors[idx+2] = cloud_->points[cp].v;
return;
}
float a = cloud_->points[cp].h / 60;
int i = floor (a);
float f = a - i;
float p = cloud_->points[cp].v * (1 - cloud_->points[cp].s);
float q = cloud_->points[cp].v * (1 - cloud_->points[cp].s * f);
float t = cloud_->points[cp].v * (1 - cloud_->points[cp].s * (1 - f));

switch (i)
colors[idx] = colors[idx+1] = colors[idx+2] = v*255;
}
else
{
case 0:
colors[idx] = cloud_->points[cp].v; colors[idx+1] = t; colors[idx+2] = p; break;
case 1:
colors[idx] = q; colors[idx+1] = cloud_->points[cp].v; colors[idx+2] = p; break;
case 2:
colors[idx] = p; colors[idx+1] = cloud_->points[cp].v; colors[idx+2] = t; break;
case 3:
colors[idx] = p; colors[idx+1] = q; colors[idx+2] = cloud_->points[cp].v; break;
case 4:
colors[idx] = t; colors[idx+1] = p; colors[idx+2] = cloud_->points[cp].v; break;
default:
colors[idx] = cloud_->points[cp].v; colors[idx+1] = p; colors[idx+2] = q; break;
// calculate p, q, t from HSV-values
float a = h / 60;
int i = floor (a);
float f = a - i;
float p = v * (1 - s);
float q = v * (1 - s * f);
float t = v * (1 - s * (1 - f));

switch (i)
{
case 0:
colors[idx] = v*255; colors[idx+1] = t*255; colors[idx+2] = p*255; break;
case 1:
colors[idx] = q*255; colors[idx+1] = v*255; colors[idx+2] = p*255; break;
case 2:
colors[idx] = p*255; colors[idx+1] = v*255; colors[idx+2] = t*255; break;
case 3:
colors[idx] = p*255; colors[idx+1] = q*255; colors[idx+2] = v*255; break;
case 4:
colors[idx] = t*255; colors[idx+1] = p*255; colors[idx+2] = v*255; break;
case 5:
colors[idx] = v*255; colors[idx+1] = p*255; colors[idx+2] = q*255; break;
}
}
j++;
idx +=3;
}
}
else
{
// Color every point
for (vtkIdType cp = 0; cp < nr_points; ++cp)
{
int idx = cp * 3;
float h = cloud_->points[cp].h;
float v = cloud_->points[cp].v;
float s = cloud_->points[cp].s;

// Fill color data with HSV here:
if (cloud_->points[cp].s == 0)
// restrict the hue value to [0,360[
h = h < 0.0f ? h - (((int)h)/360 - 1)*360 : h - (((int)h)/360)*360;

// restrict s and v to [0,1]
if (s > 1.0f) s = 1.0f;
if (s < 0.0f) s = 0.0f;
if (v > 1.0f) v = 1.0f;
if (v < 0.0f) v = 0.0f;

if (s == 0.0f)
{
colors[idx] = colors[idx+1] = colors[idx+2] = cloud_->points[cp].v;
return;
}
float a = cloud_->points[cp].h / 60;
int i = floor (a);
float f = a - i;
float p = cloud_->points[cp].v * (1 - cloud_->points[cp].s);
float q = cloud_->points[cp].v * (1 - cloud_->points[cp].s * f);
float t = cloud_->points[cp].v * (1 - cloud_->points[cp].s * (1 - f));

switch (i)
colors[idx] = colors[idx+1] = colors[idx+2] = v*255;
}
else
{
case 0:
colors[idx] = cloud_->points[cp].v; colors[idx+1] = t; colors[idx+2] = p; break;
case 1:
colors[idx] = q; colors[idx+1] = cloud_->points[cp].v; colors[idx+2] = p; break;
case 2:
colors[idx] = p; colors[idx+1] = cloud_->points[cp].v; colors[idx+2] = t; break;
case 3:
colors[idx] = p; colors[idx+1] = q; colors[idx+2] = cloud_->points[cp].v; break;
case 4:
colors[idx] = t; colors[idx+1] = p; colors[idx+2] = cloud_->points[cp].v; break;
default:
colors[idx] = cloud_->points[cp].v; colors[idx+1] = p; colors[idx+2] = q; break;
// calculate p, q, t from HSV-values
float a = h / 60;
int i = floor (a);
float f = a - i;
float p = v * (1 - s);
float q = v * (1 - s * f);
float t = v * (1 - s * (1 - f));

switch (i)
{
case 0:
colors[idx] = v*255; colors[idx+1] = t*255; colors[idx+2] = p*255; break;
case 1:
colors[idx] = q*255; colors[idx+1] = v*255; colors[idx+2] = p*255; break;
case 2:
colors[idx] = p*255; colors[idx+1] = v*255; colors[idx+2] = t*255; break;
case 3:
colors[idx] = p*255; colors[idx+1] = q*255; colors[idx+2] = v*255; break;
case 4:
colors[idx] = t*255; colors[idx+1] = p*255; colors[idx+2] = v*255; break;
case 5:
colors[idx] = v*255; colors[idx+1] = p*255; colors[idx+2] = q*255; break;
}
}
idx +=3;
}
}
return (true);
Expand Down