/
Vec3b.cs
198 lines (177 loc) · 5.49 KB
/
Vec3b.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 byte (System.Byte)
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[SuppressMessage("Design", "CA1051: Do not declare visible instance fields")]
// ReSharper disable once InconsistentNaming
public struct Vec3b : IVec<Vec3b, byte>, IEquatable<Vec3b>
{
/// <summary>
/// The value of the first component of this object.
/// </summary>
public byte Item0;
/// <summary>
/// The value of the second component of this object.
/// </summary>
public byte Item1;
/// <summary>
/// The value of the third component of this object.
/// </summary>
public byte Item2;
/// <summary>
/// Deconstructing a Vector
/// </summary>
/// <param name="item0"></param>
/// <param name="item1"></param>
/// <param name="item2"></param>
public readonly void Deconstruct(out byte item0, out byte item1, out byte item2) => (item0, item1, item2) = (Item0, Item1, Item2);
/// <summary>
/// Initializer
/// </summary>
/// <param name="item0"></param>
/// <param name="item1"></param>
/// <param name="item2"></param>
public Vec3b(byte item0, byte item1, byte item2)
{
Item0 = item0;
Item1 = item1;
Item2 = item2;
}
#region Operators
/// <summary>
/// this + other
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public readonly Vec3b Add(Vec3b other) => new(
SaturateCast.ToByte(Item0 + other.Item0),
SaturateCast.ToByte(Item1 + other.Item1),
SaturateCast.ToByte(Item2 + other.Item2));
/// <summary>
/// this - other
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public readonly Vec3b Subtract(Vec3b other) => new(
SaturateCast.ToByte(Item0 - other.Item0),
SaturateCast.ToByte(Item1 - other.Item1),
SaturateCast.ToByte(Item2 - other.Item2));
/// <summary>
/// this * alpha
/// </summary>
/// <param name="alpha"></param>
/// <returns></returns>
public readonly Vec3b Multiply(double alpha) => new(
SaturateCast.ToByte(Item0 * alpha),
SaturateCast.ToByte(Item1 * alpha),
SaturateCast.ToByte(Item2 * alpha));
/// <summary>
/// this / alpha
/// </summary>
/// <param name="alpha"></param>
/// <returns></returns>
public readonly Vec3b Divide(double alpha) => new(
SaturateCast.ToByte(Item0 / alpha),
SaturateCast.ToByte(Item1 / alpha),
SaturateCast.ToByte(Item2 / alpha));
#pragma warning disable 1591
public static Vec3b operator +(Vec3b a, Vec3b b) => a.Add(b);
public static Vec3b operator -(Vec3b a, Vec3b b) => a.Subtract(b);
public static Vec3b operator *(Vec3b a, double alpha) => a.Multiply(alpha);
public static Vec3b operator /(Vec3b a, double alpha) => a.Divide(alpha);
#pragma warning restore 1591
/// <summary>
/// Indexer
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public byte 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 Vec3s ToVec3s() => new(Item0, Item1, Item2);
public Vec3w ToVec3w() => new(Item0, Item1, 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(Vec3b 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 Vec3b b && Equals(b);
}
/// <summary>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator ==(Vec3b a, Vec3b b)
{
return a.Equals(b);
}
/// <summary>
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static bool operator !=(Vec3b a, Vec3b b)
{
return !(a == 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(Vec3b)} ({Item0}, {Item1}, {Item2})";
}
}