2 Copyright 2012-2022 Ronald Römer
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
21 #include <type_traits>
27 #include <vtkPolyData.h>
28 #include <vtkKdTreePointLocator.h>
29 #include <vtkPoints.h>
30 #include <vtkIdList.h>
35 double GetAngle (double *vA, double *vB, double *n);
38 void ComputeNormal (vtkPoints *pts, double *n, vtkIdType num, const vtkIdType *poly);
39 void FindPoints (vtkKdTreePointLocator *pl, const double *pt, vtkIdList *pts, double tol = 1e-6);
40 void WriteVTK (const char *name, vtkPolyData *pd);
48 Point3d (const double _x, const double _y, const double _z, vtkIdType _id = NOTSET) : x(_x), y(_y), z(_z), id(_id) {}
49 bool operator< (const Point3d &other) const {
50 const long x1 = std::lround(x*1e5),
51 y1 = std::lround(y*1e5),
52 z1 = std::lround(z*1e5),
53 x2 = std::lround(other.x*1e5),
54 y2 = std::lround(other.y*1e5),
55 z2 = std::lround(other.z*1e5);
57 return std::tie(x1, y1, z1) < std::tie(x2, y2, z2);
59 bool operator== (const Point3d &other) const {
60 return !(*this < other) && !(other < *this);
63 friend std::ostream& operator<< (std::ostream &out, const Point3d &p) {
64 out << "Point3d(x=" << p.x
67 << ", id=" << p.id << ")";
72 typedef std::vector<vtkIdType> IdsType;
75 typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
80 _Pair (T _f, T _g) : f(_f), g(_g) {}
81 bool operator< (const _Pair &other) const {
82 return std::tie(f, g) < std::tie(other.f, other.g);
84 bool operator== (const _Pair &other) const {
85 return f == other.f && g == other.g;
87 friend std::ostream& operator<< (std::ostream &out, const _Pair &p) {
88 out << "(" << p.f << ", " << p.g << ")";
93 // typedef _Pair<vtkIdType> Pair;
94 using Pair = _Pair<vtkIdType>;
98 typename std::enable_if<std::is_integral<T>::value
99 && std::is_integral<U>::value
100 && std::is_signed<T>::value, bool>::type = true>
102 T _b = static_cast<T>(b);
104 return ((a%_b)+_b)%_b;
110 Base (vtkPoints *pts, vtkIdType num, const vtkIdType *poly);
111 double n[3], ei[3], ej[3], d;
114 void Transform (const double *in, double *out, const Base &base);
115 void BackTransform (const double *in, double *out, const Base &base);
117 inline void Cpy (double *a, const double *b, const int n = 2) {
118 std::copy_n(b, n, a);
122 std::ostream& operator<< (typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e) {
123 return stream << static_cast<typename std::underlying_type<T>::type>(e);
126 typedef std::vector<Point3d> Poly;
127 typedef std::vector<Poly> PolysType;
129 void ComputeNormal (const Poly &poly, double *n);
130 bool PointInPoly (const Poly &poly, const Point3d &p);
132 void WritePolys (const char *name, const PolysType &polys);
134 typedef std::deque<vtkIdType> IndexedPoly;
135 typedef std::vector<IndexedPoly> IndexedPolysType;
137 typedef std::map<vtkIdType, std::reference_wrapper<const Point3d>> ReferencedPointsType;
139 void GetPolys (const ReferencedPointsType &pts, const IndexedPolysType &indexedPolys, PolysType &polys);