Apocalypse

This commit is contained in:
Skia 2016-06-06 14:09:16 +02:00
parent ff0aebb56a
commit 98ae4e3f33
26 changed files with 23 additions and 3332 deletions

6
.classpath Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path=""/>
</classpath>

17
.project Normal file
View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>VI51-Project</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View file

@ -1,13 +0,0 @@
import gui.Window;
import simulator.Simulator;
public class Launcher {
public static void main(String[] args) {
Simulator sim = new Simulator(400, 400);
Window w = new Window(sim);
w.setVisible(true);
}
}

View file

@ -1,65 +0,0 @@
package gui;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
import gui.drawingTool.DrawingObject;
import math.Point2f;
import math.Rectangle2f;
import math.Vector2f;
import simulator.environment.dataStructure.quadTree.QuadTree;
import simulator.environment.worldObject.WorldObject;
public class GraphicalMap extends JPanel{
/**
*
*/
private static final long serialVersionUID = 3379077206119377471L;
private QuadTree tree;
private Rectangle2f fieldOfView;
private Point2f center;
private float scale = 20;
private ArrayList <DrawingObject> listObj;
public GraphicalMap(QuadTree tree){
super();
this.tree = tree;
this.listObj = new ArrayList<DrawingObject>();
this.fieldOfView = new Rectangle2f(-(float)(this.getWidth())/(float)(2*scale), -(float)(this.getHeight())/(float)(2*scale), (float)(this.getWidth())/(float)(2*scale), (float)(this.getHeight())/(float)(2*scale));
this.center = new Point2f(0, 0);
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getWidth());
g.setColor(Color.GREEN);
for(DrawingObject tempObject : this.listObj){
g.fillOval(tempObject.getCentreX() - tempObject.getWidth()/2, tempObject.getCentreY() - tempObject.getHeight()/2,tempObject.getWidth(),tempObject.getHeight());
}
}
public void setVariable(){
ArrayList <WorldObject> tempList;
this.fieldOfView = new Rectangle2f(-(float)(this.getWidth())/(float)(2*scale), -(float)(this.getHeight())/(float)(2*scale), (float)(this.getWidth())/(float)(2*scale), (float)(this.getHeight())/(float)(2*scale));
this.fieldOfView = this.fieldOfView.translate(new Vector2f(this.center));
if(this.tree != null){
this.listObj.clear();
tempList = this.tree.getObjectInShape(this.fieldOfView);
int x,y,size;
for(WorldObject tempObject : tempList){
x = (int) ((tempObject.getPosition().getX() - this.center.getX()) * this.scale) + this.getWidth()/2;
y = (int) (-(tempObject.getPosition().getY() - this.center.getY()) * this.scale) + this.getHeight()/2;
size = (int) (tempObject.getShape().getRadius() * this.scale*2);
this.listObj.add(new DrawingObject(x,y,size,size));
}
}
}
}

View file

