VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
KdCountRayIntersectionHandler.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef KDCOUNTRAYINTERSECTIONHANDLER_H
35#define KDCOUNTRAYINTERSECTIONHANDLER_H
36
39
41
43//#include <geometry3d/KdTree/KdUtilities.h>
45
46#include <set>
47
48namespace kd_tree
49{
50template <typename T>
52{
53public:
54 int intersectRay(const Ray<T> &ray, Node<T> &parent, Node<T> *&child1, Node<T> *&child2,
55 std::set<ub_keys::Key3<int>> &mailbox) const override
56 {
57 if (parent.intersectRayBoundingBox(ray) == Intersection::INTERSECTION) {
58 if (parent.isLeaf()) {
59 return this->checkIntersectionWithTriFaces(ray, *parent.getTriFaces(), parent.getNodes(), mailbox);
60 } else {
61 int sum = 0;
62 if (child1) {
63 int erg = child1->intersectRay(ray, *this, mailbox);
64 if (erg < 0) {
65 return erg;
66 }
67 sum += erg;
68 }
69 if (child2) {
70 int erg = child2->intersectRay(ray, *this, mailbox);
71 if (erg < 0) {
72 return erg;
73 }
74 sum += erg;
75 }
76 return sum;
77 }
78 } else {
79 return 0;
80 }
81 }
82 /* ======================================================================================= */
83
84private:
85 int checkIntersectionWithTriFaces(const Ray<T> &ray, std::vector<GbTriFaceMesh3D::TriFace> &triFaces,
86 std::vector<GbTriFaceMesh3D::Vertex> &nodes,
87 std::set<ub_keys::Key3<int>> &mailbox) const
88 {
89 T e1x, e1y, e1z, e2x, e2y, e2z, px, py, pz, a, f, sx, sy, sz, u, qx, qy, qz, v, factor;
90
91 int counter = 0;
92
93 for (std::size_t i = 0; i < triFaces.size(); i++) {
95
96 if (mailbox.find(ub_keys::Key3<int>(triFace.getIndexVertex1(), triFace.getIndexVertex2(),
97 triFace.getIndexVertex3())) == mailbox.end()) {
98 mailbox.insert(
99 ub_keys::Key3<int>(triFace.getIndexVertex1(), triFace.getIndexVertex2(),
100 triFace.getIndexVertex3())); // schon hier rein, ansonsten muss man es unten bei
101 // JEDEm continue und am ende des ifs machen
102
103 GbTriFaceMesh3D::Vertex &v1 = triFace.getNode(0, nodes);
104 GbTriFaceMesh3D::Vertex &v2 = triFace.getNode(1, nodes);
105 GbTriFaceMesh3D::Vertex &v3 = triFace.getNode(2, nodes);
106
108 // Raytracing - start( Anm.: pr�ft NUR in Strahlrichtung
109 // Grundidee: Schnittpunkt in Baryzentrischen Koordinaten besimmten
110 // t(u,v,w) = w*v0 + u*v1 + v*v2
111 // mit w = 1.0-u-v, da fuer alle Punkte (u,v,w) im Dreick gilt u+v+w = 1
112 // wenn u, v oder w == 0 -> Punkt liegt auf Kante
113 // wenn u, v oder w == 1 -> Punkt liegt auf Eckpunkt (-> die anderen Werte muessen 0 )
114
115 // e1 = v1 - v0
116 e1x = v2.x - v1.x;
117 e1y = v2.y - v1.y;
118 e1z = v2.z - v1.z;
119
120 // e2 = v2 - v0
121 e2x = v3.x - v1.x;
122 e2y = v3.y - v1.y;
123 e2z = v3.z - v1.z;
124
125 // p = d x e2
126 px = ray.directionY * e2z - ray.directionZ * e2y;
127 py = ray.directionZ * e2x - ray.directionX * e2z;
128 pz = ray.directionX * e2y - ray.directionY * e2x;
129
130 // a = e1 dot p
131 a = e1x * px + e1y * py + e1z * pz;
132 // if( fabs(a)<1.E-10 ) continue;
134 continue;
135 f = T(1.0 / a);
136
137 // s = o - v0
138 sx = ray.originX - v1.x;
139 sy = ray.originY - v1.y;
140 sz = ray.originZ - v1.z;
141
142 // u = f * ( s dot p)
143 u = f * (sx * px + sy * py + sz * pz);
144
145 // u ist nur gueltig in [0;1]
146 if ((ub_math::less(u, T(0.0))) || (ub_math::greater(u, T(1.0)))) {
147 continue;
148 }
149
150 // q = s x e1
151 qx = sy * e1z - sz * e1y;
152 qy = sz * e1x - sx * e1z;
153 qz = sx * e1y - sy * e1x;
154
155 // v = f*(e2 dot q)
156 v = f * (ray.directionX * qx + ray.directionY * qy + ray.directionZ * qz);
157
158 // v ist nur gueltig in [0;1] da aber v bereits gueltig ist -> u+v darf nicht > 1.0 sein ;-)
159 if ((ub_math::less(v, T(0.0))) || (ub_math::greater(u + v, T(1.0)))) {
160 continue;
161 }
162
163 // t = f * (e2 dot q)
164 factor = f * (e2x * qx + e2y * qy + e2z * qz);
165 // Raytracing - end
167
168 if (ub_math::zero(factor)) {
169 return Intersection::ON_BOUNDARY; // ray.Org liegt direkt auf einem dreieck --> boundary
170 }
171 if (factor < 0.0) {
172 continue; // Schnittpunkt liegt in entgegengesetzter Strahlrichtung
173 }
174
175 // edge tests
176 // wenn u, v oder w ==0 -> Punkt liegt auf Kante bzw. Eckpunkt
177 if (ub_math::zero(u))
179 if (ub_math::zero(v))
181 if (ub_math::zero(T(1.0) - u - v))
183
184 counter++;
185 }
186 }
187 return counter;
188 }
189};
190} // namespace kd_tree
191
192#endif // KDCOUNTRAYLINEINTERSECTIONHANDLER_H
193
int intersectRay(const Ray< T > &ray, Node< T > &parent, Node< T > *&child1, Node< T > *&child2, std::set< ub_keys::Key3< int > > &mailbox) const override
int intersectRay(const Ray< T > &ray, const RayIntersectionHandler< T > &iHandler, std::set< ub_keys::Key3< int > > &mailbox)
Definition KdNode.h:229
std::shared_ptr< T > SPtr
bool greater(const T1 &value, const T2 &reference)
Definition UbMath.h:216
bool zero(const T &value)
Definition UbMath.h:139
bool less(const T1 &value, const T2 &reference)
Definition UbMath.h:202
static const int INTERSECTION
Definition KdUtilities.h:57
static const int ON_BOUNDARY
Definition KdUtilities.h:55
static const int INTERSECT_EDGE
Definition KdUtilities.h:56