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

Set default alpha value to 255 #1385

Merged
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
18 changes: 11 additions & 7 deletions common/include/pcl/impl/point_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ namespace pcl

inline RGB ()
{
r = g = b = a = 0;
r = g = b = 0;
a = 255;
}

friend std::ostream& operator << (std::ostream& os, const RGB& p);
Expand Down Expand Up @@ -614,7 +615,8 @@ namespace pcl
{
x = y = z = 0.0f;
data[3] = 1.0f;
r = g = b = a = 0;
r = g = b = 0;
a = 255;
}
inline PointXYZRGB (uint8_t _r, uint8_t _g, uint8_t _b)
{
Expand All @@ -623,7 +625,7 @@ namespace pcl
r = _r;
g = _g;
b = _b;
a = 0;
a = 255;
}

friend std::ostream& operator << (std::ostream& os, const PointXYZRGB& p);
Expand All @@ -646,7 +648,7 @@ namespace pcl
x = y = z = 0.0f;
data[3] = 1.0f;
r = g = b = 0;
a = 0;
a = 255;
label = 255;
}
inline PointXYZRGBL (uint8_t _r, uint8_t _g, uint8_t _b, uint32_t _label)
Expand All @@ -656,7 +658,7 @@ namespace pcl
r = _r;
g = _g;
b = _b;
a = 0;
a = 255;
label = _label;
}

Expand Down Expand Up @@ -934,7 +936,8 @@ namespace pcl
{
x = y = z = 0.0f;
data[3] = 1.0f;
r = g = b = a = 0;
r = g = b = 0;
a = 255;
normal_x = normal_y = normal_z = data_n[3] = 0.0f;
curvature = 0;
}
Expand Down Expand Up @@ -1587,7 +1590,8 @@ namespace pcl
x = y = z = 0.0f;
data[3] = 1.0f;
normal_x = normal_y = normal_z = data_n[3] = 0.0f;
rgba = 0;
r = g = b = 0;
a = 255;
radius = confidence = curvature = 0.0f;
}

Expand Down
9 changes: 7 additions & 2 deletions common/include/pcl/point_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,10 @@ namespace pcl
{
if (field.name == "rgb")
{
return (field.datatype == pcl::PCLPointField::FLOAT32 &&
// For fixing the alpha value bug #1141, the rgb field can also match
// uint32.
return ((field.datatype == pcl::PCLPointField::FLOAT32 ||
field.datatype == pcl::PCLPointField::UINT32) &&
field.count == 1);
}
else
Expand All @@ -712,8 +715,10 @@ namespace pcl
}
else
{
// For fixing the alpha value bug #1141, rgb can also match uint32
return (field.name == traits::name<PointT, fields::rgb>::value &&
field.datatype == traits::datatype<PointT, fields::rgb>::value &&
(field.datatype == traits::datatype<PointT, fields::rgb>::value ||
field.datatype == pcl::PCLPointField::UINT32) &&
field.count == traits::datatype<PointT, fields::rgb>::size);
}
}
Expand Down
21 changes: 14 additions & 7 deletions common/src/point_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ namespace pcl
std::ostream&
operator << (std::ostream& os, const PointXYZRGBL& p)
{
os << "(" << p.x << "," << p.y << "," << p.z << " - " << p.r << "," << p.g << "," << p.b << " - " << p.label << ")";
os << "(" << p.x << "," << p.y << "," << p.z << " - "
<< static_cast<int>(p.r) << ","
<< static_cast<int>(p.g) << ","
<< static_cast<int>(p.b) << " - "
<< p.label << ")";
return (os);
}

Expand Down Expand Up @@ -178,7 +182,11 @@ namespace pcl
std::ostream&
operator << (std::ostream& os, const PointXYZRGBNormal& p)
{
os << "(" << p.x << "," << p.y << "," << p.z << " - " << p.rgb << " - " << p.normal[0] << "," << p.normal[1] << "," << p.normal[2] << " - " << p.r << ", " << p.g << ", " << p.b << " - " << p.curvature << ")";
os << "(" << p.x << "," << p.y << "," << p.z << " - "<< p.rgb << " - " << p.normal[0] << "," << p.normal[1] << "," << p.normal[2] << " - "
<< static_cast<int>(p.r) << ", "
<< static_cast<int>(p.g) << ", "
<< static_cast<int>(p.b) << " - "
<< p.curvature << ")";
return (os);
}

