/* * 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 . * */ package de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction; import java.util.LinkedList; import java.util.List; import java.util.Random; import de.tud.kom.p2psim.api.scenario.ConfigurationException; import de.tud.kom.p2psim.api.topology.Topology; import de.tud.kom.p2psim.impl.topology.util.PositionVector; import de.tudarmstadt.maki.simonstrator.api.Binder; import de.tudarmstadt.maki.simonstrator.api.Randoms; import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.AttractionPoint; import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor; /** * Implementation of the interface {@link AttractionGenerator}. * * Generates the given number of {@link AttractionPoint}s and sets the * position randomly within the world dimensions. * * v1.1: statically available attraction points [JZ] * v1.2: added a minimum distance between attraction points [JZ] * v1.21: added a random radius to attraction points. Minimum is 10 meters, default maximum 100 meters. APs cannot have overlapping radii. * * @author Christoph Muenker, Julian Zobel * @version 1.21, 12 2018 */ public class RandomAttractionGenerator implements IAttractionGenerator { private Random rand; private PositionVector worldDimension; private int numberOfAttractionPoints; private boolean numberOfAPsSet = false; private double minimumDistance = 50; private double maximumRadius = 100; private double minimumRadius = 10; @XMLConfigurableConstructor({"numberOfAttractionPoints"}) public RandomAttractionGenerator(int numberOfAttractionPoints) { this.rand = Randoms.getRandom(RandomAttractionGenerator.class); this.worldDimension = Binder.getComponentOrNull(Topology.class) .getWorldDimensions(); if (numberOfAttractionPoints <= 0) { throw new ConfigurationException( "NumberOfAttractionPoints should be at least 1!"); } this.numberOfAPsSet = true; this.numberOfAttractionPoints = numberOfAttractionPoints; attractionPoints.clear(); createAttractionPoints(); } @Override public List getAttractionPoints() { if (!numberOfAPsSet) { throw new ConfigurationException( "Number of Attraction Points is not set in RandomAttractionGenerator!"); } if(attractionPoints.isEmpty()) createAttractionPoints(); return attractionPoints; } private void createAttractionPoints() { List result = new LinkedList(); // make a break counter to prevent more than 10 iterations and an infinity loop in general. int c = 0; create: for (int i = 0; i < numberOfAttractionPoints; i++) { PositionVector posVec = createPosVec(); // set the radius of this attraction point // minimum radius is 10 meters double radius = Math.max(minimumRadius, rand.nextDouble() * maximumRadius); if(c < 20) { // if not within the world dimensions, directly go back to calculation if(posVec.getX() + radius > worldDimension.getX() || posVec.getY() + radius > worldDimension.getY() || posVec.getX() - radius < 0 || posVec.getY() - radius < 0) { i--; c++; continue create; } for (AttractionPoint ap : result) { // if this point is closer than the given minimum distance to another point, or the radii of the points would overlap, // or if the radius would exceed the simulation area // then discard this attraction point and create a new one if(posVec.distanceTo(ap) < minimumDistance || (posVec.distanceTo(ap) - radius - ap.getRadius()) < 0 ) { i--; c++; continue create; } } } else { radius = 10; } AttractionPoint aPoint = new AttractionPointImpl("AP-"+i, posVec); aPoint.setRadius(radius); c = 0; result.add(aPoint); } attractionPoints.clear(); attractionPoints.addAll(result); } private PositionVector createPosVec() { double x = rand.nextDouble() * worldDimension.getX(); double y = rand.nextDouble() * worldDimension.getY(); return new PositionVector(x, y); } /** * Set a minimum distance between the randomly generated attraction points * @param distance */ public void setMinimumDistance(double distance) { this.minimumDistance = distance; } /** * Set a maximum radius that an attraction point can have. Minimum is 10 meters. * @param radius */ public void setMaximumRadius(double radius) { this.maximumRadius = radius; } /** * Set a maximum radius that an attraction point can have. Minimum is 10 meters. * @param radius */ public void setMinimumRadius(double radius) { this.minimumRadius = radius; } }