1#include <cuda_runtime.h>
4#include "FixedPoints.h"
8 IMPLEMENT_TCLASS(FixedPoints, TDataType)
10 template<typename TDataType>
11 FixedPoints<TDataType>::FixedPoints()
16 template<typename TDataType>
17 FixedPoints<TDataType>::~FixedPoints()
20 m_fixed_positions.clear();
24 template<typename TDataType>
25 bool FixedPoints<TDataType>::initializeImpl()
27 printf("initialized!!!!!! %d %d\n", FixedIds.size(), this->inPosition()->size());
28 if (this->inPosition()->isEmpty() || this->inVelocity()->isEmpty())
30 std::cout << "Exception: " << std::string("FixedPoints's fields are not fully initialized!") << "\n";
34 CArray<int> hostFixedIds;
35 CArray<Coord> hostFixedPos;
37 hostFixedIds.resize(FixedIds.size());
38 hostFixedPos.resize(FixedPos.size());
40 hostFixedIds.assign(FixedIds.getData());
41 hostFixedPos.assign(FixedPos.getData());
43 for (int i = 0; i < hostFixedIds.size(); i++)
45 addFixedPoint(hostFixedIds[i], hostFixedPos[i]);
54 template<typename TDataType>
55 void FixedPoints<TDataType>::updateContext()
57 int totalNum = this->inPosition()->getData().size();
58 if (m_bFixed.size() != totalNum)
60 m_bFixed_host.resize(totalNum);
61 m_fixed_positions_host.resize(totalNum);
63 m_bFixed.resize(totalNum);
64 m_fixed_positions.resize(totalNum);
67 for (int i = 0; i < m_bFixed_host.size(); i++)
72 for (auto it = m_fixedPts.begin(); it != m_fixedPts.end(); it++)
74 if (it->first >= 0 && it->first < totalNum)
76 m_bFixed_host[it->first] = 1;
77 m_fixed_positions_host[it->first] = it->second;
81 m_bFixed.assign(m_bFixed_host);
82 m_fixed_positions.assign(m_fixed_positions_host);
85 template<typename TDataType>
86 void FixedPoints<TDataType>::addFixedPoint(int id, Coord pt)
90 bUpdateRequired = true;
94 template<typename TDataType>
95 void FixedPoints<TDataType>::removeFixedPoint(int id)
97 auto it = m_fixedPts.begin();
98 while (it != m_fixedPts.end())
102 m_fixedPts.erase(it++);
108 bUpdateRequired = true;
112 template<typename TDataType>
113 void FixedPoints<TDataType>::clear()
117 bUpdateRequired = true;
120 template <typename Coord>
121 __global__ void K_DoFixPoints(
122 DArray<Coord> curPos,
123 DArray<Coord> curVel,
125 DArray<Coord> fixedPts)
127 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
128 if (pId >= curPos.size()) return;
132 curPos[pId] = fixedPts[pId];
133 curVel[pId] = Coord(0);
138 template<typename TDataType>
139 void FixedPoints<TDataType>::constrain()
141 //printf("fixed points!!!!!! %d\n", m_fixedPts.size());
143 if (m_fixedPts.size() <= 0)
149 bUpdateRequired = false;
153 uint pDims = cudaGridSize(m_bFixed.size(), BLOCK_SIZE);
155 K_DoFixPoints<Coord> << < pDims, BLOCK_SIZE >> > (this->inPosition()->getData(), this->inVelocity()->getData(), m_bFixed, m_fixed_positions);
159 template <typename Coord>
160 __global__ void K_DoPlaneConstrain(
161 DArray<Coord> curPos,
165 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
166 if (pId >= curPos.size()) return;
168 float tmp = dir.dot(curPos[pId] - origin);
171 curPos[pId] -= tmp*dir;
175 template<typename TDataType>
176 void FixedPoints<TDataType>::constrainPositionToPlane(Coord pos, Coord dir)
178 uint pDims = cudaGridSize(m_bFixed.size(), BLOCK_SIZE);
180 K_DoPlaneConstrain<< < pDims, BLOCK_SIZE >> > (this->inPosition()->getData(), pos, dir);
183 DEFINE_CLASS(FixedPoints);