Expand Down Expand Up @@ -408,14 +416,13 @@ namespace pcl
std::ostream&
operator << (std::ostream& os, const PointSurfel& p)
{
const unsigned char* rgba_ptr = reinterpret_cast<const unsigned char*>(&p.rgba);
os <<
"(" << p.x << "," << p.y << "," << p.z << " - " <<
p.normal_x << "," << p.normal_y << "," << p.normal_z << " - "
<< static_cast<int>(*rgba_ptr) << ","
<< static_cast<int>(*(rgba_ptr+1)) << ","
<< static_cast<int>(*(rgba_ptr+2)) << ","
<< static_cast<int>(*(rgba_ptr+3)) << " - " <<
<< static_cast<int>(p.r) << ","
<< static_cast<int>(p.g) << ","
<< static_cast<int>(p.b) << ","
<< static_cast<int>(p.a) << " - " <<
p.radius << " - " << p.confidence << " - " << p.curvature << ")";
return (os);
}
Expand Down
60 changes: 49 additions & 11 deletions io/include/pcl/io/impl/pcd_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ pcl::PCDWriter::generateHeader (const pcl::PointCloud<PointT> &cloud, const int
// Add the regular dimension
field_names << " " << fields[i].name;
field_sizes << " " << pcl::getFieldSize (fields[i].datatype);
field_types << " " << pcl::getFieldType (fields[i].datatype);
if ("rgb" == fields[i].name)
field_types << " " << "U";
else
field_types << " " << pcl::getFieldType (fields[i].datatype);
int count = abs (static_cast<int> (fields[i].count));
if (count == 0) count = 1; // check for 0 counts (coming from older converter code)
field_counts << " " << count;
Expand Down Expand Up @@ -576,12 +579,30 @@ pcl::PCDWriter::writeASCII (const std::string &file_name, const pcl::PointCloud<
}
case pcl::PCLPointField::FLOAT32:
{
float value;
memcpy (&value, reinterpret_cast<const char*> (&cloud.points[i]) + fields[d].offset + c * sizeof (float), sizeof (float));
if (pcl_isnan (value))
stream << "nan";
/*
* Despite the float type, store the rgb field as uint32
* because several fully opaque color values are mapped to
* nan.
*/
if ("rgb" == fields[d].name)
{
uint32_t value;
memcpy (&value, reinterpret_cast<const char*> (&cloud.points[i]) + fields[d].offset + c * sizeof (float), sizeof (float));
if (pcl_isnan (value))
stream << "nan";
else
stream << boost::numeric_cast<uint32_t>(value);
break;
}
else
stream << boost::numeric_cast<float>(value);
{
float value;
memcpy (&value, reinterpret_cast<const char*> (&cloud.points[i]) + fields[d].offset + c * sizeof (float), sizeof (float));
if (pcl_isnan (value))
stream << "nan";
else
stream << boost::numeric_cast<float>(value);
}
break;
}
case pcl::PCLPointField::FLOAT64:
Expand Down Expand Up @@ -877,12 +898,29 @@ pcl::PCDWriter::writeASCII (const std::string &file_name,
}
case pcl::PCLPointField::FLOAT32:
{
float value;
memcpy (&value, reinterpret_cast<const char*> (&cloud.points[indices[i]]) + fields[d].offset + c * sizeof (float), sizeof (float));
if (pcl_isnan (value))
stream << "nan";
/*
* Despite the float type, store the rgb field as uint32
* because several fully opaque color values are mapped to
* nan.
*/
if ("rgb" == fields[d].name)
{
uint32_t value;
memcpy (&value, reinterpret_cast<const char*> (&cloud.points[indices[i]]) + fields[d].offset + c * sizeof (float), sizeof (float));
if (pcl_isnan (value))
stream << "nan";
else
stream << boost::numeric_cast<uint32_t>(value);
}
else
stream << boost::numeric_cast<float>(value);
{
float value;
memcpy (&value, reinterpret_cast<const char*> (&cloud.points[indices[i]]) + fields[d].offset + c * sizeof (float), sizeof (float));
if (pcl_isnan (value))
stream << "nan";
else
stream << boost::numeric_cast<float>(value);
}
break;
}
case pcl::PCLPointField::FLOAT64:
Expand Down
24 changes: 21 additions & 3 deletions io/src/pcd_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,11 +1119,21 @@ pcl::PCDWriter::generateHeaderASCII (const pcl::PCLPointCloud2 &cloud,
{
// Ignore invalid padded dimensions that are inherited from binary data
if (cloud.fields[d].name != "_")
stream << pcl::getFieldType (cloud.fields[d].datatype) << " ";
{
if (cloud.fields[d].name == "rgb")
stream << "U ";
else
stream << pcl::getFieldType (cloud.fields[d].datatype) << " ";
}
}
// Ignore invalid padded dimensions that are inherited from binary data
if (cloud.fields[cloud.fields.size () - 1].name != "_")
stream << pcl::getFieldType (cloud.fields[cloud.fields.size () - 1].datatype);
{
if (cloud.fields[cloud.fields.size () - 1].name == "rgb")
stream << "U";
else
stream << pcl::getFieldType (cloud.fields[cloud.fields.size () - 1].datatype);
}

// Remove trailing spaces
result = stream.str ();
Expand Down Expand Up @@ -1384,7 +1394,15 @@ pcl::PCDWriter::writeASCII (const std::string &file_name, const pcl::PCLPointClo
}
case pcl::PCLPointField::FLOAT32:
{
copyValueString<pcl::traits::asType<pcl::PCLPointField::FLOAT32>::type>(cloud, i, point_size, d, c, stream);
/*
* Despite the float type, store the rgb field as uint32
* because several fully opaque color values are mapped to
* nan.
*/
if ("rgb" == cloud.fields[d].name)
copyValueString<pcl::traits::asType<pcl::PCLPointField::UINT32>::type>(cloud, i, point_size, d, c, stream);
else
copyValueString<pcl::traits::asType<pcl::PCLPointField::FLOAT32>::type>(cloud, i, point_size, d, c, stream);
break;
}
case pcl::PCLPointField::FLOAT64:
Expand Down
2 changes: 1 addition & 1 deletion test/common/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ TEST (PCL, CopyIfFieldExists)
pcl::for_each_type<FieldList> (CopyIfFieldExists<PointXYZRGBNormal, float> (p, "rgb", is_rgb, rgb_val));
EXPECT_EQ (is_rgb, true);
int rgb = *reinterpret_cast<int*>(&rgb_val);
EXPECT_EQ (rgb, 8339710); // alpha is 0
EXPECT_EQ (rgb, 0xff7f40fe); // alpha is 255
pcl::for_each_type<FieldList> (CopyIfFieldExists<PointXYZRGBNormal, float> (p, "normal_x", is_normal_x, normal_x_val));
EXPECT_EQ (is_normal_x, true);
EXPECT_EQ (normal_x_val, 1.0);
Expand Down
21 changes: 12 additions & 9 deletions test/io/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,18 +724,21 @@ TEST (PCL, PCDReaderWriter)
TEST (PCL, PCDReaderWriterASCIIColorPrecision)
{
PointCloud<PointXYZRGB> cloud;
cloud.points.reserve (256 / 4 * 256 / 4 * 256 / 4 * 256 / 16);
for (size_t r_i = 0; r_i < 256; r_i += 5)
for (size_t g_i = 0; g_i < 256; g_i += 5)
for (size_t b_i = 0; b_i < 256; b_i += 5)
{
PointXYZRGB p;
p.r = static_cast<unsigned char> (r_i);
p.g = static_cast<unsigned char> (g_i);
p.b = static_cast<unsigned char> (b_i);
p.x = p.y = p.z = 0.f;

cloud.push_back (p);
}
for (size_t a_i = 0; a_i < 256; a_i += 10)
{
PointXYZRGB p;
p.r = static_cast<unsigned char> (r_i);
p.g = static_cast<unsigned char> (g_i);
p.b = static_cast<unsigned char> (b_i);
p.a = static_cast<unsigned char> (a_i);
p.x = p.y = p.z = 0.f;

cloud.push_back (p);
}
cloud.height = 1;
cloud.width = uint32_t (cloud.size ());
cloud.is_dense = true;
Expand Down