// Copyright 2024 Bytedance Inc. All Rights Reserved. // Author: tianlei.richard@qq.com (tianlei.richard) #include "mesh.h" #include "OBJ_Loader.h" #include "spdlog/spdlog.h" #define OBJL_VEC2_TO_EIGEN_VECTOR(objl_v) \ Eigen::Vector2d { objl_v.X, objl_v.Y } #define OBJL_VEC3_TO_EIGEN_VECTOR(objl_v) \ Eigen::Vector3d { objl_v.X, objl_v.Y, objl_v.Z } Mesh::Mesh(const std::vector> &vertices, const std::vector &primitives) : vertices_(vertices), primitives_(primitives) {} Point2d Mesh::get_texture_coordinate(const unsigned int index) const { return (vertices_[index])->texture_coordinate; } Point3d Mesh::get_normal_vector(const unsigned int index) const { return (vertices_[index])->normal; } std::vector Mesh::load_mesh(const std::string &file_path) { objl::Loader loader{}; assert(loader.LoadFile(file_path.c_str())); std::vector res; for (const auto &mesh : loader.LoadedMeshes) { spdlog::info("Current mesh has {} vertices.", mesh.Vertices.size()); std::vector> vertices; for (int i = 0; i < mesh.Vertices.size(); i++) { vertices.push_back(std::make_shared( OBJL_VEC3_TO_EIGEN_VECTOR(mesh.Vertices[i].Position), OBJL_VEC3_TO_EIGEN_VECTOR(mesh.Vertices[i].Normal), OBJL_VEC2_TO_EIGEN_VECTOR(mesh.Vertices[i].TextureCoordinate))); } std::vector primitives; for (int i = 0; i < mesh.Indices.size(); i += 3) { primitives.push_back(Triangle(vertices, mesh.Indices[i], mesh.Indices[i + 1], mesh.Indices[i + 2])); } res.push_back(Mesh(vertices, primitives)); } return res; }