44std::vector<c_vector<double, DIM>> EvenlySpaceAlongPath(
45 std::vector<c_vector<double, DIM>>& path,
46 const bool closedPath,
47 const bool permuteOrder,
48 std::size_t numPointsToPlace,
49 const double targetSpacing
52 assert(!path.empty());
53 assert(numPointsToPlace > 0 || targetSpacing < DBL_MAX);
61 std::rotate(path.begin(), path.begin() + random_pivot, path.end());
64 path.emplace_back(path.front());
68 std::vector<c_vector<double, DIM>> edge_unit_vectors;
69 edge_unit_vectors.reserve(path.size() - 1);
71 std::vector<double> edge_length_partial_sums;
72 edge_length_partial_sums.reserve(path.size() - 1);
74 for (
unsigned i = 1; i < path.size(); ++i)
76 const c_vector<double, DIM> un_normalised_vec = path[i] - path[i - 1];
77 const double edge_length = norm_2(un_normalised_vec);
79 edge_unit_vectors.emplace_back(un_normalised_vec / edge_length);
81 if (edge_length_partial_sums.empty())
83 edge_length_partial_sums.emplace_back(edge_length);
87 edge_length_partial_sums.emplace_back(edge_length_partial_sums.back() + edge_length);
93 if (targetSpacing < DBL_MAX)
95 numPointsToPlace =
static_cast<unsigned>(!closedPath + std::ceil(edge_length_partial_sums.back() / targetSpacing));
97 const double spacing = edge_length_partial_sums.back() / (numPointsToPlace -
static_cast<unsigned>(!closedPath));
100 std::vector<c_vector<double, DIM>> new_locations;
101 new_locations.reserve(numPointsToPlace);
103 double remainder = 0.0;
104 for (
unsigned edge = 0; edge < edge_length_partial_sums.size(); ++edge)
106 unsigned new_locs_this_edge = 0u;
107 while(spacing * new_locations.size() < edge_length_partial_sums[edge])
109 new_locations.emplace_back(path[edge] + (new_locs_this_edge * spacing - remainder) * edge_unit_vectors[edge]);
110 new_locs_this_edge++;
112 remainder = edge_length_partial_sums[edge] - spacing * new_locations.size();
117 new_locations.emplace_back(path.back());
120 return new_locations;
124template std::vector<c_vector<double, 1>> EvenlySpaceAlongPath(std::vector<c_vector<double, 1>>&,
bool,
bool, std::size_t,
double);
125template std::vector<c_vector<double, 2>> EvenlySpaceAlongPath(std::vector<c_vector<double, 2>>&,
bool,
bool, std::size_t,
double);
126template std::vector<c_vector<double, 3>> EvenlySpaceAlongPath(std::vector<c_vector<double, 3>>&,
bool,
bool, std::size_t,
double);