[Mmil-commits] r28 - trunk/src/fr/loria/led/mmil/objects

alexandredenis at users.gforge.inria.fr alexandredenis at users.gforge.inria.fr
Mar 13 Fév 14:29:08 CET 2007


Author: alexandredenis
Date: 2007-02-13 14:29:06 +0100 (Tue, 13 Feb 2007)
New Revision: 28

Modified:
   trunk/src/fr/loria/led/mmil/objects/MMILPercept.java
   trunk/src/fr/loria/led/mmil/objects/MMILScene.java
Log:


Modified: trunk/src/fr/loria/led/mmil/objects/MMILPercept.java
===================================================================
--- trunk/src/fr/loria/led/mmil/objects/MMILPercept.java	2006-10-04 11:05:42 UTC (rev 27)
+++ trunk/src/fr/loria/led/mmil/objects/MMILPercept.java	2007-02-13 13:29:06 UTC (rev 28)
@@ -9,51 +9,86 @@
 import java.awt.Color;
 import java.awt.Polygon;
 import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.Toolkit;
+import java.awt.image.ImageObserver;
 
+import java.net.*;
 
 /**
     A MMILPercept is a visual object which can be accessed by linguistic expression or by gesture.
-    It is a MMILParticipant which can have features.
+    It is a MMILParticipant which can have visual features.
 */
 public class MMILPercept extends MMILParticipant
 {
-    private String name;
-    private Color color;
-    private Polygon polygon;
-    private boolean filled;
-    private boolean visible;
+    // global properties
+    private String name = "";
+    private Point position = new Point(0,0);
+   
+    // mask properties
+    private Polygon maskShape;
+    private Color maskColor = Color.black;
+    private boolean maskFilled = true;
+    private boolean maskVisible = true;
 
+    // image properties
+    private Image image = null;
+    private URL imageURL = null;
+    private boolean imageVisible = true;
+    
     /**
-        Creates a new MMILPercept with given name and polygon.
-        Default aspect is black, empty and visible.
+        Creates a new MMILPercept with given name and maskShape.
+        Default aspect is black, empty and mask is visible. The default position is (0,0).
     */
-    public MMILPercept(String name, Polygon polygon)
+    public MMILPercept(String name, Polygon maskShape)
     {
         super(name);
+        this.name = name;
+        this.maskShape = maskShape;
+        updateParticipant();
+    }
 
+ 
+    /**
+        Creates a new MMILPercept with given name and maskShape at given position.
+        Default aspect is black, empty and mask is visible.
+    */
+    public MMILPercept(String name, Point position, Polygon maskShape)
+    {
+        super(name);
         this.name = name;
-        this.polygon = polygon;
-        this.color = Color.black;
-        this.filled = false;
-        this.visible = true;
+        this.position = position;
+        this.maskShape = maskShape;
+        updateParticipant();
+    }
 
+    
+    /**
+        Creates a new MMILPercept with given name, image and mask.
+    */
+    public MMILPercept(String name, Point position, URL imageURL, Polygon maskShape)
+    {
+        super(name);
+        this.name = name;
+        this.position = position;
+        this.maskShape = maskShape;
+        setImage(imageURL);
         updateParticipant();
     }
 
 
     /**
-        Creates a new MMILPercept with given name, polygon, color and filled aspects.
+        Creates a new MMILPercept with given name, maskShape, maskColor and maskFilled aspects.
     */
-    public MMILPercept(String name, Polygon polygon, Color color, boolean filled, boolean visible)
+    public MMILPercept(String name, Point position, Polygon maskShape, Color maskColor, boolean maskFilled, boolean maskVisible)
     {
         super(name);
-
         this.name = name;
-        this.polygon = polygon;
-        this.color = color;
-        this.filled = filled;
-        this.visible = visible;
-
+        this.position = position;
+        this.maskShape = maskShape;
+        this.maskColor = maskColor;
+        this.maskFilled = maskFilled;
+        this.maskVisible = maskVisible;
         updateParticipant();
     }
 
@@ -65,50 +100,101 @@
     public MMILPercept(MMILPercept percept)
     {
         super(percept);
-
         name = percept.getName();
-        color = percept.getColor();
-        filled = percept.isFilled();
-        visible = percept.isVisible();
-        polygon = percept.getPolygon();
-
+        position = percept.getPosition();
+        maskColor = percept.getMaskColor();
+        maskFilled = percept.isMaskFilled();
+        maskVisible = percept.isMaskVisible();
+        maskShape = percept.getMaskShape();
+        image = percept.getImage();
+        imageURL = percept.getImageURL();
         updateParticipant();
     }
 
 
     /**
         Creates a new MMILPercept from a MMILparticipant.
+        Warning you should take care that the image of the percept is not necessarily fully loaded
     */
     public MMILPercept(MMILParticipant percept) throws MMILException 
     {
         super(percept);
 
         name = percept.getValueOf("mmilId");
-        color = getColor(percept.getValueOf("color"));
-        if (color==null)
-            color = Color.black;
-        filled = percept.getBooleanValueOf("filled", false);
-        visible = percept.getBooleanValueOf("visible", true);
-        String points = percept.getValueOf("points");
-        if (points.equals(""))
-            throw new NotFoundMMILException("Unable to find a vis:points feature in the participant "+percept.getId());
-        try
+        maskColor = getColorFromString(percept.getValueOf("maskColor"));
+        if (maskColor==null)
+            maskColor = Color.black;
+        maskFilled = percept.getBooleanValueOf("maskFilled", false);
+        maskVisible = percept.getBooleanValueOf("maskVisible", true);
+        
+        String posStr = percept.getValueOf("position");
+        if (posStr.equals(""))
         {
-            polygon = MMILScene.getPolyFromStr(points);
+            System.out.println(percept);
+            throw new NotFoundMMILException("The participant "+percept.getId()+" should have a vis:position feature "+
+                                            "formatted as \"x,y\" where x and y are integers");
         }
-        catch(Exception e)
-        {
-            throw new UnexpectedMMILException("Unable to retrieve a polygon from the vis:points feature in the participant "+percept.getId()+", it should be a list of points x,y");
-        }
+        else position = getPosFromStr(posStr);
+
+        String points = percept.getValueOf("maskPoints");
+        if (!points.equals(""))
+            try
+            {
+                maskShape = MMILScene.getPolyFromStr(points);
+                maskShape.translate(position.x, position.y);
+            }
+            catch(Exception e)
+            {
+                throw new UnexpectedMMILException("Unable to retrieve a maskShape from the vis:maskPoints feature "+
+                                                  "in the participant "+percept.getId()+", it should be a list of points x,y where x and y are integers");
+            }
+
+        String imageURLStr = percept.getValueOf("image"); 
+        if (!imageURLStr.equals(""))
+            try
+            {
+                imageURL = new URL(imageURLStr);
+                setImage(imageURL);
+            }
+            catch(MalformedURLException e)
+            {
+                throw new MalformedMMILException("Unable to retrieve an url from "+percept.getValueOf("image"));
+            }
     }
 
+   
+   
+    
+    /**
+		Returns width of the current image.
+		@return	width of the current image, 0 if not set
+	*/
+	public int getWidth(ImageObserver observer)
+	{
+		if (image!=null)
+			return image.getWidth(observer);
+		else return 0;
+	}
 
+    
+	/**
+		Returns height of the current image.
+		@return height of the current image, 0 if not set
+	*/
+	public int getHeight(ImageObserver observer)
+	{
+		if (image!=null)
+			return image.getHeight(observer);
+		else return 0;
+	}
+
+
     /**
         Number of points of the percept.
     */
     public int size()
     {
-        return polygon.npoints;
+        return maskShape.npoints;
     }
 
 
@@ -118,8 +204,8 @@
     public List<Point> getPoints()
     {
         List<Point> ret = new ArrayList<Point>();
-        for(int i=0; i<polygon.npoints; i++)
-            ret.add(new Point(polygon.xpoints[i], polygon.ypoints[i]));    
+        for(int i=0; i<maskShape.npoints; i++)
+            ret.add(new Point(maskShape.xpoints[i], maskShape.ypoints[i]));    
         return ret;
     }
     
@@ -143,25 +229,29 @@
     */
     public List<Point> getInsidePoints(MMILGesture gesture)
     {
+        if (!hasMask())
+            System.out.println("OBJECT DOES NOT HAVE MASK !");
         List<Point> ret = new ArrayList<Point>();
         for(Point p : gesture)
-            if (polygon.contains(p.getX(), p.getY()))
+            if (hasMask() && maskShape.contains(p.getX(), p.getY()))
                 ret.add(p);
         return ret;
     }
 
 
     /**
-        Paint this percept using its color and the fill flag.
+        Paint this percept using its maskColor and the fill flag.
     */
     public void paint(Graphics g)
     {
-        if (visible)
+        if (imageVisible)
+            g.drawImage(image, position.x, position.y, null);
+        if (maskVisible && maskShape!=null && maskShape.npoints>0)
         {
-            g.setColor(color);
-            if (filled)
-                g.fillPolygon(polygon);
-            else g.drawPolygon(polygon);
+            g.setColor(maskColor);
+            if (maskFilled)
+                g.fillPolygon(maskShape);
+            else g.drawPolygon(maskShape);
         }
     }
 
@@ -171,10 +261,13 @@
     */
     public void shift(double x, double y)
     {
-        for(int i=0; i<polygon.npoints; i++)
+        position.x += x;
+        position.y += y;
+
+        for(int i=0; i<maskShape.npoints; i++)
         {
-            polygon.xpoints[i] += x;
-            polygon.ypoints[i] += y;
+            maskShape.xpoints[i] += x;
+            maskShape.ypoints[i] += y;
         }
 
         for(MMILEntity point : getSubEntities())
@@ -197,6 +290,15 @@
     }
 
 
+    public void moveTo(int x, int y)
+    {
+        int deltaX = x - position.x;
+        int deltaY = y - position.y;
+        position = new Point(x,y);
+        maskShape.translate(deltaX, deltaY);
+    }
+
+    
     /**
         Move this percept to the origin of the referential.
     */
@@ -204,147 +306,255 @@
     {
         double minx=10000, miny=10000;
         
-        for(int i=0; i<polygon.npoints; i++)
+        for(int i=0; i<maskShape.npoints; i++)
         {
-            if (polygon.xpoints[i]<minx)
-                minx = polygon.xpoints[i];
+            if (maskShape.xpoints[i]<minx)
+                minx = maskShape.xpoints[i];
 
-            if (polygon.ypoints[i]<miny)
-                miny = polygon.ypoints[i];
+            if (maskShape.ypoints[i]<miny)
+                miny = maskShape.ypoints[i];
         }
        
         shift(-minx, -miny);
     }
  
 
-    public static Color getColor(String color) 
+    
+
+
+    /**
+        Update the features of the participant using the fields.
+        It synchronizes the participant's features with their true values.
+    */
+    public void updateParticipant()
     {
+        replace(new MMILFeature("mmilId", name));
+        if (imageURL!=null)
+            replace(new MMILFeature("vis", "image", imageURL.toString()));
+        replace(new MMILFeature("vis", "maskColor", getStringColor()));
+        replace(new MMILFeature("vis", "maskFilled", maskFilled ? "true" : "false"));
+        replace(new MMILFeature("vis", "maskVisible", maskVisible ? "true" : "false"));
+        replace(new MMILFeature("vis", "maskPoints", getPolyStr()));
+        replace(new MMILFeature("vis", "position", getPosStr()));
+    }
+
+
+
+//// GLOBAL PROPERTIES
+
+    /**
+        Returns the name of the object.
+    */
+    public String getName()
+    {
+        return name;
+    }
+
+    /**
+        Returns the position of the object.
+    */  
+    public Point getPosition()
+    {
+        return position;
+    }
+    
+    /**
+        Return a string representation of the position.
+    */
+    private String getPosStr()
+    {
+        return position.x+","+position.y;
+    }
+    
+    /**
+        Return a position from a string representation.
+    */
+    private Point getPosFromStr(String pos) throws MalformedMMILException
+    {
         try
         {
-            return (Color) Color.class.getDeclaredField(color).get(null);
+            String[] parts = pos.split(",");
+            return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
         }
         catch(Exception e)
         {
-            return null;
+            throw new MalformedMMILException("\""+pos+"\" is not a valid position, it should be formatted as \"x,y\" where x and y are integers");
         }
     }
 
+////// IMAGE PROPERTIES
 
-    public static String getStringColor(Color color)
+    public boolean hasImage()
     {
-        try
+        return image!=null;
+    }
+    
+    public Image getImage()
+    {
+        return image;
+    }
+    
+    public URL getImageURL()
+    {
+        return imageURL;
+    }
+    
+    public void setImage(URL url)
+    {
+        if (url==null)
         {
-            for(java.lang.reflect.Field f : Color.class.getFields())
-            {
-                Object obj = f.get(color);
-                if (obj instanceof Color && obj.equals(color))
-                    return f.getName();
-            }        
-            return "";
+            imageURL = null;
+            image = null;
         }
-        catch(Exception e)
+        else 
         {
-            return "";
+            imageURL = url;
+           	image = Toolkit.getDefaultToolkit().getImage(url);
         }
     }
 
+////// MASK PROPERTIES
 
     /**
-        Update the features of the participant using the fields.
-        It synchronizes the participant's features with their true values.
+        Creates a rectangular mask with given dimensions.
     */
-    public void updateParticipant()
+    public void createRectangularMask(int width, int height)
     {
-        replace(new MMILFeature("mmilId", name));
-        replace(new MMILFeature("vis", "color", getStringColor()));
-        replace(new MMILFeature("vis", "filled", filled ? "true" : "false"));
-        replace(new MMILFeature("vis", "visible", visible ? "true" : "false"));
-        replace(new MMILFeature("vis", "points", getPolyStr()));
+        System.out.println("MASK :"+width+"/"+height);
+        Polygon mask = new Polygon();
+        mask.addPoint(0,0);
+        mask.addPoint(width,0);
+        mask.addPoint(width,height);    
+        mask.addPoint(0,height);
+        maskShape = mask;
+        updateParticipant();
     }
+    
 
-
     /**
-        Return a string representation of the polygon.
+        Simply creates the mask maskShape corresponding to the whole image.
     */
-    private String getPolyStr()
+    public void createMaskFromImage(ImageObserver observer)
     {
-        return MMILScene.getPolyStr(polygon);
+        int width = getWidth(observer);
+        int height = getHeight(observer);
+        createRectangularMask(width, height);
     }
     
+   
+    public boolean hasMask()
+    {
+        return maskShape!=null && maskShape.npoints>0;
+    }
     
     /**
-        Returns the name of the object.
+        Return a string representation of the maskShape.
     */
-    public String getName()
+    private String getPolyStr()
     {
-        return name;
+        return MMILScene.getPolyStr(maskShape);
     }
+    
 
     /**
-        Returns the polygon object.
+        Returns the maskShape object.
     */
-    public Polygon getPolygon()
+    public Polygon getMaskShape()
     {
-        return polygon;
+        return maskShape;
     }
 
     /**
-        Returns the color.
+        Returns the maskColor.
     */
-    public Color getColor()
+    public Color getMaskColor()
     {
-        return color;
+        return maskColor;
     }
 
     /**
-        Returns a string representation of the color.
-        @return empty string if the color is not defined.
+        Returns a string representation of the maskColor.
+        @return empty string if the maskColor is not defined.
     */
     public String getStringColor()
     {
-        return getStringColor(color);
+        return getStringColor(maskColor);
     }
  
     /**
-        Tests if the percept is filled.
+        Tests if the percept is maskFilled.
     */
-    public boolean isFilled()
+    public boolean isMaskFilled()
     {
-        return filled;
+        return maskFilled;
     }
 
     /**
-        Tests if the percept is visible.
+        Tests if the percept is maskVisible.
     */
-    public boolean isVisible()
+    public boolean isMaskVisible()
     {
-        return visible;
+        return maskVisible;
     }
 
     /**
-        Sets the filled aspect of this percept.
+        Sets the maskFilled aspect of this percept.
     */
-    public void setFilled(boolean filled)
+    public void setMaskFilled(boolean maskFilled)
     {
-        this.filled = filled;
-        replaceFeature("filled", filled ? "true" : "false");
+        this.maskFilled = maskFilled;
+        replaceFeature("maskFilled", maskFilled ? "true" : "false");
     }
 
     /**
-        Sets the visible aspect of this percept.
+        Sets the maskVisible aspect of this percept.
     */
-    public void setVisible(boolean visible)
+    public void setMaskVisible(boolean maskVisible)
     {
-        this.visible = visible;
-        replaceFeature("visible", visible ? "true" : "false");
+        this.maskVisible = maskVisible;
+        replaceFeature("maskVisible", maskVisible ? "true" : "false");
     }
 
     /**
-        Sets the color aspect of this percept.
+        Sets the maskColor aspect of this percept.
     */
-    public void setColor(Color color)
+    public void setMaskColor(Color maskColor)
     {
-        this.color = color;
-        replaceFeature("color", getStringColor());
+        this.maskColor = maskColor;
+        replaceFeature("maskColor", getStringColor());
     }
+    
+    
+/// COLOR TO STRING converter
+
+    public static Color getColorFromString(String color) 
+    {
+        try
+        {
+            return (Color) Color.class.getDeclaredField(color).get(null);
+        }
+        catch(Exception e)
+        {
+            return null;
+        }
+    }
+
+
+    public static String getStringColor(Color maskColor)
+    {
+        try
+        {
+            for(java.lang.reflect.Field f : Color.class.getFields())
+            {
+                Object obj = f.get(maskColor);
+                if (obj instanceof Color && obj.equals(maskColor))
+                    return f.getName();
+            }        
+            return "";
+        }
+        catch(Exception e)
+        {
+            return "";
+        }
+    }
 }

