Skip to content

Commit

Permalink
Return to old alpha shape computation instead of the alpha complex pr…
Browse files Browse the repository at this point in the history
…ototype.
  • Loading branch information
nikolasschwarz committed Oct 8, 2024
1 parent 41a884c commit bc9e796
Showing 1 changed file with 3 additions and 82 deletions.
85 changes: 3 additions & 82 deletions EvoEngine_Plugins/EcoSysLab/src/AlphaShapeMeshGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,76 +88,6 @@ void AlphaShapeMeshGenerator::Generate(StrandModel& strand_model, std::vector<Ve
}
}

#ifdef USE_CGAL
inline glm::vec3 cgal_to_glm(const CGAL::Point_3<CGAL::Epick>& p) {
return glm::vec3(p.x(), p.y(), p.z());
}

// Function to compute the signed distance from point to triangle's plane
float PointPlaneDistance(const glm::vec3& point, const glm::vec3& A, const glm::vec3& B, const glm::vec3& C) {
// Compute the normal of the triangle
glm::vec3 AB = B - A;
glm::vec3 AC = C - A;
glm::vec3 normal = glm::normalize(glm::cross(AB, AC));

// Compute signed distance from point to triangle's plane
float distance = glm::dot(normal, point - A);

return distance;
}

void AlphaComplexFromDelaunay(const Delaunay_CGAL& dt, std::vector<Vertex>& vertices,
std::vector<unsigned int>& indices, double alpha) {
for (auto cell_it = dt.all_cells_begin(); cell_it != dt.all_cells_end(); cell_it++) {
// TODO: move this to GPU
auto& cell = *cell_it;

auto circumcenter = cgal_to_glm(cell.circumcenter());

if (glm::distance2(cgal_to_glm(cell.vertex(0)->point()), circumcenter) > alpha) {
continue;
}

std::vector<glm::vec3> cell_vertices;

// extract vertices of the tetrahedron
for (size_t i = 0; i < 4; i++) {
auto& vertex = *cell.vertex(i);
cell_vertices.push_back(cgal_to_glm(vertex.point()));
}

// check orientation of triangle
float d = PointPlaneDistance(cell_vertices[3], cell_vertices[0], cell_vertices[1], cell_vertices[2]);
std::vector<unsigned int> cell_indices;
// if d < 0, the point is behind the triangle 012 and we can fill the index buffer with 012, 103, 023 and 213
if (d < 0) {
cell_indices = std::vector<unsigned int>{0, 1, 2, 1, 0, 3, 0, 2, 3, 2, 1, 3};
}
// else if d > 0, the point is in front, so we need to flip everything and fill the index buffer with 021, 130, 032 and 231
else if (d > 0) {
cell_indices = std::vector<unsigned int>{0, 2, 1, 1, 3, 0, 0, 3, 2, 2, 3, 1};
} else {
// degenerate cell, discard
continue;
}

// shift indices to end of index buffer and insert
for (auto& index : cell_indices) {
index += vertices.size();
}
indices.insert(indices.end(), cell_indices.begin(), cell_indices.end());

// finally insert vertices
for (glm::vec3& point : cell_vertices)
{
Vertex v;
v.position = point;
vertices.emplace_back(v);
}
}
}
#endif

void AlphaShapeMeshGenerator::ComputeAlphaShape(std::vector<glm::vec3> points, std::vector<Vertex>& vertices,
std::vector<unsigned int>& indices, double alpha) {
#ifdef USE_CGAL
Expand All @@ -170,18 +100,10 @@ void AlphaShapeMeshGenerator::ComputeAlphaShape(std::vector<glm::vec3> points, s

EVOENGINE_LOG("Delaunay computed.");
// compute alpha shape
/*
Alpha_shape_3 as(dt);
std::cout << "Alpha shape computed in REGULARIZED mode by default." << std::endl;
// find optimal alpha values
// Alpha_shape_3::NT alpha_solid = as.find_alpha_solid();
// Alpha_iterator opt = as.find_optimal_alpha(1);
// std::cout << "Smallest alpha value to get a solid through data points is " << alpha_solid << std::endl;
// std::cout << "Optimal alpha value to get one connected component is " << *opt << std::endl;
as.set_alpha(alpha);
EVOENGINE_LOG("Alpha shape computed.");

as.set_alpha(alpha);
std::vector<Alpha_shape_3::Facet> facets;
as.get_alpha_shape_facets(std::back_inserter(facets), Alpha_shape_3::REGULAR);

Expand Down Expand Up @@ -214,8 +136,7 @@ void AlphaShapeMeshGenerator::ComputeAlphaShape(std::vector<glm::vec3> points, s
indices.emplace_back(3 * i);
indices.emplace_back(3 * i + 1);
indices.emplace_back(3 * i + 2);
}*/
AlphaComplexFromDelaunay(dt, vertices, indices, alpha);
}

#else
EVOENGINE_ERROR("CGAL is required for alpha-shape computation!");
Expand Down

0 comments on commit bc9e796

Please sign in to comment.