Skip to content

for new experimental branch #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/ipc/collisions/normal/normal_collisions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,61 @@ double NormalCollisions::compute_minimum_distance(

// ============================================================================

// NOTE: Actually distance squared
double NormalCollisions::compute_avg_distance(
const CollisionMesh& mesh,
Eigen::ConstRef<Eigen::MatrixXd> vertices,
const double dhat) const
{
assert(vertices.rows() == mesh.num_vertices());
if (empty()) {
return std::numeric_limits<double>::infinity();
}

const Eigen::MatrixXi& edges = mesh.edges();
const Eigen::MatrixXi& faces = mesh.faces();
const double dhat_sq = dhat * dhat;

// Thread-local storage: pair<sum_of_distances, count_of_distances>
tbb::enumerable_thread_specific<std::pair<double, size_t>> storage{ {0.0, 0} };

tbb::parallel_for(
tbb::blocked_range<size_t>(0, size()),
[&](const tbb::blocked_range<size_t>& range) {
auto& local = storage.local();
double& sum = local.first;
size_t& count = local.second;

for (size_t i = range.begin(); i != range.end(); ++i) {
const double dist = (*this)[i].compute_distance(
(*this)[i].dof(vertices, edges, faces));

// only accumulate those within threshold
if (dist <= dhat_sq) {
sum += dist;
count += 1;
}
}
}
);

// Combine sums and counts from all threads
const auto total = storage.combine(
[](const std::pair<double, size_t>& a, const std::pair<double, size_t>& b) {
return std::make_pair(a.first + b.first, a.second + b.second);
}
);

const double overall_sum = total.first;
const size_t overall_count = total.second;

if (overall_count == 0) {
return std::numeric_limits<double>::infinity();
}
return overall_sum / static_cast<double>(overall_count);
}

// ============================================================================
size_t NormalCollisions::size() const
{
return vv_collisions.size() + ev_collisions.size() + ee_collisions.size()
Expand Down
4 changes: 4 additions & 0 deletions src/ipc/collisions/normal/normal_collisions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class NormalCollisions {
const CollisionMesh& mesh,
Eigen::ConstRef<Eigen::MatrixXd> vertices) const;

/// @returns The avg distance between any non-adjacent elements less than dhat.
double compute_avg_distance(
const CollisionMesh& mesh, Eigen::ConstRef<Eigen::MatrixXd> vertices, const double dhat) const;

// ------------------------------------------------------------------------

/// @brief Get the number of collisions.
Expand Down