View on GitHub

Vistle

Distributed Data-parallel Scientific Visualization in VR

cellalgorithm.h
Go to the documentation of this file.
1#ifndef VISTLE_CELLINTERPOLATION_H
2#define VISTLE_CELLINTERPOLATION_H
3
4#include "export.h"
5#include "vector.h"
6#include "celltree.h"
7#include "grid.h"
8
9namespace vistle {
10
11V_COREEXPORT Vector trilinearInverse(const Vector &p0, const Vector p[8]);
12V_COREEXPORT bool originInsidePolygonZ2D(const Vector3 *corners, Index nCorners);
13V_COREEXPORT bool insidePolygon(const Vector &point, const Vector *corners, Index nCorners, const Vector &normal);
14V_COREEXPORT bool insideConvexPolygon(const Vector &point, const Vector *corners, Index nCorners, const Vector &normal);
15V_COREEXPORT std::pair<Vector, Vector> faceNormalAndCenter(Index nCorners, const Vector *corners);
16V_COREEXPORT std::pair<Vector, Vector> faceNormalAndCenter(Index nVert, const Index *verts, const Scalar *x,
17 const Scalar *y, const Scalar *z);
18V_COREEXPORT std::pair<Vector, Vector> faceNormalAndCenter(Byte type, Index f, const Index *cl, const Scalar *x,
19 const Scalar *y, const Scalar *z);
20bool V_COREEXPORT insideCell(const Vector &point, Byte type, Index nverts, const Index *cl, const Scalar *x,
21 const Scalar *y, const Scalar *z);
22
23template<typename Scalar, typename Index>
24class PointVisitationFunctor: public Celltree<Scalar, Index>::VisitFunctor {
26 typedef typename Celltree::VisitFunctor VisitFunctor;
27 typedef typename VisitFunctor::Order Order;
28
29public:
30 PointVisitationFunctor(const Vector &point): m_point(point) {}
31
32 bool checkBounds(const Scalar *min, const Scalar *max)
33 {
34#ifdef CT_DEBUG
35 std::cerr << "checkBounds: min: " << min[0] << " " << min[1] << " " << min[2] << ", max: " << max[0] << " "
36 << max[1] << " " << max[2] << std::endl;
37#endif
38 for (int i = 0; i < 3; ++i) {
39 if (min[i] > m_point[i])
40 return false;
41 if (max[i] < m_point[i])
42 return false;
43 }
44 return true;
45 }
46
47 Order operator()(const typename Celltree::Node &node)
48 {
49#ifdef CT_DEBUG
50 std::cerr << "visit subtree: Lmax: " << node.Lmax << ", Rmin: " << node.Rmin << std::endl;
51#endif
52
53 const Scalar c = m_point[node.dim];
54 if (c > node.Lmax && c < node.Rmin)
55 return VisitFunctor::None;
56 if (c < node.Rmin) {
57 return VisitFunctor::Left;
58 }
59 if (c > node.Lmax) {
61 }
62
63 const Scalar mean = Scalar(0.5) * (node.Lmax + node.Rmin);
64 if (c < mean) {
66 } else {
68 }
69 }
70
71private:
72 Vector m_point;
73};
74
75template<typename Scalar, typename Index>
76class LineVisitationFunctor: public Celltree<Scalar, Index>::VisitFunctor {
78 typedef typename Celltree::VisitFunctor VisitFunctor;
79 typedef typename VisitFunctor::Order Order;
80
81public:
82 LineVisitationFunctor(const Vector &p0, const Vector &p1): m_p0(p0), m_p1(p1) {}
83
84 bool checkBounds(const Scalar *min, const Scalar *max)
85 {
86#ifdef CT_DEBUG
87 std::cerr << "checkBounds: min: " << min[0] << " " << min[1] << " " << min[2] << ", max: " << max[0] << " "
88 << max[1] << " " << max[2] << std::endl;
89#endif
90 for (int i = 0; i < 3; ++i) {
91 if (min[i] > m_p0[i] && min[i] > m_p1[i])
92 return false;
93 if (max[i] < m_p0[i] && max[i] < m_p1[i])
94 return false;
95 }
96 return true;
97 }
98
99 Order operator()(const typename Celltree::Node &node)
100 {
101#ifdef CT_DEBUG
102 std::cerr << "visit subtree: Lmax: " << node.Lmax << ", Rmin: " << node.Rmin << std::endl;
103#endif
104
105 const Scalar c0 = m_p0[node.dim];
106 const Scalar c1 = m_p1[node.dim];
107 if (c0 > node.Lmax && c0 < node.Rmin && c1 > node.Lmax && c1 < node.Rmin)
108 return VisitFunctor::None;
109 if (c0 < node.Rmin && c1 < node.Rmin) {
110 return VisitFunctor::Left;
111 }
112 if (c0 > node.Lmax && c1 > node.Lmax) {
113 return VisitFunctor::Right;
114 }
115
116 const Scalar mean = Scalar(0.5) * (node.Lmax + node.Rmin);
117 const Scalar c = Scalar(0.5) * (c0 + c1);
118 if (c < mean) {
120 } else {
122 }
123 }
124
125private:
126 Vector m_p0, m_p1;
127};
128
129template<typename Scalar, typename Index>
130struct CellBoundsFunctor: public Celltree<Scalar, Index>::CellBoundsFunctor {
132
133 bool operator()(Index elem, Vector *min, Vector *max) const
134 {
135 auto b = m_grid->cellBounds(elem);
136 *min = b.first;
137 *max = b.second;
138 return true;
139 }
140
142};
143
144
145template<class Grid, typename Scalar, typename Index>
146class PointInclusionFunctor: public Celltree<Scalar, Index>::LeafFunctor {
147public:
148 PointInclusionFunctor(const Grid *grid, const Vector &point, bool acceptGhost = false)
149 : m_grid(grid), m_point(point), m_acceptGhost(acceptGhost), cell(InvalidIndex)
150 {}
151
152 bool operator()(Index elem)
153 {
154#ifdef CT_DEBUG
155 std::cerr << "PointInclusionFunctor: checking cell: " << elem << std::endl;
156#endif
157 if (m_acceptGhost || !m_grid->isGhostCell(elem)) {
158 if (m_grid->inside(elem, m_point)) {
159#ifdef CT_DEBUG
160 std::cerr << "PointInclusionFunctor: found cell: " << elem << std::endl;
161#endif
162 cell = elem;
163 return false; // stop traversal
164 }
165 }
166 return true;
167 }
168 const Grid *m_grid;
172};
173
174template<class Grid, typename Scalar, typename Index>
175class LineIntersectionFunctor: public Celltree<Scalar, Index>::LeafFunctor {
176public:
177 LineIntersectionFunctor(const Grid *grid, const Vector &p0, const Vector &p1, bool acceptGhost = false)
178 : m_grid(grid), m_p0(p0), m_dir(p1 - p0), m_acceptGhost(acceptGhost)
179 {}
180
181 bool operator()(Index elem)
182 {
183#ifdef CT_DEBUG
184 std::cerr << "LineIntersectionFunctor: checking cell: " << elem << std::endl;
185#endif
186 if (m_acceptGhost || !m_grid->isGhostCell(elem)) {
187 if (m_grid->cellNumFaces() != 1)
188 return true;
189 auto verts = m_grid->cellVertices(elem);
190 Index nCorners = verts.size();
191 std::vector<Vector> corners;
192 corners.reserve(nCorners);
193 for (auto v: verts) {
194 corners.emplace_back(m_grid->getVertex(v));
195 }
196 auto nc = faceNormalAndCenter(corners.size(), &corners[0]);
197 auto normal = nc.first;
198 auto center = nc.second;
199
200 const Scalar cosa = normal.dot(m_dir);
201 if (std::abs(cosa) <= 1e-7) {
202 return true;
203 }
204 const Scalar t = normal.dot(center - m_p0) / cosa;
205 if (t < 0 || t > 1) {
206 return true;
207 }
208 const auto isect = m_p0 + t * m_dir;
209 if (insidePolygon(isect, corners.data(), nCorners, normal)) {
210 intersections.emplace_back(isect, elem);
211 }
212 }
213 return true;
214 }
215 const Grid *m_grid;
218 const bool m_acceptGhost;
222 };
223 std::vector<Intersection> intersections;
224};
225
226} // namespace vistle
227#endif
Definition: celltree.h:40
Order
Definition: celltree.h:42
@ Left
Definition: celltree.h:46
@ None
Definition: celltree.h:43
@ LeftRight
Definition: celltree.h:48
@ Right
Definition: celltree.h:47
@ RightLeft
Definition: celltree.h:49
Definition: celltree.h:31
Definition: grid.h:14
virtual std::pair< Vector, Vector > cellBounds(Index elem) const =0
Definition: cellalgorithm.h:175
const bool m_acceptGhost
Definition: cellalgorithm.h:218
std::vector< Intersection > intersections
Definition: cellalgorithm.h:223
LineIntersectionFunctor(const Grid *grid, const Vector &p0, const Vector &p1, bool acceptGhost=false)
Definition: cellalgorithm.h:177
Index cell
Definition: cellalgorithm.h:221
Vector point
Definition: cellalgorithm.h:220
const Grid * m_grid
Definition: cellalgorithm.h:215
const Vector m_p0
Definition: cellalgorithm.h:216
const Vector m_dir
Definition: cellalgorithm.h:217
bool operator()(Index elem)
Definition: cellalgorithm.h:181
Definition: cellalgorithm.h:219
Definition: cellalgorithm.h:76
LineVisitationFunctor(const Vector &p0, const Vector &p1)
Definition: cellalgorithm.h:82
Order operator()(const typename Celltree::Node &node)
Definition: cellalgorithm.h:99
bool checkBounds(const Scalar *min, const Scalar *max)
Definition: cellalgorithm.h:84
Definition: cellalgorithm.h:146
PointInclusionFunctor(const Grid *grid, const Vector &point, bool acceptGhost=false)
Definition: cellalgorithm.h:148
Vector m_point
Definition: cellalgorithm.h:169
const Grid * m_grid
Definition: cellalgorithm.h:168
bool m_acceptGhost
Definition: cellalgorithm.h:170
Index cell
Definition: cellalgorithm.h:171
bool operator()(Index elem)
Definition: cellalgorithm.h:152
Definition: cellalgorithm.h:24
bool checkBounds(const Scalar *min, const Scalar *max)
Definition: cellalgorithm.h:32
PointVisitationFunctor(const Vector &point)
Definition: cellalgorithm.h:30
Order operator()(const typename Celltree::Node &node)
Definition: cellalgorithm.h:47
#define V_COREEXPORT
Definition: export.h:9
static T min(T a, T b)
Definition: messages.cpp:28
Definition: allobjects.cpp:30
bool insideConvexPolygon(const Vector &point, const Vector *corners, Index nCorners, const Vector &normal)
Definition: cellalgorithm.cpp:57
unsigned char Byte
Definition: scalar.h:9
Vector trilinearInverse(const Vector &pp0, const Vector pp[8])
Definition: cellalgorithm.cpp:11
Eigen::Matrix< Scalar, 3, 1 > Vector3
Definition: vector.h:33
bool insideCell(const Vector &point, Byte type, Index nverts, const Index *cl, const Scalar *x, const Scalar *y, const Scalar *z)
Definition: cellalgorithm.cpp:274
Vector3 Vector
Definition: vector.h:36
const Index InvalidIndex
Definition: index.h:17
float Scalar
Definition: scalar.h:14
bool insidePolygon(const Vector &point, const Vector *corners, Index nCorners, const Vector &normal)
Definition: cellalgorithm.cpp:134
bool originInsidePolygonZ2D(const Vector3 *corners, Index nCorners)
Definition: cellalgorithm.cpp:112
std::pair< Vector, Vector > faceNormalAndCenter(Byte type, Index f, const Index *cl, const Scalar *x, const Scalar *y, const Scalar *z)
Definition: cellalgorithm.cpp:211
uint32_t Index
Definition: index.h:13
Definition: celltree.h:20
Definition: cellalgorithm.h:130
const GridInterface * m_grid
Definition: cellalgorithm.h:141
bool operator()(Index elem, Vector *min, Vector *max) const
Definition: cellalgorithm.h:133
CellBoundsFunctor(const GridInterface *grid)
Definition: cellalgorithm.h:131