Browse Source

feat(export obj): add obj export in process.cpp

feat-obj-export
mckay 2 weeks ago
parent
commit
8a11215720
  1. 89
      network_process/src/process.cpp

89
network_process/src/process.cpp

@ -4,6 +4,89 @@
#include <connect_by_topo.hpp>
#include <post_topo.hpp>
#include <fstream>
void export_halfpatch_obj(
const stl_vector_mp<Eigen::Vector3d>& iso_pts,
const stl_vector_mp<polygon_face_t>& iso_faces,
const stl_vector_mp<stl_vector_mp<uint32_t>>& patches,
const std::string& filename)
{
std::string mtl_filename = filename + ".mtl";
std::ofstream mtl(mtl_filename);
for (size_t half_patch = 0; half_patch < patches.size() * 2; ++half_patch) {
size_t patch_idx = half_patch / 2;
bool is_forward = (half_patch % 2 == 0);
mtl << "newmtl patch_" << patch_idx << (is_forward ? "_in" : "_out") << "\n";
// 随机或规律分配颜色
float r = float(patch_idx % 7) / 7.0f;
float g = is_forward ? 0.8f : 0.2f;
float b = 1.0f - r;
mtl << "Kd " << r << " " << g << " " << b << "\n";
}
mtl.close();
std::string obj_filename = filename + ".obj";
std::ofstream ofs(obj_filename);
if (!ofs) return;
ofs << "mtllib "<< mtl_filename << "\n";
// 输出所有顶点
for (const auto& v : iso_pts) {
ofs << "v " << v.x() << " " << v.y() << " " << v.z() << "\n";
}
// 遍历所有 half-patch
for (size_t half_patch = 0; half_patch < patches.size() * 2; ++half_patch) {
size_t patch_idx = half_patch / 2;
bool is_forward = (half_patch % 2 == 0);
ofs << "g halfpatch_" << half_patch << "\n";
ofs << "usemtl patch_" << patch_idx << (is_forward ? "_in" : "_out") << "\n";
for (auto face_idx : patches[patch_idx]) {
const auto& face = iso_faces[face_idx];
ofs << "f";
if (is_forward) {
for (auto vi : face.vertex_indices)
ofs << " " << (vi + 1);
} else {
for (auto it = face.vertex_indices.rbegin(); it != face.vertex_indices.rend(); ++it)
ofs << " " << (*it + 1);
}
ofs << "\n";
}
}
ofs.close();
}
// Export OBJ without patch information
void export_obj_no_patch(
const stl_vector_mp<Eigen::Vector3d>& iso_pts,
const stl_vector_mp<polygon_face_t>& iso_faces,
const std::string& filename)
{
// Write OBJ file
std::string obj_filename = filename + ".obj";
std::ofstream ofs(obj_filename);
if (!ofs) return;
// Write all vertices
for (const auto& v : iso_pts) {
ofs << "v " << v.x() << " " << v.y() << " " << v.z() << "\n";
}
// Write all faces
for (const auto& face : iso_faces) {
ofs << "f";
for (auto vi : face.vertex_indices)
ofs << " " << (vi + 1);
ofs << "\n";
}
ofs.close();
}
ISNP_API void build_implicit_network_by_blobtree(const s_settings& settings,
const baked_blobtree_t& tree,
stl_vector_mp<Eigen::Vector3d>& output_vertices,
@ -71,6 +154,7 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings&
iso_verts,
iso_faces);
}
export_obj_no_patch(iso_pts, iso_faces, "halfpatch_bbefore_connect");
// connect components by topology
stl_vector_mp<uint32_t> patch_of_face(iso_faces.size());
stl_vector_mp<uint32_t> shell_of_half_patch{};
@ -91,6 +175,10 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings&
compute_patch_edges(iso_faces, edges_of_iso_face, iso_edges);
compute_patches(edges_of_iso_face, iso_edges, iso_faces, patches, patch_of_face);
compute_chains(iso_verts.size(), iso_edges, chain_vertices, chain_headers, chain_of_mid_face);
export_halfpatch_obj(iso_pts, iso_faces, patches, "halfpatch_before_connect");
for (auto &subface : tree.subfaces) {
std::cout << subface.object_ptr->data_center << std::endl;
}
for (const auto& [face_index, chain_indices] : chain_of_mid_face) {
auto& chain_indices_ = chain_of_patch[patch_of_face[face_index]];
chain_indices_ = std::move(chain_indices);
@ -189,5 +277,6 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings&
identify_chain_near_parallel(output_parameteric_planes);
}
}
export_halfpatch_obj(iso_pts, iso_faces, patches, "halfpatch_final");
}
}
Loading…
Cancel
Save