PeriDyno 1.0.0
Loading...
Searching...
No Matches
SquareEmitter.cpp
Go to the documentation of this file.
1#include "SquareEmitter.h"
2#include <time.h>
3
4#include <stdlib.h>
5
6namespace dyno
7{
9
12 {
13 srand(time(0));
14
15 this->varWidth()->setRange(0.01, 10.0f);
16 this->varHeight()->setRange(0.01, 10.0f);
17
18 this->stateOutline()->setDataPtr(std::make_shared<EdgeSet>());
19
20 auto callback = std::make_shared<FCallBackFunc>(std::bind(&SquareEmitter::tranformChanged, this));
21
22 this->varLocation()->attach(callback);
23 this->varScale()->attach(callback);
24 this->varRotation()->attach(callback);
25
26 this->varWidth()->attach(callback);
27 this->varHeight()->attach(callback);
28 }
29
30 SquareEmitter::~SquareEmitter()
31 {
32 }
33
34 void SquareEmitter::generateParticles()
35 {
36 auto sampling_distance = this->varSamplingDistance()->getData();
37
38 if (sampling_distance < EPSILON)
39 sampling_distance = 0.005f;
40
41 auto center = this->varLocation()->getData();
42 auto scale = this->varScale()->getData();
43
44 auto quat = this->computeQuaternion();
45
46 Transform<float, 3> tr(center, quat.toMatrix3x3(), scale);
47
48 std::vector<Vec3f> pos_list;
49 std::vector<Vec3f> vel_list;
50
51 Vec3f v0 = this->varVelocityMagnitude()->getData()*quat.rotate(Vec3f(0, -1, 0));
52
53 auto w = 0.5 * this->varWidth()->getData();
54 auto h = 0.5 * this->varHeight()->getData();
55
56 for (float x = -w; x <= w; x += sampling_distance)
57 {
58 for (float z = -h; z <= h; z += sampling_distance)
59 {
60 Vec3f p = Vec3f(x, 0, z);
61 if (rand() % 5 == 0)
62 {
63 pos_list.push_back(tr * p);
64 vel_list.push_back(v0);
65 }
66 }
67 }
68
69 if (pos_list.size() > 0)
70 {
71 this->mPosition.resize(pos_list.size());
72 this->mVelocity.resize(pos_list.size());
73
74 this->mPosition.assign(pos_list);
75 this->mVelocity.assign(vel_list);
76 }
77
78
79 pos_list.clear();
80 vel_list.clear();
81 }
82
83 void SquareEmitter::tranformChanged()
84 {
85 std::vector<Vec3f> vertices;
86 std::vector<TopologyModule::Edge> edges;
87
88 auto center = this->varLocation()->getData();
89 auto scale = this->varScale()->getData();
90
91 auto quat = this->computeQuaternion();
92
93 auto w = this->varWidth()->getData();
94 auto h = this->varHeight()->getData();
95
96 Transform<float, 3> tr(Coord(0), quat.toMatrix3x3(), scale);
97
98 auto Nx = tr * Vec3f(0.5 * w, 0, 0);
99 auto Nz = tr * Vec3f(0, 0, 0.5 * h);
100
101 vertices.push_back(center + Nx + Nz);
102 vertices.push_back(center + Nx - Nz);
103 vertices.push_back(center - Nx - Nz);
104 vertices.push_back(center - Nx + Nz);
105
106 edges.push_back(TopologyModule::Edge(0, 1));
107 edges.push_back(TopologyModule::Edge(1, 2));
108 edges.push_back(TopologyModule::Edge(2, 3));
109 edges.push_back(TopologyModule::Edge(3, 0));
110
111 auto edgeTopo = this->stateOutline()->getDataPtr();
112
113 edgeTopo->setPoints(vertices);
114 edgeTopo->setEdges(edges);
115
116 vertices.clear();
117 edges.clear();
118 }
119
120 void SquareEmitter::resetStates()
121 {
122 tranformChanged();
123 }
124}
#define IMPLEMENT_CLASS(name)
Definition Object.h:79
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
TEMPLATE_TYPENAME_T MAT4_T scale(MAT4_T const &m, VEC3_T const &v)
Definition vgMath.h:506
vgm::Quat quat
Definition vgMath.h:633