diff --git a/Libs/Analyze/Analyze.cpp b/Libs/Analyze/Analyze.cpp index 5db40a7695..20b2e408a6 100644 --- a/Libs/Analyze/Analyze.cpp +++ b/Libs/Analyze/Analyze.cpp @@ -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(); + } } //--------------------------------------------------------------------------- @@ -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(); diff --git a/Libs/Particles/ParticleShapeStatistics.cpp b/Libs/Particles/ParticleShapeStatistics.cpp index 93bc278df9..c93aa37d9c 100644 --- a/Libs/Particles/ParticleShapeStatistics.cpp +++ b/Libs/Particles/ParticleShapeStatistics.cpp @@ -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 diff --git a/Testing/ParticlesTests/ParticlesTests.cpp b/Testing/ParticlesTests/ParticlesTests.cpp index 9cfa799193..27c77a01ab 100644 --- a/Testing/ParticlesTests/ParticlesTests.cpp +++ b/Testing/ParticlesTests/ParticlesTests.cpp @@ -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 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);