summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/group.cc
blob: eee05bb0077167bb16470deac9fc9d2445f703ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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 <iostream>
#include <stdlib.h>
#include <math.h>

#include "group.hh"

/// Constructeur
/// @param *img classe image
/// @param *segm classe segm
Group::Group (Img *img, Segm *segm) 
    : segm_ (segm), img_ (img) 
{
    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<ObjectColor>::iterator iter = oconfig_->groupColor.begin ();
	 iter != oconfig_->groupColor.end (); ++iter)
	if (idColor == iter->color)
	  {
	    if (iter->label == "redSkittle") zone.id = redSkittle;
	    else if (iter->label == "greenSkittle") zone.id = greenSkittle;
	    else if (iter->label == "border") zone.id = border;
	    else if (iter->label == "base") zone.id = base;
	    else if (iter->label == "gap") zone.id = gap;
	    else if (iter->label == "reflectBand") zone.id = reflectBand;
	    break;
	  }
    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;
    while ((xmax < img_->width_-1)&& (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 < img_->height_-1) && (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<Zone>::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<ObjectColor> list; 
    ObjectColor obj;
    obj.label = "undefined";
    obj.color = color;
    list.push_back (obj);
    jumpPoints (list);
}

void Group::jumpPoints (const std::vector<ObjectColor> &colorList) 
{
    // Initialisation
    zoneList_.clear ();
    segm_->clearLum ();
    segm_->setMode (img_->colorMode_);
    int tmpY;
    unsigned char curColor;
    std::vector<ObjectColor>::const_iterator iter;
    // Parcours d'une partie des pixels de l'image
    for (int y=0; y<img_->height_; y+=oconfig_->jumpPointDist) 
      {
	tmpY = y*img_->width_;
	for (int x=0; x<img_->width_; 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 << "<Group::jumpPoints> 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;
}