Commit 742d2883 authored by Julian Zobel's avatar Julian Zobel
Browse files

Position Vector:

- removed dimension field (redundant, access through 'values' field
- updated toString(), now correctly working with dimensions > 2
parent 4c8daf00
...@@ -63,8 +63,6 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location; ...@@ -63,8 +63,6 @@ import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
*/ */
public class PositionVector implements Location { public class PositionVector implements Location {
private int dimensions;
/** /**
* The private (!) coordinates of this vector. Ensures, that all necessary * The private (!) coordinates of this vector. Ensures, that all necessary
* transforms can be performed in the getter-methods. * transforms can be performed in the getter-methods.
...@@ -76,7 +74,7 @@ public class PositionVector implements Location { ...@@ -76,7 +74,7 @@ public class PositionVector implements Location {
/** /**
* Timestamp of creation * Timestamp of creation
*/ */
private long created; private long timeOfCreation;
/** /**
* Create a new Position Vector * Create a new Position Vector
...@@ -92,9 +90,9 @@ public class PositionVector implements Location { ...@@ -92,9 +90,9 @@ public class PositionVector implements Location {
if(dimensions == 2) if(dimensions == 2)
dimensions = 3; dimensions = 3;
this.dimensions = dimensions; //this.dimensions = dimensions;
this.values = new double[dimensions]; this.values = new double[dimensions];
this.created = Time.getCurrentTime(); this.timeOfCreation = Time.getCurrentTime();
} }
/** /**
...@@ -125,7 +123,7 @@ public class PositionVector implements Location { ...@@ -125,7 +123,7 @@ public class PositionVector implements Location {
} }
public PositionVector(Location location) { public PositionVector(Location location) {
this.dimensions = 3; //this.dimensions = 3;
this.values = new double[3]; this.values = new double[3];
if(location.hasAltitude()) { if(location.hasAltitude()) {
...@@ -137,7 +135,7 @@ public class PositionVector implements Location { ...@@ -137,7 +135,7 @@ public class PositionVector implements Location {
this.setLatitudeOrY(location.getLatitudeOrY()); this.setLatitudeOrY(location.getLatitudeOrY());
this.setLongitudeOrX(location.getLongitudeOrX()); this.setLongitudeOrX(location.getLongitudeOrX());
this.created = Time.getCurrentTime(); this.timeOfCreation = Time.getCurrentTime();
} }
/** /**
...@@ -159,7 +157,7 @@ public class PositionVector implements Location { ...@@ -159,7 +157,7 @@ public class PositionVector implements Location {
* If you extend Position Vector, make sure to overwrite this method! * If you extend Position Vector, make sure to overwrite this method!
*/ */
PositionVector clone = new PositionVector(this); // use clone constructor PositionVector clone = new PositionVector(this); // use clone constructor
clone.created = this.created; clone.timeOfCreation = this.timeOfCreation;
return clone; return clone;
} }
...@@ -211,7 +209,7 @@ public class PositionVector implements Location { ...@@ -211,7 +209,7 @@ public class PositionVector implements Location {
* @return * @return
*/ */
public final int getDimensions() { public final int getDimensions() {
return dimensions; return values.length;
} }
/** /**
...@@ -241,7 +239,7 @@ public class PositionVector implements Location { ...@@ -241,7 +239,7 @@ public class PositionVector implements Location {
* @param values * @param values
*/ */
public void setEntries(double... values) { public void setEntries(double... values) {
assert values.length == dimensions; assert values.length == this.values.length;
for (int i = 0; i < values.length; i++) { for (int i = 0; i < values.length; i++) {
setEntry(i, values[i]); setEntry(i, values[i]);
} }
...@@ -281,8 +279,8 @@ public class PositionVector implements Location { ...@@ -281,8 +279,8 @@ public class PositionVector implements Location {
* @param delta * @param delta
*/ */
public void add(PositionVector delta) { public void add(PositionVector delta) {
assert dimensions == delta.getDimensions(); assert this.values.length == delta.getDimensions();
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
setEntry(i, getEntry(i) + delta.getEntry(i)); setEntry(i, getEntry(i) + delta.getEntry(i));
} }
} }
...@@ -293,8 +291,8 @@ public class PositionVector implements Location { ...@@ -293,8 +291,8 @@ public class PositionVector implements Location {
* @param delta * @param delta
*/ */
public void subtract(PositionVector delta) { public void subtract(PositionVector delta) {
assert dimensions == delta.getDimensions(); assert this.values.length == delta.getDimensions();
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
setEntry(i, getEntry(i) - delta.getEntry(i)); setEntry(i, getEntry(i) - delta.getEntry(i));
} }
} }
...@@ -306,7 +304,7 @@ public class PositionVector implements Location { ...@@ -306,7 +304,7 @@ public class PositionVector implements Location {
* @return the vector * @return the vector
*/ */
public PositionVector multiplyScalar(double multi) { public PositionVector multiplyScalar(double multi) {
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
setEntry(i, multi * getEntry(i)); setEntry(i, multi * getEntry(i));
} }
return this; return this;
...@@ -319,13 +317,13 @@ public class PositionVector implements Location { ...@@ -319,13 +317,13 @@ public class PositionVector implements Location {
public void normalize() { public void normalize() {
double hyp = 0.0; double hyp = 0.0;
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
hyp += getEntry(i) * getEntry(i); hyp += getEntry(i) * getEntry(i);
} }
hyp = Math.sqrt(hyp); hyp = Math.sqrt(hyp);
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
setEntry(i, getEntry(i) / hyp); setEntry(i, getEntry(i) / hyp);
} }
} }
...@@ -339,9 +337,9 @@ public class PositionVector implements Location { ...@@ -339,9 +337,9 @@ public class PositionVector implements Location {
* @return addition of this vector plus delta vector * @return addition of this vector plus delta vector
*/ */
public PositionVector plus(PositionVector delta) { public PositionVector plus(PositionVector delta) {
assert dimensions == delta.getDimensions(); assert getDimensions() == delta.getDimensions();
PositionVector result = new PositionVector(dimensions); PositionVector result = new PositionVector(getDimensions());
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
result.setEntry(i, this.getEntry(i) + delta.getEntry(i)); result.setEntry(i, this.getEntry(i) + delta.getEntry(i));
} }
return result; return result;
...@@ -356,9 +354,9 @@ public class PositionVector implements Location { ...@@ -356,9 +354,9 @@ public class PositionVector implements Location {
* @return subtraction of this vector minus delta vector * @return subtraction of this vector minus delta vector
*/ */
public PositionVector minus(PositionVector delta) { public PositionVector minus(PositionVector delta) {
assert dimensions == delta.getDimensions(); assert getDimensions() == delta.getDimensions();
PositionVector result = new PositionVector(dimensions); PositionVector result = new PositionVector(getDimensions());
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
result.setEntry(i, this.getEntry(i) - delta.getEntry(i)); result.setEntry(i, this.getEntry(i) - delta.getEntry(i));
} }
return result; return result;
...@@ -385,7 +383,7 @@ public class PositionVector implements Location { ...@@ -385,7 +383,7 @@ public class PositionVector implements Location {
* @return * @return
*/ */
public double[] asDoubleArray() { public double[] asDoubleArray() {
return Arrays.copyOf(values, dimensions); return Arrays.copyOf(values, getDimensions());
} }
/** /**
...@@ -395,7 +393,7 @@ public class PositionVector implements Location { ...@@ -395,7 +393,7 @@ public class PositionVector implements Location {
* @return 2 or 3 dimensional coordinates * @return 2 or 3 dimensional coordinates
*/ */
public Coordinate asCoordinate() { public Coordinate asCoordinate() {
if (dimensions < 2 || dimensions > 3) { if (getDimensions() < 2 || getDimensions() > 3) {
throw new AssertionError( throw new AssertionError(
"Cast to Coordinate only possible with two or three dimensional PositionVector"); "Cast to Coordinate only possible with two or three dimensional PositionVector");
} }
...@@ -404,8 +402,18 @@ public class PositionVector implements Location { ...@@ -404,8 +402,18 @@ public class PositionVector implements Location {
} }
@Override @Override
public String toString() { public String toString() {
return "(" + values[0] + ", " + values[1] + ")"; String s = "PV(";
for(int i = 0; i < getDimensions(); i++) {
s += values[i];
if(i < getDimensions() - 1) {
s += ", ";
}
}
s += ")";
return s;
//return "(" + values[0] + ", " + values[1] + ")";
//return "PV("+getDimensions()+") " + Arrays.toString(values) + ""; //return "PV("+getDimensions()+") " + Arrays.toString(values) + "";
} }
...@@ -413,7 +421,7 @@ public class PositionVector implements Location { ...@@ -413,7 +421,7 @@ public class PositionVector implements Location {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + dimensions; result = prime * result + getDimensions();
result = prime * result + Arrays.hashCode(values); result = prime * result + Arrays.hashCode(values);
return result; return result;
} }
...@@ -427,7 +435,7 @@ public class PositionVector implements Location { ...@@ -427,7 +435,7 @@ public class PositionVector implements Location {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
PositionVector other = (PositionVector) obj; PositionVector other = (PositionVector) obj;
if (dimensions != other.dimensions) if (getDimensions() != other.getDimensions())
return false; return false;
return Arrays.equals(values, other.values); return Arrays.equals(values, other.values);
...@@ -440,8 +448,8 @@ public class PositionVector implements Location { ...@@ -440,8 +448,8 @@ public class PositionVector implements Location {
* @param divisor * @param divisor
*/ */
public void divide(PositionVector divisor) { public void divide(PositionVector divisor) {
assert dimensions == divisor.getDimensions(); assert getDimensions() == divisor.getDimensions();
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
setEntry(i, this.getEntry(i) / divisor.getEntry(i)); setEntry(i, this.getEntry(i) / divisor.getEntry(i));
} }
} }
...@@ -453,8 +461,8 @@ public class PositionVector implements Location { ...@@ -453,8 +461,8 @@ public class PositionVector implements Location {
* @param multiplicator * @param multiplicator
*/ */
public void multiply(PositionVector multiplicator) { public void multiply(PositionVector multiplicator) {
assert dimensions == multiplicator.getDimensions(); assert getDimensions() == multiplicator.getDimensions();
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
setEntry(i, this.getEntry(i) * multiplicator.getEntry(i)); setEntry(i, this.getEntry(i) * multiplicator.getEntry(i));
} }
} }
...@@ -464,10 +472,9 @@ public class PositionVector implements Location { ...@@ -464,10 +472,9 @@ public class PositionVector implements Location {
* *
* @param vector * @param vector
*/ */
public void replace(PositionVector vector) { public void replace(PositionVector vector) {
this.dimensions = vector.getDimensions(); this.values = new double[vector.getDimensions()];
this.values = new double[dimensions]; for (int i = 0; i < this.getDimensions(); i++) {
for (int i = 0; i < this.dimensions; i++) {
setEntry(i, vector.getEntry(i)); setEntry(i, vector.getEntry(i));
} }
} }
...@@ -551,12 +558,12 @@ public class PositionVector implements Location { ...@@ -551,12 +558,12 @@ public class PositionVector implements Location {
@Override @Override
public boolean hasAltitude() { public boolean hasAltitude() {
return dimensions > 2; return getDimensions() > 2;
} }
@Override @Override
public long getAgeOfLocation() { public long getAgeOfLocation() {
return Time.getCurrentTime() - created; return Time.getCurrentTime() - timeOfCreation;
} }
@Override @Override
...@@ -565,7 +572,7 @@ public class PositionVector implements Location { ...@@ -565,7 +572,7 @@ public class PositionVector implements Location {
PositionVector pv = (PositionVector) dest; PositionVector pv = (PositionVector) dest;
if (pv.getDimensions() == getDimensions()) { if (pv.getDimensions() == getDimensions()) {
double dist = 0; double dist = 0;
for (int i = 0; i < dimensions; i++) { for (int i = 0; i < getDimensions(); i++) {
// faster as Math.pow // faster as Math.pow
dist += (pv.getEntry(i) - getEntry(i)) dist += (pv.getEntry(i) - getEntry(i))
* (pv.getEntry(i) - getEntry(i)); * (pv.getEntry(i) - getEntry(i));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment