Exemplo n.º 1
0
int main (int argc, char *argv[])
{

  if (argc < 3) {
    cout << "Enter the two files for registration ..\n";
    return -1;
  }

  pcl::console::setVerbosityLevel (pcl::console::L_DEBUG);

  string sourcefile = argv[1];
  string targetfile = argv[2];

  CloudPtr cloud1 ( new Cloud );
  CloudPtr cloud2 ( new Cloud );

  readPCDBinaryFile (sourcefile.c_str (), cloud1);
  readPCDBinaryFile (targetfile.c_str (), cloud2);


  //pcl::IterativeClosestPointNonLinear <Point, Point> icp;
  pcl::IterativeClosestPoint <Point, Point> icp;
  icp.setInputSource (cloud1);
  icp.setInputTarget (cloud2);
  icp.setMaximumIterations (2500);
  icp.setTransformationEpsilon (0.0000001);


  Eigen::AngleAxisf init_rotation (0.6931, Eigen::Vector3f::UnitZ ());
  Eigen::Translation3f init_translation (1.79387, 0.720047, 0);
  //Eigen::Matrix4f init_guess = Eigen::Matrix4f::Identity ();
  Eigen::Matrix4f init_guess = (init_translation * init_rotation).matrix ();

  CloudPtr output (new Cloud);
  icp.align (*output, init_guess);

  //pcl::transformPointCloud (*cloud1, *output, icp.getFinalTransformation ());

  std::cout << "ICP NL has converged:" << icp.hasConverged ()
            << " score: " << icp.getFitnessScore () << std::endl;

  cout << "--------- Final transformation ---------------\n";
  cout << icp.getFinalTransformation () << "\n\n";


  CloudPtr cloud1_ig (new Cloud);
  pcl::transformPointCloud (*cloud1, *cloud1_ig, init_guess);

  PCLVisualizer* p = new PCLVisualizer (argc, argv, "Registration");
  int vp1 = 1;
  p->createViewPort (0.0, 0.0, 0.5, 1.0, vp1);
  int vp2 = 2;
  p->createViewPort (0.5, 0.0, 1.0, 1.0, vp2);
  p->setBackgroundColor (113.0/255, 113.0/255, 154.0/255);


  int color[3] = { 255, 0, 0};
  displayPointCloud (p, cloud1_ig, color, (char *) "opcloud1", vp1);

  color[0] = 0; color[1] = 255; color[2] = 0;
  displayPointCloud (p, cloud2, color, (char *) "opcloud2", vp1);

  color[0] = 0; color[1] = 255; color[2] = 0;
  displayPointCloud (p, output, color, (char *) "opcloud11", vp2);

  color[0] = 255; color[1] = 0; color[2] = 0;
  displayPointCloud (p, cloud2, color, (char *) "opcloud12", vp2);

  p->spin ();

  return 0;
}
Exemplo n.º 2
0
 void PointCloudViewer::onInit(PCLVisualizer& visualizer) {
     // position viewport 3m behind kinect, but look around the point 2m in front of it
     visualizer.setCameraPosition(0., 0., -3., 0., 0., 2., 0., -1., 0.);
     visualizer.setCameraClipDistances(1.0, 10.0);
     visualizer.setBackgroundColor(0.3, 0.3, 0.8);
 }
