-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvector3i.go
285 lines (245 loc) · 7.16 KB
/
vector3i.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2019 The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Initially copied from G3N: github.com/g3n/engine/math32
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// with modifications needed to suit GoGi functionality.
package mat32
// Vec3i is a 3D vector/point with X, Y and Z int32 components.
type Vec3i struct {
X int32
Y int32
Z int32
}
// V3i returns a new [Vec3i] with the given x, y and y components.
func V3i(x, y, z int32) Vec3i {
return Vec3i{X: x, Y: y, Z: z}
}
// V3iScalar returns a new [Vec3i] with all components set to the given scalar value.
func V3iScalar(s int32) Vec3i {
return Vec3i{X: s, Y: s, Z: s}
}
// Set sets this vector X, Y and Z components.
func (v *Vec3i) Set(x, y, z int32) {
v.X = x
v.Y = y
v.Z = z
}
// SetScalar sets all vector X, Y and Z components to same scalar value.
func (v *Vec3i) SetScalar(s int32) {
v.X = s
v.Y = s
v.Z = s
}
// SetFromVec3 sets from a Vec3 (float32) vector.
func (v *Vec3i) SetFromVec3(vf Vec3) {
v.X = int32(vf.X)
v.Y = int32(vf.Y)
v.Z = int32(vf.Z)
}
// SetDim sets this vector component value by dimension index.
func (v *Vec3i) SetDim(dim Dims, value int32) {
switch dim {
case X:
v.X = value
case Y:
v.Y = value
case Z:
v.Z = value
default:
panic("dim is out of range: ")
}
}
// Dim returns this vector component
func (v Vec3i) Dim(dim Dims) int32 {
switch dim {
case X:
return v.X
case Y:
return v.Y
case Z:
return v.Z
default:
panic("dim is out of range")
}
}
// SetByName sets this vector component value by its case insensitive name: "x", "y", or "z".
func (v *Vec3i) SetByName(name string, value int32) {
switch name {
case "x", "X":
v.X = value
case "y", "Y":
v.Y = value
case "z", "Z":
v.Z = value
default:
panic("Invalid Vec3i component name: " + name)
}
}
// SetZero sets this vector X, Y and Z components to be zero.
func (v *Vec3i) SetZero() {
v.SetScalar(0)
}
// FromArray sets this vector's components from the specified array and offset
func (v *Vec3i) FromArray(array []int32, offset int) {
v.X = array[offset]
v.Y = array[offset+1]
v.Z = array[offset+2]
}
// ToArray copies this vector's components to array starting at offset.
func (v Vec3i) ToArray(array []int32, offset int) {
array[offset] = v.X
array[offset+1] = v.Y
array[offset+2] = v.Z
}
///////////////////////////////////////////////////////////////////////
// Basic math operations
// Add adds other vector to this one and returns result in a new vector.
func (v Vec3i) Add(other Vec3i) Vec3i {
return Vec3i{v.X + other.X, v.Y + other.Y, v.Z + other.Z}
}
// AddScalar adds scalar s to each component of this vector and returns new vector.
func (v Vec3i) AddScalar(s int32) Vec3i {
return Vec3i{v.X + s, v.Y + s, v.Z + s}
}
// SetAdd sets this to addition with other vector (i.e., += or plus-equals).
func (v *Vec3i) SetAdd(other Vec3i) {
v.X += other.X
v.Y += other.Y
v.Z += other.Z
}
// SetAddScalar sets this to addition with scalar.
func (v *Vec3i) SetAddScalar(s int32) {
v.X += s
v.Y += s
v.Z += s
}
// Sub subtracts other vector from this one and returns result in new vector.
func (v Vec3i) Sub(other Vec3i) Vec3i {
return Vec3i{v.X - other.X, v.Y - other.Y, v.Z - other.Z}
}
// SubScalar subtracts scalar s from each component of this vector and returns new vector.
func (v Vec3i) SubScalar(s int32) Vec3i {
return Vec3i{v.X - s, v.Y - s, v.Z - s}
}
// SetSub sets this to subtraction with other vector (i.e., -= or minus-equals).
func (v *Vec3i) SetSub(other Vec3i) {
v.X -= other.X
v.Y -= other.Y
v.Z -= other.Z
}
// SetSubScalar sets this to subtraction of scalar.
func (v *Vec3i) SetSubScalar(s int32) {
v.X -= s
v.Y -= s
v.Z -= s
}
// Mul multiplies each component of this vector by the corresponding one from other
// and returns resulting vector.
func (v Vec3i) Mul(other Vec3i) Vec3i {
return Vec3i{v.X * other.X, v.Y * other.Y, v.Z * other.Z}
}
// MulScalar multiplies each component of this vector by the scalar s and returns resulting vector.
func (v Vec3i) MulScalar(s int32) Vec3i {
return Vec3i{v.X * s, v.Y * s, v.Z * s}
}
// SetMul sets this to multiplication with other vector (i.e., *= or times-equals).
func (v *Vec3i) SetMul(other Vec3i) {
v.X *= other.X
v.Y *= other.Y
v.Z *= other.Z
}
// SetMulScalar sets this to multiplication by scalar.
func (v *Vec3i) SetMulScalar(s int32) {
v.X *= s
v.Y *= s
v.Z *= s
}
// Div divides each component of this vector by the corresponding one from other vector
// and returns resulting vector.
func (v Vec3i) Div(other Vec3i) Vec3i {
return Vec3i{v.X / other.X, v.Y / other.Y, v.Z / other.Z}
}
// DivScalar divides each component of this vector by the scalar s and returns resulting vector.
// If scalar is zero, returns zero.
func (v Vec3i) DivScalar(scalar int32) Vec3i {
if scalar != 0 {
return v.MulScalar(1 / scalar)
} else {
return Vec3i{}
}
}
// SetDiv sets this to division by other vector (i.e., /= or divide-equals).
func (v *Vec3i) SetDiv(other Vec3i) {
v.X /= other.X
v.Y /= other.Y
v.Z /= other.Z
}
// SetDivScalar sets this to division by scalar.
func (v *Vec3i) SetDivScalar(s int32) {
if s != 0 {
v.SetMulScalar(1 / s)
} else {
v.SetZero()
}
}
// Min returns min of this vector components vs. other vector.
func (v Vec3i) Min(other Vec3i) Vec3i {
return Vec3i{Min32i(v.X, other.X), Min32i(v.Y, other.Y), Min32i(v.Z, other.Z)}
}
// SetMin sets this vector components to the minimum values of itself and other vector.
func (v *Vec3i) SetMin(other Vec3i) {
v.X = Min32i(v.X, other.X)
v.Y = Min32i(v.Y, other.Y)
v.Z = Min32i(v.Z, other.Z)
}
// Max returns max of this vector components vs. other vector.
func (v Vec3i) Max(other Vec3i) Vec3i {
return Vec3i{Max32i(v.X, other.X), Max32i(v.Y, other.Y), Max32i(v.Z, other.Z)}
}
// SetMax sets this vector components to the maximum value of itself and other vector.
func (v *Vec3i) SetMax(other Vec3i) {
v.X = Max32i(v.X, other.X)
v.Y = Max32i(v.Y, other.Y)
v.Z = Max32i(v.Z, other.Z)
}
// Clamp sets this vector components to be no less than the corresponding components of min
// and not greater than the corresponding component of max.
// Assumes min < max, if this assumption isn't true it will not operate correctly.
func (v *Vec3i) Clamp(min, max Vec3i) {
if v.X < min.X {
v.X = min.X
} else if v.X > max.X {
v.X = max.X
}
if v.Y < min.Y {
v.Y = min.Y
} else if v.Y > max.Y {
v.Y = max.Y
}
if v.Z < min.Z {
v.Z = min.Z
} else if v.Z > max.Z {
v.Z = max.Z
}
}
// ClampScalar sets this vector components to be no less than minVal and not greater than maxVal.
func (v *Vec3i) ClampScalar(minVal, maxVal int32) {
v.Clamp(V3iScalar(minVal), V3iScalar(maxVal))
}
// Negate returns vector with each component negated.
func (v Vec3i) Negate() Vec3i {
return Vec3i{-v.X, -v.Y, -v.Z}
}
// SetNegate negates each of this vector's components.
func (v *Vec3i) SetNegate() {
v.X = -v.X
v.Y = -v.Y
v.Z = -v.Z
}
// IsEqual returns if this vector is equal to other.
func (v Vec3i) IsEqual(other Vec3i) bool {
return (other.X == v.X) && (other.Y == v.Y) && (other.Z == v.Z)
}