Commit d416e161 authored by Teseo Schneider's avatar Teseo Schneider
Browse files

added multimaterial export

parent 9235cd99
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ void write_mesh_aux(const std::string& path,
                    const std::vector<int>&         t_ids,
                    const std::vector<Scalar>&      color,
                    const bool                      binary,
                    const bool                      separate_components,
                    const std::function<bool(int)>& skip_tet,
                    const std::function<bool(int)>& skip_vertex)
{
@@ -161,6 +162,10 @@ void write_mesh_aux(const std::string& path,
        }
        PyMesh::VectorF V_flat(cnt_v * 3);
        PyMesh::VectorI T_flat(cnt_t * 4);
        PyMesh::VectorI C_flat;

        if(separate_components)
            C_flat.resize(cnt_t);

        size_t index = 0;
        for (size_t i = 0; i < mesh.tet_vertices.size(); ++i) {
@@ -179,10 +184,14 @@ void write_mesh_aux(const std::string& path,
            T_flat[index * 4 + 1] = old_2_new[mesh.tets[i][1]];
            T_flat[index * 4 + 2] = old_2_new[mesh.tets[i][3]];
            T_flat[index * 4 + 3] = old_2_new[mesh.tets[i][2]];

            if (separate_components)
                C_flat[index] = mesh.tets[i].scalar;

            index++;
        }

        mesh_saver.save_mesh(V_flat, T_flat, 3, mesh_saver.TET);
        mesh_saver.save_mesh(V_flat, T_flat, C_flat, 3, mesh_saver.TET);

        if (color.size() == mesh.tets.size()) {
            PyMesh::VectorF color_flat(cnt_t);
@@ -333,7 +342,8 @@ void MeshIO::write_mesh(const std::string& path,
                        const Mesh&             mesh,
                        const std::vector<int>& t_ids,
                        const bool              only_interior,
                        const bool              binary)
                        const bool              binary,
                        const bool              separate_components)
{
    logger().info("Writing mesh to {}...", path);
    igl::Timer timer;
@@ -342,13 +352,13 @@ void MeshIO::write_mesh(const std::string& path,
    if (only_interior) {
        const auto skip_tet    = [&mesh](const int i) { return mesh.tets[i].is_outside; };
        const auto skip_vertex = [&mesh](const int i) { return mesh.tet_vertices[i].is_outside; };
        write_mesh_aux(path, mesh, t_ids, std::vector<Scalar>(), binary, skip_tet, skip_vertex);
        write_mesh_aux(path, mesh, t_ids, std::vector<Scalar>(), binary, separate_components, skip_tet, skip_vertex);
    }
    else {
        timer.start();
        const auto skip_tet    = [&mesh](const int i) { return mesh.tets[i].is_removed; };
        const auto skip_vertex = [&mesh](const int i) { return mesh.tet_vertices[i].is_removed; };
        write_mesh_aux(path, mesh, t_ids, std::vector<Scalar>(), binary, skip_tet, skip_vertex);
        write_mesh_aux(path, mesh, t_ids, std::vector<Scalar>(), binary, separate_components, skip_tet, skip_vertex);
    }

    timer.stop();
@@ -359,7 +369,8 @@ void MeshIO::write_mesh(const std::string& path,
                        const Mesh&                mesh,
                        const bool                 only_interior,
                        const std::vector<Scalar>& color,
                        const bool                 binary)
                        const bool                 binary,
                        const bool                 separate_components)
{
    logger().info("Writing mesh to {}...", path);
    igl::Timer timer;
@@ -371,12 +382,12 @@ void MeshIO::write_mesh(const std::string& path,
    if (only_interior) {
        const auto skip_tet    = [&mesh](const int i) { return mesh.tets[i].is_outside; };
        const auto skip_vertex = [&mesh](const int i) { return mesh.tet_vertices[i].is_outside; };
        write_mesh_aux(path, mesh, t_ids, color, binary, skip_tet, skip_vertex);
        write_mesh_aux(path, mesh, t_ids, color, binary, separate_components, skip_tet, skip_vertex);
    }
    else {
        const auto skip_tet    = [&mesh](const int i) { return mesh.tets[i].is_removed; };
        const auto skip_vertex = [&mesh](const int i) { return mesh.tet_vertices[i].is_removed; };
        write_mesh_aux(path, mesh, t_ids, color, binary, skip_tet, skip_vertex);
        write_mesh_aux(path, mesh, t_ids, color, binary, separate_components, skip_tet, skip_vertex);
    }

    timer.stop();
+2 −2
Original line number Diff line number Diff line
@@ -20,9 +20,9 @@ namespace floatTetWild
		static void load_mesh(std::vector<Vector3>&  points, std::vector<Vector3i>& faces, GEO::Mesh& input, std::vector<int>& flags);

		static void write_mesh(const std::string &path, const Mesh &mesh,
		        const bool do_filter = true, const std::vector<Scalar> &color = std::vector<Scalar>(), const bool binary = true);
		        const bool do_filter = true, const std::vector<Scalar> &color = std::vector<Scalar>(), const bool binary = true, const bool separate_components = false);
		static void write_mesh(const std::string &path, const Mesh &mesh, const std::vector<int> &t_ids,
		        const bool do_filter = true, const bool binary = true);
		        const bool do_filter = true, const bool binary = true, const bool separate_components = false);
		static void write_surface_mesh(const std::string &path, const Mesh &mesh, const bool only_interior=true);

		static void extract_volume_mesh(const Mesh &mesh, MatrixXs &V, Eigen::MatrixXi &T, bool only_interior = true);
+8 −1
Original line number Diff line number Diff line
@@ -1413,6 +1413,13 @@ void floatTetWild::boolean_operation(Mesh& mesh, const json &csg_tree_with_ids){

        bool keep = CSGTreeParser::keep_tet(csg_tree_with_ids, cnt, w);
        t.is_removed = !keep;
        int tid = 0;
        for (int id = 0; id <= max_id; ++id) {
            bool inside = w[id][cnt] > 0.5;
            if(inside)
                tid = std::max(id+1, tid);
        }
        t.scalar = tid;
        cnt++;
        }

+12 −4
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ MshSaver::~MshSaver() {
    fout.close();
}

void MshSaver::save_mesh(const VectorF& nodes, const VectorI& elements,
void MshSaver::save_mesh(const VectorF& nodes, const VectorI& elements, const VectorI& components,
                         size_t dim, MshSaver::ElementType type) {
    if (dim != 2 && dim != 3) {
        std::stringstream err_msg;
@@ -38,7 +38,7 @@ void MshSaver::save_mesh(const VectorF& nodes, const VectorI& elements,

    save_header();
    save_nodes(nodes);
    save_elements(elements, type);
    save_elements(elements, components,type);
}

void MshSaver::save_header() {
@@ -91,7 +91,7 @@ void MshSaver::save_nodes(const VectorF& nodes) {
}

void MshSaver::save_elements(
        const VectorI& elements, MshSaver::ElementType type) {
        const VectorI& elements, const VectorI& components, MshSaver::ElementType type) {
    size_t nodes_per_element = 0;
    switch (type) {
        case TRI:
@@ -122,7 +122,7 @@ void MshSaver::save_elements(
    if (m_num_elements > 0) {
        int elem_type = type;
        int num_elems = m_num_elements;
        int tags = 0;
        int tags = components.size() > 0 ? 2 : 0;
        if (!m_binary) {
            for (size_t i=0; i<elements.size(); i+=nodes_per_element) {
                int elem_num = i/nodes_per_element + 1;
@@ -130,6 +130,9 @@ void MshSaver::save_elements(
                               VectorI::Ones(nodes_per_element);

                fout << elem_num << " " << elem_type << " " << tags << " ";
                if(components.size() > 0)
                    fout << components[elem_num-1] << " " << components[elem_num-1] << " ";

                for (size_t j=0; j<nodes_per_element; j++) {
                    fout << elem[j] << " ";
                }
@@ -143,7 +146,12 @@ void MshSaver::save_elements(
                int elem_num = i/nodes_per_element + 1;
                VectorI elem = elements.segment(i, nodes_per_element) +
                               VectorI::Ones(nodes_per_element);

                fout.write((char*)&elem_num, sizeof(int));
                if(components.size() > 0){
                    std::array<int, 2> comps = {{components[elem_num-1], components[elem_num-1]}};
                    fout.write((char*)comps.data(), sizeof(int) * 2);
                }
                fout.write((char*)elem.data(), sizeof(int)*nodes_per_element);
            }
        }
+2 −3
Original line number Diff line number Diff line
@@ -29,11 +29,10 @@ class MshSaver {
        };

    public:
        void save_mesh(const VectorF& nodes, const VectorI& elements,
                size_t dim, ElementType type);
        void save_mesh(const VectorF& nodes, const VectorI& elements, const VectorI& components, size_t dim, ElementType type);
        void save_header();
        void save_nodes(const VectorF& nodes);
        void save_elements(const VectorI& elements, ElementType type);
        void save_elements(const VectorI& elements, const VectorI& components, ElementType type);
        void save_scalar_field(const std::string& fieldname, const VectorF& field);
        void save_vector_field(const std::string& fieldname, const VectorF& field);
        void save_elem_scalar_field(const std::string& fieldname, const VectorF& field);
Loading