Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
MeshUtilityFunctions.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
37
38#include "RandomNumberGenerator.hpp"
39
40#include <algorithm>
41#include <cassert>
42
43template <std::size_t DIM>
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
50)
51{
52 assert(!path.empty());
53 assert(numPointsToPlace > 0 || targetSpacing < DBL_MAX);
54
55 if (closedPath)
56 {
57 if (permuteOrder)
58 {
59 // Rotate the closed path around a random pivot so we start at a random location along it
60 const unsigned random_pivot = RandomNumberGenerator::Instance()->randMod(path.size());
61 std::rotate(path.begin(), path.begin() + random_pivot, path.end());
62 }
63
64 path.emplace_back(path.front());
65 }
66
67 // Calculate a vector of edge vectors, and a vector of edge lengths
68 std::vector<c_vector<double, DIM>> edge_unit_vectors;
69 edge_unit_vectors.reserve(path.size() - 1);
70
71 std::vector<double> edge_length_partial_sums;
72 edge_length_partial_sums.reserve(path.size() - 1);
73
74 for (unsigned i = 1; i < path.size(); ++i)
75 {
76 const c_vector<double, DIM> un_normalised_vec = path[i] - path[i - 1];
77 const double edge_length = norm_2(un_normalised_vec);
78
79 edge_unit_vectors.emplace_back(un_normalised_vec / edge_length);
80
81 if (edge_length_partial_sums.empty())
82 {
83 edge_length_partial_sums.emplace_back(edge_length);
84 }
85 else
86 {
87 edge_length_partial_sums.emplace_back(edge_length_partial_sums.back() + edge_length);
88 }
89 }
90
91 // Calculate the spacing, allowing the targetSpacing to override the number of points to place.
92 // If the path is open, we want to hit the start and end, so subtract one from the numPointsToPlace.
93 if (targetSpacing < DBL_MAX)
94 {
95 numPointsToPlace = static_cast<unsigned>(!closedPath + std::ceil(edge_length_partial_sums.back() / targetSpacing));
96 }
97 const double spacing = edge_length_partial_sums.back() / (numPointsToPlace - static_cast<unsigned>(!closedPath));
98
99 // The vector of new locations to add to
100 std::vector<c_vector<double, DIM>> new_locations;
101 new_locations.reserve(numPointsToPlace);
102
103 double remainder = 0.0;
104 for (unsigned edge = 0; edge < edge_length_partial_sums.size(); ++edge)
105 {
106 unsigned new_locs_this_edge = 0u;
107 while(spacing * new_locations.size() < edge_length_partial_sums[edge])
108 {
109 new_locations.emplace_back(path[edge] + (new_locs_this_edge * spacing - remainder) * edge_unit_vectors[edge]);
110 new_locs_this_edge++;
111 }
112 remainder = edge_length_partial_sums[edge] - spacing * new_locations.size();
113 }
114
115 if (!closedPath)
116 {
117 new_locations.emplace_back(path.back());
118 }
119
120 return new_locations;
121}
122
123// Explicit instantiation
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);
static RandomNumberGenerator * Instance()
unsigned randMod(unsigned base)