GridPositionDistribution.java 3.45 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-2010 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.topology.placement;

import java.util.List;
import java.util.Random;
import java.util.Vector;

import de.tud.kom.p2psim.api.topology.TopologyComponent;
import de.tud.kom.p2psim.api.topology.placement.PlacementModel;
29
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import de.tudarmstadt.maki.simonstrator.api.Randoms;

/**
 * This Positioning tries to distribute the Nodes evenly on the field, making up
 * some kind of grid. Information on the total amount of to be positioned nodes
 * is therefore needed.
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, 05/08/2011
 */
public class GridPositionDistribution implements PlacementModel {

	private int numberOfComponents = 0;

	private boolean randInCell = true;

	private int placedComponents = 0;

	private PositionVector worldDimensions = null;
49
50
51
52
53
54
	
	/**
	 * Allows us to create a "larger" grid, even if we only have a smaller number of nodes.
	 * Eg., create a 3*3 grid for only 2 nodes
	 */
	private int fakeNumberOfComponents = -1;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

	private Random random = Randoms
			.getRandom(GridPositionDistribution.class);

	private List<PositionVector> positions = new Vector<PositionVector>();

	@Override
	public void addComponent(TopologyComponent comp) {
		if (worldDimensions == null) {
			worldDimensions = comp.getTopology().getWorldDimensions();
		}
		numberOfComponents++;
	}

	@Override
	public PositionVector place(TopologyComponent comp) {
		if (positions.isEmpty()) {
72
			calcPositions3D();
73
		}
74
		PositionVector pos = positions.get(placedComponents);		
75
76
77
		placedComponents = (placedComponents + 1) % numberOfComponents;
		return pos;
	}
78
79
80
81
	
	public void setRandomOffset(boolean randInCell) {
		this.randInCell = randInCell;
	}
82
83
84
85
	
	public void setFakeNumberOfComponents(int fakeNumberOfComponents) {
		this.fakeNumberOfComponents = fakeNumberOfComponents;
	}
86
87
	
	private void calcPositions3D() {
88
89
90
		if (fakeNumberOfComponents != -1) {
			numberOfComponents = fakeNumberOfComponents;
		}
91
92
		float ratio = (float) (worldDimensions.getX() / worldDimensions.getY());
		float ratio_1 = 1 / ratio;
93
94
		int anz_x = (int) Math.ceil(Math.sqrt(ratio * numberOfComponents));
		int anz_y = (int) Math.ceil( Math.sqrt(ratio_1 * numberOfComponents));
95
96
97
98
99
100
101
102
103
104
105
106
		int dist_x = (int) worldDimensions.getX() / anz_x;
		int dist_y = (int) worldDimensions.getY() / anz_y;
		for (int x = 0; x < anz_x; x++) {
			for (int y = 0; y < anz_y; y++) {
				double xcenter = x * dist_x + dist_x / 2;
				double ycenter = y * dist_y + dist_y / 2;
				if (randInCell) {
					xcenter += random.nextDouble() * dist_x / 2
							- dist_x / 4;
					ycenter += random.nextDouble() * dist_y / 2
							- dist_y / 4;
				}
107
				PositionVector vec = new PositionVector(xcenter, ycenter, 0);
108
109
110
111
112
113
				positions.add(vec);
			}
		}
	}

}