void SkinCluster::Cluster(){ int cId = 1; for (int i=0;i<mSize;i++){ if(!mVisited[i]){ mVisited[i]= true; std::vector<int> neighbors; GetNeighbors(i,neighbors); if (neighbors.size() < mMinPts){ mNoise[i] = true; }else{ std::vector<int> cluster; cluster.push_back(i); mClusterId[i] = cId; for (int j=0; j < int(neighbors.size()); j++){ int cj = neighbors[j]; if (!mVisited[cj]){ mVisited[cj] = true; std::vector<int> jneighbors; GetNeighbors(cj, jneighbors); if (jneighbors.size() >= mMinPts){ for (int k=0; k < int(jneighbors.size()); k++){ neighbors.push_back(jneighbors[k]); } } } if(mActive[cj]){ if (mClusterId[cj]==0){ cluster.push_back(cj); mClusterId[cj] = cId; } } } mClusters.push_back(cluster); cId++; } } } /* cout << "NbClusters: "<<mClusters.size()<<endl; for(int i=0;i<mClusters.size();i++){ cout << "Cluster: ("<<i<<") "; for(int j=0;j<mClusters[i].size();j++){ cout <<mClusters[i][j]<<" "; } cout << endl; } */ }
void ParticleSystem::computePressure(){ float h2 = h*h; for(int i = 0; i < NumParticles; i++){ std::vector<Particle *> neighbors = GetNeighbors(Particles[i]); Vector3 pi = Particles[i]->getPosition(); Vector3 vi = Particles[i]->getVelocity(); float pressure_i = Particles[i]->getPressure(); Vector3 sumPressure = Vector3(0, 0, 0); Vector3 sumViscosity = Vector3(0, 0, 0); for(int j = 0; j < neighbors.size(); j++){ Vector3 pj = neighbors[j]->getPosition(); Vector3 vj = neighbors[j]->getVelocity(); float pressure_j = neighbors[j]->getPressure(); float density_j = neighbors[j]->getDensity(); Vector3 diff = pi - pj; float rad = diff.Mag(); float rad2 = rad * rad; if(h2 > rad2 && EPSILON <= rad2){ float sqr = h - rad; sumPressure += diff/rad * ((pressure_i+pressure_j)/(2*density_j)) * sqr * sqr; sumViscosity += (vj - vi)/density_j * sqr; } } Particles[i]->addForce(sumPressure * (float(particle_mass) * mSpikyKernel * -1.0f)); Particles[i]->addForce(sumViscosity * float(fluid_viscosity*particle_mass)*mViscosityKernel); } }
void ParticleSystem::computeDenisty(){ float h2 = h*h; for(int i = 0; i < NumParticles; i++){ std::vector<Particle *> neighbors = GetNeighbors(Particles[i]); Vector3 pi = Particles[i]->getPosition(); float sum = 0; Particles[i]->setDensity(0); Particles[i]->setPressure(0); float density = 0; float pressure = 0; for(int j = 0; j < neighbors.size(); j++){ Vector3 pj = neighbors[j]->getPosition(); float rad = (pj-pi).Mag(); float rad2 = rad * rad; if(h2 > rad2 && EPSILON <= rad2){ float cubic = h2 - rad2; float d = cubic * cubic * cubic; sum += d; } } density += (sum * mPolyKernel * particle_mass); pressure += k * (density-restDensity); Particles[i]->setDensity(density); Particles[i]->setPressure(pressure); } }
int GetNeighbors( int u, int dummyU, struct _pair neighbors[], int maxN ) { int p = contigGraph[u].next ; int cnt = 0 ; while ( p != -1 ) { if ( contigGraph[p].dummyU == dummyU ) { if ( cnt >= maxN ) { std::vector<struct _pair> neighbors ; int ncnt = GetNeighbors( u, dummyU, neighbors ) ; fprintf( stderr, "(%d %d) has too many neighbors. %d>=%d: ", u, dummyU, ncnt, maxN ) ; for ( int i = 0 ; i < ncnt ; ++i ) { fprintf( stderr, " (%"PRId64" %"PRId64")", neighbors[i].a, neighbors[i].b ) ; } fprintf( stderr, "\n") ; exit( 1 ) ; } struct _pair np ; np.a = contigGraph[p].v ; np.b = contigGraph[p].dummyV ; neighbors[cnt] = np ; ++cnt ; } p = contigGraph[p].next ; } return cnt ; }
void RACScore::SetLinkedAttribute( vector<double>& vecAttributes ) { /* edge iterator */ EdgeIter ep, ep_end; BglVertex u,v; BglVertexMap indices = get( vertex_index , *m_ptrGraph); vector<int> lhsNeighbor; int idx = 0; double maxNeighbors = 0.0; for (tie(ep,ep_end) = edges(*m_ptrGraph); ep != ep_end; ++ep) { u = source(*ep,*m_ptrGraph); v = target(*ep,*m_ptrGraph); lhsNeighbor.clear(); GetNeighbors(u,lhsNeighbor); #ifdef DEBUG cout << lhsNeighbor.size() <<' ' <<GetNumCommNeighbors(v,lhsNeighbor) << endl; #endif vecAttributes[idx] = (double)GetNumCommNeighbors(v,lhsNeighbor); if (vecAttributes[idx] > maxNeighbors) { maxNeighbors = vecAttributes[idx]; } ++idx; } m_maxCommNeighbors = maxNeighbors; }
void RACScore::SetUnLinkedAttribute( vector<double>& vecAttributes ) { VertexIter vp, vp_end, ivp, ivp_end; OutEdgeIter ep, ep_end; vector<int> lhsNeighbors; int counter = 0; bool exitLoop = false; for (tie(vp, vp_end) = vertices(*m_ptrGraph); vp != vp_end; ++vp) { if(exitLoop) break; for (tie(ivp, ivp_end) = vertices(*m_ptrGraph); ivp != ivp_end; ++ivp) { if (*vp == *ivp ) continue; if (counter == m_numEdge) { exitLoop = true; break; } if (!edge(*vp,*ivp,*m_ptrGraph).second) { lhsNeighbors.clear(); GetNeighbors(*vp,lhsNeighbors); #ifdef DEBUG cout << GetNumCommNeighbors(*ivp,lhsNeighbors) << endl; #endif vecAttributes[m_numEdge+counter] = (double)GetNumCommNeighbors(*ivp,lhsNeighbors); ++counter; } } } }
void AddEdge( int u, int dummyU, int v, int dummyV, bool checkDuplicate = false ) { if ( checkDuplicate ) { std::vector<struct _pair> neighbors ; int ncnt = GetNeighbors( u, dummyU, neighbors ) ; for ( int i = 0 ; i < ncnt ; ++i ) { if ( neighbors[i].a == v && neighbors[i].b == dummyV ) return ; } } contigGraph[ edgeUsed ].u = u ; contigGraph[ edgeUsed ].v = v ; contigGraph[ edgeUsed ].dummyU = dummyU ; contigGraph[ edgeUsed ].dummyV = dummyV ; contigGraph[ edgeUsed ].next = contigGraph[u].next ; contigGraph[u].next = edgeUsed ; ++edgeUsed ; contigGraph[edgeUsed].u = v ; contigGraph[edgeUsed].v = u ; contigGraph[edgeUsed].dummyU = dummyV ; contigGraph[edgeUsed].dummyV = dummyU ; contigGraph[ edgeUsed ].next = contigGraph[v].next ; contigGraph[v].next = edgeUsed ; ++edgeUsed ; if ( edgeUsed >= n + 2 * m + 4 ) { fprintf( stderr, "Too many edges.\n" ) ; exit( 1 ) ; } }
void Chunk::Unload() { m_state = ChunkState::Unloading; if (m_loaded) { m_loaded = false; std::vector<Chunk*> neighbors = GetNeighbors(); for (Chunk* neighbor : neighbors) { if (neighbor->IsLoaded()) { neighbor->UpdateSurroundedFlag(); } } } ClearMesh(); ClearMeshCache(); }
void CommomFriendsAttribute::GetProblemAttriByEdge(vector<double>& vecAttributes , const vector<BglVertexPair>& vecPairVertex) { BglVertex u,v; vector<int> lhsNeighbor; int idx = 0; double maxNumCommNeighbors = (double)GetMaxNumCommNeghbors(); for (int i = 0; i < vecPairVertex.size(); i++) { u = vecPairVertex[i].first; v = vecPairVertex[i].second; lhsNeighbor.clear(); GetNeighbors(u,lhsNeighbor); vecAttributes[idx] = (double)GetNumCommNeighbors(v,lhsNeighbor) / maxNumCommNeighbors; ++idx; } /* BglVertex u,v; vector<int> lhsNeighbor; vector<int> vecCommNeighbors; int idx = 0; int level = 1; double maxRAScore = 1.0; for (int i = 0; i < vecPairVertex.size(); i++) { u = vecPairVertex[i].first; v = vecPairVertex[i].second; double uOutDegree = out_degree(u,*m_ptrGraph); if (uOutDegree < 1) uOutDegree = 1; double vOutDegree = out_degree(v,*m_ptrGraph); if (vOutDegree < 1) vOutDegree = 1; double numCommNeighbor_2 = (double)GetMultiLevelCommNeighbors(u,v, vecCommNeighbors, level); // double value = GetSumInvDegree(vecCommNeighbors); vecAttributes[idx] = numCommNeighbor_2; #ifdef DEBUG cout << "u: " << out_degree(u,*m_ptrGraph) << " v: " << out_degree(v,*m_ptrGraph) << ' ' << endl; #endif ++idx; }*/ }
void RACScore::GetProblemAttriByEdge(vector<double>& vecAttributes , const vector<BglVertexPair>& vecPairVertex) { BglVertex u,v; vector<int> lhsNeighbor; vector<int> vecCommNeighbors; int idx = 0; // double maxNumCommNeighbors = (double)GetMaxNumCommNeghbors(); int level = 2.0; //double maxRAScore = (double)GetMaxRAScore(level); double maxRAScore = 1.0; for (int i = 0; i < vecPairVertex.size(); i++) { u = vecPairVertex[i].first; v = vecPairVertex[i].second; double uOutDegree = out_degree(u,*m_ptrGraph); if (uOutDegree < 1) uOutDegree = 1; double vOutDegree = out_degree(v,*m_ptrGraph); if (vOutDegree < 1) vOutDegree = 1; lhsNeighbor.clear(); GetNeighbors(u,lhsNeighbor); //double numCommNeighbor_origin = (double)GetNumCommNeighbors(v,lhsNeighbor); vecCommNeighbors.clear(); double numCommNeighbor_2 = (double)GetMultiLevelCommNeighbors(u,v, vecCommNeighbors, level); double value = GetSumInvDegree(vecCommNeighbors); //vecAttributes[idx] = numCommNeighbor/pow(uOutDegree*vOutDegree,0.5); //vecAttributes[idx] = numCommNeighbor_2/maxNumCommNeighbors; vecAttributes[idx] = value/maxRAScore; #ifdef DEBUG for (int i = 0; i < vecCommNeighbors.size(); i++) { cout << vecCommNeighbors[i] << ' '; } cout << endl; cout << "u: " << out_degree(u,*m_ptrGraph) << " v: " << out_degree(v,*m_ptrGraph) << ' ' << GetNumCommNeighbors(v,lhsNeighbor) << ' ' << vecAttributes[idx] << endl; #endif ++idx; } }
float TwoCopsDijkstra2::compute_target_value( Position &pos ) { float result = 0.; float temp; NeighborSet neighbors; // get the possible positions the robber could move to GetNeighbors( pos, false, neighbors ); //nodesExpanded++; for( NeighborSet::iterator it = neighbors.begin(); it != neighbors.end(); it++ ) { //nodesTouched++; temp = min_cost[(*it).first]; if( temp == FLT_MAX ) return FLT_MAX; temp += (*it).second; result = max( result, temp ); } return result; };
int RACScore::GetMaxNumCommNeghborsByEdge(const vector<BglVertexPair>& vecPairVertex) { /* edge iterator */ EdgeIter ep, ep_end; BglVertex u,v; BglVertexMap indices = get( vertex_index , *m_ptrGraph); vector<int> lhsNeighbor; int idx = 0; double maxNeighbors = 0.0; for (int i = 0; i < vecPairVertex.size(); i++) { u = vecPairVertex[i].first; v = vecPairVertex[i].second; lhsNeighbor.clear(); GetNeighbors(u,lhsNeighbor); double tmp = (double)GetNumCommNeighbors(v,lhsNeighbor); if (tmp > maxNeighbors) { maxNeighbors = tmp; } ++idx; } return maxNeighbors; }
int RACScore::GetMaxNumCommNeghbors() { /* edge iterator */ EdgeIter ep, ep_end; BglVertex u,v; BglVertexMap indices = get( vertex_index , *m_ptrGraph); vector<int> lhsNeighbor; int idx = 0; double maxNeighbors = 0.0; for (tie(ep,ep_end) = edges(*m_ptrGraph); ep != ep_end; ++ep) { u = source(*ep,*m_ptrGraph); v = target(*ep,*m_ptrGraph); lhsNeighbor.clear(); GetNeighbors(u,lhsNeighbor); double tmp = (double)GetNumCommNeighbors(v,lhsNeighbor); if (tmp > maxNeighbors) { maxNeighbors = tmp; } ++idx; } return maxNeighbors; }
void Chunk::RebuildMesh() { m_state = ChunkState::RebuildingMesh; CacheMesh(); CreateMesh(); UpdateWallFlags(); UpdateSurroundedFlag(); std::vector<Chunk*> neighbors = GetNeighbors(); for (Chunk* neighbor : neighbors) { if (neighbor->IsLoaded()) { neighbor->UpdateSurroundedFlag(); if (m_rebuildNeighbors) { neighbor->SetNeedsRebuild(true, false); } } } m_rebuildNeighbors = false; m_rebuild = false; m_state = ChunkState::RebuildComplete; }
BMP MazeGen::Generate() { // Create a vertex with coordinates within (1, 1) and (Width-1, Height-1), // and set it's visited member to true. Vertex current; current.x = (rand() % (Width / 2)) * 2 + 1; current.y = (rand() % (Height/ 2)) * 2 + 1; setVisited(current); // Push the starting cell to the stack stk.push(current); while(!AllCellsVisited()) { // Get all neighbors of the current cell std::vector<Vertex> neighbors = GetNeighbors(current); // Get a random unvisited neighbor of the current cell, or discover that there are no unvisited neighbors int randomNeighbor = rand() % neighbors.size(); // While neighbors[randomNeighbor] is a cell that has been visited already: while( getVisited(mazeArray[neighbors[randomNeighbor].x][neighbors[randomNeighbor].y]) == true) { // Remove that random neighbor from the vector of neighbors neighbors.erase(neighbors.begin() + randomNeighbor); // If there are no neighbors left, break the loop if(neighbors.empty()) break; // Pick a new random neighbor randomNeighbor = rand() % neighbors.size(); } // If there are no unvisited neighbors of the current cell if(neighbors.empty()) { // Go back a step, popping the last cell off the stack and setting it to the current cell, // then repeating the whole process current = stk.top(); stk.pop(); } // If there was an unvisited neighbor of of the current cell else { // Remove the 'wall' between the current cell and it's neighbor, drawing a line between the two cells RemoveWall(current, neighbors[randomNeighbor]); // Set the current vertex to the randomly selected neighbor current = neighbors[randomNeighbor]; // Mark it as visted setVisited(mazeArray[current.x][current.y]); // Push it to the stack stk.push(current); } } return maze; }
/*------------------------------------------------------------------------------ | Dijkstra implementation ------------------------------------------------------------------------------*/ void TwoCopsDijkstra2::dijkstra() { nodesExpanded = 0; nodesTouched = 0; QueueEntry qe; NeighborSet neighbors; NeighborSet::iterator it; // cop1 and cop2 can have maximally numnodes*(numnodes+1)/2 joint positions // robber can be on any vertex => numnodes min_cost.assign( numnodes * (numnodes + 1) / 2 * numnodes, FLT_MAX ); max_cost.assign( numnodes * (numnodes + 1) / 2 * numnodes, FLT_MAX ); push_end_states_on_queue(); while( !queue.empty() ) { // pop qe = queue.top(); queue.pop(); nodesTouched++; if( qe.minFirst ) { // it is the cop's turn in qe // if value isn't set yet if( min_cost[qe.pos] == FLT_MAX ) { min_cost[qe.pos] = qe.value; // find all the positions the robber could have come from GetNeighbors( qe.pos, false, neighbors ); nodesExpanded++; for( it = neighbors.begin(); it != neighbors.end(); it++ ) { nodesTouched++; qe.pos = (*it).first; if( max_cost[qe.pos] == FLT_MAX ) { qe.value = compute_target_value( qe.pos ); if( qe.value != FLT_MAX ) { qe.minFirst = false; queue.push( qe ); } } } // for all previous robber positions } } else { // it is the robber's turn in qe // if value isn't set yet if( max_cost[qe.pos] == FLT_MAX ) { max_cost[qe.pos] = qe.value; float backup_value = qe.value; // find all the positions the cops could have come from GetNeighbors( qe.pos, true, neighbors ); nodesExpanded++; for( it = neighbors.begin(); it != neighbors.end(); it++ ) { nodesTouched++; qe.pos = (*it).first; if( min_cost[qe.pos] == FLT_MAX ) { qe.minFirst = true; qe.value = backup_value + (*it).second; queue.push( qe ); } } } } // minFirst } // while loop return; };
/** * @function Search */ void Dijkstra3D::Search() { //-- Initialize your states for( int i = 0; i < mDimX; i++ ) { for( int j = 0; j < mDimY; j++ ) { for( int k = 0; k < mDimZ; k++ ) { mV[ GetRef(i,j,k) ].Init( i, j, k ) ; if( mGrid->getCell( i, j, k ) != 0 ) //-- If it is an obstacle { mV[ GetRef(i,j,k) ].color = 3; } //--Mark as Obstacle (3) } } } //-- Initialize the goal state int s = GetRef( mGoalX, mGoalY, mGoalZ ) ; mV[ s ].color = 1; // gray: In Open List mV[ s ].dist = 0; // start: 0 distance of itself //-- Insert to Q (unvisited nodes) InsertOpenSet( s ); //-- Iterate int u; int v; float new_dist; while( mQ.size() != 0 ) { u = PopOpenSet( ); //-- All neighbors std::vector< int > ng = GetNeighbors( u ); for( int i = 0; i < ng.size(); i++ ) { v = ng[i]; new_dist = ( GetEdgeCost( u, v ) + mV[ u ].dist ); if( new_dist < mV[ v ].dist ) { mV[ v ].dist = new_dist ; mV[ v ].parent = u; } if( mV[ v ].color == 2 ) //-- Nowhere { mV[ v ].color = 1; //-- Add it to the OpenList (queue) InsertOpenSet( v ); } else if( mV[ v ].color == 1 ) { LowerKeyOpenSet( v ); } else { /** Pretty much nothing by now */ } } //-- End for mV[u].color = 0; //-- Closed List } //-- End while }
Pathfinding::ErrorCodes Pathfinding::CalculatePath() { if (calculated) return AlreadyCalculated; if (mapimg.empty()) return NoMap; if (IsWall(start)) return StartIsWall; if (IsWall(dest)) return DestIsWall; dir = string(); mapimg.copyTo(pathmap); int **closedSet = new int*[mrows]; float **openSet = new float*[mrows]; for (int i = 0; i < mrows; i++) { closedSet[i] = new int[mcols]; openSet[i] = new float[mcols]; for (int j = 0; j < mcols; j++) { closedSet[i][j] = 0; openSet[i][j] = -1.0f; } } priority_queue<Pathfinding::Point2A, vector<Point2A>, CompByFScore> openSetQue[2]; int osq = 0; map<Pathfinding::Point2A, Pathfinding::Point2A, CompByPos> cameFrom; start.fScore = Distance(start, dest); openSetQue[osq].push(start); openSet[start.y][start.x] = 0.0f; while (openSetQue[osq].size() != 0) { Point2A current = openSetQue[osq].top(); if (current == dest) { while (cameFrom.size() != 0) { pathmap.at<Vec3b>(current.y, current.x) = Vec3b(0, 0, 255); dir = to_string(current.dir) + dir; auto it = cameFrom.find(current); Point2A keytmp = current; if (it == cameFrom.end()) { for (int i = 0; i < mrows; i++) { delete openSet[i]; delete closedSet[i]; } delete openSet; delete closedSet; calculated = true; dir = dir.substr(1, dir.length() - 1); return NoError; } current = cameFrom[current]; cameFrom.erase(keytmp); } } openSetQue[osq].pop(); closedSet[current.y][current.x] = 1; int arraySize; Point2A *neighbors = GetNeighbors(current, arraySize); for (int i = 0; i < arraySize; i++) { Point2A neighbor = neighbors[i]; if (closedSet[neighbor.y][neighbor.x] == 1) continue; if (IsWall(neighbor)) { closedSet[neighbor.y][neighbor.x] = 1; continue; } float ngScore = neighbor.level; if (openSet[neighbor.y][neighbor.x] == -1.0f || ngScore < openSet[neighbor.y][neighbor.x]) { cameFrom[neighbor] = current; neighbor.fScore = ngScore + Distance(neighbor, dest) * astar_weight; if (openSet[neighbor.y][neighbor.x] == -1.0f) { openSet[neighbor.y][neighbor.x] = ngScore; openSetQue[osq].push(neighbor); } else { openSet[neighbor.y][neighbor.x] = ngScore; while (!(neighbor == openSetQue[osq].top())) { openSetQue[1 - osq].push(openSetQue[osq].top()); openSetQue[osq].pop(); } openSetQue[osq].pop(); if (openSetQue[osq].size() >= openSetQue[1 - osq].size()) { osq = 1 - osq; } while (!openSetQue[osq].empty()) { openSetQue[1 - osq].push(openSetQue[osq].top()); openSetQue[osq].pop(); } osq = 1 - osq; openSetQue[osq].push(neighbor); } } } delete neighbors; } return NoPath; }
void TwoCopsDijkstra2::push_end_states_on_queue() { QueueEntry qe; CRState crpos; Position pos; NeighborSet neighbors; NeighborSet::iterator it; // sanity check assert( queue.empty() ); // set initial values qe.minFirst = true; qe.value = 0.; // loop through the graph nodes - cop1 for( unsigned int i = 0; i < numnodes; i++ ) { // loop through the rest of the graph nodes - cop2 for( unsigned int j = i; j < numnodes; j++ ) { nodesTouched++; // case 1: robber is caught under cop1 crpos[0] = i; crpos[1] = i; crpos[2] = j; pos = CRHash_MemOptim( crpos ); min_cost[pos] = 0; max_cost[pos] = 0; // shouldn't be necessary // neighbors GetNeighbors( pos, true, neighbors ); nodesExpanded++; for( it = neighbors.begin(); it != neighbors.end(); it++ ) { nodesTouched++; qe.pos = (*it).first; qe.value = (*it).second; queue.push( qe ); } // case 2: robber is caught under cop2 // if cop1 and cop2 are at same position this is not a new case if( i != j ) { nodesTouched++; crpos[0] = j; crpos[1] = i; crpos[2] = j; pos = CRHash_MemOptim( crpos ); min_cost[pos] = 0; max_cost[pos] = 0; // shouldn't be necessary // neighbors GetNeighbors( pos, true, neighbors ); nodesExpanded++; for( it = neighbors.begin(); it != neighbors.end(); it++ ) { nodesTouched++; qe.pos = (*it).first; qe.value = (*it).second; queue.push( qe ); } } } // for loop cop2 } // for loop cop1 return; };