0

我尝试为 .obj 网格生成 sdf,还使用 ​​libigl 在其上运行了行进立方体算法,但输出非常粗糙且出乎意料,我似乎遇到了问题……请帮忙!这是我的代码

#include <igl/marching_cubes.h>
#include <igl/signed_distance.h>
#include <igl/read_triangle_mesh.h>
#include <igl/voxel_grid.h>
#include <igl/opengl/glfw/Viewer.h>
#include <Eigen/Core>
#include <iostream>
#include <igl/parula.h>

#include "tutorial_shared_path.h"

int main(int argc, char * argv[])
{
  using namespace Eigen;
  using namespace std;
  using namespace igl;
  MatrixXi F;
  MatrixXd V;
  Eigen::MatrixXd C;
  // Read in inputs as double precision floating point meshes
  read_triangle_mesh("armadillo.obj",V,F);
  // igl::readOBJ("armadillo.obj", V, F);
  cout<<"Creating grid..."<<endl;
  // number of vertices on the largest side
  const int s = 64;
  // create grid
  MatrixXd GV;
  Eigen::RowVector3i res;
  igl::voxel_grid(V,0,s,1,GV,res);
 
  // compute values
  cout<<"Computing distances..."<<endl;
  VectorXd S,B;
  {
    VectorXi I;
    MatrixXd C,N;
    signed_distance(GV,V,F,SIGNED_DISTANCE_TYPE_PSEUDONORMAL,S,I,C,N);
    // signed_distance(GV,V,F,SIGNED_DISTANCE_TYPE_FAST_WINDING_NUMBER,S,I,C,N);
    // Convert distances to binary inside-outside data --> aliasing artifacts
    B = S;
    for_each(B.data(),B.data()+B.size(),[](double& b){b=(b>0?1:(b<0?-1:0));});
  }
  cout<<"Marching cubes..."<<endl;
  MatrixXd SV,BV,AV;
  MatrixXi SF,BF,AF;
  igl::marching_cubes(S,GV,res(0),res(1),res(2),0,SV,SF);
  igl::marching_cubes(B,GV,res(0),res(1),res(2),0,BV,BF);
  // igl::marching_cubes(B,GV,res(1),res(2),res(3),0,BV,BF);

  cout<<R"(Usage:
  '1'  Show original mesh.
  '2'  Show marching cubes contour of signed distance.
  '3'  Show marching cubes contour of indicator function.
  )";
  igl::opengl::glfw::Viewer viewer;
  viewer.data().set_mesh(SV,SF);

  ofstream write2file("output.txt");
    if(write2file.is_open())
    {
      write2file << S;
      write2file.close();
      // std::this_thread::sleep_for(std::chrono::milliseconds(2000));
      cout<<"Output is SAVE at "<<"output.txt"<<endl;
    }

  viewer.callback_key_down =
    [&](igl::opengl::glfw::Viewer & viewer, unsigned char key, int mod)->bool
    {
      switch(key)
      {
        default:
          return false;
        case '1':
          viewer.data().clear();
          viewer.data().set_mesh(V,F);
          break;
        case '2':
          viewer.data().clear();
          viewer.data().set_mesh(SV,SF);
          break;
        case '3':
          viewer.data().clear();
          viewer.data().set_mesh(BV,BF);
          break;
       
      }
      viewer.data().set_face_based(true);
      return true;
    };

     // Use the (normalized) vertex positions as colors
  // C = 
  //   (SV.rowwise()            - SV.colwise().minCoeff()).array().rowwise()/
  //   (SV.colwise().maxCoeff() - SV.colwise().minCoeff()).array();

  // Add per-vertex colors
  // viewer.data().set_colors(C);


  viewer.launch();
}

另外:这是我使用的网格文件的链接。这是来自 shapenet https://drive.google.com/file/d/17PwifnzyzIlTI1vKN8dm_FYb9fvdUyoE/view?usp=sharing的橱柜三角形网格

这些是我得到的结果

行进立方体后生成的网格

原始网格

4

0 回答 0