Skip to content

Commit 072fa6b

Browse files
committed
some cosmetics on DoubleDictionary
1 parent 590cdaf commit 072fa6b

File tree

3 files changed

+68
-54
lines changed

3 files changed

+68
-54
lines changed

OpenMetaverse.Rendering.Meshmerizer/MeshmerizerR.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ public OMVR.SimpleMesh GenerateSimpleSculptMesh(OMV.Primitive prim, System.Drawi
110110

111111
public static FacetedMesh m_BasicBoxMesh = null;
112112
private static readonly object m_BasicBoxMeshLock = new();
113-
public static List<ushort> m_indexs_012213 = [0, 1, 2, 2, 1, 3];
113+
public static List<ushort> m_indexs_012023 = [0, 1, 2, 0, 2, 3];
114+
public static List<ushort> m_indexs_012213 = [0, 1, 2, 2, 1, 3];
115+
public static List<ushort> m_indexs_012302 = [0, 1, 2, 3, 0, 2];
114116
public static FacetedMesh GetBasicBoxMesh()
115117
{
116118
if(m_BasicBoxMesh == null)
@@ -130,7 +132,7 @@ public static FacetedMesh GetBasicBoxMesh()
130132
new() { Position = new( 0.5f, 0.5f, 0.5f), Normal = Vector3.UnitZ, TexCoord = new(1f, 1f)},
131133
new() { Position = new(-0.5f, 0.5f, 0.5f), Normal = Vector3.UnitZ, TexCoord = new(0f, 1f)},
132134
],
133-
Indices = [0, 1, 2, 0, 2, 3],
135+
Indices = m_indexs_012023,
134136
},
135137
new Face()
136138
{
@@ -185,7 +187,7 @@ public static FacetedMesh GetBasicBoxMesh()
185187
new() { Position = new( -0.5f, -0.5f, -0.5f), Normal = -Vector3.UnitZ, TexCoord = new(0f, 1f)},
186188
new() { Position = new( -0.5f, 0.5f, -0.5f), Normal = -Vector3.UnitZ, TexCoord = new(0f, 0f)},
187189
],
188-
Indices = [0, 1, 2, 3, 0, 2],
190+
Indices = m_indexs_012302,
189191
},
190192
]
191193
};

OpenMetaverseTypes/DoubleDictionary.cs

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,18 @@ public void Add(TKey1 key1, TKey2 key2, TValue value)
7373

7474
public bool Remove(TKey1 key1, TKey2 key2)
7575
{
76-
bool success;
7776
rwLock.EnterWriteLock();
78-
7977
try
8078
{
8179
Dictionary1.Remove(key1);
82-
success = Dictionary2.Remove(key2);
80+
return Dictionary2.Remove(key2);
8381
}
8482
finally { rwLock.ExitWriteLock(); }
85-
86-
return success;
8783
}
8884

8985
public bool Remove(TKey1 key1)
9086
{
91-
bool found = false;
9287
rwLock.EnterWriteLock();
93-
9488
try
9589
{
9690
// This is an O(n) operation!
@@ -103,43 +97,36 @@ public bool Remove(TKey1 key1)
10397
{
10498
Dictionary1.Remove(key1);
10599
Dictionary2.Remove(kvp.Key);
106-
found = true;
107-
break;
100+
return true;
108101
}
109102
}
110103
}
104+
return false;
111105
}
112106
finally { rwLock.ExitWriteLock(); }
113-
114-
return found;
115107
}
116108

117109
public bool Remove(TKey2 key2)
118110
{
119-
bool found = false;
120111
rwLock.EnterWriteLock();
121-
122112
try
123113
{
124114
// This is an O(n) operation!
125-
TValue value;
126-
if (Dictionary2.TryGetValue(key2, out value))
115+
if (Dictionary2.TryGetValue(key2, out TValue value))
127116
{
128117
foreach (KeyValuePair<TKey1, TValue> kvp in Dictionary1)
129118
{
130119
if (kvp.Value.Equals(value))
131120
{
132121
Dictionary2.Remove(key2);
133122
Dictionary1.Remove(kvp.Key);
134-
found = true;
135-
break;
123+
return true;
136124
}
137125
}
138126
}
127+
return false;
139128
}
140129
finally { rwLock.ExitWriteLock(); }
141-
142-
return found;
143130
}
144131

145132
public void Clear()
@@ -171,30 +158,21 @@ public bool ContainsKey(TKey2 key)
171158

172159
public bool TryGetValue(TKey1 key, out TValue value)
173160
{
174-
bool success;
175161
rwLock.EnterReadLock();
176-
177-
try { success = Dictionary1.TryGetValue(key, out value); }
162+
try { return Dictionary1.TryGetValue(key, out value); }
178163
finally { rwLock.ExitReadLock(); }
179-
180-
return success;
181164
}
182165

183166
public bool TryGetValue(TKey2 key, out TValue value)
184167
{
185-
bool success;
186168
rwLock.EnterReadLock();
187-
188-
try { success = Dictionary2.TryGetValue(key, out value); }
169+
try { return Dictionary2.TryGetValue(key, out value); }
189170
finally { rwLock.ExitReadLock(); }
190-
191-
return success;
192171
}
193172