Modified: trunk/src/fr/loria/led/mmil/objects/MMILScene.java
===================================================================
--- trunk/src/fr/loria/led/mmil/objects/MMILScene.java	2006-10-04 11:05:42 UTC (rev 27)
+++ trunk/src/fr/loria/led/mmil/objects/MMILScene.java	2007-02-13 13:29:06 UTC (rev 28)
@@ -46,7 +46,9 @@
         loadSceneFromEntity(scene);
     }
 
-
+    /**
+        Loads a scene from a MMILEntity.
+    */
     private void loadSceneFromEntity(MMILEntity scene) throws MMILException
     {
         if (!(scene instanceof MMILParticipant))
@@ -75,6 +77,35 @@
 
 
     /**
+        Prepare all the images of the scene and create masks if needed.
+        @param comp the component where the scene is drawn
+    */
+    public void prepareScene(Component comp)
+    {
+        MediaTracker tracker = new MediaTracker(comp);
+        for(int i=0; i<size(); i++)
+            if (get(i).hasImage())
+                tracker.addImage(get(i).getImage(), i);
+        try
+        {
+            tracker.waitForAll();
+        }
+        catch (InterruptedException e) {}
+    }
+
+
+    /**
+        Create the masks of every object if they do not have one.
+    */
+    public void createMasks(ImageObserver observer)
+    {
+        for(MMILPercept percept : this)
+            if (!percept.hasMask())
+                percept.createMaskFromImage(observer);
+    }
+    
+
+    /**
         Removes the given percept from the scene.
     */
     public void remove(String id)
@@ -110,6 +141,8 @@
    
     /**
         Retrieve a scene from a String.
+        Warning : the retrieved scene is not prepared, ie the masks for images are not computed if they do not exist.
+        Call the prepareScene method to build masks from image if required.
     */
     public static MMILScene retrieveScene(String scene) throws MMILException
     {






Plus d'informations sur la liste de diffusion Mmil-commits