@ -1,106 +0,0 @@
package gui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import simulator.Simulator;
public class Window extends JFrame implements ActionListener, MouseListener {
private static final long serialVersionUID = 7446192599263749847L;
private GraphicalMap gMap;
private JPanel contentPanel;
private Thread t;
public Window (Simulator sim){
super();
this.setTitle("VI51 Project");
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.gMap = new GraphicalMap(sim.getTree());
this.gMap.addMouseListener(this);
this.gMap.setPreferredSize(new Dimension(this.getWidth(), 1000));
contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
contentPanel.add(gMap);
this.setContentPane(contentPanel);
t = new Thread(new Animation());
t.start();
sim.start();
}
/* Thread and repaint function */
private void go(){
while(true){
this.gMap.setVariable();
this.gMap.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Animation implements Runnable {
public void run() {
go();
}
}
/* -----------------------------*/
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}

View file

@ -1,40 +0,0 @@
package gui.drawingTool;
public class DrawingObject{
private int centreX, centreY;
private int width,height;
public DrawingObject(int centreX,int centreY, int width,int height){
this.centreX = centreX;
this.centreY = centreY;
this.width = width;
this.height = height;
}
public int getCentreX() {
return centreX;
}
public void setCentreX(int centreX) {
this.centreX = centreX;
}
public int getCentreY() {
return centreY;
}
public void setCentreY(int centreY) {
this.centreY = centreY;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}

View file

@ -1,147 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
//import com.google.common.base.Objects;
/** 2D Rectangle with floating-point values.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public class Circle2f extends Shape2f<Circle2f> {
private static final long serialVersionUID = -2716047233536640322L;
private final Point2f center = new Point2f();
private float radius;
/**
*/
public Circle2f() {
//
}
/**
* @param center
* @param radius
*/
public Circle2f(Point2f center, float radius) {
this.center.set(center);
this.radius = radius;
}
/**
* @param x
* @param y
* @param radius
*/
public Circle2f(float x, float y, float radius) {
this.center.set(x, y);
this.radius = radius;
}
/*@Override
public boolean equals(Object obj) {
if (obj instanceof Circle2f) {
Circle2f c = (Circle2f) obj;
return Objects.equal(this.center, c.center) && this.radius == c.radius;
}
return false;
}*/
/*@Override
public int hashCode() {
return Objects.hashCode(this.center, this.radius);
}*/
@Override
public Circle2f clone() {
return (Circle2f) super.clone();
}
/** Replies a copy of the center point of this circle.
*
* @return the center point.
*/
public Point2f getCenter() {
return this.center.clone();
}
/** Replies the radius of the circle.
*
* @return the radius.
*/
public float getRadius() {
return this.radius;
}
@Override
public String toString() {
return "[" + this.center.toString() + "-" + this.radius + "]";
}
/** Replies if an intersection exists between this rectangle and the given shape.
*
* @param s - the shape.
* @return <code>true</code> if an intersection exists.
*/
public boolean intersects(Shape2f<?> s) {
if (s instanceof Circle2f) {
Circle2f r = (Circle2f) s;
float x = r.getCenter().getX() - this.center.getX();
float y = r.getCenter().getY() - this.center.getY();
return ((x*x) + (y*y)) < ((this.radius + r.radius) * (this.radius + r.radius));
}
if (s instanceof Rectangle2f) {
Rectangle2f r = (Rectangle2f) s;
return r.intersects(this);
}
if (s instanceof MotionHull2f) {
return ((MotionHull2f) s).intersects(this);
}
throw new IllegalArgumentException();
}
@Override
public Circle2f translate(Tuple2f<?> vector) {
return new Circle2f(
this.center.getX() + vector.getX(),
this.center.getY() + vector.getY(),
this.radius);
}
@Override
public Rectangle2f getBounds() {
return new Rectangle2f(
this.center.getX() - this.radius,
this.center.getY() - this.radius,
this.center.getX() + this.radius,
this.center.getY() + this.radius);
}
@Override
public float getMaxDemiSize() {
return this.radius;
}
}

View file

@ -1,285 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
/** Mathematic and geometric utilities.
*
* Copied from {@link https://github.com/gallandarakhneorg/afc/}.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public class MathUtil {
/** PI
*/
public static final float PI = (float)Math.PI;
/** E
*/
public static final float E = (float)Math.E;
/** Epsilon value, smallest such that 1.0+EPSILON != 1.0
* <p>
* Given by the Java3D's implementation of the Matrix3d class.
*/
public static final float EPSILON = (float)1.110223024E-16;
/** 2 * PI
*/
public static final float TWO_PI = 2f * PI;
/**
* Compute the signed angle between two vectors.
*
* @param x1
* @param y1
* @param x2
* @param y2
* @return the angle between <code>-PI</code> and <code>PI</code>.
*/
public static float signedAngle(float x1, float y1, float x2, float y2) {
float length1 = (float)Math.sqrt(x1 * x1 + y1 * y1);
float length2 = (float)Math.sqrt(x2 * x2 + y2 * y2);
if ((length1 == 0f) || (length2 == 0f))
return Float.NaN;
float cx1 = x1;
float cy1 = y1;
float cx2 = x2;
float cy2 = y2;
// A and B are normalized
if (length1 != 1f) {
cx1 /= length1;
cy1 /= length1;
}
if (length2 != 1f) {
cx2 /= length2;
cy2 /= length2;
}
/*
* // First method // Angle // A . B = |A|.|B|.cos(theta) = cos(theta) float dot = x1 * x2 + y1 * y2; float angle = Math.acos(dot);
*
* // On which side of A, B is located? if ((dot > -1) && (dot < 1)) { dot = MathUtil.determinant(x2, y2, x1, y1); if (dot < 0) angle = -angle; }
*/
// Second method
// A . B = |A|.|B|.cos(theta) = cos(theta)
float cos = cx1 * cx2 + cy1 * cy2;
// A x B = |A|.|B|.sin(theta).N = sin(theta) (where N is the unit vector perpendicular to plane AB)
float sin = cx1 * cy2 - cy1 * cx2;
float angle = (float)Math.atan2(sin, cos);
return angle;
}
/** Clamp the given angle in radians to {@code [0;2PI)}.
*
* @param radians is the angle to clamp
* @return the angle in {@code [0;2PI)} range.
*/
public static float clampRadian(float radians) {
return clampRadian(radians, 0f, TWO_PI);
}
/** Clamp the given angle in radians.
*
* @param radians is the angle to clamp
* @param min is the min value of the range.
* @param max is the max value of the range.
* @return the angle in the given range.
*/
public static float clampRadian(float radians, float min, float max) {
float r = radians;
while (r<min) r += TWO_PI;
while (r>=max) r -= TWO_PI;
return r;
}
/** Clamp the given value to the given range.
* <p>
* If the value is outside the {@code [min;max]}
* range, it is clamp to the nearest bounding value
* <var>min</var> or <var>max</var>.
*
* @param v is the value to clamp.
* @param min is the min value of the range.
* @param max is the max value of the range.
* @return the value in {@code [min;max]} range.
*/
public static float clamp(float v, float min, float max) {
if (min<max) {
if (v<min) return min;
if (v>max) return max;
}
else {
if (v>min) return min;
if (v<max) return max;
}
return v;
}
/** Replies the min value in the given values.
*
* @return the min value.
*/
public static float min(float... values) {
float min = values[0];
for (int i = 1; i < values.length; ++i) {
if (min > values[i]) {
min = values[i];
}
}
return min;
}
/** Replies the max value in the given values.
*
* @return the max value.
*/
public static float max(float... values) {
float max = values[0];
for (int i = 1; i < values.length; ++i) {
if (max < values[i]) {
max = values[i];
}
}
return max;
}
/**
* Replies the projection a point on a segment.
*
* @param p is the coordinate of the point to project
* @param s1 is the x-coordinate of the first line point.
* @param s2 is the x-coordinate of the second line point.
* @return the projection of the specified point on the line. If
* equal to {@code 0}, the projection is equal to the first segment point.
* If equal to {@code 1}, the projection is equal to the second segment point.
* If inside {@code ]0;1[}, the projection is between the two segment points.
* If inside {@code ]-inf;0[}, the projection is outside on the side of the
* first segment point. If inside {@code ]1;+inf[}, the projection is
* outside on the side of the second segment point.
*/
public static float projectsPointOnLine(Point2f p, Point2f s1, Point2f s2) {
float r_numerator = (p.getX()-s1.getX())*(s2.getX()-s1.getX()) + (p.getY()-s1.getY())*(s2.getY()-s1.getY());
float r_denomenator = (s2.getX()-s1.getX())*(s2.getX()-s1.getX()) + (s2.getY()-s1.getY())*(s2.getY()-s1.getY());
return r_numerator / r_denomenator;
}
private static float determinant(Tuple2f<?> a, Tuple2f<?> b) {
return a.getX()*b.getY() - b.getX()*a.getY();
}
/**
* Replies one position factor for the intersection point between two lines.
* <p>
* Let line equations for L1 and L2:<br>
* <code>L1: P1 + factor1 * (P2-P1)</code><br>
* <code>L2: P3 + factor2 * (P4-P3)</code><br>
* If lines are intersecting, then<br>
* <code>P1 + factor1 * (P2-P1) = P3 + factor2 * (P4-P3)</code>
* <p>
* This function computes and replies <code>factor1</code>.
*
* @param p1
* is the coordinates of the first point of the first segment.
* @param p2
* is the coordinates of the second point of the first segment.
* @param p3
* is the coordinates of the first point of the second segment.
* @param p4
* is the coordinates of the second point of the second segment.
* @return <code>factor1</code> or {@link Float#NaN} if no intersection.
*/
public static float getSegmentSegmentIntersectionFactor(Point2f p1, Point2f p2, Point2f p3, Point2f p4) {
Vector2f v1 = p2.operator_minus(p1);
Vector2f v2 = p4.operator_minus(p3);
// determinant is zero when parallel = det(L1,L2)
float det = determinant(v1, v2);
if (det == 0f) return Float.NaN;
// Given line equations:
// Pa = P1 + ua (P2-P1), and
// Pb = P3 + ub (P4-P3)
// and
// V = (P1-P3)
// then
// ua = det(L2,V) / det(L1,L2)
// ub = det(L1,V) / det(L1,L2)
Vector2f v3 = p1.operator_minus(p3);
float u = determinant(v1, v3) / det;
if (u < 0. || u > 1.) return Float.NaN;
u = determinant(v2, v3) / det;
return (u < 0. || u > 1.) ? Float.NaN : u;
}
/** Compute the distance between a point and a segment.
*
* @param p position of the point.
* @param s1 position of the first point of the segment.
* @param s2 position of the second point of the segment.
* @return the distance beetween the point and the segment.
*/
public static final float distancePointToSegment(Point2f p, Point2f s1, Point2f s2) {
float r_denomenator = (s2.getX()-s1.getX())*(s2.getX()-s1.getX()) + (s2.getY()-s1.getY())*(s2.getY()-s1.getY());
if (r_denomenator==0f) return p.distance(s1);
float r_numerator = (p.getX()-s1.getX())*(s2.getX()-s1.getX()) + (p.getY()-s1.getY())*(s2.getY()-s1.getY());
float ratio = r_numerator / r_denomenator;
if (ratio<=0.) {
return (float)Math.sqrt((p.getX()-s1.getX())*(p.getX()-s1.getX()) + (p.getY()-s1.getY())*(p.getY()-s1.getY()));
}
if (ratio>=1f) {
return (float)Math.sqrt((p.getX()-s2.getX())*(p.getX()-s2.getX()) + (p.getY()-s2.getY())*(p.getY()-s2.getY()));
}
float s = ((s1.getY()-p.getY())*(s2.getX()-s1.getX())-(s1.getX()-p.getX())*(s2.getY()-s1.getY()) ) / r_denomenator;
return (float)(Math.abs(s) * Math.sqrt(r_denomenator));
}
/** Compute the distance between two segments.
*
* @param p position of the point.
* @param s1 position of the first point of the segment.
* @param s2 position of the second point of the segment.
* @return the distance beetween the segments.
*/
public static final float distanceSegmentToSegment(Point2f s1, Point2f s2, Point2f s3, Point2f s4) {
float f = getSegmentSegmentIntersectionFactor(s1, s2, s3, s4);
if (!Float.isNaN(f)) {
return 0f;
}
float d1 = distancePointToSegment(s1, s3, s4);
float d2 = distancePointToSegment(s2, s3, s4);
float d3 = distancePointToSegment(s3, s1, s2);
float d4 = distancePointToSegment(s4, s1, s2);
return MathUtil.min(d1, d2, d3, d4);
}
}

View file

@ -1,190 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
import java.lang.ref.SoftReference;
//import com.google.common.base.Objects;
/** 2D Path with floating-point values.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public class MotionHull2f extends Shape2f<MotionHull2f> {
private static final long serialVersionUID = -2716047233536640322L;
private final Point2f start = new Point2f();
private final Vector2f direction = new Vector2f();
private final float size;
private SoftReference<Rectangle2f> bounds = null;
/**
* @param point
* @param vector
* @param lateralSize
*/
public MotionHull2f(Point2f point, Vector2f vector, float lateralSize) {
this.start.set(point);
this.direction.set(vector);
this.size = lateralSize;
}
/*@Override
public boolean equals(Object obj) {
if (obj instanceof MotionHull2f) {
MotionHull2f r = (MotionHull2f) obj;
return Objects.equal(this.start, r.start) && Objects.equal(this.direction, r.direction);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(this.start, this.direction);
}*/
@Override
public MotionHull2f clone() {
return (MotionHull2f) super.clone();
}
/** Replies a copy of the start point of this path.
*
* @return the start point.
*/
public Point2f getStart() {
return this.start.clone();
}
/** Replies a copy of the direction of this path.
*
* @return the upper point.
*/
public Vector2f getDirection() {
return this.direction.clone();
}
@Override
public String toString() {
return "[" + this.start.toString() + "-" + this.direction.toString() + "]";
}
/** Replies if an intersection exists between this rectangle and the given shape.
*
* @param s - the shape.
* @return <code>true</code> if an intersection exists.
*/
public boolean intersects(Shape2f<?> s) {
if (s instanceof MotionHull2f) {
MotionHull2f p = (MotionHull2f) s;
Point2f end = this.start.clone();
end.add(this.direction);
Point2f end2 = p.start.clone();
end2.add(p.direction);
float d = MathUtil.distanceSegmentToSegment(
this.start, end, p.start, end2);
return d < (this.size + p.size);
}
if (s instanceof Rectangle2f) {
Rectangle2f r = (Rectangle2f) s;
Point2f end = this.start.clone();
end.add(this.direction);
Point2f pa = new Point2f(r.getLower().getX(), r.getUpper().getY());
Point2f pb = new Point2f(r.getUpper().getX(), r.getLower().getY());
float d1 = MathUtil.distanceSegmentToSegment(
r.getLower(), pa, this.start, end);
float d2 = MathUtil.distanceSegmentToSegment(
pa, r.getUpper(), this.start, end);
float d3 = MathUtil.distanceSegmentToSegment(
r.getUpper(), pb, this.start, end);
float d4 = MathUtil.distanceSegmentToSegment(
pb, r.getLower(), this.start, end);
float d = MathUtil.min(d1, d2, d3, d4);
return d < this.size;
}
if (s instanceof Circle2f) {
Circle2f c = (Circle2f) s;
Point2f center = c.getCenter();
Point2f end = this.start.clone();
end.add(this.direction);
return MathUtil.distancePointToSegment(center, this.start, end) < (this.size + c.getRadius());
}
throw new IllegalArgumentException();
}
/** Replies the center point of the rectangle.
*
* @return the center point.
*/
public Point2f getCenter() {
return new Point2f(
this.start.getX() + this.direction.getX() / 2f,
this.start.getY() + this.direction.getY() / 2f);
}
@Override
public MotionHull2f translate(Tuple2f<?> vector) {
return new MotionHull2f(new Point2f(
this.start.getX() + vector.getX(),
this.start.getY() + vector.getY()),
this.direction.clone(),
this.size);
}
@Override
public Rectangle2f getBounds() {
Rectangle2f bb = this.bounds == null ? null : this.bounds.get();
if (bb == null) {
float x = this.start.getX() + this.direction.getX();
float y = this.start.getY() + this.direction.getY();
Vector2f d = this.direction.clone();
d.turn(MathUtil.PI / 2f);
float x1 = this.start.getX() + d.getX();
float y1 = this.start.getY() + d.getY();
float x2 = this.start.getX() - d.getX();
float y2 = this.start.getY() - d.getY();
float x3 = x + d.getX();
float y3 = y + d.getY();
float x4 = x - d.getX();
float y4 = y - d.getY();
bb = new Rectangle2f(
MathUtil.min(x1, x2, x3, x4),
MathUtil.min(y1, y2, y3, y4),
MathUtil.max(x1, x2, x3, x4),
MathUtil.max(y1, y2, y3, y4));
this.bounds = new SoftReference<Rectangle2f>(bb);
}
return bb;
}
@Override
public float getMaxDemiSize() {
return this.size;
}
}

