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