Skip to content

Commit

Permalink
Merge pull request #704 from wildmeshing/teseo/tri=ins
Browse files Browse the repository at this point in the history
added tri ins
  • Loading branch information
teseoch authored Jan 21, 2024
2 parents 564492e + 335ecf2 commit 80f7352
Show file tree
Hide file tree
Showing 20 changed files with 374 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmake/recipes/tests/wmtk_data.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ ExternalProject_Add(
SOURCE_DIR ${WMTK_DATA_ROOT}

GIT_REPOSITORY https://github.com/wildmeshing/data.git
GIT_TAG b9c1a6e86538556bdf1395c2d9941a2b9aa009ec
GIT_TAG 696fe0f17523a711ded54515dd036116da7fefd4

CONFIGURE_COMMAND ""
BUILD_COMMAND ""
Expand Down
2 changes: 2 additions & 0 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ add_component(${WMTK_COMPONENT_PREFIX} "wildmeshing")
add_component(${WMTK_COMPONENT_PREFIX} "get_all_meshes")
add_component(${WMTK_COMPONENT_PREFIX} "procedural")
add_component(${WMTK_COMPONENT_PREFIX} "fusion")
add_component(${WMTK_COMPONENT_PREFIX} "triangle_insertion")
add_component(${WMTK_COMPONENT_PREFIX} "to_points")

string(LENGTH ${json_components} json_components_length)
math(EXPR json_components_length "${json_components_length}-2")
Expand Down
3 changes: 3 additions & 0 deletions components/tests/integration_test_list.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
unit_test/input.json
unit_test/to_points.json
unit_test/delaunay.json
unit_test/insertion.json
unit_test/multimesh.json
unit_test/multimesh_boundary_2d.json
unit_test/multimesh_boundary_3d.json
Expand Down
1 change: 1 addition & 0 deletions components/tests/test_component_to_points.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO
1 change: 1 addition & 0 deletions components/tests/test_component_triangle_insertion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
namespace wmtk::components {

template <int D>
RowVectors<double, D> points_to_rowvectors(PointMesh& point_cloud)
RowVectors<double, D> points_to_rowvectors(const std::string& position, PointMesh& point_cloud)
{
auto pts_attr = point_cloud.get_attribute_handle<double>("vertices", PrimitiveType::Vertex);
auto pts_attr = point_cloud.get_attribute_handle<double>(position, PrimitiveType::Vertex);
auto pts_acc = point_cloud.create_accessor<double>(pts_attr);

const auto vertices = point_cloud.get_all(PrimitiveType::Vertex);
Expand Down Expand Up @@ -51,7 +51,8 @@ void delaunay_exec(const internal::DelaunayOptions& options, io::Cache& cache)

// make sure dimensions fit
{
auto pts_attr = point_cloud.get_attribute_handle<double>("vertices", PrimitiveType::Vertex);
auto pts_attr =
point_cloud.get_attribute_handle<double>(options.position, PrimitiveType::Vertex);
auto pts_acc = point_cloud.create_accessor<double>(pts_attr);
assert(pts_acc.dimension() == options.cell_dimension);
}
Expand All @@ -63,7 +64,7 @@ void delaunay_exec(const internal::DelaunayOptions& options, io::Cache& cache)
MeshT mesh;
Eigen::MatrixXd vertices;
Eigen::MatrixXi faces;
const auto pts_vec = points_to_rowvectors<D>(point_cloud);
const auto pts_vec = points_to_rowvectors<D>(options.position, point_cloud);
if constexpr (D == 2) {
std::tie(vertices, faces) = internal::delaunay_2d(pts_vec);
} else if constexpr (D == 3) {
Expand All @@ -73,7 +74,7 @@ void delaunay_exec(const internal::DelaunayOptions& options, io::Cache& cache)
}

mesh.initialize(faces.cast<int64_t>());
mesh_utils::set_matrix_attribute(vertices, "vertices", PrimitiveType::Vertex, mesh);
mesh_utils::set_matrix_attribute(vertices, options.position, PrimitiveType::Vertex, mesh);

cache.write_mesh(mesh, options.output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "object",
"required": [
"input",
"position",
"output",
"cell_dimension"
]
Expand All @@ -13,6 +14,11 @@
"type": "string",
"doc": "input mesh"
},
{
"pointer": "/position",
"type": "string",
"doc": "position"
},
{
"pointer": "/output",
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ namespace internal {
struct DelaunayOptions
{
std::string input;
std::string position;
std::string output;
int64_t cell_dimension;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DelaunayOptions, input, output, cell_dimension);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DelaunayOptions, input, output, position, cell_dimension);

} // namespace internal
} // namespace components
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

set(SRC_FILES
ToPtsOptions.hpp
to_points.hpp
to_points.cpp)


#CURRENT_COMPONENT_LIB_NAME is set from the main cmake
target_sources(${CURRENT_COMPONENT_LIB_NAME} PRIVATE ${SRC_FILES})
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <nlohmann/json.hpp>

namespace wmtk::components {


struct ToPtsOptions
{
std::string input;
std::string position;
std::string name;
bool add_box;
double box_scale;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ToPtsOptions, input, position, name, add_box, box_scale);

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "to_points.hpp"


#include "ToPtsOptions.hpp"

#include <bitset>

#include <wmtk/Mesh.hpp>
#include <wmtk/PointMesh.hpp>
#include <wmtk/utils/mesh_utils.hpp>

namespace wmtk::components {

void to_points(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache)
{
ToPtsOptions options = j.get<ToPtsOptions>();

const std::shared_ptr<Mesh> mesh_in = cache.read_mesh(options.input);
const Mesh& mesh = *mesh_in;

const auto pts_attr =
mesh.get_attribute_handle<double>(options.position, PrimitiveType::Vertex);
const auto pts_acc = mesh.create_const_accessor<double>(pts_attr);
const auto vs = mesh.get_all(PrimitiveType::Vertex);

Eigen::AlignedBox<double, Eigen::Dynamic> bbox(pts_acc.dimension());

int64_t box_size = std::pow(2, pts_acc.dimension());


Eigen::MatrixXd pts(vs.size() + (options.add_box ? box_size : 0), pts_acc.dimension());
int64_t index = 0;
for (const auto& v : vs) {
pts.row(index) << pts_acc.const_vector_attribute(v).transpose();
bbox.extend(pts.row(index).transpose());
++index;
}

if (options.add_box) {
auto center = bbox.center();
auto r = bbox.diagonal() / 2.;

bbox.min() = center - options.box_scale * r;
bbox.max() = center + options.box_scale * r;

for (int64_t d = 0; d < box_size; ++d) {
std::bitset<32> bits = d;
for (int64_t i = 0; i < pts.cols(); ++i) {
pts(index, i) = bits[i] == 0 ? bbox.min()[i] : bbox.max()[i];
}
++index;
}
}

PointMesh pts_mesh(pts.rows());

mesh_utils::set_matrix_attribute(pts, options.position, PrimitiveType::Vertex, pts_mesh);

cache.write_mesh(pts_mesh, options.name);
}

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <nlohmann/json.hpp>
#include <wmtk/io/Cache.hpp>

#include <wmtk/components/base/Paths.hpp>

namespace wmtk::components {

void to_points(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache);

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"pointer": "/",
"type": "object",
"required": [
"input",
"position",
"name"
],
"optional": [
"add_box",
"box_scale"
]
},
{
"pointer": "/input",
"type": "string"
},
{
"pointer": "/position",
"type": "string"
},
{
"pointer": "/name",
"type": "string"
},
{
"pointer": "/add_box",
"type": "bool",
"default": false
},
{
"pointer": "/box_scale",
"type": "float",
"default": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

set(SRC_FILES
TriInsOptions.hpp
triangle_insertion.hpp
triangle_insertion.cpp)


#CURRENT_COMPONENT_LIB_NAME is set from the main cmake
target_sources(${CURRENT_COMPONENT_LIB_NAME} PRIVATE ${SRC_FILES})
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <nlohmann/json.hpp>

namespace wmtk::components {
struct TriInsOptions
{
std::string input;
std::string input_position;
std::string background;
std::string background_position;
std::string name;
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
TriInsOptions,
input,
input_position,
background,
background_position,
name);

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "triangle_insertion.hpp"

#include "TriInsOptions.hpp"

#include <wmtk/operations/attribute_update/AttributeTransferStrategy.hpp>
#include <wmtk/utils/EigenMatrixWriter.hpp>
#include <wmtk/utils/VolumeRemesherTriangleInsertion.hpp>


#include <wmtk/Mesh.hpp>
#include <wmtk/TetMesh.hpp>
#include <wmtk/utils/Logger.hpp>

namespace wmtk::components {

void triangle_insertion(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache)
{
TriInsOptions options = j.get<TriInsOptions>();

std::shared_ptr<Mesh> mesh_in = cache.read_mesh(options.input);
std::shared_ptr<Mesh> bg_mesh = cache.read_mesh(options.background);

if (mesh_in->top_simplex_type() != PrimitiveType::Face)
log_and_throw_error("triangle_insertion supports only triangle meshes");
if (bg_mesh->top_simplex_type() != PrimitiveType::Tetrahedron)
log_and_throw_error("triangle_insertion supports only bg tet meshes");


wmtk::utils::EigenMatrixWriter writer;
mesh_in->serialize(writer);


wmtk::utils::EigenMatrixWriter writer_bg;
bg_mesh->serialize(writer_bg);

Eigen::MatrixXd V;
writer.get_double_matrix(options.input_position, PrimitiveType::Vertex, V);

Eigen::MatrixX<int64_t> F;
writer.get_FV_matrix(F);


Eigen::MatrixXd Vbg;
writer_bg.get_double_matrix(options.background_position, PrimitiveType::Vertex, Vbg);

Eigen::MatrixX<int64_t> Fbg;
writer_bg.get_TV_matrix(Fbg);

auto [tetmesh, facemesh] =
utils::generate_raw_tetmesh_with_surface_from_input(V, F, 0.1, Vbg, Fbg);
auto pt_attribute =
tetmesh->get_attribute_handle<double>(options.input_position, PrimitiveType::Vertex);
auto child_position_handle = facemesh->register_attribute<double>(
options.input_position,
PrimitiveType::Vertex,
tetmesh->get_attribute_dimension(pt_attribute.as<double>()));

auto propagate_to_child_position = [](const Eigen::MatrixXd& P) -> Eigen::VectorXd {
return P;
};
auto update_child_positon =
std::make_shared<wmtk::operations::SingleAttributeTransferStrategy<double, double>>(
child_position_handle,
pt_attribute,
propagate_to_child_position);
update_child_positon->run_on_all();

cache.write_mesh(*tetmesh, options.name);
}

} // namespace wmtk::components
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <nlohmann/json.hpp>
#include <wmtk/io/Cache.hpp>

#include <wmtk/components/base/Paths.hpp>

namespace wmtk {
namespace components {

void triangle_insertion(const base::Paths& paths, const nlohmann::json& j, io::Cache& cache);

} // namespace components
} // namespace wmtk
Loading

0 comments on commit 80f7352

Please sign in to comment.