|
| 1 | +from dataclasses import dataclass |
| 2 | +from math import sqrt |
| 3 | +from typing import Sequence |
| 4 | + |
| 5 | + |
| 6 | +kEpsilon = 0.00001 |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
1 | 10 | class Vector4:
|
2 |
| - X: float |
3 |
| - Y: float |
4 |
| - Z: float |
5 |
| - W: float |
| 11 | + '''https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector4.cs''' |
| 12 | + |
| 13 | + X: float = 0.0 |
| 14 | + Y: float = 0.0 |
| 15 | + Z: float = 0.0 |
| 16 | + W: float = 0.0 |
6 | 17 |
|
7 | 18 | def __init__(self, *args):
|
8 |
| - if len(args) == 4: # float x, float y, float z, float w |
9 |
| - self.X = args[0] |
10 |
| - self.Y = args[1] |
11 |
| - self.Z = args[2] |
12 |
| - self.W = args[3] |
13 |
| - elif len(args) == 2: # Vector3 value, float w |
14 |
| - self.X = args[0].X |
15 |
| - self.Y = args[0].Y |
16 |
| - self.Z = args[0].Z |
17 |
| - self.W = args[1] |
18 |
| - |
19 |
| - """ |
20 |
| - public float this[int index] |
21 |
| - { |
22 |
| - get |
23 |
| - { |
24 |
| - switch (index) |
25 |
| - { |
26 |
| - case 0: return X; |
27 |
| - case 1: return Y; |
28 |
| - case 2: return Z; |
29 |
| - case 3: return W; |
30 |
| - default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector4 index!"); |
31 |
| - } |
32 |
| - } |
33 |
| -
|
34 |
| - set |
35 |
| - { |
36 |
| - switch (index) |
37 |
| - { |
38 |
| - case 0: X = value; break; |
39 |
| - case 1: Y = value; break; |
40 |
| - case 2: Z = value; break; |
41 |
| - case 3: W = value; break; |
42 |
| - default: throw new ArgumentOutOfRangeException(nameof(index), "Invalid Vector4 index!"); |
43 |
| - } |
44 |
| - } |
45 |
| - } |
46 |
| -
|
47 |
| -
|
48 |
| - public override int GetHashCode() |
49 |
| - { |
50 |
| - return X.GetHashCode() ^ (Y.GetHashCode() << 2) ^ (Z.GetHashCode() >> 2) ^ (W.GetHashCode() >> 1); |
51 |
| - } |
52 |
| -
|
53 |
| - public override bool Equals(object other) |
54 |
| - { |
55 |
| - if (!(other is Vector4)) |
56 |
| - return false; |
57 |
| - return Equals((Vector4)other); |
58 |
| - } |
59 |
| -
|
60 |
| - public bool Equals(Vector4 other) |
61 |
| - { |
62 |
| - return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z) && W.Equals(other.W); |
63 |
| - } |
64 |
| -
|
65 |
| - public void Normalize() |
66 |
| - { |
67 |
| - var length = Length(); |
68 |
| - if (length > kEpsilon) |
69 |
| - { |
70 |
| - var invNorm = 1.0f / length; |
71 |
| - X *= invNorm; |
72 |
| - Y *= invNorm; |
73 |
| - Z *= invNorm; |
74 |
| - W *= invNorm; |
75 |
| - } |
76 |
| - else |
77 |
| - { |
78 |
| - X = 0; |
79 |
| - Y = 0; |
80 |
| - Z = 0; |
81 |
| - W = 0; |
82 |
| - } |
83 |
| - } |
84 |
| -
|
85 |
| - public float Length() |
86 |
| - { |
87 |
| - return (float)Math.Sqrt(LengthSquared()); |
88 |
| - } |
89 |
| -
|
90 |
| - public float LengthSquared() |
91 |
| - { |
92 |
| - return X * X + Y * Y + Z * Z + W * W; |
93 |
| - } |
94 |
| -
|
95 |
| - public static Vector4 Zero => new Vector4(); |
96 |
| -
|
97 |
| - public static Vector4 operator +(Vector4 a, Vector4 b) |
98 |
| - { |
99 |
| - return new Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W); |
100 |
| - } |
101 |
| -
|
102 |
| - public static Vector4 operator -(Vector4 a, Vector4 b) |
103 |
| - { |
104 |
| - return new Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W); |
105 |
| - } |
106 |
| -
|
107 |
| - public static Vector4 operator -(Vector4 a) |
108 |
| - { |
109 |
| - return new Vector4(-a.X, -a.Y, -a.Z, -a.W); |
110 |
| - } |
111 |
| -
|
112 |
| - public static Vector4 operator *(Vector4 a, float d) |
113 |
| - { |
114 |
| - return new Vector4(a.X * d, a.Y * d, a.Z * d, a.W * d); |
115 |
| - } |
116 |
| -
|
117 |
| - public static Vector4 operator *(float d, Vector4 a) |
118 |
| - { |
119 |
| - return new Vector4(a.X * d, a.Y * d, a.Z * d, a.W * d); |
120 |
| - } |
121 |
| -
|
122 |
| - public static Vector4 operator /(Vector4 a, float d) |
123 |
| - { |
124 |
| - return new Vector4(a.X / d, a.Y / d, a.Z / d, a.W / d); |
125 |
| - } |
126 |
| -
|
127 |
| - public static bool operator ==(Vector4 lhs, Vector4 rhs) |
128 |
| - { |
129 |
| - return (lhs - rhs).LengthSquared() < kEpsilon * kEpsilon; |
130 |
| - } |
131 |
| -
|
132 |
| - public static bool operator !=(Vector4 lhs, Vector4 rhs) |
133 |
| - { |
134 |
| - return !(lhs == rhs); |
135 |
| - } |
136 |
| -
|
137 |
| - public static implicit operator Vector2(Vector4 v) |
138 |
| - { |
139 |
| - return new Vector2(v.X, v.Y); |
140 |
| - } |
141 |
| -
|
142 |
| - public static implicit operator Vector3(Vector4 v) |
143 |
| - { |
144 |
| - return new Vector3(v.X, v.Y, v.Z); |
145 |
| - } |
146 |
| -
|
147 |
| - public static implicit operator Color(Vector4 v) |
148 |
| - { |
149 |
| - return new Color(v.X, v.Y, v.Z, v.W); |
150 |
| - } |
151 |
| -
|
152 |
| - private const float kEpsilon = 0.00001F; |
153 |
| - } |
154 |
| -} |
155 |
| -""" |
| 19 | + if len(args) == 1: |
| 20 | + args = args[0] |
| 21 | + |
| 22 | + if isinstance(args, Sequence): |
| 23 | + if len(args) == 4: # args=(x, y, z, w) |
| 24 | + self.X, self.Y, self.Z, self.W = args |
| 25 | + return |
| 26 | + if len(args) == 2: # args=(Vector3, w) |
| 27 | + self.X, self.Y, self.Z = args[0] |
| 28 | + self.W = args[1] |
| 29 | + if len(args) == 0: # args=() |
| 30 | + self.X = self.Y = self.Z = self.W = 0.0 |
| 31 | + return |
| 32 | + |
| 33 | + raise TypeError("Invalid arguments for Vector4") |
| 34 | + |
| 35 | + def __getitem__(self, index): |
| 36 | + return (self.X, self.Y, self.Z, self.W)[index] |
| 37 | + |
| 38 | + def __setitem__(self, index, value): |
| 39 | + if index == 0: |
| 40 | + self.X = value |
| 41 | + elif index == 1: |
| 42 | + self.Y = value |
| 43 | + elif index == 2: |
| 44 | + self.Z = value |
| 45 | + elif index == 3: |
| 46 | + self.W = value |
| 47 | + else: |
| 48 | + raise IndexError("Index out of range") |
| 49 | + |
| 50 | + def __hash__(self): |
| 51 | + return ( |
| 52 | + self.X.__hash__() ^ |
| 53 | + (self.Y.__hash__() << 2) ^ |
| 54 | + (self.Z.__hash__() >> 2) ^ |
| 55 | + (self.W.__hash__() >> 1) |
| 56 | + ) |
| 57 | + |
| 58 | + def normalize(self): |
| 59 | + length = self.length() |
| 60 | + if length > kEpsilon: |
| 61 | + invNorm = 1.0 / length |
| 62 | + self.X *= invNorm |
| 63 | + self.Y *= invNorm |
| 64 | + self.Z *= invNorm |
| 65 | + self.W *= invNorm |
| 66 | + else: |
| 67 | + self.X = self.Y = self.Z = self.W = 0.0 |
| 68 | + |
| 69 | + Normalize = normalize |
| 70 | + |
| 71 | + def length(self): |
| 72 | + return sqrt(self.lengthSquared()) |
| 73 | + |
| 74 | + Length = length |
| 75 | + |
| 76 | + def lengthSquared(self): |
| 77 | + return self.X ** 2 + self.Y ** 2 + self.Z ** 2 + self.W ** 2 |
| 78 | + |
| 79 | + LengthSquared = lengthSquared |
| 80 | + |
| 81 | + @staticmethod |
| 82 | + def Zero(): |
| 83 | + return Vector4(0, 0, 0, 0) |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def One(): |
| 87 | + return Vector4(1, 1, 1, 1) |
| 88 | + |
| 89 | + def __add__(a, b): |
| 90 | + return Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W) |
| 91 | + |
| 92 | + def __sub__(a, b): |
| 93 | + return Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W) |
| 94 | + |
| 95 | + def __mul__(a, d): |
| 96 | + return Vector4(a.X * d, a.Y * d, a.Z * d, a.W * d) |
| 97 | + |
| 98 | + def __truediv__(a, d): |
| 99 | + return Vector4(a.X / d, a.Y / d, a.Z / d, a.W / d) |
| 100 | + |
| 101 | + def __eq__(lhs, rhs): |
| 102 | + if isinstance(rhs, Vector4): |
| 103 | + diff = lhs - rhs |
| 104 | + return diff.lengthSquared() < kEpsilon * kEpsilon |
| 105 | + return False |
| 106 | + |
| 107 | + def __ne__(lhs, rhs): |
| 108 | + return not (lhs == rhs) |
| 109 | + |
| 110 | + def Vector3(self): |
| 111 | + from .Vector3 import Vector3 |
| 112 | + return Vector3(self.X, self.Y, self.Z) |
0 commit comments