// group.cc - Classe Group // robert - Programme du robot APBteam // Copyright (C) 2005 Olivier Gaillard /// @file group.cc Cree une liste chainee de zones correspondant aux balles #include #include #include #include "group.hh" /// Constructeur /// @param *img classe image /// @param *segm classe segm Group::Group (Img *img, Segm *segm, Eraser *eraser) : segm_ (segm), img_ (img), eraser_ (eraser) { oconfig_ = OConfig::getInstance (); } /// Ajoute une balle ou un poteau à la liste de groupes /// @param idColor numéro de la couleur du group /// @param xmin,xmax,ymin,ymax borne du group /// @param centerx, centery centre du group void Group::addZone (const int idColor, const int xmin, const int xmax, const int ymin, const int ymax) { Zone zone; zone.xmin = xmin; zone.xmax = xmax; zone.ymin = ymin; zone.ymax = ymax; zone.centerx = (xmax+xmin)/2; zone.centery = (ymax+ymin)/2; // XXX test avec l'aire ? // test si la zone est assez grande if (((xmax - xmin) < oconfig_->minLengthZone) || ((ymax - ymin) < oconfig_->minLengthZone)) return; zone.color = idColor; zone.circleUp = false; for (std::vector::iterator iter = oconfig_->groupColor.begin (); iter != oconfig_->groupColor.end (); ++iter) if (idColor == iter->color) zone.id = iter->type; zoneList_.push_back (zone); } /// Cherche l'objet complet a partir d'un pixel donne /// @param type type du group à rechercher GOAL ou BALL /// @param numColor numero de la couleur a chercher /// @param x,y coordonnees de depart pour la recherche void Group::plague (const unsigned char numColor, const int x, const int y) { int xmax = x; int xmin = x; int ymax = y; int ymin = y; // TODO ajouter une inertie ? int tmpY = img_->tabY_[y]; // Parcours de l'objet trouve de haut en bas et de gauche à droite const int j = 2; const int wMinus1 = img_->width_-1; const int hMinus1 = img_->height_-1; while ((xmax < wMinus1)&& (segm_->giveColor(img_->tabData_ + ((xmax)+tmpY)*3, true, true) == numColor)) {xmax += j;} while ((xmin > 0) && (segm_->giveColor(img_->tabData_ + ((xmin)+tmpY)*3, true, true) == numColor)) {xmin -= j;} while ((ymax < hMinus1) && (segm_->giveColor(img_->tabData_ + (img_->tabY_[ymax] + x)*3, true, true) == numColor)) {ymax +=j;} while ((ymin > 0) && (segm_->giveColor(img_->tabData_ + (img_->tabY_[ymin]+x)*3, true, true) == numColor)) {ymin -= j;} // Ajout de la zone addZone (numColor, xmin, xmax, ymin, ymax); } /// Affiche les zones trouvees void Group::showZones () const { std::cout << "Groupes :\n"; if (!zoneList_.size ()) return; for (std::vector::const_iterator iter = zoneList_.begin (); iter != zoneList_.end (); ++iter) std::cout << (int)iter->id << " " << iter->xmin << " " << iter->xmax << " " << iter->ymin << " " << iter->ymax << "\n"; std::cout << std::endl; } /// Selectionne les points a tester dans l'image /// @param numColor numero de la couleur a trouver void Group::jumpPoints (int color) { std::vector list; ObjectColor obj; obj.label = "undefined"; obj.color = color; list.push_back (obj); jumpPoints (list); } void Group::jumpPoints (const std::vector &colorList) { // Initialisation zoneList_.clear (); segm_->clearLum (); segm_->setMode (img_->colorMode_); int tmpY; unsigned char curColor; std::vector::const_iterator iter; // Parcours d'une partie des pixels de l'image for (int y=0; yheight_; y+=oconfig_->jumpPointDist) { tmpY = y*img_->width_; for (int x=0; xwidth_; x+=oconfig_->jumpPointDist) { curColor = segm_->giveColor (img_->tabData_ + ((tmpY + x) * 3), true, true, true); for (iter = colorList.begin (); iter != colorList.end (); ++iter) if ((iter->color >= 0) && (iter->color == curColor)) { plague (curColor, x, y); break; } } } std::cout << " Luminosité : " << segm_->getLum () << std::endl; } /// Convertit le label de couleur en nombre std::string Group::translateToColorName (const int color) { std::string label; if (color == Group::redSkittle) label = "redSkittle"; else if (color == Group::greenSkittle) label = "greenSkittle"; else if (color == Group::gap) label = "gap"; else if (color == Group::border) label = "border"; else if (color == Group::base) label = "base"; else if (color == Group::reflectBand) label = "reflectBand"; else label = "undefined"; return label; }