PeriDyno 1.0.0
Loading...
Searching...
No Matches
PoissonPlane.cpp
Go to the documentation of this file.
1#include "PoissonPlane.h"
2
3namespace dyno
4{
5 template <typename TDataType>
10
11
12 template <typename TDataType>
14 {
15 Vec2f area_a = this->varLower()->getValue();
16 Vec2f area_b = this->varUpper()->getValue();
17
18 float r = this->varSamplingDistance()->getData();
19
20 return abs((area_b[0] - area_a[0]) * (area_b[1] - area_a[1]) / (r * r));
21 };
22
23
24 template <typename TDataType>
26 {
27 auto r = this->varSamplingDistance()->getData();
28 dx = r / sqrt(2);
29 if (dx == 0)
30 {
31 std::cout << " The smapling distance in Poisson disk sampling module can not be ZERO!!!! " << std::endl;
32 exit(0);
33 }
34
35 Vec2f area_a = this->varLower()->getValue();
36 Vec2f area_b = this->varUpper()->getValue();
37
38 mOrigin = area_a - Vec2f(4 * dx, 4 * dx);
39 mUpperBound = area_b + Vec2f(4 * dx, 4 * dx);
40
41 nx = abs(mUpperBound[0] - mOrigin[0]) / dx;
42 ny = abs(mUpperBound[1] - mOrigin[1]) / dx;
43
44 gnum = nx * ny;
45
46 m_grid.clear();
47 m_grid.resize(gnum);
48
49 for (int i = 0; i < gnum; i++) m_grid[i] = -1;
50 }
51
52
53 template <typename TDataType>
55 {
56 Vec2u index;
57 Vec2f area_a = this->varLower()->getValue();
58 Vec2f area_b = this->varUpper()->getValue();
59
60 index[0] = (int)((point[0] - mOrigin[0]) / dx);
61 index[1] = (int)((point[1] - mOrigin[1]) / dx);
62 return index;
63 };
64
65
66 template <typename TDataType>
68 {
69 return i + j * nx;
70 };
71
72 template <typename TDataType>
74 {
75 bool flag = false;
76 auto r = this->varSamplingDistance()->getData();
77 Vec2u d_index;
78 d_index = searchGrid(point);
79 for (int i = -2; i < 3; i++)
80 for (int j = -2; j < 3; j++)
81 {
82
83 int mi = d_index[0] + i;
84 int mj = d_index[1] + j;
85 if ((mi > 0) && (mj > 0) && (mi < nx) && (mj < ny))
86 {
87 int index = indexTransform(mi, mj);
88 if (m_grid[index] != -1)
89 {
90 Vec2f d = (points[m_grid[index]] - point);
91 if (sqrt(d[0] * d[0] + d[1] * d[1]) - r < EPSILON)
92 {
93 flag = true;
94 }
95 }
96 }
97 }
98 return flag;
99 };
100
101
102 template<typename TDataType>
104 {
105 auto r = this->varSamplingDistance()->getData();
107
109
110 Vec2f area_a = this->varLower()->getValue();
111 Vec2f area_b = this->varUpper()->getValue();
112
113 Vec2f seed_point = (area_a + (area_b - area_a) / 2);
114 seed_point += (Vec2f((float)(rand() % 100) / 100.0f, (float)(rand() % 100) / 100.0f) - Vec2f(0.5, 0.5)) * r;
115 //std::cout <<"Offset: " << (Vec2f((float)(rand() % 100) / 100.0f, (float)(rand() % 100) / 100.0f) - Vec2f(0.5, 0.5)) << std::endl;
116
117 points.clear();
118 points.push_back(seed_point);
119
120 gridIndex = searchGrid(seed_point);
121 int index = indexTransform(gridIndex[0], gridIndex[1]);
122 m_grid[index] = 0;
123
124 int head = 0;
125 int tail = 1;
126
127 while ((head < desired_points) && (head < tail))
128 {
129 Vec2f source = points[head];
130 head++;
131
132 for (int ppp = 0; ppp < 100; ppp++)
133 {
134 float theta = (float)(rand() % 100) / 100.0f;
135 float dr = (1 + (float)(rand() % 100) / 100.0f) * r;
136
137 theta = theta * 2 * 3.1415926535;
138
139 Vec2f offset = Vec2f(cos(theta) * dr, sin(theta) * dr);
140
141 Vec2f new_point = source + offset;
142
143 if ((new_point[0] > area_a[0] ) && (new_point[0] < area_b[0] )
144 && (new_point[1] > area_a[1] ) && (new_point[1] < area_b[1] ))
145 {
146 if (!collisionJudge(new_point) && (tail < desired_points)) {
147 points.push_back(new_point);
148 gridIndex = searchGrid(new_point);
150 tail++;
151 }
152 }
153 }
154 }
155 }
156
157
158
160}
#define DEFINE_CLASS(name)
Definition Object.h:140
std::vector< int > m_grid
bool collisionJudge(Vec2f point)
Vec2u searchGrid(Vec2f point)
void compute() override
int indexTransform(uint i, uint j)
unsigned int desired_points
std::vector< Vec2f > points
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC Complex< Real > sin(const Complex< Real > &)
Definition Complex.inl:564
Vector< float, 2 > Vec2f
Definition Vector2D.h:81
DYN_FUNC T abs(const T &v)
Definition SimpleMath.h:81
Vector< uint32_t, 2 > Vec2u
Definition Vector2D.h:83
constexpr Real EPSILON
Definition Typedef.inl:42
DYN_FUNC Complex< Real > cos(const Complex< Real > &)
Definition Complex.inl:573
DYN_FUNC Complex< Real > sqrt(const Complex< Real > &)
Definition Complex.inl:321
unsigned int uint
Definition VkProgram.h:14