PositionVector.java 14.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2005-2011 KOM - Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

21
package de.tud.kom.p2psim.impl.topology.util;
22
23
24
25
26
27

import java.awt.Point;
import java.util.Arrays;

import com.vividsolutions.jts.geom.Coordinate;

28
import de.tudarmstadt.maki.simonstrator.api.Time;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;

/**
 * N-Dimensional Vector containing a Position. Wherever possible, applications
 * and overlay should only use the {@link Position}-interface to implement their
 * functionality.
 * 
 * Just a minor note: if you add functionality, change existing functionality,
 * or "fix bugs", please comment AND SIGN your changes - you might
 * (unintentionally) break other people's code! This is a core component within
 * Peerfact - so each change might have a lot of undesired side effects!
 * 
 * CHANGELOG
 * 
Julian Zobel's avatar
Julian Zobel committed
43
 * - 14.08.2008 Bjoern Richerzhagen: removed a number of unused, uncommented, and
44
45
46
47
48
49
 * dubious methods from this class. Fixed the replace-bug (discovered by Nils)
 * with little overhead. Implemented assertions with assertions - previous
 * method introduces unwanted overhead outside of development settings. Removed
 * the dubious equals-tolerance, which violates the hashCode contract. Seriously
 * guys...
 * 
Julian Zobel's avatar
Julian Zobel committed
50
 * - 05.09.2018 Julian Zobel: Added location support for third dimension (altitude)
51
52
 * and removed a bug in the moveStep() function.
 * 
53
 * - 10.09.2018 Julian Zobel: Adapted the replace function to work properly without 
54
55
 * assertions. Replace does now fully replace the position vector entries. PVs are now
 * always at least 3D (with 0 altitude)
56
 * 
57
 * - 14.03.2019 Louis Neumann: Added timestamp when creating a PositionVector and adjusted ageOfLocation()
58
 * 
59
 * - 04.06.2019 Julian Zobel: Clone function also clones the timestamp from the cloned position. 
60
 * 
61
 * @author Bjoern Richerzhagen, Julian Zobel, Louis Neumann
62
 * @version 1.2, 10.09.2018
63
 */
64
public class PositionVector implements Location {
65
66
67
68
69
70

	/**
	 * The private (!) coordinates of this vector. Ensures, that all necessary
	 * transforms can be performed in the getter-methods.
	 */
	private double[] values;
Julian Zobel's avatar
Julian Zobel committed
71
	 
72
	private double accuracy = -1;
Julian Zobel's avatar
Julian Zobel committed
73
	
74
	/**
Julian Zobel's avatar
Julian Zobel committed
75
	 * Timestamp of creation
76
	 */
Julian Zobel's avatar
Julian Zobel committed
77
	private long timeOfCreation;
78
79
80
81
82
83
84
85

	/**
	 * Create a new Position Vector
	 * 
	 * @param dimensions
	 */
	public PositionVector(int dimensions) {
		if (dimensions < 2) {
86
			throw new AssertionError("Vector cannot have less than 2 dimensions.");
87
		}
88
89
90
91
92
		
		// Position Vector is always at least 3D
		if(dimensions == 2)
			dimensions = 3;
		
Julian Zobel's avatar
Julian Zobel committed
93
		//this.dimensions = dimensions;
94
		this.values = new double[dimensions];
Julian Zobel's avatar
Julian Zobel committed
95
		this.timeOfCreation = Time.getCurrentTime();
96
97
98
	}

	/**
Julian Zobel's avatar
Julian Zobel committed
99
	 * Constructors for position vectors, also usable for cloning.
100
101
102
	 * 
	 * @param vec
	 */
Julian Zobel's avatar
Julian Zobel committed
103
	
104
105
106
107
	public PositionVector(PositionVector vec) {
		this(vec.getDimensions());
		for (int i = 0; i < vec.getDimensions(); i++) {
			setEntry(i, vec.getEntry(i));
Julian Zobel's avatar
Julian Zobel committed
108
		}		
109
	}
110
111
	
