Skip to content
Merged
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
13 changes: 9 additions & 4 deletions Libs/Analyze/Analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ Analyze::Analyze(ProjectHandle project) : project_(project), mesh_manager_(new M
// load data from the project
update_shapes();

// compute stats
compute_stats();

initialize_mesh_warper();
// compute stats, and the mesh warper only when there are particles to warp
if (compute_stats()) {
initialize_mesh_warper();
}
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -570,6 +570,11 @@ bool Analyze::compute_stats() {
}
}

if (point_size == 0) {
SW_WARN("No particles");
return false;
}

stats_.import_points(points, group_ids);
stats_.compute_modes();

Expand Down
7 changes: 7 additions & 0 deletions Libs/Particles/ParticleShapeStatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,13 @@ int ParticleShapeStatistics::compute_modes() {
TIME_SCOPE("ParticleShapeStatistics::compute_modes");
SW_DEBUG("computing PCA modes");

if (points_minus_mean_.size() == 0) {
eigenvalues_.clear();
eigenvectors_.resize(0, 0);
percent_variance_by_mode_.clear();
return 1;
}

// Use SVD to compute PCA. For X = U * S * V^T:
// - The covariance matrix X^T * X / (n-1) has eigenvalues S^2 / (n-1) and eigenvectors V
// - The old code computed eigenvectors_ = X * V = U * S
Expand Down
10 changes: 10 additions & 0 deletions Testing/ParticlesTests/ParticlesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ TEST(ParticlesTests, pca)
ASSERT_LE((pcaVec.block<3,2>(0,0) - ground_truth).norm(), 1E-4);
}

TEST(ParticlesTests, pca_no_particles)
{
// shapes without particles (e.g. an unoptimized project) must not crash
std::vector<Eigen::VectorXd> points(5, Eigen::VectorXd());
ParticleShapeStatistics stats;
stats.import_points(points, {});
ASSERT_EQ(stats.compute_modes(), 1);
ASSERT_TRUE(stats.get_eigen_values().empty());
}

TEST(ParticlesTests, compactness)
{
ParticleSystemEvaluation ParticleSystemEvaluation(filenames);
Expand Down
Loading