Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

cleaned up Vector2 interaction mapping with faster code #142

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Diagnostics;
using XRTK.Definitions.Devices;
using XRTK.Definitions.InputSystem;
using XRTK.Definitions.Utilities;
using NUnit.Framework;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace XRTK.Tests.InputSystem
{
Expand Down Expand Up @@ -434,6 +436,130 @@ public void Test_04_01_InitializedVector2()
Assert.IsFalse(interaction.Updated);
}

[Test]
public void Test_04_01_01_Vector2_One()
{
var iterations = 10000000;
var watch = Stopwatch.StartNew();

for (int i = 0; i < iterations; i++)
{
var test = Vector2.one;
}

watch.Stop();
Debug.Log($"Test Vector2.one execution Time: {watch.Elapsed.TotalMilliseconds} ms");

watch = Stopwatch.StartNew();
var vector2One = new Vector2(1f, 1f);

for (int i = 0; i < iterations; i++)
{
var test = vector2One;
}

watch.Stop();
Debug.Log($"Test cached Vector2 execution Time: {watch.Elapsed.TotalMilliseconds} ms");

watch = Stopwatch.StartNew();

for (int i = 0; i < iterations; i++)
{
var test = new Vector2(1f, 1f);
}

watch.Stop();
Debug.Log($"Test new Vector2 execution Time: {watch.Elapsed.TotalMilliseconds} ms");
}

[Test]
public void Test_04_01_02_Checks()
{
var iterations = 10000000;
var value = new Vector2(1, 1);
var vector2Data = new Vector2(2, 2);
var changed = false;
var updated = false;
var invertAxis = true;

var cachedVector2 = new Vector2(1f, 1f);

var watch = Stopwatch.StartNew();

for (int i = 0; i < iterations; i++)
{
var invertMultiplier = Vector2.one;

if (invertAxis)
{
invertMultiplier.x = -1f;
}

if (!invertAxis)
{
invertMultiplier.y = -1f;
}

invertAxis = !invertAxis;
var newValue = value * invertMultiplier;
changed = vector2Data != newValue;
updated = changed || !newValue.Equals(Vector2.zero);
// use the internal reading for changed so we don't reset it.
vector2Data = newValue;
}

watch.Stop();
Debug.Log($"Test Vector2.one execution Time: {watch.Elapsed.TotalMilliseconds} ms");

watch = Stopwatch.StartNew();

for (int i = 0; i < iterations; i++)
{
var invertMultiplier = cachedVector2;

if (invertAxis)
{
invertMultiplier.x = -1f;
}
else
{
invertMultiplier.x = 1f;
}

if (!invertAxis)
{
invertMultiplier.y = -1f;
}
else
{
invertMultiplier.y = 1f;
}

invertAxis = !invertAxis;
var newValue = value * invertMultiplier;
changed = vector2Data != newValue;
updated = changed || !newValue.Equals(Vector2.zero);
vector2Data = newValue;
}

watch.Stop();
Debug.Log($"Test Vector2 execution Time: {watch.Elapsed.TotalMilliseconds} ms");

watch = Stopwatch.StartNew();

for (int i = 0; i < iterations; i++)
{
var newValue = value * new Vector2(invertAxis ? -1f : 1f, !invertAxis ? -1f : 1f);
invertAxis = !invertAxis;
changed = vector2Data != newValue;
updated = changed || !newValue.Equals(Vector2.zero);
vector2Data = newValue;
}

watch.Stop();
Debug.Log($"Test new Vector2 execution Time: {watch.Elapsed.TotalMilliseconds} ms");
}

/// <summary>
/// We test by setting the interaction data to two different values.<para/>
/// We expect that <see cref="MixedRealityInteractionMapping.Changed"/> == true, then false after each subsequent check before assigning a new value.<para/>
Expand Down Expand Up @@ -1294,7 +1420,7 @@ public void Test_08_07_InteractionArrayMixedRealityPose()
var initialValue = interactions[0];

