Package eas.simulation.spatial.sim2D.standardEnvironments

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

/*
* File name:        HightProfileEnvironment2D.java (package eas.simulation.spatial.sim2D.standardEnvironments)
* Author(s):        Lukas König
* Java version:     6.0
* Generation date:  24.03.2012 (13:16:21)
*
* (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.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.Random;

import eas.math.geometry.Plane2D;
import eas.math.geometry.Polygon2D;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.math.geometry.Vector3D;
import eas.math.matrix.Matrix;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.standardEnvironments.AbstractEnvironment;
import eas.startSetup.ParCollection;
import eas.startSetup.SingleParameter;
import eas.startSetup.parameterDatatypes.Datatypes;

/**
* @author Lukas König
*/
public class HeightProfileEnvironment2D extends
        AbstractEnvironment2DFast<AbstractAgent2D<?>> {

    private static final long serialVersionUID = -2868031741917727513L;

    public HeightProfileEnvironment2D(int id, ParCollection params) {
        this("HeightProfileEnvironment_" + id, id, null, params);
    }

    public HeightProfileEnvironment2D(String ident, int id,
            AbstractEnvironment<?> fatherEnv, ParCollection params) {
        super(ident, id, fatherEnv, params);

        this.heightProfile = params.getParValueMatrix("heightProfile");
       
        if (!params.getParValueString("heightProfileFromFile").equals("null")) {
            try {
                this.heightProfile = new Matrix(new File(
                        params.getStdDirectory() + "/" + params.getParValueString("heightProfileFromFile")));
            } catch (Exception e) {
            }
        }
       
        this.maxHeight = Double.NEGATIVE_INFINITY;
        this.minHeight = Double.POSITIVE_INFINITY;
       
        for (int i = 0; i < this.heightProfile.getColumnCount(); i++) {
            for (int j = 0; j < this.heightProfile.getRowCount(); j++) {
                if (this.maxHeight < this.heightProfile.get(i, j)) {
                    this.maxHeight = this.heightProfile.get(i, j);
                }
                if (this.minHeight > this.heightProfile.get(i, j)) {
                    this.minHeight = this.heightProfile.get(i, j);
                }
            }           
        }
    }

    private Matrix heightProfile;

    private double maxHeight;
   
    private double minHeight;
   
    private Double heightProfileGridWidth = null;
   
    private Double heightProfileGridHeight = null;
   
    private Rectangle2D heightProfileBoundingBox = null;
   
    public double getHeightAtCoordinate(Vector2D coordinate) {
        if (this.heightProfileGridWidth == null) {
            Rectangle2D boundingBox = this.getBoundingBox(true);
            this.heightProfileGridWidth = boundingBox.getWidth() / (heightProfile.getColumnCount() - 1);
            this.heightProfileGridHeight = boundingBox.getHeight() / (heightProfile.getRowCount() - 1);
            this.heightProfileBoundingBox = new Rectangle2D(boundingBox);
        }
       
        if (!this.heightProfileBoundingBox.isPointInside(coordinate)) {
            return Double.NEGATIVE_INFINITY;
        }
       
        try {
            return this.heightProfile.get(
                    (int) ((coordinate.x - heightProfileBoundingBox.upperLeftCorner().x) / heightProfileGridWidth),
                    (int) ((coordinate.y - heightProfileBoundingBox.upperLeftCorner().y) / heightProfileGridHeight));
        } catch (Exception e) {
            return Double.NEGATIVE_INFINITY;
        }
    }
   
    private Vector3D planePoint1;
    private Vector3D planePoint2;
    private Vector3D planePoint3;
   
    public Vector2D getGradientAtCoordinate(Vector2D coordinate) {
        try {
            Vector2D pointInMatrix = new Vector2D(
                    (coordinate.x - heightProfileBoundingBox.upperLeftCorner().x) / heightProfileGridWidth,
                    (coordinate.y - heightProfileBoundingBox.upperLeftCorner().y) / heightProfileGridHeight);
           
            int x1 = (int) Math.floor(pointInMatrix.x);
            int x2 = (int) Math.ceil(pointInMatrix.x);
            int y1 = (int) Math.floor(pointInMatrix.y);
            int y2 = (int) Math.ceil(pointInMatrix.y);
   
                if (pointInMatrix.distance(new Vector2D(x1, y1)) < pointInMatrix.distance(new Vector2D(x2, y2))) {
                    planePoint1 = new Vector3D(x1, y1, this.heightProfile.get(x1, y1));
                } else {
                    planePoint1 = new Vector3D(x2, y2, this.heightProfile.get(x2, y2));
                }
                planePoint2 = new Vector3D(x2, y1, this.heightProfile.get(x2, y1));
                planePoint3 = new Vector3D(x1, y2, this.heightProfile.get(x1, y2));
           
            Plane2D plane = new Plane2D(planePoint1, planePoint2, planePoint3);
           
            Vector3D normalVector = plane.getNormalizedZPositiveNormalVector();
           
            Vector2D gradient = new Vector2D(normalVector.x, normalVector.y);
           
            return gradient;
        } catch (Exception e) {
            return new Vector2D(0, 0);
        }
    }
   
    @Override
    public List<SingleParameter> getParameters() {
        List<SingleParameter> list = super.getParameters();
       
        list.add(new SingleParameter(
                "heightProfileFromFile",
                Datatypes.STRING,
                "null",
                "Filename to read height profile from - null if height profile should be read from parameter \"heightProfile\"",
                this.getClass().getSimpleName().toUpperCase()));
       
        list.add(new SingleParameter(
                "heightProfile",
                Datatypes.MATRIX,
                new Matrix(new double[][] {{1.0, 1.1, 1.2}, {2.0, 2.1, 2.2}, {3.0, 3.1, 3.2}}),
                "",
                this.getClass().getSimpleName().toUpperCase()));
       
        list.add(new SingleParameter(
                "showAdditionalInfo",
                Datatypes.fixedStringSet(new String[] {
                        "none",
                        "heightProfile",
                        "heightProfile_and_gradients"}),
                "heightProfile",
                "Select which information about the height profile to visualize.",
                this.getClass().getSimpleName().toUpperCase()));
       
        return list;
    }
   
    private transient BufferedImage tempStoredHeightImg = null;
    private Rectangle2D zoomBox = null;
   
    @Override
    public synchronized void setZoomBoxMiddle(Vector2D middle) {
        super.setZoomBoxMiddle(middle);
        this.zoomBox = null;
    }

    @Override
    public void setZoomBox(Rectangle2D zoomBox) {
        super.setZoomBox(zoomBox);
        this.zoomBox = null;
    }
   
    private double height;
    private double width;
   
    @Override
    public void setScreenHeight(double height) {
        super.setScreenHeight(height);
        if (height != this.height) {
            this.zoomBox = null;
            this.height = height;
        }
    }
   
    @Override
    public void setScreenWidth(double width) {
        super.setScreenWidth(width);
        if (width != this.width) {
            this.zoomBox = null;
            this.width = width;
        }
    }
   
    @Override
    public void setVisualizationAngle(Vector2D centerPoint, double angleRAD) {
        super.setVisualizationAngle(centerPoint, angleRAD);
        this.tempStoredHeightImg = null;
    }
   
    @Override
    public BufferedImage getOutsideView() {
        return this.getOutsideView(null);
    }

    @Override
    public synchronized BufferedImage getOutsideView(Graphics2D g1) {
        if (g1 != null) {
            return super.getOutsideView(g1);
        }
       
        BufferedImage img = super.getOutsideView(null);
       
        Graphics2D g = null;
       
        if (!this.getCurrentViewBox().equals(this.zoomBox)
                || this.getParCollection().getParValueString("showAdditionalInfo").contains("gradients")) {
            this.tempStoredHeightImg = null;
        }
       
        if (tempStoredHeightImg == null
                && this.getParCollection().getParValueString("showAdditionalInfo").contains("heightProfile")) {
            tempStoredHeightImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
            g = tempStoredHeightImg.createGraphics();
            g.setBackground(new Color(0, 0, 0, 0));
           
            this.getHeightAtCoordinate(Vector2D.NULL_VECTOR);

            for (double d = this.heightProfileBoundingBox.upperLeftCorner().x;
                    d < this.heightProfileBoundingBox.lowerRightCorner().x - 0.1;
                    d += this.heightProfileGridWidth) {
                for (double e = this.heightProfileBoundingBox.upperLeftCorner().y;
                        e < this.heightProfileBoundingBox.lowerRightCorner().y - 0.1;
                        e += this.heightProfileGridHeight) {
                    Vector2D v1 = new Vector2D(d, e);
                    double height = this.getHeightAtCoordinate(v1);
                   
                    if (height == Double.NEGATIVE_INFINITY) {
                        break;
                    }
                   
                    int a = (int) ((height - this.minHeight)
                                    * (255 - 0)
                                    / (this.maxHeight - this.minHeight) + 0);
   
                   
                    Color c = new Color(a, 100, 100, 100);
                   
                    g.setColor(c);
                   
                    Polygon2D p = new Polygon2D();
                    p.add(v1);
                    p.add(new Vector2D(v1.x, v1.y + this.heightProfileGridHeight));
                    p.add(new Vector2D(v1.x + this.heightProfileGridWidth, v1.y + this.heightProfileGridHeight));
                    p.add(new Vector2D(v1.x + this.heightProfileGridWidth, v1.y));
                    p = this.getPolygonInVisualization(p);
                    Polygon pol = p.toPol();
                   
                    g.drawPolygon(pol);
                    g.fillPolygon(pol);
                    g.drawLine(pol.xpoints[0], pol.ypoints[0], pol.xpoints[2], pol.ypoints[2]);
                }
            }
           
            if (this.getParCollection().getParValueString("showAdditionalInfo").contains("gradients")) {
                g.setStroke(new BasicStroke(3));
               
                for (int f : this.getAgentIDs()) {
                    Random rand = new Random(f);
                    g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
                   
                    Vector2D v = new Vector2D(this.getAgentPosition(f));
                    Vector2D d = this.getGradientAtCoordinate(v);
                   
                    if (!Double.isNaN(d.x) && !Double.isNaN(d.y)) {
                        Vector2D vecTemp = new Vector2D(v);
                        d.mult(3);
                        vecTemp.translate(d);
                        v = this.getPointInVisualization(v);
                        vecTemp = this.getPointInVisualization(vecTemp);
                        d = vecTemp.sub(v);
                       
                        g.fillOval((int) v.x - 5, (int) v.y - 5, 10, 10);
                       
                        Vector2D p1 = this.getPointInVisualization(new Vector2D(
                                planePoint1.x * heightProfileGridWidth + heightProfileBoundingBox.upperLeftCorner().x,
                                planePoint1.y * heightProfileGridHeight + heightProfileBoundingBox.upperLeftCorner().y));
                        Vector2D p2 = this.getPointInVisualization(new Vector2D(
                                planePoint2.x * heightProfileGridWidth + heightProfileBoundingBox.upperLeftCorner().x,
                                planePoint2.y * heightProfileGridHeight + heightProfileBoundingBox.upperLeftCorner().y));
                        Vector2D p3 = this.getPointInVisualization(new Vector2D(
                                planePoint3.x * heightProfileGridWidth + heightProfileBoundingBox.upperLeftCorner().x,
                                planePoint3.y * heightProfileGridHeight + heightProfileBoundingBox.upperLeftCorner().y));
                        g.fillOval((int) p1.x - 2, (int) p1.y - 2, 4, 4);
                        g.fillOval((int) p2.x - 2, (int) p2.y - 2, 4, 4);
                        g.fillOval((int) p3.x - 2, (int) p3.y - 2, 4, 4);
                       
                        g.drawLine((int) v.x, (int) v.y, (int) v.translate(d).x, (int) v.translate(d).y);
                    }
                }
            }
           
            this.zoomBox = this.getCurrentViewBox();
        } else if (!this.getParCollection().getParValueString("showAdditionalInfo").contains("heightProfile")) {
            this.tempStoredHeightImg = null;
        }

        if (tempStoredHeightImg != null) {
            Graphics2D g2;
           
//            if (g1 == null) {
                g2 = img.createGraphics();
//            } else {
//                g2 = g1;
//            }
           
            g2.drawImage(tempStoredHeightImg, 0, 0, null);
//            g2.drawImage(super.getOutsideView(g2), 0, 0, null);
        }
       
        return img;
    }
}
TOP

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

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.