194173
public void ForEach(Action<TValue> action)
195174
{
196175
rwLock.EnterReadLock();
197-
198176
try
199177
{
200178
foreach (TValue value in Dictionary1.Values)
@@ -206,7 +184,6 @@ public void ForEach(Action<TValue> action)
206184
public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
207185
{
208186
rwLock.EnterReadLock();
209-
210187
try
211188
{
212189
foreach (KeyValuePair<TKey1, TValue> entry in Dictionary1)
@@ -218,7 +195,6 @@ public void ForEach(Action<KeyValuePair<TKey1, TValue>> action)
218195
public void ForEach(Action<KeyValuePair<TKey2, TValue>> action)
219196
{
220197
rwLock.EnterReadLock();
221-
222198
try
223199
{
224200
foreach (KeyValuePair<TKey2, TValue> entry in Dictionary2)
@@ -247,7 +223,6 @@ public IList<TValue> FindAll(Predicate<TValue> predicate)
247223
{
248224
IList<TValue> list = new List<TValue>();
249225
rwLock.EnterReadLock();
250-
251226
try
252227
{
253228
foreach (TValue value in Dictionary1.Values)

OpenMetaverseTypes/Quaternion.cs

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,41 @@ public void Sub(ref readonly Quaternion quaternion2)
377377
}
378378
}
379379

380+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
381+
public readonly Quaternion Multiply(ref readonly Quaternion b)
382+
{
383+
//if (MathF.Abs(W) > (1.0f - 1e-6f))
384+
// return b;
385+
386+
return new Quaternion(
387+
W * b.X + X * b.W + Y * b.Z - Z * b.Y,
388+
W * b.Y + Y * b.W + Z * b.X - X * b.Z,
389+
W * b.Z + Z * b.W + X * b.Y - Y * b.X,
390+
W * b.W - X * b.X - Y * b.Y - Z * b.Z
391+
);
392+
}
393+
394+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
395+
public void MultiplySelf(ref readonly Quaternion b)
396+
{
397+
if (MathF.Abs(W) > (1.0f - 1e-6f))
398+
399+
{
400+
X = b.X;
401+
Y = b.Y;
402+
Z = b.Z;
403+
W = b.W;
404+
return;
405+
}
406+
float x = W * b.X + X * b.W + Y * b.Z - Z * b.Y;
407+
float y = W * b.Y + Y * b.W + Z * b.X - X * b.Z;
408+
float z = W * b.Z + Z * b.W + X * b.Y - Y * b.X;
409+
W = W * b.W - X * b.X - Y * b.Y - Z * b.Z;
410+
X = x;
411+
Y = y;
412+
Z = z;
413+
}
414+
380415
/// <summary>
381416
/// Builds a quaternion object from a byte array
382417
/// </summary>
@@ -701,11 +736,11 @@ public static Quaternion Add(ref readonly Quaternion quaternion1, ref readonly Q
701736
/// Returns the conjugate (spatial inverse) of a quaternion
702737
/// </summary>
703738
[MethodImpl(MethodImplOptions.AggressiveInlining)]
704-
public static Quaternion Conjugate(in Quaternion quaternion)
739+
public static Quaternion Conjugate(ref readonly Quaternion quaternion)
705740
{
706741
if (Sse.IsSupported)
707742
{
708-
Vector128<float> d = Unsafe.As<Quaternion, Vector128<float>>(ref Unsafe.AsRef(quaternion));
743+
Vector128<float> d = Unsafe.As<Quaternion, Vector128<float>>(ref Unsafe.AsRef(in quaternion));
709744
Vector128<float> Mask = Vector128.Create(0x80000000, 0x80000000, 0x80000000, 0).AsSingle();
710745
d = Sse.Xor(d, Mask);
711746
return new Quaternion(ref d);
@@ -733,13 +768,14 @@ public static Quaternion CreateFromAxisAngle(float axisX, float axisY, float axi
733768
[MethodImpl(MethodImplOptions.AggressiveInlining)]
734769
public static Quaternion CreateFromAxisAngle(Vector3 axis, float angle)
735770
{
736-
axis.Normalize();
737-
738771
angle *= 0.5f;
739-
float c = MathF.Cos(angle);
740772
float s = MathF.Sin(angle);
741773

742-
return new Quaternion(axis.X * s, axis.Y * s, axis.Z * s, c);
774+
axis.Normalize();
775+
axis *= s;
776+
777+
float c = MathF.Cos(angle);
778+
return new Quaternion(axis.X, axis.Y, axis.Z, c);
743779
}
744780

745781
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -834,7 +870,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix3x3 matrix)
834870
}
835871
if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33))
836872
{
837-
num = MathF.Sqrt((((1f + matrix.M11) - matrix.M22) - matrix.M33));
873+
num = MathF.Sqrt(1f + matrix.M11 - matrix.M22 - matrix.M33);
838874
n2 = 0.5f / num;
839875
return new Quaternion(
840876
0.5f * num,
@@ -844,7 +880,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix3x3 matrix)
844880
}
845881
if (matrix.M22 > matrix.M33)
846882
{
847-
num = MathF.Sqrt((((1f + matrix.M22) - matrix.M11) - matrix.M33));
883+
num = MathF.Sqrt(1f - matrix.M11 + matrix.M22 - matrix.M33);
848884
n2 = 0.5f / num;
849885
return new Quaternion(
850886
(matrix.M21 + matrix.M12) * n2,
@@ -853,7 +889,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix3x3 matrix)
853889
(matrix.M31 - matrix.M13) * n2);
854890
}
855891

856-
num = MathF.Sqrt((((1f + matrix.M33) - matrix.M11) - matrix.M22));
892+
num = MathF.Sqrt(1f - matrix.M11 - matrix.M22 + matrix.M33 );
857893
n2 = 0.5f / num;
858894
return new Quaternion(
859895
(matrix.M31 + matrix.M13) * n2,
@@ -868,7 +904,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix4 matrix)
868904
float n2;
869905
if (num >= 0f)
870906
{
871-
num = MathF.Sqrt((num + 1f));
907+
num = MathF.Sqrt(1f + num);
872908
n2 = 0.5f / num;
873909
return new Quaternion(
874910
(matrix.M23 - matrix.M32) * n2,
@@ -878,7 +914,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix4 matrix)
878914
}
879915
if ((matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33))
880916
{
881-
num = MathF.Sqrt((((1f + matrix.M11) - matrix.M22) - matrix.M33));
917+
num = MathF.Sqrt(1f + matrix.M11 - matrix.M22 - matrix.M33);
882918
n2 = 0.5f / num;
883919
return new Quaternion(
884920
0.5f * num,
@@ -888,7 +924,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix4 matrix)
888924
}
889925
if (matrix.M22 > matrix.M33)
890926
{
891-
num = MathF.Sqrt((((1f + matrix.M22) - matrix.M11) - matrix.M33));
927+
num = MathF.Sqrt(1f - matrix.M11 + matrix.M22 - matrix.M33);
892928
n2 = 0.5f / num;
893929
return new Quaternion(
894930
(matrix.M21 + matrix.M12) * n2,
@@ -897,7 +933,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix4 matrix)
897933
(matrix.M31 - matrix.M13) * n2);
898934
}
899935

900-
num = MathF.Sqrt((((1f + matrix.M33) - matrix.M11) - matrix.M22));
936+
num = MathF.Sqrt(1f - matrix.M11 - matrix.M22 + matrix.M33);
901937
n2 = 0.5f / num;
902938
return new Quaternion(
903939
(matrix.M31 + matrix.M13) * n2,
@@ -909,7 +945,7 @@ public static Quaternion CreateFromRotationMatrix(Matrix4 matrix)
909945
[MethodImpl(MethodImplOptions.AggressiveInlining)]
910946
public static Quaternion Divide(Quaternion q1, Quaternion q2)
911947
{
912-
return Quaternion.Inverse(q1) * q2;
948+
return Inverse(q1) * q2;
913949
}
914950

915951
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -932,9 +968,8 @@ public static float Dot(Quaternion q1, Quaternion q2)
932968
public static Quaternion Inverse(Quaternion quaternion)
933969
{
934970
float normsq = quaternion.LengthSquared();
935-
936971
if (normsq < 1e-6f)
937-
return Quaternion.Identity;
972+
return Identity;
938973

939974
float oonorm = -1f / normsq;
940975
return new Quaternion(
@@ -1008,7 +1043,7 @@ public static Quaternion Subtract(Quaternion quaternion1, Quaternion quaternion2
10081043
}
10091044

10101045
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1011-
public static Quaternion Multiply(Quaternion a, Quaternion b)
1046+
public static Quaternion Multiply(ref readonly Quaternion a, ref readonly Quaternion b)
10121047
{
10131048
return new Quaternion(
10141049
a.W * b.X + a.X * b.W + a.Y * b.Z - a.Z * b.Y,
@@ -1019,7 +1054,7 @@ public static Quaternion Multiply(Quaternion a, Quaternion b)
10191054
}
10201055

10211056
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1022-
public static Quaternion Multiply(Quaternion quaternion, float scaleFactor)
1057+
public static Quaternion Multiply(ref readonly Quaternion quaternion, float scaleFactor)
10231058
{
10241059
return new Quaternion(
10251060
quaternion.X * scaleFactor,
@@ -1401,6 +1436,8 @@ public readonly string ToRawString()
14011436
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14021437
public static Quaternion operator *(Quaternion a, Quaternion b)
14031438
{
1439+
if (MathF.Abs(a.W) > (1.0f - 1e-6f)) return b;
1440+
if (MathF.Abs(b.W) > (1.0f - 1e-6f)) return a;
14041441
return new Quaternion(
14051442
a.W * b.X + a.X * b.W + a.Y * b.Z - a.Z * b.Y,
14061443
a.W * b.Y + a.Y * b.W + a.Z * b.X - a.X * b.Z,

0 commit comments

Comments
 (0)