1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include "simulation_io.hpp"
#include <pcl/io/png_io.h>
pcl::simulation::SimExample::SimExample(int argc, char** argv, int height, int width)
: height_(height), width_(width)
{
initializeGL(argc, argv);
// 1. construct member elements:
camera_ = Camera::Ptr(new Camera());
scene_ = Scene::Ptr(new Scene());
// rl_ = RangeLikelihoodGLSL::Ptr(new RangeLikelihoodGLSL(1, 1, height, width, scene_,
// 0));
rl_ = RangeLikelihood::Ptr(new RangeLikelihood(1, 1, height, width, scene_));
// rl_ = RangeLikelihood::Ptr(new RangeLikelihood(10, 10, 96, 96, scene_));
// rl_ = RangeLikelihood::Ptr(new RangeLikelihood(1, 1, height_, width_, scene_));
// Actually corresponds to default parameters:
rl_->setCameraIntrinsicsParameters(
width_, height_, 576.09757860, 576.09757860, 321.06398107, 242.97676897);
rl_->setComputeOnCPU(false);
rl_->setSumOnCPU(true);
rl_->setUseColor(true);
// 2. read mesh and setup model:
std::cout << "About to read: " << argv[2] << std::endl;
pcl::PolygonMesh mesh; // (new pcl::PolygonMesh);
pcl::io::loadPolygonFile(argv[2], mesh);
pcl::PolygonMesh::Ptr cloud(new pcl::PolygonMesh(mesh));
// Not sure if PolygonMesh assumes triangles if to, TODO: Ask a developer
PolygonMeshModel::Ptr model =
PolygonMeshModel::Ptr(new PolygonMeshModel(GL_POLYGON, cloud));
scene_->add(model);
std::cout << "Just read " << argv[2] << std::endl;
std::cout << mesh.polygons.size() << " polygons and " << mesh.cloud.data.size()
<< " triangles\n";
// works well for MIT CSAIL model 3rd floor:
// camera_->set(4.04454, 44.9377, 1.1, 0.0, 0.0, -2.00352);
// works well for MIT CSAIL model 2nd floor:
// camera_->set (27.4503, 37.383, 4.30908, 0.0, 0.0654498, -2.25802);
// works for small files:
// camera_->set(-5.0, 0.0, 1.0, 0.0, 0.0, 0.0);
camera_->set(0.471703, 1.59862, 3.10937, 0, 0.418879, -12.2129);
camera_->setPitch(0.418879); // not sure why this is here:
for (int i = 0; i < 2048; i++) {
float v = i / 2048.0;
v = powf(v, 3) * 6;
t_gamma[i] = v * 6 * 256;
}
}
void
pcl::simulation::SimExample::initializeGL(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); // was GLUT_RGBA
glutInitWindowPosition(10, 10);
glutInitWindowSize(10, 10);
// glutInitWindowSize (window_width_, window_height_);
glutCreateWindow("OpenGL range likelihood");
GLenum err = glewInit();
if (GLEW_OK != err) {
std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
exit(-1);
}
std::cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
if (glewIsSupported("GL_VERSION_2_0"))
std::cout << "OpenGL 2.0 supported" << std::endl;
else {
std::cerr << "Error: OpenGL 2.0 not supported" << std::endl;
exit(1);
}
std::cout << "GL_MAX_VIEWPORTS: " << GL_MAX_VIEWPORTS << std::endl;
const GLubyte* version = glGetString(GL_VERSION);
std::cout << "OpenGL Version: " << version << std::endl;
}
void
pcl::simulation::SimExample::doSim(Eigen::Isometry3d pose_in)
{
// No reference image - but this is kept for compatibility with range_test_v2:
float* reference = new float[rl_->getRowHeight() * rl_->getColWidth()];
const float* depth_buffer = rl_->getDepthBuffer();
// Copy one image from our last as a reference.
for (int i = 0, n = 0; i < rl_->getRowHeight(); ++i) {
for (int j = 0; j < rl_->getColWidth(); ++j) {
reference[n++] = depth_buffer[i * rl_->getWidth() + j];
}
}
std::vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> poses;
std::vector<float> scores;
poses.push_back(pose_in);
rl_->computeLikelihoods(reference, poses, scores);
std::cout << "camera: " << camera_->getX() << " " << camera_->getY() << " "
<< camera_->getZ() << " " << camera_->getRoll() << " "
<< camera_->getPitch() << " " << camera_->getYaw() << std::endl;
delete[] reference;
}
void
pcl::simulation::SimExample::write_score_image(const float* score_buffer,
std::string fname)
{
int npixels = rl_->getWidth() * rl_->getHeight();
auto* score_img = new std::uint8_t[npixels * 3];
float min_score = score_buffer[0];
float max_score = score_buffer[0];
for (int i = 1; i < npixels; i++) {
if (score_buffer[i] < min_score)
min_score = score_buffer[i];
if (score_buffer[i] > max_score)
max_score = score_buffer[i];
}
for (int y = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x) {
int i = y * width_ + x;
int i_in = (height_ - 1 - y) * width_ + x; // flip up
float d = (score_buffer[i_in] - min_score) / (max_score - min_score);
score_img[3 * i + 0] = 0;
score_img[3 * i + 1] = d * 255;
score_img[3 * i + 2] = 0;
}
}
// Write to file:
pcl::io::saveRgbPNGFile(fname, score_img, width_, height_);
delete[] score_img;
}
void
pcl::simulation::SimExample::write_depth_image(const float* depth_buffer,
std::string fname)
{
int npixels = rl_->getWidth() * rl_->getHeight();
auto* depth_img = new std::uint8_t[npixels * 3];
float min_depth = depth_buffer[0];
float max_depth = depth_buffer[0];
for (int i = 1; i < npixels; i++) {
if (depth_buffer[i] < min_depth)
min_depth = depth_buffer[i];
if (depth_buffer[i] > max_depth)
max_depth = depth_buffer[i];
}
for (int y = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x) {
int i = y * width_ + x;
int i_in = (height_ - 1 - y) * width_ + x; // flip up down
float zn = 0.7;
float zf = 20.0;
float d = depth_buffer[i_in];
float z = -zf * zn / ((zf - zn) * (d - zf / (zf - zn)));
float b = 0.075;
float f = 580.0;
auto kd = static_cast<std::uint16_t>(1090 - b * f / z * 8);
if (kd > 2047)
kd = 2047;
int pval = t_gamma[kd];
int lb = pval & 0xff;
switch (pval >> 8) {
case 0:
depth_img[3 * i + 0] = 255;
depth_img[3 * i + 1] = 255 - lb;
depth_img[3 * i + 2] = 255 - lb;
break;
case 1:
depth_img[3 * i + 0] = 255;
depth_img[3 * i + 1] = lb;
depth_img[3 * i + 2] = 0;
break;
case 2:
depth_img[3 * i + 0] = 255 - lb;
depth_img[3 * i + 1] = 255;
depth_img[3 * i + 2] = 0;
break;
case 3:
depth_img[3 * i + 0] = 0;
depth_img[3 * i + 1] = 255;
depth_img[3 * i + 2] = lb;
break;
case 4:
depth_img[3 * i + 0] = 0;
depth_img[3 * i + 1] = 255 - lb;
depth_img[3 * i + 2] = 255;
break;
case 5:
depth_img[3 * i + 0] = 0;
depth_img[3 * i + 1] = 0;
depth_img[3 * i + 2] = 255 - lb;
break;
default:
depth_img[3 * i + 0] = 0;
depth_img[3 * i + 1] = 0;
depth_img[3 * i + 2] = 0;
break;
}
}
}
// Write to file:
pcl::io::saveRgbPNGFile(fname, depth_img, width_, height_);
delete[] depth_img;
}
void
pcl::simulation::SimExample::write_depth_image_uint(const float* depth_buffer,
std::string fname)
{
int npixels = rl_->getWidth() * rl_->getHeight();
auto* depth_img = new unsigned short[npixels];
float min_depth = depth_buffer[0];
float max_depth = depth_buffer[0];
for (int i = 1; i < npixels; i++) {
if (depth_buffer[i] < min_depth)
min_depth = depth_buffer[i];
if (depth_buffer[i] > max_depth)
max_depth = depth_buffer[i];
}
for (int y = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x) {
int i = y * width_ + x;
int i_in = (height_ - 1 - y) * width_ + x; // flip up down
float zn = 0.7;
float zf = 20.0;
float d = depth_buffer[i_in];
unsigned short z_new = static_cast<unsigned short>(std::min(
std::floor(1000 * (-zf * zn / ((zf - zn) * (d - zf / (zf - zn))))), 65535.f));
if (z_new < 18000) {
std::cout << z_new << " " << d << " " << x << "\n";
}
depth_img[i] = z_new;
}
}
// Write to file:
pcl::io::saveShortPNGFile(fname, depth_img, width_, height_, 1);
delete[] depth_img;
}
void
pcl::simulation::SimExample::write_rgb_image(const std::uint8_t* rgb_buffer,
std::string fname)
{
int npixels = rl_->getWidth() * rl_->getHeight();
auto* rgb_img = new std::uint8_t[npixels * 3];
for (int y = 0; y < height_; ++y) {
for (int x = 0; x < width_; ++x) {
int px = y * width_ + x;
int px_in = (height_ - 1 - y) * width_ + x; // flip up down
rgb_img[3 * (px) + 0] = rgb_buffer[3 * px_in + 0];
rgb_img[3 * (px) + 1] = rgb_buffer[3 * px_in + 1];
rgb_img[3 * (px) + 2] = rgb_buffer[3 * px_in + 2];
}
}
// Write to file:
pcl::io::saveRgbPNGFile(fname, rgb_img, width_, height_);
delete[] rgb_img;
}
|