Exemplo n.º 1
0
    void saveMeshObject(const std::string& filename, MeshObject& mo)
    {
        std::vector<tinyobj::shape_t> out_shape(1);

        tinyobj::mesh_t& mesh = out_shape[0].mesh;

        VerticesArray& vts = mo.getVertices();
        NormalsArray& nls = mo.getNormals();
        FacesArray& fs = mo.getFaces();

        for (size_t v = 0; v < vts.size(); ++ v)
        {
            mesh.positions.push_back(vts[v].x());
            mesh.positions.push_back(vts[v].y());
            mesh.positions.push_back(vts[v].z());
        }

        for (size_t n = 0; n < nls.size(); ++ n)
        {
            mesh.normals.push_back(nls[n].x());
            mesh.normals.push_back(nls[n].y());
            mesh.normals.push_back(nls[n].z());
        }

        for (size_t f = 0; f < fs.size(); ++ f)
        {
            mesh.indices.push_back(fs[f][0]);
            mesh.indices.push_back(fs[f][1]);
            mesh.indices.push_back(fs[f][2]);
        }

        bool ret = WriteObj(filename, out_shape, false);
        assert(ret);
    }
Exemplo n.º 2
0
void Image3D::SolveUnProjectionD(const std::vector<double> &depth){
	if (ParamParser::writeMesh){
		double fMinDepth = HUGE_VAL;
		double fMaxDepth = 1.0 - HUGE_VAL;
		for (const auto & d : depth){
			fMinDepth = __min(fMinDepth, d);
			fMaxDepth = __max(fMaxDepth, d);
		}

		char fn[128];
		static int no = 0;
		sprintf_s(fn, "./mesh%d.obj", no++);
		Depth2Model d2m = Depth2Model(fMinDepth - 0.0001, fMaxDepth + 0.0001, ParamParser::smooth_thres);
		d2m.SaveModel(depth, cam, fn, false);

		Alignment align;
		align.RetainConnectRegion(d2m.point3d, std::vector<Eigen::Vector3d>(), d2m.facets);
		WriteObj(fn, d2m.point3d, std::vector<Eigen::Vector3d>(), d2m.facets);
	}

	int w = cam.W();
	int h = cam.H();
	point3d.resize(w * h);
	valid.resize(w * h, true);
	for (int i = 0; i < w; ++i){
		for (int j = 0; j < h; ++j){
			if(depth[j * w + i] < ParamParser::m_fMinDsp ||
				depth[j * w + i] > ParamParser::m_fMaxDsp){
				valid[j * w + i] = false;
			}
			else{
				cam.GetWorldCoordFromImg(i, j, (1.0 / depth[j * w + i]), point3d[j * w + i]);
			}
		}
	}
}
Exemplo n.º 3
0
void DetailSynthesis::computeDisplacementMap(std::shared_ptr<Model> model)
{
  int width = 1000, height = 1000;
  displacement_map = cv::Mat(height, width, CV_32FC1);
  std::shared_ptr<KDTreeWrapper> kdTree = mesh_para->kdTree_UV;
  AdjList adjFaces_list = mesh_para->cut_shape->getVertexShareFaces();
  for(int x = 0; x < height; x ++)
  {
    for(int y = 0; y < width; y ++)
    {
      int pt_id;
      std::vector<float> pt;
      pt.resize(2);
      pt[0] = float(x) / height;
      pt[1] = float(y) / width;
      kdTree->nearestPt(pt,pt_id);
      std::vector<int> adjFaces = adjFaces_list[pt_id];
      int face_id;
      float point[3];
      point[0] = pt[0];
      point[1] = pt[1];
      point[2] = 0;
      float lambda[3];
      int id1_before,id2_before,id3_before;
      for(size_t i = 0; i < adjFaces.size(); i ++)
      {
        float l[3];
        int v1_id,v2_id,v3_id;
        float v1[3],v2[3],v3[3];
        v1_id = (mesh_para->cut_shape->getFaceList())[3 * adjFaces[i]];
        v2_id = (mesh_para->cut_shape->getFaceList())[3 * adjFaces[i] + 1];
        v3_id = (mesh_para->cut_shape->getFaceList())[3 * adjFaces[i] + 2];
        v1[0] = (mesh_para->cut_shape->getUVCoord())[2 * v1_id];
        v1[1] = (mesh_para->cut_shape->getUVCoord())[2 * v1_id + 1];
        v1[2] = 0;
        v2[0] = (mesh_para->cut_shape->getUVCoord())[2 * v2_id];
        v2[1] = (mesh_para->cut_shape->getUVCoord())[2 * v2_id + 1];
        v2[2] = 0;
        v3[0] = (mesh_para->cut_shape->getUVCoord())[2 * v3_id];
        v3[1] = (mesh_para->cut_shape->getUVCoord())[2 * v3_id + 1];
        v3[2] = 0;
        ShapeUtility::computeBaryCentreCoord(point,v1,v2,v3,l);
        if(l[0] >= 0 && l[1] >= 0 && l[2] >= 0)
        {
          face_id = i;
          lambda[0] = l[0];
          lambda[1] = l[1];
          lambda[2] = l[2];
          id1_before = v1_id;
          id2_before = v2_id;
          id3_before = v3_id;
        }
      }
      float v1_worldCoord_before[3],v2_worldCoord_before[3],v3_worldCoord_before[3];
      v1_worldCoord_before[0] = (mesh_para->cut_shape->getVertexList())[3 * id1_before];
      v1_worldCoord_before[1] = (mesh_para->cut_shape->getVertexList())[3 * id1_before + 1];
      v1_worldCoord_before[2] = (mesh_para->cut_shape->getVertexList())[3 * id1_before + 2];
      v2_worldCoord_before[0] = (mesh_para->cut_shape->getVertexList())[3 * id2_before];
      v2_worldCoord_before[1] = (mesh_para->cut_shape->getVertexList())[3 * id2_before + 1];
      v2_worldCoord_before[2] = (mesh_para->cut_shape->getVertexList())[3 * id2_before + 2];
      v3_worldCoord_before[0] = (mesh_para->cut_shape->getVertexList())[3 * id3_before];
      v3_worldCoord_before[1] = (mesh_para->cut_shape->getVertexList())[3 * id3_before + 1];
      v3_worldCoord_before[2] = (mesh_para->cut_shape->getVertexList())[3 * id3_before + 2];
      float pt_worldCoord_before[3];
      pt_worldCoord_before[0] = lambda[0] * v1_worldCoord_before[0] + lambda[1] * v2_worldCoord_before[0] + lambda[2] * v3_worldCoord_before[0];
      pt_worldCoord_before[1] = lambda[0] * v1_worldCoord_before[1] + lambda[1] * v2_worldCoord_before[1] + lambda[2] * v3_worldCoord_before[1];
      pt_worldCoord_before[2] = lambda[0] * v1_worldCoord_before[2] + lambda[1] * v2_worldCoord_before[2] + lambda[2] * v3_worldCoord_before[2];
      int id1_after,id2_after,id3_after;
      id1_after = mesh_para->vertex_set[id1_before];
      id2_after = mesh_para->vertex_set[id2_before];
      id3_after = mesh_para->vertex_set[id3_before];
      float v1_worldCoord_after[3],v2_worldCoord_after[3],v3_worldCoord_after[3];
      v1_worldCoord_after[0] = (model->getShapeVertexList())[3 * id1_after];
      v1_worldCoord_after[1] = (model->getShapeVertexList())[3 * id1_after + 1];
      v1_worldCoord_after[2] = (model->getShapeVertexList())[3 * id1_after + 2];
      v2_worldCoord_after[0] = (model->getShapeVertexList())[3 * id2_after];
      v2_worldCoord_after[1] = (model->getShapeVertexList())[3 * id2_after + 1];
      v2_worldCoord_after[2] = (model->getShapeVertexList())[3 * id2_after + 2];
      v3_worldCoord_after[0] = (model->getShapeVertexList())[3 * id3_after];
      v3_worldCoord_after[1] = (model->getShapeVertexList())[3 * id3_after + 1];
      v3_worldCoord_after[2] = (model->getShapeVertexList())[3 * id3_after + 2];
      float pt_worldCoord_after[3];
      pt_worldCoord_after[0] = lambda[0] * v1_worldCoord_after[0] + lambda[1] * v2_worldCoord_after[0] + lambda[2] * v3_worldCoord_after[0];
      pt_worldCoord_after[1] = lambda[0] * v1_worldCoord_after[1] + lambda[1] * v2_worldCoord_after[1] + lambda[2] * v3_worldCoord_after[1];
      pt_worldCoord_after[2] = lambda[0] * v1_worldCoord_after[2] + lambda[1] * v2_worldCoord_after[2] + lambda[2] * v3_worldCoord_after[2];
      float v1_normal_original_mesh[3],v2_normal_original_mesh[3],v3_normal_original_mesh[3];
      v1_normal_original_mesh[0] = mesh_para->normal_original_mesh[3 * id1_after];
      v1_normal_original_mesh[1] = mesh_para->normal_original_mesh[3 * id1_after + 1];
      v1_normal_original_mesh[2] = mesh_para->normal_original_mesh[3 * id1_after + 2];
      v2_normal_original_mesh[0] = mesh_para->normal_original_mesh[3 * id2_after];
      v2_normal_original_mesh[1] = mesh_para->normal_original_mesh[3 * id2_after + 1];
      v2_normal_original_mesh[2] = mesh_para->normal_original_mesh[3 * id2_after + 2];
      v3_normal_original_mesh[0] = mesh_para->normal_original_mesh[3 * id3_after];
      v3_normal_original_mesh[1] = mesh_para->normal_original_mesh[3 * id3_after + 1];
      v3_normal_original_mesh[2] = mesh_para->normal_original_mesh[3 * id3_after + 2];
      float pt_normal_original_mesh[3];
      pt_normal_original_mesh[0] = lambda[0] * v1_normal_original_mesh[0] + lambda[1] * v2_normal_original_mesh[0] + lambda[2] * v3_normal_original_mesh[0];
      pt_normal_original_mesh[1] = lambda[0] * v1_normal_original_mesh[1] + lambda[1] * v2_normal_original_mesh[1] + lambda[2] * v3_normal_original_mesh[1];
      pt_normal_original_mesh[2] = lambda[0] * v1_normal_original_mesh[2] + lambda[1] * v2_normal_original_mesh[2] + lambda[2] * v3_normal_original_mesh[2];
      float pt_difference[3];
      pt_difference[0] = pt_worldCoord_after[0] - pt_worldCoord_before[0];
      pt_difference[1] = pt_worldCoord_after[1] - pt_worldCoord_before[1];
      pt_difference[2] = pt_worldCoord_after[2] - pt_worldCoord_before[2];
      displacement_map.at<float>(x,y) = pt_difference[0] * pt_normal_original_mesh[0] + pt_difference[1] * pt_normal_original_mesh[1] + pt_difference[2] * pt_normal_original_mesh[2];
    }
  }
  // show the displacement_map
  /*double min,max;
  cv::minMaxLoc(displacement_map,&min,&max);
  cv::imshow("displacement_map",(displacement_map - min) / (max - min));*/

  // check whether the displacement_map is computed correctedly
  //std::cout << "UV_coord size is :" << mesh_para->cut_shape->getUVCoord().size() << std::endl;
  VertexList vertex_check;
  /*vertex_check.resize(model->getShapeVertexList().size());*/
  vertex_check =  mesh_para->vertex_original_mesh;
  for(int i = 0; i < mesh_para->vertex_set.size(); i ++)
  {
    float pt_check[3];
    pt_check[0] = vertex_check[3 * mesh_para->vertex_set[i]];
    pt_check[1] = vertex_check[3 * mesh_para->vertex_set[i] + 1];
    pt_check[2] = vertex_check[3 * mesh_para->vertex_set[i] + 2];
    float normal_check[3];
    normal_check[0] = mesh_para->normal_original_mesh[3 * mesh_para->vertex_set[i]];
    normal_check[1] = mesh_para->normal_original_mesh[3 * mesh_para->vertex_set[i] + 1];
    normal_check[2] = mesh_para->normal_original_mesh[3 * mesh_para->vertex_set[i] + 2];
    float displacement;
    float U,V;
    U = (mesh_para->cut_shape->getUVCoord())[2 * i];
    V = (mesh_para->cut_shape->getUVCoord())[2 * i + 1];
    //std::cout << "U : " << U << " , " << "V : " << V << std::endl;
    int img_x,img_y;
    img_x = int(U * (float)height);
    img_y = int(V * (float)width);
    if(img_x == height)
    {
      img_x --;
    }
    if(img_y == width)
    {
      img_y --;    
    }
    displacement = displacement_map.at<float>(img_x,img_y);
    vertex_check[3 * mesh_para->vertex_set[i]] = pt_check[0] + normal_check[0] * displacement;
    vertex_check[3 * mesh_para->vertex_set[i] + 1] = pt_check[1] + normal_check[1] * displacement;
    vertex_check[3 * mesh_para->vertex_set[i] + 2] = pt_check[2] + normal_check[2] * displacement;
  }

  std::vector<tinyobj::shape_t> shapes;
  std::vector<tinyobj::material_t> materials;
  tinyobj::shape_t obj_shape;
  obj_shape.mesh.positions = vertex_check;
  obj_shape.mesh.indices = model->getShapeFaceList();
  //obj_shape.mesh.texcoords = model->getShapeUVCoord();
  shapes.push_back(obj_shape);

  std::string output_name = model->getOutputPath() + "/check" + ".obj";
  WriteObj(output_name, shapes, materials);
}
Exemplo n.º 4
0
void FBXIO::ReadObj(fbxInternals& a_fbxInteral, const char* fileName, const char* path)
{
	m_fbx = new FBXFile();

	//write the obj if it doesn't exist, it is then read back in.. pretty slow!
	if (!DoesFileExist(fileName))
	{
		WriteObj(a_fbxInteral, fileName, path);
	}

	std::ifstream fin(fileName, std::ios::in | std::ios::binary);

	fbxInternals fbxObj;

	vertLayout vert_ptr;

	textureLayout tex_ptr;

	if (fin.good())
	{
		unsigned int vertexSize = 0;
		unsigned int indiceSize = 0;

		unsigned int* ui_ptr = new unsigned int;

		//read vertex size
		if (!fin.eof() && fin.peek() != EOF)
		{
			fin.read((char*)ui_ptr, sizeof(unsigned int));
			vertexSize = (*ui_ptr);
		}

		//read indice size
		if (!fin.eof() && fin.peek() != EOF)
		{
			fin.read((char*)ui_ptr, sizeof(unsigned int));
			indiceSize = (*ui_ptr);
		}

		unsigned int count = 0;

		//read vertex data
		while (!fin.eof() && fin.peek() != EOF && count < vertexSize)
		{
			fin.read((char*)&vert_ptr, sizeof(vertLayout));

			FBXVertex curVert;

			curVert.position = vert_ptr.position;
			curVert.indices = vert_ptr.indices;
			curVert.normal = vert_ptr.normal;
			curVert.tangent = vert_ptr.tangent;
			curVert.binormal = vert_ptr.binormal;			
			curVert.texCoord1 = vert_ptr.texCoord1;
			curVert.texCoord2 = vert_ptr.texCoord2;
			curVert.tangent = vert_ptr.tangent;
			curVert.colour = vert_ptr.colour;

			//			mesh.m_vertices.push_back(curVert);	
			fbxObj.verts.push_back(vert_ptr);

			count++;
		}

		//read indice data
		while (!fin.eof() && fin.peek() != EOF && count < vertexSize + indiceSize)
		{
			unsigned int indice;

			fin.read((char*)&indice, sizeof(unsigned int));
			//			mesh.m_indices.push_back(indice);
			fbxObj.m_indices.push_back(indice);

			count++;
		}

		textureLayout diffuseMap;

		readString(&fin, diffuseMap.name);
		readString(&fin, diffuseMap.path);
		//read res
		if (!fin.eof() && fin.peek() != EOF)
		{
			fin.read((char*)ui_ptr, sizeof(unsigned int));
			diffuseMap.res = (*ui_ptr);
		}
		//diffuseMap.data = readImgData(&fin);

		textureLayout normalMap;

		readString(&fin, normalMap.name);
		readString(&fin, normalMap.path);
		//read res
		if (!fin.eof() && fin.peek() != EOF)
		{
			fin.read((char*)ui_ptr, sizeof(unsigned int));
			normalMap.res = (*ui_ptr);
		}
		//normalMap.data = readImgData(&fin);

		textureLayout specMap;

		readString(&fin, specMap.name);
		readString(&fin, specMap.path);
		//read res
		if (!fin.eof() && fin.peek() != EOF)
		{
			fin.read((char*)ui_ptr, sizeof(unsigned int));
			specMap.res = (*ui_ptr);
		}
		//specMap.data = readImgData(&fin);


		if (HasAnimation)
		{
			return; //read animation data in
		}

		fbxObj.m_dif = diffuseMap;
		fbxObj.m_normal = normalMap;
		fbxObj.m_spec = specMap;


		delete ui_ptr;
		fin.close();
	}

	FBXFile fbx;

	FBXMeshNode* mesh = new FBXMeshNode();



	a_fbxInteral = fbxObj; //set 

	//fbx.m_meshes.push_back();	
}
Exemplo n.º 5
0
int main(int argc, char** argv) {
    if (argc < 2) {
        doHelp(argv[0]);
        return 0;
    }
    plDebug::Init(plDebug::kDLAll);

    const char* filename = argv[1];
    plString outfile = filenameConvert(filename, ".obj");
    plString mtlfile = filenameConvert(filename, ".mtl");
    hsTArray<plString> objects;

    for (int i=2; i<argc; i++) {
        if (argv[i][0] == '-') {
            if (strcmp(argv[i], "-o") == 0) {
                if (i+1 >= argc) {
                    printf("No output filename supplied\n");
                    return 0;
                } else {
                    outfile = argv[++i];
                }
            } else if (strcmp(argv[i], "-m") == 0) {
                if (i+1 >= argc) {
                    printf("No material filename supplied\n");
                    return 0;
                } else {
                    mtlfile = argv[++i];
                }
            } else if (strcmp(argv[i], "--help") == 0) {
                doHelp(argv[0]);
                return 0;
            } else {
                printf("Unrecognized option: %s\n", argv[i]);
            }
        } else {
            objects.append(argv[i]);
        }
    }

    plResManager rm;
    plPageInfo* page;
    try {
        page = rm.ReadPage(filename);
    } catch (const hsException& e) {
        plDebug::Error("%s:%lu: %s", e.File(), e.Line(), e.what());
        return 1;
    } catch (const std::exception& e) {
        plDebug::Error("%s", e.what());
        return 1;
    } catch (...) {
        plDebug::Error("Undefined error!");
        return 1;
    }

    hsFileStream OS;

    if (!OS.open(mtlfile, fmCreate)) {
        fprintf(stderr, "Error opening %s for writing!\n", mtlfile.cstr());
        return 1;
    }
    OS.writeStr("#Generated by Prp2Obj\n");

    try {
        std::vector<plKey> MObjs = rm.getKeys(page->getLocation(), kGMaterial);
        for (size_t i = 0; i < MObjs.size(); i++) {
            hsGMaterial* mat = hsGMaterial::Convert(rm.getObject(MObjs[i]));
            WriteMat(mat, &OS);
        }
    } catch (const hsException& e) {
        plDebug::Error("%s:%lu: %s", e.File(), e.Line(), e.what());
        return 1;
    }
    OS.close();

    if (!OS.open(outfile, fmCreate)) {
        fprintf(stderr, "Error opening %s for writing!\n", outfile.cstr());
        return 1;
    }
    OS.writeStr("#Generated by Prp2Obj\n");
    OS.writeStr(plString::Format("mtllib %s\n", mtlfile.cstr()));

    size_t nObjects = 0;
    try {
        std::vector<plKey> SObjs = rm.getKeys(page->getLocation(), kSceneObject);
        for (size_t i = 0; i < SObjs.size(); i++) {
            if (objects.empty() || objects.find(SObjs[i]->getName()) != (size_t)-1) {
                plSceneObject* obj = plSceneObject::Convert(rm.getObject(SObjs[i]));
                if (obj->getDrawInterface().Exists())
                    WriteObj(obj, &OS, objects.empty());
                nObjects++;
            }
        }
    } catch (const hsException& e) {
        plDebug::Error("%s:%lu: %s", e.File(), e.Line(), e.what());
        return 1;
    }
    OS.close();

    printf("Successfully wrote %u objects\n", (unsigned int)nObjects);
    return 0;
}