Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tf_smoothing_kernel.h
1/*******************************************************************************
2 * This file is part of mdcore.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
20#ifndef _MDCORE_SOURCE_TF_SMOOTHING_KERNEL_H_
21#define _MDCORE_SOURCE_TF_SMOOTHING_KERNEL_H_
22
23#include <mdcore_config.h>
24#include <cmath>
25
26#include <tf_fptype.h>
27
28
29namespace TissueForge {
30
31
32 #if defined(__x86_64__) || defined(_M_X64)
33 // faster than 1.0f/std::sqrt, but with little accuracy.
34 TF_ALWAYS_INLINE float qsqrt(const float f)
35 {
36 __m128 temp = _mm_set_ss(f);
37 temp = _mm_rsqrt_ss(temp);
38 return 1.0 / _mm_cvtss_f32(temp);
39 }
40 #endif
41
42 #if defined(__ARM_NEON)
43 TF_ALWAYS_INLINE float qsqrt(const float f)
44 {
45 return 1.0f/std::sqrt(f);
46 }
47 #endif
48
49 TF_ALWAYS_INLINE FPTYPE w_cubic_spline(FPTYPE r2, FPTYPE h) {
50 FPTYPE r = (FPTYPE)qsqrt(r2);
51 FPTYPE x = r/h;
52 FPTYPE y;
53
54 if(x < 1.f) {
55 FPTYPE x2 = x * x;
56 y = 1.f - (3.f / 2.f) * x2 + (3.f / 4.f) * x2 * x;
57 }
58 else if(x >= 1.f && x < 2.f) {
59 FPTYPE arg = 2.f - x;
60 y = (1.f / 4.f) * arg * arg * arg;
61 }
62 else {
63 y = 0.f;
64 }
65
66 return y / (M_PI * h * h * h);
67 }
68
69 TF_ALWAYS_INLINE FPTYPE grad_w_cubic_spline(FPTYPE r2, FPTYPE h) {
70 FPTYPE r = (FPTYPE)qsqrt(r2);
71 FPTYPE x = r/h;
72 FPTYPE y;
73
74 if(x < 1.f) {
75 y = (9.f / 4.f) * x * x - (3.f) * x;
76 }
77 else if(x >= 1.f && x < 2.f) {
78 FPTYPE arg = 2.f - x;
79 y = -(3.f / 4.f) * arg * arg;
80 }
81 else {
82 y = 0.f;
83 }
84
85 return y / (M_PI * h * h * h * h);
86 }
87
88 TF_ALWAYS_INLINE FPTYPE W(FPTYPE r2, FPTYPE h) { return w_cubic_spline(r2, h); };
89
90 TF_ALWAYS_INLINE FPTYPE grad_W(FPTYPE r2, FPTYPE h) { return grad_w_cubic_spline(r2, h); };
91
92};
93
94#endif // _MDCORE_SOURCE_TF_SMOOTHING_KERNEL_H_
Include Python header, disable linking to pythonX_d.lib on Windows in debug mode.
Definition tfAngleConfig.h:26