Package eas.simulation.spatial.sim2D.standardEnvironments

Source Code of eas.simulation.spatial.sim2D.standardEnvironments.WandaEnvironment

/*
* File name:        AbstractWandaEnvironment.java (package eas.simulation.spatial.standardBrains.mdle.wandaRobot)
* Author(s):        Funky
* Java version:     6.0
* Generation date:  May 4, 2011 (2:56:13 PM)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
*   author or licensor (but not in any way that suggests that they endorse
*   you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
*   distribute the resulting work only under the same or a similar license to
*   this one.
*
* + Detailed license conditions (Germany):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
*   http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/

package eas.simulation.spatial.sim2D.standardEnvironments;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.imageio.ImageIO;

import eas.math.geometry.Pixel2D_24Bit;
import eas.math.geometry.Vector2D;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* @author Funky
*
*/
public class WandaEnvironment<Agent extends AbstractAgent2D<?>> extends
    AbstractEnvironment2DFast<Agent> {

  /**
     *
     */
    private static final long serialVersionUID = -392317289882172264L;
    /**
   * BufferedImage mit Bodenplan
   */
  private transient BufferedImage floorImage = null;
  /**
   * Linke obere Ecke des Bodenplans
   */
  private Vector2D floorImagePos = null;
  /**
   * BufferedImage mit projeziertem Bild
   */
  private transient BufferedImage projectorImage = null;
  /**
   * Linke obere Ecke des projezierten Bildes
   */
  private Vector2D projectorImagePos = null;
 
  /**
   * Draw respective image in environment
   */
  public boolean drawFloorImage = true;
  public boolean drawProjectorImage = true;

  @Override
  public List<SingleParameter> getParameters() {
      List<SingleParameter> list = super.getParameters();
     
      list.add(new SingleParameter("DrawFloorImage", Datatypes.BOOLEAN, true, "--", this.getEnvironmentName()));
      list.add(new SingleParameter("DrawProjectorImage", Datatypes.BOOLEAN, true, "--", this.getEnvironmentName()));
     
      return list;
  }
 
  /**
   * @param ident
   * @param params
   */
  public WandaEnvironment(int id, ParCollection params) {
    super(id, params);
   
    this.drawFloorImage = params.getParValueBoolean("DrawFloorImage");
    this.drawProjectorImage = params.getParValueBoolean("DrawProjectorImage");
  }

  /**
   * Sets the floor image to img at position pos
   *
   * @param img
   * @param pos
   */
  public void setFloorImage(BufferedImage img, Vector2D pos) {
    this.floorImage = img;
    this.floorImagePos = pos;
  }

  /**
   * Sets the floor image to img at its current position or (0,0) if current
   * position is null
   *
   * @param img
   */
  public void setFloorImage(BufferedImage img) {
    setFloorImage(img, floorImagePos != null ? floorImagePos
        : Vector2D.NULL_VECTOR);
  }

  /**
   * Sets the projector image to img at position pos
   *
   * @param img
   * @param pos
   */
  public void setProjectorImage(BufferedImage img, Vector2D pos) {
    this.projectorImage = img;
    this.projectorImagePos = pos;
  }

  /**
   * Sets the projector image to img at its current position or (0,0) if
   * current position is null
   *
   * @param img
   */
  public void setProjectorImage(BufferedImage img) {
    setProjectorImage(img, projectorImagePos != null ? projectorImagePos
        : Vector2D.NULL_VECTOR);
  }

  /**
   * @return Returns the floorImagePos.
   */
  public Vector2D getFloorImagePos() {
    return this.floorImagePos;
  }

  /**
   * @param floorImagePos
   *            The floorImagePos to set.
   */
  public void setFloorImagePos(Vector2D floorImagePos) {
    this.floorImagePos = floorImagePos;
  }

  /**
   * @return Returns the projectorImagePos.
   */
  public Vector2D getProjectorImagePos() {
    return this.projectorImagePos;
  }

  /**
   * @param projectorImagePos
   *            The projectorImagePos to set.
   */
  public void setProjectorImagePos(Vector2D projectorImagePos) {
    this.projectorImagePos = projectorImagePos;
  }

  /**
   * @return Returns the floorImage.
   */
  public BufferedImage getFloorImage() {
    return this.floorImage;
  }

  /**
   * @return Returns the projectorImage.
   */
  public BufferedImage getProjectorImage() {
    return this.projectorImage;
  }

  private Pixel2D_24Bit getPixelAt(Vector2D pos, BufferedImage img, Vector2D imgPos){
    Vector2D posInImg = new Vector2D(pos);

    if (img != null && imgPos != null) {
      posInImg.sub(imgPos);

      if (posInImg.x > 0 && posInImg.x < img.getWidth()
          && posInImg.y > 0 && posInImg.y < img.getHeight()) {
       
        int[] rgb = new int[3];       
        img.getData().getPixel(Math.round((int) posInImg.x), (int) Math.round(posInImg.y), rgb);
       
        return new Pixel2D_24Bit(posInImg.x, posInImg.y, rgb);
      }
    }

    Color bg = this.getBackgroundColor();
    return new Pixel2D_24Bit((int) Math.round(posInImg.x),
        (int) Math.round(posInImg.y), new int[] { bg.getRed(),
            bg.getGreen(), bg.getBlue() });
  }
 
  /**
   * Returns the RGB color of the pixel in the floor image at pos if pos is
   * within image, else the background color of the environment is returned
   *
   * @param pos
   * @return
   */
  public Pixel2D_24Bit getFloorPixelAt(Vector2D pos) {
    return getPixelAt(pos, floorImage, floorImagePos);
  }

  /**
   * Returns the RGB color of the pixel in the projector image at pos if pos
   * is within image, else the background color of the environment is returned
   *
   * @param pos
   * @return
   */
  public Pixel2D_24Bit getProjectorPixelAt(Vector2D pos) {
    return getPixelAt(pos, projectorImage, projectorImagePos);
  }
 
  private void drawScaled(Graphics g, BufferedImage img, Vector2D pos){
   
    Image scaled = img.getScaledInstance((int) Math.round(this.globalScale() * img.getWidth()),
        (int) Math.round(this.globalScale() * img.getHeight()), Image.SCALE_FAST);
    Vector2D visPos = this.getPointInVisualization(pos);
   
    g.drawImage(scaled, (int) Math.round(visPos.x), (int) Math.round(visPos.y), null);
   
  }
 
  /* (non-Javadoc)
   * @see eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2D#generateEnvironmentView()
   */
  @Override
  public synchronized BufferedImage getOutsideView() {
    BufferedImage img =  super.getOutsideView();
    Graphics g = img.getGraphics();
   
    if(drawFloorImage && floorImage != null){
      drawScaled(g, floorImage, floorImagePos);
    }
   
    if(drawProjectorImage && projectorImage != null){
      drawScaled(g, projectorImage, projectorImagePos);
    }
   
    return img;
  }
 
  public static BufferedImage loadImageFromFile(File f, ParCollection params){
   
    BufferedImage ret = null;
   
    try {
      ret = ImageIO.read(f);
    } catch (IOException e) {
        params.logError(e.toString() + "\n" + Arrays.deepToString(e.getStackTrace()));
    }
   
    return ret;
  }
}
TOP

Related Classes of eas.simulation.spatial.sim2D.standardEnvironments.WandaEnvironment

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.