Assert.IsNotNull(initialValue);
MixedRealityPose initialSixDofValue = initialValue.PoseData;
var initialSixDofValue = initialValue.PoseData;

Assert.IsTrue(initialSixDofValue.Position == Vector3.zero);
Assert.IsTrue(initialSixDofValue == MixedRealityPose.ZeroIdentity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public MixedRealityInteractionMapping(MixedRealityInteractionMapping mixedRealit
changed = false;
}

private readonly Vector2 vector2One = new Vector2(1f, 1f);

#region Interaction Properties

[SerializeField]
Expand Down Expand Up @@ -381,21 +383,21 @@ public float FloatData
{
if (AxisType != AxisType.SingleAxis)
{
Debug.LogError($"SetFloatValue is only valid for AxisType.SingleAxis InteractionMappings\nPlease check the {inputType} mapping for the current controller");
Debug.LogError(
$"SetFloatValue is only valid for AxisType.SingleAxis InteractionMappings\nPlease check the {inputType} mapping for the current controller");
}

var newValue = value;

if (invertXAxis)
{
Changed = !floatData.Equals(value * -1f);
floatData = value * -1f;
}
else
{
Changed = !floatData.Equals(value);
// use the internal reading for changed so we don't reset it.
Updated = changed || !floatData.Equals(0f);
floatData = value;
newValue *= -1f;
}

Changed = !floatData.Equals(newValue);
// use the internal reading for changed so we don't reset it.
Updated = changed || !floatData.Equals(0f);
floatData = value;
}
}

Expand All @@ -414,25 +416,23 @@ public Vector2 Vector2Data
Debug.LogError($"SetVector2Value is only valid for AxisType.DualAxis InteractionMappings\nPlease check the {inputType} mapping for the current controller");
}

if (invertXAxis || invertYAxis)
var invertMultiplier = vector2One;

if (invertXAxis)
{
var invertXAxisFactor = invertXAxis ? -1f : 1f;
var invertYAxisFactor = invertYAxis ? -1f : 1f;

Changed = !vector2Data.x.Equals(value.x * invertXAxisFactor) ||
!vector2Data.y.Equals(value.y * invertYAxisFactor);
// use the internal reading for changed so we don't reset it.
Updated = changed || !vector2Data.Equals(Vector2.zero);
vector2Data.x = value.x * invertXAxisFactor;
vector2Data.y = value.y * invertYAxisFactor;
invertMultiplier.x = -1f;
}
else

if (invertYAxis)
{
Changed = vector2Data != value;
// use the internal reading for changed so we don't reset it.
Updated = changed || !vector2Data.Equals(Vector2.zero);
vector2Data = value;
invertMultiplier.y = -1f;
}

