1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/////////////////////////////////////////////////////////////
// //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC) //
// http://www.uq.edu.au/esscc //
// //
// Primary Business: Brisbane, Queensland, Australia //
// Licensed under the Open Software License version 3.0 //
// http://www.opensource.org/licenses/osl-3.0.php //
// //
/////////////////////////////////////////////////////////////
#include "Geometry/SimpleNTable.h"
//------ ASimpleNTable member functions ----
ASimpleNTable::ASimpleNTable()
: m_data(NULL),
m_p0(Vec3::ZERO),
m_dim(0.0),
m_numInsertedParticles(0)
{
}
ASimpleNTable::~ASimpleNTable()
{
if (m_data != NULL) delete [] m_data;
}
/*!
get all particles near a given position
\param pos the position
*/
const vector<SimpleParticle>* ASimpleNTable::getNeighbors(const Vec3& pos) const
{
return &(m_data[index(pos)]);
}
/*!
Add particle to all neighorlists it belongs to
\param cbp the particle
*/
void ASimpleNTable::insertParticle(SimpleParticle cbp)
{
//cout << "ASimpleNTable::insertParticle(" << cbp << ")" << endl;
const vector<int> idx=allidx(cbp.getPos());
if (idx.size() > 0) {
m_numInsertedParticles++;
}
for(vector<int>::const_iterator citer=idx.begin();citer!=idx.end();citer++){
m_data[*citer].push_back(cbp);
}
insertParticleCircular(cbp);
}
int ASimpleNTable::getNumInsertedParticles() const
{
return m_numInsertedParticles;
}
/*!
get particle closest to given position
\param pos the position
\warning doesn't check if position is in space
*/
int ASimpleNTable::getClosestParticleID(const Vec3& pos) const
{
vector<SimpleParticle>* parts=&(m_data[index(pos)]);
int id=-1;
double dist=m_dim;
for(vector<SimpleParticle>::iterator iter=parts->begin();
iter!=parts->end();
iter++){
double ndist=(pos-iter->getPos()).norm();
if(ndist<dist){
dist=ndist;
id=iter->getID();
}
}
return id;
}
//------ CSimple2DNTable member functions ----
/*!
Constructor
\param pos position of the (xmin,ymin) point
\param dim size of the space
\param r grid spacing
*/
CSimple2DNTable::CSimple2DNTable(const Vec3& pos,const Vec3& dim,double r,bool xcirc,bool ycirc)
{
//cout << "CSimple2DNTable(" << pos << " , " << dim << " , " << r << ")" << endl;
m_xsize=int(ceil(dim.X()/r));
m_ysize=int(ceil(dim.Y()/r));
m_xcirc=xcirc;
m_ycirc=ycirc;
m_p0=pos;
m_dim=r;
// pad in circular directions
if(m_xcirc) {
m_xsize+=2;
m_p0-=Vec3(r,0.0,0.0);
m_xshift=Vec3(dim.X(),0.0,0.0);
}
if(m_ycirc) {
m_ysize+=2;
m_p0-=Vec3(0.0,r,0.0);
m_yshift=Vec3(0.0,dim.Y(),0.0);
}
//cout << "NT size : " << m_xsize << " , " << m_ysize << endl;
m_data=new vector<SimpleParticle>[m_xsize*m_ysize];
}
/*!
insert circular images of the particle
\param cbp the particle
*/
void CSimple2DNTable::insertParticleCircular(SimpleParticle cbp)
{
if(m_xcirc){
if (int((cbp.getPos().X()-m_p0.X())/m_dim)==1) {
cbp.moveTo(cbp.getPos()+m_xshift);
const vector<int> idx=allidx(cbp.getPos());
for(vector<int>::const_iterator citer=idx.begin();citer!=idx.end();citer++){
m_data[*citer].push_back(cbp);
}
} else if (int((cbp.getPos().X()-m_p0.X())/m_dim)==m_xsize-2) {
cbp.moveTo(cbp.getPos()-m_xshift);
const vector<int> idx=allidx(cbp.getPos());
for(vector<int>::const_iterator citer=idx.begin();citer!=idx.end();citer++){
m_data[*citer].push_back(cbp);
}
}
}
}
/*!
Return the grid index of a position.
\param pos the position
\warning does not check if pos is within space
*/
int CSimple2DNTable::index(const Vec3& pos) const
{
int ix=int((pos.X()-m_p0.X())/m_dim);
int iy=int((pos.Y()-m_p0.Y())/m_dim);
int idx=ix+m_xsize*iy;
return idx;
}
/*!
Get all indices to which a particle at a given position will be added.
\param pos the position
\warning does not check if pos is within space
*/
vector<int> CSimple2DNTable::allidx(const Vec3& pos) const
{
vector<int> res;
int ix=int((pos.X()-m_p0.X())/m_dim);
int iy=int((pos.Y()-m_p0.Y())/m_dim);
int idx=ix+m_xsize*iy;
res.push_back(idx);
if(ix>0){
res.push_back(idx-1);
if(iy>0){
res.push_back(idx-m_xsize-1);
}
if(iy<m_ysize-1){
res.push_back(idx+m_xsize-1);
}
}
if(ix<m_xsize-1){
res.push_back(idx+1);
if(iy>0){
res.push_back(idx-m_xsize+1);
}
if(iy<m_ysize-1){
res.push_back(idx+m_xsize+1);
}
}
if(iy>0){
res.push_back(idx-m_xsize);
}
if(iy<m_ysize-1){
res.push_back(idx+m_xsize);
}
return res;
}
/*!
Put all interactions into a set
\param iset the set into which to put them
*/
void CSimple2DNTable::getInteractions(set<BasicInteraction,BILess>& iset,double dmax)
{
for(int i=0;i<m_xsize;i++){
for(int j=0;j<m_ysize;j++){
int idx=i+m_xsize*j;
//cout << "-- " << i << " , " << j << " , " << idx << endl;
if(m_data[idx].size()>=2){
for(vector<SimpleParticle>::iterator iter=m_data[idx].begin();
iter!=m_data[idx].end()-1;
iter++){
for(vector<SimpleParticle>::iterator iter2=iter+1;
iter2!=m_data[idx].end();
iter2++){
// cout << "Pair: " << iter->getID() << " - " << iter2 ->getID() << endl;
if((iter->getPos()-iter2->getPos()).norm()<dmax*(iter->getRad()+iter2->getRad())){
iset.insert(BasicInteraction(iter->getID(),iter2 ->getID()));
}
}
}
}
}
}
}
// debugging only
void CSimple2DNTable::print()
{
for(int i=0;i<m_xsize;i++){
for(int j=0;j<m_ysize;j++){
int idx=i+m_xsize*j;
cout << "-- " << i << " , " << j << " , " << idx << endl;
for(
vector<SimpleParticle>::iterator iter=m_data[idx].begin();
iter!=m_data[idx].end();
iter++)
{
cout << iter->getPos() << " , " << iter->getRad() << endl;
}
}
}
}
|