AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
RayStep.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
9  [Serializable]
10  public struct RayStep
11  {
12  public RayStep(Vector3 origin, Vector3 terminus) : this()
13  {
14  Origin = origin;
15  Terminus = terminus;
16  Length = Vector3.Distance(origin, terminus);
17  Direction = (Terminus - Origin).normalized;
18  }
19 
20  public Vector3 Origin { get; private set; }
21  public Vector3 Terminus { get; private set; }
22  public Vector3 Direction { get; private set; }
23  public float Length { get; private set; }
24 
25  public Vector3 GetPoint(float distance)
26  {
27  return Vector3.MoveTowards(Origin, Terminus, distance);
28  }
29 
30  public void UpdateRayStep(Vector3 origin, Vector3 terminus)
31  {
32  Origin = origin;
33  Terminus = terminus;
34  Length = Vector3.Distance(origin, terminus);
35  Direction = (Terminus - Origin).normalized;
36  }
37 
38  public void CopyRay(Ray ray, float rayLength)
39  {
40  Length = rayLength;
41  Origin = ray.origin;
42  Direction = ray.direction;
43  Terminus = Origin + (Direction * Length);
44  }
45 
46  public static implicit operator Ray(RayStep r)
47  {
48  return new Ray(r.Origin, r.Direction);
49  }
50 
51  #region static utility functions
52 
59  public static Vector3 GetPointByDistance(RayStep[] steps, float distance)
60  {
61  Debug.Assert(steps != null);
62  Debug.Assert(steps.Length > 0);
63 
64  Vector3 point = Vector3.zero;
65  float remainingDistance = distance;
66 
67  for (int i = 0; i < steps.Length; i++)
68  {
69  if (remainingDistance > steps[i].Length)
70  {
71  remainingDistance -= steps[i].Length;
72  }
73  else
74  {
75  point = Vector3.Lerp(steps[i].Origin, steps[i].Terminus, remainingDistance / steps[i].Length);
76  remainingDistance = 0;
77  break;
78  }
79  }
80 
81  if (remainingDistance > 0)
82  {
83  // If we reach the end and still have distance left, set the point to the terminus of the last step
84  point = steps[steps.Length - 1].Terminus;
85  }
86 
87  return point;
88  }
89 
96  public static RayStep GetStepByDistance(RayStep[] steps, float distance)
97  {
98  Debug.Assert(steps != null);
99  Debug.Assert(steps.Length > 0);
100 
101  RayStep step = new RayStep();
102  float remainingDistance = distance;
103 
104  for (int i = 0; i < steps.Length; i++)
105  {
106  if (remainingDistance > steps[i].Length)
107  {
108  remainingDistance -= steps[i].Length;
109  }
110  else
111  {
112  step = steps[i];
113  remainingDistance = 0;
114  break;
115  }
116  }
117 
118  if (remainingDistance > 0)
119  {
120  // If we reach the end and still have distance left, return the last step
121  step = steps[steps.Length - 1];
122  }
123 
124  return step;
125  }
126 
133  public static Vector3 GetDirectionByDistance(RayStep[] steps, float distance)
134  {
135  Debug.Assert(steps != null);
136  Debug.Assert(steps.Length > 0);
137 
138  return GetStepByDistance(steps, distance).Direction;
139  }
140 
141  #endregion
142  }
143 }
void CopyRay(Ray ray, float rayLength)
Definition: RayStep.cs:38
Vector3 GetPoint(float distance)
Definition: RayStep.cs:25
Definition: Origin.cs:6
void UpdateRayStep(Vector3 origin, Vector3 terminus)
Definition: RayStep.cs:30
static Vector3 GetPointByDistance(RayStep[] steps, float distance)
Returns a point along an array of RaySteps by distance
Definition: RayStep.cs:59
static Vector3 GetDirectionByDistance(RayStep[] steps, float distance)
Returns a direction along an array of RaySteps by distance
Definition: RayStep.cs:133
static RayStep GetStepByDistance(RayStep[] steps, float distance)
Returns a RayStep along an array of RaySteps by distance
Definition: RayStep.cs:96
RayStep(Vector3 origin, Vector3 terminus)
Definition: RayStep.cs:12