diff options
Diffstat (limited to 'OpenSim/Region/Physics/Meshing/HelperTypes.cs')
-rw-r--r-- | OpenSim/Region/Physics/Meshing/HelperTypes.cs | 136 |
1 files changed, 131 insertions, 5 deletions
diff --git a/OpenSim/Region/Physics/Meshing/HelperTypes.cs b/OpenSim/Region/Physics/Meshing/HelperTypes.cs index 7b17a3f..efc5968 100644 --- a/OpenSim/Region/Physics/Meshing/HelperTypes.cs +++ b/OpenSim/Region/Physics/Meshing/HelperTypes.cs | |||
@@ -32,16 +32,136 @@ using System.Globalization; | |||
32 | using OpenSim.Region.Physics.Manager; | 32 | using OpenSim.Region.Physics.Manager; |
33 | using OpenSim.Region.Physics.Meshing; | 33 | using OpenSim.Region.Physics.Meshing; |
34 | 34 | ||
35 | public class Vertex : PhysicsVector, IComparable<Vertex> | 35 | public class Quaternion |
36 | { | 36 | { |
37 | public Vertex(float x, float y, float z) | 37 | public float x = 0; |
38 | : base(x, y, z) | 38 | public float y = 0; |
39 | public float z = 0; | ||
40 | public float w = 1; | ||
41 | |||
42 | public Quaternion() | ||
39 | { | 43 | { |
40 | } | ||
41 | 44 | ||
45 | } | ||
46 | public Quaternion(float x1, float y1, float z1, float w1) | ||
47 | { | ||
48 | x = x1; y = y1; z = z1; w = w1; | ||
49 | } | ||
50 | public Quaternion(Vertex axis, float angle) | ||
51 | { | ||
52 | // using (* 0.5) instead of (/2) | ||
53 | w = (float)Math.Cos(angle * 0.5f); | ||
54 | x = axis.X * (float)Math.Sin(angle * 0.5f); | ||
55 | y = axis.Y * (float)Math.Sin(angle * 0.5f); | ||
56 | z = axis.Z * (float)Math.Sin(angle * 0.5f); | ||
57 | normalize(); | ||
58 | } | ||
59 | public static Quaternion operator *(Quaternion a, Quaternion b) | ||
60 | { | ||
61 | Quaternion c = new Quaternion(); | ||
62 | c.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y; | ||
63 | c.y = a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z; | ||
64 | c.z = a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x; | ||
65 | c.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z; | ||
66 | return c; | ||
67 | } | ||
68 | |||
69 | |||
70 | public Matrix4 computeMatrix() | ||
71 | { | ||
72 | return new Matrix4(this); | ||
73 | } | ||
74 | public void normalize() | ||
75 | { | ||
76 | float mag = length(); | ||
77 | |||
78 | w /= mag; | ||
79 | x /= mag; | ||
80 | y /= mag; | ||
81 | z /= mag; | ||
82 | } | ||
42 | public float length() | 83 | public float length() |
43 | { | 84 | { |
44 | return (float)Math.Sqrt(X * X + Y * Y + Z * Z); | 85 | return (float)Math.Sqrt(w * w + x * x + y * y + z * z); |
86 | } | ||
87 | } | ||
88 | public class Matrix4 | ||
89 | { | ||
90 | public float m00 = 0; | ||
91 | public float m01 = 0; | ||
92 | public float m02 = 0; | ||
93 | public float m03 = 0; | ||
94 | public float m10 = 0; | ||
95 | public float m11 = 0; | ||
96 | public float m12 = 0; | ||
97 | public float m13 = 0; | ||
98 | public float m20 = 0; | ||
99 | public float m21 = 0; | ||
100 | public float m22 = 0; | ||
101 | public float m23 = 0; | ||
102 | public float m30 = 0; | ||
103 | public float m31 = 0; | ||
104 | public float m32 = 0; | ||
105 | public float m33 = 1; | ||
106 | |||
107 | public Matrix4(float m001, float m011, float m021, float m031, float m101, float m111, float m121, float m131, float m201, float m211, float m221, float m231, float m301, float m311, float m321, float m331) | ||
108 | { | ||
109 | m00 = m001; | ||
110 | m01 = m011; | ||
111 | m02 = m021; | ||
112 | m03 = m031; | ||
113 | m10 = m101; | ||
114 | m11 = m111; | ||
115 | m12 = m121; | ||
116 | m13 = m131; | ||
117 | m20 = m201; | ||
118 | m21 = m211; | ||
119 | m22 = m221; | ||
120 | m23 = m231; | ||
121 | m30 = m301; | ||
122 | m31 = m311; | ||
123 | m32 = m321; | ||
124 | m33 = m331; | ||
125 | } | ||
126 | public Matrix4() | ||
127 | { | ||
128 | } | ||
129 | public Matrix4(Quaternion r) | ||
130 | { | ||
131 | m00 = 1 - (2 * (r.y * r.y)) - (2 * (r.z * r.z)); | ||
132 | m01 = (r.x * r.y * 2) - (r.w * r.z * 2); | ||
133 | m02 = (r.x * r.z * 2) + (r.w * r.y * 2); | ||
134 | m03 = 0f; | ||
135 | m10 = (r.x * r.y * 2) + (r.w * r.z * 2); | ||
136 | m11 = 1 - (2 * (r.x * r.x)) - (2 * (r.z * r.z)); | ||
137 | m12 = (r.y * r.z * 2) - (r.w * r.x * 2); | ||
138 | m13 = 0f; | ||
139 | m20 = (r.x * r.z * 2) - (r.w * r.y * 2); | ||
140 | m21 = (r.y * r.z * 2) - (r.w * r.x * 2); | ||
141 | m22 = 1 - (2 * (r.x * r.x)) - (2 * (r.y * r.y)); | ||
142 | m23 = 0f; | ||
143 | m30 = 0f; | ||
144 | m31 = 0f; | ||
145 | m32 = 0f; | ||
146 | m33 = 1f; | ||
147 | } | ||
148 | public Vertex transform(Vertex o) | ||
149 | { | ||
150 | Vertex r = new Vertex(0,0,0); | ||
151 | // w value implicitly 1 therefore the last + m3x actually represents (m3x * o.W) = m3x | ||
152 | // in calculating the dot product. | ||
153 | r.X = (m00 * o.X) + (m10 * o.Y) + (m20 * o.Z) + m30; | ||
154 | r.Y = (m01 * o.X) + (m11 * o.Y) + (m21 * o.Z) + m31; | ||
155 | r.Z = (m02 * o.X) + (m12 * o.Y) + (m22 * o.Z) + m32; | ||
156 | return r; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | public class Vertex : PhysicsVector, IComparable<Vertex> | ||
161 | { | ||
162 | public Vertex(float x, float y, float z) | ||
163 | : base(x, y, z) | ||
164 | { | ||
45 | } | 165 | } |
46 | 166 | ||
47 | public Vertex normalize() | 167 | public Vertex normalize() |
@@ -62,6 +182,12 @@ public class Vertex : PhysicsVector, IComparable<Vertex> | |||
62 | return new Vertex(Y * v.Z - Z * v.Y, Z * v.X - X * v.Z, X * v.Y - Y * v.X); | 182 | return new Vertex(Y * v.Z - Z * v.Y, Z * v.X - X * v.Z, X * v.Y - Y * v.X); |
63 | } | 183 | } |
64 | 184 | ||
185 | public static Vertex operator *(Vertex v, Quaternion q) | ||
186 | { | ||
187 | Matrix4 tm = q.computeMatrix(); | ||
188 | return tm.transform(v); | ||
189 | } | ||
190 | |||
65 | public static Vertex operator +(Vertex v1, Vertex v2) | 191 | public static Vertex operator +(Vertex v1, Vertex v2) |
66 | { | 192 | { |
67 | return new Vertex(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z); | 193 | return new Vertex(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z); |