| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "mesh.h" |
| 7 |
|
| 8 |
#include "mutex.h" |
| 9 |
#include "neighborperceiver.h" |
| 10 |
|
| 11 |
namespace Avogadro::Core { |
| 12 |
|
| 13 |
Mesh::Mesh() : m_stable(true), m_other(0), m_cube(0), m_lock(new Mutex) |
| 14 |
{ |
| 15 |
m_vertices.reserve(100); |
| 16 |
m_normals.reserve(100); |
| 17 |
m_colors.reserve(1); |
| 18 |
} |
| 19 |
|
| 20 |
Mesh::Mesh(const Mesh& other) |
| 21 |
: m_vertices(other.m_vertices), m_normals(other.m_normals), |
| 22 |
m_colors(other.m_colors), m_triangles(other.m_triangles), |
| 23 |
m_name(other.m_name), m_stable(true), m_isoValue(other.m_isoValue), |
| 24 |
m_other(other.m_other), m_cube(other.m_cube), m_lock(new Mutex) |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
Mesh::~Mesh() |
| 29 |
{ |
| 30 |
delete m_lock; |
| 31 |
m_lock = nullptr; |
| 32 |
} |
| 33 |
|
| 34 |
bool Mesh::reserve(unsigned int size, bool useColors) |
| 35 |
{ |
| 36 |
m_vertices.reserve(size); |
| 37 |
m_normals.reserve(size); |
| 38 |
if (useColors) |
| 39 |
m_colors.reserve(size); |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
void Mesh::setStable(bool isStable) |
| 44 |
{ |
| 45 |
m_stable = isStable; |
| 46 |
} |
| 47 |
|
| 48 |
bool Mesh::stable() |
| 49 |
{ |
| 50 |
return m_stable; |
| 51 |
} |
| 52 |
|
| 53 |
const Core::Array<Vector3f>& Mesh::vertices() const |
| 54 |
{ |
| 55 |
return m_vertices; |
| 56 |
} |
| 57 |
|
| 58 |
const Vector3f* Mesh::vertex(int n) const |
| 59 |
{ |
| 60 |
return &(m_vertices[n * 3]); |
| 61 |
} |
| 62 |
|
| 63 |
bool Mesh::setTriangles(const Core::Array<Vector3f>& values) |
| 64 |
{ |
| 65 |
m_triangles.clear(); |
| 66 |
m_triangles = values; |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
const Core::Array<Vector3f>& Mesh::triangles() const |
| 71 |
{ |
| 72 |
return m_triangles; |
| 73 |
} |
| 74 |
|
| 75 |
bool Mesh::setVertices(const Core::Array<Vector3f>& values) |
| 76 |
{ |
| 77 |
m_vertices.clear(); |
| 78 |
m_vertices = values; |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
bool Mesh::addVertices(const Core::Array<Vector3f>& values) |
| 83 |
{ |
| 84 |
if (m_vertices.capacity() < m_vertices.size() + values.size()) |
| 85 |
m_vertices.reserve(m_vertices.capacity() * 2); |
| 86 |
if (values.size() % 3 == 0) { |
| 87 |
for (const auto& value : values) |
| 88 |
m_vertices.push_back(value); |
| 89 |
return true; |
| 90 |
} else { |
| 91 |
return false; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
const Core::Array<Vector3f>& Mesh::normals() const |
| 96 |
{ |
| 97 |
return m_normals; |
| 98 |
} |
| 99 |
|
| 100 |
const Vector3f* Mesh::normal(int n) const |
| 101 |
{ |
| 102 |
return &(m_normals[n * 3]); |
| 103 |
} |
| 104 |
|
| 105 |
bool Mesh::setNormals(const Core::Array<Vector3f>& values) |
| 106 |
{ |
| 107 |
m_normals.clear(); |
| 108 |
m_normals = values; |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
bool Mesh::addNormals(const Core::Array<Vector3f>& values) |
| 113 |
{ |
| 114 |
if (m_normals.capacity() < m_normals.size() + values.size()) |
| 115 |
m_normals.reserve(m_normals.capacity() * 2); |
| 116 |
if (values.size() % 3 == 0) { |
| 117 |
for (const auto& value : values) |
| 118 |
m_normals.push_back(value); |
| 119 |
return true; |
| 120 |
} else { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
const Core::Array<Color3f>& Mesh::colors() const |
| 126 |
{ |
| 127 |
return m_colors; |
| 128 |
} |
| 129 |
|
| 130 |
const Color3f* Mesh::color(int n) const |
| 131 |
{ |
| 132 |
// If there is only one color return that, otherwise colored by vertex. |
| 133 |
if (m_colors.size() == 1) |
| 134 |
return m_colors.data(); |
| 135 |
else |
| 136 |
return &(m_colors[n * 3]); |
| 137 |
} |
| 138 |
|
| 139 |
bool Mesh::setColors(const Core::Array<Color3f>& values) |
| 140 |
{ |
| 141 |
m_colors.clear(); |
| 142 |
m_colors = values; |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
bool Mesh::addColors(const Core::Array<Color3f>& values) |
| 147 |
{ |
| 148 |
if (m_colors.capacity() < m_colors.size() + values.size()) |
| 149 |
m_colors.reserve(m_colors.capacity() * 2); |
| 150 |
if (values.size() % 3 == 0) { |
| 151 |
for (auto value : values) |
| 152 |
m_colors.push_back(value); |
| 153 |
return true; |
| 154 |
} else { |
| 155 |
return false; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
bool Mesh::valid() const |
| 160 |
{ |
| 161 |
return (m_vertices.size() == m_normals.size()) && |
| 162 |
(m_colors.size() == 1 || m_colors.size() == m_vertices.size()); |
| 163 |
} |
| 164 |
|
| 165 |
bool Mesh::clear() |
| 166 |
{ |
| 167 |
m_vertices.clear(); |
| 168 |
m_normals.clear(); |
| 169 |
m_colors.clear(); |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
Mesh& Mesh::operator=(const Mesh& other) |
| 174 |
{ |
| 175 |
m_vertices = other.m_vertices; |
| 176 |
m_normals = other.m_normals; |
| 177 |
m_colors = other.m_colors; |
| 178 |
m_name = other.m_name; |
| 179 |
m_isoValue = other.m_isoValue; |
| 180 |
m_triangles = other.m_triangles; |
| 181 |
|
| 182 |
return *this; |
| 183 |
} |
| 184 |
|
| 185 |
void Mesh::smooth(int iterationCount) |
| 186 |
{ |
| 187 |
if (m_vertices.empty() || iterationCount <= 0) |
| 188 |
return; |
| 189 |
|
| 190 |
// Build vertex adjacency information from triangles (1-ring) |
| 191 |
std::vector<std::vector<size_t>> adjacencyList(m_vertices.size()); |
| 192 |
for (const auto& tri : m_triangles) { |
| 193 |
size_t i = static_cast<size_t>(tri.x()); |
| 194 |
size_t j = static_cast<size_t>(tri.y()); |
| 195 |
size_t k = static_cast<size_t>(tri.z()); |
| 196 |
|
| 197 |
adjacencyList[i].push_back(j); |
| 198 |
adjacencyList[i].push_back(k); |
| 199 |
adjacencyList[j].push_back(i); |
| 200 |
adjacencyList[j].push_back(k); |
| 201 |
adjacencyList[k].push_back(i); |
| 202 |
adjacencyList[k].push_back(j); |
| 203 |
} |
| 204 |
|
| 205 |
// Remove duplicate neighbors and sort for faster lookups later (if needed) |
| 206 |
for (auto& neighbors : adjacencyList) { |
| 207 |
std::sort(neighbors.begin(), neighbors.end()); |
| 208 |
neighbors.erase(std::unique(neighbors.begin(), neighbors.end()), |
| 209 |
neighbors.end()); |
| 210 |
} |
| 211 |
|
| 212 |
float weight = 1.0f; |
| 213 |
for (int iteration = 0; iteration < iterationCount; ++iteration) { |
| 214 |
Array<Vector3f> newVertices = m_vertices; // Store smoothed vertices |
| 215 |
|
| 216 |
for (size_t i = 0; i < m_vertices.size(); ++i) { |
| 217 |
Vector3f sum(0.0f, 0.0f, 0.0f); |
| 218 |
size_t neighborCount = adjacencyList[i].size(); |
| 219 |
|
| 220 |
if (neighborCount > 0) { // Prevent division by zero for isolated vertices |
| 221 |
for (size_t neighbor : adjacencyList[i]) { |
| 222 |
sum += m_vertices[neighbor]; |
| 223 |
} |
| 224 |
sum += weight * m_vertices[i]; |
| 225 |
newVertices[i] = sum / (weight + neighborCount); |
| 226 |
} // Else keep the original vertex position if isolated |
| 227 |
} |
| 228 |
m_vertices = newVertices; // Update vertices after processing all |
| 229 |
} |
| 230 |
|
| 231 |
m_normals.clear(); |
| 232 |
m_normals.resize(m_vertices.size(), Vector3f(0.0f, 0.0f, 0.0f)); |
| 233 |
|
| 234 |
for (const auto& tri : m_triangles) { |
| 235 |
size_t i = static_cast<size_t>(tri.x()); |
| 236 |
size_t j = static_cast<size_t>(tri.y()); |
| 237 |
size_t k = static_cast<size_t>(tri.z()); |
| 238 |
|
| 239 |
Vector3f a = m_vertices[i]; |
| 240 |
Vector3f b = m_vertices[j]; |
| 241 |
Vector3f c = m_vertices[k]; |
| 242 |
Vector3f triangleNormal = (b - a).cross(c - a).normalized(); |
| 243 |
|
| 244 |
m_normals[i] += triangleNormal; |
| 245 |
m_normals[j] += triangleNormal; |
| 246 |
m_normals[k] += triangleNormal; |
| 247 |
} |
| 248 |
|
| 249 |
for (auto& normal : m_normals) { |
| 250 |
normal.normalize(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
} // End namespace Avogadro::Core |