1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2025, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* As an exception, when this program is distributed through (i) the *
* App Store by Apple Inc.; (ii) the Mac App Store by Apple Inc.; or *
* (iii) Google Play by Google Inc., then that store may impose any *
* digital rights management, device limits and/or redistribution *
* restrictions that are required by its terms of service. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
**************************************************************************/
#include "link/spatiallink.h"
#include "utilities/exception.h"
#include <fstream>
namespace regina {
SpatialLink::SpatialLink(std::initializer_list<std::initializer_list<Node>>
components) {
for (auto c : components) {
auto& next = components_.emplace_back();
next.reserve(c.size());
for (auto n : c) {
next.push_back(n);
}
}
}
SpatialLink& SpatialLink::operator = (const SpatialLink& src) {
if (std::addressof(src) == this)
return *this;
// We use a basic PacketChangeSpan here, not a richer ChangeAndClearSpan,
// since we do not want to touch computed properties (if/when these are
// ever implemented). Our intention here is to clone them, not clear them.
PacketChangeSpan span(*this);
components_ = src.components_;
radius_ = src.radius_;
// Clone properties:
defaultRadius_ = src.defaultRadius_;
return *this;
}
SpatialLink& SpatialLink::operator = (SpatialLink&& src) {
// We use a basic PacketChangeSpan here, not a richer ChangeAndClearSpan,
// since we do not want to touch computed properties (if/when these are
// ever implemented). Our intention here is to move them, not clear them.
PacketChangeSpan span(*this);
components_ = std::move(src.components_);
radius_ = src.radius_;
// Move properties:
defaultRadius_ = src.defaultRadius_;
return *this;
}
std::pair<SpatialLink::Node, SpatialLink::Node> SpatialLink::range() const {
std::pair<SpatialLink::Node, SpatialLink::Node> ans;
bool found = false;
for (const auto& c : components_)
for (const auto& n : c) {
if (! found) {
ans.first = ans.second = n;
found = true;
} else {
if (n.x < ans.first.x)
ans.first.x = n.x;
if (n.y < ans.first.y)
ans.first.y = n.y;
if (n.z < ans.first.z)
ans.first.z = n.z;
if (n.x > ans.second.x)
ans.second.x = n.x;
if (n.y > ans.second.y)
ans.second.y = n.y;
if (n.z > ans.second.z)
ans.second.z = n.z;
}
}
if (found)
return ans;
else
return {{ 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 }};
}
void SpatialLink::computeDefaultRadius() {
if (isEmpty())
defaultRadius_ = 1.0; /* The actual value is irrelevant. */
auto r = range();
auto min = std::min(std::min(r.second.x - r.first.x,
r.second.y - r.first.y), r.second.z - r.first.z);
defaultRadius_ = min / 20;
}
void SpatialLink::writeTextShort(std::ostream& out) const {
if (components_.empty()) {
out << "Empty spatial link";
return;
}
if (components_.size() == 1)
out << components_.front().size() << "-crossing spatial knot";
else
out << size() << "-node, "
<< components_.size() << "-component spatial link";
}
void SpatialLink::writeTextLong(std::ostream& out) const {
if (components_.empty()) {
out << "Empty spatial link" << std::endl;
return;
}
if (components_.size() == 1)
out << components_.front().size() << "-crossing spatial knot";
else
out << components_.size() << "-component spatial link";
if (hasRadius())
out << "\nRendering radius: " << radius_;
out << "\n\n";
int comp = 0;
for (const auto& c : components_) {
out << "Component " << comp++ << " (";
if (c.size() == 1)
out << "1 node):\n";
else
out << c.size() << " nodes):\n";
for (const auto& n : c)
out << " (" << n.x << ", " << n.y << ", " << n.z << ")\n";
}
}
void SpatialLink::swap(SpatialLink& other) {
if (&other == this)
return;
// We use a basic PacketChangeSpan here, not a richer ChangeAndClearSpan,
// since we do not want to touch computed properties (if/when these are
// ever implemented). Our intention here is to swap them, not clear them.
PacketChangeSpan span1(*this);
PacketChangeSpan span2(other);
components_.swap(other.components_);
std::swap(radius_, other.radius_);
// Swap properties:
std::swap(defaultRadius_, other.defaultRadius_);
}
void SpatialLink::scale(double factor) {
ChangeAndClearSpan<> span(*this);
for (auto& c : components_)
for (auto& n : c) {
n.x *= factor;
n.y *= factor;
n.z *= factor;
}
if (hasRadius())
radius_ *= factor;
}
void SpatialLink::translate(const Vector3D<double>& vector) {
ChangeAndClearSpan<> span(*this);
for (auto& c : components_)
for (auto& n : c) {
n.x += vector.x;
n.y += vector.y;
n.z += vector.z;
}
}
void SpatialLink::reflect(int axis) {
ChangeAndClearSpan<> span(*this);
switch (axis) {
case 0:
for (auto& c : components_)
for (auto& n : c)
n.x = -n.x;
break;
case 1:
for (auto& c : components_)
for (auto& n : c)
n.y = -n.y;
break;
case 2:
for (auto& c : components_)
for (auto& n : c)
n.z = -n.z;
break;
default:
throw InvalidInput("reflect(): the given axis must be 0, 1 or 2");
}
}
void SpatialLink::refine() {
ChangeAndClearSpan<> span(*this);
// See the comments in the implementation of refine(int) for where these
// coefficients 9/16 and -1/16 come from (they just fix u = 1/2).
static constexpr double inner = 9.0 / 16.0;
static constexpr double outer = -1.0 / 16.0;
for (auto& c : components_) {
Component refined;
refined.reserve(c.size() * 2);
for (size_t i = 0; i < c.size(); ++i) {
const Node& n1 = c[i == 0 ? c.size() - 1 : i - 1];
const Node& n2 = c[i];
const Node& n3 = c[i < c.size() - 1 ? i + 1 : 0];
const Node& n4 = c[i < c.size() - 2 ? i + 2 : i + 2 - c.size()];
refined.push_back(n2);
refined.push_back((n2 + n3) * inner + (n1 + n4) * outer);
}
c.swap(refined);
}
}
void SpatialLink::refine(int sub) {
ChangeAndClearSpan<> span(*this);
for (auto& c : components_) {
Component refined;
refined.reserve(c.size() * sub);
for (size_t i = 0; i < c.size(); ++i) {
const Node& n1 = c[i == 0 ? c.size() - 1 : i - 1];
const Node& n2 = c[i];
const Node& n3 = c[i < c.size() - 1 ? i + 1 : 0];
const Node& n4 = c[i < c.size() - 2 ? i + 2 : i + 2 - c.size()];
// In general, the Catmull-Rom spline with tension τ=0.5 follows
// the following path where 0 ≤ u ≤ 1:
//
// - n1 * u * (1-u)^2 / 2 +
// n2 * (1-u) * (-2 u^2 - (1-u)^2 + 3) / 2 +
// n3 * u * (-2 (1-u)^2 - u^2 + 3) / 2 +
// - n4 * u^2 * (1-u) / 2
//
// There is a simple write-up of this by Christopher Twigg at:
// http://www.cs.cmu.edu/~fp/courses/graphics/asst5/catmullRom.pdf
//
// For u = 1/2, these coefficients become -1/16, 9/16, 9/16, -1/16,
// hence the values of the inner and outer constants above.
refined.push_back(n2);
for (int j = 1; j < sub; ++j) {
double u = double(j) / double(sub);
double coeff[4] = {
-u * (1-u) * (1-u) / 2.0,
(1-u) * (3 - 2 * u * u - (1-u) * (1-u)) / 2.0,
u * (3 - 2 * (1-u) * (1-u) - u * u) / 2.0,
-u * u * (1-u) / 2.0
};
refined.push_back(n1 * coeff[0] + n2 * coeff[1] +
n3 * coeff[2] + n4 * coeff[3]);
}
}
c.swap(refined);
}
}
} // namespace regina
|