]> Creatis software - creaVtk.git/blob - lib/creaVtk/vtkPolyDataContactFilter.h
#3493 MeshManager
[creaVtk.git] / lib / creaVtk / vtkPolyDataContactFilter.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 __vtkPolyDataContactFilter_h
18 #define __vtkPolyDataContactFilter_h
19
20 #include <map>
21 #include <tuple>
22 #include <set>
23
24 #include <vtkPolyDataAlgorithm.h>
25 #include <vtkIdTypeArray.h>
26
27 #include "Utilities.h"
28
29 class vtkOBBNode;
30 class vtkMatrix4x4;
31
32 enum class Src {
33     A,
34     B
35 };
36
37 enum class End {
38     NONE,
39     BEGIN,
40     END
41 };
42
43 class InterPt {
44 public:
45     InterPt () = delete;
46
47     InterPt (double x, double y, double z, double t, vtkIdType a, vtkIdType b, End end, Src src) : t(t), edge(a, b), end(end), src(src), srcA(NOTSET), srcB(NOTSET), inaccurate(false) {
48         pt[0] = x;
49         pt[1] = y;
50         pt[2] = z;
51     }
52
53     double pt[3], t;
54     Pair edge;
55     End end;
56     Src src;
57     vtkIdType srcA, srcB;
58     bool inaccurate;
59
60     friend std::ostream& operator<< (std::ostream &out, const InterPt &s) {
61         out << "pt [" << s.pt[0] << ", " << s.pt[1] << ", " << s.pt[2] << "]"
62             << ", t " << s.t
63             << ", edge " << s.edge
64             << ", end " << s.end
65             << ", src " << s.src;
66
67         return out;
68     }
69
70     void Merge (const InterPt &other) {
71         assert(src != other.src);
72
73         if (src == Src::A) {
74             srcA = end == End::END ? edge.g : edge.f;
75         } else {
76             srcB = end == End::END ? edge.g : edge.f;
77         }
78
79         if (std::abs(other.t-t) < 1e-5) {
80             if (other.src == Src::A) {
81                 srcA = other.end == End::END ? other.edge.g : other.edge.f;
82             } else {
83                 srcB = other.end == End::END ? other.edge.g : other.edge.f;
84             }
85         }
86
87         if (other.inaccurate) {
88             inaccurate = true;
89         }
90     }
91
92 };
93
94 typedef std::vector<InterPt> InterPtsType;
95
96 typedef std::vector<std::tuple<InterPt, InterPt, vtkIdType, vtkIdType>> OverlapsType;
97
98 typedef std::set<Pair> InvalidEdgesType;
99
100 class VTK_EXPORT vtkPolyDataContactFilter : public vtkPolyDataAlgorithm {
101
102     void PreparePolyData (vtkPolyData *pd);
103
104     static void InterEdgeLine (InterPtsType &interPts, vtkPolyData *pd, vtkIdType idA, vtkIdType idB, const double *r, const double *pt, Src src);
105     static void InterPolyLine (InterPtsType &interPts, vtkPolyData *pd, vtkIdType num, const vtkIdType *poly, const double *r, const double *pt, Src src, const double *n);
106     void InterPolys (vtkIdType idA, vtkIdType idB);
107     void OverlapLines (OverlapsType &ols, InterPtsType &intersA, InterPtsType &intersB, vtkIdType idA, vtkIdType idB);
108     void AddContactLines (InterPtsType &intersA, InterPtsType &intersB, vtkIdType idA, vtkIdType idB);
109
110     static void CheckInters (InterPtsType &interPts, vtkPolyData *pd, vtkIdType idA, vtkIdType idB);
111
112     vtkIdTypeArray *contA, *contB;
113
114     vtkPolyData *contLines;
115     vtkPoints *contPts;
116
117     vtkPolyData *pdA, *pdB;
118
119     vtkIdTypeArray *sourcesA, *sourcesB;
120
121     vtkIdTypeArray *neigsA, *neigsB;
122
123     bool invalidA, invalidB;
124     InvalidEdgesType edgesA, edgesB;
125
126     void GetInvalidEdges (vtkPolyData *pd, InvalidEdgesType &edges);
127
128     vtkIdTypeArray *accuracy;
129
130 public:
131     vtkTypeMacro(vtkPolyDataContactFilter, vtkPolyDataAlgorithm);
132
133     static vtkPolyDataContactFilter* New();
134
135     static int InterOBBNodes (vtkOBBNode *nodeA, vtkOBBNode *nodeB, vtkMatrix4x4 *mat, void *caller);
136
137 protected:
138     vtkPolyDataContactFilter ();
139     ~vtkPolyDataContactFilter ();
140
141     int ProcessRequest (vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override;
142
143     void PrintSelf (ostream&, vtkIndent) override {};
144
145 private:
146     vtkPolyDataContactFilter (const vtkPolyDataContactFilter&) = delete;
147     void operator= (const vtkPolyDataContactFilter&) = delete;
148
149 };
150
151 #endif