Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
Corners.cs
Go to the documentation of this file.
1 using System;
2 using UnityEngine;
3 
4 namespace droid.Runtime.Utilities.GameObjects.BoundingBoxes.Experimental {
7  public static class Corners {
8  public static Vector3[] ExtractCorners(Vector3 v3_center,
9  Vector3 v3_extents,
10  Transform reference_transform = null) {
11  var v3_front_top_left = new Vector3(v3_center.x - v3_extents.x,
12  v3_center.y + v3_extents.y,
13  v3_center.z - v3_extents.z); // Front top left corner
14  var v3_front_top_right = new Vector3(v3_center.x + v3_extents.x,
15  v3_center.y + v3_extents.y,
16  v3_center.z - v3_extents.z); // Front top right corner
17  var v3_front_bottom_left = new Vector3(v3_center.x - v3_extents.x,
18  v3_center.y - v3_extents.y,
19  v3_center.z - v3_extents.z); // Front bottom left corner
20  var v3_front_bottom_right = new Vector3(v3_center.x + v3_extents.x,
21  v3_center.y - v3_extents.y,
22  v3_center.z - v3_extents.z); // Front bottom right corner
23  var v3_back_top_left = new Vector3(v3_center.x - v3_extents.x,
24  v3_center.y + v3_extents.y,
25  v3_center.z + v3_extents.z); // Back top left corner
26  var v3_back_top_right = new Vector3(v3_center.x + v3_extents.x,
27  v3_center.y + v3_extents.y,
28  v3_center.z + v3_extents.z); // Back top right corner
29  var v3_back_bottom_left = new Vector3(v3_center.x - v3_extents.x,
30  v3_center.y - v3_extents.y,
31  v3_center.z + v3_extents.z); // Back bottom left corner
32  var v3_back_bottom_right = new Vector3(v3_center.x + v3_extents.x,
33  v3_center.y - v3_extents.y,
34  v3_center.z + v3_extents.z); // Back bottom right corner
35  if (reference_transform) {
36  v3_front_top_left = reference_transform.TransformPoint(v3_front_top_left);
37  v3_front_top_right = reference_transform.TransformPoint(v3_front_top_right);
38  v3_front_bottom_left = reference_transform.TransformPoint(v3_front_bottom_left);
39  v3_front_bottom_right = reference_transform.TransformPoint(v3_front_bottom_right);
40  v3_back_top_left = reference_transform.TransformPoint(v3_back_top_left);
41  v3_back_top_right = reference_transform.TransformPoint(v3_back_top_right);
42  v3_back_bottom_left = reference_transform.TransformPoint(v3_back_bottom_left);
43  v3_back_bottom_right = reference_transform.TransformPoint(v3_back_bottom_right);
44  }
45 
46  return new[] {
47  v3_front_top_left,
48  v3_front_top_right,
49  v3_front_bottom_left,
50  v3_front_bottom_right,
51  v3_back_top_left,
52  v3_back_top_right,
53  v3_back_bottom_left,
54  v3_back_bottom_right
55  };
56  }
57 
69  public static void DrawBox(Vector3 v3_front_top_left,
70  Vector3 v3_front_top_right,
71  Vector3 v3_front_bottom_left,
72  Vector3 v3_front_bottom_right,
73  Vector3 v3_back_top_left,
74  Vector3 v3_back_top_right,
75  Vector3 v3_back_bottom_left,
76  Vector3 v3_back_bottom_right,
77  Color color) {
78  Debug.DrawLine(v3_front_top_left, v3_front_top_right, color);
79  Debug.DrawLine(v3_front_top_right, v3_front_bottom_right, color);
80  Debug.DrawLine(v3_front_bottom_right, v3_front_bottom_left, color);
81  Debug.DrawLine(v3_front_bottom_left, v3_front_top_left, color);
82 
83  Debug.DrawLine(v3_back_top_left, v3_back_top_right, color);
84  Debug.DrawLine(v3_back_top_right, v3_back_bottom_right, color);
85  Debug.DrawLine(v3_back_bottom_right, v3_back_bottom_left, color);
86  Debug.DrawLine(v3_back_bottom_left, v3_back_top_left, color);
87 
88  Debug.DrawLine(v3_front_top_left, v3_back_top_left, color);
89  Debug.DrawLine(v3_front_top_right, v3_back_top_right, color);
90  Debug.DrawLine(v3_front_bottom_right, v3_back_bottom_right, color);
91  Debug.DrawLine(v3_front_bottom_left, v3_back_bottom_left, color);
92  }
93 
102  public static void DrawCircle(Vector2 center,
103  float radius,
104  Color color,
105  float num_segments = 40,
106  float duration = 0.01f) {
107  var rot_quaternion = Quaternion.AngleAxis(360.0f / num_segments, Vector3.forward);
108  var vertex_start = new Vector2(radius, 0.0f);
109  for (var i = 0; i < num_segments; i++) {
110  Vector2 rotated_point = rot_quaternion * vertex_start;
111 
112  // Draw the segment, shifted by the center
113  Debug.DrawLine(center + vertex_start, center + rotated_point, color, duration);
114 
115  vertex_start = rotated_point;
116  }
117  }
118 
126  public static void DrawBox(Vector2 world_top_left,
127  Vector2 world_bottom_right,
128  Color color,
129  float duration = 0.01f) {
130  var world_top_right = new Vector2(world_bottom_right.x, world_top_left.y);
131  var world_bottom_left = new Vector2(world_top_left.x, world_bottom_right.y);
132 
133  Debug.DrawLine(world_top_left, world_bottom_left, color, duration);
134  Debug.DrawLine(world_bottom_left, world_bottom_right, color, duration);
135  Debug.DrawLine(world_bottom_right, world_top_right, color, duration);
136  Debug.DrawLine(world_top_right, world_top_left, color, duration);
137  }
138 
145  public static void DrawEdges(Vector2[] world_points, Color color, float duration = 0.01f) {
146  // Draw each segment except the last
147  for (var i = 0; i < world_points.Length - 1; i++) {
148  Vector3 next_point = world_points[i + 1];
149  Vector3 current_point = world_points[i];
150  Debug.DrawLine(current_point, next_point, color, duration);
151  }
152  }
153 
160  public static void DrawPolygon(Vector2[] world_points, Color color, float duration = 0.01f) {
161  DrawEdges(world_points, color, duration);
162 
163  // Polygons are just edges with the first and last points connected
164  if (world_points.Length > 1) {
165  Debug.DrawLine(world_points[world_points.Length - 1], world_points[0], color, duration);
166  }
167  }
168 
176  public static void DrawArrow(Vector2 origin, Vector2 endpoint, Color color, float duration = 0.01f) {
177  // Draw the line that makes up the body of the arrow
178  Debug.DrawLine(origin, endpoint, color, 0.01f);
179 
180  // Draw arrowhead so we can see direction
181  var arrow_direction = endpoint - origin;
182  DebugDrawArrowhead(endpoint,
183  arrow_direction.normalized,
184  GetArrowSizeForLine(arrow_direction),
185  color,
186  duration);
187  }
188 
189  static float GetArrowSizeForLine(Vector2 line) {
190  const Single default_arrow_percentage = 0.05f;
191  return (line * default_arrow_percentage).magnitude;
192  }
193 
194  static void DebugDrawArrowhead(Vector2 origin,
195  Vector2 direction,
196  float size,
197  Color color,
198  float duration = 0.01f,
199  float theta = 30.0f) {
200  // Theta angle is the acute angle of the arrow, so flip direction or else arrow will be pointing "backwards"
201  var arrowhead_handle = -direction * size;
202 
203  var arrow_rotation_r = Quaternion.AngleAxis(theta, Vector3.forward);
204  Vector2 arrowhead_r = arrow_rotation_r * arrowhead_handle;
205  Debug.DrawLine(origin, origin + arrowhead_r, color, duration);
206 
207  var arrow_rotation_l = Quaternion.AngleAxis(-theta, Vector3.forward);
208  Vector2 arrowhead_l = arrow_rotation_l * arrowhead_handle;
209  Debug.DrawLine(origin, origin + arrowhead_l, color, duration);
210  }
211  }
212 }