File: Using-vectors.sb

package info (click to toggle)
python-pyglm 2.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,008 kB
  • sloc: cpp: 53,029; python: 3,683; makefile: 7
file content (285 lines) | stat: -rw-r--r-- 11,947 bytes parent folder | download
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
\h1\Using Vectors\h1\
\lo \
\-\ \url #Initialization \Initialization\ url\\list switch\
\--\ \url #initialization-with-no-arguments \with no arguments\ url\
\--\ \url #Initialization-with-a-single-number \with single number\ url\
\--\ \url #Initializing-all-components-with-numbers \all components with numbers\ url\
\--\ \url #Copying-a-vector \copying a vector\ url\
\--\ \url #Initializing-vectors-with-larger-vectors \with larger vectors\ url\
\--\ \url #Constructing-vectors-from-other-vectors-and-numbers \with vectors and numbers\ url\
\--\ \url #lists-and-other-iterables \Lists (and other iterables)\ url\
\--\ \url #objects-that-support-the-buffer-protocol-numpy-bytes \Buffer protocol (numpy, bytes)\ url\\list switch\
\-\ \url #Members \Members\ url\
\-\ \url #Methods \Methods\ url\\list switch\
\--\ \url #the-copy-protocol \The copy protocol\ url\
\--\ \url #pickling \Pickling\ url\
\--\ \url #to-list--tuple \To list / tuple\ url\
\--\ \url #to-and-from-bytes \To and from bytes\ url\\list switch\
\-\ \url #Operators \Operators\ url\\list switch\
\--\ \url #add--operator \add\ url\
\--\ \url #sub--operator \sub\ url\
\--\ \url #mul--operator \mul\ url\
\--\ \url #matmul--operator \matmul\ url\
\--\ \url #div--operator \div\ url\
\--\ \url #mod--operator \mod\ url\
\--\ \url #floordiv--operator \floordiv\ url\
\--\ \url #divmod \divmod\ url\
\--\ \url #lshift--operator \lshift\ url\
\--\ \url #rshift--operator \rshift\ url\
\--\ \url #and--operator \and\ url\
\--\ \url #or--operator \or\ url\
\--\ \url #xor--operator \xor\ url\
\--\ \url #pow--operator \pow\ url\
\--\ \url #len \len\ url\
\--\ \url #getitem-and-setitem--operator \getitem and setitem\ url\
\--\ \url #contains-in-operator \contains\ url\
\--\ \url #richcompare-eg--operator \richcompare\ url\
\--\ \url #iter \iter\ url\
\--\ \url #hash\hash\url\
\ list\
\h2\Initialization\h2\
There are dozens of ways of constructing a vector.  
For simplicity, if the same initialization process applies to all vector types, it will only be shown for \code\glm.vec2\code\.
\h4\Initialization with no arguments\h4\
Initializing a vector without any additional arguments will set all of it's components to zero (of the respective type).  
i.e. \code\glm.vec2()\code\ returns vector \code\(0.0, 0.0)\code\.  
A boolean vector would also be initialized with zero (or \code\False\code\ if you will).
\h4\Initialization with a single number\h4\
Initializing a vector with a number will set all of it's components to the given number (which may be converted if necessary).  
i.e. \code\glm.vec2(2.43)\code\ returns vector \code\(2.43, 2.43)\code\.  
\h4\Initializing all components with numbers\h4\
A vector \code\vecN\code\ can be initialized with \i\N\i\ numbers, which will be copied (or may be converted) to their components.  
i.e. \code\glm.vec2(1, 2)\code\ returns vector \code\(1.0, 2.0)\code\  
\code\glm.vec3(4, 5, 6)\code\ returns vector \code\(4.0, 5.0, 6.0)\code\  
\code\glm.ivec4(9, 8, 7, 6)\code\ returns vector \code\(9, 8, 7, 6)\code\
\h4\Copying a vector\h4\
A copy of a vector can be obtained by initializing a vector with an instance of a vector.  
i.e. \code\glm.vec2(glm.vec2(3, 2))\code\ returns vector \code\(3.0, 2.0)\code\  
This is what's known as the copy constructor.
\h4\Initializing vectors with larger vectors\h4\
You can initialize any vector with a larger vector (which will discard any values that don't fit into the new vector).  
i.e. \code\glm.vec1(glm.vec3(1, 2, 3))\code\ returns vector \code\(1.0)\code\  
likewise \code\glm.vec2(glm.vec4(5, 6, 7, 8))\code\ returns vector \code\(5.0, 6.0)\code\
\h4\Constructing vectors from other vectors and numbers\h4\
As long as you don't use any \code\vec1\code\s in your equation, you can construct any vector from a combination of vectors and / or numbers if their sum equals the length of the target vector.  
i.e. \code\glm.vec4(glm.vec2(1, 2), 3, 4)\code\ returns vector \code\(1.0, 2.0, 3.0, 4.0)\code\  
likewise \code\glm.vec3(5, glm.vec2(4, 3))\code\ returns vector \code\(5.0, 4.0, 3.0)\code\  

but \code\glm.vec2(glm.vec1(1), 2)\code\ doesn't work.  
\code\glm.vec3(glm.vec2(1, 2), glm.vec2(3, 4))\code\ also doesn't work.
\h3\Lists (and other iterables)\h3\
Instead of using vectors to initialize vectors, you can also use lists and other iterables.  
e.g. \code\glm.vec2([1, 2])\code\ returns vector \code\(1.0, 2.0)\code\  
or \code\glm.vec3((3, 4), 5)\code\ returns vector \code\(3.0, 4.0, 5.0)\code\  

\h3\Objects that support the buffer protocol (numpy, bytes)\h3\
A few objects in Python support a functionality called the buffer protocol.  
One such example would be the Python \code\bytes\code\ type or \code\numpy.array\code\.  
PyGLM also supports this protocol and thus can be converted to or from any other object that supports it, granted it's in a fitting format.  
e.g. \code\bytes(glm.u8vec2(1,2))\code\ returns \code\b'\\x01\\x02'\code\  
and \code\glm.u8vec2(b'\\x01\\x02')\code\ returns an 8-bit unsigned integer vector \code\(1, 2)\code\

or \code\glm.vec3(numpy.array([4,5,6]))\code\ returns vector \code\(4.0, 5.0, 6.0)\code\  
and \code\numpy.array(glm.vec3(4, 5, 6))\code\ returns \code\array([4., 5., 6.], dtype=float32)\code\  

\i\Note: objects that use the buffer protocol \i\may\i\ request a reference instead of a copy of the object, meaning that if you change the 'copy', you'll also change the original.\i\

\h2\Members\h2\
A vector has a member for each of it's values.  
\code\vec1\code\ has members: \code\(x)\code\  
\code\vec2\code\ has members: \code\(x, y)\code\  
\code\vec3\code\ has members: \code\(x, y, z)\code\  
\code\vec4\code\ has members: \code\(x, y, z, w)\code\  

Using swizzling, you can also construct vectors from up to four members:
\code Python \
v  = vec4(1, 2, 3, 4)
v2 = v.xy             # returns vec2(1, 2)
v3 = v.zw             # returns vec2(3, 4)
v4 = v.xxxw           # returns vec4(1, 1, 1, 4)
\ code\

\h2\Methods\h2\
Any vector type implements the following methods:

\h3\The copy protocol\h3\
Vectors support the copy protocol (see \url https://docs.python.org/3/library/copy.html \here\ url\).  
You can use \code\copy.copy(<vector>)\code\ or \code\copy.deepcopy(<vector>)\code\ to get a copy of a vector.

\h3\Pickling\h3\
Vectors support \url https://docs.python.org/3/library/pickle.html#module-interface \pickling\url\ (as of PyGLM 2.0.0), which is Python's serialization method.

\h3\To list / tuple\h3\
Any vector type has a \code\to_list()\code\ and a \code\to_tuple()\code\ function, which return's the vector's data represented as a list or tuple respectively.

\h3\To and from bytes\h3\
All vectors have a \code\to_bytes()\code\ and a \code\from_bytes()\code\ method, which allows for conversion of the vector's data to and from bytes strings.

\h2\Operators\h2\
Vector types support a \i\lot\i\ of operators.
\h3\add (\code\+\code\ operator)\h3\
Vectors support addition with other vectors and numbers.  
\code Python \
sum1 = vec2(1, 2) + vec2(4, 0) # returns vec2(5, 2)
sum2 = vec2(1, 2) + 4          # returns vec2(5, 6)
\ code\
\h3\sub (\code\-\code\ operator)\h3\
Vectors support subtraction with other vectors and numbers.  
\code Python \
diff1 = vec2(1, 2) - vec2(4, 0) # returns vec2(-3,  2)
diff2 = vec2(1, 2) - 4          # returns vec2(-3, -2)
\ code\
\h3\mul (\code\*\code\ operator)\h3\
Vectors support multiplication with other vectors and numbers.  
\code Python \
prod1 = vec2(1, 2) * vec2(4, 0) # returns vec2(4, 0)
prod2 = vec2(1, 2) * 4          # returns vec2(4, 8)
\ code\
\h3 \matmul (\code\@\code\ operator)\ h3\
Has the same effects as the \code\*\code\ operator, but with the arguments switched.
I.e. \code\a * b == b @ a\code\
\h3\div (\code\/\code\ operator)\h3\
Vectors support division with other vectors and numbers.  
\code Python \
quot1 = vec2(1, 2) / vec2(4, 0.5) # returns vec2(0.25, 4  )
quot2 = vec2(1, 2) / 4            # returns vec2(0.25, 0.5)
\ code\
\h3\mod (\code\%\code\ operator)\h3\
Vectors support modulo operations with other vectors and numbers.  
\code Python \
mod1 = vec2(1, 2) % vec2(4, 2) # returns vec2(1, 0)
mod2 = vec2(1, 2) % 4            # returns vec2(1, 2)
\ code\
\h3\floordiv (\code\//\code\ operator)\h3\
Vectors support floored division with other vectors and numbers.  
\code Python \
fquot1 = vec2(1, 2) // vec2(4, 0.5) # returns vec2(0, 4)
fquot2 = vec2(1, 2) // 4            # returns vec2(0, 0)
\ code\
\h3\divmod\h3\
Vectors support combined floor division and modulo operations with other vectors and numbers.  
\code Python \
divmod1 = divmod(vec2(1, 2), vec2(4, 2)) # returns (vec2(0, 1), vec2(1, 0))
divmod2 = divmod(vec2(1, 2), 4)          # returns (vec2(0, 0), vec2(1, 2))
\ code\
\h3\lshift (\code\<<\code\ operator)\h3\
Integer vectors support the bitwise left shift operator.  
\code Python \
>>> ivec3(1, 2, 3) << 4
ivec3( 16, 32, 48 )
>>> uvec3(1, 2, 3) << uvec3(1, 2, 3)
uvec3( 2, 8, 24 )
\ code\
\h3\rshift (\code\>>\code\ operator)\h3\
Integer vectors support the bitwise right shift operator.  
\code Python \
>>> ivec3(16, 32, 48) >> 4
ivec3( 1, 2, 3 )
>>> uvec3(2, 8, 24) >> uvec3(1, 2, 3)
uvec3( 1, 2, 3 )
\ code\
\h3\and (\code\&\code\ operator)\h3\
Integer vectors support the bitwise and operator.  
\code Python \
>>> ivec3(1, 2, 3) & 2
ivec3( 0, 2, 2 )
>>> uvec3(1, 2, 3) & uvec3(3, 2, 1)
uvec3( 1, 2, 1 )
\ code\
\h3\or (\code\|\code\ operator)\h3\
Integer vectors support the bitwise or operator.  
\code Python \
>>> ivec3(1, 2, 3) | 2
ivec3( 3, 2, 3 )
>>> uvec3(1, 2, 3) | uvec3(6, 5, 4)
uvec3( 7, 7, 7 )
\ code\
\h3\xor (\code\^\code\ operator)\h3\
Integer vectors support the bitwise xor operator.  
\code Python \
>>> ivec3(1, 2, 3) ^ 2
ivec3( 3, 0, 1 )
>>> uvec3(1, 2, 3) ^ uvec3(3, 2, 1)
uvec3( 2, 0, 2 )
\ code\
\h3\pow (\code\**\code\ operator)\h3\
Vectors support pow operations with other vectors and numbers.  
\code Python \
pow1 = vec2(1, 2) ** vec2(4, 2) # returns vec2(1,  4)
pow2 = vec2(1, 2) ** 4          # returns vec2(1, 16)
\ code\
\h3\len\h3\
The length of a vector can be queried using \code\len()\code\.
\code Python \
vec_length = len(vec2()) # returns 2
\ code\
\h3\getitem and setitem (\code\[]\code\ operator)\h3\
You can get the values of a vector using indices.
\code Python \
v = vec2(1, 2)
print(v[0]) # prints 1.0
print(v[1]) # prints 2.0
\ code\
Likewise you can set the values.
\code Python \
v    = vec2(1, 2)
v[0] = 9
print(v.x) # prints 9.0
\ code\
\h3\contains (\code\in\code\ operator)\h3\
You can query wether or not a value is contained by a vector using the \code\in\code\ operator.
\code Python \
v     = vec2(1, 2)
true  = 2    in v
false = 2.01 in v
\ code\
\h3\richcompare (e.g. \code\==\code\ operator)\h3\
You can compare vectors using the richcompare operators:
\code Python \
vec2(1, 2) == vec2(1, 2)    # True
vec2(1, 2) == vec2(2, 2)    # False
vec2(1, 2) == vec3(1, 2, 3) # False

vec2(1, 2) != vec2(1, 2)    # False
vec2(1, 2) != vec2(2, 2)    # True
vec2(1, 2) != vec3(1, 2, 3) # True

vec2(1, 2) < vec2(5, 5)     # vec2(1, 1)
vec2(1, 2) < vec2(2, 2)     # vec2(1, 0)
vec2(1, 2) < vec2(0, 0)     # vec2(0, 0)

vec2(1, 2) <= vec2(5, 5)    # vec2(1, 1)
vec2(1, 2) <= vec2(2, 2)    # vec2(1, 1)
vec2(1, 2) <= vec2(0, 0)    # vec2(0, 0)

vec2(1, 2) > vec2(5, 5)     # vec2(0, 0)
vec2(1, 2) > vec2(2, 2)     # vec2(0, 0)
vec2(1, 2) > vec2(0, 0)     # vec2(1, 1)

vec2(1, 2) >= vec2(5, 5)    # vec2(0, 0)
vec2(1, 2) >= vec2(2, 2)    # vec2(0, 1)
vec2(1, 2) >= vec2(0, 0)    # vec2(1, 1)
\ code\
\h3\iter\h3\
You can generate an iterable from vectors using \code\iter()\code\.
\code Python \
v  = vec2(1, 2)
it = iter(v)
print(next(it)) # prints 1.0
print(next(it)) # prints 2.0
\ code\

\h3\hash\h3\
You can generate a hash value for vectors using \code\hash()\code\
Example:
\code Python\
>>> v = vec2()
>>> hash(v)
-1952026010959490761
>>> v2 = vec2(1, 2)
>>> hash(v2)
8639716006723752019
>>> v3 = v2 * 0
>>> hash(v3)
-1952026010959490761
\code\