Exemplo n.º 3
0
void
visualize (const ModelLibrary::HashTable& hash_table)
{
  PCLVisualizer vis;
  vis.setBackgroundColor (0.1, 0.1, 0.1);

  const ModelLibrary::HashTableCell* cells = hash_table.getVoxels ();
  size_t max_num_entries = 0;
  int i, id3[3], num_cells = hash_table.getNumberOfVoxels ();
  float half_side, b[6], cell_center[3], spacing = hash_table.getVoxelSpacing ()[0];
  char cube_id[128];

  // Just get the maximal number of entries in the cells
  for ( i = 0 ; i < num_cells ; ++i, ++cells )
  {
    if (cells->size ()) // That's the number of models in the cell (it's maximum one, since we loaded only one model)
    {
      size_t num_entries = (*cells->begin ()).second.size(); // That's the number of entries in the current cell for the model we loaded
      // Get the max number of entries
      if ( num_entries > max_num_entries )
        max_num_entries = num_entries;
    }
  }

  // Now, that we have the max. number of entries, we can compute the
  // right scale factor for the spheres
  float s = (0.5f*spacing)/static_cast<float> (max_num_entries);

  cout << "s = " << s << ", max_num_entries = " << max_num_entries << endl;

  // Now, render a sphere with the right radius at the right place
  for ( i = 0, cells = hash_table.getVoxels () ; i < num_cells ; ++i, ++cells )
  {
    // Does the cell have any entries?
    if (cells->size ())
    {
      hash_table.compute3dId (i, id3);
      hash_table.computeVoxelCenter (id3, cell_center);

      // That's half of the cube's side length
      half_side = s*static_cast<float> ((*cells->begin ()).second.size ());

      // Adjust the bounds of the cube
      b[0] = cell_center[0] - half_side; b[1] = cell_center[0] + half_side;
      b[2] = cell_center[1] - half_side; b[3] = cell_center[1] + half_side;
      b[4] = cell_center[2] - half_side; b[5] = cell_center[2] + half_side;

      // Set the id
      sprintf (cube_id, "cube %i", i);

      // Add to the visualizer
      vis.addCube (b[0], b[1], b[2], b[3], b[4], b[5], 1.0, 1.0, 0.0, cube_id);
    }
  }

  vis.addCoordinateSystem(1.5, "global");
  vis.resetCamera ();

  // Enter the main loop
  while (!vis.wasStopped ())
  {
    vis.spinOnce (100);
    boost::this_thread::sleep (boost::posix_time::microseconds (100000));
  }
}
void
visualize (const ModelLibrary::HashTable* hash_table)
{
  const ModelLibrary::HashTableCell* cells = hash_table->getVoxels ();
  size_t max_num_entries = 0;
  int id3[3], num_cells = hash_table->getNumberOfVoxels ();
  double cell_center[3];

  vtkPoints* sphere_centers = vtkPoints::New (VTK_DOUBLE);
  vtkDoubleArray* scale = vtkDoubleArray::New ();
  scale->SetNumberOfComponents(1);

  // Get the positions of the spheres (one sphere per full cell)
  for (int i = 0 ; i < num_cells ; ++i, ++cells)
  {
    // Does the cell have any entries?
    if (cells->size ())
    {
      // Insert the center of the current voxel in the point set
      hash_table->compute3dId (i, id3);
      hash_table->computeVoxelCenter (id3, cell_center);
      sphere_centers->InsertNextPoint (cell_center);

      // Save the number of entries
      scale->InsertNextValue (static_cast<double> (cells->size ()));

      // Get the max
      if (cells->size () > max_num_entries)
        max_num_entries = cells->size ();
    }
  }

  PCLVisualizer vis;
  vis.setBackgroundColor (0.1, 0.1, 0.1);

  // Is there something to visualize?
  if (max_num_entries)
  {
    // Compute the factor which maps all the scale values in (0, 1]
    double factor = 1.0/static_cast<double> (max_num_entries);
    // Set the true scale
    for (vtkIdType i = 0 ; i < scale->GetNumberOfTuples () ; ++i)
      scale->SetValue(i, factor*scale->GetValue (i));

    // Input for the glyph object: the centers + scale
    vtkPolyData *positions = vtkPolyData::New ();
    positions->SetPoints (sphere_centers);
    positions->GetPointData ()->SetScalars (scale);
    // The spheres
    vtkSphereSource* sphere_src = vtkSphereSource::New ();
    sphere_src->SetPhiResolution(8);
    sphere_src->SetThetaResolution(8);
    sphere_src->SetRadius(0.5*hash_table->getVoxelSpacing ()[0]);

    // Now that we have the points and the corresponding scalars, build the glyph object
    vtkGlyph3D *glyph = vtkGlyph3D::New ();
    glyph->SetScaleModeToScaleByScalar ();
    glyph->SetColorModeToColorByScalar ();
    glyph->SetInput (positions);
    glyph->SetSource (sphere_src->GetOutput ());
    glyph->Update ();

    vtkSmartPointer<vtkPolyData> glyph_output (glyph->GetOutput ());
    vis.addModelFromPolyData(glyph_output);

    // Cleanup
    glyph->Delete ();
    positions->Delete ();
    sphere_src->Delete ();
  }

  vis.spin();

  // Cleanup
  sphere_centers->Delete();
  scale->Delete();
}