void loadPedestrianFile(string filename) { var sr = new StreamReader(Application.dataPath + "/" + filename); var fileContents = sr.ReadToEnd(); sr.Close(); PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader>(); string[] lines = fileContents.Split("\n"[0]); foreach (string line in lines) { string[] v = line.Split(' '); if (v.Length >= 3) { decimal time; int id; float x; float y; decimal.TryParse(v[0], out time); int.TryParse(v[1], out id); float.TryParse(v[2], out x); float.TryParse(v[3], out y); pl.addPedestrianPosition(new PedestrianPosition(id, time, x, y)); } } pl.createPedestrians(); }
void loadPedestrianFile(string filename) { if (!System.IO.File.Exists(filename)) { Debug.Log("Error: File " + filename + " not found."); return; } XmlDocument xmlDocTraj = new XmlDocument(); xmlDocTraj.LoadXml(System.IO.File.ReadAllText(filename)); PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader>(); XmlNode trajectories = xmlDocTraj.SelectSingleNode("//trajectories"); int fps = 8, FrameID; //8 is used as a default framerate. Actual value is read from xmlDocTraj foreach (XmlElement header in trajectories.SelectNodes("header")) { XDocument doc = XDocument.Load(filename); var fpsInput = doc.Descendants("frameRate"); foreach (var fpsVal in fpsInput) { if (float.Parse(fpsVal.Value) != 0) { fps = (int)float.Parse(fpsVal.Value); } } } foreach (XmlElement frame in trajectories.SelectNodes("frame")) { int.TryParse(frame.GetAttribute("ID"), out FrameID); if (FrameID % fps == 0) { foreach (XmlElement agent in frame) { decimal time; int id; float x; float y; float z; decimal.TryParse(frame.GetAttribute("ID"), out time); int.TryParse(agent.GetAttribute("ID"), out id); float.TryParse(agent.GetAttribute("x"), out x); float.TryParse(agent.GetAttribute("y"), out y); float.TryParse(agent.GetAttribute("z"), out z); pl.addPedestrianPosition(new PedestrianPosition(id, time / fps, x, y, z)); } } } pl.createPedestrians(); }
void Start() { gameObject.AddComponent <BoxCollider>(); transform.Rotate(0, 90, 0); myColor = new Color(Random.value, Random.value, Random.value); GetComponentInChildren <Renderer>().materials[1].color = myColor; addTile(); it = GameObject.Find("InfoText").GetComponent <InfoText>(); pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader>(); pc = GameObject.Find("PlaybackControl").GetComponent <PlaybackControl>(); r = GetComponentInChildren <Renderer>() as Renderer; gl = GameObject.Find("GeometryLoader").GetComponent <GeometryLoader>(); gp = gl.groundplane; }
void OnGUI() { string btnText = "PLAY"; if (playing) { btnText = "PAUSE"; } if (GUI.Button(new Rect(40, 20, 120, 30), btnText)) { playing = !playing; } current_time = (decimal)GUI.HorizontalSlider(new Rect(170, 30, 400, 30), (float)current_time, 0.0f, (float)total_time); btnText = "show trajectories"; if (trajectoriesShown) { btnText = "hide trajectories"; } if (GUI.Button(new Rect(40, 60, 120, 30), btnText)) { PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader>(); if (trajectoriesShown) { foreach (GameObject p in pl.pedestrians) { p.GetComponent <Pedestrian>().hideTrajectory(); } trajectoriesShown = false; } else { foreach (GameObject p in pl.pedestrians) { p.GetComponent <Pedestrian>().showTrajectory(); } trajectoriesShown = true; } } tileColoringMode = TileColoringMode.TileColoringDensity; GUIStyle style = new GUIStyle(); style.normal.background = new Texture2D(1, 1, TextureFormat.RGB24, false); style.normal.background.SetPixel(0, 0, new Color(1f, 0, 0)); style.normal.background.Apply(); GUI.Label(new Rect(40, 150, 5, 30), string.Empty, style); GUI.Label(new Rect(65, 145, 30, 30), "1,50"); style.normal.background.SetPixel(0, 0, new Color(1f, 0.6f, 0)); style.normal.background.Apply(); GUI.Label(new Rect(40, 180, 5, 30), string.Empty, style); GUI.Label(new Rect(65, 175, 30, 30), "1,25"); style.normal.background.SetPixel(0, 0, new Color(1f, 1f, 0)); style.normal.background.Apply(); GUI.Label(new Rect(40, 210, 5, 30), string.Empty, style); GUI.Label(new Rect(65, 205, 30, 30), "1,00"); style.normal.background.SetPixel(0, 0, new Color(0.6f, 1f, 0)); style.normal.background.Apply(); GUI.Label(new Rect(40, 240, 5, 30), string.Empty, style); GUI.Label(new Rect(65, 235, 30, 30), "0,75"); style.normal.background.SetPixel(0, 0, new Color(0, 1f, 0)); style.normal.background.Apply(); GUI.Label(new Rect(40, 270, 5, 30), string.Empty, style); GUI.Label(new Rect(65, 265, 30, 30), "0,50"); style.normal.background.SetPixel(0, 0, new Color(0, 0.6f, 1f)); style.normal.background.Apply(); GUI.Label(new Rect(40, 300, 5, 30), string.Empty, style); GUI.Label(new Rect(65, 295, 30, 30), "0,25"); }
void OnGUI() { playing = GUI.Toggle(new Rect(30, 25, 100, 30), playing, " PLAY"); current_time = (decimal)GUI.HorizontalSlider(new Rect(100, 30, 400, 30), (float)current_time, 0.0f, (float)total_time); string btnText = "show trajectories"; if (trajectoriesShown) { btnText = "hide trajectories"; } if (GUI.Button(new Rect(510, 20, 120, 30), btnText)) { PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader>(); if (trajectoriesShown) { foreach (GameObject p in pl.pedestirans) { p.GetComponent <Pedestrian>().hideTrajectory(); } trajectoriesShown = false; } else { foreach (GameObject p in pl.pedestirans) { p.GetComponent <Pedestrian>().showTrajectory(); } trajectoriesShown = true; } } GeometryLoader gl = GameObject.Find("GeometryLoader").GetComponent <GeometryLoader> (); Groundplane gp = gl.groundplane; btnText = "add line"; if (drawLine) { GUI.color = Color.red; } if (lineIsDrawn) { btnText = "remove line"; } if (GUI.Button(new Rect(640, 20, 80, 30), btnText)) { if (lineIsDrawn) { gp.removeLine(); drawLine = false; lineIsDrawn = false; } else { drawLine = !drawLine; if (!drawLine) { gp.removeLine(); } } } GUI.color = Color.white; if (tiles == 0) { btnText = "colors by speed"; } if (tiles == 1) { btnText = "colors by density"; } if (tiles == 2) { btnText = "hide colors"; } if (GUI.Button(new Rect(730, 20, 120, 30), btnText)) { tiles = (tiles + 1) % 3; InfoText it = GameObject.Find("InfoText").GetComponent <InfoText> (); if (it.diagram) { it.removeDiagram(); } if (tiles == 0) { tileColoringMode = TileColoringMode.TileColoringNone; } if (tiles == 1) { tileColoringMode = TileColoringMode.TileColoringSpeed; } if (tiles == 2) { tileColoringMode = TileColoringMode.TileColoringDensity; } } if (tileColoringMode == TileColoringMode.TileColoringDensity) { threshold = GUI.HorizontalSlider(new Rect(730, 55, 120, 30), threshold, 0.0f, 6.0f); GUI.Label(new Rect(730, 70, 120, 30), "Threshold: " + System.Math.Round(threshold, 2) + "/m²"); } }
private void drawDiagram(Rect position) { diagram = true; FundamentalDiagram fd = GameObject.Find("FundamentalDiagram").GetComponent <FundamentalDiagram> (); if (fd.fundamental) { fd.removeFundamentalDiagram(); } diagramPosition = position; diagramLines = new List <VectorLine> (); PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader> (); float maxSpeed = float.MinValue; float maxDensity = float.MinValue; List <Vector2> points = new List <Vector2> (); foreach (GameObject p in pl.pedestirans) { Pedestrian ped = p.GetComponent <Pedestrian>(); Renderer r = ped.GetComponentInChildren <Renderer>() as Renderer; if (r.enabled) { float speed = ped.getSpeed(); float density = ped.getDensity(); maxSpeed = Mathf.Max(speed, maxSpeed); maxDensity = Mathf.Max(density, maxDensity); points.Add(new Vector2(speed, density)); } } for (int i = 0; i < points.Count; i++) { points[i] = new Vector2(points[i].x * position.width / maxSpeed + position.x, points[i].y * position.height / maxDensity + position.y); } PlaybackControl pc = GameObject.Find("PlaybackControl").GetComponent <PlaybackControl> (); //data points VectorPoints.SetCamera(GameObject.Find("Flycam").camera); Vector2[] dp = new Vector2[(int)pc.total_time]; for (int i = 0; i < dp.Length; i++) { dp[i] = new Vector2(position.x + position.width * ((float)(i / pc.total_time)), position.y); } dataPoints = VectorLine.SetLine(Color.white, dp); dataPoints.depth = 99; dataPoints.Draw(); diagramLines.Add(dataPoints); //frame VectorLine line = VectorLine.SetLine(new Color(1f, 1f, 1f, 0.5f), new Vector2[] { new Vector2(position.x - 6, position.y - 2), new Vector2(position.x - 6, position.y + 9 + position.height), new Vector2(position.x + 5 + position.width, position.y + 9 + position.height), new Vector2(position.x + 5 + position.width, position.y - 2), new Vector2(position.x - 6, position.y - 2) }); line.depth = 2; line.Draw(); diagramLines.Add(line); int maxTime = (int)pc.total_time; //lines xLabels = new List <Label> (); float scaleFactor = 10f; int numberOfLines = (int)(maxTime * (1 / scaleFactor)); if (numberOfLines > 0) { for (int i = 1; i <= numberOfLines; i++) { float hx = position.x + i * (position.width / (maxTime * (1 / scaleFactor))); line = VectorLine.SetLine(new Color(1f, 1f, 1f, 0.5f), new Vector2[] { new Vector2(hx, position.y - 2), new Vector2(hx, position.y + 8 + position.height) }); line.depth = 1; line.Draw(); diagramLines.Add(line); xLabels.Add(new Label(new Rect(Screen.width - position.x - position.width + hx - 44, Screen.height - position.y, 28, 28), "" + i * scaleFactor)); } } float tx = ((float)(pc.current_time / pc.total_time)) * position.width + position.x; timeIndicator = VectorLine.SetLine(Color.red, new Vector2[] { new Vector2(tx, position.y - 2), new Vector2(tx, position.y + 8 + position.height) }); //background line = VectorLine.SetLine(new Color(0, 0, 0, 0.7f), new Vector2[] { new Vector2(position.x - 6, position.y + (position.height + 6) / 2), new Vector2(position.x + position.width + 6, position.y + (position.height + 6) / 2) }); line.lineWidth = position.height + 12; line.Draw(); diagramLines.Add(line); }
void OnGUI() { PlaybackControl pc = GameObject.Find("PlaybackControl").GetComponent <PlaybackControl> (); PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader> (); GeometryLoader gl = GameObject.Find("GeometryLoader").GetComponent <GeometryLoader> (); Groundplane gp = gl.groundplane; infos = new List <Entry> (); string text = ""; string minutes = Mathf.Floor((float)pc.current_time / 60).ToString("00");; string seconds = ((float)pc.current_time % 60).ToString("00");; text += "current time: " + minutes + ":" + seconds + "\n"; if (pl.population != null) { infos.Add(new Entry("population", "", pl.population[(int)pc.current_time], 0, true, pl.pedestirans.Count)); } if (gp.point1active && gp.point2active) { infos.Add(new Entry("line length", "m", Vector3.Distance(gp.point1, gp.point2), 2, false, 0)); infos.Add(new Entry("line crossings", "", gp.lineCrossed, 0, true, pl.pedestirans.Count)); infos.Add(new Entry("line flow", "/s", gp.crossings.Count, 0, true, 10.0f)); infos.Add(new Entry("avg. crossing speed", "m/s", gp.crossingSpeed, 2, true, 3.0f)); infos.Add(new Entry("current flow", "/ms", gp.crossings.Count / Vector3.Distance(gp.point1, gp.point2), 2, true, 3.0f)); } if (pc.tileColoringMode == TileColoringMode.TileColoringSpeed) { infos.Add(new Entry("current speed", "m/s", currentValue(currentSpeed), 2, true, 3.0f)); infos.Add(new Entry("min. speed", "m/s", minSpeed, 2, false, 3.0f)); infos.Add(new Entry("max. speed", "m/s", maxSpeed, 2, false, 3.0f)); } else { maxSpeed = float.MinValue; minSpeed = float.MaxValue; } if (pc.tileColoringMode == TileColoringMode.TileColoringDensity) { infos.Add(new Entry("current density", "/m²", currentValue(currentDensity), 2, true, 3.0f)); infos.Add(new Entry("min. density", "/m²", minDenstiy, 2, false, 5.0f)); infos.Add(new Entry("max. density", "/m²", maxDensity, 2, false, 5.0f)); } else { maxDensity = float.MinValue; minDenstiy = float.MaxValue; crossings = 0; } for (int i = 0; i < infos.Count; i++) { Entry e = infos[i]; text += infos[i].name + ": " + System.Math.Round(e.value, e.decimals) + e.unit + "\n"; if (e.graphable) { if (GUI.Toggle(new Rect(Screen.width * (transform.position.x) - 20, Screen.height * (1 - transform.position.y) - (15 * (infos.Count - i) + 17), 100, 15), i == activeEntry, "") && i != activeEntry) { removeDiagram(); activeEntry = i; Rect position = new Rect(30, 30, 400, 300); position.x = Screen.width - position.x - position.width; drawDiagram(position); } } } guiText.text = text; if (diagram) { GUI.Label(new Rect(diagramPosition.x, Screen.height - diagramPosition.y - diagramPosition.height - 30, diagramPosition.width, 30), infos[activeEntry].name); GUI.Label(new Rect(diagramPosition.x - 15, Screen.height - diagramPosition.y, 30, 30), "[s]"); GUI.Label(new Rect(diagramPosition.x - 35, Screen.height - diagramPosition.y - diagramPosition.height - 10, 30, 30), System.Math.Round(infos[activeEntry].maxValue, infos[activeEntry].decimals) + ""); if (infos[activeEntry].unit != "") { GUI.Label(new Rect(diagramPosition.x - 35, Screen.height - diagramPosition.y - diagramPosition.height - 30, 30, 30), "[" + infos[activeEntry].unit + "]"); } } }
private void drawFundamentalDiagram(Rect position) { pc.playing = false; InfoText it = GameObject.Find("InfoText").GetComponent <InfoText> (); if (it.diagram) { it.removeDiagram(); } fundamentalDiagramLines = new List <VectorLine> (); PedestrianLoader pl = GameObject.Find("PedestrianLoader").GetComponent <PedestrianLoader> (); float maxSpeed = float.MinValue; float maxDensity = float.MinValue; List <Vector2> points = new List <Vector2> (); foreach (GameObject p in pl.pedestirans) { Pedestrian ped = p.GetComponent <Pedestrian>(); if (p.hideFlags != HideFlags.HideInHierarchy) { float speed = ped.getSpeed(); float density = ped.getDensity(); maxSpeed = Mathf.Max(speed, maxSpeed); maxDensity = Mathf.Max(density, maxDensity); points.Add(new Vector2(density, speed)); } } if (points.Count == 0) { fundamental = false; return; } VectorPoints.SetCamera(GameObject.Find("Flycam").GetComponent <Camera>()); //trendline int steps = 5; float stepper = maxDensity / steps; float[] avgSpeed = new float [steps]; int[] avgNumber = new int [steps]; for (int i = 0; i < points.Count; i++) { int j = (int)(points[i].y / stepper); if (j < avgNumber.Length) { avgNumber[j]++; avgSpeed[j] = avgSpeed[j] + points[i].x; } } List <Vector2> l = new List <Vector2> (); for (int i = 0; i < steps; i++) { avgSpeed[i] = avgSpeed[i] / avgNumber[i]; Vector2 a = new Vector2((i * stepper + (i + 1) * stepper) / 2, avgSpeed[i]); if (avgSpeed[i] > 0) { l.Add(new Vector2(a.x * position.width / maxDensity + position.x, a.y * position.height / maxSpeed + position.y)); } } VectorLine line; if (l.Count > 1) { line = new VectorLine("spline", new Vector2[l.Count], null, 1, LineType.Continuous); line.SetColor(Color.red); line.MakeSpline(l.ToArray()); line.depth = 99; line.Draw(); fundamentalDiagramLines.Add(line); } //data points for (int i = 0; i < points.Count; i++) { points[i] = new Vector2(points[i].x * position.width / maxDensity + position.x, points[i].y * position.height / maxSpeed + position.y); } fundamentalDiagramPoints = new Vectrosity.VectorPoints("Data", points.ToArray(), Color.white, null, 3); fundamentalDiagramPoints.depth = 99; fundamentalDiagramPoints.Draw(); //frame line = VectorLine.SetLine(new Color(1f, 1f, 1f, 0.5f), new Vector2[] { new Vector2(position.x - 6, position.y - 2), new Vector2(position.x - 6, position.y + 9 + position.height), new Vector2(position.x + 5 + position.width, position.y + 9 + position.height), new Vector2(position.x + 5 + position.width, position.y - 2), new Vector2(position.x - 6, position.y - 2) }); line.depth = 2; line.Draw(); fundamentalDiagramLines.Add(line); //lines yLabels = new List <Label> (); float scaleFactor = 0.5f; int numberOfLines = (int)(maxSpeed * (1 / scaleFactor)); if (numberOfLines > 0) { for (int i = 1; i <= numberOfLines; i++) { float hy = position.y + i * (position.height / (maxSpeed * (1 / scaleFactor))); line = VectorLine.SetLine(new Color(1f, 1f, 1f, 0.5f), new Vector2[] { new Vector2(position.x - 5, hy), new Vector2(position.x + 5 + position.width, hy) }); line.depth = 1; line.Draw(); fundamentalDiagramLines.Add(line); yLabels.Add(new Label(new Rect(position.x - 28, Screen.height - hy - 12, 20, 25), "" + i * scaleFactor)); } } //lines xLabels = new List <Label> (); scaleFactor = 0.5f; numberOfLines = (int)(maxDensity * (1 / scaleFactor)); if (numberOfLines > 0) { for (int i = 1; i <= numberOfLines; i++) { float hx = position.x + i * (position.width / (maxDensity * (1 / scaleFactor))); line = VectorLine.SetLine(new Color(1f, 1f, 1f, 0.5f), new Vector2[] { new Vector2(hx, position.y - 2), new Vector2(hx, position.y + 8 + position.height) }); line.depth = 1; line.Draw(); fundamentalDiagramLines.Add(line); xLabels.Add(new Label(new Rect(Screen.width - position.x - position.width + hx - 44, Screen.height - position.y, 28, 28), "" + i * scaleFactor)); } } //background line = VectorLine.SetLine(new Color(0, 0, 0, 0.7f), new Vector2[] { new Vector2(position.x - 6, position.y + (position.height + 6) / 2), new Vector2(position.x + position.width + 6, position.y + (position.height + 6) / 2) }); line.lineWidth = position.height + 12; line.Draw(); fundamentalDiagramLines.Add(line); }