PeriDyno 1.0.0
Loading...
Searching...
No Matches
FixedPoints.cu
Go to the documentation of this file.
1#include <cuda_runtime.h>
2#include "Log.h"
3#include "Node.h"
4#include "FixedPoints.h"
5
6namespace dyno
7{
8 IMPLEMENT_TCLASS(FixedPoints, TDataType)
9
10 template<typename TDataType>
11 FixedPoints<TDataType>::FixedPoints()
12 : ConstraintModule()
13 {
14 }
15
16 template<typename TDataType>
17 FixedPoints<TDataType>::~FixedPoints()
18 {
19 m_bFixed.clear();
20 m_fixed_positions.clear();
21 }
22
23
24 template<typename TDataType>
25 bool FixedPoints<TDataType>::initializeImpl()
26 {
27 printf("initialized!!!!!! %d %d\n", FixedIds.size(), this->inPosition()->size());
28 if (this->inPosition()->isEmpty() || this->inVelocity()->isEmpty())
29 {
30 std::cout << "Exception: " << std::string("FixedPoints's fields are not fully initialized!") << "\n";
31 return false;
32 }
33
34 CArray<int> hostFixedIds;
35 CArray<Coord> hostFixedPos;
36
37 hostFixedIds.resize(FixedIds.size());
38 hostFixedPos.resize(FixedPos.size());
39
40 hostFixedIds.assign(FixedIds.getData());
41 hostFixedPos.assign(FixedPos.getData());
42
43 for (int i = 0; i < hostFixedIds.size(); i++)
44 {
45 addFixedPoint(hostFixedIds[i], hostFixedPos[i]);
46 }
47
48 hostFixedIds.clear();
49 hostFixedPos.clear();
50 return true;
51 }
52
53
54 template<typename TDataType>
55 void FixedPoints<TDataType>::updateContext()
56 {
57 int totalNum = this->inPosition()->getData().size();
58 if (m_bFixed.size() != totalNum)
59 {
60 m_bFixed_host.resize(totalNum);
61 m_fixed_positions_host.resize(totalNum);
62
63 m_bFixed.resize(totalNum);
64 m_fixed_positions.resize(totalNum);
65 }
66
67 for (int i = 0; i < m_bFixed_host.size(); i++)
68 {
69 m_bFixed_host[i] = 0;
70 }
71
72 for (auto it = m_fixedPts.begin(); it != m_fixedPts.end(); it++)
73 {
74 if (it->first >= 0 && it->first < totalNum)
75 {
76 m_bFixed_host[it->first] = 1;
77 m_fixed_positions_host[it->first] = it->second;
78 }
79 }
80
81 m_bFixed.assign(m_bFixed_host);
82 m_fixed_positions.assign(m_fixed_positions_host);
83 }
84
85 template<typename TDataType>
86 void FixedPoints<TDataType>::addFixedPoint(int id, Coord pt)
87 {
88 m_fixedPts[id] = pt;
89
90 bUpdateRequired = true;
91 }
92
93
94 template<typename TDataType>
95 void FixedPoints<TDataType>::removeFixedPoint(int id)
96 {
97 auto it = m_fixedPts.begin();
98 while (it != m_fixedPts.end())
99 {
100 if (it->first == id)
101 {
102 m_fixedPts.erase(it++);
103 }
104 else
105 it++;
106 }
107
108 bUpdateRequired = true;
109 }
110
111
112 template<typename TDataType>
113 void FixedPoints<TDataType>::clear()
114 {
115 m_fixedPts.clear();
116
117 bUpdateRequired = true;
118 }
119
120 template <typename Coord>
121 __global__ void K_DoFixPoints(
122 DArray<Coord> curPos,
123 DArray<Coord> curVel,
124 DArray<int> bFixed,
125 DArray<Coord> fixedPts)
126 {
127 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
128 if (pId >= curPos.size()) return;
129
130 if (bFixed[pId])
131 {
132 curPos[pId] = fixedPts[pId];
133 curVel[pId] = Coord(0);
134 }
135
136 }
137
138 template<typename TDataType>
139 void FixedPoints<TDataType>::constrain()
140 {
141 //printf("fixed points!!!!!! %d\n", m_fixedPts.size());
142
143 if (m_fixedPts.size() <= 0)
144 return;
145
146 if (bUpdateRequired)
147 {
148 updateContext();
149 bUpdateRequired = false;
150 }
151
152
153 uint pDims = cudaGridSize(m_bFixed.size(), BLOCK_SIZE);
154
155 K_DoFixPoints<Coord> << < pDims, BLOCK_SIZE >> > (this->inPosition()->getData(), this->inVelocity()->getData(), m_bFixed, m_fixed_positions);
156 }
157
158
159 template <typename Coord>
160 __global__ void K_DoPlaneConstrain(
161 DArray<Coord> curPos,
162 Coord origin,
163 Coord dir)
164 {
165 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
166 if (pId >= curPos.size()) return;
167
168 float tmp = dir.dot(curPos[pId] - origin);
169 if (tmp < 0)
170 {
171 curPos[pId] -= tmp*dir;
172 }
173 }
174
175 template<typename TDataType>
176 void FixedPoints<TDataType>::constrainPositionToPlane(Coord pos, Coord dir)
177 {
178 uint pDims = cudaGridSize(m_bFixed.size(), BLOCK_SIZE);
179
180 K_DoPlaneConstrain<< < pDims, BLOCK_SIZE >> > (this->inPosition()->getData(), pos, dir);
181 }
182
183 DEFINE_CLASS(FixedPoints);
184}