	public PositionVector(double longitudeOrX, double latitudeOrY) {
112
		this(3);
113
114
		this.setLatitudeOrY(latitudeOrY);
		this.setLongitudeOrX(longitudeOrX);		
115
		this.setAltitude(0);
116
117
118
119
120
121
122
123
124
	}
	
	public PositionVector(double longitudeOrX, double latitudeOrY, double altitude) {
		this(3);
		this.setLatitudeOrY(latitudeOrY);
		this.setLongitudeOrX(longitudeOrX);
		this.setAltitude(altitude);
	}

125
	public PositionVector(Location location) {			
Julian Zobel's avatar
Julian Zobel committed
126
		//this.dimensions = 3;
127
128
129
		this.values = new double[3];
		
		if(location.hasAltitude()) {			
130
131
132
			this.setAltitude(location.getAltitude());
		}
		else {
133
			this.setAltitude(0);
134
135
136
137
		}
		
		this.setLatitudeOrY(location.getLatitudeOrY());
		this.setLongitudeOrX(location.getLongitudeOrX());
Julian Zobel's avatar
Julian Zobel committed
138
		this.timeOfCreation = Time.getCurrentTime();
Julian Zobel's avatar
Julian Zobel committed
139
	}	
140
141
142
143
144
145
146
147
148
149
150
151
152

	/**
	 * Convenience Constructor, initializes a Vector with values.length
	 * Dimensions and sets Entries, using the callback setEntry
	 * 
	 * @param values
	 */
	public PositionVector(double... values) {
		this(values.length);
		for (int i = 0; i < values.length; i++) {
			setEntry(i, values[i]);
		}
	}
153
	
Julian Zobel's avatar
Julian Zobel committed
154
155
156
157
158
	@Override
	public PositionVector clone() {
		/*
		 * If you extend Position Vector, make sure to overwrite this method!
		 */
159
		PositionVector clone = new PositionVector(this); // use clone constructor
Julian Zobel's avatar
Julian Zobel committed
160
		clone.timeOfCreation = this.timeOfCreation;
161
		return clone;
Julian Zobel's avatar
Julian Zobel committed
162
163
164
165
	}
	
	
	/*
166
	 *  
Julian Zobel's avatar
Julian Zobel committed
167
168
	 * 
	 */
169
	@Override
170
	public void setLatitudeOrY(double latitudeOrY)
171
	{
172
		this.setEntry(1, latitudeOrY);
173
174
175
	}
	
	@Override
176
	public void setLongitudeOrX(double longitudeOrX)
177
	{
178
		this.setEntry(0, longitudeOrX);
179
180
	}
	
181
182
	@Override
	public void setAltitude(double altitude) {
183
		this.setEntry(2, altitude);		
184
185
	}
	
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
	@Override
	public void setAccuracy(double accuracy)
			throws UnsupportedOperationException {
		if (accuracy < 0) {
			throw new AssertionError();
		}
		this.accuracy = accuracy;
	}
	
	@Override
	public double getAccuracy() {
		assert hasAccuracy() : "should check for hasAccuracy first!";
		return accuracy;
	}
	
	@Override
	public boolean hasAccuracy() {
		return accuracy != -1;
	}
205
206
207
208
209
210
211

	/**
	 * Number of Dimensions
	 * 
	 * @return
	 */
	public final int getDimensions() {
Julian Zobel's avatar
Julian Zobel committed
212
		return values.length;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	}

	/**
	 * returns the nth position in the coord-Vector, starting with 0
	 * 
	 * @param dim
	 * @return
	 */
	public double getEntry(int dim) {
		return values[dim];
	}

	/**
	 * Saves a new value. Implementations might perform error control or
	 * additional scaling/translation
	 * 
	 * @param dim
	 * @param value
	 */
	public void setEntry(int dim, double value) {
		values[dim] = value;
	}

