AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
EdgeHelpers.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.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.Boundary
8 {
9  public static class EdgeHelpers
10  {
11  // A value that is larger than the widest possible room. We use this
12  // to create line segments that are "guaranteed" to hit a piece of the
13  // room boundary
14  private const float largeValue = 10000;
15  private const float smallValue = -largeValue;
16 
17  // Sentinel value
18  public static readonly Vector2 InvalidPoint = new Vector2(float.NegativeInfinity, float.NegativeInfinity);
19 
23  public static Edge[] ConvertVector3ListToEdgeArray(IList<Vector3> points)
24  {
25  var edges = new Edge[points.Count];
26 
27  for (int pointIndex = 0; pointIndex < points.Count; pointIndex++)
28  {
29  var pointA = points[pointIndex];
30  var pointB = points[(pointIndex + 1) % points.Count];
31  edges[pointIndex] = new Edge(pointA.x, pointA.z, pointB.x, pointB.z);
32  }
33 
34  return edges;
35  }
36 
40  public static bool IsValidPoint(Vector2 point)
41  {
42  return !float.IsNegativeInfinity(point.x) && !float.IsNegativeInfinity(point.y);
43  }
44 
48  public static bool IsInside(Edge[] edges, Vector2 point)
49  {
50  // Check if a ray to the right (X+) intersects with an odd number of edges (inside) or an even number of edges (outside)
51  Vector2 farPoint = point;
52  farPoint.x = largeValue;
53  var rightEdge = new Edge(point, farPoint);
54  int intersections = 0;
55  foreach (var edge in edges)
56  {
57  if (IsValidPoint(GetIntersection(edge, rightEdge)))
58  {
59  ++intersections;
60  }
61  }
62  return (intersections & 1) == 1;
63  }
64 
68  public static Vector2 GetIntersection(Edge edge1, Edge edge2)
69  {
70  float s1_x = edge1.Bx - edge1.Ax;
71  float s1_y = edge1.By - edge1.Ay;
72  float s2_x = edge2.Bx - edge2.Ax;
73  float s2_y = edge2.By - edge2.Ay;
74 
75  float s, t;
76  s = (-s1_y * (edge1.Ax - edge2.Ax) + s1_x * (edge1.Ay - edge2.Ay)) / (-s2_x * s1_y + s1_x * s2_y);
77  t = (s2_x * (edge1.Ay - edge2.Ay) - s2_y * (edge1.Ax - edge2.Ax)) / (-s2_x * s1_y + s1_x * s2_y);
78 
79  if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
80  {
81  // Collision detected
82  return new Vector2(edge1.Ax + (t * s1_x), edge1.Ay + (t * s1_y));
83  }
84 
85  return InvalidPoint;
86  }
87  }
88 }
static bool IsInside(Edge[] edges, Vector2 point)
Returns true if the given point is within the boundary.
Definition: EdgeHelpers.cs:48
static Edge [] ConvertVector3ListToEdgeArray(IList< Vector3 > points)
Helper to convert a list of Vector3 points into an array of Edges.
Definition: EdgeHelpers.cs:23
static Vector2 GetIntersection(Edge edge1, Edge edge2)
Gets the point where two edges intersect. Value is InvalidPoint if they do not.
Definition: EdgeHelpers.cs:68
Helper struct to hold an edge.
Definition: Edge.cs:11
static bool IsValidPoint(Vector2 point)
Helper to know when a point is invalid.
Definition: EdgeHelpers.cs:40