/// <summary> /// Adds an agent node to the json /// </summary> /// <param name="data">Json object where data will be added</param> /// <param name="scenarioAgent">Scenario agent to serialize</param> private static void SerializeAgentNode(JSONNode data, ScenarioAgent scenarioAgent) { var agentNode = new JSONObject(); data.Add(agentNode); if (scenarioAgent.Variant is CloudAgentVariant cloudVariant) { agentNode.Add("id", new JSONString(cloudVariant.guid)); } agentNode.Add("uid", new JSONString(scenarioAgent.Uid)); agentNode.Add("variant", new JSONString(scenarioAgent.Variant.Name)); agentNode.Add("type", new JSONNumber(scenarioAgent.Source.AgentTypeId)); agentNode.Add("parameterType", new JSONString(scenarioAgent.Source.ParameterType)); var transform = new JSONObject(); agentNode.Add("transform", transform); var position = new JSONObject().WriteVector3(scenarioAgent.TransformToMove.position); transform.Add("position", position); var rotation = new JSONObject().WriteVector3(scenarioAgent.TransformToRotate.rotation.eulerAngles); transform.Add("rotation", rotation); foreach (var extension in scenarioAgent.Extensions) { extension.Value.SerializeToJson(agentNode); } }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="previousElement">Scenario element that has been deselected</param> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement previousElement, ScenarioElement selectedElement) { //Detach from current agent events if (colorExtension != null) { colorExtension.ColorChanged -= SelectedAgentOnColorChanged; } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { colorExtension = selectedAgent.GetExtension <AgentColorExtension>(); if (colorExtension == null) { Hide(); } else { colorExtension.ColorChanged += SelectedAgentOnColorChanged; Show(); } } else { Hide(); } }
/// <inheritdoc/> public override IEnumerator Apply(PlaybackPanel playbackPanel, TriggerEffector triggerEffector, ITriggerAgent triggerAgent) { if (!(triggerEffector is TimeToCollisionEffector ttcEffector)) { ScenarioManager.Instance.logPanel.EnqueueError( $"Invalid trigger effector passed to the {GetType().Name}."); yield break; } var egos = ScenarioManager.Instance.GetExtension <ScenarioAgentsManager>().Agents .Where(a => a.Source.AgentType == AgentType.Ego); ScenarioAgent collisionEgo = null; var lowestTTC = TimeToCollisionEffector.TimeToCollisionLimit; foreach (var ego in egos) { var ttc = ttcEffector.CalculateTTC(ego, triggerAgent, triggerAgent.MovementSpeed); if (ttc >= lowestTTC || ttc < 0.0f) { continue; } lowestTTC = ttc; collisionEgo = ego; } yield return(playbackPanel.StartCoroutine(ttcEffector.Apply(lowestTTC, collisionEgo, triggerAgent))); }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="previousElement">Scenario element that has been deselected</param> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement previousElement, ScenarioElement selectedElement) { //Detach from current agent events if (destinationPointExtension != null && selectedAgent == previousElement) { //Hide destination point if something else than the destination point is selected if (!(selectedElement is ScenarioDestinationPoint || selectedElement is ScenarioDestinationPointWaypoint) && destinationPointExtension.DestinationPoint != null) { destinationPointExtension.DestinationPoint.SetVisibility(false); } destinationPointExtension = null; } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { destinationPointExtension = selectedAgent.GetExtension <AgentDestinationPoint>(); if (destinationPointExtension == null) { Hide(); } else { Show(); } } else { Hide(); } }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement selectedElement) { //Detach from current agent events if (selectedAgent != null) { //Hide destination point if agent is deselected and something else is selected if (destinationPointExtension != null && selectedElement != destinationPointExtension.DestinationPoint) { destinationPointExtension.DestinationPoint.SetVisibility(false); } } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { destinationPointExtension = selectedAgent.GetExtension <AgentDestinationPoint>(); if (destinationPointExtension == null) { Hide(); } else { Show(); } } else { Hide(); } }
public void AddScenario(ScenarioAgent scenario) { if (!Scenarii.Exists(s => s.AgentId.Equals(scenario.AgentId))) { Scenarii.Add(scenario); } }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement selectedElement) { //Detach from current agent events if (behaviourExtension != null) { behaviourExtension.BehaviourChanged -= BehaviourExtensionOnBehaviourChanged; } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { behaviourExtension = selectedAgent.GetExtension <AgentBehaviour>(); if (behaviourExtension == null) { Hide(); } else { behaviourExtension.BehaviourChanged += BehaviourExtensionOnBehaviourChanged; Show(); } } else { Hide(); } }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement selectedElement) { if (agentWaypoints != null) { agentWaypoints.IsActiveChanged -= AgentWaypointsOnIsActiveChanged; } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { agentWaypoints = selectedAgent.GetExtension <AgentWaypoints>(); if (agentWaypoints == null) { Hide(); } else { agentWaypoints.IsActiveChanged += AgentWaypointsOnIsActiveChanged; Show(); } } else { Hide(); } }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement selectedElement) { //Detach from current agent events if (selectedAgent != null) { selectedAgent.VariantChanged -= SelectedAgentOnVariantChanged; if (sensorsConfiguration != null) { sensorsConfiguration.SensorsConfigurationIdChanged -= SelectedAgentOnSensorsConfigurationIdChanged; } } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { sensorsConfiguration = selectedAgent.GetExtension <AgentSensorsConfiguration>(); if (sensorsConfiguration == null) { Hide(); } else { selectedAgent.VariantChanged += SelectedAgentOnVariantChanged; sensorsConfiguration.SensorsConfigurationIdChanged += SelectedAgentOnSensorsConfigurationIdChanged; Show(); } } else { Hide(); } }
/// <inheritdoc/> public override void Deinitialize() { ParentAgent.ExtensionAdded -= ParentAgentOnExtensionAdded; ParentAgent.ExtensionRemoved -= ParentAgentOnExtensionRemoved; var behaviourExtension = ParentAgent.GetExtension <AgentBehaviour>(); if (behaviourExtension != null) { behaviourExtension.BehaviourChanged -= ParentAgentOnBehaviourChanged; } base.Deinitialize(); ParentAgent = null; }
/// <inheritdoc/> public override void Initialize(ScenarioElement parentElement) { base.Initialize(parentElement); ParentAgent = (ScenarioAgent)parentElement; SetStartEndElements(parentElement, null); ParentAgent.ExtensionAdded += ParentAgentOnExtensionAdded; ParentAgent.ExtensionRemoved += ParentAgentOnExtensionRemoved; var behaviourExtension = ParentAgent.GetExtension <AgentBehaviour>(); if (behaviourExtension != null) { behaviourExtension.BehaviourChanged += ParentAgentOnBehaviourChanged; } }
/// <summary> /// Attach this destination point to the agent /// </summary> /// <param name="agent">Scenario agent to which destination point will be attached</param> /// <param name="initializeTransform">Should this attach initialize the transform</param> public void AttachToAgent(ScenarioAgent agent, bool initializeTransform) { ParentAgent = agent; PlaybackPath.Initialize(this); var extension = agent.GetExtension <AgentDestinationPoint>(); extension.SetDestinationPoint(this); if (!initializeTransform) { return; } transform.SetParent(agent.transform); var forward = agent.TransformToMove.forward; TransformToMove.localPosition = forward * InitialOffset; TransformToRotate.localRotation = Quaternion.LookRotation(forward); Refresh(); }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="previousElement">Scenario element that has been deselected</param> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement previousElement, ScenarioElement selectedElement) { //Detach from current agent events if (behaviourExtension != null) { behaviourExtension.BehaviourChanged -= SelectedAgentOnBehaviourChanged; Hide(); } selectedAgent = selectedElement as ScenarioAgent; behaviourExtension = selectedAgent == null ? null : selectedAgent.GetExtension <AgentBehaviour>(); //Attach to selected agent events if (behaviourExtension != null) { behaviourExtension.BehaviourChanged += SelectedAgentOnBehaviourChanged; Show(); } SelectedAgentOnBehaviourChanged(behaviourExtension == null ? "" : behaviourExtension.Behaviour); }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="previousElement">Scenario element that has been deselected</param> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement previousElement, ScenarioElement selectedElement) { //Detach from current agent events if (selectedAgent != null) { selectedAgent.VariantChanged -= SelectedAgentOnVariantChanged; } selectedAgent = selectedElement as ScenarioAgent; //Attach to selected agent events if (selectedAgent != null) { Show(); selectedAgent.VariantChanged += SelectedAgentOnVariantChanged; } else { Hide(); } }
/// <summary> /// Method called when another scenario element has been selected /// </summary> /// <param name="previousElement">Scenario element that has been deselected</param> /// <param name="selectedElement">Scenario element that has been selected</param> private void OnSelectedOtherElement(ScenarioElement previousElement, ScenarioElement selectedElement) { if (isAddingWaypoints) { ScenarioManager.Instance.GetExtension <InputManager>().CancelAddingElements(this); } //Force input apply on deselect if (selectedWaypoint != null) { SubmitChangedInputs(); } selectedWaypoint = selectedElement as ScenarioAgentWaypoint; selectedAgent = selectedWaypoint != null ? (ScenarioAgent)selectedWaypoint.ParentElement : null; selectedAgentWaypointsPath = selectedAgent == null ? null : selectedAgent.GetExtension <AgentWaypointsPath>(); //Disable waypoints for ego vehicles if (selectedAgent == null || selectedAgentWaypointsPath == null || !selectedAgent.Source.AgentSupportWaypoints(selectedAgent)) { gameObject.SetActive(false); } else { gameObject.SetActive(true); speedPanel.SetActive(selectedWaypoint != null); accelerationPanel.SetActive(selectedWaypoint != null); waitTimePanel.SetActive(selectedWaypoint != null); if (selectedWaypoint != null) { speedInput.CurrentContext = selectedWaypoint; speedInput.ExternalValueChange(selectedWaypoint.DestinationSpeed, selectedWaypoint, false); accelerationInput.CurrentContext = selectedWaypoint; accelerationInput.ExternalValueChange(selectedWaypoint.Acceleration, selectedWaypoint, false); waitTimeInput.text = selectedWaypoint.WaitTime.ToString("F"); } triggerEditPanel.OnSelectedNewTrigger(selectedWaypoint.LinkedTrigger); UnityUtilities.LayoutRebuild(transform as RectTransform); } }
/// <summary> /// Constructor /// </summary> /// <param name="startTime">Playback time when this action starts</param> /// <param name="agent">Scenario agent that will be moved by this action</param> /// <param name="previousAction">Previous agent's move action used for initial parameters</param> /// <param name="destination">Agent's destination position applied when the action ends</param> /// <param name="rotation">Agent's rotation which will be applied when this action is performed</param> /// <param name="destinationSpeed">Agent's destination speed used during the playback movement</param> /// <param name="acceleration">Agent's acceleration used during the playback movement</param> public AgentMoveAction(float startTime, ScenarioAgent agent, AgentMoveAction previousAction, Vector3 destination, Quaternion rotation, float destinationSpeed, float acceleration) : base( startTime) { Agent = agent; InitialPosition = previousAction?.Destination ?? Agent.TransformForPlayback.position; InitialRotation = Agent.TransformForPlayback.rotation; Destination = destination; Rotation = rotation; InitialSpeed = previousAction?.DestinationSpeed ?? 0.0f; DestinationSpeed = destinationSpeed; Acceleration = acceleration; var distance = Vector3.Distance(InitialPosition, destination); if (acceleration > 0) { // If max speed is lower than the initial speed convert acceleration to deceleration if (destinationSpeed < InitialSpeed) { Acceleration *= -1; } if (!UniformlyAcceleratedMotion.CalculateDuration(acceleration, InitialSpeed, distance, ref destinationSpeed, out var accelerationDuration, out var accelerationDistance)) { // Max speed will not be reached with current acceleration AccelerationDestination = destination; DestinationSpeed = destinationSpeed; Duration = AccelerationDuration = accelerationDuration; } else { // Calculate mixed duration of accelerated and linear movements AccelerationDestination = InitialPosition + (destination - InitialPosition).normalized * accelerationDistance; var linearDistance = distance - accelerationDistance; AccelerationDuration = accelerationDuration; Duration = AccelerationDuration + linearDistance / DestinationSpeed; } }
/// <inheritdoc/> public override bool AgentSupportWaypoints(ScenarioAgent agent) { return(false); }
/// <summary> /// Constructor /// </summary> /// <param name="parentAgent">Scenario agent that will be controlled</param> public AgentController(ScenarioAgent parentAgent) { agent = parentAgent; }
/// <inheritdoc/> public override bool AgentSupportWaypoints(ScenarioAgent agent) { var behaviourExtension = agent.GetExtension <AgentBehaviour>(); return(behaviourExtension != null && behaviourExtension.Behaviour == nameof(NPCWaypointBehaviour)); }
/// <summary> /// Unregisters the agent in the manager /// </summary> /// <param name="agent">Agent to register</param> public void UnregisterAgent(ScenarioAgent agent) { Agents.Remove(agent); AgentUnregistered?.Invoke(agent); }
/// <summary> /// Checks if the given agent supports waypoints /// </summary> /// <param name="agent">Agent to check</param> /// <returns>True if agent supports waypoints, false otherwise</returns> public abstract bool AgentSupportWaypoints(ScenarioAgent agent);
private void DrawAgentIntent(ScenarioAgent agent, Graphics graphics, Rectangle rect) { if (agent.Intent != null) { var intentVec = new PointF() { X = agent.GetPosition().X - agent.Intent.GetPosition().X, Y = agent.GetPosition().Y - agent.Intent.GetPosition().Y }; var intentLength = Magnitude(intentVec); if (intentLength > 0.05f) { intentVec.X /= intentLength; intentVec.Y /= intentLength; if (!agent.IntentAttractive) { intentVec.X = -intentVec.X; intentVec.Y = -intentVec.Y; } var tip = CoordinatesToDisplayPoint(agent.GetPosition().X - intentVec.X * agent.Radius, agent.GetPosition().Y - intentVec.Y * agent.Radius, rect); var bottomCenter = new PointF(agent.GetPosition().X + intentVec.X * agent.Radius, agent.GetPosition().Y + intentVec.Y * agent.Radius); var intentOrthogonalVec = Normalize(new PointF(-intentVec.Y, intentVec.X)); var bottomL = CoordinatesToDisplayPoint(bottomCenter.X - intentOrthogonalVec.X * agent.Radius * 0.3f, bottomCenter.Y - intentOrthogonalVec.Y * agent.Radius * 0.3f, rect); var bottomR = CoordinatesToDisplayPoint(bottomCenter.X + intentOrthogonalVec.X * agent.Radius * 0.3f, bottomCenter.Y + intentOrthogonalVec.Y * agent.Radius * 0.3f, rect); var bcExtended = new PointF(agent.GetPosition().X + intentVec.X * agent.Radius * 1.2f, agent.GetPosition().Y + intentVec.Y * agent.Radius * 1.2f); var bc = CoordinatesToDisplayPoint(bcExtended.X, bcExtended.Y, rect); Color darker = GetDarkerColor(agent.Color); Color lighter = GetLighterColor(agent.Color); using (var gradient = new LinearGradientBrush(tip, bc, lighter, darker)) { var poly = new Point[] { tip, bottomL, bottomR, tip }; graphics.DrawLines(Pens.Black, poly); graphics.FillPolygon(gradient, poly); using (var dashPen = new Pen(agent.Color)) { var agentCenter = CoordinatesToDisplayPoint(agent.GetPosition().X, agent.GetPosition().Y, rect); dashPen.DashStyle = DashStyle.Dash; graphics.DrawLine(dashPen, agentCenter, CoordinatesToDisplayPoint(agent.Intent.GetPosition().X, agent.Intent.GetPosition().Y, rect)); } } } } else { using (var ellipse = new GraphicsPath()) { var insetRect = ToDisplayRect(agent, rect, 0.35f); ellipse.AddEllipse(insetRect); using (var gradient = new PathGradientBrush(ellipse)) { gradient.CenterColor = GetDarkerColor(agent.Color); gradient.SurroundColors = new[] { GetLighterColor(agent.Color) }; graphics.FillRectangle(gradient, insetRect); } } } }
/// <summary> /// Registers the agent in the manager /// </summary> /// <param name="agent">Agent to register</param> public void RegisterAgent(ScenarioAgent agent) { Agents.Add(agent); AgentRegistered?.Invoke(agent); }
public void AddScenario(ScenarioAgent scenario) { Engine.AddScenario(scenario); }