Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
GraspingUtilities.cs
Go to the documentation of this file.
1 using UnityEngine;
2 
3 namespace droid.Runtime.Utilities.Misc.Grasping {
6  public static class GraspingUtilities {
12  public static void DrawBoxFromCenter(Vector3 p, float r, Color c) {
13  // p is pos.yition of the center, r is "radius" and c is the color of the box
14  //Bottom lines
15  Debug.DrawLine(new Vector3(-r + p.x, -r + p.y, -r + p.z), new Vector3(r + p.x, -r + p.y, -r + p.z), c);
16  Debug.DrawLine(new Vector3(-r + p.x, -r + p.y, -r + p.z), new Vector3(-r + p.x, -r + p.y, r + p.z), c);
17  Debug.DrawLine(new Vector3(r + p.x, -r + p.y, r + p.z), new Vector3(-r + p.x, -r + p.y, r + p.z), c);
18  Debug.DrawLine(new Vector3(r + p.x, -r + p.y, r + p.z), new Vector3(r + p.x, -r + p.y, -r + p.z), c);
19 
20  //Vertical lines
21  Debug.DrawLine(new Vector3(-r + p.x, r + p.y, -r + p.z), new Vector3(r + p.x, r + p.y, -r + p.z), c);
22  Debug.DrawLine(new Vector3(-r + p.x, r + p.y, -r + p.z), new Vector3(-r + p.x, r + p.y, r + p.z), c);
23  Debug.DrawLine(new Vector3(r + p.x, r + p.y, r + p.z), new Vector3(-r + p.x, r + p.y, r + p.z), c);
24  Debug.DrawLine(new Vector3(r + p.x, r + p.y, r + p.z), new Vector3(r + p.x, r + p.y, -r + p.z), c);
25 
26  //Top lines
27  Debug.DrawLine(new Vector3(-r + p.x, -r + p.y, -r + p.z), new Vector3(-r + p.x, r + p.y, -r + p.z), c);
28  Debug.DrawLine(new Vector3(-r + p.x, -r + p.y, r + p.z), new Vector3(-r + p.x, r + p.y, r + p.z), c);
29  Debug.DrawLine(new Vector3(r + p.x, -r + p.y, -r + p.z), new Vector3(r + p.x, r + p.y, -r + p.z), c);
30  Debug.DrawLine(new Vector3(r + p.x, -r + p.y, r + p.z), new Vector3(r + p.x, r + p.y, r + p.z), c);
31  }
32 
40  public static void DrawRect(float x_size, float y_size, float z_size, Vector3 pos, Color color) {
41  var x = x_size / 2;
42  var y = y_size / 2;
43  var z = z_size / 2;
44 
45  //Vertical lines
46  Debug.DrawLine(new Vector3(-x + pos.x, -y + pos.y, -z + pos.z),
47  new Vector3(-x + pos.x, y + pos.y, -z + pos.z),
48  color);
49  Debug.DrawLine(new Vector3(x + pos.x, -y + pos.y, -z + pos.z),
50  new Vector3(x + pos.x, y + pos.y, -z + pos.z),
51  color);
52  Debug.DrawLine(new Vector3(-x + pos.x, -y + pos.y, z + pos.z),
53  new Vector3(-x + pos.x, y + pos.y, z + pos.z),
54  color);
55  Debug.DrawLine(new Vector3(x + pos.x, -y + pos.y, z + pos.z),
56  new Vector3(x + pos.x, y + pos.y, z + pos.z),
57  color);
58 
59  //Horizontal top
60  Debug.DrawLine(new Vector3(-x + pos.x, y + pos.y, -z + pos.z),
61  new Vector3(x + pos.x, y + pos.y, -z + pos.z),
62  color);
63  Debug.DrawLine(new Vector3(-x + pos.x, y + pos.y, z + pos.z),
64  new Vector3(x + pos.x, y + pos.y, z + pos.z),
65  color);
66  Debug.DrawLine(new Vector3(-x + pos.x, y + pos.y, -z + pos.z),
67  new Vector3(-x + pos.x, y + pos.y, z + pos.z),
68  color);
69  Debug.DrawLine(new Vector3(x + pos.x, y + pos.y, -z + pos.z),
70  new Vector3(x + pos.x, y + pos.y, z + pos.z),
71  color);
72 
73  //Horizontal bottom
74  Debug.DrawLine(new Vector3(-x + pos.x, -y + pos.y, -z + pos.z),
75  new Vector3(x + pos.x, -y + pos.y, -z + pos.z),
76  color);
77  Debug.DrawLine(new Vector3(-x + pos.x, -y + pos.y, z + pos.z),
78  new Vector3(x + pos.x, -y + pos.y, z + pos.z),
79  color);
80  Debug.DrawLine(new Vector3(-x + pos.x, -y + pos.y, -z + pos.z),
81  new Vector3(-x + pos.x, -y + pos.y, z + pos.z),
82  color);
83  Debug.DrawLine(new Vector3(x + pos.x, -y + pos.y, -z + pos.z),
84  new Vector3(x + pos.x, -y + pos.y, z + pos.z),
85  color);
86  }
87 
93  public static bool DidTransformsChange(
94  Transform[] old_transforms,
95  Transform[] newly_acquired_transforms) {
96  if (old_transforms.Length != newly_acquired_transforms.Length) {
97  return true;
98  }
99 
100  var i = 0;
101  foreach (var old in old_transforms) {
102  if (old.position != newly_acquired_transforms[i].position
103  || old.rotation != newly_acquired_transforms[i].rotation) {
104  return true;
105  }
106 
107  i++;
108  }
109 
110  return false;
111  }
112 
117  public static Bounds GetTotalMeshFilterBounds(Transform object_transform) {
118  var mesh_filter = object_transform.GetComponent<MeshFilter>();
119 
120  var result = mesh_filter != null ? mesh_filter.mesh.bounds : new Bounds();
121 
122  foreach (Transform transform in object_transform) {
123  var bounds = GetTotalMeshFilterBounds(transform);
124  result.Encapsulate(bounds.min);
125  result.Encapsulate(bounds.max);
126  }
127 
128  /*var bounds1 = GetTotalColliderBounds(objectTransform);
129  result.Encapsulate(bounds1.min);
130  result.Encapsulate(bounds1.max);
131  */
132  /*
133  foreach (Transform transform in objectTransform) {
134  var bounds = GetTotalColliderBounds(transform);
135  result.Encapsulate(bounds.min);
136  result.Encapsulate(bounds.max);
137  }
138  */
139  var scaled_min = result.min;
140  scaled_min.Scale(object_transform.localScale);
141  result.min = scaled_min;
142  var scaled_max = result.max;
143  scaled_max.Scale(object_transform.localScale);
144  result.max = scaled_max;
145  return result;
146  }
147 
152  public static Bounds GetTotalColliderBounds(Transform object_transform) {
153  var mesh_filter = object_transform.GetComponent<Collider>();
154 
155  var result = mesh_filter != null ? mesh_filter.bounds : new Bounds();
156 
157  foreach (Transform transform in object_transform) {
158  var bounds = GetTotalColliderBounds(transform);
159  result.Encapsulate(bounds.min);
160  result.Encapsulate(bounds.max);
161  }
162 
163  var scaled_min = result.min;
164  scaled_min.Scale(object_transform.localScale);
165  result.min = scaled_min;
166  var scaled_max = result.max;
167  scaled_max.Scale(object_transform.localScale);
168  result.max = scaled_max;
169  return result;
170  }
171 
176  public static Bounds GetMaxBounds(GameObject g) {
177  var b = new Bounds(g.transform.position, Vector3.zero);
178  foreach (var r in g.GetComponentsInChildren<Renderer>()) {
179  b.Encapsulate(r.bounds);
180  }
181 
182  return b;
183  }
184  }
185 }