Merge pull request #626 from ejoerns/pull-req/cooja-vis-advanced

[Cooja] plugins/Visualizer: Multi-mote selection functionality
This commit is contained in:
Fredrik Österlind 2014-04-11 09:57:37 +02:00
commit dbb8f3ec13
4 changed files with 926 additions and 643 deletions

View file

@ -26,13 +26,13 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.contikios.mrm;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Set;
import org.apache.log4j.Logger;
@ -50,11 +50,13 @@ import org.contikios.mrm.ChannelModel.TxPair;
@ClassDescription("Radio environment (MRM)")
@SupportedArguments(radioMediums = {MRM.class})
public class MRMVisualizerSkin implements VisualizerSkin {
private static Logger logger = Logger.getLogger(MRMVisualizerSkin.class);
private static final Logger logger = Logger.getLogger(MRMVisualizerSkin.class);
private Simulation simulation = null;
private Visualizer visualizer = null;
@Override
public void setActive(Simulation simulation, Visualizer vis) {
if (!(simulation.getRadioMedium() instanceof MRM)) {
logger.fatal("Cannot activate MRM skin for unknown radio medium: " + simulation.getRadioMedium());
@ -64,6 +66,7 @@ public class MRMVisualizerSkin implements VisualizerSkin {
this.visualizer = vis;
}
@Override
public void setInactive() {
if (simulation == null) {
/* Skin was never activated */
@ -71,21 +74,25 @@ public class MRMVisualizerSkin implements VisualizerSkin {
}
}
@Override
public Color[] getColorOf(Mote mote) {
Mote selectedMote = visualizer.getSelectedMote();
if (mote == selectedMote) {
if (visualizer.getSelectedMotes().contains(mote)) {
return new Color[]{Color.CYAN};
}
return null;
}
@Override
public void paintBeforeMotes(Graphics g) {
final Mote selectedMote = visualizer.getSelectedMote();
if (simulation == null
|| selectedMote == null
|| selectedMote.getInterfaces().getRadio() == null) {
Set<Mote> selectedMotes = visualizer.getSelectedMotes();
if (simulation == null || selectedMotes == null) {
return;
}
for (final Mote selectedMote : selectedMotes) {
if (selectedMote.getInterfaces().getRadio() == null) {
continue;
}
final Position sPos = selectedMote.getInterfaces().getPosition();
/* Paint transmission and interference range for selected mote */
@ -117,9 +124,12 @@ public class MRMVisualizerSkin implements VisualizerSkin {
}
final Radio dRadio = d.getInterfaces().getRadio();
TxPair txPair = new RadioPair() {
@Override
public Radio getFromRadio() {
return selectedMote.getInterfaces().getRadio();
}
@Override
public Radio getToRadio() {
return dRadio;
}
@ -149,10 +159,13 @@ public class MRMVisualizerSkin implements VisualizerSkin {
g.setColor(Color.BLACK);
g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3);
}
}
@Override
public void paintAfterMotes(Graphics g) {
}
@Override
public Visualizer getVisualizer() {
return visualizer;
}

File diff suppressed because it is too large Load diff

View file

@ -27,13 +27,13 @@
* SUCH DAMAGE.
*
*/
package org.contikios.cooja.plugins.skins;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Set;
import org.apache.log4j.Logger;
@ -52,11 +52,13 @@ import org.contikios.cooja.radiomediums.DirectedGraphMedium;
@ClassDescription("Radio environment (DGRM)")
@SupportedArguments(radioMediums = {DirectedGraphMedium.class})
public class DGRMVisualizerSkin implements VisualizerSkin {
private static Logger logger = Logger.getLogger(DGRMVisualizerSkin.class);
private static final Logger logger = Logger.getLogger(DGRMVisualizerSkin.class);
private Simulation simulation = null;
private Visualizer visualizer = null;
@Override
public void setActive(Simulation simulation, Visualizer vis) {
if (!(simulation.getRadioMedium() instanceof DirectedGraphMedium)) {
logger.fatal("Cannot activate DGRM skin for unknown radio medium: " + simulation.getRadioMedium());
@ -66,6 +68,7 @@ public class DGRMVisualizerSkin implements VisualizerSkin {
this.visualizer = vis;
}
@Override
public void setInactive() {
if (simulation == null) {
/* Skin was never activated */
@ -73,22 +76,26 @@ public class DGRMVisualizerSkin implements VisualizerSkin {
}
}
@Override
public Color[] getColorOf(Mote mote) {
Mote selectedMote = visualizer.getSelectedMote();
if (mote == selectedMote) {
if (visualizer.getSelectedMotes().contains(mote)) {
return new Color[]{Color.CYAN};
}
return null;
}
@Override
public void paintBeforeMotes(Graphics g) {
Mote selectedMote = visualizer.getSelectedMote();
if (simulation == null
|| selectedMote == null
|| selectedMote.getInterfaces().getRadio() == null) {
Set<Mote> selectedMotes = visualizer.getSelectedMotes();
if (simulation == null || selectedMotes == null) {
return;
}
for (final Mote selectedMote : selectedMotes) {
if (selectedMote.getInterfaces().getRadio() == null) {
continue;
}
/* Paint transmission and interference range for selected mote */
Position motePos = selectedMote.getInterfaces().getPosition();
@ -109,41 +116,57 @@ public class DGRMVisualizerSkin implements VisualizerSkin {
int msgWidth = fm.stringWidth(msg);
g.setColor(Color.BLACK);
g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3);
return;
continue;
}
String msg = dests.length + " edges";
int msgWidth = fm.stringWidth(msg);
g.setColor(Color.BLACK);
g.drawString(msg, x - msgWidth / 2, y + 2 * Visualizer.MOTE_RADIUS + 3);
/* Draw LQI/RSSI edges */
for (DestinationRadio r : dests) {
double prob = ((DGRMDestinationRadio) r).ratio;
double rssi = ((DGRMDestinationRadio) r).signal;
double pos_rssi = rssi + 100;
int lqi = ((DGRMDestinationRadio) r).lqi;
float red = (float) (1 - prob * pos_rssi / 90 * lqi / 100);
if(red > 1) red = 1;
if(red < 0) red = 0;
if (red > 1) {
red = 1;
}
if (red < 0) {
red = 0;
}
float green = (float) (prob * pos_rssi / 90 * lqi / 100);
if(green > 1) green = 1;
if(green < 0) green = 0;
if (green > 1) {
green = 1;
}
if (green < 0) {
green = 0;
}
if (prob == 0.0d) {
continue;
}
msg = String.format("%1.1f%%", 100.0*prob);
Position pos = r.radio.getPosition();
Point pixel = visualizer.transformPositionToPixel(pos);
msgWidth = fm.stringWidth(msg);
g.setColor(new Color(red, green, 0.0f));
g.drawString("LQI: " + lqi + " RSSI: " + rssi,(x + pixel.x)/2,(y + pixel.y)/2);
g.drawString("LQI: " + lqi, (x + pixel.x) / 2, (y + pixel.y) / 2);
g.drawString("RSSI: " + rssi, (x + pixel.x) / 2, (y + pixel.y) / 2 + g.getFontMetrics().getHeight());
g.drawLine(x, y, pixel.x, pixel.y);
/* Draw success ratio only if single mote selected */
if (selectedMotes.size() == 1) {
g.setColor(Color.BLACK);
msg = String.format("%1.1f%%", 100.0 * prob);
msgWidth = fm.stringWidth(msg);
g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
}
}
}
}
@Override
public void paintAfterMotes(Graphics g) {
}
@Override
public Visualizer getVisualizer() {
return visualizer;
}

View file

@ -27,15 +27,18 @@
* SUCH DAMAGE.
*
*/
package org.contikios.cooja.plugins.skins;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.beans.PropertyVetoException;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.Box;
@ -83,6 +86,7 @@ import org.contikios.cooja.radiomediums.UDGM;
@ClassDescription("Radio environment (UDGM)")
@SupportedArguments(radioMediums = {UDGM.class})
public class UDGMVisualizerSkin implements VisualizerSkin {
private static final Logger logger = Logger.getLogger(UDGMVisualizerSkin.class);
private static final Color COLOR_TX = new Color(0, 255, 0, 100);
@ -154,8 +158,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
txRangeSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
radioMedium.setTxRange(((SpinnerNumberModel)
txRangeSpinner.getModel()).getNumber().doubleValue());
radioMedium.setTxRange(((SpinnerNumberModel) txRangeSpinner.getModel())
.getNumber().doubleValue());
visualizer.repaint();
}
});
@ -163,8 +167,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
interferenceRangeSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
radioMedium.setInterferenceRange(((SpinnerNumberModel)
interferenceRangeSpinner.getModel()).getNumber().doubleValue());
radioMedium.setInterferenceRange(((SpinnerNumberModel) interferenceRangeSpinner.getModel())
.getNumber().doubleValue());
visualizer.repaint();
}
});
@ -172,8 +176,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
successRatioTxSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel)
successRatioTxSpinner.getModel()).getNumber().doubleValue();
radioMedium.SUCCESS_RATIO_TX = ((SpinnerNumberModel) successRatioTxSpinner.getModel())
.getNumber().doubleValue();
visualizer.repaint();
}
});
@ -181,8 +185,8 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
successRatioRxSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel)
successRatioRxSpinner.getModel()).getNumber().doubleValue();
radioMedium.SUCCESS_RATIO_RX = ((SpinnerNumberModel) successRatioRxSpinner.getModel())
.getNumber().doubleValue();
visualizer.repaint();
}
});
@ -259,8 +263,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
@Override
public Color[] getColorOf(Mote mote) {
Mote selectedMote = visualizer.getSelectedMote();
if (mote == selectedMote) {
if (visualizer.getSelectedMotes().contains(mote)) {
return new Color[]{Color.CYAN};
}
return null;
@ -268,13 +271,21 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
@Override
public void paintBeforeMotes(Graphics g) {
Mote selectedMote = visualizer.getSelectedMote();
if (simulation == null
|| selectedMote == null
|| selectedMote.getInterfaces().getRadio() == null) {
Set<Mote> selectedMotes = visualizer.getSelectedMotes();
if (simulation == null || selectedMotes == null) {
return;
}
Area intRangeArea = new Area();
Area intRangeMaxArea = new Area();
Area trxRangeArea = new Area();
Area trxRangeMaxArea = new Area();
for (Mote selectedMote : selectedMotes) {
if (selectedMote.getInterfaces().getRadio() == null) {
continue;
}
/* Paint transmission and interference range for selected mote */
Position motePos = selectedMote.getInterfaces().getPosition();
@ -284,24 +295,24 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
// Fetch current output power indicator (scale with as percent)
Radio selectedRadio = selectedMote.getInterfaces().getRadio();
double moteInterferenceRange =
radioMedium.INTERFERENCE_RANGE
double moteInterferenceRange
= radioMedium.INTERFERENCE_RANGE
* ((double) selectedRadio.getCurrentOutputPowerIndicator()
/ (double) selectedRadio.getOutputPowerIndicatorMax());
double moteTransmissionRange =
radioMedium.TRANSMITTING_RANGE
double moteTransmissionRange
= radioMedium.TRANSMITTING_RANGE
* ((double) selectedRadio.getCurrentOutputPowerIndicator()
/ (double) selectedRadio.getOutputPowerIndicatorMax());
Point translatedZero = visualizer.transformPositionToPixel(0.0, 0.0, 0.0);
Point translatedInterference =
visualizer.transformPositionToPixel(moteInterferenceRange, moteInterferenceRange, 0.0);
Point translatedTransmission =
visualizer.transformPositionToPixel(moteTransmissionRange, moteTransmissionRange, 0.0);
Point translatedInterferenceMax =
visualizer.transformPositionToPixel(radioMedium.INTERFERENCE_RANGE, radioMedium.INTERFERENCE_RANGE, 0.0);
Point translatedTransmissionMax =
visualizer.transformPositionToPixel(radioMedium.TRANSMITTING_RANGE, radioMedium.TRANSMITTING_RANGE, 0.0);
Point translatedInterference
= visualizer.transformPositionToPixel(moteInterferenceRange, moteInterferenceRange, 0.0);
Point translatedTransmission
= visualizer.transformPositionToPixel(moteTransmissionRange, moteTransmissionRange, 0.0);
Point translatedInterferenceMax
= visualizer.transformPositionToPixel(radioMedium.INTERFERENCE_RANGE, radioMedium.INTERFERENCE_RANGE, 0.0);
Point translatedTransmissionMax
= visualizer.transformPositionToPixel(radioMedium.TRANSMITTING_RANGE, radioMedium.TRANSMITTING_RANGE, 0.0);
translatedInterference.x = Math.abs(translatedInterference.x - translatedZero.x);
translatedInterference.y = Math.abs(translatedInterference.y - translatedZero.y);
@ -313,47 +324,59 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
translatedTransmissionMax.y = Math.abs(translatedTransmissionMax.y - translatedZero.y);
/* Interference range */
g.setColor(COLOR_INT);
g.fillOval(
intRangeArea.add(new Area(new Ellipse2D.Double(
x - translatedInterference.x,
y - translatedInterference.y,
2 * translatedInterference.x,
2 * translatedInterference.y);
2 * translatedInterference.y)));
/* Transmission range */
g.setColor(COLOR_TX);
g.fillOval(
/* Interference range (MAX) */
trxRangeArea.add(new Area(new Ellipse2D.Double(
x - translatedTransmission.x,
y - translatedTransmission.y,
2 * translatedTransmission.x,
2 * translatedTransmission.y);
2 * translatedTransmission.y)));
/* Interference range (MAX) */
g.setColor(Color.GRAY);
g.drawOval(
intRangeMaxArea.add(new Area(new Ellipse2D.Double(
x - translatedInterferenceMax.x,
y - translatedInterferenceMax.y,
2 * translatedInterferenceMax.x,
2 * translatedInterferenceMax.y);
2 * translatedInterferenceMax.y)));
/* Transmission range (MAX) */
g.drawOval(
trxRangeMaxArea.add(new Area(new Ellipse2D.Double(
x - translatedTransmissionMax.x,
y - translatedTransmissionMax.y,
2 * translatedTransmissionMax.x,
2 * translatedTransmissionMax.y);
2 * translatedTransmissionMax.y)));
}
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(COLOR_INT);
g2d.fill(intRangeArea);
g.setColor(Color.GRAY);
g2d.draw(intRangeMaxArea);
g.setColor(COLOR_TX);
g2d.fill(trxRangeArea);
g.setColor(Color.GRAY);
g2d.draw(trxRangeMaxArea);
FontMetrics fm = g.getFontMetrics();
g.setColor(Color.BLACK);
/* Print transmission success probabilities */
/* Print transmission success probabilities only if single mote is selected */
if (selectedMotes.size() == 1) {
Mote selectedMote = selectedMotes.toArray(new Mote[0])[0];
Radio selectedRadio = selectedMote.getInterfaces().getRadio();
for (Mote m : simulation.getMotes()) {
if (m == selectedMote) {
continue;
}
double prob =
((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio());
double prob
= ((UDGM) simulation.getRadioMedium()).getSuccessProbability(selectedRadio, m.getInterfaces().getRadio());
if (prob == 0.0d) {
continue;
}
@ -363,6 +386,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
int msgWidth = fm.stringWidth(msg);
g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
}
}
}
@ -371,6 +395,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
}
public static class RangeMenuAction implements SimulationMenuAction {
@Override
public boolean isEnabled(Visualizer visualizer, Simulation simulation) {
return true;
@ -396,6 +421,7 @@ public class UDGMVisualizerSkin implements VisualizerSkin {
};
public static class SuccessRatioMenuAction implements SimulationMenuAction {
@Override
public boolean isEnabled(Visualizer visualizer, Simulation simulation) {
return true;