Commit 3505a760 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

Gruppen grafische Darstellung in Onlinevisualisierung

parent 037fd190
Pipeline #55233 passed with stage
in 45 seconds
...@@ -58,7 +58,8 @@ public class OnlinevisualizationRenderer extends SimulationRenderer { ...@@ -58,7 +58,8 @@ public class OnlinevisualizationRenderer extends SimulationRenderer {
g.setColor(model.config.getPedestrianDefaultColor()); g.setColor(model.config.getPedestrianDefaultColor());
for (Agent ped : model.getAgents()) { for (Agent ped : model.getAgents()) {
VPoint position = ped.getPosition(); VPoint position = ped.getPosition();
g.fill(ped.getShape()); // g.fill(ped.getShape());
ped.render(g);
if (!pedestrianPositions.containsKey(ped.getId())) { if (!pedestrianPositions.containsKey(ped.getId())) {
pedestrianPositions.put(ped.getId(), new LinkedList()); pedestrianPositions.put(ped.getId(), new LinkedList());
......
...@@ -53,6 +53,6 @@ public class CentroidGroupFactory extends GroupFactory { ...@@ -53,6 +53,6 @@ public class CentroidGroupFactory extends GroupFactory {
public void elementRemoved(Pedestrian ped) { public void elementRemoved(Pedestrian ped) {
CentroidGroup group = groupCollection.removeMember(ped); CentroidGroup group = groupCollection.removeMember(ped);
System.out.printf("Remove ped %s from group %s %n", ped.getId(), group != null ? group.getID() : "noGroup"); // System.out.printf("Remove ped %s from group %s %n", ped.getId(), group != null ? group.getID() : "noGroup");
} }
} }
...@@ -100,7 +100,7 @@ public class CentroidGroupPotential implements PotentialFieldAgent { ...@@ -100,7 +100,7 @@ public class CentroidGroupPotential implements PotentialFieldAgent {
@Override @Override
public double getAgentPotential(VPoint pos, Agent pedestrian, public double getAgentPotential(VPoint pos, Agent pedestrian,
Agent otherPedestrian) { Agent otherPedestrian) {
System.out.printf("Ped1: %s, Ped1: %s %n", pedestrian.getId(), otherPedestrian.getId()); // System.out.printf("Ped1: %s, Ped1: %s %n", pedestrian.getId(), otherPedestrian.getId());
CentroidGroup group = groupCollection.getGroup(pedestrian); CentroidGroup group = groupCollection.getGroup(pedestrian);
CentroidGroup groupOther = groupCollection.getGroup(otherPedestrian); CentroidGroup groupOther = groupCollection.getGroup(otherPedestrian);
double potential = potentialFieldPedestrian.getAgentPotential(pos, double potential = potentialFieldPedestrian.getAgentPotential(pos,
......
package org.vadere.state.scenario; package org.vadere.state.scenario;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -8,6 +9,8 @@ import java.util.Random; ...@@ -8,6 +9,8 @@ import java.util.Random;
import org.apache.commons.math3.random.JDKRandomGenerator; import org.apache.commons.math3.random.JDKRandomGenerator;
import org.apache.commons.math3.random.RandomGenerator; import org.apache.commons.math3.random.RandomGenerator;
import org.vadere.state.attributes.scenario.AttributesAgent; import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.renderer.AgentDefaultRenderer;
import org.vadere.state.scenario.renderer.Renderer;
import org.vadere.util.geometry.Vector2D; import org.vadere.util.geometry.Vector2D;
import org.vadere.util.geometry.shapes.VCircle; import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VPoint; import org.vadere.util.geometry.shapes.VPoint;
...@@ -32,6 +35,7 @@ public abstract class Agent extends DynamicElement { ...@@ -32,6 +35,7 @@ public abstract class Agent extends DynamicElement {
private double freeFlowSpeed; private double freeFlowSpeed;
private final AttributesAgent attributes; private final AttributesAgent attributes;
private Renderer renderer;
public Agent(AttributesAgent attributesAgent) { public Agent(AttributesAgent attributesAgent) {
position = new VPoint(0, 0); position = new VPoint(0, 0);
...@@ -40,8 +44,22 @@ public abstract class Agent extends DynamicElement { ...@@ -40,8 +44,22 @@ public abstract class Agent extends DynamicElement {
nextTargetListIndex = 0; nextTargetListIndex = 0;
attributes = attributesAgent; attributes = attributesAgent;
setRenderer(new AgentDefaultRenderer());
} }
public void setRenderer(Renderer render){
this.renderer = render;
}
public void render(Graphics2D g){
renderer.render(this, g);
}
public void render(Graphics2D g, Color c){
renderer.render(this, g, c);
}
public Agent(AttributesAgent attributesAgent, Random random) { public Agent(AttributesAgent attributesAgent, Random random) {
this(attributesAgent); this(attributesAgent);
...@@ -69,7 +87,6 @@ public abstract class Agent extends DynamicElement { ...@@ -69,7 +87,6 @@ public abstract class Agent extends DynamicElement {
this.velocity = other.velocity; this.velocity = other.velocity;
this.freeFlowSpeed = other.freeFlowSpeed; this.freeFlowSpeed = other.freeFlowSpeed;
} }
public LinkedList<Integer> getTargets() { public LinkedList<Integer> getTargets() {
return targetIds; return targetIds;
} }
......
package org.vadere.state.scenario; package org.vadere.state.scenario;
import java.util.*;
import org.vadere.state.attributes.scenario.AttributesAgent; import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.renderer.AgentGlyphRenderer;
import org.vadere.state.scenario.renderer.PedestrianGroupRenderer;
import org.vadere.state.scenario.renderer.PedestrianTriangleRenderer;
import org.vadere.state.types.ScenarioElementType; import org.vadere.state.types.ScenarioElementType;
import org.vadere.util.geometry.shapes.VShape; import org.vadere.util.geometry.shapes.VShape;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
public class Pedestrian extends Agent { public class Pedestrian extends Agent {
/** Target ID if the pedestrian represents a target, -1 otherwise. */ /** Target ID if the pedestrian represents a target, -1 otherwise. */
...@@ -40,6 +46,7 @@ public class Pedestrian extends Agent { ...@@ -40,6 +46,7 @@ public class Pedestrian extends Agent {
isChild = false; isChild = false;
isLikelyInjured = false; isLikelyInjured = false;
groupIds = new LinkedList<>(); groupIds = new LinkedList<>();
setRenderer(new PedestrianGroupRenderer());
} }
/** /**
......
package org.vadere.state.scenario.renderer;
import org.vadere.state.scenario.Agent;
import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VShape;
import java.awt.*;
public class AgentDefaultRenderer implements ShapeRenderer{
public AgentDefaultRenderer(){
}
public void setColor(Agent a, Graphics2D g) {
g.setColor(Color.GREEN);
}
@Override
public VShape drawShape(Agent a) {
return new VCircle(a.getPosition(), a.getAttributes().getRadius());
}
@Override
public void setColor(Graphics2D g, Color c) {
g.setColor(c);
}
}
package org.vadere.state.scenario.renderer;
import org.vadere.state.scenario.Agent;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
public class AgentGlyphRenderer implements GlyphRenderer{
@Override
public void render(Agent a, Graphics2D g, Color c) {
}
@Override
public void render(Agent a, Graphics2D g) {
String s = "\u20df";
Font font = new Font("Serif", Font.PLAIN, 12);
FontRenderContext frc = g.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, s);
g.drawGlyphVector(gv, (float)a.getPosition().x, (float)a.getPosition().y);
}
}
package org.vadere.state.scenario.renderer;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VPolygon;
import java.awt.geom.Path2D;
public class FormHelper {
enum FORM {
PENTAGON,
DIAMOND,
TRIANGLE,
STAR,
STAR_X;
}
public static VPolygon getShape(int id, VPoint p, double r){
int form = id % 6;
if (form == 0){
return getPentagon(p, r);
} else if(form == 1){
return getDiamond(p, r);
} else if(form == 2) {
return getTriangle(p, r);
} else if(form == 4){
return getStar5(p, r);
} else if(form == 5){
return getStarX(p, r);
} else {
return getStar7(p, r);
}
}
public static VPolygon getPentagon(VPoint center, double r){
return polygon(center, r, 5);
}
public static VPolygon getDiamond (VPoint center, double r){
return polygon(center, r, 4);
}
public static VPolygon getTriangle(VPoint center, double r){
return polygon(center, r, 3);
}
public static VPolygon getStar7(VPoint center, double r){
return star(center, r, 7);
}
public static VPolygon polygon(VPoint center, double r, int corners){
VPoint p = new VPoint(0,r);
Path2D.Double path = new Path2D.Double();
path.moveTo(center.x + p.x, center.y + p.y);
for (int i = 0 ; i < corners ; i++) {
p = p.rotate(2 * Math.PI / corners);
path.lineTo(center.x + p.x, center.y + p.y);
}
return new VPolygon(path);
}
public static VPolygon getStar5(VPoint center, double r) {
return star(center, r, 5);
}
public static VPolygon getStarX(VPoint center, double r) {
return star(center, r, 4).rotate(center, Math.PI/4);
}
public static VPolygon star(VPoint center, double r, int corners){
VPoint p = new VPoint(0,r);
Path2D.Double path = new Path2D.Double();
path.moveTo(center.x + p.x, center.y + p.y);
double alpha = Math.PI / corners;
for (int i = 0 ; i < corners ; i++) {
p = p.rotate(alpha);
path.lineTo(center.x + p.x, center.y + p.y);
p = p.scalarMultiply(0.5);
path.lineTo(center.x + p.x, center.y + p.y);
p = p.rotate(alpha);
path.lineTo(center.x + p.x, center.y + p.y);
p = p.scalarMultiply(2.0);
path.lineTo(center.x + p.x, center.y + p.y);
}
VPolygon polygon = new VPolygon(path);
return polygon.rotate(center, -0.5*alpha);
}
}
package org.vadere.state.scenario.renderer;
public interface GlyphRenderer extends Renderer {
}
package org.vadere.state.scenario.renderer;
import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.geometry.shapes.VShape;
import java.awt.*;
import java.util.concurrent.ConcurrentHashMap;
public class PedestrianGroupRenderer implements ShapeRenderer{
private static ConcurrentHashMap<Integer, Color> color = new ConcurrentHashMap<>();
private static final Integer COLOR_NUM = 9;
@Override
public void render(Agent a, final Graphics2D g){
g.setColor(getColor(a));
g.fill(drawShape(a));
}
@Override
public VShape drawShape(Agent a) {
// return FormHelper.getStarX(a.getPosition(), a.getRadius());
Pedestrian ped = (Pedestrian)a;
return FormHelper.getShape(ped.getGroupIds().getFirst(), ped.getPosition(), ped.getRadius());
}
private Color getHSBColor(int groupId){
float hue = ((float)(groupId) / COLOR_NUM);
// System.out.printf("groupId: %d | hue: %f%n",groupId, hue);
return new Color(Color.HSBtoRGB(hue, 1f, 0.75f));
}
private Color getColor(Agent a){
Pedestrian ped = (Pedestrian)a;
int groupId = ped.getGroupIds().getFirst();
Color c = color.get(groupId);
if (c == null){
c = getHSBColor(groupId);
color.put(groupId, c);
}
return c;
}
public void setColor(Agent a, Graphics2D g) {
g.setColor(getColor(a));
}
}
package org.vadere.state.scenario.renderer;
import org.vadere.state.scenario.Agent;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
public class PedestrianTriangleRenderer extends AgentDefaultRenderer {
@Override
public VShape drawShape(Agent a) {
VPoint pos = a.getPosition();
double r = a.getRadius();
return FormHelper.getTriangle(a.getPosition(), a.getRadius());
}
}
package org.vadere.state.scenario.renderer;
import org.vadere.state.scenario.Agent;
import java.awt.*;
public interface Renderer{
void render(final Agent a, final Graphics2D g, Color c);
void render(final Agent a, final Graphics2D g);
default void setColor(final Graphics2D g, Color c){
g.setColor(c);
}
}
package org.vadere.state.scenario.renderer;
import org.vadere.state.scenario.Agent;
import org.vadere.util.geometry.shapes.VShape;
import java.awt.*;
public interface ShapeRenderer extends Renderer {
VShape drawShape(final Agent a);
default void setfill(final Agent a, final Graphics2D g){
g.fill(drawShape(a));
}
default void render(final Agent a, final Graphics2D g, Color c){
g.setColor(c);
g.fill(drawShape(a));
}
default void render(final Agent a, final Graphics2D g){
g.setColor(Color.BLUE);
g.fill(drawShape(a));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment