2019-01-01 22:44:54 +01:00
|
|
|
# (C) Copyright 2015-2019 Sei Lisa. All rights reserved.
|
2015-03-05 23:18:41 +01:00
|
|
|
#
|
|
|
|
|
# This file is part of LSL PyOptimizer.
|
|
|
|
|
#
|
|
|
|
|
# LSL PyOptimizer is free software: you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# LSL PyOptimizer is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with LSL PyOptimizer. If not, see <http://www.gnu.org/licenses/>.
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2015-03-05 23:18:41 +01:00
|
|
|
# This module is used by the optimizer for resolving constant values.
|
|
|
|
|
#
|
|
|
|
|
# The functions it implements are all functions that always return the same
|
|
|
|
|
# result when given the same input, and that have no side effects.
|
|
|
|
|
#
|
2016-12-22 01:05:21 +01:00
|
|
|
# For example, llAbs() is here, but llGetPos() is not, because it doesn't
|
|
|
|
|
# always return the same result.
|
2015-03-05 23:18:41 +01:00
|
|
|
#
|
|
|
|
|
# This implies that functions present in this module can be precomputed if
|
|
|
|
|
# their arguments are constants.
|
|
|
|
|
#
|
|
|
|
|
# In some instances, the result can't be computed; in these cases the function
|
|
|
|
|
# raises a LSLCantCompute exception that is caught by the optimizer to leave
|
|
|
|
|
# the expression unchanged. For example, llBase64ToInteger("AA") returns
|
|
|
|
|
# unpredictable garbage in the low bytes in LSL, so it is left unchanged.
|
|
|
|
|
#
|
|
|
|
|
# The JSON functions have been separated to their own module.
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
from lslcommon import *
|
|
|
|
|
import lslcommon
|
|
|
|
|
from ctypes import c_float
|
|
|
|
|
import math
|
|
|
|
|
import hashlib
|
|
|
|
|
from base64 import b64encode, b64decode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Regular expressions used along the code. They are needed mainly because
|
|
|
|
|
# Python lacks a C-like strtod/strtol (it comes close, but it is very picky
|
|
|
|
|
# with what it accepts). We need to extract the number part of a string, or
|
|
|
|
|
# Python will complain.
|
|
|
|
|
# Also, Base64 needs the correct count of characters (len mod 4 can't be = 1).
|
|
|
|
|
# The RE helps both in isolating the Base64 section and in trimming out the
|
|
|
|
|
# offending characters; it just doesn't help with padding, with which Python is
|
2017-01-19 03:31:02 +01:00
|
|
|
# also picky. We deal with that in the code by padding with '='*(-length & 3).
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Despite what http://www.gnu.org/software/libc/manual/html_node/Parsing-of-Floats.html#Parsing-of-Floats
|
|
|
|
|
# says, NaN(chars) does not work in LSL (which is relevant in vectors).
|
|
|
|
|
# Note infinity vs. inf is necessary for parsing vectors & rotations,
|
|
|
|
|
# e.g. (vector)"<1,inf,infix>" is not valid but (vector)"<1,inf,infinity>" is
|
|
|
|
|
# as is (vector)"<1,inf,info>". The 1st gives <0,0,0>, the others <1,inf,inf>.
|
|
|
|
|
# The lookahead (?!i) is essential for parsing them that way without extra code.
|
|
|
|
|
# Note that '|' in REs is order-sensitive.
|
|
|
|
|
float_re = re.compile(ur'^\s*[+-]?(?:0(x)(?:[0-9a-f]+(?:\.[0-9a-f]*)?|\.[0-9a-f]+)(?:p[+-]?[0-9]+)?'
|
2017-02-12 06:05:57 +01:00
|
|
|
ur'|(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:e[+-]?[0-9]+)?|inf|(nan))',
|
2015-02-28 17:34:26 +01:00
|
|
|
re.I)
|
|
|
|
|
vfloat_re = re.compile(ur'^\s*[+-]?(?:0(x)(?:[0-9a-f]+(?:\.[0-9a-f]*)?|\.[0-9a-f]+)(?:p[+-]?[0-9]+)?'
|
2017-02-12 06:05:57 +01:00
|
|
|
ur'|(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:e[+-]?[0-9]+)?|infinity|inf(?!i)|(nan))',
|
2014-07-26 02:43:44 +02:00
|
|
|
re.I)
|
|
|
|
|
|
|
|
|
|
int_re = re.compile(ur'^0(x)[0-9a-f]+|^\s*[+-]?[0-9]+', re.I)
|
|
|
|
|
|
|
|
|
|
key_re = re.compile(ur'^[0-9a-f]{8}(?:-[0-9a-f]{4}){4}[0-9a-f]{8}$', re.I)
|
|
|
|
|
|
|
|
|
|
b64_re = re.compile(ur'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2,3})?')
|
|
|
|
|
|
|
|
|
|
ZERO_VECTOR = Vector((0.0, 0.0, 0.0))
|
|
|
|
|
ZERO_ROTATION = Quaternion((0.0, 0.0, 0.0, 1.0))
|
2014-07-26 23:50:49 +02:00
|
|
|
NULL_KEY = u'00000000-0000-0000-0000-000000000000'
|
2017-08-30 19:23:38 +02:00
|
|
|
TOUCH_INVALID_TEXCOORD = Vector((-1.0, -1.0, 0.0))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
Infinity = float('inf')
|
2015-09-07 02:56:44 +02:00
|
|
|
Indet = Infinity * 0
|
2017-10-18 13:45:46 +02:00
|
|
|
NaN = -Indet # Don't use float("nan") - Windows gets upset.
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
class ELSLTypeMismatch(Exception):
|
|
|
|
|
def __init__(self):
|
2016-12-20 19:22:48 +01:00
|
|
|
super(ELSLTypeMismatch, self).__init__(u"Type mismatch")
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
class ELSLMathError(Exception):
|
|
|
|
|
def __init__(self):
|
2016-12-20 19:22:48 +01:00
|
|
|
super(ELSLMathError, self).__init__(u"Math Error")
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
class ELSLInvalidType(Exception):
|
|
|
|
|
def __init__(self):
|
2016-12-20 19:22:48 +01:00
|
|
|
super(ELSLInvalidType, self).__init__(u"Internal error: Invalid type")
|
2015-02-11 05:43:13 +01:00
|
|
|
|
|
|
|
|
class ELSLCantCompute(Exception):
|
|
|
|
|
pass
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2017-01-22 19:45:14 +01:00
|
|
|
# We don't yet support the LSO string model (arbitrary zero-terminated byte
|
|
|
|
|
# sequences). This exception is triggered to report attempts at using it.
|
|
|
|
|
class ELSONotSupported(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
# LSL types are translated to Python types as follows:
|
|
|
|
|
# * LSL string -> Python unicode
|
|
|
|
|
# * LSL key -> Key (class derived from unicode, no significant changes except __repr__)
|
|
|
|
|
# * LSL integer -> Python int (should never be long)
|
|
|
|
|
# * LSL float -> Python float
|
|
|
|
|
# * LSL vector -> Vector (class derived from Python tuple) of 3 numbers (float)
|
|
|
|
|
# * LSL rotation -> Quaternion (class derived from Python tuple) of 4 numbers (float)
|
|
|
|
|
# * LSL list -> Python list
|
|
|
|
|
|
|
|
|
|
Types = {
|
2017-10-18 13:45:46 +02:00
|
|
|
int: 1, # TYPE_INTEGER
|
|
|
|
|
float: 2, # TYPE_FLOAT
|
|
|
|
|
unicode: 3, # TYPE_STRING
|
|
|
|
|
Key: 4, # TYPE_KEY
|
|
|
|
|
Vector: 5, # TYPE_VECTOR
|
|
|
|
|
Quaternion: 6, # TYPE_ROTATION
|
|
|
|
|
list: 0, # TYPE_INVALID
|
2014-07-26 02:43:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Utility functions
|
|
|
|
|
|
|
|
|
|
def F32(f, f32=True):
|
|
|
|
|
"""Truncate a float to have a precision equivalent to IEEE single"""
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if not f32: # don't truncate
|
2014-07-26 02:43:44 +02:00
|
|
|
return f
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if isinstance(f, tuple): # vector, quaternion
|
2014-07-26 02:43:44 +02:00
|
|
|
return f.__class__(F32(i) for i in f)
|
|
|
|
|
|
|
|
|
|
# Alternative to the big blurb below. This relies on the machine using IEEE-754, though.
|
|
|
|
|
|
|
|
|
|
# Using array:
|
|
|
|
|
#from array import array
|
|
|
|
|
#return array('f',(f,))[0]
|
|
|
|
|
|
2015-03-05 23:18:41 +01:00
|
|
|
# Using struct:
|
|
|
|
|
#from struct import pack, unpack
|
|
|
|
|
#return unpack('f', pack('f', f))[0]
|
|
|
|
|
|
2016-05-21 01:59:05 +02:00
|
|
|
# Using numpy:
|
|
|
|
|
#import numpy
|
|
|
|
|
#return float(numpy.float32(f))
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
# Using ctypes:
|
|
|
|
|
#from ctypes import c_float
|
|
|
|
|
return c_float(f).value
|
|
|
|
|
|
2015-03-05 23:18:41 +01:00
|
|
|
# These are other approaches that are not fully debugged:
|
|
|
|
|
|
2016-05-06 20:11:41 +02:00
|
|
|
# This one is tested against c_float, but not carefully verified:
|
|
|
|
|
# if math.isnan(f) or math.isinf(f) or f == 0.0:
|
|
|
|
|
# return f
|
|
|
|
|
#
|
|
|
|
|
# m, x = math.frexp(abs(f))
|
|
|
|
|
#
|
|
|
|
|
# if x > 128:
|
|
|
|
|
# return math.copysign(Infinity, f)
|
|
|
|
|
#
|
|
|
|
|
# if x < -125:
|
|
|
|
|
# m = math.ldexp(m, x + 149)
|
|
|
|
|
# x = -125
|
|
|
|
|
# else:
|
|
|
|
|
# m = m * 0x1000000
|
|
|
|
|
#
|
|
|
|
|
# frac = m % 1
|
|
|
|
|
# m -= frac
|
|
|
|
|
# assert m.is_integer()
|
|
|
|
|
# m = int(m)
|
|
|
|
|
#
|
|
|
|
|
# # Round to even
|
|
|
|
|
# if frac > 0.5 or frac == 0.5 and (m & 1):
|
|
|
|
|
# m += 1
|
|
|
|
|
# if m == 0x1000000:
|
|
|
|
|
# m = 0x800000
|
|
|
|
|
# x += 1
|
|
|
|
|
#
|
|
|
|
|
# # re-check for overflow
|
|
|
|
|
# if x > 128:
|
|
|
|
|
# return math.copysign(Infinity, f)
|
|
|
|
|
#
|
|
|
|
|
# if m == 0:
|
|
|
|
|
# return math.copysign(0.0, f)
|
|
|
|
|
#
|
|
|
|
|
# return math.ldexp(math.copysign(m/16777216.0, f), x)
|
|
|
|
|
|
|
|
|
|
# # Another alternative.
|
2014-07-26 02:43:44 +02:00
|
|
|
# m, x = math.frexp(abs(f))
|
|
|
|
|
# if x > 128:
|
|
|
|
|
# return math.copysign(Infinity, f)
|
|
|
|
|
# if x < -149:
|
|
|
|
|
# return math.copysign(0.0, f)
|
|
|
|
|
# if x < -125:
|
|
|
|
|
# e = 1<<(x+149)
|
|
|
|
|
# else:
|
|
|
|
|
# e = 16777216.0
|
|
|
|
|
# # Special corner case with rounding near the maximum float (e.g. 3.4028236e38 gets rounded up, going out of range for a F32)
|
|
|
|
|
# if m*e >= 16777215.5 and x == 128:
|
|
|
|
|
# return math.copysign(Infinity, f)
|
|
|
|
|
# return math.ldexp(math.copysign(math.floor(m*e+0.5)/e, f), x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # Original old-fashioned strategy (watch out for the 16777215.5 bug above):
|
|
|
|
|
#
|
|
|
|
|
# if math.isinf(f) or math.isnan(f) or f==0:
|
|
|
|
|
# return f
|
|
|
|
|
# s = math.copysign(1, f)
|
|
|
|
|
# # This number may not be precise enough if Python had infinite precision, but it works for us.
|
|
|
|
|
# if f < 0.0000000000000000000000000000000000000000000007006492321624086132496:
|
|
|
|
|
# return math.copysign(0.0, s)
|
|
|
|
|
# f = abs(f)
|
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
# # TO DO: Check this boundary (this is 2^128)
|
|
|
|
|
# if f >= 340282366920938463463374607431768211456.0:
|
|
|
|
|
# return math.copysign(Infinity, s)
|
|
|
|
|
#
|
|
|
|
|
# # TO DO: Check this boundary (2^-126; hopefully there's some overlap and the precision can be cut)
|
|
|
|
|
# if f < 0.000000000000000000000000000000000000011754943508222875079687365372222456778186655567720875215087517062784172594547271728515625:
|
|
|
|
|
# # Denormal range
|
|
|
|
|
# f *= 713623846352979940529142984724747568191373312.0
|
2017-10-18 13:45:46 +02:00
|
|
|
# e = 0.00000000000000000000000000000000000000000000140129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125 # 2^-149
|
2014-07-26 02:43:44 +02:00
|
|
|
# else:
|
|
|
|
|
# e = 1.0
|
|
|
|
|
# # This first loop is an optimization to get closer to the destination faster for very small numbers
|
|
|
|
|
# while f < 1.0:
|
|
|
|
|
# f *= 16777216.0
|
|
|
|
|
# e *= 0.000000059604644775390625
|
|
|
|
|
# # Go bit by bit
|
|
|
|
|
# while f < 8388608.0:
|
|
|
|
|
# f *= 2.0
|
|
|
|
|
# e *= 0.5
|
|
|
|
|
#
|
|
|
|
|
# #This first loop is an optimization to get closer to the destination faster for very big numbers
|
|
|
|
|
# while f >= 140737488355328.0:
|
|
|
|
|
# f *= 0.000000059604644775390625
|
|
|
|
|
# e *= 16777216.0
|
|
|
|
|
# # Go bit by bit
|
|
|
|
|
# while f >= 16777216.0:
|
|
|
|
|
# f *= 0.5
|
|
|
|
|
# e *= 2.0
|
|
|
|
|
#
|
|
|
|
|
# return math.copysign(math.floor(f+0.5)*e, s)
|
|
|
|
|
|
|
|
|
|
def S32(val):
|
|
|
|
|
"""Return a signed integer truncated to 32 bits (must deal with longs too)"""
|
|
|
|
|
if -2147483648 <= val <= 2147483647:
|
|
|
|
|
return int(val)
|
|
|
|
|
val &= 0xFFFFFFFF
|
|
|
|
|
if val > 2147483647:
|
|
|
|
|
return int(val - 4294967296)
|
|
|
|
|
return int(val)
|
|
|
|
|
|
|
|
|
|
def zstr(s):
|
|
|
|
|
if not isinstance(s, unicode):
|
2017-10-14 11:33:18 +02:00
|
|
|
# This can only be the result of an internal error; call attention to
|
|
|
|
|
# it by raising ELSLInvalidType instead of ELSLTypeMismatch.
|
2014-07-26 02:43:44 +02:00
|
|
|
raise ELSLInvalidType
|
|
|
|
|
|
|
|
|
|
zi = s.find(u'\0')
|
|
|
|
|
if zi < 0:
|
|
|
|
|
return s
|
|
|
|
|
return s.__class__(s[:zi])
|
|
|
|
|
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
def fi(x):
|
|
|
|
|
"""Force x to be an int"""
|
|
|
|
|
if type(x) != int or not (-2147483648 <= x <= 2147483647):
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
return x
|
|
|
|
|
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
def ff(x):
|
|
|
|
|
"""Force x to be a float"""
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
if int != type(x) != float:
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
2016-05-21 03:10:31 +02:00
|
|
|
if type(x) != float:
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
return InternalTypecast(x, float, False, True)
|
2016-12-21 06:01:03 +01:00
|
|
|
return F32(x)
|
|
|
|
|
|
|
|
|
|
def fk(k):
|
|
|
|
|
"""Force k to be a key"""
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
if unicode != type(k) != Key:
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
2016-12-21 06:01:03 +01:00
|
|
|
if type(k) != Key:
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
k = InternalTypecast(k, Key, False, False)
|
2016-12-21 06:01:03 +01:00
|
|
|
return k
|
|
|
|
|
|
|
|
|
|
def fs(s):
|
|
|
|
|
"""Force s to be a string"""
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
if unicode != type(s) != Key:
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
2016-12-21 06:01:03 +01:00
|
|
|
if type(s) != unicode:
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = InternalTypecast(s, unicode, False, False)
|
2016-12-21 06:01:03 +01:00
|
|
|
return s
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
def fl(L):
|
|
|
|
|
"""Force l to be a list, and its elements to have sane types."""
|
|
|
|
|
Lorig = L
|
|
|
|
|
if type(L) != list:
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
for i in xrange(len(L)):
|
|
|
|
|
t = type(L[i])
|
|
|
|
|
if t not in Types:
|
|
|
|
|
raise ELSLInvalidType
|
|
|
|
|
if t == Vector:
|
|
|
|
|
# copy on write
|
|
|
|
|
if L is Lorig:
|
|
|
|
|
L = L[:]
|
|
|
|
|
L[i] = v2f(L[i])
|
|
|
|
|
if t == Quaternion:
|
|
|
|
|
# copy on write
|
|
|
|
|
if L is Lorig:
|
|
|
|
|
L = L[:]
|
|
|
|
|
L[i] = q2f(L[i])
|
|
|
|
|
return L
|
|
|
|
|
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
def q2f(q):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
if type(q) != Quaternion:
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
if type(q[0]) == type(q[1]) == type(q[2]) == type(q[3]) == float:
|
|
|
|
|
return q
|
|
|
|
|
return Quaternion((ff(q[0]), ff(q[1]), ff(q[2]), ff(q[3])))
|
|
|
|
|
|
|
|
|
|
def v2f(v):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
if type(v) != Vector:
|
2017-10-14 11:33:18 +02:00
|
|
|
raise ELSLTypeMismatch
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
if type(v[0]) == type(v[1]) == type(v[2]) == float:
|
|
|
|
|
return v
|
|
|
|
|
return Vector((ff(v[0]), ff(v[1]), ff(v[2])))
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
def f2s(val, DP=6):
|
|
|
|
|
if math.isinf(val):
|
|
|
|
|
return u'Infinity' if val > 0 else u'-Infinity'
|
|
|
|
|
if math.isnan(val):
|
|
|
|
|
return u'NaN'
|
|
|
|
|
if lslcommon.LSO or val == 0.:
|
2017-10-18 13:45:46 +02:00
|
|
|
return u'%.*f' % (DP, val) # deals with -0.0 too
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Format according to Mono rules (7 decimals after the DP, found experimentally)
|
|
|
|
|
s = u'%.*f' % (DP+7, val)
|
|
|
|
|
|
|
|
|
|
if s[:DP+3] == u'-0.' + '0'*DP and s[DP+3] < u'5':
|
2017-10-18 13:45:46 +02:00
|
|
|
return u'0.' + '0'*DP # underflown negatives return 0.0 except for -0.0 dealt with above
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Separate the sign
|
|
|
|
|
sgn = u'-' if s[0] == u'-' else u''
|
|
|
|
|
if sgn: s = s[1:]
|
|
|
|
|
|
|
|
|
|
# Look for position of first nonzero from the left
|
|
|
|
|
i = 0
|
|
|
|
|
while s[i] in u'0.':
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
dot = s.index(u'.')
|
|
|
|
|
|
|
|
|
|
# Find rounding point. It's either the 7th digit after the first significant one,
|
|
|
|
|
# or the (DP+1)-th decimal after the period, whichever comes first.
|
|
|
|
|
digits = 0
|
|
|
|
|
while digits < 7:
|
|
|
|
|
if i >= dot+1+DP:
|
|
|
|
|
break
|
|
|
|
|
if i == dot:
|
|
|
|
|
i += 1
|
|
|
|
|
i += 1
|
|
|
|
|
digits += 1
|
|
|
|
|
|
2015-09-24 22:02:46 +02:00
|
|
|
if s[i if i != dot else i+1] >= u'5':
|
|
|
|
|
# Rounding - increment s[:i] storing result into new_s
|
2014-07-26 02:43:44 +02:00
|
|
|
new_s = u''
|
2017-10-18 13:45:46 +02:00
|
|
|
ci = i-1 # carry index
|
2014-07-26 02:43:44 +02:00
|
|
|
while ci >= 0 and s[ci] == u'9':
|
|
|
|
|
new_s = u'0' + new_s
|
|
|
|
|
ci -= 1
|
|
|
|
|
if ci == dot:
|
2017-10-18 13:45:46 +02:00
|
|
|
ci -= 1 # skip over the dot
|
|
|
|
|
new_s = u'.' + new_s # but add it to new_s
|
2014-07-26 02:43:44 +02:00
|
|
|
if ci < 0:
|
2017-10-18 13:45:46 +02:00
|
|
|
new_s = u'1' + new_s # 9...9 -> 10...0
|
2014-07-26 02:43:44 +02:00
|
|
|
else:
|
|
|
|
|
# increment s[ci] e.g. 43999 -> 44000
|
2017-10-18 13:45:46 +02:00
|
|
|
new_s = s[:ci] + chr(ord(s[ci]) + 1) + new_s
|
2014-07-26 02:43:44 +02:00
|
|
|
else:
|
|
|
|
|
new_s = s[:i]
|
|
|
|
|
|
|
|
|
|
if i <= dot:
|
2017-10-18 13:45:46 +02:00
|
|
|
return sgn + new_s + u'0' * (dot - i) + u'.' + u'0' * DP
|
|
|
|
|
return sgn + new_s + u'0' * (dot + 1 + DP - i)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def vr2s(v, DP=6):
|
2017-10-18 13:45:46 +02:00
|
|
|
assert len(v) == (3 if type(v) == Vector else 4)
|
|
|
|
|
return u'<' + ', '.join(f2s(x, DP) for x in v) + u'>'
|
2017-01-23 00:08:24 +01:00
|
|
|
|
|
|
|
|
def qnz(q):
|
2017-10-18 13:45:46 +02:00
|
|
|
return Quaternion((0.,0.,0.,1.)) if all(x == 0. for x in q) else q
|
2017-01-23 00:08:24 +01:00
|
|
|
|
2017-01-23 18:55:30 +01:00
|
|
|
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
|
2017-10-18 13:45:46 +02:00
|
|
|
eps_h = 1.0000021457672119140625 # float.fromhex('0x1.000024p0')
|
|
|
|
|
eps_l = 0.99999797344207763671875 # float.fromhex('0x1.FFFFBCp-1')
|
2017-01-23 18:55:30 +01:00
|
|
|
if mag2 >= eps_h or mag2 <= eps_l:
|
|
|
|
|
# Renormalize
|
|
|
|
|
mag2 = math.sqrt(mag2)
|
|
|
|
|
return Quaternion((q[0]/mag2, q[1]/mag2, q[2]/mag2, q[3]/mag2))
|
|
|
|
|
return q
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
def InternalTypecast(val, out, InList, f32):
|
|
|
|
|
"""Type cast val to out, following LSL rules.
|
|
|
|
|
|
|
|
|
|
To avoid mutual recursion, it deals with everything except lists. That way
|
|
|
|
|
it does not need to call InternalList2Strings which needs to call it.
|
|
|
|
|
"""
|
|
|
|
|
tval = type(val)
|
|
|
|
|
# The case tval == list is handled in typecast() below.
|
|
|
|
|
if out == list:
|
|
|
|
|
return [val]
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if tval == int: # integer
|
2014-07-26 02:43:44 +02:00
|
|
|
val = S32(val)
|
|
|
|
|
if out == int: return val
|
|
|
|
|
if out == float: return F32(val, f32)
|
|
|
|
|
if out == unicode: return unicode(val)
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
if tval == float:
|
|
|
|
|
val = F32(val, f32)
|
|
|
|
|
if out == int: return S32(int(val)) if val >= -2147483648.0 and val < 2147483648.0 else -2147483648
|
|
|
|
|
if out == float: return val
|
|
|
|
|
if out == unicode: return f2s(val, 6)
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
if tval == Vector:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
val = v2f(val)
|
2014-07-26 02:43:44 +02:00
|
|
|
if out == Vector: return val
|
|
|
|
|
if out == unicode: return vr2s(val, 6 if InList else 5)
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
if tval == Quaternion:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
val = q2f(val)
|
2014-07-26 02:43:44 +02:00
|
|
|
if out == Quaternion: return val
|
|
|
|
|
if out == unicode: return vr2s(val, 6 if InList else 5)
|
|
|
|
|
raise ELSLTypeMismatch
|
2017-10-18 13:45:46 +02:00
|
|
|
if tval == Key: # key
|
2014-07-26 02:43:44 +02:00
|
|
|
if out == Key: return zstr(val)
|
|
|
|
|
if out == unicode: return zstr(unicode(val))
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
if tval == unicode:
|
|
|
|
|
val = zstr(val)
|
|
|
|
|
if out == unicode: return val
|
|
|
|
|
if out == Key: return Key(val)
|
|
|
|
|
if out == float:
|
|
|
|
|
# Clean up the string for Picky Python
|
2016-06-26 22:33:10 +02:00
|
|
|
match = float_re.search(val)
|
2014-07-26 02:43:44 +02:00
|
|
|
if match is None:
|
|
|
|
|
return 0.0
|
|
|
|
|
if match.group(1):
|
2018-04-27 23:44:27 +02:00
|
|
|
ret = float.fromhex(match.group(0))
|
2017-02-12 06:05:57 +01:00
|
|
|
elif match.group(2):
|
|
|
|
|
# (float)"-nan" produces NaN instead of Indet, even though
|
|
|
|
|
# (vector)"<-nan,0,0>" produces <Indet, 0., 0.>. Go figure.
|
|
|
|
|
ret = NaN
|
2015-09-21 21:15:48 +02:00
|
|
|
else:
|
2018-04-27 23:44:27 +02:00
|
|
|
ret = float(match.group(0))
|
2018-05-09 19:04:09 +02:00
|
|
|
if not lslcommon.LSO and abs(ret) < 1.1754943157898259e-38:
|
2015-09-21 21:15:48 +02:00
|
|
|
# Mono doesn't return denormals when using (float)"val"
|
|
|
|
|
# (but it returns them when using (vector)"<val,...>")
|
2015-09-22 14:14:26 +02:00
|
|
|
ret = 0.0
|
2018-04-27 23:44:27 +02:00
|
|
|
return F32(ret, f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
if out == int:
|
2016-06-26 22:33:10 +02:00
|
|
|
match = int_re.search(val)
|
2014-07-26 02:43:44 +02:00
|
|
|
if match is None:
|
|
|
|
|
return 0
|
|
|
|
|
val = match.group(0)
|
|
|
|
|
if match.group(1):
|
|
|
|
|
val = int(val, 0)
|
|
|
|
|
else:
|
|
|
|
|
val = int(val)
|
|
|
|
|
if -4294967295 <= val <= 4294967295:
|
|
|
|
|
return S32(val)
|
|
|
|
|
return -1
|
|
|
|
|
if out in (Vector, Quaternion):
|
|
|
|
|
Z,dim = (ZERO_VECTOR,3) if out == Vector else (ZERO_ROTATION,4)
|
|
|
|
|
ret = []
|
|
|
|
|
if val[0:1] != u'<':
|
|
|
|
|
return Z
|
|
|
|
|
val = val[1:]
|
|
|
|
|
for _ in range(dim):
|
2016-06-26 22:33:10 +02:00
|
|
|
match = vfloat_re.search(val)
|
2014-07-26 02:43:44 +02:00
|
|
|
if match is None:
|
|
|
|
|
return Z
|
|
|
|
|
if match.group(1):
|
|
|
|
|
ret.append(F32(float.fromhex(match.group(0)), f32))
|
2017-02-12 06:05:57 +01:00
|
|
|
elif match.group(2):
|
|
|
|
|
ret.append(Indet if match.group(0)[0] == '-' else NaN)
|
2014-07-26 02:43:44 +02:00
|
|
|
else:
|
|
|
|
|
ret.append(F32(float(match.group(0)), f32))
|
|
|
|
|
if len(ret) < dim:
|
|
|
|
|
i = match.end()
|
|
|
|
|
if val[i:i+1] != u',':
|
|
|
|
|
return Z
|
|
|
|
|
val = val[i+1:]
|
2017-10-18 13:45:46 +02:00
|
|
|
return out(ret) # convert type
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# To avoid mutual recursion, this was moved:
|
2017-10-18 13:45:46 +02:00
|
|
|
#if tval == list: # etc.
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
raise ELSLInvalidType
|
|
|
|
|
|
|
|
|
|
def InternalList2Strings(val):
|
|
|
|
|
"""Convert a list of misc.items to a list of strings."""
|
|
|
|
|
ret = []
|
|
|
|
|
for elem in val:
|
|
|
|
|
ret.append(InternalTypecast(elem, unicode, InList=True, f32=True))
|
|
|
|
|
return ret
|
|
|
|
|
|
2014-07-26 21:06:53 +02:00
|
|
|
def InternalUTF8toString(s):
|
|
|
|
|
# Note Mono and LSO behave differently here.
|
|
|
|
|
# LSO *CAN* store invalid UTF-8.
|
|
|
|
|
# For example, llEscapeURL(llUnescapeURL("%80%C3")) gives "%80%C3" in LSO.
|
|
|
|
|
# (But llEscapeURL(llUnescapeURL("%80%00%C3")) still gives "%80")
|
|
|
|
|
# We don't emulate it, we've built this with Unicode strings in mind.
|
|
|
|
|
|
|
|
|
|
# decode(..., 'replace') replaces invalid chars with U+FFFD which is not
|
|
|
|
|
# what LSL does (LSL replaces with '?'). Since U+FFFD must be preserved if
|
|
|
|
|
# present, we need to write our own algorithm.
|
|
|
|
|
|
|
|
|
|
# Problem: Aliases are not valid UTF-8 for LSL, and code points above
|
|
|
|
|
# U+10FFFF are not supported. Both things complicate the alg a bit.
|
|
|
|
|
|
|
|
|
|
ret = u''
|
|
|
|
|
partialchar = b''
|
|
|
|
|
pending = 0
|
|
|
|
|
for c in s:
|
|
|
|
|
o = ord(c)
|
|
|
|
|
if partialchar:
|
|
|
|
|
if 0x80 <= o < 0xC0 and (
|
|
|
|
|
partialchar[1:2]
|
2016-12-13 03:10:01 +01:00
|
|
|
or b'\xC2' <= partialchar < b'\xF4' and partialchar not in b'\xE0\xED\xF0'
|
2014-07-26 21:06:53 +02:00
|
|
|
or partialchar == b'\xE0' and o >= 0xA0
|
2016-12-13 03:10:01 +01:00
|
|
|
or partialchar == b'\xED' and o < 0xA0
|
2014-07-26 21:06:53 +02:00
|
|
|
or partialchar == b'\xF0' and o >= 0x90
|
|
|
|
|
or partialchar == b'\xF4' and o < 0x90
|
|
|
|
|
):
|
|
|
|
|
partialchar += c
|
|
|
|
|
pending -= 1
|
|
|
|
|
if pending == 0:
|
|
|
|
|
ret += partialchar.decode('utf8')
|
|
|
|
|
partialchar = b''
|
|
|
|
|
c = c
|
|
|
|
|
# NOTE: Without the above line, the following one hits a bug in
|
|
|
|
|
# python-coverage. It IS executed but not detected.
|
|
|
|
|
continue
|
2016-12-21 00:22:49 +01:00
|
|
|
if lslcommon.LSO:
|
2016-12-20 19:22:48 +01:00
|
|
|
raise ELSONotSupported(u"Byte strings not supported")
|
2014-07-26 21:06:53 +02:00
|
|
|
ret += u'?' * len(partialchar)
|
|
|
|
|
partialchar = b''
|
|
|
|
|
# fall through to process current character
|
|
|
|
|
if o >= 0xC2 and o <= 0xF4:
|
|
|
|
|
partialchar = c
|
|
|
|
|
pending = 1 if o < 0xE0 else 2 if o < 0xF0 else 3
|
|
|
|
|
elif o >= 0x80:
|
2016-12-21 00:22:49 +01:00
|
|
|
if lslcommon.LSO:
|
2016-12-20 19:22:48 +01:00
|
|
|
raise ELSONotSupported(u"Byte strings not supported")
|
2014-07-26 21:06:53 +02:00
|
|
|
ret += u'?'
|
|
|
|
|
else:
|
|
|
|
|
ret += c.decode('utf8')
|
|
|
|
|
|
|
|
|
|
if partialchar:
|
2016-12-21 00:22:49 +01:00
|
|
|
if lslcommon.LSO:
|
2016-12-20 19:22:48 +01:00
|
|
|
raise ELSONotSupported(u"Byte strings not supported")
|
2014-07-26 21:06:53 +02:00
|
|
|
ret += u'?' * len(partialchar)
|
|
|
|
|
|
|
|
|
|
return zstr(ret)
|
|
|
|
|
|
2017-01-22 07:43:02 +01:00
|
|
|
# The code of llDeleteSubList and llDeleteSubString is identical except for the
|
|
|
|
|
# type check. Same for llGetSubString and llList2List. They are all joined into
|
|
|
|
|
# one single function.
|
|
|
|
|
def InternalGetDeleteSubSequence(val, start, end, isGet):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
start = fi(start)
|
|
|
|
|
end = fi(end)
|
2014-07-26 21:06:53 +02:00
|
|
|
L = len(val)
|
|
|
|
|
|
|
|
|
|
# Python does much of the same thing as LSL here, which helps a lot
|
|
|
|
|
if end == -1: end += L
|
2017-01-22 07:43:02 +01:00
|
|
|
if (start+L if start < 0 else start) > (end+L if end < 0 else end):
|
|
|
|
|
# Exclusion range - get/delete from end and start
|
2017-10-18 13:45:46 +02:00
|
|
|
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:]
|
2014-07-26 21:06:53 +02:00
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
def typecast(val, out, InList=False, f32=True):
|
|
|
|
|
"""Type cast an item. Calls InternalList2Strings for lists and
|
|
|
|
|
defers the rest to InternalTypecast.
|
|
|
|
|
"""
|
|
|
|
|
if type(val) == list:
|
|
|
|
|
if out == list:
|
2017-10-18 13:45:46 +02:00
|
|
|
return val # NOTE: We're not duplicating it here.
|
2014-07-26 02:43:44 +02:00
|
|
|
if out == unicode:
|
|
|
|
|
return u''.join(InternalList2Strings(val))
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
return InternalTypecast(val, out, InList, f32)
|
|
|
|
|
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
def neg(val):
|
2014-07-26 02:43:44 +02:00
|
|
|
if type(val) in (int, float):
|
|
|
|
|
if type(val) == int and val == -2147483648:
|
|
|
|
|
return val
|
|
|
|
|
return -val
|
|
|
|
|
if isinstance(val, tuple):
|
|
|
|
|
return val.__class__(-f for f in val)
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
def add(a, b, f32=True):
|
|
|
|
|
# defined for:
|
|
|
|
|
# scalar+scalar
|
|
|
|
|
# vector+vector
|
|
|
|
|
# rotation+rotation
|
|
|
|
|
# string+string
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# (our extension:) key+string, string+key
|
2014-07-26 02:43:44 +02:00
|
|
|
# list+any
|
|
|
|
|
# any+list
|
|
|
|
|
ta=type(a)
|
|
|
|
|
tb=type(b)
|
|
|
|
|
if ta in (int, float) and tb in (int, float):
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
if ta == tb == int:
|
2014-07-26 02:43:44 +02:00
|
|
|
return S32(a+b)
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(ff(a)+ff(b), f32)
|
|
|
|
|
|
|
|
|
|
if ta == tb in (list, unicode):
|
|
|
|
|
return a + b
|
|
|
|
|
# string + key, key + string are allowed here
|
2014-07-31 19:18:26 +02:00
|
|
|
if ta in (unicode, Key) and tb in (unicode, Key) and not (ta == tb == Key):
|
2014-07-26 02:43:44 +02:00
|
|
|
return a + b
|
|
|
|
|
if ta == list:
|
|
|
|
|
return a + [b]
|
|
|
|
|
if tb == list:
|
|
|
|
|
return [a] + b
|
|
|
|
|
if ta == tb in (Vector, Quaternion):
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(ta(ff(a[i])+ff(b[i]) for i in range(len(a))), f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
def sub(a, b, f32=True):
|
|
|
|
|
# defined for:
|
|
|
|
|
# scalar+scalar
|
|
|
|
|
# vector+vector
|
|
|
|
|
# rotation+rotation
|
|
|
|
|
ta=type(a)
|
|
|
|
|
tb=type(b)
|
|
|
|
|
if ta in (int, float) and tb in (int, float):
|
|
|
|
|
if ta == tb == int:
|
|
|
|
|
return S32(a-b)
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(ff(a)-ff(b), f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
if ta == tb in (Vector, Quaternion):
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(ta(ff(a[i])-ff(b[i]) for i in range(len(a))), f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
def mul(a, b, f32=True):
|
|
|
|
|
# defined for:
|
|
|
|
|
# scalar*scalar
|
|
|
|
|
# scalar*vector
|
|
|
|
|
# vector*scalar
|
|
|
|
|
# vector*vector
|
|
|
|
|
# vector*rotation
|
|
|
|
|
# rotation*rotation
|
|
|
|
|
ta = type(a)
|
|
|
|
|
tb = type(b)
|
|
|
|
|
# If either type is string, list, or key, error
|
|
|
|
|
if ta in (unicode, list, Key) or tb in (unicode, list, Key):
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
# only int, float, vector, quaternion here
|
|
|
|
|
if ta in (int, float):
|
|
|
|
|
if tb in (int, float):
|
|
|
|
|
if ta == tb == int:
|
|
|
|
|
return S32(a*b)
|
2017-10-18 13:45:46 +02:00
|
|
|
if math.isnan(a) and math.isnan(b):
|
|
|
|
|
return (-NaN
|
|
|
|
|
if math.copysign(1, a) == math.copysign(1, b) == -1
|
|
|
|
|
else NaN)
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(ff(a)*ff(b), f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
if tb != Vector:
|
|
|
|
|
# scalar * quat is not defined
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
# scalar * vector
|
2017-10-14 15:16:18 +02:00
|
|
|
a, ta, b, tb = b, tb, a, ta # turn into vector * scalar
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
if ta == Quaternion:
|
|
|
|
|
# quat * scalar and quat * vector are not defined
|
|
|
|
|
if tb != Quaternion:
|
|
|
|
|
raise ELSLTypeMismatch
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = q2f(a)
|
|
|
|
|
b = q2f(b)
|
2014-07-26 02:43:44 +02:00
|
|
|
# quaternion product - product formula reversed
|
|
|
|
|
return Quaternion(F32((a[0] * b[3] + a[3] * b[0] + a[2] * b[1] - a[1] * b[2],
|
|
|
|
|
a[1] * b[3] - a[2] * b[0] + a[3] * b[1] + a[0] * b[2],
|
|
|
|
|
a[2] * b[3] + a[1] * b[0] - a[0] * b[1] + a[3] * b[2],
|
|
|
|
|
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]), f32))
|
|
|
|
|
|
|
|
|
|
if ta != Vector:
|
2017-10-18 13:45:46 +02:00
|
|
|
raise ELSLInvalidType # Should never happen at this point
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
if tb in (int, float):
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = v2f(a)
|
|
|
|
|
b = ff(b)
|
2017-10-14 15:16:18 +02:00
|
|
|
return Vector(F32((mul(a[0], b), mul(a[1], b), mul(a[2], b)), f32))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
if tb == Vector:
|
|
|
|
|
# scalar product
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = v2f(a)
|
|
|
|
|
b = v2f(b)
|
2014-07-26 02:43:44 +02:00
|
|
|
return F32(math.fsum((a[0]*b[0], a[1]*b[1], a[2]*b[2])), f32)
|
|
|
|
|
|
|
|
|
|
if tb != Quaternion:
|
2017-10-18 13:45:46 +02:00
|
|
|
raise ELSLInvalidType # Should never happen at this point
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# vector * quaternion: perform conjugation
|
|
|
|
|
#v = mul(Quaternion((-b[0], -b[1], -b[2], b[3])), mul(Quaternion((a[0], a[1], a[2], 0.0)), b, f32=False))
|
|
|
|
|
#return Vector((v[0], v[1], v[2]))
|
|
|
|
|
# this is more precise as it goes directly to the gist of it:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = v2f(a)
|
|
|
|
|
b = q2f(b)
|
|
|
|
|
return Vector(F32((
|
|
|
|
|
math.fsum(( a[0]*(b[0]*b[0]-b[1]*b[1]-b[2]*b[2]+b[3]*b[3]),
|
|
|
|
|
a[1]*2*(b[0]*b[1]-b[2]*b[3]),
|
|
|
|
|
a[2]*2*(b[0]*b[2]+b[1]*b[3]))),
|
|
|
|
|
math.fsum(( a[0]*2*(b[0]*b[1]+b[2]*b[3]),
|
2017-10-18 13:45:46 +02:00
|
|
|
-a[1]*(b[0]*b[0]-b[1]*b[1]+b[2]*b[2]-b[3]*b[3]), # notice minus sign
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a[2]*2*(b[1]*b[2]-b[0]*b[3]))),
|
|
|
|
|
math.fsum(( a[0]*2*(b[0]*b[2]-b[1]*b[3]),
|
|
|
|
|
a[1]*2*(b[1]*b[2]+b[0]*b[3]),
|
2017-10-18 13:45:46 +02:00
|
|
|
-a[2]*(b[0]*b[0]+b[1]*b[1]-b[2]*b[2]-b[3]*b[3]))) # notice minus sign
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
), f32))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def div(a, b, f32=True):
|
|
|
|
|
# defined for:
|
|
|
|
|
# scalar/scalar
|
|
|
|
|
# vector/scalar
|
|
|
|
|
# vector/rotation
|
|
|
|
|
# rotation/rotation
|
|
|
|
|
ta = type(a)
|
|
|
|
|
tb = type(b)
|
|
|
|
|
if tb in (int, float):
|
|
|
|
|
if b == 0:
|
|
|
|
|
raise ELSLMathError
|
|
|
|
|
if ta in (int, float):
|
|
|
|
|
if ta == int and tb == int:
|
|
|
|
|
# special case
|
|
|
|
|
if a == -2147483648 and b == -1:
|
2017-10-18 13:45:46 +02:00
|
|
|
return a # this could be handled by using S32 but it's probably faster this way
|
2014-07-26 02:43:44 +02:00
|
|
|
if (a < 0) ^ (b < 0):
|
|
|
|
|
# signs differ - Python rounds towards -inf, we need rounding towards 0
|
2016-11-06 04:09:43 +01:00
|
|
|
return -(a//-b)
|
2014-07-26 02:43:44 +02:00
|
|
|
return a//b
|
2016-05-15 18:21:24 +02:00
|
|
|
ret = F32(ff(a)/ff(b), f32)
|
2017-10-18 13:45:46 +02:00
|
|
|
if math.isnan(ret): # A NaN result gives a math error.
|
2016-05-15 18:21:24 +02:00
|
|
|
raise ELSLMathError
|
|
|
|
|
return ret
|
2014-07-26 02:43:44 +02:00
|
|
|
if ta == Vector:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = v2f(a)
|
|
|
|
|
b = ff(b)
|
2017-10-14 15:16:18 +02:00
|
|
|
return Vector(F32(tuple(NaN if math.isnan(x) and math.isnan(b)
|
|
|
|
|
and math.copysign(1, x)
|
|
|
|
|
!= math.copysign(1, b)
|
|
|
|
|
else x/b
|
|
|
|
|
for x in a), f32))
|
|
|
|
|
if tb == Quaternion: # division by a rotation is multiplication by the conjugate of the rotation
|
2014-07-26 02:43:44 +02:00
|
|
|
# defer the remaining type checks to mul()
|
2015-02-11 05:43:13 +01:00
|
|
|
return mul(a, Quaternion((-b[0],-b[1],-b[2],b[3])), f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
|
|
|
|
def mod(a, b, f32=True):
|
|
|
|
|
# defined only for integers and vectors
|
|
|
|
|
if type(a) == type(b) == int:
|
2015-02-11 05:43:13 +01:00
|
|
|
if b == 0:
|
|
|
|
|
raise ELSLMathError
|
2014-07-26 02:43:44 +02:00
|
|
|
if a < 0:
|
|
|
|
|
return int(-((-a) % abs(b)))
|
|
|
|
|
return int(a % abs(b))
|
|
|
|
|
if type(a) == type(b) == Vector:
|
|
|
|
|
# cross product
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = v2f(a)
|
|
|
|
|
b = v2f(b)
|
2017-01-17 02:22:10 +01:00
|
|
|
return Vector(F32((a[1]*b[2]-a[2]*b[1],
|
|
|
|
|
a[2]*b[0]-a[0]*b[2],
|
|
|
|
|
a[0]*b[1]-a[1]*b[0]), f32))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
raise ELSLTypeMismatch
|
|
|
|
|
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
def compare(a, b, Eq = True):
|
|
|
|
|
"""Calculate a == b when Eq is True, or a != b when not"""
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2017-01-22 19:45:14 +01:00
|
|
|
# Defined for all types as long as one of them can be auto-cast to the other
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
ta = type(a)
|
|
|
|
|
tb = type(b)
|
|
|
|
|
if ta in (int, float) and tb in (int, float):
|
|
|
|
|
# we trust that NaN == NaN is False
|
2017-01-17 02:38:52 +01:00
|
|
|
if ta == tb == int:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
ret = a == b
|
2017-01-17 02:38:52 +01:00
|
|
|
else:
|
|
|
|
|
ret = ff(a) == ff(b)
|
2017-10-18 13:45:46 +02:00
|
|
|
return int(ret == Eq)
|
2015-02-11 05:43:13 +01:00
|
|
|
if ta in (unicode, Key) and tb in (unicode, Key):
|
2017-10-21 12:32:41 +02:00
|
|
|
ret = 0 if a == b else 1 if (not lslcommon.LSO
|
|
|
|
|
or a.encode('utf8') > b.encode('utf8')) else -1
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return int(not ret) if Eq else ret
|
|
|
|
|
if ta == tb in (Vector, Quaternion):
|
2017-10-18 13:45:46 +02:00
|
|
|
ret = not any(ae != be for ae, be in zip(a, b))
|
|
|
|
|
return int(ret == Eq)
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
if ta == tb == list:
|
|
|
|
|
ret = len(a) - len(b)
|
|
|
|
|
return int(not ret) if Eq else ret
|
|
|
|
|
raise ELSLTypeMismatch
|
2014-07-26 02:43:44 +02:00
|
|
|
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
def less(a, b):
|
|
|
|
|
"""Calculate a < b. The rest can be derived by swapping components and by
|
|
|
|
|
negating: a > b is less(b,a); a <= b is 1-less(b,a); a >= b is 1-less(a,b).
|
|
|
|
|
"""
|
|
|
|
|
if type(a) == type(b) == int:
|
|
|
|
|
return int(a < b)
|
|
|
|
|
if type(a) in (int, float) and type(b) in (int, float):
|
|
|
|
|
return int(ff(a) < ff(b))
|
|
|
|
|
raise ELSLTypeMismatch
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2014-07-26 23:50:49 +02:00
|
|
|
def cond(x):
|
|
|
|
|
"""Test whether x evaluates to True in a condition (if, while, for, ...)"""
|
|
|
|
|
tx = type(x)
|
2017-10-14 14:17:42 +02:00
|
|
|
if tx not in Types:
|
|
|
|
|
raise ELSLInvalidType
|
2014-07-26 23:50:49 +02:00
|
|
|
if tx == Key:
|
|
|
|
|
if x == NULL_KEY or len(x) != 36:
|
|
|
|
|
return False
|
2016-06-26 22:33:10 +02:00
|
|
|
return bool(key_re.search(x))
|
2014-07-26 23:50:49 +02:00
|
|
|
if tx == Vector:
|
|
|
|
|
return bool(compare(x, ZERO_VECTOR, Eq=False))
|
|
|
|
|
if tx == Quaternion:
|
|
|
|
|
return bool(compare(x, ZERO_ROTATION, Eq=False))
|
2017-01-04 01:05:34 +01:00
|
|
|
if lslcommon.LSO and tx == list:
|
|
|
|
|
# SVC-689: lists of 1 element count as false
|
|
|
|
|
return len(x) > 1
|
2017-10-18 13:45:46 +02:00
|
|
|
return bool(x) # works fine for int, float, string, list
|
2014-07-26 23:50:49 +02:00
|
|
|
|
2016-05-21 03:56:27 +02:00
|
|
|
def reduce(t):
|
|
|
|
|
t = F32(t)
|
|
|
|
|
if not t.is_integer():
|
2017-10-18 13:45:46 +02:00
|
|
|
return t # Accurate-ish until big numbers come into play
|
2016-05-21 03:56:27 +02:00
|
|
|
return int(t * 18446744073709551616) % 115904311329233965478 / 18446744073709551616.
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
#
|
|
|
|
|
# LSL-compatible computation functions
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
def llAbs(i):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
i = fi(i)
|
2017-01-22 19:45:14 +01:00
|
|
|
if i != -2147483648:
|
|
|
|
|
return abs(i)
|
|
|
|
|
if lslcommon.LSO:
|
|
|
|
|
return i
|
|
|
|
|
# Mono raises an OverflowException in this case.
|
|
|
|
|
raise ELSLCantCompute
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llAcos(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
2017-10-18 02:42:58 +02:00
|
|
|
return F32(math.acos(f)) if not math.isnan(f) else f
|
2014-07-26 02:43:44 +02:00
|
|
|
except ValueError:
|
|
|
|
|
return NaN
|
|
|
|
|
|
|
|
|
|
def llAngleBetween(r1, r2):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r1 = q2f(r1)
|
|
|
|
|
r2 = q2f(r2)
|
2017-01-23 00:08:24 +01:00
|
|
|
return llRot2Angle(div(qnz(r1), qnz(r2), f32=False))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llAsin(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
2017-10-18 02:42:58 +02:00
|
|
|
return F32(math.asin(f)) if not math.isnan(f) else f
|
2014-07-26 02:43:44 +02:00
|
|
|
except ValueError:
|
|
|
|
|
return NaN
|
|
|
|
|
|
|
|
|
|
def llAtan2(y, x):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
y = ff(y)
|
|
|
|
|
x = ff(x)
|
2016-12-22 01:08:07 +01:00
|
|
|
if math.isnan(x) and math.isnan(y):
|
2017-10-18 13:45:46 +02:00
|
|
|
return mul(x, y)
|
|
|
|
|
if math.isnan(x):
|
2016-12-22 01:08:07 +01:00
|
|
|
return x
|
2017-10-18 13:45:46 +02:00
|
|
|
if math.isnan(y):
|
2016-12-22 01:08:07 +01:00
|
|
|
return y
|
2016-12-21 06:01:03 +01:00
|
|
|
return F32(math.atan2(y, x))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llAxes2Rot(fwd, left, up):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
fwd = v2f(fwd)
|
|
|
|
|
left = v2f(left)
|
|
|
|
|
up = v2f(up)
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
# One of the hardest.
|
|
|
|
|
|
|
|
|
|
t = math.fsum((fwd[0], left[1], up[2]))
|
2017-10-18 13:45:46 +02:00
|
|
|
if t > 0.: # no danger of division by zero or negative roots
|
2014-07-26 02:43:44 +02:00
|
|
|
r = math.sqrt(1. + t)
|
|
|
|
|
s = 0.5/r
|
|
|
|
|
|
|
|
|
|
# For the case of ix+jy+kz > 0, it can return an unnormalized quaternion
|
2017-01-23 01:18:19 +01:00
|
|
|
return Quaternion(F32((s*(left[2]-up[1]), s*(up[0]-fwd[2]), s*(fwd[1]-left[0]), r*0.5)))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Find a positive combo. LSL normalizes the result in these cases only, so we do the same.
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if left[1] <= fwd[0] >= up[2]: # is fwd[0] the greatest?
|
2014-07-26 02:43:44 +02:00
|
|
|
r = math.sqrt(1. + fwd[0] - left[1] - up[2])
|
|
|
|
|
s = 0.5/r
|
|
|
|
|
q = (r*0.5, s*(fwd[1]+left[0]), s*(up[0]+fwd[2]), s*(left[2]-up[1]))
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
elif fwd[0] <= left[1] >= up[2]: # is left[1] the greatest?
|
2014-07-26 02:43:44 +02:00
|
|
|
r = math.sqrt(1. - fwd[0] + left[1] - up[2])
|
|
|
|
|
s = 0.5/r
|
|
|
|
|
q = (s*(fwd[1]+left[0]), r*0.5, s*(left[2]+up[1]), s*(up[0]-fwd[2]))
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# Only one case remaining: up[2] is the greatest
|
|
|
|
|
r = math.sqrt(1. - fwd[0] - left[1] + up[2])
|
|
|
|
|
s = 0.5/r
|
|
|
|
|
q = (s*(up[0]+fwd[2]), s*(left[2]+up[1]), r*0.5, s*(fwd[1]-left[0]))
|
|
|
|
|
|
|
|
|
|
# Normalize
|
2017-01-23 01:59:32 +01:00
|
|
|
q = qnz(q)
|
|
|
|
|
mag = math.sqrt(math.fsum((q[0]*q[0], q[1]*q[1], q[2]*q[2], q[3]*q[3])))
|
2014-07-26 02:43:44 +02:00
|
|
|
return Quaternion(F32((q[0]/mag, q[1]/mag, q[2]/mag, q[3]/mag)))
|
|
|
|
|
|
|
|
|
|
def llAxisAngle2Rot(axis, angle):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
axis = v2f(axis)
|
|
|
|
|
angle = ff(angle)
|
2015-02-11 18:17:01 +01:00
|
|
|
axis = llVecNorm(axis, f32=False)
|
2014-07-26 02:43:44 +02:00
|
|
|
if axis == ZERO_VECTOR:
|
|
|
|
|
angle = 0.
|
2016-12-21 06:01:03 +01:00
|
|
|
c = math.cos(angle*0.5)
|
|
|
|
|
s = math.sin(angle*0.5)
|
2014-07-26 02:43:44 +02:00
|
|
|
return Quaternion(F32((axis[0]*s, axis[1]*s, axis[2]*s, c)))
|
|
|
|
|
|
2015-02-11 05:43:13 +01:00
|
|
|
# NOTE: This one does not always return the same value in LSL. When it isn't
|
|
|
|
|
# deterministic, it raises ELSLCantCompute.
|
2014-07-26 02:43:44 +02:00
|
|
|
def llBase64ToInteger(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
if len(s) > 8:
|
|
|
|
|
return 0
|
2016-06-26 22:33:10 +02:00
|
|
|
s = b64_re.search(s).group()
|
2014-07-26 02:43:44 +02:00
|
|
|
i = len(s)
|
2015-02-11 05:43:13 +01:00
|
|
|
s = b64decode(s + u'='*(-i & 3))
|
|
|
|
|
if len(s) < 3:
|
|
|
|
|
# not computable deterministically
|
|
|
|
|
raise ELSLCantCompute
|
|
|
|
|
s = (s + b'\0')[:4]
|
2014-07-26 02:43:44 +02:00
|
|
|
i = ord(s[0]) if s[0] < b'\x80' else ord(s[0])-256
|
|
|
|
|
return (i<<24)+(ord(s[1])<<16)+(ord(s[2])<<8)+ord(s[3])
|
|
|
|
|
|
2016-12-13 15:04:02 +01:00
|
|
|
b64tos_re = re.compile(
|
|
|
|
|
b'('
|
|
|
|
|
# Those pass through and are caught by InternalUTF8toString:
|
2017-10-18 13:45:46 +02:00
|
|
|
b'\x00$' # NUL at last position (zstr removes it)
|
2017-01-19 07:00:06 +01:00
|
|
|
b'|[\x09\x0A\x0F\x1F-\x7F\xFE\xFF]|[\xC2-\xDF][\x80-\xBF]'
|
2016-12-13 15:04:02 +01:00
|
|
|
b'|(?:\xE0[\xA0-\xBF]|[\xE1-\xEF][\x80-\xBF])[\x80-\xBF]'
|
|
|
|
|
b'|(?:\xF0[\x90-\xBF]|[\xF1-\xF7][\x80-\xBF])[\x80-\xBF]{2}'
|
|
|
|
|
b'|(?:\xF8[\x88-\xBF]|[\xF9-\xFB][\x80-\xBF])[\x80-\xBF]{3}'
|
|
|
|
|
b'|(?:\xFC[\x84-\xBF]|\xFD[\x80-\xBF])[\x80-\xBF]{4}'
|
|
|
|
|
b')|('
|
|
|
|
|
# Those are caught here and substituted by a single "?"
|
|
|
|
|
# (greediness is important here):
|
2017-01-19 07:00:06 +01:00
|
|
|
b'[\x00-\x1F\x80-\xBF]'
|
2016-12-13 15:04:02 +01:00
|
|
|
b'|[\xC0-\xDF][\x80-\xBF]?'
|
|
|
|
|
b'|[\xE0-\xEF][\x80-\xBF]{0,2}'
|
|
|
|
|
b'|[\xF0-\xF7][\x80-\xBF]{0,3}'
|
|
|
|
|
b'|[\xF8-\xFB][\x80-\xBF]{0,4}'
|
|
|
|
|
b'|[\xFC-\xFD][\x80-\xBF]{0,5}'
|
2017-10-18 13:45:46 +02:00
|
|
|
b')|(.)' # should never be reached
|
2016-12-13 15:04:02 +01:00
|
|
|
)
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
def llBase64ToString(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2016-06-26 22:33:10 +02:00
|
|
|
s = b64_re.search(s).group(0)
|
2016-12-13 15:04:02 +01:00
|
|
|
|
|
|
|
|
# llUnescapeURL and llBase64ToString behave differently.
|
|
|
|
|
# llBase64ToString does a first check on the UTF-8 before the standard
|
|
|
|
|
# conversion, unlike llUnescapeURL. That makes it have a much more similar
|
|
|
|
|
# behaviour to LSO's than llUnescapeURL does. But LL being LL, the check
|
|
|
|
|
# is, of course, flawed, and some illegal sequences pass as good (but in
|
|
|
|
|
# Mono they are fortunately stopped on the conversion to UTF-8 instead).
|
|
|
|
|
# The check that llBase64ToString does has the quirk that the invalid
|
|
|
|
|
# sequences that it catches are treated as 1 single bad character instead
|
|
|
|
|
# of as many as the sequence has. The latter is what normal conversion to
|
|
|
|
|
# UTF-8 does. This causes inconsistencies in the number of ?'s returned.
|
|
|
|
|
|
|
|
|
|
# In llBase64ToString, trailing NUL is stripped, and embedded NULs are
|
2017-01-19 07:00:06 +01:00
|
|
|
# converted to "?". In addition, characters in range 00-1F are also
|
|
|
|
|
# converted to "?" except for \x09, \x0A, \x0F, \x1F.
|
2016-12-13 15:04:02 +01:00
|
|
|
|
2017-01-19 03:31:02 +01:00
|
|
|
byteseq = bytearray(b64decode(s + u'=' * (-len(s) & 3)))
|
2016-12-13 15:04:02 +01:00
|
|
|
|
|
|
|
|
pos = 0
|
|
|
|
|
match = b64tos_re.search(byteseq, pos)
|
|
|
|
|
while match is not None:
|
|
|
|
|
assert match.group(3) is None, 'Fail in b64tos_re: ' + match.group(3)
|
|
|
|
|
L = len(match.group(2) or '')
|
|
|
|
|
if L:
|
|
|
|
|
byteseq[pos:pos+L] = b'?'
|
|
|
|
|
pos = match.end(2) - L + 1
|
|
|
|
|
else:
|
|
|
|
|
pos = match.end(1)
|
|
|
|
|
|
|
|
|
|
match = b64tos_re.search(byteseq, pos)
|
|
|
|
|
|
|
|
|
|
return InternalUTF8toString(bytes(byteseq))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llCSV2List(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
bracketlevel = 0
|
2016-03-25 20:52:48 +01:00
|
|
|
lastwascomma = True # first space is eaten!!!
|
2014-07-26 02:43:44 +02:00
|
|
|
lastidx = 0
|
|
|
|
|
i = 0
|
|
|
|
|
ret = []
|
|
|
|
|
for c in s:
|
|
|
|
|
if bracketlevel:
|
|
|
|
|
# ignore ',', focus on nesting level
|
|
|
|
|
if c == u'<':
|
|
|
|
|
bracketlevel += 1
|
|
|
|
|
elif c == u'>':
|
|
|
|
|
bracketlevel -= 1
|
2017-10-18 13:45:46 +02:00
|
|
|
elif lastwascomma and c == u' ': # eat space after comma
|
2014-07-26 02:43:44 +02:00
|
|
|
lastwascomma = False
|
|
|
|
|
lastidx = i+1
|
|
|
|
|
else:
|
2016-03-25 20:44:59 +01:00
|
|
|
lastwascomma = False
|
2014-07-26 02:43:44 +02:00
|
|
|
if c == u',':
|
|
|
|
|
lastwascomma = True
|
|
|
|
|
ret.append(s[lastidx:i])
|
|
|
|
|
lastidx = i+1
|
|
|
|
|
elif c == u'<':
|
|
|
|
|
bracketlevel += 1
|
|
|
|
|
i += 1
|
|
|
|
|
ret.append(s[lastidx:i])
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def llCeil(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isnan(f) or math.isinf(f) or f >= 2147483648.0 or f < -2147483648.0:
|
|
|
|
|
return -2147483648
|
|
|
|
|
return int(math.ceil(f))
|
|
|
|
|
|
|
|
|
|
def llCos(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isinf(f):
|
2015-09-07 02:56:44 +02:00
|
|
|
return Indet
|
2016-05-21 02:50:55 +02:00
|
|
|
if -9223372036854775808.0 < f < 9223372036854775808.0:
|
2016-05-21 04:16:34 +02:00
|
|
|
return F32(math.cos(reduce(f)))
|
2014-07-26 02:43:44 +02:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
def llDeleteSubList(lst, start, end):
|
|
|
|
|
# This acts as llList2List if there's wraparound
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
2017-01-22 07:43:02 +01:00
|
|
|
return InternalGetDeleteSubSequence(lst, start, end, isGet=False)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llDeleteSubString(s, start, end):
|
|
|
|
|
# This acts as llGetSubString if there's wraparound
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2017-01-22 07:43:02 +01:00
|
|
|
return InternalGetDeleteSubSequence(s, start, end, isGet=False)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llDumpList2String(lst, sep):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
sep = fs(sep)
|
2014-07-26 02:43:44 +02:00
|
|
|
return sep.join(InternalList2Strings(lst))
|
|
|
|
|
|
|
|
|
|
def llEscapeURL(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2017-10-18 13:45:46 +02:00
|
|
|
s = s.encode('utf8') # get bytes
|
2014-07-26 02:43:44 +02:00
|
|
|
ret = u''
|
|
|
|
|
for c in s:
|
|
|
|
|
if b'A' <= c <= b'Z' or b'a' <= c <= b'z' or b'0' <= c <= b'9':
|
|
|
|
|
ret += c.encode('utf8')
|
|
|
|
|
else:
|
|
|
|
|
ret += u'%%%02X' % ord(c)
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def llEuler2Rot(v):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
v = v2f(v)
|
2014-07-26 02:43:44 +02:00
|
|
|
c0 = math.cos(v[0]*0.5)
|
|
|
|
|
s0 = math.sin(v[0]*0.5)
|
|
|
|
|
c1 = math.cos(v[1]*0.5)
|
|
|
|
|
s1 = math.sin(v[1]*0.5)
|
|
|
|
|
c2 = math.cos(v[2]*0.5)
|
|
|
|
|
s2 = math.sin(v[2]*0.5)
|
|
|
|
|
|
2017-01-24 05:45:42 +01:00
|
|
|
r = F32((s0 * c1 * c2 + c0 * s1 * s2,
|
|
|
|
|
c0 * s1 * c2 - s0 * c1 * s2,
|
|
|
|
|
c0 * c1 * s2 + s0 * s1 * c2,
|
|
|
|
|
c0 * c1 * c2 - s0 * s1 * s2))
|
|
|
|
|
|
|
|
|
|
# Fix the sign
|
|
|
|
|
c0 = math.cos(v[0])
|
|
|
|
|
s0 = math.sin(v[0])
|
|
|
|
|
c1 = math.cos(v[1])
|
|
|
|
|
s1 = math.sin(v[1])
|
|
|
|
|
c2 = math.cos(v[2])
|
|
|
|
|
s2 = math.sin(v[2])
|
|
|
|
|
d1 = c1*c2
|
|
|
|
|
d2 = c0*c2 - s0*s1*s2
|
|
|
|
|
d3 = c0*c1
|
|
|
|
|
if d1 + d2 + d3 > 0:
|
|
|
|
|
return Quaternion(-f for f in r) if r[3] < 0 else Quaternion(r)
|
|
|
|
|
i = 0
|
|
|
|
|
if d2 > d1:
|
|
|
|
|
i = 1
|
|
|
|
|
if d1 < d3 > d2:
|
|
|
|
|
i = 2
|
|
|
|
|
return Quaternion(-f for f in r) if r[i] < 0 else Quaternion(r)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llFabs(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2017-10-18 13:45:46 +02:00
|
|
|
if f == 0.0 or math.isnan(f): # llFabs(-0.0) is -0.0; llFabs(-nan) is -nan
|
2017-01-24 06:02:45 +01:00
|
|
|
return f
|
2016-12-21 06:01:03 +01:00
|
|
|
return math.fabs(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llFloor(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isnan(f) or math.isinf(f) or f >= 2147483648.0 or f < -2147483648.0:
|
|
|
|
|
return -2147483648
|
|
|
|
|
return int(math.floor(f))
|
|
|
|
|
|
2016-12-22 01:05:21 +01:00
|
|
|
def llFrand(lim):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lim = ff(lim)
|
2017-01-16 20:37:11 +01:00
|
|
|
if math.isinf(lim):
|
2016-12-22 01:05:21 +01:00
|
|
|
return 0.
|
2017-01-16 20:37:11 +01:00
|
|
|
if abs(lim) < float.fromhex('0x1p-126'):
|
|
|
|
|
return -0. if lim < 0 else 0.
|
2016-12-22 01:05:21 +01:00
|
|
|
if math.isnan(lim):
|
|
|
|
|
return lim
|
|
|
|
|
|
|
|
|
|
if lslcommon.IsCalc:
|
|
|
|
|
import random
|
2016-12-21 01:18:25 +01:00
|
|
|
val = random.random() * lim
|
|
|
|
|
# Truncate, rather than rounding
|
|
|
|
|
m, e = math.frexp(val)
|
2016-12-22 01:05:21 +01:00
|
|
|
val = F32(math.ldexp(int(m * 16777216.) * .000000059604644775390625, e))
|
2016-12-21 01:18:25 +01:00
|
|
|
if val == lim:
|
2016-12-22 01:05:21 +01:00
|
|
|
# this should never happen
|
|
|
|
|
# (it can happen on denormals, but these cause output of 0.0)
|
2017-10-18 13:45:46 +02:00
|
|
|
val = 0. # pragma: no cover
|
2016-12-21 01:18:25 +01:00
|
|
|
return val
|
|
|
|
|
|
2016-12-22 01:05:21 +01:00
|
|
|
# Can't give a concrete value
|
|
|
|
|
raise ELSLCantCompute
|
|
|
|
|
|
|
|
|
|
def llGenerateKey():
|
|
|
|
|
if lslcommon.IsCalc:
|
|
|
|
|
import time
|
|
|
|
|
import random
|
|
|
|
|
|
2016-12-21 02:45:17 +01:00
|
|
|
s = hashlib.md5((u'%.17g %f %f' % (time.time(), random.random(),
|
|
|
|
|
random.random())).encode('utf8')
|
2016-12-21 06:01:03 +01:00
|
|
|
).hexdigest()
|
2017-10-18 13:45:46 +02:00
|
|
|
return Key('-'.join((s[:8], s[8:12], s[12:16], s[16:20], s[20:32])))
|
2016-12-21 01:18:25 +01:00
|
|
|
|
2016-12-22 01:05:21 +01:00
|
|
|
# Can't give a concrete value
|
|
|
|
|
raise ELSLCantCompute
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llGetListEntryType(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
2017-01-19 04:46:11 +01:00
|
|
|
return Types[type(lst[pos])]
|
2014-07-26 02:43:44 +02:00
|
|
|
except IndexError:
|
2017-10-14 11:33:18 +02:00
|
|
|
# list index out of bounds
|
2017-10-18 13:45:46 +02:00
|
|
|
return 0 # TYPE_INVALID
|
2014-07-26 02:43:44 +02:00
|
|
|
except KeyError:
|
2017-10-14 11:33:18 +02:00
|
|
|
# type of element not in Types
|
2014-07-26 02:43:44 +02:00
|
|
|
raise ELSLInvalidType
|
|
|
|
|
|
|
|
|
|
def llGetListLength(lst):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
2014-07-26 02:43:44 +02:00
|
|
|
return len(lst)
|
|
|
|
|
|
|
|
|
|
def llGetSubString(s, start, end):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2017-01-22 07:43:02 +01:00
|
|
|
return InternalGetDeleteSubSequence(s, start, end, isGet=True)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llInsertString(s, pos, src):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
pos = fi(pos)
|
|
|
|
|
src = fs(src)
|
2017-10-18 13:45:46 +02:00
|
|
|
if pos < 0: pos = 0 # llInsertString does not support negative indices
|
2014-07-26 02:43:44 +02:00
|
|
|
return s[:pos] + src + s[pos:]
|
|
|
|
|
|
|
|
|
|
def llIntegerToBase64(x):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
x = fi(x)
|
2017-10-18 13:45:46 +02:00
|
|
|
return b64encode(chr((x>>24)&255) + chr((x>>16)&255) + chr((x>>8)&255)
|
|
|
|
|
+ chr(x&255)).decode('utf8')
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llList2CSV(lst):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
2014-07-26 21:25:06 +02:00
|
|
|
ret = []
|
2014-07-31 19:18:26 +02:00
|
|
|
for elem in lst:
|
2014-07-26 21:25:06 +02:00
|
|
|
# This always uses LSO rules for float to string.
|
|
|
|
|
if type(elem) == float:
|
2016-05-15 19:55:45 +02:00
|
|
|
if math.isnan(elem) and math.copysign(1.0, elem) < 0:
|
2015-09-07 02:56:44 +02:00
|
|
|
ret.append(u'-nan')
|
|
|
|
|
else:
|
|
|
|
|
ret.append(u'%.6f' % elem)
|
2014-07-26 21:25:06 +02:00
|
|
|
elif type(elem) in (Vector, Quaternion):
|
2015-09-09 18:14:27 +02:00
|
|
|
ret.append(u'<' + llList2CSV(list(elem)) + u'>')
|
2014-07-26 21:25:06 +02:00
|
|
|
else:
|
|
|
|
|
ret.append(InternalTypecast(elem, unicode, InList=True, f32=True))
|
|
|
|
|
ret = u', '.join(ret)
|
2014-07-26 02:43:44 +02:00
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def llList2Float(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
elem = lst[pos]
|
|
|
|
|
if type(elem) == float:
|
|
|
|
|
return elem
|
|
|
|
|
if type(elem) in (int, unicode):
|
|
|
|
|
return InternalTypecast(elem, float, InList=True, f32=True)
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
|
|
def llList2Integer(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
elem = lst[pos]
|
|
|
|
|
if type(elem) == int:
|
|
|
|
|
return elem
|
|
|
|
|
if type(elem) in (float, unicode):
|
|
|
|
|
return InternalTypecast(elem, int, InList=True, f32=True)
|
|
|
|
|
return 0
|
|
|
|
|
except IndexError:
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
def llList2Key(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
elem = lst[pos]
|
|
|
|
|
if type(elem) == Key:
|
|
|
|
|
return elem
|
|
|
|
|
if type(elem) == unicode:
|
|
|
|
|
return Key(elem)
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
if lslcommon.LSO:
|
2014-07-26 23:50:49 +02:00
|
|
|
return Key(NULL_KEY)
|
2014-07-26 02:43:44 +02:00
|
|
|
return Key(u'')
|
|
|
|
|
|
|
|
|
|
def llList2List(lst, start, end):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
start = fi(start)
|
|
|
|
|
end = fi(end)
|
2017-01-22 07:43:02 +01:00
|
|
|
return InternalGetDeleteSubSequence(lst, start, end, isGet=True)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llList2ListStrided(lst, start, end, stride):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
start = fi(start)
|
|
|
|
|
end = fi(end)
|
|
|
|
|
stride = fi(stride)
|
2014-07-26 02:43:44 +02:00
|
|
|
stride = abs(stride) if stride != 0 else 1
|
|
|
|
|
L = len(lst)
|
|
|
|
|
if start < 0: start += L
|
|
|
|
|
if end < 0: end += L
|
|
|
|
|
if start > end:
|
|
|
|
|
start = 0
|
|
|
|
|
end = L-1
|
|
|
|
|
# start is rounded up to ceil(start/stride)*stride
|
|
|
|
|
start = ((start+stride-1)/stride)*stride
|
|
|
|
|
# end is rounded down to floor(start/stride)*stride
|
|
|
|
|
end = (end/stride)*stride
|
|
|
|
|
|
|
|
|
|
return lst[start:end+1:stride]
|
|
|
|
|
|
|
|
|
|
def llList2Rot(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
elem = lst[pos]
|
|
|
|
|
if type(elem) == Quaternion:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# The list should not contain integer quaternion components, but
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
# we don't err here if not. Instead we return the integer-less
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# quaternion when asked.
|
|
|
|
|
return q2f(elem)
|
2014-07-26 02:43:44 +02:00
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
return ZERO_ROTATION
|
|
|
|
|
|
|
|
|
|
def llList2String(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
return InternalTypecast(lst[pos], unicode, InList=True, f32=True)
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
return u''
|
|
|
|
|
|
|
|
|
|
def llList2Vector(lst, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
elem = lst[pos]
|
|
|
|
|
if type(elem) == Vector:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# The list should not contain integer vector components, but
|
|
|
|
|
# we don't control that here. Instead we return the integer-less
|
|
|
|
|
# vector when asked.
|
2015-02-11 05:43:13 +01:00
|
|
|
return v2f(elem)
|
2014-07-26 02:43:44 +02:00
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
return ZERO_VECTOR
|
|
|
|
|
|
|
|
|
|
def llListFindList(lst, elems):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
elems = fl(elems)
|
2014-07-26 02:43:44 +02:00
|
|
|
# NaN is found in floats, but not in vectors
|
|
|
|
|
L1 = len(lst)
|
|
|
|
|
L2 = len(elems)
|
|
|
|
|
if L2 > L1:
|
2017-10-18 13:45:46 +02:00
|
|
|
return -1 # can't find a sublist longer than the original list
|
2014-07-26 02:43:44 +02:00
|
|
|
if L2 == 0:
|
2015-05-24 04:22:31 +02:00
|
|
|
# empty list is always found at position 0 in Mono,
|
|
|
|
|
# and in LSO if the first list isn't empty
|
|
|
|
|
return -1 if lslcommon.LSO and L1 == 0 else 0
|
2014-07-26 02:43:44 +02:00
|
|
|
for i in xrange(L1-L2+1):
|
|
|
|
|
for j in xrange(L2):
|
|
|
|
|
e1 = lst[i+j]
|
|
|
|
|
e2 = elems[j]
|
|
|
|
|
if type(e1) == type(e2) == float:
|
|
|
|
|
if e1 == e2:
|
|
|
|
|
continue
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# Exceptionally, NaN equals NaN
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isnan(e1) and math.isnan(e2):
|
|
|
|
|
continue
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# Mismatch
|
2014-07-26 02:43:44 +02:00
|
|
|
break
|
|
|
|
|
elif type(e1) == type(e2) in (Vector, Quaternion):
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# Act as if the list's vector/quat was all floats, even if not
|
|
|
|
|
if type(e1) == Vector:
|
|
|
|
|
e1 = v2f(e1)
|
|
|
|
|
e2 = v2f(e2)
|
|
|
|
|
else:
|
|
|
|
|
e1 = q2f(e1)
|
|
|
|
|
e2 = q2f(e2)
|
2014-07-26 02:43:44 +02:00
|
|
|
# Unfortunately, Python fails to consider (NaN,) != (NaN,) sometimes
|
|
|
|
|
# so we need to implement our own test
|
|
|
|
|
for e1e,e2e in zip(e1,e2):
|
2017-10-18 13:45:46 +02:00
|
|
|
if e1e != e2e: # NaNs are considered different to themselves here as normal
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# Mismatch in vector/quaternion sub-element
|
2014-07-26 02:43:44 +02:00
|
|
|
break
|
|
|
|
|
else:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# No mismatch in any sub-element, try next list element
|
|
|
|
|
continue
|
2017-10-18 13:45:46 +02:00
|
|
|
break # discrepancy found
|
2014-07-26 02:43:44 +02:00
|
|
|
elif type(e1) != type(e2) or e1 != e2:
|
2017-10-18 13:45:46 +02:00
|
|
|
break # mismatch
|
2014-07-26 02:43:44 +02:00
|
|
|
else:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# no mismatch
|
2014-07-26 02:43:44 +02:00
|
|
|
return i
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
def llListInsertList(lst, elems, pos):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
elems = fl(elems)
|
|
|
|
|
pos = fi(pos)
|
2014-07-26 02:43:44 +02:00
|
|
|
# Unlike llInsertString, this function does support negative indices.
|
|
|
|
|
return lst[:pos] + elems + lst[pos:]
|
|
|
|
|
|
|
|
|
|
# not implemented as it does not give the same output for the same input
|
|
|
|
|
#def llListRandomize(x):
|
|
|
|
|
|
|
|
|
|
def llListReplaceList(lst, elems, start, end):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
elems = fl(elems)
|
|
|
|
|
start = fi(start)
|
|
|
|
|
end = fi(end)
|
2014-07-26 02:43:44 +02:00
|
|
|
L = len(lst)
|
2015-03-04 00:31:55 +01:00
|
|
|
if start < -L:
|
|
|
|
|
# llListReplaceList([0,1,2,3],[5],-5,-5) should return [0,1,2,3]
|
|
|
|
|
# llListReplaceList([0,1,2,3],[5],-5,-4) should return [1,2,3]
|
|
|
|
|
# llListReplaceList([0,1,2,3],[5],-5,-7) should return []
|
|
|
|
|
elems = []
|
2014-07-26 02:43:44 +02:00
|
|
|
if (start + L if start < 0 else start) > (end + L if end < 0 else end):
|
|
|
|
|
# Exclusion range. Appends elems at 'start' i.e. at end :)
|
|
|
|
|
if end == -1: end += L
|
|
|
|
|
return lst[end+1:start] + elems
|
|
|
|
|
if end == -1: end += L
|
|
|
|
|
return lst[:start] + elems + lst[end+1:]
|
|
|
|
|
|
|
|
|
|
def llListSort(lst, stride, asc):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
lst = fl(lst)
|
|
|
|
|
stride = fi(stride)
|
|
|
|
|
asc = fi(asc)
|
2017-10-18 13:45:46 +02:00
|
|
|
lst = lst[:] # make a copy
|
2014-07-26 02:43:44 +02:00
|
|
|
L = len(lst)
|
2017-10-18 13:45:46 +02:00
|
|
|
broken = u'\ufb1a' > u'\U0001d41a' # that happens on Windows
|
2014-07-26 02:43:44 +02:00
|
|
|
if stride < 1: stride = 1
|
|
|
|
|
if L % stride:
|
|
|
|
|
return lst
|
|
|
|
|
for i in xrange(0, L-stride, stride):
|
|
|
|
|
# Optimized by caching the element in the outer loop AND after swapping.
|
|
|
|
|
a = lst[i]
|
|
|
|
|
ta = type(a)
|
|
|
|
|
if ta == Vector:
|
2017-10-18 13:45:46 +02:00
|
|
|
a = v2f(a) # list should contain vectors made only of floats
|
2014-07-26 02:43:44 +02:00
|
|
|
a = a[0]*a[0] + a[1]*a[1] + a[2]*a[2]
|
2017-10-21 12:32:41 +02:00
|
|
|
if lslcommon.LSO:
|
|
|
|
|
# LSO compares bytes, not Unicode.
|
|
|
|
|
a = a.encode('utf8')
|
|
|
|
|
elif broken and ta in (unicode, Key):
|
2016-12-22 05:31:03 +01:00
|
|
|
# Note this breaks type consistency between a and ta!
|
|
|
|
|
# It should be OK because only equal types are compared.
|
2017-10-18 13:45:46 +02:00
|
|
|
a = a.encode('utf-32-be') # pragma: no cover
|
2014-07-26 02:43:44 +02:00
|
|
|
for j in xrange(i+stride, L, stride):
|
|
|
|
|
b = lst[j]
|
|
|
|
|
tb = type(b)
|
|
|
|
|
gt = False
|
|
|
|
|
if ta == tb:
|
|
|
|
|
if tb == Vector:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
b = v2f(b)
|
2014-07-26 02:43:44 +02:00
|
|
|
gt = not (a <= b[0]*b[0] + b[1]*b[1] + b[2]*b[2])
|
2017-01-17 02:53:26 +01:00
|
|
|
# (note NaNs compare as > thus the reversed condition!)
|
2014-07-26 02:43:44 +02:00
|
|
|
elif tb != Quaternion:
|
2017-10-21 12:32:41 +02:00
|
|
|
if lslcommon.LSO:
|
|
|
|
|
b = b.encode('utf8')
|
|
|
|
|
elif broken and tb in (unicode, Key):
|
2017-10-18 13:45:46 +02:00
|
|
|
b = b.encode('utf-32-be') # pragma: no cover
|
|
|
|
|
gt = not (a <= b) # float, integer, string, key all take this branch
|
2017-01-17 02:53:26 +01:00
|
|
|
# (note NaNs compare as > thus the reversed condition!)
|
2014-07-26 02:43:44 +02:00
|
|
|
if gt ^ (asc != 1):
|
|
|
|
|
# swap
|
|
|
|
|
lst[i:i+stride],lst[j:j+stride] = lst[j:j+stride],lst[i:i+stride]
|
|
|
|
|
# Re-cache
|
|
|
|
|
a = lst[i]
|
|
|
|
|
ta = type(a)
|
|
|
|
|
if ta == Vector:
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
a = v2f(a)
|
2014-07-26 02:43:44 +02:00
|
|
|
a = a[0]*a[0] + a[1]*a[1] + a[2]*a[2]
|
2016-12-22 05:31:03 +01:00
|
|
|
if broken and ta in (unicode, Key):
|
2017-10-18 13:45:46 +02:00
|
|
|
a = a.encode('utf-32-be') # pragma: no cover
|
2014-07-26 02:43:44 +02:00
|
|
|
return lst
|
|
|
|
|
|
|
|
|
|
def llListStatistics(op, lst):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
op = fi(op)
|
|
|
|
|
lst = fl(lst)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
nums = []
|
|
|
|
|
# Extract numbers in reverse order. LIST_STAT_MEDIAN uses that.
|
|
|
|
|
for elem in lst:
|
|
|
|
|
if type(elem) in (int, float):
|
|
|
|
|
nums.insert(0, float(elem))
|
|
|
|
|
|
|
|
|
|
if nums == []:
|
|
|
|
|
return 0.0
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if op == 8: # LIST_STAT_NUM_COUNT
|
2014-07-26 02:43:44 +02:00
|
|
|
return float(len(nums))
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if op in (0, 1, 2): # LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX
|
2014-07-26 02:43:44 +02:00
|
|
|
min = None
|
|
|
|
|
for elem in nums:
|
|
|
|
|
if min is None:
|
|
|
|
|
min = max = elem
|
|
|
|
|
else:
|
|
|
|
|
if elem < min:
|
|
|
|
|
min = elem
|
|
|
|
|
if elem > max:
|
|
|
|
|
max = elem
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(max - min if op == 0 else min if op == 1 else max)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if op == 4: # LIST_STAT_MEDIAN requires special treatment
|
2014-07-26 02:43:44 +02:00
|
|
|
# The function behaves very strangely with NaNs. This seems to reproduce it:
|
|
|
|
|
|
|
|
|
|
# llListSort seems to do the right thing with NaNs as needed by the median.
|
|
|
|
|
nums = llListSort(nums, 1, 1)
|
|
|
|
|
L = len(nums)
|
|
|
|
|
if L & 1:
|
|
|
|
|
return F32(nums[L>>1])
|
|
|
|
|
return F32((nums[(L>>1)-1] + nums[L>>1])*0.5)
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if op in (3, 5, 6, 7): # LIST_STAT_MEAN, STD_DEV, SUM, SUM_SQUARES
|
2014-07-26 02:43:44 +02:00
|
|
|
sum = 0.
|
|
|
|
|
sumsq = 0.
|
|
|
|
|
mean = 0.
|
|
|
|
|
N = 0.
|
|
|
|
|
M2 = 0.
|
|
|
|
|
for elem in nums:
|
|
|
|
|
N += 1.
|
|
|
|
|
sum += elem
|
|
|
|
|
sumsq += elem*elem
|
|
|
|
|
delta = elem - mean
|
|
|
|
|
mean += delta/N
|
|
|
|
|
M2 += delta*(elem-mean)
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if op == 5: # LIST_STAT_STD_DEV
|
2014-07-26 02:43:44 +02:00
|
|
|
return 0. if N == 1. else F32(math.sqrt(M2/(N-1.)))
|
2017-10-18 13:45:46 +02:00
|
|
|
if op == 6: # LIST_STAT_SUM
|
2014-07-26 02:43:44 +02:00
|
|
|
return F32(sum)
|
2017-10-18 13:45:46 +02:00
|
|
|
if op == 7: # LIST_STAT_SUM_SQUARES
|
2014-07-26 02:43:44 +02:00
|
|
|
return F32(sumsq)
|
|
|
|
|
return F32(mean)
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if op == 9: # LIST_STAT_GEOMETRIC_MEAN
|
2014-07-26 02:43:44 +02:00
|
|
|
N = 0.
|
|
|
|
|
GMlog = 0.
|
|
|
|
|
for elem in nums:
|
|
|
|
|
if elem <= 0.:
|
|
|
|
|
return 0.
|
|
|
|
|
N += 1.
|
|
|
|
|
delta = math.log(elem) - GMlog
|
|
|
|
|
GMlog += delta/N
|
|
|
|
|
return F32(math.exp(GMlog))
|
|
|
|
|
|
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
|
|
def llLog(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isinf(f) and f < 0 or math.isnan(f) or f <= 0.0:
|
|
|
|
|
return 0.0
|
|
|
|
|
return F32(math.log(f))
|
|
|
|
|
|
|
|
|
|
def llLog10(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isinf(f) and f < 0 or math.isnan(f) or f <= 0.0:
|
|
|
|
|
return 0.0
|
|
|
|
|
return F32(math.log10(f))
|
|
|
|
|
|
|
|
|
|
def llMD5String(s, salt):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
salt = fi(salt)
|
2014-07-26 02:43:44 +02:00
|
|
|
return hashlib.md5(zstr(s).encode('utf8') + b':' + bytes(salt)).hexdigest().decode('utf8')
|
|
|
|
|
|
2017-01-07 20:46:14 +01:00
|
|
|
def llModPow(base, exp, mod):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
base = fi(base)
|
|
|
|
|
exp = fi(exp)
|
|
|
|
|
mod = fi(mod)
|
2017-01-07 20:46:14 +01:00
|
|
|
if not lslcommon.IsCalc:
|
|
|
|
|
# This function has a delay, therefore it's not safe to compute it
|
|
|
|
|
# unless in calculator mode.
|
|
|
|
|
raise ELSLCantCompute
|
2014-07-26 02:43:44 +02:00
|
|
|
# With some luck, this works fully with native ints on 64 bit machines.
|
|
|
|
|
if mod in (0, 1):
|
|
|
|
|
return 0
|
|
|
|
|
if exp == 0:
|
|
|
|
|
return 1
|
|
|
|
|
# Convert all numbers to unsigned
|
2017-10-18 13:45:46 +02:00
|
|
|
base &= 0xFFFFFFFF
|
|
|
|
|
exp &= 0xFFFFFFFF
|
|
|
|
|
mod &= 0xFFFFFFFF
|
2014-07-26 02:43:44 +02:00
|
|
|
prod = base
|
|
|
|
|
ret = 1
|
|
|
|
|
while True:
|
|
|
|
|
if exp & 1:
|
|
|
|
|
ret = ((ret * prod) & 0xFFFFFFFF) % mod
|
|
|
|
|
exp = exp >> 1
|
|
|
|
|
if exp == 0:
|
|
|
|
|
break
|
|
|
|
|
prod = ((prod * prod) & 0xFFFFFFFF) % mod
|
|
|
|
|
|
|
|
|
|
return S32(ret)
|
|
|
|
|
|
|
|
|
|
def llParseString2List(s, exc, inc, KeepNulls=False):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
exc = fl(exc)
|
|
|
|
|
inc = fl(inc)
|
2014-07-26 02:43:44 +02:00
|
|
|
if s == u'' and KeepNulls:
|
|
|
|
|
return [s]
|
|
|
|
|
exc = exc[:8]
|
|
|
|
|
inc = inc[:8]
|
|
|
|
|
regex = u''
|
|
|
|
|
for i in exc:
|
|
|
|
|
if i != u'':
|
|
|
|
|
regex += u'|' + re.escape(i)
|
|
|
|
|
for i in inc:
|
|
|
|
|
if i != u'':
|
|
|
|
|
regex += u'|' + re.escape(i)
|
|
|
|
|
if regex == u'':
|
|
|
|
|
split = [s]
|
|
|
|
|
else:
|
|
|
|
|
regex = u'(' + regex[1:] + u')'
|
|
|
|
|
split = re.split(regex, s)
|
|
|
|
|
return [i for i in split if (KeepNulls or i != u'') and i not in exc]
|
|
|
|
|
|
|
|
|
|
def llParseStringKeepNulls(s, exc, inc):
|
|
|
|
|
return llParseString2List(s, exc, inc, KeepNulls=True)
|
|
|
|
|
|
|
|
|
|
def llPow(base, exp):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
base = ff(base)
|
|
|
|
|
exp = ff(exp)
|
2014-07-26 02:43:44 +02:00
|
|
|
try:
|
|
|
|
|
# Python corner cases and LSL corner cases differ
|
|
|
|
|
|
|
|
|
|
# Python matches these two, but we don't want to get trapped by our own checks.
|
|
|
|
|
if math.isnan(base) or math.isnan(exp):
|
|
|
|
|
return NaN
|
|
|
|
|
if exp == 0.0:
|
|
|
|
|
return 1.0
|
|
|
|
|
|
2017-10-18 13:45:46 +02:00
|
|
|
if base == 0.0: # Python gives exception on these, LSL returns stuff
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isinf(exp) and exp < 0:
|
2017-10-18 13:45:46 +02:00
|
|
|
return Infinity # llPow(0.0, -inf) = inf
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
if exp < 0.0:
|
|
|
|
|
# Negative finite exponent cases
|
|
|
|
|
if math.copysign(1, base) < 0 and exp.is_integer() and not (exp/2.).is_integer():
|
2017-10-18 13:45:46 +02:00
|
|
|
return -Infinity # llPow(-0.0, -odd_integer) = -inf
|
2014-07-26 02:43:44 +02:00
|
|
|
return Infinity
|
|
|
|
|
|
|
|
|
|
elif abs(base) == 1.0 and math.isinf(exp):
|
2017-10-18 13:45:46 +02:00
|
|
|
return NaN # Python says 1.0
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
f = F32(math.pow(base, exp))
|
2017-10-18 13:45:46 +02:00
|
|
|
return 0.0 if f == 0.0 else f # don't return -0.0
|
|
|
|
|
except ValueError: # should happen only with negative base and noninteger exponent
|
2015-09-07 02:56:44 +02:00
|
|
|
return Indet
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRot2Angle(r):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r = q2f(r)
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
# Used by llAngleBetween.
|
2014-07-26 02:43:44 +02:00
|
|
|
# Version based on research by Moon Metty, Miranda Umino and Strife Onizuka
|
|
|
|
|
return F32(2.*math.atan2(math.sqrt(math.fsum((r[0]*r[0], r[1]*r[1], r[2]*r[2]))), abs(r[3])));
|
|
|
|
|
|
|
|
|
|
def llRot2Axis(r):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r = q2f(r)
|
2016-01-22 20:54:43 +01:00
|
|
|
if r[3] < 0:
|
|
|
|
|
return llVecNorm(Vector((-r[0], -r[1], -r[2])))
|
2015-02-11 05:43:13 +01:00
|
|
|
return llVecNorm(Vector((r[0], r[1], r[2])))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRot2Euler(r):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r = q2f(r)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Another one of the hardest. The formula for Z angle in the
|
|
|
|
|
# singularity case was inspired by the viewer code.
|
2017-01-23 18:55:30 +01:00
|
|
|
r = qnorm(r)
|
2014-07-26 02:43:44 +02:00
|
|
|
y = 2*(r[0]*r[2] + r[1]*r[3])
|
|
|
|
|
|
2017-01-23 18:55:30 +01:00
|
|
|
# Check gimbal lock condition
|
2014-07-26 02:43:44 +02:00
|
|
|
if abs(y) > 0.99999:
|
2017-01-23 18:55:30 +01:00
|
|
|
return Vector(F32((0.,
|
|
|
|
|
math.asin(1. if y > 1. else y),
|
|
|
|
|
math.atan2(r[2]*r[3]+r[0]*r[1],
|
|
|
|
|
.5-(r[0]*r[0]+r[2]*r[2]))
|
|
|
|
|
)))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
qy2 = r[1]*r[1]
|
2017-01-23 16:39:58 +01:00
|
|
|
return Vector(F32((
|
2017-01-23 16:41:56 +01:00
|
|
|
math.atan2(r[0]*r[3]-r[1]*r[2], .5-(r[0]*r[0]+qy2)),
|
2014-07-26 02:43:44 +02:00
|
|
|
math.asin(y),
|
2017-01-23 16:41:56 +01:00
|
|
|
math.atan2(r[2]*r[3]-r[0]*r[1], .5-(r[2]*r[2]+qy2))
|
2017-01-23 16:39:58 +01:00
|
|
|
)))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRot2Fwd(r):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r = q2f(r)
|
2016-12-22 03:06:56 +01:00
|
|
|
v = Vector((1., 0., 0.))
|
2017-01-23 00:08:24 +01:00
|
|
|
return llVecNorm(mul(v, qnz(r), f32=False))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRot2Left(r):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r = q2f(r)
|
2016-12-22 03:06:56 +01:00
|
|
|
v = Vector((0., 1., 0.))
|
2017-01-23 00:08:24 +01:00
|
|
|
return llVecNorm(mul(v, qnz(r), f32=False))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRot2Up(r):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
r = q2f(r)
|
2016-12-22 03:06:56 +01:00
|
|
|
v = Vector((0., 0., 1.))
|
2017-01-23 00:08:24 +01:00
|
|
|
return llVecNorm(mul(v, qnz(r), f32=False))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRotBetween(v1, v2):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
v1 = v2f(v1)
|
|
|
|
|
v2 = v2f(v2)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2017-01-27 02:31:00 +01:00
|
|
|
# Loosely based on the "Bad" reference implementation and
|
|
|
|
|
# on SL source code (pre Moon Metty's changes).
|
|
|
|
|
# See <https://bitbucket.org/lindenlab/viewer-release/src/015080d8/indra/llmath/llquaternion.cpp#llquaternion.cpp-425>
|
|
|
|
|
v1 = llVecNorm(v1)
|
|
|
|
|
v2 = llVecNorm(v2)
|
|
|
|
|
dot = mul(v1, v2)
|
|
|
|
|
axis = mod(v1, v2)
|
|
|
|
|
threshold = float.fromhex('0x1.fffffcp-1')
|
|
|
|
|
if -threshold <= dot <= threshold:
|
|
|
|
|
# non-aligned - their cross product is a good axis
|
|
|
|
|
m = math.sqrt(mul(axis, axis) + (dot + 1.) * (dot + 1.))
|
|
|
|
|
return Quaternion(F32((axis[0] / m, axis[1] / m, axis[2] / m,
|
|
|
|
|
(dot + 1.) / m)))
|
|
|
|
|
# about aligned - two cases to deal with
|
|
|
|
|
if dot > 0.:
|
|
|
|
|
# same signs
|
|
|
|
|
return Quaternion((0., 0., 0., 1.))
|
|
|
|
|
# opposite signs - find one vector in the plane perpendicular to
|
|
|
|
|
# either vector, to use as axis. We do this by choosing an arbitrary
|
|
|
|
|
# vector (<1,0,0> in our case), and calculating the cross product with it,
|
|
|
|
|
# which will be perpendicular to both. But matching the SL results requires
|
|
|
|
|
# another cross product of the input with the result, so we do that.
|
|
|
|
|
ortho = mod(mod(v1, Vector((1., 0., 0.))), v1)
|
2017-10-18 13:45:46 +02:00
|
|
|
ortho = Vector((0. if f == 0. else f for f in ortho)) # remove minus zero
|
2017-01-27 02:31:00 +01:00
|
|
|
m = mul(ortho, ortho)
|
|
|
|
|
if m < float.fromhex('0x1.b7cdfep-34'):
|
|
|
|
|
# The input vectors were aligned with <1,0,0>, so this was not a
|
|
|
|
|
# good choice. Return 180 deg. rotation over Z instead.
|
|
|
|
|
return Quaternion((0., 0., 1., 0.))
|
|
|
|
|
m = math.sqrt(m)
|
|
|
|
|
return Quaternion(F32((ortho[0] / m, ortho[1] / m, ortho[2] / m, 0.)))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llRound(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isnan(f) or math.isinf(f) or f >= 2147483647.5 or f < -2147483648.0:
|
|
|
|
|
return -2147483648
|
2017-01-24 05:50:07 +01:00
|
|
|
return int(math.floor(F32(f+0.5)))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llSHA1String(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
return hashlib.sha1(s.encode('utf8')).hexdigest().decode('utf8')
|
|
|
|
|
|
|
|
|
|
def llSin(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isinf(f):
|
2015-09-07 02:56:44 +02:00
|
|
|
return Indet
|
2016-05-21 02:50:55 +02:00
|
|
|
if -9223372036854775808.0 < f < 9223372036854775808.0:
|
2016-05-21 04:16:34 +02:00
|
|
|
return F32(math.sin(reduce(f)))
|
2014-07-26 02:43:44 +02:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
def llSqrt(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if f < 0.0:
|
2015-09-07 02:56:44 +02:00
|
|
|
return Indet
|
2014-07-26 02:43:44 +02:00
|
|
|
# LSL and Python both produce -0.0 when the input is -0.0.
|
2015-06-16 04:41:36 +02:00
|
|
|
return F32(math.sqrt(f))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llStringLength(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
return len(s)
|
|
|
|
|
|
|
|
|
|
def llStringToBase64(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
return b64encode(s.encode('utf8')).decode('utf8')
|
|
|
|
|
|
|
|
|
|
def llStringTrim(s, mode):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
mode = fi(mode)
|
2014-07-26 02:43:44 +02:00
|
|
|
head = 0
|
|
|
|
|
length = len(s)
|
|
|
|
|
tail = length-1
|
2017-10-18 13:45:46 +02:00
|
|
|
if mode & 1: # STRING_TRIM_HEAD
|
2014-07-26 02:43:44 +02:00
|
|
|
while head < length and s[head] in u'\x09\x0a\x0b\x0c\x0d\x20':
|
|
|
|
|
head += 1
|
2017-10-18 13:45:46 +02:00
|
|
|
if mode & 2: # STRING_TRIM_TAIL
|
2014-07-26 02:43:44 +02:00
|
|
|
while tail >= head and s[tail] in u'\x09\x0a\x0b\x0c\x0d\x20':
|
|
|
|
|
tail -= 1
|
|
|
|
|
return s[head:tail+1]
|
|
|
|
|
|
|
|
|
|
def llSubStringIndex(s, pattern):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
pattern = fs(pattern)
|
2014-07-26 02:43:44 +02:00
|
|
|
return s.find(pattern)
|
|
|
|
|
|
|
|
|
|
def llTan(f):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
f = ff(f)
|
2014-07-26 02:43:44 +02:00
|
|
|
if math.isinf(f):
|
2015-09-07 02:56:44 +02:00
|
|
|
return Indet
|
2016-05-21 02:50:55 +02:00
|
|
|
if -9223372036854775808.0 < f < 9223372036854775808.0:
|
2016-05-21 04:16:34 +02:00
|
|
|
return F32(math.tan(reduce(f)))
|
2014-07-26 02:43:44 +02:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
|
def llToLower(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
if lslcommon.LSO:
|
|
|
|
|
return zstr(re.sub(u'[A-Z]', lambda x: x.group().lower(), s))
|
|
|
|
|
return zstr(s.lower())
|
|
|
|
|
|
|
|
|
|
def llToUpper(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
if lslcommon.LSO:
|
|
|
|
|
return zstr(re.sub(u'[a-z]', lambda x: x.group().upper(), s))
|
|
|
|
|
return zstr(s.upper())
|
|
|
|
|
|
|
|
|
|
def llUnescapeURL(s):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
2014-07-26 02:43:44 +02:00
|
|
|
ret = b''
|
|
|
|
|
L = len(s)
|
|
|
|
|
i = 0
|
|
|
|
|
while i < L:
|
|
|
|
|
c = s[i]
|
|
|
|
|
i += 1
|
|
|
|
|
if c != u'%':
|
|
|
|
|
ret += c.encode('utf8')
|
|
|
|
|
continue
|
|
|
|
|
if i >= L:
|
|
|
|
|
break
|
2017-10-18 13:45:46 +02:00
|
|
|
c = s[i] # First digit
|
2014-07-26 02:43:44 +02:00
|
|
|
i += 1
|
|
|
|
|
if i >= L:
|
|
|
|
|
break
|
|
|
|
|
v = 0
|
|
|
|
|
if u'0' <= c <= u'9' or u'A' <= c <= u'F' or u'a' <= c <= u'f':
|
|
|
|
|
v = int(c, 16)<<4
|
2017-10-18 13:45:46 +02:00
|
|
|
c = s[i] # Second digit
|
2014-07-26 02:43:44 +02:00
|
|
|
if c == u'%':
|
|
|
|
|
ret += chr(v)
|
2017-01-20 01:59:59 +01:00
|
|
|
i += 1
|
2014-07-26 02:43:44 +02:00
|
|
|
continue
|
|
|
|
|
i += 1
|
|
|
|
|
if u'0' <= c <= u'9' or u'A' <= c <= u'F' or u'a' <= c <= u'f':
|
|
|
|
|
v += int(c, 16)
|
|
|
|
|
ret += chr(v)
|
|
|
|
|
return InternalUTF8toString(ret)
|
|
|
|
|
|
|
|
|
|
def llVecDist(v1, v2):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
v1 = v2f(v1)
|
|
|
|
|
v2 = v2f(v2)
|
2015-02-11 05:43:13 +01:00
|
|
|
# For improved accuracy, do the intermediate calcs as doubles
|
|
|
|
|
vx = v1[0]-v2[0]
|
|
|
|
|
vy = v1[1]-v2[1]
|
|
|
|
|
vz = v1[2]-v2[2]
|
|
|
|
|
return F32(math.sqrt(math.fsum((vx*vx, vy*vy, vz*vz))))
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llVecMag(v):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
v = v2f(v)
|
2014-07-26 02:43:44 +02:00
|
|
|
return F32(math.sqrt(math.fsum((v[0]*v[0], v[1]*v[1], v[2]*v[2]))))
|
|
|
|
|
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
def llVecNorm(v, f32 = True):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
v = v2f(v)
|
2014-07-26 02:43:44 +02:00
|
|
|
if v == ZERO_VECTOR:
|
|
|
|
|
return v
|
|
|
|
|
f = math.sqrt(math.fsum((v[0]*v[0], v[1]*v[1], v[2]*v[2])))
|
Thorough review of lslbasefuncs and associated files, adding new stuff.
- Scratch TODO item: Change shouldbeXXX to asserts.
- shoudlbeXXX(x) has been turned into assert isXXX(x). Affects lsl2json too.
- New base functions:
- compare (for == and !=)
- less (for <, >, <=, >=)
- minus() renamed to neg() for consistency. Affects testfuncs too.
- add() now supports key+string and string+key.
- Allow integers in place of floats. That has caused the addition of three utility functions:
- ff (force float)
- v2f (force floats in all components of a vector)
- q2f (force floats in all components of a quaternion)
Used everywhere where necessary.
Special care was taken in a few funcs (llListFindList and maybe others) to ensure that lists containing vectors or quaternions which in turn contain integer components, behave as if they were floats, because LSL lists can not physically hold integer values as components of vectors/quats.
This also fixes a case where if a large integer had more precision than a F32 (e.g. 16777217), the product/division would be more precise than what LSL returns.
- Fix bugs of missing F32() in some places (llRotBetween, llEuler2Rot).
- Some functions marked for moving in an incoming commit.
- llList2CSV marked with a warning that it is not thread safe. That's a future TODO.
- Document llListFindList better.
- Make llListStatistics Use a more orthodox method to return the result for LIST_STAT_RANGE, LIST_STAT_MIN, LIST_STAT_MAX.
- Make llAxisAngle2Rot use more precision from llVecNorm, by adding an optional truncation parameter in the latter.
- Change order of some F32(Quaternion(...)) to not force so much instancing.
- Bugfix: llVecNorm did not return a Vector.
2014-07-26 20:54:01 +02:00
|
|
|
return F32(Vector((v[0]/f,v[1]/f,v[2]/f)), f32)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
def llXorBase64(s, xor):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
xor = fs(xor)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Xor the underlying bytes.
|
|
|
|
|
|
|
|
|
|
if xor == u'':
|
|
|
|
|
return s
|
|
|
|
|
|
2016-06-26 22:33:10 +02:00
|
|
|
s = b64_re.search(s).group(0)
|
2014-07-26 02:43:44 +02:00
|
|
|
L1 = len(s)
|
2016-06-26 22:33:10 +02:00
|
|
|
xor = b64_re.search(xor).group(0)
|
2014-07-26 02:43:44 +02:00
|
|
|
L2 = len(xor)
|
|
|
|
|
|
|
|
|
|
if L2 == 0:
|
2015-02-11 05:43:13 +01:00
|
|
|
# The input xor string starts with zero or one valid Base64 characters.
|
|
|
|
|
# This produces garbage bytes (the first byte is zero though).
|
2017-01-19 02:58:42 +01:00
|
|
|
if L1 > 2:
|
|
|
|
|
# We don't produce a result in this case.
|
|
|
|
|
raise ELSLCantCompute
|
|
|
|
|
L2 = 2
|
|
|
|
|
xor = u'AA'
|
|
|
|
|
|
|
|
|
|
s = b64decode(s + u'=' * (-L1 & 3))
|
|
|
|
|
xor = b64decode(xor + u'=' * (-L2 & 3))
|
2014-07-26 02:43:44 +02:00
|
|
|
L2 = len(xor)
|
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
ret = b''
|
|
|
|
|
|
|
|
|
|
Bug3763 = 3763 in Bugs
|
|
|
|
|
# BUG-3763 consists of the binary string having an extra NULL every time after the second repetition of
|
2016-11-14 03:10:29 +01:00
|
|
|
# the XOR pattern. For example, if the XOR binary string is b'pqr' and the input string is
|
2014-07-26 02:43:44 +02:00
|
|
|
# b'12345678901234567890', the XOR binary string behaves as if it was b'pqrpqr\0pqr\0pqr\0pqr\0pq'.
|
|
|
|
|
# We emulate that by adding the zero and increasing the length the first time.
|
|
|
|
|
for c in s:
|
|
|
|
|
ret += chr(ord(c) ^ ord(xor[i]))
|
|
|
|
|
i += 1
|
|
|
|
|
if i >= L2:
|
|
|
|
|
i = 0
|
|
|
|
|
if Bug3763:
|
|
|
|
|
Bug3763 = False
|
|
|
|
|
xor = xor + b'\x00'
|
|
|
|
|
L2 += 1
|
|
|
|
|
return b64encode(ret).decode('utf8')
|
|
|
|
|
|
2017-01-07 20:46:14 +01:00
|
|
|
def llXorBase64Strings(s, xor):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
xor = fs(xor)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
2017-01-07 20:46:14 +01:00
|
|
|
if not lslcommon.IsCalc:
|
|
|
|
|
# This function has a delay, therefore it's not safe to compute it
|
|
|
|
|
# unless in calculator mode.
|
|
|
|
|
raise ELSLCantCompute
|
|
|
|
|
|
2014-07-26 02:43:44 +02:00
|
|
|
if xor == u'':
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
B64 = u'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
|
|
|
|
|
|
|
|
|
# Special case when the first character is not a Base64 one. (LL's ways are inextricable)
|
|
|
|
|
base = B64.find(xor[0])
|
|
|
|
|
if base < 0:
|
|
|
|
|
if xor[0] == u'=':
|
|
|
|
|
xor = u'+' + xor[1:]
|
|
|
|
|
base = 62
|
|
|
|
|
else:
|
|
|
|
|
xor = u'/' + xor[1:]
|
|
|
|
|
base = 63
|
|
|
|
|
|
|
|
|
|
ret = u''
|
|
|
|
|
i = 0
|
|
|
|
|
L = len(xor)
|
|
|
|
|
for c1 in s:
|
|
|
|
|
val1 = B64.find(c1)
|
|
|
|
|
val2 = B64.find(xor[i])
|
|
|
|
|
i += 1
|
|
|
|
|
if i >= L:
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
|
|
if val1 < 0:
|
|
|
|
|
ret += u'='
|
|
|
|
|
else:
|
|
|
|
|
if val2 < 0:
|
|
|
|
|
val2 = base
|
|
|
|
|
i = 1
|
|
|
|
|
ret += B64[val1 ^ val2]
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def llXorBase64StringsCorrect(s, xor):
|
Change strategy for the checking of function input types.
Rather than assert that the types are correct, use the force type functions on the parameters:
ff, fk, fs, q2f, v2f, and the new fi, fl.
These functions have also been modified to ensure that the input type supports an implicit typecast to the target type and perform it, or emit ELSLInvalidType otherwise, rather than an assertion failure. fl in particular returns the original list if it isn't changed, or a copy if it is.
A couple bugs were found in testfuncs.py as a result, which have been fixed as well. A test has been added to ensure that the exception that caught these bugs remains in place.
The isxxxx functions are no longer necessary, so they are removed. Same goes for the painful cast handling process in foldconst, which was basically performing this task, and not necessarily well.
This approach is much more robust and should have been used since the beginning, but I didn't figure it out then.
2017-10-12 16:14:48 +02:00
|
|
|
s = fs(s)
|
|
|
|
|
xor = fs(xor)
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
# Xor the underlying bytes but repeating the xor parameter pattern at the first zero (SCR-35).
|
|
|
|
|
|
|
|
|
|
if xor == u'':
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
2016-06-26 22:33:10 +02:00
|
|
|
s = b64_re.search(s).group(0)
|
2014-07-26 02:43:44 +02:00
|
|
|
L1 = len(s)
|
2016-06-26 22:33:10 +02:00
|
|
|
xor = b64_re.search(xor).group(0)
|
2014-07-26 02:43:44 +02:00
|
|
|
L2 = len(xor)
|
|
|
|
|
|
|
|
|
|
if L2 == 0:
|
2015-02-11 05:43:13 +01:00
|
|
|
# The input xor string starts with zero or one valid Base64 characters.
|
|
|
|
|
# This produces garbage bytes (the first byte is zero though).
|
2017-01-19 02:58:42 +01:00
|
|
|
if L1 > 2:
|
|
|
|
|
# We don't produce a result in this case.
|
|
|
|
|
raise ELSLCantCompute
|
|
|
|
|
L2 = 2
|
|
|
|
|
xor = u'AA'
|
|
|
|
|
|
|
|
|
|
s = b64decode(s + u'=' * (-L1 & 3))
|
|
|
|
|
xor = b64decode(xor + u'=' * (-L2 & 3)) + b'\x00'
|
2014-07-26 02:43:44 +02:00
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
ret = b''
|
|
|
|
|
|
|
|
|
|
for c in s:
|
|
|
|
|
ret += chr(ord(c) ^ ord(xor[i]))
|
|
|
|
|
i += 1
|
|
|
|
|
if xor[i] == b'\x00':
|
|
|
|
|
i = 0
|
|
|
|
|
return b64encode(ret).decode('utf8')
|