-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProceduralPathGenerators.cs
493 lines (429 loc) · 19.1 KB
/
ProceduralPathGenerators.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
-----------------------------------------------------------------------------
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.
-----------------------------------------------------------------------------
*/
//#ifndef PROCEDURAL_PATH_GENERATORS_INCLUDED
#define PROCEDURAL_PATH_GENERATORS_INCLUDED
//use new std wrapper...ok
namespace Mogre_Procedural
{
using System;
using System.Collections.Generic;
using System.Text;
using Mogre;
using Math = Mogre.Math;
using Mogre_Procedural.std;
using ControlPoint = Mogre_Procedural.CubicHermiteSplineControlPoint<Mogre.Vector3>;
//*
// * \addtogroup shapegrp
// * @{
//
//-----------------------------------------------------------------------
//*
// * Builds a path from a Catmull-Rom Spline.
// * Catmull-Rom Spline is the exact equivalent of Ogre's simple spline, ie
// * a spline for which position is smoothly interpolated between control points
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport CatmullRomSpline3 : public BaseSpline3<CatmullRomSpline3>
public class CatmullRomSpline3 : BaseSpline3<CatmullRomSpline3>
{
private std_vector<Vector3> mPoints = new std_vector<Vector3>();
/// Default constructor
public CatmullRomSpline3() {
}
/// Copy constructor from an Ogre simplespline
public CatmullRomSpline3(Mogre.SimpleSpline input) {
mPoints.resize((int)input.NumPoints);
for (ushort i = 0; i < input.NumPoints; i++)
mPoints.push_back(input.GetPoint(i));
}
/// Outputs current spline to an Ogre spline
//
//ORIGINAL LINE: Ogre::SimpleSpline toSimpleSpline() const
public SimpleSpline toSimpleSpline() {
Mogre.SimpleSpline spline = new SimpleSpline();
for (ushort i = 0; i < mPoints.Count; i++)
spline.AddPoint(mPoints[i]);
return spline;
}
/// Adds a control point
public CatmullRomSpline3 addPoint(Vector3 pt) {
mPoints.push_back(pt);
return this;
}
/// Adds a control point
public CatmullRomSpline3 addPoint(float x, float y, float z) {
mPoints.push_back(new Vector3(x, y, z));
return this;
}
/// Safely gets a control point
//
//ORIGINAL LINE: inline const Ogre::Vector3& safeGetPoint(uint i) const
public Vector3 safeGetPoint(uint i) {
if (mClosed)
return mPoints[Utils.modulo((int)i, mPoints.Count)];
return mPoints[Utils.cap((int)i, 0, mPoints.Count - 1)];
}
// *
// * Build a path from Catmull-Rom control points
//
//-----------------------------------------------------------------------
public Path realizePath() {
Path path = new Path();
int numPoints = mClosed ? mPoints.Count : mPoints.Count - 1;
for (uint i = 0; i < numPoints; ++i) {
Vector3 P1 = safeGetPoint(i - 1);
Vector3 P2 = safeGetPoint(i);
Vector3 P3 = safeGetPoint(i + 1);
Vector3 P4 = safeGetPoint(i + 2);
std_vector<Vector3> lref = path.getPointsReference();
GlobalMembers.computeCatmullRomPoints(P1, P2, P3, P4, mNumSeg, ref lref);
if (i == mPoints.size() - 2 && !mClosed)
path.addPoint(P3);
}
if (mClosed)
path.close();
return path;
}
}
//-----------------------------------------------------------------------
//*
// * Produces a path from Cubic Hermite control points
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport CubicHermiteSpline3 : public BaseSpline3<CubicHermiteSpline3>
public class CubicHermiteSpline3 : BaseSpline3<CubicHermiteSpline3>
{
//#define ControlPoint_AlternateDefinition1
private std_vector<ControlPoint> mPoints = new std_vector<CubicHermiteSplineControlPoint<Vector3>>();
/// Adds a control point
public CubicHermiteSpline3 addPoint(Vector3 p, Vector3 before, Vector3 after) {
mPoints.push_back(new ControlPoint(p, before, after));
return this;
}
/// Adds a control point
public CubicHermiteSpline3 addPoint(Vector3 p, Vector3 tangent) {
mPoints.push_back(new ControlPoint(p, tangent, tangent));
return this;
}
/// Adds a control point
public CubicHermiteSpline3 addPoint(Vector3 p) {
return addPoint(p, CubicHermiteSplineAutoTangentMode.AT_CATMULL);
}
//
//ORIGINAL LINE: inline CubicHermiteSpline3& addPoint(const Ogre::Vector3& p, CubicHermiteSplineAutoTangentMode autoTangentMode = AT_CATMULL)
public CubicHermiteSpline3 addPoint(Vector3 p, CubicHermiteSplineAutoTangentMode autoTangentMode) {
ControlPoint cp = new CubicHermiteSplineControlPoint<Vector3>();
cp.position = p;
cp.autoTangentBefore = autoTangentMode;
cp.autoTangentAfter = autoTangentMode;
mPoints.push_back(cp);
return this;
}
/// Adds a control point
public CubicHermiteSpline3 addPoint(float x, float y, float z) {
return addPoint(x, y, z, CubicHermiteSplineAutoTangentMode.AT_CATMULL);
}
//
//ORIGINAL LINE: inline CubicHermiteSpline3& addPoint(Ogre::float x, Ogre::float y, Ogre::float z, CubicHermiteSplineAutoTangentMode autoTangentMode = AT_CATMULL)
public CubicHermiteSpline3 addPoint(float x, float y, float z, CubicHermiteSplineAutoTangentMode autoTangentMode) {
ControlPoint cp = new CubicHermiteSplineControlPoint<Vector3>();
cp.position = new Vector3(x, y, z);
cp.autoTangentBefore = autoTangentMode;
cp.autoTangentAfter = autoTangentMode;
mPoints.push_back(cp);
return this;
}
/// Safely gets a control point
//
//ORIGINAL LINE: inline const CubicHermiteSplineControlPoint<Ogre::Vector3>& safeGetPoint(uint i) const
public CubicHermiteSplineControlPoint<Vector3> safeGetPoint(uint i) {
if (mClosed)
return mPoints[Utils.modulo((int)i, mPoints.Count)];
return mPoints[Utils.cap((int)i, 0, mPoints.Count - 1)];
}
// *
// * Builds a path from control points
//
//-----------------------------------------------------------------------
public Path realizePath() {
Path path = new Path();
//Precompute tangents
for (uint i = 0; i < mPoints.size(); ++i) {
ControlPoint mp = mPoints[(int)i];
GlobalMembers.computeTangents(ref mp, safeGetPoint(i - 1).position, safeGetPoint(i + 1).position);
}
int numPoints = mClosed ? mPoints.size() : (mPoints.size() - 1);
for (int i = 0; i < numPoints; ++i) {
ControlPoint pointBefore = mPoints[i];
ControlPoint pointAfter = safeGetPoint((uint)i + 1);
std_vector<Vector3>path_getPointsReference=path.getPointsReference();
GlobalMembers.computeCubicHermitePoints(pointBefore, pointAfter, mNumSeg, ref path_getPointsReference);
if (i == mPoints.size() - 2 && !mClosed)
path.addPoint(pointAfter.position);
}
if (mClosed)
path.close();
return path;
}
}
//-----------------------------------------------------------------------
/// Builds a line Path between 2 points
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport LinePath
public class LinePath
{
private Vector3 mPoint1 = new Vector3();
private Vector3 mPoint2 = new Vector3();
private uint mNumSeg;
/// Default constructor
public LinePath() {
mPoint1 = Vector3.ZERO;
mPoint2 = Vector3.UNIT_Y;
mNumSeg = 1;
}
/// Sets first point
public LinePath setPoint1(Vector3 point1) {
//
//ORIGINAL LINE: mPoint1 = point1;
mPoint1 = (point1);
return this;
}
/// Sets second point
public LinePath setPoint2(Vector3 point2) {
//
//ORIGINAL LINE: mPoint2 = point2;
mPoint2 = (point2);
return this;
}
/// Sets the number of segments for this line
/// \exception Ogre::InvalidParametersException Minimum of numSeg is 1
public LinePath setNumSeg(uint numSeg) {
if (numSeg == 0)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "There must be more than 0 segments", "Procedural::LinePath::setNumSeg(unsigned int)");
;
mNumSeg = numSeg;
return this;
}
private void OGRE_EXCEPT(string p, string p_2, string p_3) {
throw new Exception(p+"_"+p_2+"_"+p_3);
}
/// Builds a linepath between 2 points
public LinePath betweenPoints(Vector3 point1, Vector3 point2) {
//
//ORIGINAL LINE: mPoint1 = point1;
mPoint1 = (point1);
//
//ORIGINAL LINE: mPoint2 = point2;
mPoint2 = (point2);
return this;
}
/// Outputs a path
public Path realizePath() {
Path p = new Path();
for (uint i = 0; i <= mNumSeg; ++i) {
p.addPoint((1 - i / (float)mNumSeg) * mPoint1 + i / (float)mNumSeg * mPoint2);
}
return p;
}
}
//-----------------------------------------------------------------------
//*
// * Produces a path by rounding corners of a straight-lines path
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport RoundedCornerSpline3 : public BaseSpline3<RoundedCornerSpline3>
public class RoundedCornerSpline3 : BaseSpline3<RoundedCornerSpline3>
{
private float mRadius = 0f;
private std_vector<Vector3> mPoints = new std_vector<Vector3>();
/// Default constructor
public RoundedCornerSpline3() {
mRadius = 0.1f;
}
/// Sets the radius of the corners (default = 0.1)
public RoundedCornerSpline3 setRadius(float radius) {
mRadius = radius;
return this;
}
/// Adds a control point
public RoundedCornerSpline3 addPoint(Vector3 p) {
mPoints.push_back(p);
return this;
}
/// Adds a control point
public RoundedCornerSpline3 addPoint(float x, float y, float z) {
mPoints.push_back(new Vector3(x, y, z));
return this;
}
/// Safely gets a control point
//
//ORIGINAL LINE: inline const Ogre::Vector3& safeGetPoint(uint i) const
public Vector3 safeGetPoint(uint i) {
if (mClosed)
return mPoints[Utils.modulo((int)i, mPoints.Count)];
return mPoints[Utils.cap((int)i, 0, mPoints.Count - 1)];
}
// *
// * Builds a shape from control points
// * \exception Ogre::InvalidStateException The path contains no points
//
//-----------------------------------------------------------------------
public Path realizePath() {
if (mPoints.empty())
OGRE_EXCEPT("Ogre::Exception::ERR_INVALID_STATE", "The path contains no points", "Procedural::RoundedCornerSpline3::realizePath()");
;
Path path = new Path();
int numPoints = mClosed ? mPoints.Count : (mPoints.Count - 2);
if (!mClosed)
path.addPoint(mPoints[0]);
for (uint i = 0; i < numPoints; ++i) {
Vector3 p0 = safeGetPoint(i);
Vector3 p1 = safeGetPoint(i + 1);
Vector3 p2 = safeGetPoint(i + 2);
Vector3 vBegin = p1 - p0;
Vector3 vEnd = p2 - p1;
// We're capping the radius if it's too big compared to segment length
float radius = mRadius;
float smallestSegLength = System.Math.Min(vBegin.Length, vEnd.Length);
if (smallestSegLength < 2 * mRadius)
radius = smallestSegLength / 2.0f;
Vector3 pBegin = p1 - vBegin.NormalisedCopy * radius;
Vector3 pEnd = p1 + vEnd.NormalisedCopy * radius;
Mogre_Procedural.Plane plane1 = new Plane(vBegin, pBegin);
Mogre_Procedural.Plane plane2 = new Plane(vEnd, pEnd);
Line axis = new Line();
plane1.intersect(plane2, ref axis);
Vector3 vradBegin = axis.shortestPathToPoint(pBegin);
Vector3 vradEnd = axis.shortestPathToPoint(pEnd);
Quaternion q = vradBegin.GetRotationTo(vradEnd);
Vector3 center = pBegin - vradBegin;
Radian angleTotal = new Radian();
Vector3 vAxis = new Vector3();
q.ToAngleAxis(out angleTotal, out vAxis);
for (uint j = 0; j <= mNumSeg; j++) {
q.FromAngleAxis(angleTotal * (float)j / (float)mNumSeg, vAxis);
path.addPoint(center + q * vradBegin);
}
}
if (!mClosed)
path.addPoint(mPoints[mPoints.size() - 1]);
if (mClosed)
path.close();
return path;
}
}
//-----------------------------------------------------------------------
//*
// * Builds a path from a Bezier-Curve.
//
//C++ TO C# CONVERTER WARNING: The original type declaration contained unconverted modifiers:
//ORIGINAL LINE: class _ProceduralExport BezierCurve3 : public BaseSpline3<BezierCurve3>
public class BezierCurve3 : BaseSpline3<BezierCurve3>
{
private std_vector<Vector3> mPoints = new std_vector<Vector3>();
private uint mNumSeg;
/// Default constructor
public BezierCurve3() {
mNumSeg = 8;
}
/// Sets number of segments per two control points
/// \exception Ogre::InvalidParametersException Minimum of numSeg is 1
public BezierCurve3 setNumSeg(uint numSeg) {
if (numSeg == 0)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALIDPARAMS", "There must be more than 0 segments", "Procedural::BezierCurve3::setNumSeg(unsigned int)");
;
mNumSeg = numSeg;
return this;
}
/// Adds a control point
public BezierCurve3 addPoint(Vector3 pt) {
mPoints.push_back(pt);
return this;
}
/// Adds a control point
public BezierCurve3 addPoint(float x, float y, float z) {
mPoints.push_back(new Vector3(x, y, z));
return this;
}
/// Safely gets a control point
//
//ORIGINAL LINE: inline const Ogre::Vector3& safeGetPoint(uint i) const
public Vector3 safeGetPoint(uint i) {
if (mClosed)
return mPoints[Utils.modulo((int)i, mPoints.Count)];
return mPoints[Utils.cap((int)i, 0, mPoints.Count - 1)];
}
// *
// * Build a path from bezier control points
// * @exception Ogre::InvalidStateException The curve must at least contain 2 points
//
//-----------------------------------------------------------------------
public Path realizePath() {
if (mPoints.size() < 2)
OGRE_EXCEPT("Ogre::Exception::ERR_INVALID_STATE", "The curve must at least contain 2 points", "Procedural::BezierCurve3::realizePath()");
;
uint[] coef = new uint[mPoints.size()];
if (mPoints.size() == 2) {
coef[0] = 1;
coef[1] = 1;
}
else if (mPoints.size() == 3) {
coef[0] = 1;
coef[1] = 2;
coef[2] = 1;
}
else if (mPoints.size() == 4) {
coef[0] = 1;
coef[1] = 3;
coef[2] = 3;
coef[3] = 1;
}
else {
for (uint i = 0; i < (int)mPoints.Count; i++)
coef[i] = Utils.binom((uint)mPoints.Count - 1, i);
}
uint div = (uint)(mPoints.Count - 1) * mNumSeg + 1;
float dt = 1.0f / (float)div;
Path path = new Path();
float t = 0.0f;
while (t < 1.0f) {
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
for (int i = 0; i < (int)mPoints.size(); i++) {
float fac = coef[i] * (float)System.Math.Pow(t, i) * (float)System.Math.Pow(1.0f - t, (int)mPoints.Count - 1 - i);
x += fac * mPoints[i].x;
y += fac * mPoints[i].y;
z += fac * mPoints[i].z;
}
path.addPoint(x, y, z);
t += dt;
}
coef = null;
return path;
}
}
//* @}
}