int binIndex = getBinIndexForPosition(position_);
ArrayList<CRBaseSituatedModel> returnArrayList = new ArrayList<CRBaseSituatedModel>();
returnArrayList.addAll(situatedModelBins[binIndex]);
//-- test if other bin indexes are applicable
CRVector3d binCoordinates = getBinCoordinatesForPosition(position_);
boolean addedNorth = false;
boolean addedSouth = false;
boolean addedWest = false;
boolean addedEast = false;
int northBinY = (int)(binCoordinates.y - 1);
int southBinY = (int)(binCoordinates.y + 1);
int westBinX = (int)(binCoordinates.x - 1);
int eastBinX = (int)(binCoordinates.x + 1);
int numRows = (int)Math.ceil(size.y / binSize.y);
int numCols = (int)Math.ceil(size.x / binSize.x);
if (position_.y - radius_ <= binCoordinates.y*binSize.y) { // test if reaching to the bin north of here
addedNorth = true;
//-- wrap the coordinate around
if (northBinY < 0 ) {
northBinY = numRows-1;
}
//-- add the bin to the list
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(binCoordinates.x, northBinY, 0))]);
}
if (position_.y + radius_ >= southBinY*binSize.y || position_.y + radius_ >= size.y) { //test bin south of here if north not added
addedSouth = true;
//-- wrap the coordinate around
if (southBinY >= numRows) {
southBinY = 0;
}
//-- add the bin to the list
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(binCoordinates.x, southBinY, 0))]);
}
if (position_.x - radius_ <= binCoordinates.x*binSize.x) { // test bin west of here
addedWest = true;
//-- wrap the coordinate around
if (westBinX < 0 ) {
westBinX = numCols-1;
}
//-- add the bin to the list
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(westBinX, binCoordinates.y, 0))]);
}
if (position_.x + radius_ >= eastBinX*binSize.x || position_.x + radius_ >= size.x) { // test bin east of here
addedEast = true;
//-- wrap the coordinate around
if (eastBinX >= numCols) {
eastBinX = 0;
}
//-- add the bin to the list
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(eastBinX, binCoordinates.y, 0))]);
}
//-- if added north and west, north west should also be added, etc.
if (addedNorth && addedWest) {
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(westBinX, northBinY, 0))]);
}
if (addedNorth && addedEast) {
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(eastBinX, northBinY, 0))]);
}
if (addedSouth && addedWest) {
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(westBinX, southBinY, 0))]);
}
if (addedSouth && addedEast) {
returnArrayList.addAll(situatedModelBins[getBinIndexForBinCoordinates(new CRVector3d(eastBinX, southBinY, 0))]);
}
return returnArrayList;
}