View file

@ -1,384 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
/** 2D Point with 2 floating-point numbers.
*
* Copied from {@link https://github.com/gallandarakhneorg/afc/}.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public class Point2f extends Tuple2f<Point2f> {
private static final long serialVersionUID = 8963319137253544821L;
/**
*/
public Point2f() {
//
}
/**
* @param tuple is the tuple to copy.
*/
public Point2f(Tuple2f<?> tuple) {
super(tuple);
}
/**
* @param tuple is the tuple to copy.
*/
public Point2f(int[] tuple) {
super(tuple);
}
/**
* @param tuple is the tuple to copy.
*/
public Point2f(float[] tuple) {
super(tuple);
}
/**
* @param x
* @param y
*/
public Point2f(int x, int y) {
super(x,y);
}
/**
* @param x
* @param y
*/
public Point2f(float x, float y) {
super(x,y);
}
/**
* @param x
* @param y
*/
public Point2f(double x, double y) {
super((float)x,(float)y);
}
/**
* @param x
* @param y
*/
public Point2f(long x, long y) {
super(x,y);
}
/**
* Computes the square of the distance between this point and point p1.
* @param p1 the other point
* @return the distance.
*/
public float distanceSquared(Point2f p1) {
float dx, dy;
dx = this.x-p1.getX();
dy = this.y-p1.getY();
return (dx*dx+dy*dy);
}
/**
* Computes the distance between this point and point p1.
* @param p1 the other point
* @return the distance.
*/
public float distance(Point2f p1) {
float dx, dy;
dx = this.x-p1.getX();
dy = this.y-p1.getY();
return (float)Math.sqrt(dx*dx+dy*dy);
}
/**
* Computes the L-1 (Manhattan) distance between this point and
* point p1. The L-1 distance is equal to abs(x1-x2) + abs(y1-y2).
* @param p1 the other point
* @return the distance.
*/
public float distanceL1(Point2f p1) {
return (Math.abs(this.x-p1.getX()) + Math.abs(this.y-p1.getY()));
}
/**
* Computes the L-infinite distance between this point and
* point p1. The L-infinite distance is equal to
* MAX[abs(x1-x2), abs(y1-y2)].
* @param p1 the other point
* @return the distance.
*/
public float distanceLinf(Point2f p1) {
return Math.max( Math.abs(this.x-p1.getX()), Math.abs(this.y-p1.getY()));
}
/**
* Sets the value of this tuple to the sum of tuples t1 and t2.
* @param t1 the first tuple
* @param t2 the second tuple
*/
public void add(Point2f t1, Vector2f t2) {
this.x = t1.getX() + t2.getX();
this.y = t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the sum of tuples t1 and t2.
* @param t1 the first tuple
* @param t2 the second tuple
*/
public void add(Vector2f t1, Point2f t2) {
this.x = t1.getX() + t2.getX();
this.y = t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the sum of itself and t1.
* @param t1 the other tuple
*/
public void add(Vector2f t1) {
this.x += t1.getX();
this.y += t1.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1 plus tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
public void scaleAdd(int s, Vector2f t1, Point2f t2) {
this.x = s * t1.getX() + t2.getX();
this.y = s * t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1 plus tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
public void scaleAdd(float s, Vector2f t1, Point2f t2) {
this.x = s * t1.getX() + t2.getX();
this.y = s * t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1 plus tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
public void scaleAdd(int s, Point2f t1, Vector2f t2) {
this.x = s * t1.getX() + t2.getX();
this.y = s * t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1 plus tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
public void scaleAdd(float s, Point2f t1, Vector2f t2) {
this.x = s * t1.getX() + t2.getX();
this.y = s * t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of itself and then adds tuple t1 (this = s*this + t1).
* @param s the scalar value
* @param t1 the tuple to be added
*/
public void scaleAdd(int s, Vector2f t1) {
this.x = s * this.x + t1.getX();
this.y = s * this.y + t1.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of itself and then adds tuple t1 (this = s*this + t1).
* @param s the scalar value
* @param t1 the tuple to be added
*/
public void scaleAdd(float s, Vector2f t1) {
this.x = s * this.x + t1.getX();
this.y = s * this.y + t1.getY();
}
/**
* Sets the value of this tuple to the difference
* of tuples t1 and t2 (this = t1 - t2).
* @param t1 the first tuple
* @param t2 the second tuple
*/
public void sub(Point2f t1, Vector2f t2) {
this.x = t1.getX() - t2.getX();
this.y = t1.getY() - t2.getY();
}
/**
* Sets the value of this tuple to the difference
* of itself and t1 (this = this - t1).
* @param t1 the other tuple
*/
public void sub(Vector2f t1) {
this.x -= t1.getX();
this.y -= t1.getY();
}
/** {@inheritDoc}
*/
@Override
public Point2f clone() {
return (Point2f)super.clone();
}
/** Sum of vectors: r = this + v.
*
* @param v the vector
* @return the result.
* @see #add(Point2f, Vector2f)
*/
public Point2f operator_plus(Vector2f v) {
Point2f r = new Point2f();
r.add(this, v);
return r;
}
/** Sum of vectors: this += v.
* It is equivalent to {@code this.add(v)}.
*
* @param v the vector
* @return the result.
* @see #add(Vector2f)
*/
public void operator_add(Vector2f v) {
add(v);
}
/** Subtract of vectors: r = this - v.
*
* @param v the vector
* @return the result.
* @see #sub(Point2f, Vector2f)
*/
public Point2f operator_minus(Vector2f v) {
Point2f r = new Point2f();
r.sub(this, v);
return r;
}
/** Compute a vectors: r = this - p.
*
* @param p the point
* @return the vector from the p to this.
* @see Vector2f#sub(Point2f, Point2f)
*/
public Vector2f operator_minus(Point2f p) {
Vector2f r = new Vector2f();
r.sub(this, p);
return r;
}
/** Subtract of vectors: this -= v.
* It is equivalent to {@code this.sub(v)}.
*
* @param v the vector
* @return the result.
* @see #sub(Vector2f)
*/
public void operator_remove(Vector2f v) {
sub(v);
}
/** Replies if the vectors are equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_equals(Vector2f v) {
return equals(v);
}
/** Replies if the vectors are not equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_notEquals(Vector2f v) {
return !equals(v);
}
/** Replies if the vectors are equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_tripleEquals(Vector2f v) {
return equals(v);
}
/** Replies if the vectors are not equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_tripleNotEquals(Vector2f v) {
return !equals(v);
}
/** Replies if the distance between this and v
*
* @param v the vector.
* @return the distance.
* @see #distance(Point2f)
*/
public float operator_upTo(Point2f v) {
return distance(v);
}
/** If the tuple is nul then b else a.
*
* @param v the vector.
* @return the vector.
* @see #epsilonNul(float)
* @see MathUtil#EPSILON
*/
public Point2f operator_elvis(Point2f v) {
if (epsilonNul(MathUtil.EPSILON)) {
return v;
}
return this;
}
}

View file

@ -1,190 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
//import com.google.common.base.Objects;
/** 2D Rectangle with floating-point values.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public class Rectangle2f extends Shape2f<Rectangle2f> {
private static final long serialVersionUID = -2716047233536640322L;
private final Point2f lower = new Point2f();
private final Point2f upper = new Point2f();
/**
*/
public Rectangle2f() {
//
}
/**
* @param p1
* @param p2
*/
public Rectangle2f(Point2f p1, Point2f p2) {
this.lower.set(
Math.min(p1.getX(), p2.getX()),
Math.min(p1.getY(), p2.getY()));
this.upper.set(
Math.max(p1.getX(), p2.getX()),
Math.max(p1.getY(), p2.getY()));
}
/**
* @param x1
* @param y1
* @param x2
* @param y2
*/
public Rectangle2f(float x1, float y1, float x2, float y2) {
this.lower.set(
Math.min(x1, x2),
Math.min(y1, y2));
this.upper.set(
Math.max(x1, x2),
Math.max(y1, y2));
}
/*@Override
public boolean equals(Object obj) {
if (obj instanceof Rectangle2f) {
Rectangle2f r = (Rectangle2f) obj;
return Objects.equal(this.lower, r.lower) && Objects.equal(this.upper, r.upper);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(this.lower, this.upper);
}
*/
@Override
public Rectangle2f clone() {
return (Rectangle2f) super.clone();
}
/** Replies a copy of the lower point of this rectangle.
*
* @return the lower point.
*/
public Point2f getLower() {
return this.lower.clone();
}
/** Replies a copy of the upper point of this rectangle.
*
* @return the upper point.
*/
public Point2f getUpper() {
return this.upper.clone();
}
@Override
public String toString() {
return "[" + this.lower.toString() + "-" + this.upper.toString() + "]";
}
/** Replies if an intersection exists between this rectangle and the given shape.
*
* @param s - the shape.
* @return <code>true</code> if an intersection exists.
*/
public boolean intersects(Shape2f<?> s) {
if (s instanceof Rectangle2f) {
Rectangle2f r = (Rectangle2f) s;
return intersects(this.lower.getX(), this.upper.getX(), r.lower.getX(), r.upper.getX())
&& intersects(this.lower.getY(), this.upper.getY(), r.lower.getY(), r.upper.getY());
}
if (s instanceof Circle2f) {
Circle2f c = (Circle2f) s;
Point2f center = c.getCenter();
float x = MathUtil.clamp(center.getX(), this.lower.getX(), this.upper.getX());
float y = MathUtil.clamp(center.getY(), this.lower.getY(), this.upper.getY());
x -= center.getX();
y -= center.getY();
float radius = c.getRadius();
return (x*x+y*y) < (radius * radius);
}
if (s instanceof MotionHull2f) {
return ((MotionHull2f) s).intersects(this);
}
throw new IllegalArgumentException();
}
private boolean intersects(float a1, float a2, float b1, float b2) {
assert (a1 <= a2);
assert (b1 <= b2);
return (a2 > b1) && (b2 > a1);
}
/** Replies the center point of the rectangle.
*
* @return the center point.
*/
public Point2f getCenter() {
return new Point2f(
(this.lower.getX() + this.upper.getX()) / 2f,
(this.lower.getY() + this.upper.getY()) / 2f);
}
@Override
public Rectangle2f translate(Tuple2f<?> vector) {
return new Rectangle2f(
this.lower.getX() + vector.getX(),
this.lower.getY() + vector.getY(),
this.upper.getX() + vector.getX(),
this.upper.getY() + vector.getY());
}
@Override
public Rectangle2f getBounds() {
return clone();
}
/** Replies the width of the rectangle.
*
* @return the width.
*/
public float getWidth() {
return this.upper.getX() - this.lower.getX();
}
/** Replies the height of the rectangle.
*
* @return the height.
*/
public float getHeight() {
return this.upper.getY() - this.lower.getY();
}
@Override
public float getMaxDemiSize() {
return Math.max(getWidth(), getHeight()) / 2f;
}
}

View file

@ -1,76 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
import java.io.Serializable;
/** Abstract implementation of a shape.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public abstract class Shape2f<S extends Shape2f<S>> implements Serializable, Cloneable {
private static final long serialVersionUID = 2348511614700927051L;
/**
*/
public Shape2f() {
//
}
@SuppressWarnings("unchecked")
@Override
public S clone() {
try {
return (S) super.clone();
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
}
/** Replies if an intersection exists between this rectangle and the given rectangle.
*
* @param r - the rectangle.
* @return <code>true</code> if an intersection exists.
*/
public abstract boolean intersects(Shape2f<?> r);
/** Create a clone of this shape with a translation.
*
* @param vector the translation.
* @return the clone.
*/
public abstract S translate(Tuple2f<?> vector);
/** Replies the bounds for this shape.
*
* @return the bounds of the shape.
*/
public abstract Rectangle2f getBounds();
/** Replies the biggest demi-size of the shape.
*
* @return the biggest demi-size of the shape.
*/
public abstract float getMaxDemiSize();
}

View file

@ -1,622 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
import java.io.Serializable;
//import com.google.common.base.Objects;
/** 2D tuple with 2 floating-point numbers.
*
* Copied from {@link https://github.com/gallandarakhneorg/afc/}.
*
* @param <T> is the implementation type of the tuple.
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public abstract class Tuple2f<T extends Tuple2f<?>> implements Serializable, Cloneable, Comparable<T> {
private static final long serialVersionUID = 6447733811545555778L;
/** x coordinate.
*/
protected float x;
/** y coordinate.
*/
protected float y;
/**
*/
public Tuple2f() {
this.x = this.y = 0;
}
/**
* @param tuple is the tuple to copy.
*/
public Tuple2f(Tuple2f<?> tuple) {
this.x = tuple.getX();
this.y = tuple.getY();
}
/**
* @param tuple is the tuple to copy.
*/
public Tuple2f(int[] tuple) {
this.x = tuple[0];
this.y = tuple[1];
}
/**
* @param tuple is the tuple to copy.
*/
public Tuple2f(float[] tuple) {
this.x = tuple[0];
this.y = tuple[1];
}
/**
* @param x
* @param y
*/
public Tuple2f(int x, int y) {
this.x = x;
this.y = y;
}
/**
* @param x
* @param y
*/
public Tuple2f(float x, float y) {
this.x = x;
this.y = y;
}
/** Clone this point.
*
* @return the clone.
*/
@SuppressWarnings("unchecked")
public T clone() {
try {
return (T) super.clone();
}
catch(CloneNotSupportedException e) {
throw new Error(e);
}
}
/**
* Sets each component of this tuple to its absolute value.
*/
public void absolute() {
this.x = Math.abs(this.x);
this.y = Math.abs(this.y);
}
/**
* Sets each component of the tuple parameter to its absolute
* value and places the modified values into this tuple.
* @param t the source tuple, which will not be modified
*/
public void absolute(T t) {
t.set(Math.abs(this.x), Math.abs(this.y));
}
/**
* Sets the value of this tuple to the sum of itself and x and y.
* @param x
* @param y
*/
public void add(int x, int y) {
this.x += x;
this.y += y;
}
/**
* Sets the value of this tuple to the sum of itself and x and y.
* @param x
* @param y
*/
public void add(float x, float y) {
this.x += x;
this.y += y;
}
/**
* Sets the x value of this tuple to the sum of itself and x.
* @param x
*/
public void addX(int x) {
this.x += x;
}
/**
* Sets the x value of this tuple to the sum of itself and x.
* @param x
*/
public void addX(float x) {
this.x += x;
}
/**
* Sets the y value of this tuple to the sum of itself and y.
* @param y
*/
public void addY(int y) {
this.y += y;
}
/**
* Sets the y value of this tuple to the sum of itself and y.
* @param y
*/
public void addY(float y) {
this.y += y;
}
/**
* Clamps this tuple to the range [low, high].
* @param min the lowest value in this tuple after clamping
* @param max the highest value in this tuple after clamping
*/
public void clamp(int min, int max) {
if (this.x < min) this.x = min;
else if (this.x > max) this.x = max;
if (this.y < min) this.y = min;
else if (this.y > max) this.y = max;
}
/**
* Clamps this tuple to the range [low, high].
* @param min the lowest value in this tuple after clamping
* @param max the highest value in this tuple after clamping
*/
public void clamp(float min, float max) {
if (this.x < min) this.x = min;
else if (this.x > max) this.x = max;
if (this.y < min) this.y = min;
else if (this.y > max) this.y = max;
}
/**
* Clamps the minimum value of this tuple to the min parameter.
* @param min the lowest value in this tuple after clamping
*/
public void clampMin(int min) {
if (this.x < min) this.x = min;
if (this.y < min) this.y = min;
}
/**
* Clamps the minimum value of this tuple to the min parameter.
* @param min the lowest value in this tuple after clamping
*/
public void clampMin(float min) {
if (this.x < min) this.x = min;
if (this.y < min) this.y = min;
}
/**
* Clamps the maximum value of this tuple to the max parameter.
* @param max the highest value in the tuple after clamping
*/
public void clampMax(int max) {
if (this.x > max) this.x = max;
if (this.y > max) this.y = max;
}
/**
* Clamps the maximum value of this tuple to the max parameter.
* @param max the highest value in the tuple after clamping
*/
public void clampMax(float max) {
if (this.x > max) this.x = max;
if (this.y > max) this.y = max;
}
/**
* Copies the values of this tuple into the tuple t.
* @param t is the target tuple
*/
public void get(T t) {
t.set(this.x, this.y);
}
/**
* Copies the value of the elements of this tuple into the array t.
* @param t the array that will contain the values of the vector
*/
public void get(int[] t) {
t[0] = (int)this.x;
t[1] = (int)this.y;
}
/**
* Copies the value of the elements of this tuple into the array t.
* @param t the array that will contain the values of the vector
*/
public void get(float[] t) {
t[0] = this.x;
t[1] = this.y;
}
/**
* Sets the value of this tuple to the negation of tuple t1.
* @param t1 the source tuple
*/
public void negate(T t1) {
this.x = -t1.getX();
this.y = -t1.getY();
}
/**
* Negates the value of this tuple in place.
*/
public void negate() {
this.x = -this.x;
this.y = -this.y;
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1.
* @param s the scalar value
* @param t1 the source tuple
*/
public void scale(int s, T t1) {
this.x = s * t1.getX();
this.y = s * t1.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1.
* @param s the scalar value
* @param t1 the source tuple
*/
public void scale(float s, T t1) {
this.x = (s * t1.getX());
this.y = (s * t1.getY());
}
/**
* Sets the value of this tuple to the scalar multiplication
* of the scale factor with this.
* @param s the scalar value
*/
public void scale(int s) {
this.x = s * this.x;
this.y = s * this.y;
}
/**
* Sets the value of this tuple to the scalar multiplication
* of the scale factor with this.
* @param s the scalar value
*/
public void scale(float s) {
this.x = (s * this.x);
this.y = (s * this.y);
}
/**
* Sets the value of this tuple to the value of tuple t1.
* @param t1 the tuple to be copied
*/
public void set(Tuple2f<?> t1) {
this.x = t1.getX();
this.y = t1.getY();
}
/**
* Sets the value of this tuple to the specified x and y
* coordinates.
* @param x the x coordinate
* @param y the y coordinate
*/
public void set(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Sets the value of this tuple to the specified x and y
* coordinates.
* @param x the x coordinate
* @param y the y coordinate
*/
public void set(float x, float y) {
this.x = x;
this.y = y;
}
/**
* Sets the value of this tuple from the 2 values specified in
* the array.
* @param t the array of length 2 containing xy in order
*/
public void set(int[] t) {
this.x = t[0];
this.y = t[1];
}
/**
* Sets the value of this tuple from the 2 values specified in
* the array.
* @param t the array of length 2 containing xy in order
*/
public void set(float[] t) {
this.x = t[0];
this.y = t[1];
}
/**
* Get the <i>x</i> coordinate.
*
* @return the x coordinate.
*/
public float getX() {
return this.x;
}
/**
* Set the <i>x</i> coordinate.
*
* @param x value to <i>x</i> coordinate.
*/
public void setX(int x) {
this.x = x;
}
/**
* Set the <i>x</i> coordinate.
*
* @param x value to <i>x</i> coordinate.
*/
public void setX(float x) {
this.x = x;
}
/**
* Get the <i>y</i> coordinate.
*
* @return the <i>y</i> coordinate.
*/
public float getY() {
return this.y;
}
/**
* Set the <i>y</i> coordinate.
*
* @param y value to <i>y</i> coordinate.
*/
public void setY(int y) {
this.y = y;
}
/**
* Set the <i>y</i> coordinate.
*
* @param y value to <i>y</i> coordinate.
*/
public void setY(float y) {
this.y = y;
}
/**
* Sets the value of this tuple to the difference of itself and x and y.
* @param x
* @param y
*/
public void sub(int x, int y) {
this.x -= x;
this.y -= y;
}
/**
* Sets the value of this tuple to the difference of itself and x and y.
* @param x
* @param y
*/
public void sub(float x, float y) {
this.x -= x;
this.y -= y;
}
/**
* Sets the x value of this tuple to the difference of itself and x.
* @param x
*/
public void subX(int x) {
this.x -= x;
}
/**
* Sets the x value of this tuple to the difference of itself and x.
* @param x
*/
public void subX(float x) {
this.x -= x;
}
/**
* Sets the y value of this tuple to the difference of itself and y.
* @param y
*/
public void subY(int y) {
this.y -= y;
}
/**
* Sets the y value of this tuple to the difference of itself and y.
* @param y
*/
public void subY(float y) {
this.y -= y;
}
/**
* Linearly interpolates between tuples t1 and t2 and places the
* result into this tuple: this = (1-alpha)*t1 + alpha*t2.
* @param t1 the first tuple
* @param t2 the second tuple
* @param alpha the alpha interpolation parameter
*/
public void interpolate(T t1, T t2, float alpha) {
this.x = ((1f-alpha)*t1.getX() + alpha*t2.getX());
this.y = ((1f-alpha)*t1.getY() + alpha*t2.getY());
}
/**
* Linearly interpolates between this tuple and tuple t1 and
* places the result into this tuple: this = (1-alpha)*this + alpha*t1.
* @param t1 the first tuple
* @param alpha the alpha interpolation parameter
*/
public void interpolate(T t1, float alpha) {
this.x = ((1f-alpha)*this.x + alpha*t1.getX());
this.y = ((1f-alpha)*this.y + alpha*t1.getY());
}
/**
* Returns true if all of the data members of Tuple2f t1 are
* equal to the corresponding data members in this Tuple2f.
* @param t1 the vector with which the comparison is made
* @return true or false
*/
public boolean equals(Tuple2f<?> t1) {
try {
return(this.x == t1.getX() && this.y == t1.getY());
}
catch (NullPointerException e2) {
return false;
}
}
/**
* Returns true if the Object t1 is of type Tuple2f and all of the
* data members of t1 are equal to the corresponding data members in
* this Tuple2f.
* @param t1 the object with which the comparison is made
* @return true or false
*/
@SuppressWarnings("unchecked")
@Override
public boolean equals(Object t1) {
try {
T t2 = (T) t1;
return(this.x == t2.getX() && this.y == t2.getY());
}
catch(AssertionError e) {
throw e;
}
catch (Throwable e2) {
return false;
}
}
/**
* Returns true if the L-infinite distance between this tuple
* and tuple t1 is less than or equal to the epsilon parameter,
* otherwise returns false. The L-infinite
* distance is equal to MAX[abs(x1-x2), abs(y1-y2)].
* @param t1 the tuple to be compared to this tuple
* @param epsilon the threshold value
* @return true or false
*/
public boolean epsilonEquals(T t1, float epsilon) {
float diff;
diff = this.x - t1.getX();
if(Float.isNaN(diff)) return false;
if((diff<0f?-diff:diff) > epsilon) return false;
diff = this.y - t1.getY();
if(Float.isNaN(diff)) return false;
if((diff<0f?-diff:diff) > epsilon) return false;
return true;
}
/**
* Returns true if the L-infinite distance between this tuple
* and the zero tuple is less than or equal to the epsilon parameter,
* otherwise returns false. The L-infinite
* distance is equal to MAX[abs(x1), abs(y1)].
* @param epsilon the threshold value
* @return true or false
*/
public boolean epsilonNul(float epsilon) {
if((this.x<0f?-this.x:this.x) > epsilon) return false;
if((this.y<0f?-this.y:this.y) > epsilon) return false;
return true;
}
/**
* Returns a hash code value based on the data values in this
* object. Two different Tuple2f objects with identical data values
* (i.e., Tuple2f.equals returns true) will return the same hash
* code value. Two objects with different data members may return the
* same hash value, although this is not likely.
* @return the integer hash code value
*/
/*@Override
public int hashCode() {
return Objects.hashCode(this.x, this.y);
}*/
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "(" //$NON-NLS-1$
+this.x
+";" //$NON-NLS-1$
+this.y
+")"; //$NON-NLS-1$
}
@Override
public int compareTo(T o) {
if (o == null) {
return Integer.MAX_VALUE;
}
int c = Float.compare(this.x, o.x);
if (c != 0) {
return c;
}
return Float.compare(this.y, o.y);
}
/** Replies if the tuple has zero coordinates.
*
* @return <code>true</code> if the x and y coordinates are equal to zero.
*/
public boolean isEmpty() {
return this.x == 0f && this.y == 0f;
}
}

View file

@ -1,670 +0,0 @@
/*
* $Id$
*
* Copyright (c) 2011-15 Stephane GALLAND <stephane.galland@utbm.fr>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
*/
package math;
/** 2D Vector with 2 floating-point values.
*
* Copied from {@link https://github.com/gallandarakhneorg/afc/}.
*
* @author St&eacute;phane GALLAND &lt;stephane.galland@utbm.fr&gt;
* @version $Name$ $Revision$ $Date$
*/
public class Vector2f extends Tuple2f<Vector2f> {
private static final long serialVersionUID = -2062941509400877679L;
/**
*/
public Vector2f() {
//
}
/**
* @param tuple is the tuple to copy.
*/
public Vector2f(Tuple2f<?> tuple) {
this(tuple, false);
}
/**
* @param tuple is the tuple to copy.
* @param normalize
*/
public Vector2f(Tuple2f<?> tuple, boolean normalize) {
super(tuple);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* @param tuple is the tuple to copy.
*/
public Vector2f(int[] tuple) {
this(tuple, false);
}
/**
* @param tuple is the tuple to copy.
*/
public Vector2f(float[] tuple) {
this(tuple, false);
}
/**
* @param tuple is the tuple to copy.
* @param normalize
*/
public Vector2f(int[] tuple, boolean normalize) {
super(tuple);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* @param tuple is the tuple to copy.
* @param normalize
*/
public Vector2f(float[] tuple, boolean normalize) {
super(tuple);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* @param x
* @param y
*/
public Vector2f(int x, int y) {
this(x,y,false);
}
/**
* @param x
* @param y
*/
public Vector2f(float x, float y) {
this(x,y,false);
}
/**
* @param x
* @param y
*/
public Vector2f(double x, double y) {
this((float)x,(float)y,false);
}
/**
* @param x
* @param y
*/
public Vector2f(long x, long y) {
this(x,y,false);
}
/**
* @param x
* @param y
* @param normalize
*/
public Vector2f(int x, int y, boolean normalize) {
super(x,y);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* @param x
* @param y
* @param normalize
*/
public Vector2f(float x, float y, boolean normalize) {
super(x,y);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* @param x
* @param y
* @param normalize
*/
public Vector2f(double x, double y, boolean normalize) {
super((float)x,(float)y);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* @param x
* @param y
* @param normalize
*/
public Vector2f(long x, long y, boolean normalize) {
super(x,y);
if (normalize) {
try {
normalize();
} catch (Throwable o) {
//
}
}
}
/**
* Sets the value of this tuple to the sum of tuples t1 and t2.
* @param t1 the first tuple
* @param t2 the second tuple
*/
public void add(Vector2f t1, Vector2f t2) {
this.x = t1.getX() + t2.getX();
this.y = t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the sum of itself and t1.
* @param t1 the other tuple
*/
public void add(Vector2f t1) {
this.x += t1.getX();
this.y += t1.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1 plus tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
public void scaleAdd(int s, Vector2f t1, Vector2f t2) {
this.x = s * t1.getX() + t2.getX();
this.y = s * t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of tuple t1 plus tuple t2 (this = s*t1 + t2).
* @param s the scalar value
* @param t1 the tuple to be multipled
* @param t2 the tuple to be added
*/
public void scaleAdd(float s, Vector2f t1, Vector2f t2) {
this.x = s * t1.getX() + t2.getX();
this.y = s * t1.getY() + t2.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of itself and then adds tuple t1 (this = s*this + t1).
* @param s the scalar value
* @param t1 the tuple to be added
*/
public void scaleAdd(int s, Vector2f t1) {
this.x = s * this.x + t1.getX();
this.y = s * this.y + t1.getY();
}
/**
* Sets the value of this tuple to the scalar multiplication
* of itself and then adds tuple t1 (this = s*this + t1).
* @param s the scalar value
* @param t1 the tuple to be added
*/
public void scaleAdd(float s, Vector2f t1) {
this.x = s * this.x + t1.getX();
this.y = s * this.y + t1.getY();
}
/**
* Sets the value of this tuple to the difference
* of tuples t1 and t2 (this = t1 - t2).
* @param t1 the first tuple
* @param t2 the second tuple
*/
public void sub(Vector2f t1, Vector2f t2) {
this.x = t1.getX() - t2.getX();
this.y = t1.getY() - t2.getY();
}
/**
* Sets the value of this tuple to the difference
* of tuples t1 and t2 (this = t1 - t2).
* @param t1 the first tuple
* @param t2 the second tuple
*/
public void sub(Point2f t1, Point2f t2) {
this.x = t1.getX() - t2.getX();
this.y = t1.getY() - t2.getY();
}
/**
* Sets the value of this tuple to the difference
* of itself and t1 (this = this - t1).
* @param t1 the other tuple
*/
public void sub(Vector2f t1) {
this.x -= t1.getX();
this.y -= t1.getY();
}
/**
* Computes the dot product of the this vector and vector v1.
* @param v1 the other vector
* @return the dot product.
*/
public float dot(Vector2f v1) {
return (this.x*v1.getX() + this.y*v1.getY());
}
/** Change the coordinates of this vector to make it a perpendicular
* vector to the original coordinates.
*/
public void perpendicularize() {
// Based on the cross product in 3D of (vx,vy,0)x(0,0,1), right-handed
//set(getY(), -getX());
// Based on the cross product in 3D of (vx,vy,0)x(0,0,1), left-handed
set(-getY(), getX());
}
/**
* Returns the length of this vector.
* @return the length of this vector
*/
public float length() {
return (float) Math.sqrt(this.x*this.x + this.y*this.y);
}
/**
* Returns the squared length of this vector.
* @return the squared length of this vector
*/
public float lengthSquared() {
return (this.x*this.x + this.y*this.y);
}
/**
* Set the length of this vector.
* @param length - the length of this vector
*/
public void setLength(float length) {
if (length >= 0f) {
float norm = this.x*this.x + this.y*this.y;
if (norm != 0f) {
norm = (float)Math.sqrt(norm);
norm = length / norm;
this.x *= norm;
this.y *= norm;
} else {
this.x = length;
this.y = 0f;
}
} else {
this.x = this.y = 0f;
}
}
/** Create a colinear vector with the given length.
*
* @param length - the length.
* @return the colinear vector.
*/
public Vector2f toColinearVector(float length) {
float x, y;
if (length >= 0f) {
float norm = this.x*this.x + this.y*this.y;
if (norm != 0f) {
norm = (float)Math.sqrt(norm);
norm = length / norm;
x = this.x * norm;
y = this.y * norm;
} else {
x = length;
y = 0f;
}
} else {
x = y = 0f;
}
return new Vector2f(x, y);
}
/**
* Sets the value of this vector to the normalization of vector v1.
* @param v1 the un-normalized vector
*/
public void normalize(Vector2f v1) {
float norm = 1f / v1.length();
this.x = (int)(v1.getX()*norm);
this.y = (int)(v1.getY()*norm);
}
/**
* Normalizes this vector in place.
*/
public void normalize() {
float norm;
norm = (float)(1./Math.sqrt(this.x*this.x + this.y*this.y));
this.x *= norm;
this.y *= norm;
}
/**
* Returns the angle in radians between this vector and the vector
* parameter; the return value is constrained to the range [0,PI].
* @param v1 the other vector
* @return the angle in radians in the range [0,PI]
*/
public float angle(Vector2f v1) {
double vDot = dot(v1) / ( length()*v1.length() );
if( vDot < -1.) vDot = -1.;
if( vDot > 1.) vDot = 1.;
return((float) (Math.acos( vDot )));
}
/** Compute a signed angle between this vector and the given vector.
* <p>
* The signed angle between this vector and {@code v}
* is the rotation angle to apply to this vector
* to be colinear to {@code v} and pointing the
* same demi-plane. It means that the angle replied
* by this function is be negative if the rotation
* to apply is clockwise, and positive if
* the rotation is counterclockwise.
* <p>
* The value replied by {@link #angle(Vector2D)}
* is the absolute value of the vlaue replied by this
* function.
*
* @param v is the vector to reach.
* @return the rotation angle to turn this vector to reach
* {@code v}.
*/
public float signedAngle(Vector2f v) {
return MathUtil.signedAngle(getX(), getY(), v.getX(), v.getY());
}
/** Turn this vector about the given rotation angle.
*
* @param angle is the rotation angle in radians.
*/
public void turn(float angle) {
float sin = (float)Math.sin(angle);
float cos = (float)Math.cos(angle);
float x = cos * getX() + -sin * getY(); // -sin is on this line according to the coordinate system
float y = sin * getX() + cos * getY();
set(x,y);
}
/** Replies the orientation angle on a trigonometric circle
* that is corresponding to the given direction of this vector.
*
* @return the angle on a trigonometric circle that is corresponding
* to the given orientation vector.
*/
public float getOrientationAngle() {
float angle = (float)Math.acos(getX());
if (getY()<0f) angle = -angle;
return MathUtil.clampRadian(angle);
}
/** Set this vector with the direction of the orientation angle on a trigonometric circle.
*
* @param angle the angle on a trigonometric circle.
*/
public void setOrientationAngle(float angle) {
set(
(float) Math.cos(angle),
(float) Math.sin(angle));
}
@Override
public Vector2f clone() {
return (Vector2f) super.clone();
}
/** Replies the orientation vector, which is corresponding
* to the given angle on a trigonometric circle.
*
* @param angle is the angle in radians to translate.
* @return the orientation vector which is corresponding to the given angle.
*/
public static Vector2f toOrientationVector(float angle) {
return new Vector2f(
(float)Math.cos(angle),
(float)Math.sin(angle));
}
/** Sum of vectors: r = this + v.
*
* @param v the vector
* @return the result.
* @see #add(Vector2f, Vector2f)
*/
public Vector2f operator_plus(Vector2f v) {
Vector2f r = new Vector2f();
r.add(this, v);
return r;
}
/** Add vector to a point: r = this + p.
*
* @param p the point
* @return the result.
* @see Point2f#add(Point2f, Vector2f)
*/
public Point2f operator_plus(Point2f p) {
Point2f r = new Point2f();
r.add(p, this);
return r;
}
/** Sum of vectors: this += v.
* It is equivalent to {@code this.add(v)}.
*
* @param v the vector
* @return the result.
* @see #add(Vector2f)
*/
public void operator_add(Vector2f v) {
add(v);
}
/** Subtract of vectors: r = this - v.
*
* @param v the vector
* @return the result.
* @see #sub(Vector2f, Vector2f)
*/
public Vector2f operator_minus(Vector2f v) {
Vector2f r = new Vector2f();
r.sub(this, v);
return r;
}
/** Subtract of vectors: this -= v.
* It is equivalent to {@code this.sub(v)}.
*
* @param v the vector
* @return the result.
* @see #sub(Vector2f)
*/
public void operator_remove(Vector2f v) {
sub(v);
}
/** Negation: r = -this.
* It is equivalent to {@code this.negate(r)}
*
* @param v the vector
* @return the result.
* @see #negate(Vector2f)
*/
public Vector2f operator_minus() {
Vector2f r = new Vector2f();
negate(r);
return r;
}
/** Dot product: r = this.v.
*
* @param v the vector
* @return the result.
* @see #dot(Vector2f)
*/
public float operator_multiply(Vector2f v) {
return dot(v);
}
/** Scale a vector: r = this * f.
*
* @param v the vector
* @return the scaled vector.
*/
public Vector2f operator_multiply(float f) {
Vector2f r = new Vector2f(this);
r.scale(f);
return r;
}
/** Scale a vector: r = this / f.
*
* @param v the vector
* @return the scaled vector.
*/
public Vector2f operator_divide(float f) {
Vector2f r = new Vector2f(this);
r.scale(1f/f);
return r;
}
/** Replies if the vectors are equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_equals(Vector2f v) {
return equals(v);
}
/** Replies if the vectors are not equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_notEquals(Vector2f v) {
return !equals(v);
}
/** Replies if the vectors are equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_tripleEquals(Vector2f v) {
return equals(v);
}
/** Replies if the vectors are not equal.
*
* @param v the vector.
* @return test result.
*/
public boolean operator_tripleNotEquals(Vector2f v) {
return !equals(v);
}
/** Replies if the absolute angle between this and v
*
* @param v the vector.
* @return the signed angle.
* @see #angle(Vector2f)
*/
public float operator_upTo(Vector2f v) {
return angle(v);
}
/** Replies if the signed angle from this to v
*
* @param v the vector.
* @return the signed angle.
* @see #signedAngle(Vector2f)
*/
public float operator_greaterThanDoubleDot(Vector2f v) {
return signedAngle(v);
}
/** Replies if the signed angle from v to this
*
* @param v the vector.
* @return the signed angle.
* @see #signedAngle(Vector2f)
*/
public float operator_doubleDotLessThan(Vector2f v) {
return - signedAngle(v);
}
/** If the tuple is nul then b else a.
*
* @param v the vector.
* @return the vector.
* @see #epsilonNul(float)
* @see MathUtil#EPSILON
*/
public Vector2f operator_elvis(Vector2f v) {
if (epsilonNul(MathUtil.EPSILON)) {
return v;
}
return this;
}
}

View file

@ -1,51 +0,0 @@
package simulator;
import simulator.agent.AgentSimulator;
import simulator.environment.Environment;
import simulator.environment.dataStructure.quadTree.QuadTree;
public class Simulator extends Thread{
private Environment environment;
private AgentSimulator agentSim;
private boolean pause = false;
private boolean close = false;
private float time;
public Simulator(float width,float height){
environment = new Environment(width,height);
//agentSim = new AgentSimulator();
this.time = System.currentTimeMillis();
}
@Override
public void run() {
float newTime;
while(!close){
if(!pause){
newTime = System.currentTimeMillis();
//environment.perceive();
//agentSim.think();
environment.live(0.01f);
time = newTime;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public synchronized void changePause(){
this.pause = !this.pause;
}
public synchronized void end(){
this.close = true;
}
public QuadTree getTree(){
return environment.getTree();
}
}

View file

@ -1,13 +0,0 @@
package simulator.agent;
import java.util.ArrayList;
import simulator.agent.mind.Agent;
public class AgentSimulator {
ArrayList <Agent> agents;
public void think(){
}
}

View file

@ -1,28 +0,0 @@
package simulator.agent.mind;
import java.util.ArrayList;
import simulator.linkBodytoAgent.PerceivedObject;
import simulator.linkBodytoAgent.linkAgentBody;
public abstract class Agent {
private final linkAgentBody linkedBody;
private ArrayList<PerceivedObject> perceivedObject;
Agent (linkAgentBody linkedBody){
this.linkedBody = linkedBody;
}
public ArrayList<PerceivedObject> getPerceivedObject() {
return perceivedObject;
}
public void setPerceivedObject(ArrayList<PerceivedObject> perceivedObject) {
this.perceivedObject = perceivedObject;
}
public linkAgentBody getLinkedBody(){
return this.linkedBody;
}
}

View file

@ -1,66 +0,0 @@
package simulator.environment;
import java.util.ArrayList;
import math.Point2f;
import math.Vector2f;
import simulator.environment.dataStructure.quadTree.QuadTree;
import simulator.environment.movingManager.MovingManager;
import simulator.environment.worldObject.AgentBody;
import simulator.environment.worldObject.MovableObject;
import simulator.environment.worldObject.WorldObject;
import simulator.linkBodytoAgent.PerceivedObject;
public class Environment {
private QuadTree tree;
private ArrayList<AgentBody> bodies;
private MovingManager movingManager = new MovingManager();
MovableObject mo;
MovableObject mo2;
public Environment (float width, float height){
tree = new QuadTree(width,height);
mo = new MovableObject(new Point2f(-3, 0), 1, 5);
mo.setVelocity(new Vector2f(1,0));
tree.insertObject(mo);
movingManager.add(mo);
mo2 = new MovableObject(new Point2f(3, 0), 1,5);
mo2.setVelocity(new Vector2f(-1,0));
tree.insertObject(mo2);
movingManager.add(mo2);
bodies = new ArrayList<AgentBody> ();
}
//TODO
public void live(float Dt){
this.movingManager.moveAll(this.tree,Dt);
if(mo.getShape().intersects(mo2.getShape())){
System.out.println("SUCCESS");
}
else{
System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
}
}
public void perceive (){
for(AgentBody b : bodies){
b.getLinkedAgent().sendPerceptions(this.BodyPerception(b));
}
}
private ArrayList<PerceivedObject> BodyPerception(AgentBody a){
ArrayList<WorldObject> tempList = this.tree.getObjectInShape(a.getShape());
ArrayList<PerceivedObject> perceptObject = new ArrayList<PerceivedObject> ();
for(WorldObject o : tempList){
if(o != a){
perceptObject.add(new PerceivedObject(o, a.getPosition()));
}
}
return perceptObject;
}
public QuadTree getTree(){
return this.tree;
}
}

View file

@ -1,109 +0,0 @@
package simulator.environment.dataStructure.quadTree;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import simulator.environment.worldObject.WorldObject;
import math.Rectangle2f;
import math.Shape2f;
public class Node {
private List <Node> child;
private Rectangle2f bounds;
private List <WorldObject> listObject;
Node(Rectangle2f bounds){
this.bounds = bounds;
this.listObject = new ArrayList<WorldObject> ();
}
public synchronized void insertObject(WorldObject o){
if(child == null){
if(listObject.isEmpty()){
this.listObject.add(o);
o.setNode(this);
}
else{
this.createchilds();
ArrayList<WorldObject> tempList = new ArrayList<WorldObject>(this.listObject);
this.listObject.clear();
for(WorldObject objectInList : tempList){
this.insertObject(objectInList);
}
this.insertObject(o);
}
}
else{
boolean intersect = false;
for(Node c : this.child){
if(o.getShape().intersects(c.bounds))
intersect = true;
}
if(intersect){
this.listObject.add(o);
o.setNode(this);
}
else{
boolean find = false;
Iterator<Node> it = this.child.iterator();
Node tempChild;
while(it.hasNext() && !find){
tempChild = it.next();
if(o.getPosition().getX() <= tempChild.bounds.getUpper().getX() && o.getPosition().getX() >= tempChild.bounds.getLower().getX() && o.getPosition().getY() <= tempChild.bounds.getUpper().getY() && o.getPosition().getY() >= tempChild.bounds.getLower().getY()){
find = true;
tempChild.insertObject(o);
}
}
}
}
}
public synchronized ArrayList<WorldObject> getObjectInShape(Shape2f<?> shape){
ArrayList <WorldObject> tempListObject = new ArrayList<WorldObject> ();
if(child == null){
for(WorldObject o : this.listObject){
if(o.getShape().intersects(shape))
tempListObject.add(o);
}
}
else{
int intersection = 0;
for(Node c : this.child){
if(c.bounds.intersects(shape)){
++ intersection;
tempListObject.addAll(c.getObjectInShape(shape));
}
}
if(intersection > 1){
for(WorldObject o : this.listObject){
if(o.getShape().intersects(shape))
tempListObject.add(o);
}
}
}
return tempListObject;
}
private void createchilds(){
this.child = new ArrayList<Node>();
this.child.add(new Node(new Rectangle2f(this.bounds.getLower().getX(),this.bounds.getUpper().getY(),this.bounds.getCenter().getX(),this.bounds.getCenter().getY())));
this.child.add(new Node(new Rectangle2f(this.bounds.getUpper().getX(),this.bounds.getUpper().getY(),this.bounds.getCenter().getX(),this.bounds.getCenter().getY())));
this.child.add(new Node(new Rectangle2f(this.bounds.getLower().getX(),this.bounds.getLower().getY(),this.bounds.getCenter().getX(),this.bounds.getCenter().getY())));
this.child.add(new Node(new Rectangle2f(this.bounds.getUpper().getX(),this.bounds.getLower().getY(),this.bounds.getCenter().getX(),this.bounds.getCenter().getY())));
}
public synchronized void removeObject(WorldObject o){
this.listObject.remove(o);
}
}

View file

@ -1,27 +0,0 @@
package simulator.environment.dataStructure.quadTree;
import simulator.environment.worldObject.WorldObject;
import java.util.ArrayList;
import math.Rectangle2f;
import math.Shape2f;
public class QuadTree {
private Node root;
public QuadTree(float width, float height){
root = new Node(new Rectangle2f(-width/2,-height/2,width/2,height/2));
}
public synchronized void insertObject(WorldObject o){
this.root.insertObject(o);
}
public synchronized ArrayList <WorldObject> getObjectInShape(Shape2f <?> shape){
return this.root.getObjectInShape(shape);
}
}

View file

@ -1,76 +0,0 @@
package simulator.environment.movingManager;
import java.util.ArrayList;
import math.Circle2f;
import math.Vector2f;
import simulator.environment.dataStructure.quadTree.QuadTree;
import simulator.environment.worldObject.MovableObject;
import simulator.environment.worldObject.WorldObject;
public class MovingManager {
private ArrayList<MovableObject> movingObject;
private float maxSpeed = 0; // the maximum speed of the fastest object in movingObject
public MovingManager(){
movingObject = new ArrayList<MovableObject> ();
}
public void moveAll(QuadTree tree, float deltaT){
Float dt = null;
for(MovableObject mo : this.movingObject){
ArrayList<WorldObject> potentialObstacle = tree.getObjectInShape(new Circle2f(mo.getPosition(), mo.getMaxSpeed() + this.maxSpeed));
potentialObstacle.remove(mo);
for(WorldObject obstacle : potentialObstacle){
dt = mo.collision(obstacle,deltaT);
if(dt != null){
System.out.println(dt);
}
/*
if(dt <= 1 && dt > 0){
if(obstacle instanceof MovableObject ){
//Vector2f tempVelocity = mo.getVelocity().clone();
//mo.setVelocity(tempVelocity.operator_multiply(dt).operator_plus(((MovableObject) obstacle).getVelocity().operator_multiply(1-dt)));
}
else{
// TODO
}
}
}*/
}
}
for(MovableObject mo : this.movingObject){
if(dt!=null){
if(dt <= 1 && dt > 0){
mo.Translate(mo.getVelocity().operator_multiply(deltaT));
mo.getNode().removeObject(mo);
tree.insertObject(mo);
mo.setVelocity(new Vector2f(0,0));
}
else {
mo.Translate(mo.getVelocity().operator_multiply(deltaT));
mo.getNode().removeObject(mo);
tree.insertObject(mo);
}
}
else{
mo.Translate(mo.getVelocity().operator_multiply(deltaT));
mo.getNode().removeObject(mo);
tree.insertObject(mo);
}
System.out.println(mo.getPosition().toString());
}
}
public void add(MovableObject o){
if(o.getMaxSpeed() > maxSpeed)
this.maxSpeed = o.getMaxSpeed();
this.movingObject.add(o);
}
}

View file

@ -1,24 +0,0 @@
package simulator.environment.worldObject;
import math.Point2f;
import math.Shape2f;
import simulator.linkBodytoAgent.linkAgentBody;
public class AgentBody extends MovableObject{
private Shape2f<?> fieldOfView;
private final linkAgentBody linkedAgent;
public AgentBody (linkAgentBody linkedAgent, Point2f position, float radius,float maxSpeed){
super(position, radius, maxSpeed);
this.linkedAgent = linkedAgent;
}
public linkAgentBody getLinkedAgent(){
return this.linkedAgent;
}
public Shape2f<?> getFieldOfView(){
return this.fieldOfView;
}
}

View file

@ -1,67 +0,0 @@
package simulator.environment.worldObject;
import math.Point2f;
import math.Vector2f;
public class MovableObject extends WorldObject{
private Vector2f velocity;
private final float maxSpeed;
public MovableObject(Point2f position, float radius,float maxSpeed){
super(position, radius);
this.maxSpeed = maxSpeed;
}
public float getMaxSpeed (){
return this.maxSpeed;
}
public synchronized Vector2f getVelocity() {
return velocity;
}
public synchronized void setVelocity(Vector2f velocity) {
this.velocity = velocity;
if(velocity.length() > maxSpeed)
this.velocity.setLength(this.maxSpeed);
}
/*
* return the minimun dt if there is a collision
* return null if there is no collision
*/
public Float collision(WorldObject obstacle, float deltaT) {
/*
* all object have cicrle box
* 3 possibility : no collision, 1 dt, 2dt
*/
float r,a,b,c,discriminant;
Vector2f v,d;
if(obstacle instanceof MovableObject)
v = (this.velocity.operator_minus(((MovableObject)obstacle).velocity));
else
v = this.velocity;
if(v.isEmpty())
return null;
v = v.operator_multiply(deltaT);
d = this.getPosition().operator_minus(obstacle.getPosition());
r = this.getShape().getRadius() + obstacle.getShape().getRadius();
a = v.lengthSquared();
b = 2*(v.dot(d));
c = d.lengthSquared() - r*r;
System.out.println("a = " + a + " b = " + b + " c = " + c + " v = " + v.toString());
discriminant = b*b - 4*(a*c);
if(discriminant < 0){
return null;
}
else if(discriminant > 0){
float dt1 = (-b + (float)(Math.sqrt(discriminant)))/(2*a);
float dt2 = (-b - (float) (Math.sqrt(discriminant)))/(2*a);
return new Float(Math.min(dt1, dt2));
}
else{
return new Float(-b/(2*a));
}
}
}

View file

@ -1,43 +0,0 @@
package simulator.environment.worldObject;
import math.Circle2f;
import math.Point2f;
import math.Vector2f;
import simulator.environment.dataStructure.quadTree.Node;
public class WorldObject {
private Point2f position;
private Circle2f shape;
private Node node;
public WorldObject (WorldObject o){
this.position = o.position.clone();
this.shape = o.shape.clone();
}
public WorldObject(Point2f p, float radius){
this.position = p;
this.shape = new Circle2f(p, radius);
}
public synchronized Point2f getPosition() {
return position;
}
public synchronized void Translate(Vector2f v) {
this.position = this.position.operator_plus(v);
this.shape = this.shape.translate(v);
// changer dans quel node il se trouve TODO
}
public synchronized Circle2f getShape() {
return shape;
}
public Node getNode() {
return node;
}
public void setNode(Node node) {
this.node = node;
}
}

View file

@ -1,21 +0,0 @@
package simulator.linkBodytoAgent;
import math.Circle2f;
import math.Point2f;
import math.Vector2f;
import simulator.environment.worldObject.WorldObject;
public class PerceivedObject {
private WorldObject object;
public PerceivedObject (WorldObject o, Point2f repere){
this.object = new WorldObject(o);
this.object.Translate(new Vector2f(repere).operator_multiply(-1));
}
public Point2f getPosition (){
return this.object.getPosition();
}
public Circle2f getShape (){
return this.object.getShape();
}
}

View file

@ -1,19 +0,0 @@
package simulator.linkBodytoAgent;
import java.util.ArrayList;
import math.Vector2f;
import simulator.agent.mind.Agent;
import simulator.environment.worldObject.AgentBody;
public class linkAgentBody {
private AgentBody agentBody;
private Agent agentMind;
public void sendPerceptions(ArrayList <PerceivedObject> perceivedObject){
this.agentMind.setPerceivedObject(perceivedObject);
}
public void aplyInfluence(Vector2f velocity){
this.agentBody.setVelocity(velocity);
}
}