/
Vec3i.cs
196 lines (175 loc) · 5.32 KB
/
Vec3i.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using OpenCvSharp.Internal.Util;
namespace OpenCvSharp;
/// <summary>
/// 3-Tuple of int (System.Int32)
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields")]
// ReSharper disable once InconsistentNaming
public struct Vec3i : IVec<Vec3i, int>, IEquatable<Vec3i>
{
/// <summary>
/// The value of the first component of this object.
/// </summary>
public int Item0;
/// <summary>
/// The value of the second component of this object.
/// </summary>
public int Item1;
/// <summary>
/// The value of the third component of this object.
/// </summary>
public int Item2;
/// <summary>
/// Deconstructing a Vector
/// </summary>
/// <param name="item0"></param>
/// <param name="item1"></param>
/// <param name="item2"></param>
public readonly void Deconstruct(out int item0, out int item1, out int item2)
=> (item0, item1, item2) = (Item0, Item1, Item2);
/// <summary>
/// Initializer
/// </summary>
/// <param name="item0"></param>
/// <param name="item1"></param>
/// <param name="item2"></param>
public Vec3i(int item0, int item1, int item2)
{
Item0 = item0;
Item1 = item1;
Item2 = item2;
}
#region Operators
/// <summary>
/// this + other
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public readonly Vec3i Add(Vec3i other) => new(
SaturateCast.ToInt32(Item0 + other.Item0),
SaturateCast.ToInt32(Item1 + other.Item1),
SaturateCast.ToInt32(Item2 + other.Item2));
/// <summary>
/// this - other
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public readonly Vec3i Subtract(Vec3i other) => new(
SaturateCast.ToInt32(Item0 - other.Item0),
SaturateCast.ToInt32(Item1 - other.Item1),
SaturateCast.ToInt32(Item2 - other.Item2));
/// <summary>
/// this * alpha
/// </summary>
/// <param name="alpha"></param>
/// <returns></returns>
public readonly Vec3i Multiply(double alpha) => new(
SaturateCast.ToInt32(Item0 * alpha),
SaturateCast.ToInt32(Item1 * alpha),
SaturateCast.ToInt32(Item2 * alpha));
/// <summary>
/// this / alpha
/// </summary>
/// <param name="alpha"></param>
/// <returns></returns>
public readonly Vec3i Divide(double alpha) => new(
SaturateCast.ToInt32(Item0 / alpha),
SaturateCast.ToInt32(Item1 / alpha),
SaturateCast.ToInt32(Item2 / alpha));
#pragma warning disable 1591
public static Vec3i operator +(Vec3i a, Vec3i b) => a.Add(b);
public static Vec3i operator -(Vec3i a, Vec3i b) => a.Subtract(b);
public static Vec3i operator *(Vec3i a, double alpha) => a.Multiply(alpha);
public static Vec3i operator /(Vec3i a, double alpha) => a.Divide(alpha);
#pragma warning restore 1591
/// <summary>
/// Indexer
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public int this[int i]
{
readonly get
{
return i switch
{
0 => Item0,
1 => Item1,
2 => Item2,
_ => throw new ArgumentOutOfRangeException(nameof(i))
};
}
set
{
switch (i)
{
case 0: Item0 = value; break;
case 1: Item1 = value; break;
case 2: Item2 = value; break;
default: throw new ArgumentOutOfRangeException(nameof(i));
}
}
}
#endregion
#pragma warning disable 1591
// ReSharper disable InconsistentNaming
public Vec3f ToVec3f() => new(Item0, Item1, Item2);
public Vec3d ToVec3d() => new(Item0, Item1, Item2);
// ReSharper restore InconsistentNaming
#pragma warning restore 1591
/// <inheritdoc />
public readonly bool Equals(Vec3i other)
{
return Item0 == other.Item0 &&
Item1 == other.Item1 &&
Item2 == other.Item2;
}
/// <inheritdoc />
public override readonly bool Equals(object? obj)
{
if (obj is null) return false;
return obj is Vec3i v && Equals(v);
}
/// <summary>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator ==(Vec3i a, Vec3i b)
{
return a.Equals(b);
}
/// <summary>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator !=(Vec3i a, Vec3i b)
{
return !a.Equals(b);
}
/// <inheritdoc />
public override readonly int GetHashCode()
{
#if DOTNET_FRAMEWORK || NETSTANDARD2_0
unchecked
{
var hashCode = Item0;
hashCode = (hashCode * 397) ^ Item1;
hashCode = (hashCode * 397) ^ Item2;
return hashCode;
}
#else
return HashCode.Combine(Item0, Item1, Item2);
#endif
}
/// <inheritdoc />
public override readonly string ToString()
{
return $"{nameof(Vec3i)} ({Item0}, {Item1}, {Item2})";
}
}