]> Creatis software - creaVtk.git/blob - lib/creaVtk/Utilities.h
#3493 MeshManager
[creaVtk.git] / lib / creaVtk / Utilities.h
1 /*
2 Copyright 2012-2022 Ronald Römer
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #ifndef __Utilities_h
18 #define __Utilities_h
19
20 #include <iostream>
21 #include <type_traits>
22 #include <functional>
23 #include <vector>
24 #include <deque>
25 #include <map>
26
27 #include <vtkPolyData.h>
28 #include <vtkKdTreePointLocator.h>
29 #include <vtkPoints.h>
30 #include <vtkIdList.h>
31 #include <vtkMath.h>
32
33 #define NOTSET -1
34
35 double GetAngle (double *vA, double *vB, double *n);
36
37 /* VTK */
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);
41
42 class Point3d {
43 public:
44     const double x, y, z;
45     vtkIdType id;
46
47     Point3d () = delete;
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);
56
57         return std::tie(x1, y1, z1) < std::tie(x2, y2, z2);
58     }
59     bool operator== (const Point3d &other) const {
60         return !(*this < other) && !(other < *this);
61     }
62
63     friend std::ostream& operator<< (std::ostream &out, const Point3d &p) {
64         out << "Point3d(x=" << p.x
65             << ", y=" << p.y
66             << ", z=" << p.z
67             << ", id=" << p.id << ")";
68         return out;
69     }
70 };
71
72 typedef std::vector<vtkIdType> IdsType;
73
74 template<typename T,
75     typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
76 class _Pair {
77 public:
78     T f, g;
79     _Pair () = delete;
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);
83     }
84     bool operator== (const _Pair &other) const {
85         return f == other.f && g == other.g;
86     }
87     friend std::ostream& operator<< (std::ostream &out, const _Pair &p) {
88         out << "(" << p.f << ", " << p.g << ")";
89         return out;
90     }
91 };
92
93 // typedef _Pair<vtkIdType> Pair;
94 using Pair = _Pair<vtkIdType>;
95
96 template<typename T,
97     typename U,
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>
101 T Mod (T a, U b) {
102     T _b = static_cast<T>(b);
103
104     return ((a%_b)+_b)%_b;
105 }
106
107 class Base {
108 public:
109     Base () {}
110     Base (vtkPoints *pts, vtkIdType num, const vtkIdType *poly);
111     double n[3], ei[3], ej[3], d;
112 };
113
114 void Transform (const double *in, double *out, const Base &base);
115 void BackTransform (const double *in, double *out, const Base &base);
116
117 inline void Cpy (double *a, const double *b, const int n = 2) {
118     std::copy_n(b, n, a);
119 }
120
121 template<typename T>
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);
124 }
125
126 typedef std::vector<Point3d> Poly;
127 typedef std::vector<Poly> PolysType;
128
129 void ComputeNormal (const Poly &poly, double *n);
130 bool PointInPoly (const Poly &poly, const Point3d &p);
131
132 void WritePolys (const char *name, const PolysType &polys);
133
134 typedef std::deque<vtkIdType> IndexedPoly;
135 typedef std::vector<IndexedPoly> IndexedPolysType;
136
137 typedef std::map<vtkIdType, std::reference_wrapper<const Point3d>> ReferencedPointsType;
138
139 void GetPolys (const ReferencedPointsType &pts, const IndexedPolysType &indexedPolys, PolysType &polys);
140
141 #endif