VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
KdUtilities.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 KDUTILIES_H
35#define KDUTILIES_H
36
40
42
43#include <algorithm>
44#include <vector>
45
46namespace kd_tree
47{
48struct Axis {
49 static const int X; // = 0;
50 static const int Y; // = 1;
51 static const int Z; // = 2;
52};
53/* ======================================================================================= */
55 static const int ON_BOUNDARY; // = -2;
56 static const int INTERSECT_EDGE; // = -1;
57 static const int INTERSECTION; // = 1;
58 static const int NO_INTERSECTION; // = 0;
59};
60/* ======================================================================================= */
61template <typename T>
62inline void project2Axis(GbTriFaceMesh3D::TriFace &triFace, std::vector<GbTriFaceMesh3D::Vertex> &nodes,
63 const int &axis, std::vector<T> &projection)
64{
65 projection.resize(3);
66
67 if (axis == Axis::X) {
68 projection[0] = triFace.getV1x(nodes);
69 projection[1] = triFace.getV2x(nodes);
70 projection[2] = triFace.getV3x(nodes);
71 } else if (axis == Axis::Y) {
72 projection[0] = triFace.getV1y(nodes);
73 projection[1] = triFace.getV2y(nodes);
74 projection[2] = triFace.getV3y(nodes);
75 } else if (axis == Axis::Z) {
76 projection[0] = triFace.getV1z(nodes);
77 projection[1] = triFace.getV2z(nodes);
78 projection[2] = triFace.getV3z(nodes);
79 } else
80 throw UbException(UB_EXARGS, "unknown axis");
81
82 std::sort(projection.begin(), projection.end(), std::less<>());
83}
84/* ======================================================================================= */
85template <typename T>
86inline bool isPointOnPlane(const T &px, const T &py, const T &pz, const T &precision,
88{
89 return std::fabs((px - pointOfTriFace.x) * triFace.nx + (py - pointOfTriFace.y) * triFace.ny +
90 (pz - pointOfTriFace.z) * triFace.nz) < precision;
91}
92/* ======================================================================================= */
93template <typename T>
94inline bool isPointOnTriangle(const T &px, const T &py, const T &pz, const T &precision, GbTriFaceMesh3D::Vertex &p1,
97{
98 if (kd_tree::isPointOnPlane(px, py, pz, precision, p1, triFace)) {
99 T a_x = p1.x - px;
100 T a_y = p1.y - py;
101 T a_z = p1.z - pz;
102 T b_x = p2.x - px;
103 T b_y = p2.y - py;
104 T b_z = p2.z - pz;
105 T c_x = p3.x - px;
106 T c_y = p3.y - py;
107 T c_z = p3.z - pz;
108
109 const T factor = 0.5;
110 T Q1_x = (a_y * b_z - a_z * b_y) * factor;
111 T Q1_y = (a_z * b_x - a_x * b_z) * factor;
112 T Q1_z = (a_x * b_y - a_y * b_x) * factor;
113
114 T Q2_x = (b_y * c_z - b_z * c_y) * factor;
115 T Q2_y = (b_z * c_x - b_x * c_z) * factor;
116 T Q2_z = (b_x * c_y - b_y * c_x) * factor;
117
118 T Q3_x = (c_y * a_z - c_z * a_y) * factor;
119 T Q3_y = (c_z * a_x - c_x * a_z) * factor;
120 T Q3_z = (c_x * a_y - c_y * a_x) * factor;
121
122 T Q_x = Q1_x + Q2_x + Q3_x;
123 T Q_y = Q1_y + Q2_y + Q3_y;
124 T Q_z = Q1_z + Q2_z + Q3_z;
125
126 if (ub_math::zero(Q_x * Q1_x + Q_y * Q1_y + Q_z * Q1_z))
127 return true;
128 else if (ub_math::zero(Q_x * Q2_x + Q_y * Q2_y + Q_z * Q2_z))
129 return true;
130 else if (ub_math::zero(Q_x * Q3_x + Q_y * Q3_y + Q_z * Q3_z))
131 return true;
132 else if (ub_math::less(Q_x * Q1_x + Q_y * Q1_y + Q_z * Q1_z, T(0.0)))
133 return false;
134 else if (ub_math::less(Q_x * Q2_x + Q_y * Q2_y + Q_z * Q2_z, T(0.0)))
135 return false;
136 else if (ub_math::less(Q_x * Q3_x + Q_y * Q3_y + Q_z * Q3_z, T(0.0)))
137 return false;
138
139 return true;
140 }
141
142 return false;
143}
144/* ======================================================================================= */
145template <typename T>
147 std::vector<GbTriFaceMesh3D::Vertex> &nodes)
148{
149 GbTriFaceMesh3D::Vertex &p0 = triFace.getNode(0, nodes);
150
151 const T &n1X = val<1>(n1);
152 const T &n1Y = val<2>(n1);
153 const T &n1Z = val<3>(n1);
154
155 const T &n2X = val<1>(n2);
156 const T &n2Y = val<2>(n2);
157 const T &n2Z = val<3>(n2);
158
159 // if( kd_tree::isPointOnPlane(n1X, n1Y, n1Z, T(1.0E-6), p0, triFace)
160 // && kd_tree::isPointOnPlane(n2X, n2Y, n2Z, T(1.0E-6), p0, triFace))
161 //{
162 // return true;
163 //}
164
165 T denom = (n2X - n1X) * triFace.nx + (n2Y - n1Y) * triFace.ny + (n2Z - n1Z) * triFace.nz;
166
167 if (ub_math::zero(denom)) // line does not intersect the plane of the triangle !
168 {
169 return false;
170 } else {
171 T d = -triFace.nx * p0.x - triFace.ny * p0.y - triFace.nz * p0.z;
172 T mu = T(-1.0 * (d + n1X * triFace.nx + n1Y * triFace.ny + n1Z * triFace.nz) / denom);
173
174 if (!ub_math::inClosedInterval(mu, T(0.0),
175 T(1.0))) // Point of intersection of line and plane does not lie on the triangle
176 {
177 return false;
178 } else {
179 // intersection with plane
180
181 // Test whether Point lies inside the triangle or not
182 GbTriFaceMesh3D::Vertex &p1 = triFace.getNode(1, nodes);
183 GbTriFaceMesh3D::Vertex &p2 = triFace.getNode(2, nodes);
184
185 return kd_tree::isPointOnTriangle(n1X + ((n2X - n1X) * mu) // intersectionPointX
186 ,
187 n1Y + ((n2Y - n1Y) * mu) // intersectionPointY
188 ,
189 n1Z + ((n2Z - n1Z) * mu) // intersectionPointZ
190 ,
191 T(0.001), p0, p1, p2, triFace);
192 }
193 }
194}
195} // namespace kd_tree
196
197#endif // KDUTILIES_H
198
std::shared_ptr< T > SPtr
#define UB_EXARGS
Definition UbException.h:73
Definition Axis.h:48
bool intersectLine(const UbTuple< T, T, T > &n1, const UbTuple< T, T, T > &n2, GbTriFaceMesh3D::TriFace &triFace, std::vector< GbTriFaceMesh3D::Vertex > &nodes)
bool isPointOnPlane(const T &px, const T &py, const T &pz, const T &precision, GbTriFaceMesh3D::Vertex &pointOfTriFace, GbTriFaceMesh3D::TriFace &triFace)
Definition KdUtilities.h:86
bool isPointOnTriangle(const T &px, const T &py, const T &pz, const T &precision, GbTriFaceMesh3D::Vertex &p1, GbTriFaceMesh3D::Vertex &p2, GbTriFaceMesh3D::Vertex &p3, GbTriFaceMesh3D::TriFace &triFace)
Definition KdUtilities.h:94
void project2Axis(GbTriFaceMesh3D::TriFace &triFace, std::vector< GbTriFaceMesh3D::Vertex > &nodes, const int &axis, std::vector< T > &projection)
Definition KdUtilities.h:62
bool inClosedInterval(const T1 &value, const T2 &threshold1, const T3 &threshold2)
Definition UbMath.h:309
bool zero(const T &value)
Definition UbMath.h:139
bool less(const T1 &value, const T2 &reference)
Definition UbMath.h:202
static const int Y
Definition KdUtilities.h:50
static const int X
Definition KdUtilities.h:49
static const int Z
Definition KdUtilities.h:51
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
static const int NO_INTERSECTION
Definition KdUtilities.h:58