Skip to content

ENH: update vtkbool to latest version #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 95 additions & 26 deletions CombineModels/Logic/Utilities.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2024 Ronald Römer
Copyright 2012-2025 Ronald Römer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#define _USE_MATH_DEFINES // Needed for making M_PI symbol available on Windows

#include "Utilities.h"

#include <cmath>
Expand All @@ -32,33 +30,20 @@ limitations under the License.
double ComputeNormal (vtkPoints *pts, double *n, vtkIdType num, const vtkIdType *poly) {
n[0] = 0; n[1] = 0; n[2] = 0;

if (num == 3) {
double pA[3], pB[3], pC[3], a[3], b[3];

pts->GetPoint(poly[0], pA);
pts->GetPoint(poly[1], pB);
pts->GetPoint(poly[2], pC);

vtkMath::Subtract(pB, pA, a);
vtkMath::Subtract(pC, pA, b);

vtkMath::Cross(a, b, n);
} else {
double pA[3], pB[3];
double pA[3], pB[3];

vtkIdType i, a, b;
vtkIdType i, a, b;

for (i = 0; i < num; i++) {
a = poly[i];
b = poly[i+1 == num ? 0 : i+1];
for (i = 0; i < num; i++) {
a = poly[i];
b = poly[i+1 == num ? 0 : i+1];

pts->GetPoint(a, pA);
pts->GetPoint(b, pB);
pts->GetPoint(a, pA);
pts->GetPoint(b, pB);

n[0] += (pA[1]-pB[1])*(pA[2]+pB[2]);
n[1] += (pA[2]-pB[2])*(pA[0]+pB[0]);
n[2] += (pA[0]-pB[0])*(pA[1]+pB[1]);
}
n[0] += (pA[1]-pB[1])*(pA[2]+pB[2]);
n[1] += (pA[2]-pB[2])*(pA[0]+pB[0]);
n[2] += (pA[0]-pB[0])*(pA[1]+pB[1]);
}

return vtkMath::Normalize(n);
Expand Down Expand Up @@ -258,3 +243,87 @@ void GetPolys (const ReferencedPointsType &pts, const IndexedPolysType &indexedP
polys.push_back(std::move(newPoly));
}
}

void GetPoly (vtkPoints *pts, vtkIdType num, const vtkIdType *poly, Poly &out) {
vtkIdType i;

double p[3];

for (i = 0; i < num; i++) {
pts->GetPoint(poly[i], p);

out.emplace_back(p[0], p[1], p[2], poly[i]);
}
}

void FlattenPoly (const Poly &poly, Poly &out, const Base &base) {
double pt[3], tr[2];

vtkIdType i = 0;

for (auto &p : poly) {
pt[0] = p.x;
pt[1] = p.y;
pt[2] = p.z;

Transform(pt, tr, base);

out.emplace_back(tr[0], tr[1], 0, i++);
}
}

std::shared_ptr<Proj> GetEdgeProj (const Poly &poly, const Point3d &p) {
Poly::const_iterator itrA, itrB;

double d, t;

std::shared_ptr<Point3d> proj;

for (itrA = poly.begin(); itrA != poly.end(); ++itrA) {
itrB = itrA+1;

if (itrB == poly.end()) {
itrB = poly.begin();
}

ProjOnLine(*itrA, *itrB, p, &d, &t, proj);

if (d > 0 && d < 1e-5 && t > 0 && t < 1) {
return std::make_shared<Proj>(itrA->id, itrB->id, *proj, d);
}

}

return nullptr;
}

void ProjOnLine (const Point3d &a, const Point3d &b, const Point3d &p, double *d, double *t, std::shared_ptr<Point3d> &proj) {
double v[3], w[3];

double v_ = Point3d::GetVec(a, b, v);
double w_ = Point3d::GetVec(a, p, w);

double pr = std::min(std::max(vtkMath::Dot(v, w), -1.), 1.);

*d = std::sin(std::acos(pr))*w_;

vtkMath::MultiplyScalar(v, std::sqrt(w_*w_-*d**d));

proj = std::make_shared<Point3d>(a.x+v[0], a.y+v[1], a.z+v[2]);

*t = pr*w_/v_;
}

void ProjOnLine (vtkPolyData *pd, const Pair &line, const Point3d &p, std::shared_ptr<Point3d> &proj) {
double pA[3], pB[3];

pd->GetPoint(line.f, pA);
pd->GetPoint(line.g, pB);

Point3d a(pA[0], pA[1], pA[2]);
Point3d b(pB[0], pB[1], pB[2]);

double d, t;

ProjOnLine(a, b, p, &d, &t, proj);
}
61 changes: 43 additions & 18 deletions CombineModels/Logic/Utilities.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2024 Ronald Römer
Copyright 2012-2025 Ronald Römer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@ limitations under the License.
#include <deque>
#include <map>
#include <set>
#include <memory>

#include <vtkPolyData.h>
#include <vtkKdTreePointLocator.h>
Expand All @@ -48,7 +49,7 @@ class Point3d {
vtkIdType id;

Point3d () = delete;
Point3d (const double _x, const double _y, const double _z, vtkIdType _id = NOTSET) : x(_x), y(_y), z(_z), id(_id) {}
Point3d (const double x, const double y, const double z, vtkIdType id = NOTSET) : x(x), y(y), z(z), id(id) {}
bool operator< (const Point3d &other) const {
const long x1 = std::lround(x*1e5),
y1 = std::lround(y*1e5),
Expand Down Expand Up @@ -78,34 +79,41 @@ class Point3d {

return vtkMath::Normalize(v);
}

static double GetDist (const Point3d &a, const Point3d &b) {
double dx = b.x-a.x;
double dy = b.y-a.y;
double dz = b.z-a.z;

return dx*dx+dy*dy+dz*dz;
}
};

typedef std::vector<vtkIdType> IdsType;
typedef std::set<vtkIdType> _IdsType;

#ifndef __VTK_WRAP__
template<typename T,
typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
class _Pair {
class Pair {
public:
T f, g;
_Pair () = delete;
_Pair (T _f, T _g) : f(_f), g(_g) {}
bool operator< (const _Pair &other) const {
vtkIdType f, g;
Pair () = delete;
Pair (vtkIdType f, vtkIdType g) : f(f), g(g) {}
bool operator< (const Pair &other) const {
return std::tie(f, g) < std::tie(other.f, other.g);
}
bool operator== (const _Pair &other) const {
bool operator== (const Pair &other) const {
return f == other.f && g == other.g;
}
friend std::ostream& operator<< (std::ostream &out, const _Pair &p) {
vtkIdType operator& (const Pair &other) const {
if (f == other.f || f == other.g) {
return f;
}
return g;
}
friend std::ostream& operator<< (std::ostream &out, const Pair &p) {
out << "(" << p.f << ", " << p.g << ")";
return out;
}
};
#endif // __VTK_WRAP__

// typedef _Pair<vtkIdType> Pair;
using Pair = _Pair<vtkIdType>;

class Base {
public:
Expand All @@ -126,7 +134,7 @@ std::ostream& operator<< (typename std::enable_if<std::is_enum<T>::value, std::o
return stream << static_cast<typename std::underlying_type<T>::type>(e);
}

typedef std::vector<Point3d> Poly;
typedef std::vector<Point3d> Poly, Points;
typedef std::vector<Poly> PolysType;

double ComputeNormal (const Poly &poly, double *n);
Expand All @@ -141,4 +149,21 @@ typedef std::map<vtkIdType, std::reference_wrapper<const Point3d>> ReferencedPoi

void GetPolys (const ReferencedPointsType &pts, const IndexedPolysType &indexedPolys, PolysType &polys);

#endif
void GetPoly (vtkPoints *pts, vtkIdType num, const vtkIdType *poly, Poly &out);
void FlattenPoly (const Poly &poly, Poly &out, const Base &base);

class Proj {
public:
Proj (vtkIdType a, vtkIdType b, const Point3d &proj, double d) : edge(a, b), proj(proj), d(d) {}

Pair edge;
Point3d proj;
double d;
};

std::shared_ptr<Proj> GetEdgeProj (const Poly &poly, const Point3d &p);

void ProjOnLine (const Point3d &a, const Point3d &b, const Point3d &p, double *d, double *t, std::shared_ptr<Point3d> &proj);
void ProjOnLine (vtkPolyData *pd, const Pair &line, const Point3d &p, std::shared_ptr<Point3d> &proj);

#endif
61 changes: 29 additions & 32 deletions CombineModels/Logic/vtkPolyDataBooleanFilter.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2024 Ronald Römer
Copyright 2012-2025 Ronald Römer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -148,6 +148,9 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio
modPdA->DeepCopy(cl->GetOutput(1));
modPdB->DeepCopy(cl->GetOutput(2));

modPdA->EditableOn();
modPdB->EditableOn();

#ifdef DEBUG
std::cout << "Exporting contLines.vtk" << std::endl;
WriteVTK("contLines.vtk", contLines);
Expand Down Expand Up @@ -175,16 +178,23 @@ int vtkPolyDataBooleanFilter::RequestData(vtkInformation *request, vtkInformatio

auto cells = vtkSmartPointer<vtkIdList>::New();

bool hasGap(false);

for (i = 0; i < numPts; i++) {
contLines->GetPointCells(i, cells);

if (cells->GetNumberOfIds() == 1) {
vtkErrorMacro("Contact ends suddenly.");
return 1;
vtkErrorMacro("Contact ends suddenly at point " << i << ".");

hasGap = true;
}

}

if (hasGap) {
return 1;
}

// sichert die OrigCellIds

vtkIdTypeArray *origCellIdsA = vtkIdTypeArray::SafeDownCast(modPdA->GetCellData()->GetScalars("OrigCellIds"));
Expand Down Expand Up @@ -614,7 +624,9 @@ bool vtkPolyDataBooleanFilter::GetPolyStrips (vtkPolyData *pd, vtkIdTypeArray *c
// sucht nach gleichen captPts

{
std::map<Point3d, std::map<vtkIdType, std::vector<std::reference_wrapper<StripPt>>>> collapsed;
// std::map<Point3d, std::map<vtkIdType, std::vector<std::reference_wrapper<StripPt>>>> collapsed;

std::map<Point3d, std::set<vtkIdType>> collapsed;

PolyStripsType::iterator itr;
StripPtsType::iterator itr2;
Expand All @@ -628,39 +640,24 @@ bool vtkPolyDataBooleanFilter::GetPolyStrips (vtkPolyData *pd, vtkIdTypeArray *c
StripPt &sp = itr2->second;

if (sp.capt & Capt::BOUNDARY) {
collapsed[{sp.cutPt[0], sp.cutPt[1], sp.cutPt[2]}][sp.ind].push_back(sp);
}
}
}
// collapsed[{sp.cutPt[0], sp.cutPt[1], sp.cutPt[2]}][sp.ind].push_back(sp);

for (auto &[pt, map] : collapsed) {
if (map.size() > 1) {
// std::cout << pt << std::endl;
auto inds = collapsed[{sp.cutPt[0], sp.cutPt[1], sp.cutPt[2]}];

// for (auto &[ind, pts] : map) {
// for (auto &p : pts) {
// std::cout << p << std::endl;
// }
// }

// if (map.size() == 2 && std::all_of(map.begin(), map.end(), [](const auto &m) { return m.second.size() == 1 && m.second.back().get().capt == Capt::EDGE; })) {

// for (auto &[ind, pts] : map) {
// StripPt &sp = pts.back();

// sp.capt = Capt::NOT;
// sp.edge[0] = sp.edge[1] = NOTSET;
// Cpy(sp.cutPt, sp.pt, 3);
// }

// } else {
// return true;
// }

return true;
inds.emplace(sp.ind);

if (inds.size() > 1) {
return true;
}
}
}
}

// for (auto &[pt, map] : collapsed) {
// if (map.size() > 1) {
// return true;
// }
// }
}

for (itr = polyLines.begin(); itr != polyLines.end(); ++itr) {
Expand Down
14 changes: 8 additions & 6 deletions CombineModels/Logic/vtkPolyDataBooleanFilter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2024 Ronald Römer
Copyright 2012-2025 Ronald Römer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,11 +33,13 @@ limitations under the License.

#include "Utilities.h"

#define OPER_NONE 0
#define OPER_UNION 1
#define OPER_INTERSECTION 2
#define OPER_DIFFERENCE 3
#define OPER_DIFFERENCE2 4
enum OperMode {
OPER_NONE = 0,
OPER_UNION,
OPER_INTERSECTION,
OPER_DIFFERENCE,
OPER_DIFFERENCE2
};

enum class Capt {
NOT = 1 << 0,
Expand Down
Loading