	/**
	 * Sets all entries.
	 * 
	 * @param values
	 */
	public void setEntries(double... values) {
Julian Zobel's avatar
Julian Zobel committed
242
		assert values.length == this.values.length;
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
		for (int i = 0; i < values.length; i++) {
			setEntry(i, values[i]);
		}
	}

	/**
	 * Getter for the common X dimension.
	 * 
	 * @return value of dimension 0
	 */
	public double getX() {
		return getEntry(0);
	}

	/**
	 * Getter for the common Y dimension.
	 * 
	 * @return value of dimension 1
	 */
	public double getY() {
		return getEntry(1);
	}

	/**
	 * Getter for the common Z dimension.
	 * 
	 * @return value of dimension 2
	 */
	public double getZ() {
		return getEntry(2);
	}

	/**
	 * Modifies the current positionVector-instace by adding the delta-vector.
	 * Addition is done for each element of the vector.
	 * 
	 * @param delta
	 */
	public void add(PositionVector delta) {
Julian Zobel's avatar
Julian Zobel committed
282
283
		assert this.values.length == delta.getDimensions();
		for (int i = 0; i < getDimensions(); i++) {
284
285
286
287
288
289
290
291
292
293
			setEntry(i, getEntry(i) + delta.getEntry(i));
		}
	}

	/**
	 * Subtract a vector from the given vector
	 * 
	 * @param delta
	 */
	public void subtract(PositionVector delta) {
Julian Zobel's avatar
Julian Zobel committed
294
295
		assert  this.values.length == delta.getDimensions();
		for (int i = 0; i <  getDimensions(); i++) {
296
297
298
299
300
301
302
303
			setEntry(i, getEntry(i) - delta.getEntry(i));
		}
	}

	/**
	 * Multiply this vector with a scalar value
	 * 
	 * @param multi
Niklas-Stoehr's avatar
Niklas-Stoehr committed
304
	 * @return the vector
305
	 */
Niklas-Stoehr's avatar
Niklas-Stoehr committed
306
	public PositionVector multiplyScalar(double multi) {
Julian Zobel's avatar
Julian Zobel committed
307
		for (int i = 0; i <  getDimensions(); i++) {
308
309
			setEntry(i, multi * getEntry(i));
		}
Niklas-Stoehr's avatar
Niklas-Stoehr committed
310
		return this;
311
312
313
314
315
316
317
318
319
	}

	/**
	 * Converts this vector to its normalized form (ie. its length is equal to
	 * one)
	 */
	public void normalize() {
		double hyp = 0.0;

Julian Zobel's avatar
Julian Zobel committed
320
		for (int i = 0; i <  getDimensions(); i++) {
321
322
323
324
325
			hyp += getEntry(i) * getEntry(i);
		}

		hyp = Math.sqrt(hyp);

Julian Zobel's avatar
Julian Zobel committed
326
		for (int i = 0; i <  getDimensions(); i++) {
327
328
329
330
331
332
333
334
335
336
337
338
339
			setEntry(i, getEntry(i) / hyp);
		}
	}

	/**
	 * Additive arithmetic. Produces a new vector as result. Current vector is
	 * not changed. If you want the current vector instance to change, you
	 * should use add instead.
	 * 
	 * @param delta
	 * @return addition of this vector plus delta vector
	 */
	public PositionVector plus(PositionVector delta) {
Julian Zobel's avatar
Julian Zobel committed
340
341
342
		assert getDimensions() == delta.getDimensions();
		PositionVector result = new PositionVector(getDimensions());
		for (int i = 0; i < getDimensions(); i++) {
343
344
345
346
347
348
349
350
351
352
353
354
355
356
			result.setEntry(i, this.getEntry(i) + delta.getEntry(i));
		}
		return result;
	}

	/**
	 * Subtractive arithmetic. Produces a new vector as result. Current vector
	 * is not changed. If you want the current vector instance to change, you
	 * should use subtract instead.
	 * 
	 * @param delta
	 * @return subtraction of this vector minus delta vector
	 */
	public PositionVector minus(PositionVector delta) {
Julian Zobel's avatar
Julian Zobel committed
357
358
359
		assert getDimensions() == delta.getDimensions();
		PositionVector result = new PositionVector(getDimensions());
		for (int i = 0; i < getDimensions(); i++) {
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
			result.setEntry(i, this.getEntry(i) - delta.getEntry(i));
		}
		return result;
	}

