void quadsquare::NotifyChildDisable(const quadcornerdata& cd, int index) // Marks the indexed child quadrant as disabled. Deletes the child node // if it isn't static. { // Clear enabled flag for the child. EnabledFlags &= ~(16 << index); // Update child enabled counts for the affected edge verts. quadsquare* s; if (index & 2) s = this; else s = GetNeighbor(1, cd); if (s) { s->SubEnabledCount[1]--; } if (index == 1 || index == 2) s = GetNeighbor(2, cd); else s = this; if (s) { s->SubEnabledCount[0]--; } /* We don't really want to delete nodes when they're disabled, do we? if (Child[index]->Static == false) { delete Child[index]; Child[index] = 0; BlockDeleteCount++;//xxxxx } */ }
unsigned int StateMenu::SolveTargetTile(unsigned int newNumb) { int neighbor_this_tile[6]; int defenseWeight=0; int offenseWeight=0; int maxDecweight=-1; int resultTile=0; std::vector<XDHexagonTile*>::iterator iter_; Player* playernow; if( m_PlayerTurn == PlayerType::B ) { playernow = &m_Player_B; } else { playernow = &m_Player_A; } for(iter_=m_HexTiles.begin(); iter_!=m_HexTiles.end(); iter_++) { int index_this_tile=(*iter_)->Index; neighbor_this_tile[0]=-1; neighbor_this_tile[1]=-1; neighbor_this_tile[2]=-1; neighbor_this_tile[3]=-1; neighbor_this_tile[4]=-1; neighbor_this_tile[5]=-1; GetNeighbor(index_this_tile,neighbor_this_tile); int numbToCompare=newNumb; int colToCompare=m_HexTiles.at(index_this_tile)->Color; int decweight=0; for(int n=0; n<6; n++) { if( neighbor_this_tile[n]>=0 && neighbor_this_tile[n]<(m_HexaColCount * m_HexaRowCount) ) { int numb=m_HexTiles.at(neighbor_this_tile[n])->Number; int col=m_HexTiles.at(neighbor_this_tile[n])->Color; if(numb<numbToCompare) { int divider_=numbToCompare-numb; if( col==HEXACOLOR::NETRAL ) decweight+=4; else if(col!=playernow->Color) decweight+=30+30/(divider_); else if(col==playernow->Color) decweight+=2; } else if(numb>=numbToCompare) { if(col!=playernow->Color) decweight+=0; else if(col==playernow->Color) decweight+=20; } }///end for if(maxDecweight<decweight) { if(colToCompare==HEXACOLOR::NETRAL) { maxDecweight=decweight; resultTile=index_this_tile; } } } } cout<<"[ computer ] Number Now : "<<newNumb<<endl; return resultTile; }
void StateMenu::UpdateTiles( const double delta_time, int ret[] ) { // check pointer bool areYouOdd=false; unsigned int index =0; unsigned int indexPointed = -1; for(int j=0; j<m_HexaColCount; j++) { for(int i=0; i<m_HexaRowCount; i++) { int px = 0; int py = 0; if( areYouOdd ) { px = m_StartPointX+(i*(m_HexaWidth+32))+48; py = m_StartPointY+(j*(m_HexaHeight/2 -1)); } else { px = m_StartPointX+(i*(m_HexaWidth+32)) ; py = m_StartPointY+(j*(m_HexaHeight/2 -1 )); } ChangeOnHover( index , HEXACOLOR::NETRAL); if( ( m_PointerX > px ) && ( m_PointerX < (px + m_HexaWidth) ) && ( m_PointerY > py ) && ( m_PointerY < (py + m_HexaHeight)) ) { if( indexPointed == -1 ) indexPointed = index; else { ChangeOnHover( index , HEXACOLOR::NETRAL); } } index++; } areYouOdd = !areYouOdd; } // if vs computer if( m_PlayMode == PLAYMODE::H_VS_C ) { if( m_PlayerTurn == PlayerType::B ) { // discard indexPointed calcualtion above, use m_AI_Answer indexPointed = m_AI_Answer; } } if( indexPointed < (m_HexaColCount * m_HexaRowCount) ) { ChangeOnHover( indexPointed ); ret[0] = indexPointed; // get neighbor int neighbor_[6]; GetNeighbor( indexPointed, neighbor_ ); for(int i=0; i<6; i++) { ret[ i+1 ] = neighbor_ [i]; if( neighbor_[i]>=0 && neighbor_[i]<(m_HexaColCount * m_HexaRowCount) ) { ChangeOnHover( neighbor_[i] , HEXACOLOR::NETRAL); } } } else { cout<<"indexPointed FALSE "<<endl; ret[0] = -1; } }
graphnode* CloneGraph(graphnode* graph, int n) { int k; int i; hashnode *hashtab; graphnode *ograph, *cgraph, *copygraph; //构造简化的hash表用来判断标记节点是否已经分配内存,如果已分配内存则记下地址 hashtab =(hashnode*)malloc(sizeof(hashnode)*n); //初始化为0表示标记未分配内存,为1表示标记已分配内存 for(i = 0; i < n; i++) hashtab[i].flag = 0; //构建队列用于广度优先遍历,队列中存储节点的地址 queue *graphqueue = (queue*)malloc(sizeof(queue)); graphqueue->front = 0; graphqueue->rear = 0; graphqueue->array = (graphnode**)malloc(sizeof(graphnode*)*n); if(!graph) return NULL; copygraph = (graphnode*)malloc(sizeof(graphnode));//图的头节点 copygraph->label = graph->label; graphqueue->array[0] = graph; graphqueue->front++; cgraph = copygraph; hashtab[graph->label].flag = 1; hashtab[graph->label].pnode = copygraph; while(graphqueue->front != graphqueue->rear) { ograph = graphqueue->array[graphqueue->rear++]; k = GetNeighbor(ograph); cgraph = hashtab[ograph->label].pnode; cgraph->neighbor = (graphnode**)malloc(sizeof(graphnode*) *(k+1)); for(i = 0; i < k; i++) { if(hashtab[ograph->neighbor[i]->label].flag == 0) { cgraph->neighbor[i] = (graphnode*)malloc(sizeof(graphnode)); cgraph->neighbor[i]->label = ograph->neighbor[i]->label; hashtab[ograph->neighbor[i]->label].flag = 1; hashtab[ograph->neighbor[i]->label].pnode = cgraph->neighbor[i]; graphqueue->array[graphqueue->front++] = ograph->neighbor[i]; } else cgraph->neighbor[i] = hashtab[ograph->neighbor[i]->label].pnode; } cgraph->neighbor[i] = NULL; } free(graphqueue->array); free(graphqueue); free(hashtab); return copygraph; }
BOOL PSProvince::IsCoastal() { for (int k = 0; k < GetNumberOfNeighbors(); k++) { if (GetNeighbor(k)->GetType() == WATER_BODY) { return TRUE; } } return FALSE; }
//扩展节点 void CAstar::ExtendNode(CAstarNode* s) { vector<Point> neighbor = GetNeighbor(s->m_hgevCoordinate); for(vector<Point>::iterator iter1 = neighbor.begin(); iter1 != neighbor.end(); iter1++) { bool exist = false; vector<CAstarNode>::iterator iter2 = m_vClosedList.begin(); for(; iter2 != m_vClosedList.end(); iter2++) { if((*iter1) == (*iter2).m_hgevCoordinate) { exist = true; break; } } //节点在close表中,跳过该节点 if(exist) continue; //计算该节点的确定耗费 int tentative_g = s->m_nG; if((abs((*iter1).x-s->m_hgevCoordinate.x) + abs((*iter1).y-s->m_hgevCoordinate.y)) == 1) tentative_g += DEMO::MOVE_MIN_STEP; else tentative_g += DEMO::MOVE_MAX_STEP; iter2 = m_vOpenList.begin(); for(; iter2 != m_vOpenList.end(); iter2++) { if((*iter1) == (*iter2).m_hgevCoordinate) { exist = true; break; } } //节点不在open表中,则添加到open表中 if(!exist) { CAstarNode temp(*iter1,tentative_g,CalculateH(*iter1),s); m_vOpenList.push_back(temp); push_heap(m_vOpenList.begin(),m_vOpenList.end(),cmpAstarNode); } //否则若g小于open表中该节点的g值,则更新表中的g else if((*iter2).m_nG > tentative_g) { (*iter2).m_nG = tentative_g; } } }
int smoothFunctionFH(int pixel1, int pixel2, int label1, int label2, void* extraData) { ForSmoothFH* data = (ForSmoothFH*)extraData; CvPoint* point1 = (*(data->pointMapping))[pixel1]; CvPoint* point2 = (*(data->pointMapping))[pixel2]; CvPoint shift1 = GetShift(label1, data->shiftSize); CvPoint shift2 = GetShift(label2, data->shiftSize); CvPoint origin1 = cvPoint(shift1.x + point1->x, shift1.y + point1->y); CvPoint origin2 = cvPoint(shift2.x + point2->x, shift2.y + point2->y); CvPoint neighbor1 = GetNeighbor(*point1, *point2, origin1); CvPoint neighbor2 = GetNeighbor(*point2, *point1, origin2); CvSize inputSize = cvSize(data->input->width, data->input->height); if(IsOutside(origin1, inputSize) || IsOutside(origin2, inputSize) || IsOutside(neighbor1, inputSize) || IsOutside(neighbor2, inputSize)) return 10000; int energy = 0; energy += SquareColorDifference(origin1, neighbor2, data->input); energy += SquareColorDifference(origin2, neighbor1, data->input); energy += SquareColorDifference(origin1, neighbor2, data->inputGradient); energy += SquareColorDifference(origin2, neighbor1, data->inputGradient); }
void CClassifyGrid::RefineChunk( CClassifyChunk * chunk ) { for ( int x = 0; x < m_nUnitNumber[ 0 ]; x++ ) for ( int y = 0; y < m_nUnitNumber[ 1 ]; y++ ) { PointDataVector & cell_vector = chunk->m_vecGridIndex[ chunk->Index( x, y ) ]; for ( int i = 0; i < ( int )cell_vector.size(); i++ ) { PointData & point = *( cell_vector[ i ] ); GetNeighbor( point, 2, chunk, x, y ); CFeatureCalculator::RefineClassification( point, m_vecPointData ); } } m_vecState[ chunk->m_iIndex ] = chunk->CCS_Refined; NotifyCCSRefined( chunk->m_iX, chunk->m_iY ); }
int smoothFunctionFHMask2(CvPoint inputPoint, CvPoint outputPoint, CvPoint knownPoint, ForDataFH2* forData) { CvPoint inputNeighbor = GetNeighbor(knownPoint, outputPoint, inputPoint); // neighbor of inputPoint // neighbor of the knownPoint is actually the outputPoint int energy = 0; if(IsOutside(inputNeighbor, cvSize(forData->input->width, forData->input->height))) { return 100000; } // data term energy += SquareColorDifference(inputNeighbor, forData->input, knownPoint, forData->mask, forData->input); energy += SquareColorDifference(inputPoint, forData->input, outputPoint, forData->mask, forData->input); // gradient different term energy += 2 * SquareColorDifference(inputNeighbor, forData->inputGradient, knownPoint, forData->mask, forData->inputGradient); energy += 2 * SquareColorDifference(inputPoint, forData->inputGradient, outputPoint, forData->mask, forData->inputGradient); return energy; }
/** * @brief Extract MSER regions * @param img Input image * @param[out] regions Output regions */ void MSERExtractor::Extract( const image::Image<unsigned char> & img , std::vector<MSERRegion> & regions ) const { // Compute minimum and maximum region area relative to this image const int minRegArea = img.Width() * img.Height() * m_minimum_area; const int maxRegArea = img.Width() * img.Height() * m_maximum_area; // List of processed pixels (maybe we can use a more efficient structure) std::vector<std::vector<bool >> processed; processed.resize( img.Width() ); for (int i = 0; i < img.Width(); ++i ) { processed[ i ].resize( img.Height() ); std::fill( processed[ i ].begin() , processed[ i ].end() , false ); } // Holds the boundary of given grayscale value (boundary[0] -> pixels in the boundary with 0 grayscale value) std::vector<PixelStackElt> boundary[ 256 ]; // List of regions computed so far (not only valid MSER regions) std::vector<MSERRegion *> regionStack; // Push en empty region regionStack.push_back( new MSERRegion ); // Start processing from top left pixel PixelStackElt cur_pix; cur_pix.pix_x = 0; cur_pix.pix_y = 0; cur_pix.pix_level = img( 0 , 0 ); cur_pix.edge_index = PIXEL_RIGHT; processed[ cur_pix.pix_x ][ cur_pix.pix_y ] = true; regionStack.push_back( new MSERRegion( cur_pix.pix_level , cur_pix.pix_x , cur_pix.pix_y ) ); int priority = 256; // Start process while (1) { bool restart = false; // Process neighboring to see if there's something to search with lower grayscale level for ( PixelNeighborsDirection curDir = cur_pix.edge_index; curDir <= PIXEL_BOTTOM_RIGHT; curDir = NextDirection( curDir , m_connectivity ) ) { int nx , ny; GetNeighbor( cur_pix.pix_x , cur_pix.pix_y , curDir , img.Width() , img.Height() , nx , ny ); // Pixel was not processed before if (ValidPixel( nx , ny , img.Width() , img.Height() ) && ! processed[ nx ][ ny ] ) { const int nLevel = img( ny , nx ); processed[ nx ][ ny ] = true; // Info of the neighboring pixel PixelStackElt n_elt; n_elt.pix_x = nx; n_elt.pix_y = ny; n_elt.pix_level = nLevel; n_elt.edge_index = PIXEL_RIGHT; // Now look from which pixel do we have to continue if (nLevel >= cur_pix.pix_level ) { // Continue from the same pixel boundary[ nLevel ].push_back( n_elt ); // Store the lowest value so far priority = std::min( nLevel , priority ); } else { // Go on with the neighboring pixel (go down) cur_pix.edge_index = NextDirection( curDir , m_connectivity ); // Next time we have to process the next boundary pixel boundary[ cur_pix.pix_level ].push_back( cur_pix ); // Store the lowest value so far priority = std::min( cur_pix.pix_level , priority ); // Push the next pixel to process cur_pix = n_elt; restart = true; break; } } } // Do we have to restart from a new pixel ? if (restart ) { // If so it's that because we found a lower grayscale value so let's start a new region regionStack.push_back( new MSERRegion( cur_pix.pix_level , cur_pix.pix_x , cur_pix.pix_y ) ); continue; } // We have process all the neighboring pixels, current pixel is the lowest we have found so far // now process the current pixel regionStack.back()->AppendPixel( cur_pix.pix_x , cur_pix.pix_y ); // End of the process : we have no boundary region, compute MSER from graph if (priority == 256 ) { regionStack.back()->ComputeMSER( m_delta , minRegArea , maxRegArea , m_max_variation , m_min_diversity , regions ); break; } PixelStackElt next_pix = boundary[ priority ].back(); boundary[ priority ].pop_back(); // Get the next pixel level while (boundary[ priority ].empty() && ( priority < 256 )) { ++priority; } // Clear the stack const int newLevel = next_pix.pix_level; // Process the current stack of pixels if the next processing pixel is not at the same curent level if (newLevel != cur_pix.pix_level ) { // Try to merge the regions to fomr a tree ProcessStack( newLevel , next_pix.pix_x , next_pix.pix_y , regionStack ); } // Update next pixel for processing cur_pix = next_pix; } // Clear region stack created so far for (size_t i = 0; i < regionStack.size(); ++i ) { delete regionStack[ i ]; } }
void quadsquare::UpdateAux(const quadcornerdata& cd, const float ViewerLocation[3], float CenterError, clip_result_t vis ) // Does the actual work of updating enabled states and tree growing/shrinking. { BlockUpdateCount++; //xxxxx check_assertion( vis != NotVisible, "Invalid visibility value" ); if ( vis != NoClip ) { vis = ClipSquare( cd ); if ( vis == NotVisible ) { return; } } // Make sure error values are current. if (Dirty) { RecomputeError(cd); } int half = 1 << cd.Level; int whole = half << 1; // See about enabling child verts. // East vert. if ( (EnabledFlags & 1) == 0 && VertexTest(cd.xorg + whole, Vertex[1].Y, cd.zorg + half, Error[0], ViewerLocation, cd.Level, East) == true ) { EnableEdgeVertex(0, false, cd); } // South vert. if ( (EnabledFlags & 8) == 0 && VertexTest(cd.xorg + half, Vertex[4].Y, cd.zorg + whole, Error[1], ViewerLocation, cd.Level, South) == true ) { EnableEdgeVertex(3, false, cd); } if (cd.Level > 0) { if ((EnabledFlags & 32) == 0) { if (BoxTest(cd.xorg, cd.zorg, half, MinY, MaxY, Error[3], ViewerLocation) == true) EnableChild(1, cd); // nw child.er } if ((EnabledFlags & 16) == 0) { if (BoxTest(cd.xorg + half, cd.zorg, half, MinY, MaxY, Error[2], ViewerLocation) == true) EnableChild(0, cd); // ne child. } if ((EnabledFlags & 64) == 0) { if (BoxTest(cd.xorg, cd.zorg + half, half, MinY, MaxY, Error[4], ViewerLocation) == true) EnableChild(2, cd); // sw child. } if ((EnabledFlags & 128) == 0) { if (BoxTest(cd.xorg + half, cd.zorg + half, half, MinY, MaxY, Error[5], ViewerLocation) == true) EnableChild(3, cd); // se child. } // Recurse into child quadrants as necessary. quadcornerdata q; if (EnabledFlags & 32) { SetupCornerData(&q, cd, 1); Child[1]->UpdateAux(q, ViewerLocation, Error[3], vis); } if (EnabledFlags & 16) { SetupCornerData(&q, cd, 0); Child[0]->UpdateAux(q, ViewerLocation, Error[2], vis); } if (EnabledFlags & 64) { SetupCornerData(&q, cd, 2); Child[2]->UpdateAux(q, ViewerLocation, Error[4], vis); } if (EnabledFlags & 128) { SetupCornerData(&q, cd, 3); Child[3]->UpdateAux(q, ViewerLocation, Error[5], vis); } } // Test for disabling. East, South, and center. if ( (EnabledFlags & 1) && SubEnabledCount[0] == 0 && VertexTest(cd.xorg + whole, Vertex[1].Y, cd.zorg + half, Error[0], ViewerLocation, cd.Level, East) == false) { EnabledFlags &= ~1; quadsquare* s = GetNeighbor(0, cd); if (s) s->EnabledFlags &= ~4; } if ( (EnabledFlags & 8) && SubEnabledCount[1] == 0 && VertexTest(cd.xorg + half, Vertex[4].Y, cd.zorg + whole, Error[1], ViewerLocation, cd.Level, South) == false) { EnabledFlags &= ~8; quadsquare* s = GetNeighbor(3, cd); if (s) s->EnabledFlags &= ~2; } if (EnabledFlags == 0 && cd.Parent != NULL && BoxTest(cd.xorg, cd.zorg, whole, MinY, MaxY, CenterError, ViewerLocation) == false) { // Disable ourself. cd.Parent->Square->NotifyChildDisable(*cd.Parent, cd.ChildIndex); // nb: possibly deletes 'this'. } }
void quadsquare::StaticCullAux(const quadcornerdata& cd, float ThresholdDetail, int TargetLevel) // Check this node and its descendents, and remove nodes which don't contain // necessary detail. { int i, j; quadcornerdata q; if (cd.Level > TargetLevel) { // Just recurse to child nodes. for (j = 0; j < 4; j++) { if (j < 2) i = 1 - j; else i = j; if (Child[i]) { SetupCornerData(&q, cd, i); Child[i]->StaticCullAux(q, ThresholdDetail, TargetLevel); } } return; } // We're at the target level. Check this node to see if it's OK to delete it. // Check edge vertices to see if they're necessary. float size = 2 << cd.Level; // Edge length. if (Child[0] == NULL && Child[3] == NULL && Error[0] * ThresholdDetail < size) { quadsquare* s = GetNeighbor(0, cd); if (s == NULL || (s->Child[1] == NULL && s->Child[2] == NULL)) { // Force vertex height to the edge value. float y = (cd.Verts[0].Y + cd.Verts[3].Y) * 0.5; Vertex[1].Y = y; Error[0] = 0; // Force alias vertex to match. if (s) s->Vertex[3].Y = y; Dirty = true; } } if (Child[2] == NULL && Child[3] == NULL && Error[1] * ThresholdDetail < size) { quadsquare* s = GetNeighbor(3, cd); if (s == NULL || (s->Child[0] == NULL && s->Child[1] == NULL)) { float y = (cd.Verts[2].Y + cd.Verts[3].Y) * 0.5; Vertex[4].Y = y; Error[1] = 0; if (s) s->Vertex[2].Y = y; Dirty = true; } } // See if we have child nodes. bool StaticChildren = false; for (i = 0; i < 4; i++) { if (Child[i]) { StaticChildren = true; if (Child[i]->Dirty) Dirty = true; } } // If we have no children and no necessary edges, then see if we can delete ourself. if (StaticChildren == false && cd.Parent != NULL) { bool NecessaryEdges = false; for (i = 0; i < 4; i++) { // See if vertex deviates from edge between corners. float diff = fabs(Vertex[i+1].Y - (cd.Verts[i].Y + cd.Verts[(i+3)&3].Y) * 0.5); if (diff > 0.00001) { NecessaryEdges = true; } } if (!NecessaryEdges) { size *= 1.414213562; // sqrt(2), because diagonal is longer than side. if (cd.Parent->Square->Error[2 + cd.ChildIndex] * ThresholdDetail < size) { delete cd.Parent->Square->Child[cd.ChildIndex]; // Delete this. cd.Parent->Square->Child[cd.ChildIndex] = 0; // Clear the pointer. } } } }