PeriDyno 1.0.0
Loading...
Searching...
No Matches
PoissonEmitter.cpp
Go to the documentation of this file.
1#include "PoissonEmitter.h"
3
4#include <time.h>
5
6#include <stdlib.h>
7
8namespace dyno
9{
10 template<typename TDataType>
12 : ParticleEmitter<TDataType>()
13 {
14 srand(time(0));
15
16 this->varWidth()->setRange(0.01, 10.0f);
17 this->varHeight()->setRange(0.01, 10.0f);
18
19 this->varSamplingDistance()->setValue(0.008f);
20
21 this->stateOutline()->setDataPtr(std::make_shared<EdgeSet<TDataType>>());
22
23 auto callback = std::make_shared<FCallBackFunc>(std::bind(&PoissonEmitter<TDataType>::tranformChanged, this));
24
25 this->varLocation()->attach(callback);
26 this->varScale()->attach(callback);
27 this->varRotation()->attach(callback);
28
29 this->varWidth()->attach(callback);
30 this->varHeight()->attach(callback);
31
32 mPlane = std::make_shared< PoissonPlane<TDataType>>();
33
34 auto wireRender = std::make_shared<GLWireframeVisualModule>();
35 wireRender->setColor(Color(0, 1, 0));
36 this->stateOutline()->connect(wireRender->inEdgeSet());
37 this->graphicsPipeline()->pushModule(wireRender);
38 }
39
40 template<typename TDataType>
44
45
46
47 template<typename TDataType>
49 {
50 mCounter++;
51 uint delayStart = this->varDelayStart()->getValue();
52 //std::cout << delayStart << ", " << mCounter << std::endl;
53 if (delayStart > mCounter)
54 {
55 this->mPosition.reset();
56 this->mVelocity.reset();
57 return;
58 }
59
60
61 //std::cout << mPlane->getPoints().size() << std::endl;
62
63 std::vector<Vec2f> temp_points = mPlane->getPoints();
64
65 auto sampling_distance = this->varSamplingDistance()->getData();
66
67 if (sampling_distance < EPSILON)
68 sampling_distance = Real(0.005);
69
70 auto center = this->varLocation()->getData();
71 auto scale = this->varScale()->getData();
72
73 auto quat = this->computeQuaternion();
74
75 Transform<Real, 3> tr(center, quat.toMatrix3x3(), scale);
76
77 std::vector<Coord> pos_list;
78 std::vector<Coord> vel_list;
79
80 Coord v0 = this->varVelocityMagnitude()->getData()*quat.rotate(Vec3f(0, -1, 0));
81
82 auto w = 0.5 * this->varWidth()->getData();
83 auto h = 0.5 * this->varHeight()->getData();
84
85 //mPlane->varUpper()->setValue(Vec2f(w, h));
86 //mPlane->varLower()->setValue(Vec2f(-w, -h));
87 //mPlane->compute();
88 //mPlane->varSamplingDistance()->setValue(this->varSamplingDistance()->getValue());
89
90 if (this->varEmitterShape()->getData() == EmitterShape::Round)
91 {
92 Real radius = w < h ? w : h;
93
94 mPlane->varUpper()->setValue(Vec2f(radius, radius));
95 mPlane->varLower()->setValue(Vec2f(-radius, -radius));
96 mPlane->compute();
97 mPlane->varSamplingDistance()->setValue(this->varSamplingDistance()->getValue());
98
99
100 for (int i = 0; i < temp_points.size(); i++)
101 {
102 if (sqrt(temp_points[i][0] * temp_points[i][0] + temp_points[i][1] * temp_points[i][1]) < radius)
103 {
104 Coord pos_temp = Coord(temp_points[i][0], 0.0, temp_points[i][1]);
105 pos_list.push_back(tr * pos_temp);
106 vel_list.push_back(v0);
107 }
108 }
109 }
110
111 else
112 {
113 mPlane->varUpper()->setValue(Vec2f(w, h));
114 mPlane->varLower()->setValue(Vec2f(-w, -h));
115 mPlane->compute();
116 mPlane->varSamplingDistance()->setValue(this->varSamplingDistance()->getValue());
117
118
119 for (int i = 0; i < temp_points.size(); i++)
120 {
121 Coord pos_temp = Coord(temp_points[i][0], 0.0, temp_points[i][1]);
122 pos_list.push_back(tr * pos_temp);
123 vel_list.push_back(v0);
124 }
125 }
126
127 if (pos_list.size() > 0)
128 {
129 this->mPosition.resize(pos_list.size());
130 this->mVelocity.resize(pos_list.size());
131
132 this->mPosition.assign(pos_list);
133 this->mVelocity.assign(vel_list);
134 }
135
136
137 pos_list.clear();
138 vel_list.clear();
139 }
140
141 template<typename TDataType>
143 {
144 std::vector<Coord> vertices;
145 std::vector<TopologyModule::Edge> edges;
146
147 auto center = this->varLocation()->getData();
148 auto scale = this->varScale()->getData();
149
150 auto quat = this->computeQuaternion();
151
152 auto w = this->varWidth()->getData();
153 auto h = this->varHeight()->getData();
154
155 Transform<Real, 3> tr(Coord(0), quat.toMatrix3x3(), scale);
156
157 auto Nx = tr * Coord(0.5 * w, 0, 0);
158 auto Nz = tr * Coord(0, 0, 0.5 * h);
159
160 vertices.push_back(center + Nx + Nz);
161 vertices.push_back(center + Nx - Nz);
162 vertices.push_back(center - Nx - Nz);
163 vertices.push_back(center - Nx + Nz);
164
165 edges.push_back(TopologyModule::Edge(0, 1));
166 edges.push_back(TopologyModule::Edge(1, 2));
167 edges.push_back(TopologyModule::Edge(2, 3));
168 edges.push_back(TopologyModule::Edge(3, 0));
169
170 auto edgeTopo = this->stateOutline()->getDataPtr();
171
172 edgeTopo->setPoints(vertices);
173 edgeTopo->setEdges(edges);
174
175 vertices.clear();
176 edges.clear();
177 }
178
179
180 template<typename TDataType>
187
189}
#define DEFINE_CLASS(name)
Definition Object.h:140
double Real
Definition Typedef.inl:23
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:320
Quat< Real > computeQuaternion()
DArray< Coord > mVelocity
DArray< Coord > mPosition
TDataType::Coord Coord
std::shared_ptr< PoissonPlane< TDataType > > mPlane
void resetStates() override
void generateParticles() override
TDataType::Real Real
VectorND< PointType, 2 > Edge
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 2 > Vec2f
Definition Vector2D.h:81
constexpr Real EPSILON
Definition Typedef.inl:42
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
DYN_FUNC Complex< Real > sqrt(const Complex< Real > &)
Definition Complex.inl:321
unsigned int uint
Definition VkProgram.h:14
vgm::Quat quat
Definition vgMath.h:633