var newValue = value * invertMultiplier;
Changed = vector2Data != newValue;
// use the internal reading for changed so we don't reset it.
Updated = changed || !newValue.Equals(Vector2.zero);
vector2Data = newValue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,22 @@ public static void GetColliderBoundsPoints(GameObject target, List<Vector3> boun
}
else if (colliders[i] is CapsuleCollider)
{
CapsuleCollider cc = colliders[i] as CapsuleCollider;
Bounds capsuleBounds = new Bounds(cc.center, Vector3.zero);
var cc = (CapsuleCollider)colliders[i];
var capsuleBounds = new Bounds(cc.center, Vector3.zero);
var radius = cc.radius;

switch (cc.direction)
{
case 0:
capsuleBounds.size = new Vector3(cc.height, cc.radius * 2, cc.radius * 2);
capsuleBounds.size = new Vector3(cc.height, cc.radius * 2, radius * 2);
break;

case 1:
capsuleBounds.size = new Vector3(cc.radius * 2, cc.height, cc.radius * 2);
break;

case 2:
capsuleBounds.size = new Vector3(cc.radius * 2, cc.radius * 2, cc.height);
capsuleBounds.size = new Vector3(cc.radius * 2, radius * 2, cc.height);
break;
}
capsuleBounds.GetFacePositions(cc.transform, ref corners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void SetLayerRecursively(this GameObject root, int layer)
/// <param name="root">Start point of the traverse</param>
/// <param name="layer">The layer to apply</param>
/// <param name="cache">The previously set layer for each object</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="root"/> is <see langword="null"/></exception>
public static void SetLayerRecursively(this GameObject root, int layer, out Dictionary<GameObject, int> cache)
{
if (root == null) { throw new ArgumentNullException(nameof(root)); }
Expand All @@ -52,8 +53,9 @@ public static void SetLayerRecursively(this GameObject root, int layer, out Dict

foreach (var child in root.transform.EnumerateHierarchy())
{
cache[child.gameObject] = child.gameObject.layer;
child.gameObject.layer = layer;
var childGameObject = child.gameObject;
cache[childGameObject] = childGameObject.layer;
childGameObject.layer = layer;
}
}

Expand All @@ -62,16 +64,19 @@ public static void SetLayerRecursively(this GameObject root, int layer, out Dict
/// </summary>
/// <param name="root">Start point of the traverse</param>
/// <param name="cache">The previously set layer for each object</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="root"/> is <see langword="null"/></exception>
/// <exception cref="T:System.ArgumentNullException"><paramref name="cache"/> is <see langword="null"/></exception>
public static void ApplyLayerCacheRecursively(this GameObject root, Dictionary<GameObject, int> cache)
{
if (root == null) { throw new ArgumentNullException(nameof(root)); }
if (cache == null) { throw new ArgumentNullException(nameof(cache)); }

foreach (var child in root.transform.EnumerateHierarchy())
{
if (!cache.TryGetValue(child.gameObject, out int layer)) { continue; }
child.gameObject.layer = layer;
cache.Remove(child.gameObject);
var childGameObject = child.gameObject;
if (!cache.TryGetValue(childGameObject, out var layer)) { continue; }
childGameObject.layer = layer;
cache.Remove(childGameObject);
}
}

Expand All @@ -95,7 +100,7 @@ public static bool IsInLayerMask(this GameObject gameObject, LayerMask layerMask
public static void ApplyToHierarchy(this GameObject root, Action<GameObject> action)
{
action(root);
Transform[] items = root.GetComponentsInChildren<Transform>();
var items = root.GetComponentsInChildren<Transform>();

for (var i = 0; i < items.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,15 +671,16 @@ public GameObject GetFloorVisualization()
}

var floorScale = FloorScale;
var position = MixedRealityToolkit.Instance.MixedRealityPlayspace.position;

// Render the floor.
currentFloorObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
currentFloorObject.name = "Boundary System Floor";
currentFloorObject.transform.localScale = new Vector3(floorScale.x, BoundaryObjectThickness, floorScale.y);
currentFloorObject.transform.Translate(new Vector3(
MixedRealityToolkit.Instance.MixedRealityPlayspace.position.x,
position.x,
FloorHeight.Value - (currentFloorObject.transform.localScale.y * 0.5f),
MixedRealityToolkit.Instance.MixedRealityPlayspace.position.z));
position.z));
currentFloorObject.layer = FloorPhysicsLayer;
currentFloorObject.GetComponent<Renderer>().sharedMaterial = FloorMaterial;

Expand Down Expand Up @@ -756,11 +757,12 @@ public GameObject GetTrackedAreaVisualization()
{
layer = DefaultIgnoreRaycastLayer
};

var position = MixedRealityToolkit.Instance.MixedRealityPlayspace.position;

currentTrackedAreaObject.AddComponent<LineRenderer>();
currentTrackedAreaObject.transform.Translate(new Vector3(
MixedRealityToolkit.Instance.MixedRealityPlayspace.position.x,
BoundaryObjectRenderOffset,
MixedRealityToolkit.Instance.MixedRealityPlayspace.position.z));
currentTrackedAreaObject.transform.Translate(
new Vector3(position.x, BoundaryObjectRenderOffset, position.z));
currentPlayAreaObject.layer = TrackedAreaPhysicsLayer;

// Configure the renderer properties.
Expand Down
Loading