xref: /aosp_15_r20/external/deqp/modules/gles3/scripts/genutil.py (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker# -*- coding: utf-8 -*-
2*35238bceSAndroid Build Coastguard Worker
3*35238bceSAndroid Build Coastguard Worker#-------------------------------------------------------------------------
4*35238bceSAndroid Build Coastguard Worker# drawElements Quality Program utilities
5*35238bceSAndroid Build Coastguard Worker# --------------------------------------
6*35238bceSAndroid Build Coastguard Worker#
7*35238bceSAndroid Build Coastguard Worker# Copyright 2016 The Android Open Source Project
8*35238bceSAndroid Build Coastguard Worker#
9*35238bceSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
10*35238bceSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
11*35238bceSAndroid Build Coastguard Worker# You may obtain a copy of the License at
12*35238bceSAndroid Build Coastguard Worker#
13*35238bceSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
14*35238bceSAndroid Build Coastguard Worker#
15*35238bceSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
16*35238bceSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
17*35238bceSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18*35238bceSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
19*35238bceSAndroid Build Coastguard Worker# limitations under the License.
20*35238bceSAndroid Build Coastguard Worker#
21*35238bceSAndroid Build Coastguard Worker#-------------------------------------------------------------------------
22*35238bceSAndroid Build Coastguard Worker
23*35238bceSAndroid Build Coastguard Workerimport re
24*35238bceSAndroid Build Coastguard Workerimport math
25*35238bceSAndroid Build Coastguard Workerimport random
26*35238bceSAndroid Build Coastguard Worker
27*35238bceSAndroid Build Coastguard WorkerPREAMBLE = """
28*35238bceSAndroid Build Coastguard Worker# WARNING: This file is auto-generated. Do NOT modify it manually, but rather
29*35238bceSAndroid Build Coastguard Worker# modify the generating script file. Otherwise changes will be lost!
30*35238bceSAndroid Build Coastguard Worker"""[1:]
31*35238bceSAndroid Build Coastguard Worker
32*35238bceSAndroid Build Coastguard Workerclass CaseGroup(object):
33*35238bceSAndroid Build Coastguard Worker    def __init__(self, name, description, children):
34*35238bceSAndroid Build Coastguard Worker        self.name = name
35*35238bceSAndroid Build Coastguard Worker        self.description = description
36*35238bceSAndroid Build Coastguard Worker        self.children = children
37*35238bceSAndroid Build Coastguard Worker
38*35238bceSAndroid Build Coastguard Workerclass ShaderCase(object):
39*35238bceSAndroid Build Coastguard Worker    def __init__(self):
40*35238bceSAndroid Build Coastguard Worker        pass
41*35238bceSAndroid Build Coastguard Worker
42*35238bceSAndroid Build Coastguard Workerg_processedCases = {}
43*35238bceSAndroid Build Coastguard Worker
44*35238bceSAndroid Build Coastguard Workerdef indentTextBlock(text, indent):
45*35238bceSAndroid Build Coastguard Worker    indentStr = indent * "\t"
46*35238bceSAndroid Build Coastguard Worker    lines = text.split("\n")
47*35238bceSAndroid Build Coastguard Worker    lines = [indentStr + line for line in lines]
48*35238bceSAndroid Build Coastguard Worker    lines = [ ["", line][line.strip() != ""] for line in lines]
49*35238bceSAndroid Build Coastguard Worker    return "\n".join(lines)
50*35238bceSAndroid Build Coastguard Worker
51*35238bceSAndroid Build Coastguard Workerdef writeCase(f, case, indent, prefix):
52*35238bceSAndroid Build Coastguard Worker    print("    %s" % (prefix + case.name))
53*35238bceSAndroid Build Coastguard Worker    if isinstance(case, CaseGroup):
54*35238bceSAndroid Build Coastguard Worker        f.write(indentTextBlock('group %s "%s"\n\n' % (case.name, case.description), indent))
55*35238bceSAndroid Build Coastguard Worker        for child in case.children:
56*35238bceSAndroid Build Coastguard Worker            writeCase(f, child, indent + 1, prefix + case.name + ".")
57*35238bceSAndroid Build Coastguard Worker        f.write(indentTextBlock("\nend # %s\n" % case.name, indent))
58*35238bceSAndroid Build Coastguard Worker    else:
59*35238bceSAndroid Build Coastguard Worker        # \todo [petri] Fix hack.
60*35238bceSAndroid Build Coastguard Worker        fullPath = prefix + case.name
61*35238bceSAndroid Build Coastguard Worker        assert (fullPath not in g_processedCases)
62*35238bceSAndroid Build Coastguard Worker        g_processedCases[fullPath] = None
63*35238bceSAndroid Build Coastguard Worker        f.write(indentTextBlock(str(case) + "\n", indent))
64*35238bceSAndroid Build Coastguard Worker
65*35238bceSAndroid Build Coastguard Workerdef writeAllCases(fileName, caseList):
66*35238bceSAndroid Build Coastguard Worker    # Write all cases to file.
67*35238bceSAndroid Build Coastguard Worker    print("  %s.." % fileName)
68*35238bceSAndroid Build Coastguard Worker    f = file(fileName, "wb")
69*35238bceSAndroid Build Coastguard Worker    f.write(PREAMBLE + "\n")
70*35238bceSAndroid Build Coastguard Worker    for case in caseList:
71*35238bceSAndroid Build Coastguard Worker        writeCase(f, case, 0, "")
72*35238bceSAndroid Build Coastguard Worker    f.close()
73*35238bceSAndroid Build Coastguard Worker
74*35238bceSAndroid Build Coastguard Worker    print("done! (%d cases written)" % len(g_processedCases))
75*35238bceSAndroid Build Coastguard Worker
76*35238bceSAndroid Build Coastguard Worker# Template operations.
77*35238bceSAndroid Build Coastguard Worker
78*35238bceSAndroid Build Coastguard Workerdef genValues(inputs, outputs):
79*35238bceSAndroid Build Coastguard Worker    res = []
80*35238bceSAndroid Build Coastguard Worker    for (name, values) in inputs:
81*35238bceSAndroid Build Coastguard Worker        res.append("input %s = [ %s ];" % (name, " | ".join([str(v) for v in values]).lower()))
82*35238bceSAndroid Build Coastguard Worker    for (name, values) in outputs:
83*35238bceSAndroid Build Coastguard Worker        res.append("output %s = [ %s ];" % (name, " | ".join([str(v) for v in values]).lower()))
84*35238bceSAndroid Build Coastguard Worker    return ("\n".join(res))
85*35238bceSAndroid Build Coastguard Worker
86*35238bceSAndroid Build Coastguard Workerdef fillTemplate(template, params):
87*35238bceSAndroid Build Coastguard Worker    s = template
88*35238bceSAndroid Build Coastguard Worker
89*35238bceSAndroid Build Coastguard Worker    for (key, value) in params.items():
90*35238bceSAndroid Build Coastguard Worker        m = re.search(r"^(\s*)\$\{\{%s\}\}$" % key, s, re.M)
91*35238bceSAndroid Build Coastguard Worker        if m is not None:
92*35238bceSAndroid Build Coastguard Worker            start = m.start(0)
93*35238bceSAndroid Build Coastguard Worker            end = m.end(0)
94*35238bceSAndroid Build Coastguard Worker            ws = m.group(1)
95*35238bceSAndroid Build Coastguard Worker            if value is not None:
96*35238bceSAndroid Build Coastguard Worker                repl = "\n".join(["%s%s" % (ws, line) for line in value.split("\n")])
97*35238bceSAndroid Build Coastguard Worker                s = s[:start] + repl + s[end:]
98*35238bceSAndroid Build Coastguard Worker            else:
99*35238bceSAndroid Build Coastguard Worker                s = s[:start] + s[end+1:] # drop the whole line
100*35238bceSAndroid Build Coastguard Worker        else:
101*35238bceSAndroid Build Coastguard Worker            s = s.replace("${{%s}}" % key, value)
102*35238bceSAndroid Build Coastguard Worker    return s
103*35238bceSAndroid Build Coastguard Worker
104*35238bceSAndroid Build Coastguard Worker# Return shuffled version of list
105*35238bceSAndroid Build Coastguard Workerdef shuffled(lst):
106*35238bceSAndroid Build Coastguard Worker    tmp = lst[:]
107*35238bceSAndroid Build Coastguard Worker    random.shuffle(tmp)
108*35238bceSAndroid Build Coastguard Worker    return tmp
109*35238bceSAndroid Build Coastguard Worker
110*35238bceSAndroid Build Coastguard Workerdef repeatToLength(lst, toLength):
111*35238bceSAndroid Build Coastguard Worker    return (toLength / len(lst)) * lst + lst[: toLength % len(lst)]
112*35238bceSAndroid Build Coastguard Worker
113*35238bceSAndroid Build Coastguard Worker# Helpers to convert a list of Scalar/Vec values into another type.
114*35238bceSAndroid Build Coastguard Worker
115*35238bceSAndroid Build Coastguard Workerdef toFloat(lst): return [Scalar(float(v.x)) for v in lst]
116*35238bceSAndroid Build Coastguard Workerdef toInt(lst): return [Scalar(int(v.x)) for v in lst]
117*35238bceSAndroid Build Coastguard Workerdef toUint(lst): return [Uint(int(v.x)) for v in lst]
118*35238bceSAndroid Build Coastguard Workerdef toBool(lst): return [Scalar(bool(v.x)) for v in lst]
119*35238bceSAndroid Build Coastguard Workerdef toVec4(lst): return [v.toFloat().toVec4() for v in lst]
120*35238bceSAndroid Build Coastguard Workerdef toVec3(lst): return [v.toFloat().toVec3() for v in lst]
121*35238bceSAndroid Build Coastguard Workerdef toVec2(lst): return [v.toFloat().toVec2() for v in lst]
122*35238bceSAndroid Build Coastguard Workerdef toIVec4(lst): return [v.toInt().toVec4() for v in lst]
123*35238bceSAndroid Build Coastguard Workerdef toIVec3(lst): return [v.toInt().toVec3() for v in lst]
124*35238bceSAndroid Build Coastguard Workerdef toIVec2(lst): return [v.toInt().toVec2() for v in lst]
125*35238bceSAndroid Build Coastguard Workerdef toBVec4(lst): return [v.toBool().toVec4() for v in lst]
126*35238bceSAndroid Build Coastguard Workerdef toBVec3(lst): return [v.toBool().toVec3() for v in lst]
127*35238bceSAndroid Build Coastguard Workerdef toBVec2(lst): return [v.toBool().toVec2() for v in lst]
128*35238bceSAndroid Build Coastguard Workerdef toUVec4(lst): return [v.toUint().toUVec4() for v in lst]
129*35238bceSAndroid Build Coastguard Workerdef toUVec3(lst): return [v.toUint().toUVec3() for v in lst]
130*35238bceSAndroid Build Coastguard Workerdef toUVec2(lst): return [v.toUint().toUVec2() for v in lst]
131*35238bceSAndroid Build Coastguard Workerdef toMat2(lst): return [v.toMat2() for v in lst]
132*35238bceSAndroid Build Coastguard Workerdef toMat2x3(lst): return [v.toMat2x3() for v in lst]
133*35238bceSAndroid Build Coastguard Workerdef toMat2x4(lst): return [v.toMat2x4() for v in lst]
134*35238bceSAndroid Build Coastguard Workerdef toMat3x2(lst): return [v.toMat3x2() for v in lst]
135*35238bceSAndroid Build Coastguard Workerdef toMat3(lst): return [v.toMat3() for v in lst]
136*35238bceSAndroid Build Coastguard Workerdef toMat3x4(lst): return [v.toMat3x4() for v in lst]
137*35238bceSAndroid Build Coastguard Workerdef toMat4x2(lst): return [v.toMat4x2() for v in lst]
138*35238bceSAndroid Build Coastguard Workerdef toMat4x3(lst): return [v.toMat4x3() for v in lst]
139*35238bceSAndroid Build Coastguard Workerdef toMat4(lst): return [v.toMat4() for v in lst]
140*35238bceSAndroid Build Coastguard Worker
141*35238bceSAndroid Build Coastguard Worker# Random value generation.
142*35238bceSAndroid Build Coastguard Worker
143*35238bceSAndroid Build Coastguard Workerclass GenRandom(object):
144*35238bceSAndroid Build Coastguard Worker    def __init__(self):
145*35238bceSAndroid Build Coastguard Worker        pass
146*35238bceSAndroid Build Coastguard Worker
147*35238bceSAndroid Build Coastguard Worker    def uniformVec4(self, count, mn, mx):
148*35238bceSAndroid Build Coastguard Worker        ret = [Vec4(random.uniform(mn, mx), random.uniform(mn, mx), random.uniform(mn, mx), random.uniform(mn, mx)) for x in xrange(count)]
149*35238bceSAndroid Build Coastguard Worker        ret[0].x = mn
150*35238bceSAndroid Build Coastguard Worker        ret[1].x = mx
151*35238bceSAndroid Build Coastguard Worker        ret[2].x = (mn + mx) * 0.5
152*35238bceSAndroid Build Coastguard Worker        return ret
153*35238bceSAndroid Build Coastguard Worker
154*35238bceSAndroid Build Coastguard Worker    def uniformBVec4(self, count):
155*35238bceSAndroid Build Coastguard Worker        ret = [Vec4(random.random() >= 0.5, random.random() >= 0.5, random.random() >= 0.5, random.random() >= 0.5) for x in xrange(count)]
156*35238bceSAndroid Build Coastguard Worker        ret[0].x = True
157*35238bceSAndroid Build Coastguard Worker        ret[1].x = False
158*35238bceSAndroid Build Coastguard Worker        return ret
159*35238bceSAndroid Build Coastguard Worker
160*35238bceSAndroid Build Coastguard Worker#    def uniform(self,
161*35238bceSAndroid Build Coastguard Worker
162*35238bceSAndroid Build Coastguard Worker# Math operating on Scalar/Vector types.
163*35238bceSAndroid Build Coastguard Worker
164*35238bceSAndroid Build Coastguard Workerdef glslSign(a): return 0.0 if (a == 0) else +1.0 if (a > 0.0) else -1.0
165*35238bceSAndroid Build Coastguard Workerdef glslMod(x, y): return x - y*math.floor(x/y)
166*35238bceSAndroid Build Coastguard Workerdef glslClamp(x, mn, mx): return mn if (x < mn) else mx if (x > mx) else x
167*35238bceSAndroid Build Coastguard Worker
168*35238bceSAndroid Build Coastguard Workerclass GenMath(object):
169*35238bceSAndroid Build Coastguard Worker    @staticmethod
170*35238bceSAndroid Build Coastguard Worker    def unary(func): return lambda val: val.applyUnary(func)
171*35238bceSAndroid Build Coastguard Worker
172*35238bceSAndroid Build Coastguard Worker    @staticmethod
173*35238bceSAndroid Build Coastguard Worker    def binary(func): return lambda a, b: (b.expandVec(a)).applyBinary(func, a.expandVec(b))
174*35238bceSAndroid Build Coastguard Worker
175*35238bceSAndroid Build Coastguard Worker    @staticmethod
176*35238bceSAndroid Build Coastguard Worker    def frac(val): return val.applyUnary(lambda x: x - math.floor(x))
177*35238bceSAndroid Build Coastguard Worker
178*35238bceSAndroid Build Coastguard Worker    @staticmethod
179*35238bceSAndroid Build Coastguard Worker    def exp2(val): return val.applyUnary(lambda x: math.pow(2.0, x))
180*35238bceSAndroid Build Coastguard Worker
181*35238bceSAndroid Build Coastguard Worker    @staticmethod
182*35238bceSAndroid Build Coastguard Worker    def log2(val): return val.applyUnary(lambda x: math.log(x, 2.0))
183*35238bceSAndroid Build Coastguard Worker
184*35238bceSAndroid Build Coastguard Worker    @staticmethod
185*35238bceSAndroid Build Coastguard Worker    def rsq(val): return val.applyUnary(lambda x: 1.0 / math.sqrt(x))
186*35238bceSAndroid Build Coastguard Worker
187*35238bceSAndroid Build Coastguard Worker    @staticmethod
188*35238bceSAndroid Build Coastguard Worker    def sign(val): return val.applyUnary(glslSign)
189*35238bceSAndroid Build Coastguard Worker
190*35238bceSAndroid Build Coastguard Worker    @staticmethod
191*35238bceSAndroid Build Coastguard Worker    def isEqual(a, b): return Scalar(a.isEqual(b))
192*35238bceSAndroid Build Coastguard Worker
193*35238bceSAndroid Build Coastguard Worker    @staticmethod
194*35238bceSAndroid Build Coastguard Worker    def isNotEqual(a, b): return Scalar(not a.isEqual(b))
195*35238bceSAndroid Build Coastguard Worker
196*35238bceSAndroid Build Coastguard Worker    @staticmethod
197*35238bceSAndroid Build Coastguard Worker    def step(a, b): return (b.expandVec(a)).applyBinary(lambda edge, x: [1.0, 0.0][x < edge], a.expandVec(b))
198*35238bceSAndroid Build Coastguard Worker
199*35238bceSAndroid Build Coastguard Worker    @staticmethod
200*35238bceSAndroid Build Coastguard Worker    def length(a): return a.length()
201*35238bceSAndroid Build Coastguard Worker
202*35238bceSAndroid Build Coastguard Worker    @staticmethod
203*35238bceSAndroid Build Coastguard Worker    def distance(a, b): return a.distance(b)
204*35238bceSAndroid Build Coastguard Worker
205*35238bceSAndroid Build Coastguard Worker    @staticmethod
206*35238bceSAndroid Build Coastguard Worker    def dot(a, b): return a.dot(b)
207*35238bceSAndroid Build Coastguard Worker
208*35238bceSAndroid Build Coastguard Worker    @staticmethod
209*35238bceSAndroid Build Coastguard Worker    def cross(a, b): return a.cross(b)
210*35238bceSAndroid Build Coastguard Worker
211*35238bceSAndroid Build Coastguard Worker    @staticmethod
212*35238bceSAndroid Build Coastguard Worker    def normalize(a): return a.normalize()
213*35238bceSAndroid Build Coastguard Worker
214*35238bceSAndroid Build Coastguard Worker    @staticmethod
215*35238bceSAndroid Build Coastguard Worker    def boolAny(a): return a.boolAny()
216*35238bceSAndroid Build Coastguard Worker
217*35238bceSAndroid Build Coastguard Worker    @staticmethod
218*35238bceSAndroid Build Coastguard Worker    def boolAll(a): return a.boolAll()
219*35238bceSAndroid Build Coastguard Worker
220*35238bceSAndroid Build Coastguard Worker    @staticmethod
221*35238bceSAndroid Build Coastguard Worker    def boolNot(a): return a.boolNot()
222*35238bceSAndroid Build Coastguard Worker
223*35238bceSAndroid Build Coastguard Worker    @staticmethod
224*35238bceSAndroid Build Coastguard Worker    def abs(a): return a.abs()
225*35238bceSAndroid Build Coastguard Worker
226*35238bceSAndroid Build Coastguard Worker# ..
227*35238bceSAndroid Build Coastguard Worker
228*35238bceSAndroid Build Coastguard Workerclass Scalar(object):
229*35238bceSAndroid Build Coastguard Worker    def __init__(self, x):
230*35238bceSAndroid Build Coastguard Worker        self.x = x
231*35238bceSAndroid Build Coastguard Worker
232*35238bceSAndroid Build Coastguard Worker    def applyUnary(self, func): return Scalar(func(self.x))
233*35238bceSAndroid Build Coastguard Worker    def applyBinary(self, func, other): return Scalar(func(self.x, other.x))
234*35238bceSAndroid Build Coastguard Worker
235*35238bceSAndroid Build Coastguard Worker    def isEqual(self, other): assert isinstance(other, Scalar); return (self.x == other.x)
236*35238bceSAndroid Build Coastguard Worker
237*35238bceSAndroid Build Coastguard Worker    def expandVec(self, val): return val
238*35238bceSAndroid Build Coastguard Worker    def toScalar(self): return Scalar(self.x)
239*35238bceSAndroid Build Coastguard Worker    def toVec2(self): return Vec2(self.x, self.x)
240*35238bceSAndroid Build Coastguard Worker    def toVec3(self): return Vec3(self.x, self.x, self.x)
241*35238bceSAndroid Build Coastguard Worker    def toVec4(self): return Vec4(self.x, self.x, self.x, self.x)
242*35238bceSAndroid Build Coastguard Worker    def toUVec2(self): return UVec2(self.x, self.x)
243*35238bceSAndroid Build Coastguard Worker    def toUVec3(self): return UVec3(self.x, self.x, self.x)
244*35238bceSAndroid Build Coastguard Worker    def toUVec4(self): return UVec4(self.x, self.x, self.x, self.x)
245*35238bceSAndroid Build Coastguard Worker    def toMat2(self): return Mat.fromScalar(2, 2, float(self.x))
246*35238bceSAndroid Build Coastguard Worker    def toMat2x3(self): return Mat.fromScalar(2, 3, float(self.x))
247*35238bceSAndroid Build Coastguard Worker    def toMat2x4(self): return Mat.fromScalar(2, 4, float(self.x))
248*35238bceSAndroid Build Coastguard Worker    def toMat3x2(self): return Mat.fromScalar(3, 2, float(self.x))
249*35238bceSAndroid Build Coastguard Worker    def toMat3(self): return Mat.fromScalar(3, 3, float(self.x))
250*35238bceSAndroid Build Coastguard Worker    def toMat3x4(self): return Mat.fromScalar(3, 4, float(self.x))
251*35238bceSAndroid Build Coastguard Worker    def toMat4x2(self): return Mat.fromScalar(4, 2, float(self.x))
252*35238bceSAndroid Build Coastguard Worker    def toMat4x3(self): return Mat.fromScalar(4, 3, float(self.x))
253*35238bceSAndroid Build Coastguard Worker    def toMat4(self): return Mat.fromScalar(4, 4, float(self.x))
254*35238bceSAndroid Build Coastguard Worker
255*35238bceSAndroid Build Coastguard Worker    def toFloat(self): return Scalar(float(self.x))
256*35238bceSAndroid Build Coastguard Worker    def toInt(self): return Scalar(int(self.x))
257*35238bceSAndroid Build Coastguard Worker    def toUint(self): return Uint(int(self.x))
258*35238bceSAndroid Build Coastguard Worker    def toBool(self): return Scalar(bool(self.x))
259*35238bceSAndroid Build Coastguard Worker
260*35238bceSAndroid Build Coastguard Worker    def getNumScalars(self): return 1
261*35238bceSAndroid Build Coastguard Worker    def getScalars(self): return [self.x]
262*35238bceSAndroid Build Coastguard Worker
263*35238bceSAndroid Build Coastguard Worker    def typeString(self):
264*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
265*35238bceSAndroid Build Coastguard Worker            return "bool"
266*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
267*35238bceSAndroid Build Coastguard Worker            return "int"
268*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
269*35238bceSAndroid Build Coastguard Worker            return "float"
270*35238bceSAndroid Build Coastguard Worker        else:
271*35238bceSAndroid Build Coastguard Worker            assert False
272*35238bceSAndroid Build Coastguard Worker
273*35238bceSAndroid Build Coastguard Worker    def vec4Swizzle(self):
274*35238bceSAndroid Build Coastguard Worker        return ""
275*35238bceSAndroid Build Coastguard Worker
276*35238bceSAndroid Build Coastguard Worker    def __str__(self):
277*35238bceSAndroid Build Coastguard Worker        return str(self.x).lower()
278*35238bceSAndroid Build Coastguard Worker
279*35238bceSAndroid Build Coastguard Worker    def __float__(self):
280*35238bceSAndroid Build Coastguard Worker        return float(self.x)
281*35238bceSAndroid Build Coastguard Worker
282*35238bceSAndroid Build Coastguard Worker    def length(self):
283*35238bceSAndroid Build Coastguard Worker        return Scalar(abs(self.x))
284*35238bceSAndroid Build Coastguard Worker
285*35238bceSAndroid Build Coastguard Worker    def distance(self, v):
286*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Scalar)
287*35238bceSAndroid Build Coastguard Worker        return Scalar(abs(self.x - v.x))
288*35238bceSAndroid Build Coastguard Worker
289*35238bceSAndroid Build Coastguard Worker    def dot(self, v):
290*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Scalar)
291*35238bceSAndroid Build Coastguard Worker        return Scalar(self.x * v.x)
292*35238bceSAndroid Build Coastguard Worker
293*35238bceSAndroid Build Coastguard Worker    def normalize(self):
294*35238bceSAndroid Build Coastguard Worker        return Scalar(glslSign(self.x))
295*35238bceSAndroid Build Coastguard Worker
296*35238bceSAndroid Build Coastguard Worker    def abs(self):
297*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
298*35238bceSAndroid Build Coastguard Worker            return Scalar(self.x)
299*35238bceSAndroid Build Coastguard Worker        else:
300*35238bceSAndroid Build Coastguard Worker            return Scalar(abs(self.x))
301*35238bceSAndroid Build Coastguard Worker
302*35238bceSAndroid Build Coastguard Worker    def __neg__(self):
303*35238bceSAndroid Build Coastguard Worker        return Scalar(-self.x)
304*35238bceSAndroid Build Coastguard Worker
305*35238bceSAndroid Build Coastguard Worker    def __add__(self, val):
306*35238bceSAndroid Build Coastguard Worker        assert isinstance(val, Scalar)
307*35238bceSAndroid Build Coastguard Worker        return Scalar(self.x + val.x)
308*35238bceSAndroid Build Coastguard Worker
309*35238bceSAndroid Build Coastguard Worker    def __sub__(self, val):
310*35238bceSAndroid Build Coastguard Worker        return self + (-val)
311*35238bceSAndroid Build Coastguard Worker
312*35238bceSAndroid Build Coastguard Worker    def __mul__(self, val):
313*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
314*35238bceSAndroid Build Coastguard Worker            return Scalar(self.x * val.x)
315*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec2):
316*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x * val.x, self.x * val.y)
317*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec3):
318*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x * val.x, self.x * val.y, self.x * val.z)
319*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec4):
320*35238bceSAndroid Build Coastguard Worker            return Vec4(self.x * val.x, self.x * val.y, self.x * val.z, self.x * val.w)
321*35238bceSAndroid Build Coastguard Worker        else:
322*35238bceSAndroid Build Coastguard Worker            assert False
323*35238bceSAndroid Build Coastguard Worker
324*35238bceSAndroid Build Coastguard Worker    def __div__(self, val):
325*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
326*35238bceSAndroid Build Coastguard Worker            return Scalar(self.x / val.x)
327*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec2):
328*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x / val.x, self.x / val.y)
329*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec3):
330*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x / val.x, self.x / val.y, self.x / val.z)
331*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec4):
332*35238bceSAndroid Build Coastguard Worker            return Vec4(self.x / val.x, self.x / val.y, self.x / val.z, self.x / val.w)
333*35238bceSAndroid Build Coastguard Worker        else:
334*35238bceSAndroid Build Coastguard Worker            assert False
335*35238bceSAndroid Build Coastguard Worker
336*35238bceSAndroid Build Coastguard Workerclass Uint(Scalar):
337*35238bceSAndroid Build Coastguard Worker    def __init__(self, x):
338*35238bceSAndroid Build Coastguard Worker        assert x >= 0
339*35238bceSAndroid Build Coastguard Worker        self.x = x
340*35238bceSAndroid Build Coastguard Worker
341*35238bceSAndroid Build Coastguard Worker    def typeString(self):
342*35238bceSAndroid Build Coastguard Worker        return "uint"
343*35238bceSAndroid Build Coastguard Worker
344*35238bceSAndroid Build Coastguard Worker    def abs(self):
345*35238bceSAndroid Build Coastguard Worker        return Scalar.abs(self).toUint()
346*35238bceSAndroid Build Coastguard Worker
347*35238bceSAndroid Build Coastguard Worker    def __neg__(self):
348*35238bceSAndroid Build Coastguard Worker        return Scalar.__neg__(self).toUint()
349*35238bceSAndroid Build Coastguard Worker
350*35238bceSAndroid Build Coastguard Worker    def __add__(self, val):
351*35238bceSAndroid Build Coastguard Worker        return Scalar.__add__(self, val).toUint()
352*35238bceSAndroid Build Coastguard Worker
353*35238bceSAndroid Build Coastguard Worker    def __sub__(self, val):
354*35238bceSAndroid Build Coastguard Worker        return self + (-val)
355*35238bceSAndroid Build Coastguard Worker
356*35238bceSAndroid Build Coastguard Worker    def __mul__(self, val):
357*35238bceSAndroid Build Coastguard Worker        return Scalar.__mul__(self, val).toUint()
358*35238bceSAndroid Build Coastguard Worker
359*35238bceSAndroid Build Coastguard Worker    def __div__(self, val):
360*35238bceSAndroid Build Coastguard Worker        return Scalar.__div__(self, val).toUint()
361*35238bceSAndroid Build Coastguard Worker
362*35238bceSAndroid Build Coastguard Workerclass Vec(object):
363*35238bceSAndroid Build Coastguard Worker    @staticmethod
364*35238bceSAndroid Build Coastguard Worker    def fromScalarList(lst):
365*35238bceSAndroid Build Coastguard Worker        assert (len(lst) >= 1 and len(lst) <= 4)
366*35238bceSAndroid Build Coastguard Worker        if (len(lst) == 1): return Scalar(lst[0])
367*35238bceSAndroid Build Coastguard Worker        elif (len(lst) == 2): return Vec2(lst[0], lst[1])
368*35238bceSAndroid Build Coastguard Worker        elif (len(lst) == 3): return Vec3(lst[0], lst[1], lst[2])
369*35238bceSAndroid Build Coastguard Worker        else: return Vec4(lst[0], lst[1], lst[2], lst[3])
370*35238bceSAndroid Build Coastguard Worker
371*35238bceSAndroid Build Coastguard Worker    def isEqual(self, other):
372*35238bceSAndroid Build Coastguard Worker        assert isinstance(other, Vec);
373*35238bceSAndroid Build Coastguard Worker        return (self.getScalars() == other.getScalars())
374*35238bceSAndroid Build Coastguard Worker
375*35238bceSAndroid Build Coastguard Worker    def length(self):
376*35238bceSAndroid Build Coastguard Worker        return Scalar(math.sqrt(self.dot(self).x))
377*35238bceSAndroid Build Coastguard Worker
378*35238bceSAndroid Build Coastguard Worker    def normalize(self):
379*35238bceSAndroid Build Coastguard Worker        return self * Scalar(1.0 / self.length().x)
380*35238bceSAndroid Build Coastguard Worker
381*35238bceSAndroid Build Coastguard Worker    def swizzle(self, indexList):
382*35238bceSAndroid Build Coastguard Worker        inScalars = self.getScalars()
383*35238bceSAndroid Build Coastguard Worker        outScalars = map(lambda ndx: inScalars[ndx], indexList)
384*35238bceSAndroid Build Coastguard Worker        return Vec.fromScalarList(outScalars)
385*35238bceSAndroid Build Coastguard Worker
386*35238bceSAndroid Build Coastguard Worker    def __init__(self):
387*35238bceSAndroid Build Coastguard Worker        pass
388*35238bceSAndroid Build Coastguard Worker
389*35238bceSAndroid Build Coastguard Worker    def __eq__(self, other):
390*35238bceSAndroid Build Coastguard Worker        return self.isEqual(other)
391*35238bceSAndroid Build Coastguard Worker
392*35238bceSAndroid Build Coastguard Worker    def __ne__(self, other):
393*35238bceSAndroid Build Coastguard Worker        return not self.isEqual(other)
394*35238bceSAndroid Build Coastguard Worker
395*35238bceSAndroid Build Coastguard Workerclass Vec2(Vec):
396*35238bceSAndroid Build Coastguard Worker    def __init__(self, x, y):
397*35238bceSAndroid Build Coastguard Worker        assert(x.__class__ == y.__class__)
398*35238bceSAndroid Build Coastguard Worker        self.x = x
399*35238bceSAndroid Build Coastguard Worker        self.y = y
400*35238bceSAndroid Build Coastguard Worker
401*35238bceSAndroid Build Coastguard Worker    def applyUnary(self, func): return Vec2(func(self.x), func(self.y))
402*35238bceSAndroid Build Coastguard Worker    def applyBinary(self, func, other): return Vec2(func(self.x, other.x), func(self.y, other.y))
403*35238bceSAndroid Build Coastguard Worker
404*35238bceSAndroid Build Coastguard Worker    def expandVec(self, val): return val.toVec2()
405*35238bceSAndroid Build Coastguard Worker    def toScalar(self): return Scalar(self.x)
406*35238bceSAndroid Build Coastguard Worker    def toVec2(self): return Vec2(self.x, self.y)
407*35238bceSAndroid Build Coastguard Worker    def toVec3(self): return Vec3(self.x, self.y, 0.0)
408*35238bceSAndroid Build Coastguard Worker    def toVec4(self): return Vec4(self.x, self.y, 0.0, 0.0)
409*35238bceSAndroid Build Coastguard Worker    def toUVec2(self): return UVec2(self.x, self.y)
410*35238bceSAndroid Build Coastguard Worker    def toUVec3(self): return UVec3(self.x, self.y, 0.0)
411*35238bceSAndroid Build Coastguard Worker    def toUVec4(self): return UVec4(self.x, self.y, 0.0, 0.0)
412*35238bceSAndroid Build Coastguard Worker    def toMat2(self): return Mat2(float(self.x), 0.0, 0.0, float(self.y));
413*35238bceSAndroid Build Coastguard Worker
414*35238bceSAndroid Build Coastguard Worker    def toFloat(self): return Vec2(float(self.x), float(self.y))
415*35238bceSAndroid Build Coastguard Worker    def toInt(self): return Vec2(int(self.x), int(self.y))
416*35238bceSAndroid Build Coastguard Worker    def toUint(self): return UVec2(int(self.x), int(self.y))
417*35238bceSAndroid Build Coastguard Worker    def toBool(self): return Vec2(bool(self.x), bool(self.y))
418*35238bceSAndroid Build Coastguard Worker
419*35238bceSAndroid Build Coastguard Worker    def getNumScalars(self): return 2
420*35238bceSAndroid Build Coastguard Worker    def getScalars(self): return [self.x, self.y]
421*35238bceSAndroid Build Coastguard Worker
422*35238bceSAndroid Build Coastguard Worker    def typeString(self):
423*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
424*35238bceSAndroid Build Coastguard Worker            return "bvec2"
425*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
426*35238bceSAndroid Build Coastguard Worker            return "ivec2"
427*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
428*35238bceSAndroid Build Coastguard Worker            return "vec2"
429*35238bceSAndroid Build Coastguard Worker        else:
430*35238bceSAndroid Build Coastguard Worker            assert False
431*35238bceSAndroid Build Coastguard Worker
432*35238bceSAndroid Build Coastguard Worker    def vec4Swizzle(self):
433*35238bceSAndroid Build Coastguard Worker        return ".xyxy"
434*35238bceSAndroid Build Coastguard Worker
435*35238bceSAndroid Build Coastguard Worker    def __str__(self):
436*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
437*35238bceSAndroid Build Coastguard Worker            return "bvec2(%s, %s)" % (str(self.x).lower(), str(self.y).lower())
438*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
439*35238bceSAndroid Build Coastguard Worker            return "ivec2(%i, %i)" % (self.x, self.y)
440*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
441*35238bceSAndroid Build Coastguard Worker            return "vec2(%s, %s)" % (self.x, self.y)
442*35238bceSAndroid Build Coastguard Worker        else:
443*35238bceSAndroid Build Coastguard Worker            assert False
444*35238bceSAndroid Build Coastguard Worker
445*35238bceSAndroid Build Coastguard Worker    def distance(self, v):
446*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec2)
447*35238bceSAndroid Build Coastguard Worker        return (self - v).length()
448*35238bceSAndroid Build Coastguard Worker
449*35238bceSAndroid Build Coastguard Worker    def dot(self, v):
450*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec2)
451*35238bceSAndroid Build Coastguard Worker        return Scalar(self.x*v.x + self.y*v.y)
452*35238bceSAndroid Build Coastguard Worker
453*35238bceSAndroid Build Coastguard Worker    def abs(self):
454*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
455*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x, self.y)
456*35238bceSAndroid Build Coastguard Worker        else:
457*35238bceSAndroid Build Coastguard Worker            return Vec2(abs(self.x), abs(self.y))
458*35238bceSAndroid Build Coastguard Worker
459*35238bceSAndroid Build Coastguard Worker    def __neg__(self):
460*35238bceSAndroid Build Coastguard Worker        return Vec2(-self.x, -self.y)
461*35238bceSAndroid Build Coastguard Worker
462*35238bceSAndroid Build Coastguard Worker    def __add__(self, val):
463*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
464*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x + val, self.y + val)
465*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec2):
466*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x + val.x, self.y + val.y)
467*35238bceSAndroid Build Coastguard Worker        else:
468*35238bceSAndroid Build Coastguard Worker            assert False
469*35238bceSAndroid Build Coastguard Worker
470*35238bceSAndroid Build Coastguard Worker    def __sub__(self, val):
471*35238bceSAndroid Build Coastguard Worker        return self + (-val)
472*35238bceSAndroid Build Coastguard Worker
473*35238bceSAndroid Build Coastguard Worker    def __mul__(self, val):
474*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
475*35238bceSAndroid Build Coastguard Worker            val = val.toVec2()
476*35238bceSAndroid Build Coastguard Worker        assert isinstance(val, Vec2)
477*35238bceSAndroid Build Coastguard Worker        return Vec2(self.x * val.x, self.y * val.y)
478*35238bceSAndroid Build Coastguard Worker
479*35238bceSAndroid Build Coastguard Worker    def __div__(self, val):
480*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
481*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x / val.x, self.y / val.x)
482*35238bceSAndroid Build Coastguard Worker        else:
483*35238bceSAndroid Build Coastguard Worker            assert isinstance(val, Vec2)
484*35238bceSAndroid Build Coastguard Worker            return Vec2(self.x / val.x, self.y / val.y)
485*35238bceSAndroid Build Coastguard Worker
486*35238bceSAndroid Build Coastguard Worker    def boolAny(self): return Scalar(self.x or self.y)
487*35238bceSAndroid Build Coastguard Worker    def boolAll(self): return Scalar(self.x and self.y)
488*35238bceSAndroid Build Coastguard Worker    def boolNot(self): return Vec2(not self.x, not self.y)
489*35238bceSAndroid Build Coastguard Worker
490*35238bceSAndroid Build Coastguard Workerclass UVec2(Vec2):
491*35238bceSAndroid Build Coastguard Worker    def __init__(self, x, y):
492*35238bceSAndroid Build Coastguard Worker        assert isinstance(x, int) and isinstance(y, int)
493*35238bceSAndroid Build Coastguard Worker        assert x >= 0 and y >= 0
494*35238bceSAndroid Build Coastguard Worker        Vec2.__init__(self, x, y)
495*35238bceSAndroid Build Coastguard Worker
496*35238bceSAndroid Build Coastguard Worker    def typeString(self):
497*35238bceSAndroid Build Coastguard Worker        return "uvec2"
498*35238bceSAndroid Build Coastguard Worker
499*35238bceSAndroid Build Coastguard Worker    def __str__(self):
500*35238bceSAndroid Build Coastguard Worker        return "uvec2(%i, %i)" % (self.x, self.y)
501*35238bceSAndroid Build Coastguard Worker
502*35238bceSAndroid Build Coastguard Worker    def abs(self):
503*35238bceSAndroid Build Coastguard Worker        return Vec2.abs(self).toUint()
504*35238bceSAndroid Build Coastguard Worker
505*35238bceSAndroid Build Coastguard Workerclass Vec3(Vec):
506*35238bceSAndroid Build Coastguard Worker    def __init__(self, x, y, z):
507*35238bceSAndroid Build Coastguard Worker        assert((x.__class__ == y.__class__) and (x.__class__ == z.__class__))
508*35238bceSAndroid Build Coastguard Worker        self.x = x
509*35238bceSAndroid Build Coastguard Worker        self.y = y
510*35238bceSAndroid Build Coastguard Worker        self.z = z
511*35238bceSAndroid Build Coastguard Worker
512*35238bceSAndroid Build Coastguard Worker    def applyUnary(self, func): return Vec3(func(self.x), func(self.y), func(self.z))
513*35238bceSAndroid Build Coastguard Worker    def applyBinary(self, func, other): return Vec3(func(self.x, other.x), func(self.y, other.y), func(self.z, other.z))
514*35238bceSAndroid Build Coastguard Worker
515*35238bceSAndroid Build Coastguard Worker    def expandVec(self, val): return val.toVec3()
516*35238bceSAndroid Build Coastguard Worker    def toScalar(self): return Scalar(self.x)
517*35238bceSAndroid Build Coastguard Worker    def toVec2(self): return Vec2(self.x, self.y)
518*35238bceSAndroid Build Coastguard Worker    def toVec3(self): return Vec3(self.x, self.y, self.z)
519*35238bceSAndroid Build Coastguard Worker    def toVec4(self): return Vec4(self.x, self.y, self.z, 0.0)
520*35238bceSAndroid Build Coastguard Worker    def toUVec2(self): return UVec2(self.x, self.y)
521*35238bceSAndroid Build Coastguard Worker    def toUVec3(self): return UVec3(self.x, self.y, self.z)
522*35238bceSAndroid Build Coastguard Worker    def toUVec4(self): return UVec4(self.x, self.y, self.z, 0.0)
523*35238bceSAndroid Build Coastguard Worker    def toMat3(self): return Mat3(float(self.x), 0.0, 0.0,  0.0, float(self.y), 0.0,  0.0, 0.0, float(self.z));
524*35238bceSAndroid Build Coastguard Worker
525*35238bceSAndroid Build Coastguard Worker    def toFloat(self): return Vec3(float(self.x), float(self.y), float(self.z))
526*35238bceSAndroid Build Coastguard Worker    def toInt(self): return Vec3(int(self.x), int(self.y), int(self.z))
527*35238bceSAndroid Build Coastguard Worker    def toUint(self): return UVec3(int(self.x), int(self.y), int(self.z))
528*35238bceSAndroid Build Coastguard Worker    def toBool(self): return Vec3(bool(self.x), bool(self.y), bool(self.z))
529*35238bceSAndroid Build Coastguard Worker
530*35238bceSAndroid Build Coastguard Worker    def getNumScalars(self): return 3
531*35238bceSAndroid Build Coastguard Worker    def getScalars(self): return [self.x, self.y, self.z]
532*35238bceSAndroid Build Coastguard Worker
533*35238bceSAndroid Build Coastguard Worker    def typeString(self):
534*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
535*35238bceSAndroid Build Coastguard Worker            return "bvec3"
536*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
537*35238bceSAndroid Build Coastguard Worker            return "ivec3"
538*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
539*35238bceSAndroid Build Coastguard Worker            return "vec3"
540*35238bceSAndroid Build Coastguard Worker        else:
541*35238bceSAndroid Build Coastguard Worker            assert False
542*35238bceSAndroid Build Coastguard Worker
543*35238bceSAndroid Build Coastguard Worker    def vec4Swizzle(self):
544*35238bceSAndroid Build Coastguard Worker        return ".xyzx"
545*35238bceSAndroid Build Coastguard Worker
546*35238bceSAndroid Build Coastguard Worker    def __str__(self):
547*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
548*35238bceSAndroid Build Coastguard Worker            return "bvec3(%s, %s, %s)" % (str(self.x).lower(), str(self.y).lower(), str(self.z).lower())
549*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
550*35238bceSAndroid Build Coastguard Worker            return "ivec3(%i, %i, %i)" % (self.x, self.y, self.z)
551*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
552*35238bceSAndroid Build Coastguard Worker            return "vec3(%s, %s, %s)" % (self.x, self.y, self.z)
553*35238bceSAndroid Build Coastguard Worker        else:
554*35238bceSAndroid Build Coastguard Worker            assert False
555*35238bceSAndroid Build Coastguard Worker
556*35238bceSAndroid Build Coastguard Worker    def distance(self, v):
557*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec3)
558*35238bceSAndroid Build Coastguard Worker        return (self - v).length()
559*35238bceSAndroid Build Coastguard Worker
560*35238bceSAndroid Build Coastguard Worker    def dot(self, v):
561*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec3)
562*35238bceSAndroid Build Coastguard Worker        return Scalar(self.x*v.x + self.y*v.y + self.z*v.z)
563*35238bceSAndroid Build Coastguard Worker
564*35238bceSAndroid Build Coastguard Worker    def cross(self, v):
565*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec3)
566*35238bceSAndroid Build Coastguard Worker        return Vec3(self.y*v.z - v.y*self.z,
567*35238bceSAndroid Build Coastguard Worker                    self.z*v.x - v.z*self.x,
568*35238bceSAndroid Build Coastguard Worker                    self.x*v.y - v.x*self.y)
569*35238bceSAndroid Build Coastguard Worker
570*35238bceSAndroid Build Coastguard Worker    def abs(self):
571*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
572*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x, self.y, self.z)
573*35238bceSAndroid Build Coastguard Worker        else:
574*35238bceSAndroid Build Coastguard Worker            return Vec3(abs(self.x), abs(self.y), abs(self.z))
575*35238bceSAndroid Build Coastguard Worker
576*35238bceSAndroid Build Coastguard Worker    def __neg__(self):
577*35238bceSAndroid Build Coastguard Worker        return Vec3(-self.x, -self.y, -self.z)
578*35238bceSAndroid Build Coastguard Worker
579*35238bceSAndroid Build Coastguard Worker    def __add__(self, val):
580*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
581*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x + val, self.y + val)
582*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec3):
583*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x + val.x, self.y + val.y, self.z + val.z)
584*35238bceSAndroid Build Coastguard Worker        else:
585*35238bceSAndroid Build Coastguard Worker            assert False
586*35238bceSAndroid Build Coastguard Worker
587*35238bceSAndroid Build Coastguard Worker    def __sub__(self, val):
588*35238bceSAndroid Build Coastguard Worker        return self + (-val)
589*35238bceSAndroid Build Coastguard Worker
590*35238bceSAndroid Build Coastguard Worker    def __mul__(self, val):
591*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
592*35238bceSAndroid Build Coastguard Worker            val = val.toVec3()
593*35238bceSAndroid Build Coastguard Worker        assert isinstance(val, Vec3)
594*35238bceSAndroid Build Coastguard Worker        return Vec3(self.x * val.x, self.y * val.y, self.z * val.z)
595*35238bceSAndroid Build Coastguard Worker
596*35238bceSAndroid Build Coastguard Worker    def __div__(self, val):
597*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
598*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x / val.x, self.y / val.x, self.z / val.x)
599*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec3):
600*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x / val.x, self.y / val.y, self.z / val.z)
601*35238bceSAndroid Build Coastguard Worker        else:
602*35238bceSAndroid Build Coastguard Worker            assert False
603*35238bceSAndroid Build Coastguard Worker
604*35238bceSAndroid Build Coastguard Worker    def boolAny(self): return Scalar(self.x or self.y or self.z)
605*35238bceSAndroid Build Coastguard Worker    def boolAll(self): return Scalar(self.x and self.y and self.z)
606*35238bceSAndroid Build Coastguard Worker    def boolNot(self): return Vec3(not self.x, not self.y, not self.z)
607*35238bceSAndroid Build Coastguard Worker
608*35238bceSAndroid Build Coastguard Workerclass UVec3(Vec3):
609*35238bceSAndroid Build Coastguard Worker    def __init__(self, x, y, z):
610*35238bceSAndroid Build Coastguard Worker        assert isinstance(x, int) and isinstance(y, int) and isinstance(z, int)
611*35238bceSAndroid Build Coastguard Worker        assert x >= 0 and y >= 0 and z >= 0
612*35238bceSAndroid Build Coastguard Worker        Vec3.__init__(self, x, y, z)
613*35238bceSAndroid Build Coastguard Worker
614*35238bceSAndroid Build Coastguard Worker    def typeString(self):
615*35238bceSAndroid Build Coastguard Worker        return "uvec3"
616*35238bceSAndroid Build Coastguard Worker
617*35238bceSAndroid Build Coastguard Worker    def __str__(self):
618*35238bceSAndroid Build Coastguard Worker        return "uvec3(%i, %i, %i)" % (self.x, self.y, self.z)
619*35238bceSAndroid Build Coastguard Worker
620*35238bceSAndroid Build Coastguard Worker    def abs(self):
621*35238bceSAndroid Build Coastguard Worker        return Vec3.abs(self).toUint()
622*35238bceSAndroid Build Coastguard Worker
623*35238bceSAndroid Build Coastguard Workerclass Vec4(Vec):
624*35238bceSAndroid Build Coastguard Worker    def __init__(self, x, y, z, w):
625*35238bceSAndroid Build Coastguard Worker        assert((x.__class__ == y.__class__) and (x.__class__ == z.__class__) and (x.__class__ == w.__class__))
626*35238bceSAndroid Build Coastguard Worker        self.x = x
627*35238bceSAndroid Build Coastguard Worker        self.y = y
628*35238bceSAndroid Build Coastguard Worker        self.z = z
629*35238bceSAndroid Build Coastguard Worker        self.w = w
630*35238bceSAndroid Build Coastguard Worker
631*35238bceSAndroid Build Coastguard Worker    def applyUnary(self, func): return Vec4(func(self.x), func(self.y), func(self.z), func(self.w))
632*35238bceSAndroid Build Coastguard Worker    def applyBinary(self, func, other): return Vec4(func(self.x, other.x), func(self.y, other.y), func(self.z, other.z), func(self.w, other.w))
633*35238bceSAndroid Build Coastguard Worker
634*35238bceSAndroid Build Coastguard Worker    def expandVec(self, val): return val.toVec4()
635*35238bceSAndroid Build Coastguard Worker    def toScalar(self): return Scalar(self.x)
636*35238bceSAndroid Build Coastguard Worker    def toVec2(self): return Vec2(self.x, self.y)
637*35238bceSAndroid Build Coastguard Worker    def toVec3(self): return Vec3(self.x, self.y, self.z)
638*35238bceSAndroid Build Coastguard Worker    def toVec4(self): return Vec4(self.x, self.y, self.z, self.w)
639*35238bceSAndroid Build Coastguard Worker    def toUVec2(self): return UVec2(self.x, self.y)
640*35238bceSAndroid Build Coastguard Worker    def toUVec3(self): return UVec3(self.x, self.y, self.z)
641*35238bceSAndroid Build Coastguard Worker    def toUVec4(self): return UVec4(self.x, self.y, self.z, self.w)
642*35238bceSAndroid Build Coastguard Worker    def toMat2(self): return Mat2(float(self.x), float(self.y), float(self.z), float(self.w))
643*35238bceSAndroid Build Coastguard Worker    def toMat4(self): return Mat4(float(self.x), 0.0, 0.0, 0.0,  0.0, float(self.y), 0.0, 0.0,  0.0, 0.0, float(self.z), 0.0,  0.0, 0.0, 0.0, float(self.w));
644*35238bceSAndroid Build Coastguard Worker
645*35238bceSAndroid Build Coastguard Worker    def toFloat(self): return Vec4(float(self.x), float(self.y), float(self.z), float(self.w))
646*35238bceSAndroid Build Coastguard Worker    def toInt(self): return Vec4(int(self.x), int(self.y), int(self.z), int(self.w))
647*35238bceSAndroid Build Coastguard Worker    def toUint(self): return UVec4(int(self.x), int(self.y), int(self.z), int(self.w))
648*35238bceSAndroid Build Coastguard Worker    def toBool(self): return Vec4(bool(self.x), bool(self.y), bool(self.z), bool(self.w))
649*35238bceSAndroid Build Coastguard Worker
650*35238bceSAndroid Build Coastguard Worker    def getNumScalars(self): return 4
651*35238bceSAndroid Build Coastguard Worker    def getScalars(self): return [self.x, self.y, self.z, self.w]
652*35238bceSAndroid Build Coastguard Worker
653*35238bceSAndroid Build Coastguard Worker    def typeString(self):
654*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
655*35238bceSAndroid Build Coastguard Worker            return "bvec4"
656*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
657*35238bceSAndroid Build Coastguard Worker            return "ivec4"
658*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
659*35238bceSAndroid Build Coastguard Worker            return "vec4"
660*35238bceSAndroid Build Coastguard Worker        else:
661*35238bceSAndroid Build Coastguard Worker            assert False
662*35238bceSAndroid Build Coastguard Worker
663*35238bceSAndroid Build Coastguard Worker    def vec4Swizzle(self):
664*35238bceSAndroid Build Coastguard Worker        return ""
665*35238bceSAndroid Build Coastguard Worker
666*35238bceSAndroid Build Coastguard Worker    def __str__(self):
667*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
668*35238bceSAndroid Build Coastguard Worker            return "bvec4(%s, %s, %s, %s)" % (str(self.x).lower(), str(self.y).lower(), str(self.z).lower(), str(self.w).lower())
669*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, int):
670*35238bceSAndroid Build Coastguard Worker            return "ivec4(%i, %i, %i, %i)" % (self.x, self.y, self.z, self.w)
671*35238bceSAndroid Build Coastguard Worker        elif isinstance(self.x, float):
672*35238bceSAndroid Build Coastguard Worker            return "vec4(%s, %s, %s, %s)" % (self.x, self.y, self.z, self.w)
673*35238bceSAndroid Build Coastguard Worker        else:
674*35238bceSAndroid Build Coastguard Worker            assert False
675*35238bceSAndroid Build Coastguard Worker
676*35238bceSAndroid Build Coastguard Worker    def distance(self, v):
677*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec4)
678*35238bceSAndroid Build Coastguard Worker        return (self - v).length()
679*35238bceSAndroid Build Coastguard Worker
680*35238bceSAndroid Build Coastguard Worker    def dot(self, v):
681*35238bceSAndroid Build Coastguard Worker        assert isinstance(v, Vec4)
682*35238bceSAndroid Build Coastguard Worker        return Scalar(self.x*v.x + self.y*v.y + self.z*v.z + self.w*v.w)
683*35238bceSAndroid Build Coastguard Worker
684*35238bceSAndroid Build Coastguard Worker    def abs(self):
685*35238bceSAndroid Build Coastguard Worker        if isinstance(self.x, bool):
686*35238bceSAndroid Build Coastguard Worker            return Vec4(self.x, self.y, self.z, self.w)
687*35238bceSAndroid Build Coastguard Worker        else:
688*35238bceSAndroid Build Coastguard Worker            return Vec4(abs(self.x), abs(self.y), abs(self.z), abs(self.w))
689*35238bceSAndroid Build Coastguard Worker
690*35238bceSAndroid Build Coastguard Worker    def __neg__(self):
691*35238bceSAndroid Build Coastguard Worker        return Vec4(-self.x, -self.y, -self.z, -self.w)
692*35238bceSAndroid Build Coastguard Worker
693*35238bceSAndroid Build Coastguard Worker    def __add__(self, val):
694*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
695*35238bceSAndroid Build Coastguard Worker            return Vec3(self.x + val, self.y + val)
696*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec4):
697*35238bceSAndroid Build Coastguard Worker            return Vec4(self.x + val.x, self.y + val.y, self.z + val.z, self.w + val.w)
698*35238bceSAndroid Build Coastguard Worker        else:
699*35238bceSAndroid Build Coastguard Worker            assert False
700*35238bceSAndroid Build Coastguard Worker
701*35238bceSAndroid Build Coastguard Worker    def __sub__(self, val):
702*35238bceSAndroid Build Coastguard Worker        return self + (-val)
703*35238bceSAndroid Build Coastguard Worker
704*35238bceSAndroid Build Coastguard Worker    def __mul__(self, val):
705*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
706*35238bceSAndroid Build Coastguard Worker            val = val.toVec4()
707*35238bceSAndroid Build Coastguard Worker        assert isinstance(val, Vec4)
708*35238bceSAndroid Build Coastguard Worker        return Vec4(self.x * val.x, self.y * val.y, self.z * val.z, self.w * val.w)
709*35238bceSAndroid Build Coastguard Worker
710*35238bceSAndroid Build Coastguard Worker    def __div__(self, val):
711*35238bceSAndroid Build Coastguard Worker        if isinstance(val, Scalar):
712*35238bceSAndroid Build Coastguard Worker            return Vec4(self.x / val.x, self.y / val.x, self.z / val.x, self.w / val.x)
713*35238bceSAndroid Build Coastguard Worker        elif isinstance(val, Vec4):
714*35238bceSAndroid Build Coastguard Worker            return Vec4(self.x / val.x, self.y / val.y, self.z / val.z, self.w / val.w)
715*35238bceSAndroid Build Coastguard Worker        else:
716*35238bceSAndroid Build Coastguard Worker            assert False
717*35238bceSAndroid Build Coastguard Worker
718*35238bceSAndroid Build Coastguard Worker    def boolAny(self): return Scalar(self.x or self.y or self.z or self.w)
719*35238bceSAndroid Build Coastguard Worker    def boolAll(self): return Scalar(self.x and self.y and self.z and self.w)
720*35238bceSAndroid Build Coastguard Worker    def boolNot(self): return Vec4(not self.x, not self.y, not self.z, not self.w)
721*35238bceSAndroid Build Coastguard Worker
722*35238bceSAndroid Build Coastguard Workerclass UVec4(Vec4):
723*35238bceSAndroid Build Coastguard Worker    def __init__(self, x, y, z, w):
724*35238bceSAndroid Build Coastguard Worker        assert isinstance(x, int) and isinstance(y, int) and isinstance(z, int) and isinstance(w, int)
725*35238bceSAndroid Build Coastguard Worker        assert x >= 0 and y >= 0 and z >= 0 and w >= 0
726*35238bceSAndroid Build Coastguard Worker        Vec4.__init__(self, x, y, z, w)
727*35238bceSAndroid Build Coastguard Worker
728*35238bceSAndroid Build Coastguard Worker    def typeString(self):
729*35238bceSAndroid Build Coastguard Worker        return "uvec4"
730*35238bceSAndroid Build Coastguard Worker
731*35238bceSAndroid Build Coastguard Worker    def __str__(self):
732*35238bceSAndroid Build Coastguard Worker        return "uvec4(%i, %i, %i, %i)" % (self.x, self.y, self.z, self.w)
733*35238bceSAndroid Build Coastguard Worker
734*35238bceSAndroid Build Coastguard Worker    def abs(self):
735*35238bceSAndroid Build Coastguard Worker        return Vec4.abs(self).toUint()
736*35238bceSAndroid Build Coastguard Worker
737*35238bceSAndroid Build Coastguard Worker# \note Column-major storage.
738*35238bceSAndroid Build Coastguard Workerclass Mat(object):
739*35238bceSAndroid Build Coastguard Worker    def __init__ (self, numCols, numRows, scalars):
740*35238bceSAndroid Build Coastguard Worker        assert len(scalars) == numRows*numCols
741*35238bceSAndroid Build Coastguard Worker        self.numCols = numCols
742*35238bceSAndroid Build Coastguard Worker        self.numRows = numRows
743*35238bceSAndroid Build Coastguard Worker        self.scalars = scalars
744*35238bceSAndroid Build Coastguard Worker
745*35238bceSAndroid Build Coastguard Worker    @staticmethod
746*35238bceSAndroid Build Coastguard Worker    def fromScalar (numCols, numRows, scalar):
747*35238bceSAndroid Build Coastguard Worker        scalars = []
748*35238bceSAndroid Build Coastguard Worker        for col in range(0, numCols):
749*35238bceSAndroid Build Coastguard Worker            for row in range(0, numRows):
750*35238bceSAndroid Build Coastguard Worker                scalars.append(scalar if col == row else 0.0)
751*35238bceSAndroid Build Coastguard Worker        return Mat(numCols, numRows, scalars)
752*35238bceSAndroid Build Coastguard Worker
753*35238bceSAndroid Build Coastguard Worker    @staticmethod
754*35238bceSAndroid Build Coastguard Worker    def identity (numCols, numRows):
755*35238bceSAndroid Build Coastguard Worker        return Mat.fromScalar(numCols, numRows, 1.0)
756*35238bceSAndroid Build Coastguard Worker
757*35238bceSAndroid Build Coastguard Worker    def get (self, colNdx, rowNdx):
758*35238bceSAndroid Build Coastguard Worker        assert 0 <= colNdx and colNdx < self.numCols
759*35238bceSAndroid Build Coastguard Worker        assert 0 <= rowNdx and rowNdx < self.numRows
760*35238bceSAndroid Build Coastguard Worker        return self.scalars[colNdx*self.numRows + rowNdx]
761*35238bceSAndroid Build Coastguard Worker
762*35238bceSAndroid Build Coastguard Worker    def set (self, colNdx, rowNdx, scalar):
763*35238bceSAndroid Build Coastguard Worker        assert 0 <= colNdx and colNdx < self.numCols
764*35238bceSAndroid Build Coastguard Worker        assert 0 <= rowNdx and rowNdx < self.numRows
765*35238bceSAndroid Build Coastguard Worker        self.scalars[colNdx*self.numRows + rowNdx] = scalar
766*35238bceSAndroid Build Coastguard Worker
767*35238bceSAndroid Build Coastguard Worker    def toMatrix (self, numCols, numRows):
768*35238bceSAndroid Build Coastguard Worker        res = Mat.identity(numCols, numRows)
769*35238bceSAndroid Build Coastguard Worker        for col in range(0, min(self.numCols, numCols)):
770*35238bceSAndroid Build Coastguard Worker            for row in range(0, min(self.numRows, numRows)):
771*35238bceSAndroid Build Coastguard Worker                res.set(col, row, self.get(col, row))
772*35238bceSAndroid Build Coastguard Worker        return res
773*35238bceSAndroid Build Coastguard Worker
774*35238bceSAndroid Build Coastguard Worker    def toMat2 (self): return self.toMatrix(2, 2)
775*35238bceSAndroid Build Coastguard Worker    def toMat2x3 (self): return self.toMatrix(2, 3)
776*35238bceSAndroid Build Coastguard Worker    def toMat2x4 (self): return self.toMatrix(2, 4)
777*35238bceSAndroid Build Coastguard Worker    def toMat3x2 (self): return self.toMatrix(3, 2)
778*35238bceSAndroid Build Coastguard Worker    def toMat3 (self): return self.toMatrix(3, 3)
779*35238bceSAndroid Build Coastguard Worker    def toMat3x4 (self): return self.toMatrix(3, 4)
780*35238bceSAndroid Build Coastguard Worker    def toMat4x2 (self): return self.toMatrix(4, 2)
781*35238bceSAndroid Build Coastguard Worker    def toMat4x3 (self): return self.toMatrix(4, 3)
782*35238bceSAndroid Build Coastguard Worker    def toMat4 (self): return self.toMatrix(4, 4)
783*35238bceSAndroid Build Coastguard Worker
784*35238bceSAndroid Build Coastguard Worker    def typeString(self):
785*35238bceSAndroid Build Coastguard Worker        if self.numRows == self.numCols:
786*35238bceSAndroid Build Coastguard Worker            return "mat%d" % self.numRows
787*35238bceSAndroid Build Coastguard Worker        else:
788*35238bceSAndroid Build Coastguard Worker            return "mat%dx%d" % (self.numCols, self.numRows)
789*35238bceSAndroid Build Coastguard Worker
790*35238bceSAndroid Build Coastguard Worker    def __str__(self):
791*35238bceSAndroid Build Coastguard Worker        return "%s(%s)" % (self.typeString(), ", ".join(["%s" % s for s in self.scalars]))
792*35238bceSAndroid Build Coastguard Worker
793*35238bceSAndroid Build Coastguard Worker    def isTypeEqual (self, other):
794*35238bceSAndroid Build Coastguard Worker        return isinstance(other, Mat) and self.numRows == other.numRows and self.numCols == other.numCols
795*35238bceSAndroid Build Coastguard Worker
796*35238bceSAndroid Build Coastguard Worker    def isEqual(self, other):
797*35238bceSAndroid Build Coastguard Worker        assert self.isTypeEqual(other)
798*35238bceSAndroid Build Coastguard Worker        return (self.scalars == other.scalars)
799*35238bceSAndroid Build Coastguard Worker
800*35238bceSAndroid Build Coastguard Worker    def compMul(self, val):
801*35238bceSAndroid Build Coastguard Worker        assert self.isTypeEqual(val)
802*35238bceSAndroid Build Coastguard Worker        return Mat(self.numRows, self.numCols, [self.scalars(i) * val.scalars(i) for i in range(self.numRows*self.numCols)])
803*35238bceSAndroid Build Coastguard Worker
804*35238bceSAndroid Build Coastguard Workerclass Mat2(Mat):
805*35238bceSAndroid Build Coastguard Worker    def __init__(self, m00, m01, m10, m11):
806*35238bceSAndroid Build Coastguard Worker        Mat.__init__(self, 2, 2, [m00, m10, m01, m11])
807*35238bceSAndroid Build Coastguard Worker
808*35238bceSAndroid Build Coastguard Workerclass Mat3(Mat):
809*35238bceSAndroid Build Coastguard Worker    def __init__(self, m00, m01, m02, m10, m11, m12, m20, m21, m22):
810*35238bceSAndroid Build Coastguard Worker        Mat.__init__(self, 3, 3, [m00, m10, m20,
811*35238bceSAndroid Build Coastguard Worker                                  m01, m11, m21,
812*35238bceSAndroid Build Coastguard Worker                                  m02, m12, m22])
813*35238bceSAndroid Build Coastguard Worker
814*35238bceSAndroid Build Coastguard Workerclass Mat4(Mat):
815*35238bceSAndroid Build Coastguard Worker    def __init__(self, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33):
816*35238bceSAndroid Build Coastguard Worker        Mat.__init__(self, 4, 4, [m00, m10, m20, m30,
817*35238bceSAndroid Build Coastguard Worker                                  m01, m11, m21, m31,
818*35238bceSAndroid Build Coastguard Worker                                  m02, m12, m22, m32,
819*35238bceSAndroid Build Coastguard Worker                                  m03, m13, m23, m33])
820