// Use this for initialization protected override void Init() { base.Init(); if (m_arrayExtracted == null) { m_arrayExtracted = new IMLSerialVector(); } }
/// <summary> /// Updates Feature values /// </summary> /// <returns></returns> public object UpdateFeature() { float[] valueToOutput = GetInputValue <float[]>("inputSerialVector", this.inputSerialVector); if (m_arrayExtracted == null) { m_arrayExtracted = new IMLSerialVector(); } m_arrayExtracted.SetValues(valueToOutput); return(this); }
/// <summary> /// Updates Feature values /// </summary> /// <returns></returns> public object UpdateFeature() { //Debug.Log("Extracting Velocity..."); // The velocity extractor expects any other feature extracted to make calculations FeatureToInput = GetInputValue <Node>("FeatureToInput"); // If we managed to get the input if (FeatureToInput != null) { // We check that it is an IML Feature var featureToUse = (FeatureToInput as IFeatureIML).FeatureValues; if (featureToUse != null) { // Calculate the velocity arrays size //m_CurrentVelocity = new float[featureToUse.Values.Length]; // If the velocity hasn't been updated yet... (unlocked externally in the IML Component) if (!isUpdated) { // We check in case the input feature length changed if (m_CurrentVelocity == null || m_CurrentVelocity.Length != featureToUse.Values.Length) { // If it did, we resize the current vel vector and lastframe vector m_CurrentVelocity = new float[featureToUse.Values.Length]; m_LastFrameFeatureValue = null; } if (m_LastFrameFeatureValue == null || m_LastFrameFeatureValue.Length != m_CurrentVelocity.Length) { if (m_CurrentVelocity == null) { Debug.Log("Current Velocity is null"); } m_LastFrameFeatureValue = new float[m_CurrentVelocity.Length]; } // Calculate velocity itself for (int i = 0; i < m_CurrentVelocity.Length; i++) { m_CurrentVelocity[i] = (featureToUse.Values[i] - m_LastFrameFeatureValue[i]) / Time.smoothDeltaTime; // Debug.Log(i + " " + m_CurrentVelocity[i]); // Store last known feature values for next frame //m_LastFrameFeatureValue[i] = featureToUse.Values[i]; } // We make sure that the velocity extracted serial vector is not null if (m_VelocityExtracted == null) { m_VelocityExtracted = new IMLSerialVector(m_CurrentVelocity); } // Set values for velocity extracted and for last frame feature value m_VelocityExtracted.SetValues(m_CurrentVelocity); featureToUse.Values.CopyTo(m_LastFrameFeatureValue, 0); //for (int i = 0; i < m_CurrentVelocity.Length; i++) //{ // //Debug.Log(i + " = " + m_CurrentVelocity[i]); // // Store last known feature values for next frame // //m_LastFrameFeatureValue[i] = featureToUse.Values[i]; //} // Make sure to mark the feature as updated to avoid calculating twice isUpdated = true; } return(this); } else { //// Dispose of arrays to avoid carrying on any configs //m_CurrentVelocity = null; //m_LastFrameFeatureValue = null; return(null); } } // If we couldn't get an input, we return null else { //// Dispose of arrays to avoid carrying on any configs //m_CurrentVelocity = null; //m_LastFrameFeatureValue = null; return(null); } }
public object UpdateFeature() { // This extractor expects any other feature extracted to make calculations FirstInput = GetInputValue <Node>("FeatureToInput"); SecondInput = GetInputValues <Node>("FeaturesToMeasureDistanceToFirst").ToList(); if (!isUpdated) { // If we managed to get the input if (FirstInput != null && SecondInput != null) { // We check that it is an IML feature var featureToUseIML = (FirstInput as IFeatureIML).FeatureValues; if (featureToUseIML != null) { // Clear distances vector m_DistancesToFirstInput = new float[SecondInput.Count]; // We check that the features to measure the distance to the first are IML features for (int i = 0; i < SecondInput.Count; i++) { //Debug.Log("Calculating distance iteration: " + i); var feautureToMeasure = SecondInput[i]; var featureMeasureIML = (feautureToMeasure as IFeatureIML).FeatureValues; // We check that the second inputs are also iml features if (featureMeasureIML != null) { // Then we calculate the distance to the first feature // We make sure that the features to calculate are the same if (featureToUseIML.DataType == featureMeasureIML.DataType) { // Calculate distance between each of the entries in the values vector float[] distancesEachFeature = new float[featureToUseIML.Values.Length]; for (int j = 0; j < featureToUseIML.Values.Length; j++) { distancesEachFeature[j] = (featureMeasureIML.Values[j] - featureToUseIML.Values[j]); } // Follow the euclidean formula for distance: square and add together all distances for (int j = 0; j < featureToUseIML.Values.Length; j++) { m_DistancesToFirstInput[i] += (distancesEachFeature[j] * distancesEachFeature[j]); } // We make sure that the extracted serial vector is not null if (m_DistancesExtracted == null) { m_DistancesExtracted = new IMLSerialVector(m_DistancesToFirstInput); } // Set values for velocity extracted and for last frame feature value m_DistancesExtracted.SetValues(m_DistancesToFirstInput); // Make sure to mark the feature as updated to avoid calculating twice isUpdated = true; } else { Debug.LogError("Features Types to measure distance are different!"); return(null); } } // If we couldn't get an input (in the second input), we return null else { Debug.LogError("Could not get second input " + i + " when measuring distance!"); return(null); } } return(this); } // If we couldn't get an input (in the first input), we return null else { Debug.LogError("Could not get first input when measuring distance!"); return(null); } } // If we couldn't get an input (at all), we return null else { return(null); } } // Avoid calculating again and return the feature else { return(this); } }