-
Notifications
You must be signed in to change notification settings - Fork 0
/
testPacking.py
72 lines (53 loc) · 1.64 KB
/
testPacking.py
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
import math
import struct
import os
from mathutils import Vector
def readPackedVector(f, format):
packed = f
output = Vector()
if format == 'XZY':
output.x = packed
output.y = packed / 65536.0
output.z = packed / 256.0
elif format == 'ZXY':
output.x = packed / 256.0
output.y = packed / 65536.0
output.z = packed
elif format == 'XYZ':
output.x = packed
output.y = packed / 256.0
output.z = packed / 65536.0
output.x -= math.floor(output.x)
output.y -= math.floor(output.y)
output.z -= math.floor(output.z)
output.x = output.x*2 - 1
output.y = output.y*2 - 1
output.z = output.z*2 - 1
return output
def pack(vec, format):
scalar = []
if format == 'XZY':
vec = [vec[0], vec[2], vec[1]]
elif format == 'ZXY':
vec = [vec[2], vec[0], vec[1]]
elif format == 'XYZ':
scalar = [1.0, 256.0, 65536.0]
#vec = [(x + 1.0)/2.0 for x in vec]
vec[0] = (vec[0] + 1.0) / 2.0 * 255
vec[1] = (vec[1] + 1.0) / 2.0 * 255
vec[2] = (vec[2] + 1.0) / 2.0 * 255
#newVec = Vector()
#newVec.x = vec[0] * scalar[0]
#newVec.y = vec[1] * scalar[1]
#newVec.z = vec[2] * scalar[2]
packedInt = (int(vec[2]) << 16) | (int(vec[1]) << 8) | int(vec[0])
packedFloat = ((float(packedInt)) / (float((1 << 24))) ) * 65536.0
total = packedFloat
#return sum([a*b for a,b in zip(vec, scalar)])
return (total)
vec = [-0.2, -0.3, -0.4]
print(str(vec)+"\n")
packed = pack(vec, 'XYZ')
print(str(packed)+"\n")
unpack = readPackedVector(packed, 'XYZ')
print(str(unpack)+"\n")