Neighbors Board::getNeighbors(int pos) { // row is current position, elements are its neighbors, at most 4, -1 means null static const int rawData[][4] = { {1,3,8,-1}, {0,2,4,-1}, {1,5,13,-1}, {0,4,6,9}, {1,3,5,-1}, {2,4,7,12}, {3,7,10,-1}, {5,6,11,-1}, {0,9,20,-1}, {3,8,10,17}, {6,9,14,-1}, {7,12,16,-1}, {5,11,13,19}, {2,12,22,-1}, {10,15,17,-1}, {14,16,18,-1}, {11,15,19,-1}, {9,14,18,20}, {15,17,19,21}, {12,16,18,22}, {8,17,21,-1}, {18,20,22,-1}, {13,19,21,-1} }; Neighbors result; for(int j=0; j<4; ++j) if(rawData[pos][j] > -1) result.push_back(rawData[pos][j]); return result; }
Neighbors find_neighbors_bruteforce_impl(const RandomAccessIterator& begin, const RandomAccessIterator& end, Callback callback, IndexType k) { timed_context context("Distance sorting based neighbors search"); typedef std::pair<RandomAccessIterator, ScalarType> DistanceRecord; typedef std::vector<DistanceRecord> Distances; Neighbors neighbors; neighbors.reserve(end-begin); for (RandomAccessIterator iter=begin; iter!=end; ++iter) { Distances distances; for (RandomAccessIterator around_iter=begin; around_iter!=end; ++around_iter) distances.push_back(std::make_pair(around_iter, callback.distance(iter,around_iter))); std::nth_element(distances.begin(),distances.begin()+k+1,distances.end(), distances_comparator<DistanceRecord>()); LocalNeighbors local_neighbors; local_neighbors.reserve(k); for (typename Distances::const_iterator neighbors_iter=distances.begin(); neighbors_iter!=distances.begin()+k+1; ++neighbors_iter) { if (neighbors_iter->first != iter) local_neighbors.push_back(neighbors_iter->first - begin); } neighbors.push_back(local_neighbors); } return neighbors; }
DBSCAN::Neighbors DBSCAN::find_neighbors( const DBSCAN::DistanceMatrix& D, uint32_t pid ) { Neighbors ne; for ( uint32_t j = 0; j < D.size1(); ++j ) { if ( D( pid, j ) <= m_eps ) { ne.push_back( j ); } } return ne; }
void Surface3D::addLayer(const AllNeighbors& totalNeighbors, int sX, int sY, int sZ) { Surface2D surfaceXY; surfaceXY.reserve(sY); for (int y = 0; y < sY; ++y) { Surface1D surfaceX; surfaceX.reserve(sX); for (int x = 0; x < sX; ++x) { Atoms atoms; atoms.reserve(totalNeighbors.size()); for (auto const& neighbors : totalNeighbors) { Neighbors neighbs; // First neighbors count char numberNeighbs = 0; for (int nb = 0; nb < 4; ++nb) { if (x + neighbors[nb].x >= 0 && y + neighbors[nb].y >= 0 && x + neighbors[nb].x < sX && y + neighbors[nb].y < sY) { ++numberNeighbs; AtomType neighb = {x + neighbors[nb].x, y + neighbors[nb].y, sZ + neighbors[nb].z, neighbors[nb].type, false}; neighbs.push_back(neighb); } } AtomInfo atom; atom.neighbors = neighbs; atom.firstNeighborsCount = numberNeighbs; atom.deleted = numberNeighbs == 0; atoms.push_back(atom); } surfaceX.push_back(Cell(atoms)); } surfaceXY.push_back(surfaceX); } push_back(surfaceXY); }
Neighbors find_neighbors_vptree_impl(const RandomAccessIterator& begin, const RandomAccessIterator& end, Callback callback, IndexType k) { timed_context context("VP-Tree based neighbors search"); Neighbors neighbors; neighbors.reserve(end-begin); VantagePointTree<RandomAccessIterator,Callback> tree(begin,end,callback); for (RandomAccessIterator i=begin; i!=end; ++i) { LocalNeighbors local_neighbors = tree.search(i,k+1); std::remove(local_neighbors.begin(),local_neighbors.end(),i-begin); neighbors.push_back(local_neighbors); } return neighbors; }
void DBSCAN::dbscan( const DBSCAN::DistanceMatrix& dm ) { std::vector< uint8_t > visited( dm.size1() ); uint32_t cluster_id = 0; for ( uint32_t pid = 0; pid < dm.size1(); ++pid ) { if ( !visited[pid] ) { visited[pid] = 1; Neighbors ne = find_neighbors( dm, pid ); if ( ne.size() >= m_min_elems ) { m_labels[pid] = cluster_id; for ( uint32_t i = 0; i < ne.size(); ++i ) { uint32_t nPid = ne[i]; if ( !visited[nPid] ) { visited[nPid] = 1; Neighbors ne1 = find_neighbors( dm, nPid ); if ( ne1.size() >= m_min_elems ) { for ( const auto& n1 : ne1 ) { ne.push_back( n1 ); } } } if ( m_labels[nPid] == -1 ) { m_labels[nPid] = cluster_id; } } ++cluster_id; } } } }
int main(int argc, char *argv[]) { std::string input; int width; int height; int row = 0; int attempt = 0; while(std::getline(std::cin, input)) { char c = input.at(0); if(c == '.' || c == 'X' || c == '*') { ++row; neighbors.clear(); for(int pos = 0; pos < (int) input.length(); ++pos) { VertexWeight vertexWeight; vertexWeight.first = pos; char temp = input.at(pos); if(temp == '.') { vertexWeight.second = 1; } else if(temp == '*') { vertexWeight.second = 2; } else { vertexWeight.second = 3; } neighbors.push_back(vertexWeight); } adjList.push_back(neighbors); if(height == row) { visited.clear(); for(int index = 0; index < height; ++index) { visited.push_back(std::vector<bool>(width, false)); } for(int i = 0; i < height; ++i) { for(int j = 0; j < width; ++j) { if((visited[i])[j] == false && ((adjList[i])[j]).second != 1) { area = 0; floodFill(i, j, ((adjList[i])[j]).second); if(area) areas.push_back(area); } } } std::cout << "Throw " << ++attempt << std::endl; std::sort(areas.begin(), areas.end()); int numAreas = (int)areas.size() - 1; for(std::vector<int>::iterator it = areas.begin(); it != areas.end(); ++it) { std::cout << *it; if(numAreas--) { std::cout << " "; } } std::cout << std::endl; std::cout << std::endl; } } else { row = 0; std::stringstream sbuf; sbuf << input; sbuf >> width >> height; adjList.clear(); areas.clear(); if(width == 0 && height == 0) { break; } } } return 0; }
void newDocument() { mask.clear(); etchingAction->setEnabled(true); maskAction->setEnabled(true); saveAct->setEnabled(true); QTime t; z_min = 0; int const xMax = SIZE_X; int const yMax = SIZE_Y; int const zMax = SIZE_Z; z_center = z_min + (zMax - 2 - z_min) / 2; cell = Cell(h, k, l); Xsize = cell.getXSize(); Ysize = cell.getYSize(); Zsize = cell.getZSize(); Vx = cell.getVx(); Vy = cell.getVy(); Vz = cell.getVz(); cell.optimize(); auto const numberOfAtomInCell = static_cast<unsigned int>(cell.size()); neighbors = cell.findNeighbors(Xsize, Ysize, Zsize); surfaceXYZ.reset(new Surface3D(cell)); surfaceXYZ->clear(); surfaceXYZ->reserve(SIZE_Z); for (int z = 0; z < SIZE_Z; ++z) { Surface2D surfaceXY; surfaceXY.reserve(SIZE_Y); for (int y = 0; y < SIZE_Y; ++y) { Surface1D surfaceX; surfaceX.reserve(SIZE_X); for (int x = 0; x < SIZE_X; ++x) { Cell cell; for (unsigned char a = 0; a < numberOfAtomInCell; ++a) { Neighbors neighbs; char numberNeighbs = 0; // number of the first neighbors for (int nb = 0; nb < 4; ++nb) { auto& neighborsANb = neighbors[a][nb]; if (x + neighborsANb.x >= 0 && y + neighborsANb.y >= 0 && z + neighborsANb.z >= 0 && x + neighborsANb.x < xMax && y + neighborsANb.y < yMax && z + neighborsANb.z < zMax + 1) { ++numberNeighbs; AtomType neighb = {x + neighborsANb.x, y + neighborsANb.y, z + neighborsANb.z, neighborsANb.type, false}; neighbs.push_back(neighb); } } AtomInfo atom; atom.neighbors = neighbs; atom.firstNeighborsCount = numberNeighbs; atom.deleted = numberNeighbs == 0; // TODO: atom.type = cell.addAtom(atom); } surfaceX.push_back(cell); } surfaceXY.push_back(surfaceX); } surfaceXYZ->push_back(surfaceXY); } surfaceXYZ->rebuildSurfaceAtoms(); drawResult(); }
void ClustererDBSCAN::run_cluster(Points samples) { ClusterId cid = 1; // foreach pid for (PointId pid = 0; pid < samples.size(); pid++) { // not already visited if (!_visited[pid]){ _visited[pid] = true; // get the neighbors Neighbors ne = findNeighbors(pid, _eps); // not enough support -> mark as noise if (ne.size() < _minPts) { _noise[pid] = true; } else { //else it's a core point _core[pid] = true; // Add p to current cluster CCluster c; // a new cluster c.push_back(pid); // assign pid to cluster _pointId_to_clusterId[pid]=cid; // go to neighbors for (unsigned int i = 0; i < ne.size(); i++) { PointId nPid = ne[i]; // not already visited if (!_visited[nPid]) { _visited[nPid] = true; // go to neighbors Neighbors ne1 = findNeighbors(nPid, _eps); // enough support if (ne1.size() >= _minPts) { _core[nPid] = true; // join BOOST_FOREACH(Neighbors::value_type n1, ne1) { // join neighbord ne.push_back(n1); } } } // not already assigned to a cluster if (!_pointId_to_clusterId[nPid]) { // add it to the current cluster c.push_back(nPid); _pointId_to_clusterId[nPid]=cid; } }