File size: 15,247 Bytes
985c397 | 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <cmath>
#include <Base/Tools.h>
#include "Algorithm.h"
#include "Approximation.h"
#include "Iterator.h"
#include "MeshKernel.h"
#include "Smoothing.h"
using namespace MeshCore;
AbstractSmoothing::AbstractSmoothing(MeshKernel& m)
: kernel(m)
{}
AbstractSmoothing::~AbstractSmoothing() = default;
void AbstractSmoothing::initialize(Component comp, Continuity cont)
{
this->component = comp;
this->continuity = cont;
}
PlaneFitSmoothing::PlaneFitSmoothing(MeshKernel& m)
: AbstractSmoothing(m)
{}
void PlaneFitSmoothing::Smooth(unsigned int iterations)
{
MeshCore::MeshPoint center;
MeshCore::MeshPointArray PointArray = kernel.GetPoints();
MeshCore::MeshPointIterator v_it(kernel);
MeshCore::MeshRefPointToPoints vv_it(kernel);
MeshCore::MeshPointArray::_TConstIterator v_beg = kernel.GetPoints().begin();
for (unsigned int i = 0; i < iterations; i++) {
Base::Vector3f N, L;
for (v_it.Begin(); v_it.More(); v_it.Next()) {
MeshCore::PlaneFit pf;
pf.AddPoint(*v_it);
center = *v_it;
const std::set<PointIndex>& cv = vv_it[v_it.Position()];
if (cv.size() < 3) {
continue;
}
std::set<PointIndex>::const_iterator cv_it;
for (cv_it = cv.begin(); cv_it != cv.end(); ++cv_it) {
pf.AddPoint(v_beg[*cv_it]);
center += v_beg[*cv_it];
}
float scale = 1.0F / (static_cast<float>(cv.size()) + 1.0F);
center.Scale(scale, scale, scale);
// get the mean plane of the current vertex with the surrounding vertices
pf.Fit();
N = pf.GetNormal();
N.Normalize();
// look in which direction we should move the vertex
L.Set(v_it->x - center.x, v_it->y - center.y, v_it->z - center.z);
if (N * L < 0.0F) {
N.Scale(-1.0, -1.0, -1.0);
}
// maximum value to move is distance to mean plane
float d = std::min<float>(std::fabs(this->maximum), fabs(N * L));
N.Scale(d, d, d);
PointArray[v_it.Position()].Set(v_it->x - N.x, v_it->y - N.y, v_it->z - N.z);
}
// assign values without affecting iterators
PointIndex count = kernel.CountPoints();
for (PointIndex idx = 0; idx < count; idx++) {
kernel.SetPoint(idx, PointArray[idx]);
}
}
}
void PlaneFitSmoothing::SmoothPoints(unsigned int iterations, const std::vector<PointIndex>& point_indices)
{
MeshCore::MeshPoint center;
MeshCore::MeshPointArray PointArray = kernel.GetPoints();
MeshCore::MeshPointIterator v_it(kernel);
MeshCore::MeshRefPointToPoints vv_it(kernel);
MeshCore::MeshPointArray::_TConstIterator v_beg = kernel.GetPoints().begin();
for (unsigned int i = 0; i < iterations; i++) {
Base::Vector3f N, L;
for (PointIndex it : point_indices) {
v_it.Set(it);
MeshCore::PlaneFit pf;
pf.AddPoint(*v_it);
center = *v_it;
const std::set<PointIndex>& cv = vv_it[v_it.Position()];
if (cv.size() < 3) {
continue;
}
std::set<PointIndex>::const_iterator cv_it;
for (cv_it = cv.begin(); cv_it != cv.end(); ++cv_it) {
pf.AddPoint(v_beg[*cv_it]);
center += v_beg[*cv_it];
}
float scale = 1.0F / (static_cast<float>(cv.size()) + 1.0F);
center.Scale(scale, scale, scale);
// get the mean plane of the current vertex with the surrounding vertices
pf.Fit();
N = pf.GetNormal();
N.Normalize();
// look in which direction we should move the vertex
L.Set(v_it->x - center.x, v_it->y - center.y, v_it->z - center.z);
if (N * L < 0.0F) {
N.Scale(-1.0, -1.0, -1.0);
}
// maximum value to move is distance to mean plane
float d = std::min<float>(std::fabs(this->maximum), fabs(N * L));
N.Scale(d, d, d);
PointArray[v_it.Position()].Set(v_it->x - N.x, v_it->y - N.y, v_it->z - N.z);
}
// assign values without affecting iterators
PointIndex count = kernel.CountPoints();
for (PointIndex idx = 0; idx < count; idx++) {
kernel.SetPoint(idx, PointArray[idx]);
}
}
}
LaplaceSmoothing::LaplaceSmoothing(MeshKernel& m)
: AbstractSmoothing(m)
{}
void LaplaceSmoothing::Umbrella(
const MeshRefPointToPoints& vv_it,
const MeshRefPointToFacets& vf_it,
double stepsize
)
{
const MeshCore::MeshPointArray& points = kernel.GetPoints();
MeshCore::MeshPointArray::_TConstIterator v_it, v_beg = points.begin(), v_end = points.end();
PointIndex pos = 0;
for (v_it = points.begin(); v_it != v_end; ++v_it, ++pos) {
const std::set<PointIndex>& cv = vv_it[pos];
if (cv.size() < 3) {
continue;
}
if (cv.size() != vf_it[pos].size()) {
// do nothing for border points
continue;
}
size_t n_count = cv.size();
double w {};
w = 1.0 / double(n_count);
double delx = 0.0, dely = 0.0, delz = 0.0;
std::set<PointIndex>::const_iterator cv_it;
for (cv_it = cv.begin(); cv_it != cv.end(); ++cv_it) {
delx += w * static_cast<double>((v_beg[*cv_it]).x - v_it->x);
dely += w * static_cast<double>((v_beg[*cv_it]).y - v_it->y);
delz += w * static_cast<double>((v_beg[*cv_it]).z - v_it->z);
}
float x = static_cast<float>(static_cast<double>(v_it->x) + stepsize * delx);
float y = static_cast<float>(static_cast<double>(v_it->y) + stepsize * dely);
float z = static_cast<float>(static_cast<double>(v_it->z) + stepsize * delz);
kernel.SetPoint(pos, x, y, z);
}
}
void LaplaceSmoothing::Umbrella(
const MeshRefPointToPoints& vv_it,
const MeshRefPointToFacets& vf_it,
double stepsize,
const std::vector<PointIndex>& point_indices
)
{
const MeshCore::MeshPointArray& points = kernel.GetPoints();
MeshCore::MeshPointArray::_TConstIterator v_beg = points.begin();
for (PointIndex it : point_indices) {
const std::set<PointIndex>& cv = vv_it[it];
if (cv.size() < 3) {
continue;
}
if (cv.size() != vf_it[it].size()) {
// do nothing for border points
continue;
}
size_t n_count = cv.size();
double w {};
w = 1.0 / double(n_count);
double delx = 0.0, dely = 0.0, delz = 0.0;
std::set<PointIndex>::const_iterator cv_it;
for (cv_it = cv.begin(); cv_it != cv.end(); ++cv_it) {
delx += w * static_cast<double>((v_beg[*cv_it]).x - (v_beg[it]).x);
dely += w * static_cast<double>((v_beg[*cv_it]).y - (v_beg[it]).y);
delz += w * static_cast<double>((v_beg[*cv_it]).z - (v_beg[it]).z);
}
float x = static_cast<float>(static_cast<double>((v_beg[it]).x) + stepsize * delx);
float y = static_cast<float>(static_cast<double>((v_beg[it]).y) + stepsize * dely);
float z = static_cast<float>(static_cast<double>((v_beg[it]).z) + stepsize * delz);
kernel.SetPoint(it, x, y, z);
}
}
void LaplaceSmoothing::Smooth(unsigned int iterations)
{
MeshCore::MeshRefPointToPoints vv_it(kernel);
MeshCore::MeshRefPointToFacets vf_it(kernel);
for (unsigned int i = 0; i < iterations; i++) {
Umbrella(vv_it, vf_it, lambda);
}
}
void LaplaceSmoothing::SmoothPoints(unsigned int iterations, const std::vector<PointIndex>& point_indices)
{
MeshCore::MeshRefPointToPoints vv_it(kernel);
MeshCore::MeshRefPointToFacets vf_it(kernel);
for (unsigned int i = 0; i < iterations; i++) {
Umbrella(vv_it, vf_it, lambda, point_indices);
}
}
TaubinSmoothing::TaubinSmoothing(MeshKernel& m)
: LaplaceSmoothing(m)
{}
void TaubinSmoothing::Smooth(unsigned int iterations)
{
MeshCore::MeshRefPointToPoints vv_it(kernel);
MeshCore::MeshRefPointToFacets vf_it(kernel);
// Theoretically Taubin does not shrink the surface
iterations = (iterations + 1) / 2; // two steps per iteration
for (unsigned int i = 0; i < iterations; i++) {
Umbrella(vv_it, vf_it, GetLambda());
Umbrella(vv_it, vf_it, -(GetLambda() + micro));
}
}
void TaubinSmoothing::SmoothPoints(unsigned int iterations, const std::vector<PointIndex>& point_indices)
{
MeshCore::MeshRefPointToPoints vv_it(kernel);
MeshCore::MeshRefPointToFacets vf_it(kernel);
// Theoretically Taubin does not shrink the surface
iterations = (iterations + 1) / 2; // two steps per iteration
for (unsigned int i = 0; i < iterations; i++) {
Umbrella(vv_it, vf_it, GetLambda(), point_indices);
Umbrella(vv_it, vf_it, -(GetLambda() + micro), point_indices);
}
}
namespace
{
using AngleNormal = std::pair<double, Base::Vector3d>;
inline Base::Vector3d find_median(std::vector<AngleNormal>& container)
{
auto compare_angle_normal = [](const AngleNormal& an1, const AngleNormal& an2) {
return an1.first < an2.first;
};
size_t n = container.size() / 2;
std::nth_element(container.begin(), container.begin() + n, container.end(), compare_angle_normal);
if ((container.size() % 2) == 1) {
return container[n].second;
}
// even sized vector -> average the two middle values
auto max_it = std::max_element(container.begin(), container.begin() + n, compare_angle_normal);
Base::Vector3d vec = (max_it->second + container[n].second) / 2.0;
vec.Normalize();
return vec;
}
} // namespace
MedianFilterSmoothing::MedianFilterSmoothing(MeshKernel& m)
: AbstractSmoothing(m)
{}
void MedianFilterSmoothing::Smooth(unsigned int iterations)
{
std::vector<unsigned long> point_indices(kernel.CountPoints());
std::generate(point_indices.begin(), point_indices.end(), Base::iotaGen<unsigned long>(0));
MeshCore::MeshRefFacetToFacets ff_it(kernel);
MeshCore::MeshRefPointToFacets vf_it(kernel);
for (unsigned int i = 0; i < iterations; i++) {
UpdatePoints(ff_it, vf_it, point_indices);
}
}
void MedianFilterSmoothing::SmoothPoints(
unsigned int iterations,
const std::vector<PointIndex>& point_indices
)
{
MeshCore::MeshRefFacetToFacets ff_it(kernel);
MeshCore::MeshRefPointToFacets vf_it(kernel);
for (unsigned int i = 0; i < iterations; i++) {
UpdatePoints(ff_it, vf_it, point_indices);
}
}
void MedianFilterSmoothing::UpdatePoints(
const MeshRefFacetToFacets& ff_it,
const MeshRefPointToFacets& vf_it,
const std::vector<PointIndex>& point_indices
)
{
const MeshCore::MeshPointArray& points = kernel.GetPoints();
const MeshCore::MeshFacetArray& facets = kernel.GetFacets();
// Initialize the array with the real normals
std::vector<Base::Vector3d> faceNormals;
faceNormals.reserve(facets.size());
MeshCore::MeshFacetIterator iter(kernel);
for (iter.Init(); iter.More(); iter.Next()) {
faceNormals.emplace_back(Base::toVector<double>(iter->GetNormal()));
}
// Step 1: determine face normals
for (FacetIndex pos = 0; pos < facets.size(); pos++) {
iter.Set(pos);
Base::Vector3d refNormal = Base::toVector<double>(iter->GetNormal());
const std::set<FacetIndex>& cv = ff_it[pos];
const MeshCore::MeshFacet& facet = facets[pos];
std::vector<AngleNormal> anglesWithFaces;
for (auto fi : cv) {
iter.Set(fi);
Base::Vector3d faceNormal = Base::toVector<double>(iter->GetNormal());
double angle = refNormal.GetAngle(faceNormal);
int absWeight = std::abs(weights);
if (absWeight > 1 && facet.IsNeighbour(fi)) {
if (weights < 0) {
angle = -angle;
}
for (int i = 0; i < absWeight; i++) {
anglesWithFaces.emplace_back(angle, faceNormal);
}
}
else {
anglesWithFaces.emplace_back(angle, faceNormal);
}
}
faceNormals[pos] = find_median(anglesWithFaces);
}
// Step 2: move vertices
for (auto pos : point_indices) {
Base::Vector3d P = Base::toVector<double>(points[pos]);
const std::set<FacetIndex>& cv = vf_it[pos];
double totalArea = 0.0;
Base::Vector3d totalvT;
for (auto it : cv) {
iter.Set(it);
double faceArea = iter->Area();
totalArea += faceArea;
Base::Vector3d C = Base::toVector<double>(iter->GetGravityPoint());
Base::Vector3d PC = C - P;
Base::Vector3d mT = faceNormals[it];
Base::Vector3d vT = (PC * mT) * mT;
totalvT += vT * faceArea;
}
P = P + totalvT / totalArea;
kernel.SetPoint(pos, Base::toVector<float>(P));
}
}
|