From c171d745b33a2ed785cca33886f7c619b5afc0d6 Mon Sep 17 00:00:00 2001 From: Louis Montaut Date: Thu, 5 Oct 2023 19:08:53 +0200 Subject: [PATCH] [ConvexBase] Fix neighbor copy in copy constructor. Previously, the pointers in neighbors were not pointing to the data stored in nneighbors_ but to other.nneighbors_. If other.nneighbors_ is destroyed then the neighbors point to deleted data. This commit fixes that. --- src/shape/geometric_shapes.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/shape/geometric_shapes.cpp b/src/shape/geometric_shapes.cpp index 86c67ae04..4ac11a017 100644 --- a/src/shape/geometric_shapes.cpp +++ b/src/shape/geometric_shapes.cpp @@ -67,13 +67,25 @@ ConvexBase::ConvexBase(const ConvexBase& other) } else points.reset(); - if (other.neighbors.get() && other.neighbors->size() > 0) { - neighbors.reset(new std::vector(*(other.neighbors))); - } else - neighbors.reset(); - if (other.nneighbors_.get() && other.nneighbors_->size() > 0) { + // Deep copy the list of all the neighbors of all the points nneighbors_.reset(new std::vector(*(other.nneighbors_))); + if (other.neighbors.get() && other.neighbors->size() > 0) { + // Fill each neighbors for each point in the Convex object. + neighbors.reset(new std::vector(other.neighbors->size())); + assert(neighbors->size() == points->size()); + unsigned int* p_nneighbors = nneighbors_->data(); + + std::vector& neighbors_ = *neighbors; + const std::vector& other_neighbors_ = *(other.neighbors); + for (size_t i = 0; i < neighbors->size(); ++i) { + Neighbors& n = neighbors_[i]; + n.count_ = other_neighbors_[i].count_; + n.n_ = p_nneighbors; + p_nneighbors += n.count_; + } + } else + neighbors.reset(); } else nneighbors_.reset();