AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Int3.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 System.Globalization;
6 using System.Runtime.InteropServices;
7 using UnityEngine;
8 
9 namespace HoloToolkit.Unity
10 {
14  [System.Serializable]
15  [StructLayout(LayoutKind.Sequential, Pack = 4)]
16  public struct Int3 : IEquatable<Int3>, IFormattable
17  {
18  public static readonly Int3 zero = new Int3(0, 0, 0);
19  public static readonly Int3 one = new Int3(1, 1, 1);
20 
21  public static readonly Int3 forward = new Int3(0, 0, 1);
22  public static readonly Int3 back = new Int3(0, 0, -1);
23  public static readonly Int3 up = new Int3(0, 1, 0);
24  public static readonly Int3 down = new Int3(0, -1, 0);
25  public static readonly Int3 left = new Int3(-1, 0, 0);
26  public static readonly Int3 right = new Int3(1, 0, 0);
27 
28  public int x;
29  public int y;
30  public int z;
31 
32  public Int3(int x, int y, int z)
33  {
34  this.x = x;
35  this.y = y;
36  this.z = z;
37  }
38 
39  public Int3(int x, int y)
40  {
41  this.x = x;
42  this.y = y;
43  this.z = 0;
44  }
45 
46  public override int GetHashCode()
47  {
48  var hashCode = x.GetHashCode();
49  hashCode = (hashCode * 397) ^ y.GetHashCode();
50  hashCode = (hashCode * 397) ^ z.GetHashCode();
51  return hashCode;
52  }
53 
54  public int this[int index]
55  {
56  get
57  {
58  if (index == 0) return x;
59  if (index == 1) return y;
60  if (index == 2) return z;
61  throw new ArgumentOutOfRangeException("Invalid Int3 index!");
62  }
63  set
64  {
65  if (index == 0) x = value;
66  if (index == 1) y = value;
67  if (index == 2) z = value;
68  throw new ArgumentOutOfRangeException("Invalid Int3 index!");
69  }
70  }
71 
72  #region conversion
73  public static explicit operator Vector3(Int3 v)
74  {
75  return new Vector3(v.x, v.y, v.z);
76  }
77 
78  public static explicit operator Vector2(Int3 v)
79  {
80  return new Vector2(v.x, v.y);
81  }
82  #endregion
83 
84  #region math operations
85  public float magnitude
86  {
87  get { return Magnitude(this); }
88  }
89 
90  public int sqrMagnitude
91  {
92  get { return SqrMagnitude(this); }
93  }
94 
95  public int dotOne
96  {
97  get { return DotOne(this); }
98  }
99 
100  public float Magnitude(Int3 v)
101  {
102  return Mathf.Sqrt(SqrMagnitude(v));
103  }
104 
105  public int SqrMagnitude(Int3 v)
106  {
107  return v.x * v.x + v.y * v.y + v.z * v.z;
108  }
109 
110  public static Int3 Scale(Int3 l, Int3 r)
111  {
112  return new Int3(l.x * r.x, l.y * r.y, l.z * r.z);
113  }
114 
115  public static int Dot(Int3 l, Int3 r)
116  {
117  return l.x * r.x + l.y * r.y + l.z * r.z;
118  }
119 
120  public static int DotOne(Int3 v)
121  {
122  return v.x * v.y * v.z;
123  }
124 
125  public static Int3 Cross(Int3 l, Int3 r)
126  {
127  return new Int3(l.y * r.z - l.z * r.y,
128  l.z * r.x - l.x * r.z,
129  l.x * r.y - l.y * r.x);
130  }
131 
132  public static Int3 Min(Int3 l, Int3 r)
133  {
134  return new Int3(Mathf.Min(l.x, r.x), Mathf.Min(l.y, r.y), Mathf.Min(l.z, r.z));
135  }
136 
137  public static Int3 Max(Int3 l, Int3 r)
138  {
139  return new Int3(Mathf.Max(l.x, r.x), Mathf.Max(l.y, r.y), Mathf.Max(l.z, r.z));
140  }
141 
142  public static Int3 Clamp(Int3 v, Int3 min, Int3 max)
143  {
144  return new Int3(Mathf.Clamp(v.x, min.x, max.x),
145  Mathf.Clamp(v.y, min.y, max.y),
146  Mathf.Clamp(v.z, min.z, max.z));
147  }
148 
149  public static Int3 ClosestPowerOfTwo(Int3 v)
150  {
151  return new Int3(Mathf.ClosestPowerOfTwo(v.x), Mathf.ClosestPowerOfTwo(v.y), Mathf.ClosestPowerOfTwo(v.z));
152  }
153 
154  public static int CubicToLinearIndex(Int3 v, Int3 size)
155  {
156  return (v.x) +
157  (v.y * size.x) +
158  (v.z * size.x * size.y);
159  }
160 
161  public static Int3 LinearToCubicIndex(int v, Int3 size)
162  {
163  return new Int3(v % size.x,
164  (v / size.x) % size.y,
165  (v / (size.x * size.y)) % size.z);
166  }
167  #endregion math operations
168 
169  #region math operators
170  public static Int3 operator +(Int3 l, Int3 r)
171  {
172  return new Int3(l.x + r.x, l.y + r.y, l.z + r.z);
173  }
174 
175  public static Int3 operator -(Int3 l, Int3 r)
176  {
177  return new Int3(l.x - r.x, l.y - r.y, l.z - r.z);
178  }
179 
180  public static Int3 operator -(Int3 v)
181  {
182  return new Int3(-v.x, -v.y, -v.z);
183  }
184 
185  public static Int3 operator *(Int3 v, int d)
186  {
187  return new Int3(v.x * d, v.y * d, v.z * d);
188  }
189 
190  public static Int3 operator *(int d, Int3 v)
191  {
192  return new Int3(v.x * d, v.y * d, v.z * d);
193  }
194 
195  public static Int3 operator /(Int3 v, int d)
196  {
197  return new Int3(v.x / d, v.y / d, v.z / d);
198  }
199  #endregion math operators
200 
201  #region comparison
202  public bool Equals(Int3 other)
203  {
204  return x.Equals(other.x) && y.Equals(other.y) && z.Equals(other.z);
205  }
206 
207  public static bool operator ==(Int3 l, Int3 r)
208  {
209  return l.Equals(r);
210  }
211 
212  public static bool operator !=(Int3 l, Int3 r)
213  {
214  return !l.Equals(r);
215  }
216 
217  public override bool Equals(object value)
218  {
219  return (value is Int3) ? Equals((Int3)value) : false;
220  }
221  #endregion
222 
223  #region IFormattable
224  public override string ToString()
225  {
226  return ToString(CultureInfo.CurrentCulture);
227  }
228 
229  public string ToString(IFormatProvider formatProvider)
230  {
231  return string.Format(formatProvider, "X:{0} Y:{1} Z:{2}", x, y, z);
232  }
233 
234  public string ToString(string format, IFormatProvider formatProvider)
235  {
236  if (format == null)
237  {
238  return ToString(formatProvider);
239  }
240 
241  return string.Format(formatProvider,
242  "X:{0} Y:{1} Z:{2}",
243  x.ToString(format, formatProvider),
244  y.ToString(format, formatProvider),
245  z.ToString(format, formatProvider));
246  }
247  #endregion
248  }
249 }
static int Dot(Int3 l, Int3 r)
Definition: Int3.cs:115
bool Equals(Int3 other)
Definition: Int3.cs:202
int SqrMagnitude(Int3 v)
Definition: Int3.cs:105
static int CubicToLinearIndex(Int3 v, Int3 size)
Definition: Int3.cs:154
override int GetHashCode()
Definition: Int3.cs:46
Int3(int x, int y, int z)
Definition: Int3.cs:32
static Int3 LinearToCubicIndex(int v, Int3 size)
Definition: Int3.cs:161
override bool Equals(object value)
Definition: Int3.cs:217
float Magnitude(Int3 v)
Definition: Int3.cs:100
static Int3 Scale(Int3 l, Int3 r)
Definition: Int3.cs:110
static int DotOne(Int3 v)
Definition: Int3.cs:120
3D integer class - operates similarly to Unity&#39;s Vector3D
Definition: Int3.cs:16
static Int3 Max(Int3 l, Int3 r)
Definition: Int3.cs:137
override string ToString()
Definition: Int3.cs:224
static Int3 ClosestPowerOfTwo(Int3 v)
Definition: Int3.cs:149
static Int3 Cross(Int3 l, Int3 r)
Definition: Int3.cs:125
Int3(int x, int y)
Definition: Int3.cs:39
string ToString(IFormatProvider formatProvider)
Definition: Int3.cs:229
static Int3 Min(Int3 l, Int3 r)
Definition: Int3.cs:132
static Int3 Clamp(Int3 v, Int3 min, Int3 max)
Definition: Int3.cs:142
string ToString(string format, IFormatProvider formatProvider)
Definition: Int3.cs:234