	@Override
	public int getTransmissionSize() {
		return getDimensions() * 8;
	}

	/**
	 * Mainly for drawing purposes, Representation of the first two dimensions
	 * as a Point
	 * 
	 * @return
	 */
	public Point asPoint() {
		return new Point((int) getEntry(0), (int) getEntry(1));
	}

	/**
	 * Representation of the Vector as a Double-Array
	 * 
	 * @return
	 */
	public double[] asDoubleArray() {
Julian Zobel's avatar
Julian Zobel committed
386
		return Arrays.copyOf(values, getDimensions());
387
388
389
390
391
392
393
394
395
	}

	/**
	 * Cast of 2 or 3-dimensional PositionVector to jts geometry library
	 * coordinates.
	 * 
	 * @return 2 or 3 dimensional coordinates
	 */
	public Coordinate asCoordinate() {
Julian Zobel's avatar
Julian Zobel committed
396
		if (getDimensions() < 2 || getDimensions() > 3) {
397
398
399
			throw new AssertionError(
					"Cast to Coordinate only possible with two or three dimensional PositionVector");
		}
400
401
		
		return new Coordinate(getX(), getY(), getZ());
402
403
404
	}

	@Override
Julian Zobel's avatar
Julian Zobel committed
405
	public String toString() {
Julian Zobel's avatar
Julian Zobel committed
406
		String s = "PV(";		
Julian Zobel's avatar
Julian Zobel committed
407
408
409
410
411
412
413
414
		for(int i = 0; i < getDimensions(); i++) {
			s += values[i];
			if(i < getDimensions() - 1) {
				s += ", ";
			}
		}
		s += ")";
		return s;
415
	}
416
	
417
418
419
420
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
Julian Zobel's avatar
Julian Zobel committed
421
		result = prime * result + getDimensions();
422
423
424
425
426
427
428
429
430
431
432
433
434
		result = prime * result + Arrays.hashCode(values);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PositionVector other = (PositionVector) obj;
Julian Zobel's avatar
Julian Zobel committed
435
		if (getDimensions() != other.getDimensions())
436
437
438
439
440
441
442
443
444
445
446
447
			return false;

		return Arrays.equals(values, other.values);
	}

	/**
	 * Change this position vector by dividing each coordinate through the
	 * corresponding coordinate of the multiplicator
	 * 
	 * @param divisor
	 */
	public void divide(PositionVector divisor) {
Julian Zobel's avatar
Julian Zobel committed
448
449
		assert getDimensions() == divisor.getDimensions();
		for (int i = 0; i < getDimensions(); i++) {
450
451
452
453
454
455
456
457
458
459
460
			setEntry(i, this.getEntry(i) / divisor.getEntry(i));
		}
	}

	/**
	 * Change this position vector by multiplying each coordinate with the
	 * corresponding coordinate of the multiplicator
	 * 
	 * @param multiplicator
	 */
	public void multiply(PositionVector multiplicator) {
Julian Zobel's avatar
Julian Zobel committed
461
462
		assert getDimensions() == multiplicator.getDimensions();
		for (int i = 0; i < getDimensions(); i++) {
463
464
465
466
467
468
469
470
471
			setEntry(i, this.getEntry(i) * multiplicator.getEntry(i));
		}
	}

	/**
	 * Changes the current positionVector to be equal to the passed one.
	 * 
	 * @param vector
	 */
