-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
316 lines (270 loc) · 12.2 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
-----------------------------------------------------------------------------
This source file is part of mogre-procedural
For the latest info, see http://code.google.com/p/mogre-procedural/
my blog:http://hi.baidu.com/rainssoft
this is overwrite ogre-procedural c++ project using c#, look ogre-procedural c++ source http://code.google.com/p/ogre-procedural/
Copyright (c) 2013-2020 rains soft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
/// Holds a bunch of static utility functions
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport Utils
public partial class Utils
{
private static int counter = 0;
/// Outputs something to the ogre log, with a [PROCEDURAL] prefix
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
// static void log(Ogre::String st);
public static void log(string st) {
LogManager.Singleton.LogMessage("[PROCEDURAL] " + st);
}
/// <summary>
/// 前缀名+唯一递增数
/// </summary>
/// <param name="prefix"></param>
/// <returns></returns>
public static string getName(string prefix) {
counter++;
return prefix + (counter).ToString();
}
//-----------------------------------------------------------------------
public static Quaternion _computeQuaternion(Vector3 directionr) {
return _computeQuaternion(directionr, Vector3.UNIT_Y);
}
public static Quaternion _computeQuaternion(Vector3 direction, Vector3 upVector) {
Quaternion q = new Quaternion();
Vector3 zVec = direction;
zVec = zVec.NormalisedCopy;
Vector3 xVec = upVector.CrossProduct(zVec);
if (xVec.IsZeroLength)
xVec = Vector3.UNIT_X;
xVec = xVec.NormalisedCopy;
Vector3 yVec = zVec.CrossProduct(xVec);
yVec = yVec.NormalisedCopy;
q.FromAxes(xVec, yVec, zVec);
return q;
}
////////////////////////////////////////
/// Gets the min of the coordinates between 2 vectors
public static Vector3 min(Vector3 v1, Vector3 v2) {
return new Vector3(System.Math.Min(v1.x, v2.x), System.Math.Min(v1.y, v2.y), System.Math.Min(v1.z, v2.z));
}
/// Gets the max of the coordinates between 2 vectors
public static Vector3 max(Vector3 v1, Vector3 v2) {
return new Vector3(System.Math.Max(v1.x, v2.x), System.Math.Max(v1.y, v2.y), System.Math.Max(v1.z, v2.z));
}
/// Gets the min of the coordinates between 2 vectors
public static Vector2 min(Vector2 v1, Vector2 v2) {
return new Vector2(System.Math.Min(v1.x, v2.x), System.Math.Min(v1.y, v2.y));
}
/// Gets the max of the coordinates between 2 vectors
public static Vector2 max(Vector2 v1, Vector2 v2) {
return new Vector2(System.Math.Max(v1.x, v2.x), System.Math.Max(v1.y, v2.y));
}
/// Builds an AABB from a list of points
public static AxisAlignedBox AABBfromPoints(List<Vector3> points) {
AxisAlignedBox aabb = new AxisAlignedBox();
if (points.Count == 0)
return aabb;
aabb.SetMinimum(points[0]);
aabb.SetMaximum(points[0]);
//for (List< Vector3>.Enumerator it = points.GetEnumerator(); it.MoveNext(); ++it.)
foreach (var it in points) {
aabb.SetMinimum(min(aabb.Minimum, it));
aabb.SetMaximum(max(aabb.Maximum, it));
}
return aabb;
}
/// Generate a name from a prefix and a counter
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
// static string getName(string prefix);
/// Shifts the components of the vector to the right
public static Vector3 vectorPermute(Vector3 @in) {
return new Vector3(@in.z, @in.x, @in.y);
}
/// Shifts the components of the vector to the left
public static Vector3 vectorAntiPermute(Vector3 @in) {
return new Vector3(@in.y, @in.z, @in.x);
}
// Rotates a Vector2 by a given oriented angle
public static Vector2 rotateVector2(Vector2 @in, Radian angleR) {
float angle = angleR.ValueRadians;
return new Vector2(@in.x * (float)Math.Cos(angle) - @in.y * (float)Math.Sin(angle), @in.x * (float)Math.Sin(angle) + @in.y * (float)Math.Cos(angle));
}
/// Caps n between min and max
public static int cap(int n, int min, int max) {
return System.Math.Max(System.Math.Min(n, max), min);
}
// *
// * An extend version of the standard modulo, in that int values are "wrapped"
// * in both directions, whereas with standard modulo, (-1)%2 == -1
// * Always return an int between 0 and cap-1
//
public static int modulo(int n, int cap) {
if (n >= 0)
return n % cap;
return (cap - 1) - ((1 + n) % cap);
}
// *
// * Equivalent of Ogre::Vector3::angleBetween, applied to Ogre::Vector2
//
public static Radian angleBetween(Vector2 v1, Vector2 v2) {
float lenProduct = v1.Length * v2.Length;
// Divide by zero check
if (lenProduct < 1e-6f)
lenProduct = 1e-6f;
float f = v1.DotProduct(v2) / lenProduct;
f = Math_Clamp(f, -1.0f, 1.0f);
return Math.ACos(f);
}
public static Radian angleBetween(Vector3 v1, Vector3 dest) {
float lenProduct = v1.Length * dest.Length;
// Divide by zero check
if (lenProduct < 1e-6f)
lenProduct = 1e-6f;
float f = v1.DotProduct(dest) / lenProduct;
f = Clamp(f, (float)-1.0f, 1.0f);
return Math.ACos(f);
}
/// <summary>
/// 取中间值
/// </summary>
/// <param name="val"></param>
/// <param name="minval"></param>
/// <param name="maxval"></param>
/// <returns></returns>
public static float Clamp(float val, float minval, float maxval) {
//assert (minval <= maxval && "Invalid clamp range");
return (float)System.Math.Max(System.Math.Min(val, maxval), minval);
}
public static int Clamp(int val, int minval, int maxval) {
//assert (minval <= maxval && "Invalid clamp range");
return (int)System.Math.Max(System.Math.Min(val, maxval), minval);
}
/// <summary>
/// 返回3个值得中间值
/// </summary>
/// <param name="f"></param>
/// <param name="p"></param>
/// <param name="p_3"></param>
/// <returns></returns>
private static float Math_Clamp(float value, float max, float min) {
var result = value;
if (value.CompareTo(max) > 0) {
result = max;
}
if (value.CompareTo(min) < 0) {
result = min;
}
return result;
}
// *
// * Gives the oriented angle from v1 to v2 in the [0;2PI[ range
//
public static Radian angleTo(Vector2 v1, Vector2 v2) {
Radian angle = angleBetween(v1, v2);
if (v1.CrossProduct(v2) < 0f)
angle = (Radian)Math.TWO_PI - angle;
return angle;
}
// *
// * Gives the oriented angle from v1 to v2 in the ]-PI;PI] range
//
public static Radian signedAngleTo(Vector2 v1, Vector2 v2) {
Radian angle = angleBetween(v1, v2);
if (v1.CrossProduct(v2) < 0f)
angle = -angle;
return angle;
}
// *
// * Computes a quaternion between UNIT_Z and direction.
// * It keeps the "up" vector to UNIT_Y
//
//C++ TO C# CONVERTER TODO TASK: The implementation of the following method could not be found:
// static Ogre::Quaternion _computeQuaternion(Ogre::Vector3 direction, Ogre::Vector3 upVector);
// *
// * Maps a vector2 to vector3, with Y=0
//
public static Vector3 vec2ToVec3Y(Vector2 pos) {
return new Vector3(pos.x, 0, pos.y);
}
// *
// * binomial coefficients (a over b)
//
public static uint binom(uint a, uint b) {
uint tmpA;
uint tmpB;
if ((b == 0) || (a == b))
return 1;
else {
tmpA = binom(a - 1, b);
tmpB = binom(a - 1, b - 1);
return tmpA + tmpB;
}
}
/// Transforms an input vector expressed in the 0,0->1,1 rect towards another rect
public static Vector2 reframe(RealRect rect, Vector2 input) {
return new Vector2(rect.left + input.x * rect.Width, rect.top + input.y * rect.Height);
}
public static void ThrowException(Exception exception) {
if (exception != null) { throw exception; }
}
//public static object Clone(object obj, bool deepClone) {
// if (deepClone && obj is IDeeplyCloneable) {
// return ((IDeeplyCloneable)obj).DeepClone();
// }
// else if (obj is ICloneable) {
// return ((ICloneable)obj).Clone();
// }
// else {
// return obj;
// }
//}
//public static bool ObjectEquals(object obj1, object obj2, bool deepCmp) {
// if (obj1 == null && obj2 == null) { return true; }
// else if (obj1 == null || obj2 == null) { return false; }
// else if (!obj1.GetType().Equals(obj2.GetType())) { return false; }
// else if (deepCmp && obj1 is IContentEquatable) {
// return ((IContentEquatable)obj1).ContentEquals(obj2);
// }
// else {
// return obj1.Equals(obj2);
// }
//}
public static object ChangeType(object obj, Type newType, IFormatProvider fmtProvider) {
ThrowException(newType == null ? new ArgumentNullException("newType") : null);
if (newType.IsAssignableFrom(obj.GetType())) {
return obj;
}
else {
return Convert.ChangeType(obj, newType, fmtProvider); // throws InvalidCastException, FormatException, OverflowException
}
}
//internal static int GetHashCode(object obj) {
// throw new NotImplementedException();
//}
}
}