PeriDyno 1.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PointSet.cu
Go to the documentation of this file.
1#include "PointSet.h"
2#include <fstream>
3#include <iostream>
4#include <sstream>
5
6namespace dyno
7{
8 IMPLEMENT_TCLASS(PointSet, TDataType)
9
10 template<typename TDataType>
11 PointSet<TDataType>::PointSet()
12 : TopologyModule()
13 {
14 //this->setUpdateAlways(true);
15 }
16
17 template<typename TDataType>
18 PointSet<TDataType>::~PointSet()
19 {
20 mCoords.clear();
21 }
22
23 template<typename TDataType>
24 void PointSet<TDataType>::loadObjFile(std::string filename)
25 {
26 if (filename.size() < 5 || filename.substr(filename.size() - 4) != std::string(".obj")) {
27 std::cerr << "Error: Expected OBJ file with filename of the form <name>.obj.\n";
28 return;
29 }
30
31 std::ifstream infile(filename);
32 if (!infile) {
33 std::cerr << "Failed to open. Terminating.\n";
34 return;
35 }
36
37 int ignored_lines = 0;
38 std::string line;
39 std::vector<Coord> vertList;
40 while (!infile.eof()) {
41 std::getline(infile, line);
42
43 //.obj files sometimes contain vertex normals indicated by "vn"
44 if (line.substr(0, 1) == std::string("v") && line.substr(0, 2) != std::string("vn")) {
45 std::stringstream data(line);
46 char c;
47 Coord point;
48 data >> c >> point[0] >> point[1] >> point[2];
49 vertList.push_back(point);
50 }
51 else {
52 ++ignored_lines;
53 }
54 }
55 infile.close();
56
57 std::cout << "Total number of particles: " << vertList.size() << std::endl;
58
59 setPoints(vertList);
60
61 vertList.clear();
62 }
63
64 template<typename TDataType>
65 void PointSet<TDataType>::copyFrom(PointSet<TDataType>& pointSet)
66 {
67 mCoords.assign(pointSet.getPoints());
68 }
69
70 template<typename TDataType>
71 void PointSet<TDataType>::setPoints(const std::vector<Coord>& pos)
72 {
73 mCoords.resize(pos.size());
74 mCoords.assign(pos);
75
76 tagAsChanged();
77 }
78
79 template<typename TDataType>
80 void PointSet<TDataType>::setPoints(const DArray<Coord>& pos)
81 {
82 mCoords.resize(pos.size());
83 mCoords.assign(pos);
84
85 tagAsChanged();
86 }
87
88 template<typename TDataType>
89 void PointSet<TDataType>::setSize(int size)
90 {
91 mCoords.resize(size);
92 mCoords.reset();
93 }
94
95 template<typename TDataType>
96 void PointSet<TDataType>::requestBoundingBox(Coord& lo, Coord& hi)
97 {
98 Reduction<Coord> reduce;
99 lo = reduce.minimum(mCoords.begin(), mCoords.size());
100 hi = reduce.maximum(mCoords.begin(), mCoords.size());
101 }
102
103 template <typename Real, typename Coord>
104 __global__ void PS_Scale(
105 DArray<Coord> vertex,
106 Real s)
107 {
108 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
109 if (pId >= vertex.size()) return;
110 //return;
111 vertex[pId] = vertex[pId] * s;
112 }
113
114 template<typename TDataType>
115 void PointSet<TDataType>::scale(const Real s)
116 {
117 cuExecute(mCoords.size(), PS_Scale, mCoords, s);
118 }
119
120 template <typename Coord>
121 __global__ void PS_Scale(
122 DArray<Coord> vertex,
123 Coord s)
124 {
125 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
126 if (pId >= vertex.size()) return;
127
128 Coord pos_i = vertex[pId];
129 vertex[pId] = Coord(pos_i[0] * s[0], pos_i[1] * s[1], pos_i[2] * s[2]);
130 }
131
132 template<typename TDataType>
133 void PointSet<TDataType>::scale(const Coord s)
134 {
135 cuExecute(mCoords.size(), PS_Scale, mCoords, s);
136 }
137
138 template <typename Coord>
139 __global__ void PS_Translate(
140 DArray<Coord> vertex,
141 Coord t)
142 {
143 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
144 if (pId >= vertex.size()) return;
145
146 vertex[pId] = vertex[pId] + t;
147 }
148
149
150 template<typename TDataType>
151 void PointSet<TDataType>::translate(const Coord t)
152 {
153 cuExecute(mCoords.size(), PS_Translate, mCoords, t);
154 }
155
156 template <typename Coord>
157 __global__ void PS_Rotate(
158 DArray<Coord> vertex,
159 Coord theta,
160 Coord origin
161 )
162 {
163 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
164 if (pId >= vertex.size()) return;
165 //return;
166 Coord pos = vertex[pId];
167
168 Real x = pos[0];
169 Real y = pos[1];
170 Real z = pos[2];
171
172 pos[1] = y * cos(theta[1]) - z * sin(theta[1]);
173 pos[2] = y * sin(theta[1]) + z * cos(theta[1]);
174
175 x = pos[0];
176 y = pos[1];
177 z = pos[2];
178
179 pos[0] = x * cos(theta[0]) - y * sin(theta[0]);
180 pos[1] = x * sin(theta[0]) + y * cos(theta[0]);
181
182 x = pos[0];
183 y = pos[1];
184 z = pos[2];
185
186 pos[2] = z * cos(theta[2]) - x * sin(theta[2]);
187 pos[0] = z * sin(theta[2]) + x * cos(theta[2]);
188
189 vertex[pId] = pos;
190 }
191
192
193 template<typename TDataType>
194 void PointSet<TDataType>::rotate(const Coord angle)
195 {
196 cuExecute(mCoords.size(), PS_Rotate, mCoords, angle, Coord(0.0f));
197 }
198
199 template <typename Coord>
200 __global__ void PS_Rotate(
201 DArray<Coord> vertex,
202 Quat<Real> q)
203 {
204 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
205 if (pId >= vertex.size()) return;
206 Mat3f rot = q.toMatrix3x3();
207
208 vertex[pId] = rot * vertex[pId];
209 }
210
211 template<typename TDataType>
212 void PointSet<TDataType>::rotate(const Quat<Real> q)
213 {
214 cuExecute(mCoords.size(), PS_Rotate, mCoords, q);
215 }
216
217 template<typename TDataType>
218 bool PointSet<TDataType>::isEmpty()
219 {
220 return mCoords.size() == 0;
221 }
222
223 template<typename TDataType>
224 void PointSet<TDataType>::clear()
225 {
226 mCoords.clear();
227 }
228
229 DEFINE_CLASS(PointSet);
230}