lslbasefuncs: Rewrite some things in a different way.

Also some formatting changes. No functionality changes.
This commit is contained in:
Sei Lisa 2017-10-18 13:45:46 +02:00
parent ea28e13e4a
commit 028b244a9e

View file

@ -379,30 +379,26 @@ def f2s(val, DP=6):
new_s = u'1' + new_s # 9...9 -> 10...0
else:
# increment s[ci] e.g. 43999 -> 44000
new_s = s[:ci] + chr(ord(s[ci])+1) + new_s
new_s = s[:ci] + chr(ord(s[ci]) + 1) + new_s
else:
new_s = s[:i]
if i <= dot:
return sgn + new_s + u'0'*(dot-i) + u'.' + u'0'*DP
return sgn + new_s + u'0'*(dot+1+DP-i)
return sgn + new_s + u'0' * (dot - i) + u'.' + u'0' * DP
return sgn + new_s + u'0' * (dot + 1 + DP - i)
def vr2s(v, DP=6):
if type(v) == Vector:
return u'<'+f2s(v[0],DP)+u', '+f2s(v[1],DP)+u', '+f2s(v[2],DP)+u'>'
return u'<'+f2s(v[0],DP)+u', '+f2s(v[1],DP)+u', '+f2s(v[2],DP)+u', '+f2s(v[3],DP)+u'>'
assert len(v) == (3 if type(v) == Vector else 4)
return u'<' + ', '.join(f2s(x, DP) for x in v) + u'>'
def qnz(q):
if all(x == 0. for x in q):
return Quaternion((0.,0.,0.,1.))
return q
return Quaternion((0.,0.,0.,1.)) if all(x == 0. for x in q) else q
def qnorm(q):
q = qnz(q)
mag2 = math.fsum((q[0]*q[0], q[1]*q[1], q[2]*q[2], q[3]*q[3]))
# Threshold for renormalization
eps_h = 1.0000021457672119140625 #float.fromhex('0x1.000024p0')
eps_h = 1.0000021457672119140625 # float.fromhex('0x1.000024p0')
eps_l = 0.99999797344207763671875 # float.fromhex('0x1.FFFFBCp-1')
if mag2 >= eps_h or mag2 <= eps_l:
# Renormalize
@ -590,12 +586,8 @@ def InternalGetDeleteSubSequence(val, start, end, isGet):
if end == -1: end += L
if (start+L if start < 0 else start) > (end+L if end < 0 else end):
# Exclusion range - get/delete from end and start
if isGet:
return val[:end+1] + val[start:]
return val[end+1:start]
if isGet:
return val[start:end+1]
return val[:start] + val[end+1:]
return val[:end+1] + val[start:] if isGet else val[end+1:start]
return val[start:end+1] if isGet else val[:start] + val[end+1:]
def typecast(val, out, InList=False, f32=True):
"""Type cast an item. Calls InternalList2Strings for lists and
@ -680,8 +672,10 @@ def mul(a, b, f32=True):
if tb in (int, float):
if ta == tb == int:
return S32(a*b)
if math.isnan(a) and math.isnan(b) and math.copysign(1, a) != math.copysign(1, b):
return NaN
if math.isnan(a) and math.isnan(b):
return (-NaN
if math.copysign(1, a) == math.copysign(1, b) == -1
else NaN)
return F32(ff(a)*ff(b), f32)
if tb != Vector:
# scalar * quat is not defined
@ -803,15 +797,13 @@ def compare(a, b, Eq = True):
ret = a == b
else:
ret = ff(a) == ff(b)
return int(ret) if Eq else 1-ret
return int(ret == Eq)
if ta in (unicode, Key) and tb in (unicode, Key):
ret = 0 if a == b else 1 if a > b or not lslcommon.LSO else -1
ret = 0 if a == b else 1 if not lslcommon.LSO or a > b else -1
return int(not ret) if Eq else ret
if ta == tb in (Vector, Quaternion):
for ae,be in zip(a,b):
if ae != be:
return int(not Eq)
return int(Eq)
ret = not any(ae != be for ae, be in zip(a, b))
return int(ret == Eq)
if ta == tb == list:
ret = len(a) - len(b)
return int(not ret) if Eq else ret
@ -887,12 +879,10 @@ def llAtan2(y, x):
y = ff(y)
x = ff(x)
if math.isnan(x) and math.isnan(y):
if math.copysign(1, x) == -1 and math.copysign(1, y) == -1:
return -NaN
return NaN
elif math.isnan(x):
return mul(x, y)
if math.isnan(x):
return x
elif math.isnan(y):
if math.isnan(y):
return y
return F32(math.atan2(y, x))
@ -934,7 +924,6 @@ def llAxes2Rot(fwd, left, up):
mag = math.sqrt(math.fsum((q[0]*q[0], q[1]*q[1], q[2]*q[2], q[3]*q[3])))
return Quaternion(F32((q[0]/mag, q[1]/mag, q[2]/mag, q[3]/mag)))
def llAxisAngle2Rot(axis, angle):
axis = v2f(axis)
angle = ff(angle)
@ -1165,8 +1154,7 @@ def llGenerateKey():
s = hashlib.md5((u'%.17g %f %f' % (time.time(), random.random(),
random.random())).encode('utf8')
).hexdigest()
return Key(s[:8] + '-' + s[8:12] + '-' + s[12:16] + '-' + s[16:20]
+ '-' + s[20:32])
return Key('-'.join((s[:8], s[8:12], s[12:16], s[16:20], s[20:32])))
# Can't give a concrete value
raise ELSLCantCompute
@ -1200,7 +1188,8 @@ def llInsertString(s, pos, src):
def llIntegerToBase64(x):
x = fi(x)
return b64encode(chr((x>>24)&255) + chr((x>>16)&255) + chr((x>>8)&255) + chr(x&255)).decode('utf8')
return b64encode(chr((x>>24)&255) + chr((x>>16)&255) + chr((x>>8)&255)
+ chr(x&255)).decode('utf8')
def llList2CSV(lst):
lst = fl(lst)
@ -1463,7 +1452,7 @@ def llListStatistics(op, lst):
if op == 8: # LIST_STAT_NUM_COUNT
return float(len(nums))
if op in (0, 1, 2) : # LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX
if op in (0, 1, 2): # LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX
min = None
for elem in nums:
if min is None:
@ -1551,12 +1540,9 @@ def llModPow(base, exp, mod):
if exp == 0:
return 1
# Convert all numbers to unsigned
if base < 0:
base += 4294967296
if exp < 0:
exp += 4294967296
if mod < 0:
mod += 4294967296
base &= 0xFFFFFFFF
exp &= 0xFFFFFFFF
mod &= 0xFFFFFFFF
prod = base
ret = 1
while True: