/
Vec3s.cs
198 lines (177 loc) · 5.6 KB
/
Vec3s.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
197
198
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using OpenCvSharp.Internal.Util;
namespace OpenCvSharp;
/// <summary>
/// 3-Tuple of short (System.Int16)
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields")]
// ReSharper disable once InconsistentNaming
public struct Vec3s : IVec<Vec3s, short>, IEquatable<Vec3s>
{
/// <summary>
/// The value of the first component of this object.
/// </summary>
public short Item0;
/// <summary>
/// The value of the second component of this object.
/// </summary>
public short Item1;
/// <summary>
/// The value of the third component of this object.
/// </summary>
public short Item2;
/// <summary>
/// Deconstructing a Vector
/// </summary>
/// <param name="item0"></param>
/// <param name="item1"></param>
/// <param name="item2"></param>
public readonly void Deconstruct(out short item0, out short item1, out short item2) => (item0, item1, item2) = (Item0, Item1, Item2);
/// <summary>
/// Initializer
/// </summary>
/// <param name="item0"></param>
/// <param name="item1"></param>
/// <param name="item2"></param>
public Vec3s(short item0, short item1, short item2)
{
Item0 = item0;
Item1 = item1;
Item2 = item2;
}
#region Operators
/// <summary>
/// this + other
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public readonly Vec3s Add(Vec3s other) => new(
SaturateCast.ToInt16(Item0 + other.Item0),
SaturateCast.ToInt16(Item1 + other.Item1),
SaturateCast.ToInt16(Item2 + other.Item2));
/// <summary>
/// this - other
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public readonly Vec3s Subtract(Vec3s other) => new(
SaturateCast.ToInt16(Item0 - other.Item0),
SaturateCast.ToInt16(Item1 - other.Item1),
SaturateCast.ToInt16(Item2 - other.Item2));
/// <summary>
/// this * alpha
/// </summary>
/// <param name="alpha"></param>
/// <returns></returns>
public readonly Vec3s Multiply(double alpha) => new(
SaturateCast.ToInt16(Item0 * alpha),
SaturateCast.ToInt16(Item1 * alpha),
SaturateCast.ToInt16(Item2 * alpha));
/// <summary>
/// this / alpha
/// </summary>
/// <param name="alpha"></param>
/// <returns></returns>
public readonly Vec3s Divide(double alpha) => new(
SaturateCast.ToInt16(Item0 / alpha),
SaturateCast.ToInt16(Item1 / alpha),
SaturateCast.ToInt16(Item2 / alpha));
#pragma warning disable 1591
public static Vec3s operator +(Vec3s a, Vec3s b) => a.Add(b);
public static Vec3s operator -(Vec3s a, Vec3s b) => a.Subtract(b);
public static Vec3s operator *(Vec3s a, double alpha) => a.Multiply(alpha);
public static Vec3s operator /(Vec3s a, double alpha) => a.Divide(alpha);
#pragma warning restore 1591
/// <summary>
/// Indexer
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public short 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 Vec6b ToVec6b() => new Vec6b((byte)Item0, (byte)Item1, (byte)Item2, (byte)Item3, (byte)Item4, (byte)Item5);
public Vec3w ToVec3w() => new((ushort)Item0, (ushort)Item1, (ushort)Item2);
public Vec3i ToVec3i() => new(Item0, Item1, Item2);
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(Vec3s 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 Vec3s v && Equals(v);
}
/// <summary>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator ==(Vec3s a, Vec3s b)
{
return a.Equals(b);
}
/// <summary>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator !=(Vec3s a, Vec3s b)
{
return !a.Equals(b);
}
/// <inheritdoc />
public override readonly int GetHashCode()
{
#if DOTNET_FRAMEWORK || NETSTANDARD2_0
unchecked
{
var hashCode = Item0.GetHashCode();
hashCode = (hashCode * 397) ^ Item1.GetHashCode();
hashCode = (hashCode * 397) ^ Item2.GetHashCode();
return hashCode;
}
#else
return HashCode.Combine(Item0, Item1, Item2);
#endif
}
/// <inheritdoc />
public override readonly string ToString()
{
return $"{nameof(Vec3s)} ({Item0}, {Item1}, {Item2})";
}
}