TorusPositioning.java 4.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * 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/>.
 *
 */


package de.tud.kom.p2psim.impl.network.modular.st.positioning;

import java.util.Random;

import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.impl.network.modular.db.NetMeasurementDB;
import de.tud.kom.p2psim.impl.network.modular.st.PositioningStrategy;
29
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
30
import de.tudarmstadt.maki.simonstrator.api.Randoms;
31
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

/**
 * Applies a uniformly-distributed random position on a 2-or multi-dimensional
 * torus surface. Parameters: torusDimensionSize(double), noOfDimensions(int)
 * 
 * @author Leo Nobach
 * 
 */
public class TorusPositioning implements PositioningStrategy {

	private int noOfDimensions = 2;

	double torusDimensionSize = 1d;

	double halfTorusDimensionSize = 0.5d;

	Random rand = Randoms.getRandom(TorusPosition.class);

	@Override
51
	public Location getPosition(SimHost host, NetMeasurementDB db,
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
			NetMeasurementDB.Host hostMeta) {

		double[] rawPos = getRawTorusPositionFor(host);
		assert rawPos.length == noOfDimensions : "The raw torus position returned by getRawTorusPositionFor(Host) does not return an array of length "
				+ noOfDimensions
				+ ", instead it returns one with the length "
				+ rawPos.length;

		return new TorusPosition(rawPos);

	}

	/**
	 * May be overridden for more detailled torus positioning.
	 * 
	 * @param host
	 * @return
	 */
	protected double[] getRawTorusPositionFor(SimHost host) {
		double[] result = new double[noOfDimensions];
		for (int i = 0; i < noOfDimensions; i++)
			result[i] = rand.nextDouble() * halfTorusDimensionSize * 2;
		return result;
	}

	/**
	 * To be set by the configurator.
	 * 
	 * @param noOfDimensions
	 */
	public void setNoOfDimensions(int noOfDimensions) {
		this.noOfDimensions = 2;
	}

	/**
	 * To be set by the configurator.
	 * 
	 * @param torusDimensionSize
	 */
	public void setTorusDimensionSize(double torusDimensionSize) {
		this.torusDimensionSize = torusDimensionSize;
		this.halfTorusDimensionSize = torusDimensionSize * 0.5d;
	}

96
	public class TorusPosition extends PositionVector {
97
98
99
100
101
102
103


		TorusPosition(double[] rawPos) {
			super(rawPos);
		}

		@Override
104
		public double distanceTo(Location netPosition) {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
			if (!(netPosition instanceof PositionVector))
				throw new AssertionError(
						"Can not calculate distances between different position classes: "
								+ this.getClass() + " and "
								+ netPosition.getClass());
			TorusPosition other = (TorusPosition) netPosition;

			double accuDistanceSq = 0;

			for (int i = 0; i < getDimensions(); i++) {
				double distDim = getEntry(i) - other.getEntry(i);
				if (distDim > halfTorusDimensionSize) {
					// we wrap the torus
					distDim = torusDimensionSize - distDim;
				} else if (distDim < -halfTorusDimensionSize) {
					// we wrap the torus, other way
					distDim = -torusDimensionSize + distDim;
				}
				accuDistanceSq += distDim * distDim;
			}

			return Math.sqrt(accuDistanceSq);

		}

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

		public double[] getRawPos() {
			return asDoubleArray();
		}

		@Override
		public TorusPosition clone() {
			return new TorusPosition(asDoubleArray());
		}

	}

	@Override
	public void writeBackToXML(BackWriter bw) {
		bw.writeSimpleType("torusDimensionSize", torusDimensionSize);
		bw.writeSimpleType("torusDimensionSize", torusDimensionSize);
	}

}