package utils.images;
import java.awt.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Region;
import utils.defines.Defines;
/**
* Esta clase representa una figura (objeto) reconocido en una
* imagen, junto con los datos del centroide, �rea, color, ID,
* y coordenadas de la esquina superior izquierda del rect�ngulo
* que la encierra (<i>bounding box</i>).
*/
public class RecognizedShape
{
private Region polygon;
int[] points;
public double x, y; // Coord. del centroide
public double xOld, yOld; // Coord. previa del centroide (para c�lculo de direcci�n)
public double bx, by; // Coord. de la esquina superior izquiera
public double vx, vy; // Coord. del punto de visi�n
public double area; // �rea
public Color color;
public String shapeId;
public RecognizedShape()
{
polygon = new Region();
}
public RecognizedShape( double x, double y,
double bx, double by,
double area, Color color,
int[] xp, int[] yp, int np)
{
this.x = x;
this.y = y;
this.xOld = x;
this.yOld = y;
this.bx = bx;
this.by = by;
this.vx = x; // En principio, se toma como "punto de
this.vy = y; // visi�n" al centroide.
this.area = area;
this.color = new Color(color.getRGB());
if ( xp != null && yp != null )
{
points = new int[ 2 * np ];
for (int i=0; i<points.length; i+=2)
{
points[i] = xp[i/2];
points[i+1] = yp[i/2];
}
polygon = new Region();
polygon.add( points );
}
}
public void setCentroid(double x, double y)
{
this.x = x;
this.y = y;
}
public void setBoundingBox(double bx, double by)
{
this.bx = bx;
this.by = by;
}
public void setVisionPoint(double vx, double vy)
{
this.vx = vx;
this.vy = vy;
}
public void setArea(double a)
{
this.area = a;
}
public void setColor(int r, int g, int b)
{
this.color = new Color(r,g,b);
}
public boolean contains(double x, double y)
{
return polygon.contains( (int)x , (int)y );
}
public int getNPoints()
{
return points.length;
}
public int[] getPoints()
{
return points;
}
public void addShape(Shape shape)
{
points = shape.getPoints();
polygon.add( points );
}
public boolean contains(Point p) {
return polygon.contains(p);
}
public String getXMLConfig(String tab)
{
String xml = new String();
String tabulator = new String();
tabulator = tab;
xml = xml.concat(tabulator + "<recognizedShape shapeId=\""+this.shapeId+"\">" + Defines.CR);
tabulator = tab + "\t";
xml = xml.concat(tabulator + "<centroid>" + Defines.CR);
tabulator = tab + "\t\t";
xml=xml.concat(tabulator + "<x>" + this.x + "</x>" + Defines.CR);
xml=xml.concat(tabulator + "<y>" + this.y + "</y>" + Defines.CR);
tabulator = tab + "\t";
xml = xml.concat(tabulator + "</centroid>" + Defines.CR);
xml = xml.concat(tabulator + "<boundingBox>" + Defines.CR);
tabulator = tab + "\t\t";
xml=xml.concat(tabulator + "<x>" + this.bx + "</x>" + Defines.CR);
xml=xml.concat(tabulator + "<y>" + this.by + "</y>" + Defines.CR);
tabulator = tab + "\t";
xml = xml.concat(tabulator + "</boundingBox>" + Defines.CR);
xml = xml.concat(tabulator + "<visionPoint>" + Defines.CR);
tabulator = tab + "\t\t";
xml=xml.concat(tabulator + "<x>" + this.vx + "</x>" + Defines.CR);
xml=xml.concat(tabulator + "<y>" + this.vy + "</y>" + Defines.CR);
tabulator = tab + "\t";
xml = xml.concat(tabulator + "</visionPoint>" + Defines.CR);
xml = xml.concat(tabulator + "<area>" + this.area + "</area>" + Defines.CR);
xml = xml.concat(tabulator + "<color>" + Defines.CR);
tabulator = tab + "\t\t";
xml=xml.concat(tabulator + "<r>" + this.color.getRed() + "</r>" + Defines.CR);
xml=xml.concat(tabulator + "<g>" + this.color.getGreen() + "</g>" + Defines.CR);
xml=xml.concat(tabulator + "<b>" + this.color.getBlue() + "</b>" + Defines.CR);
tabulator = tab + "\t";
xml = xml.concat(tabulator + "</color>" + Defines.CR);
xml = xml.concat(tabulator + "<shape>" + Defines.CR);
tabulator = tab + "\t\t";
for (int i=0 ; i<points.length;i+=2)
{
xml = xml.concat(tabulator + "<point>" + Defines.CR);
tabulator = tab + "\t\t\t";
xml=xml.concat(tabulator + "<x>" + points[i] + "</x>" + Defines.CR);
xml=xml.concat(tabulator + "<y>" + points[i+1] + "</y>" + Defines.CR);
tabulator = tab + "\t\t";
xml = xml.concat(tabulator + "</point>" + Defines.CR);
}
tabulator = tab + "\t";
xml = xml.concat(tabulator + "</shape>" + Defines.CR);
tabulator = tab;
xml = xml.concat(tabulator + "</recognizedShape>" + Defines.CR);
return xml;
}
}