Julian Zobel's avatar
Julian Zobel committed
472
473
474
	public void replace(PositionVector vector) {		
		this.values = new double[vector.getDimensions()];
		for (int i = 0; i < this.getDimensions(); i++) {
475
476
			setEntry(i, vector.getEntry(i));
		}
477
478
479
480
481
482
483
484
	}

	/**
	 * Returns a new PositionVector that is a <strong>copy</strong> of the
	 * current position moved into the direction of destination with the given
	 * speed. Does not alter the current position-vector instance. Does not
	 * alter the destination vector instance.
	 * 
485
486
487
488
	 * IFF the speed would lead to us overshooting the destination, we will just
	 * move right onto the destination instead. This prevents oscillations in
	 * movement models.
	 * 
489
	 * FIXME BR: this method signature and purpose is not well defined. As it is
490
491
	 * only used in movement models, its functionality might be better
	 * implemented there...
492
493
494
495
496
	 * 
	 * @param destination
	 * @param speed
	 * @return
	 */
497
	@Deprecated
498
	public PositionVector moveStep(PositionVector destination, double speed) {
499

500
501
502
503
		if(speed == 0) {
			return new PositionVector(this);
		}
		
504
505
506
507
508
509
510
511
		double distance = destination.distanceTo(this);
		if (distance < speed) {
			/*
			 * We would overshoot the target.
			 */
			return new PositionVector(destination);
		}

512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
		PositionVector direction = new PositionVector(destination);
		direction.subtract(this);
		direction.normalize();
		direction.multiplyScalar(speed);

		PositionVector newPosition = new PositionVector(this);
		newPosition.add(direction);

		return newPosition;
	}

	public double getLength() {
		double sum = 0;
		for (double val : values) {
			sum += val * val;
		}
		return Math.sqrt(sum);
	}

	@Override
	public void set(Location l) {
533
534
535
536
537
538
		if (l instanceof PositionVector) {
			this.replace((PositionVector) l);
		}
		else {
			throw new AssertionError("Cannot replace PositionVector with Location");
		}
539
540
541
	}

	@Override
542
	public double getLatitudeOrY() {
543
		return getY();
544
545
546
	}

	@Override
547
	public double getLongitudeOrX() {		
548
		return getX();
549
550
	}

551
552
553
554
555
556
557
	@Override
	public double getAltitude() {		
		return getZ();
	}
	
	@Override
	public boolean hasAltitude() {		
Julian Zobel's avatar
Julian Zobel committed
558
		return getDimensions() > 2;
559
560
	}
	
561
562
	@Override
	public long getAgeOfLocation() {
Julian Zobel's avatar
Julian Zobel committed
563
		return Time.getCurrentTime() - timeOfCreation; 		
564
	}
Julian Zobel's avatar
Julian Zobel committed
565
		
566
567
	@Override
	public double distanceTo(Location dest) {
568
569
570
571
		if (dest instanceof PositionVector) {
			PositionVector pv = (PositionVector) dest;
			if (pv.getDimensions() == getDimensions()) {
				double dist = 0;
Julian Zobel's avatar
Julian Zobel committed
572
				for (int i = 0; i < getDimensions(); i++) {
573
574
575
576
577
					// faster as Math.pow
					dist += (pv.getEntry(i) - getEntry(i))
							* (pv.getEntry(i) - getEntry(i));
				}
				return Math.sqrt(dist);
578
579
			} 
			else {
580
581
582
583
584
585
				throw new AssertionError(
						"Can not compute distance between Vectors of different length!");
			}
		} else {
			throw new AssertionError("Incompatible Types!");
		}
586
587
588
589
	}

	@Override
	public float bearingTo(Location dest) {
590
		if (dest instanceof PositionVector) {
591
592
			
			// This will be the angle between the difference vector (vec to dest) and the x-axis!
593
594
595
596
597
			PositionVector t = (PositionVector) dest;
			/*
			 * Calculates the angle using atan2 - this implies that the first
			 * two dimensions in your vector are the plane you are interested
			 * in.
598
			 */					
599
600
601
602
603
604
			return (float) Math.atan2(t.getEntry(1) - this.getEntry(1),
					t.getEntry(0) - this.getEntry(0));
		} else {
			throw new AssertionError(
					"Can only calculate an Angle on elements of type position vector");
		}
605
	}
606
607

	
608
609

	
610
}