forked from leap71/PicoGK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PicoGK_Utils.cs
655 lines (545 loc) · 25 KB
/
PicoGK_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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//
// SPDX-License-Identifier: Apache-2.0
//
// PicoGK ("peacock") is a compact software kernel for computational geometry,
// specifically for use in Computational Engineering Models (CEM).
//
// For more information, please visit https://picogk.org
//
// PicoGK is developed and maintained by LEAP 71 - © 2023 by LEAP 71
// https://leap71.com
//
// Computational Engineering will profoundly change our physical world in the
// years ahead. Thank you for being part of the journey.
//
// We have developed this library to be used widely, for both commercial and
// non-commercial projects alike. Therefore, we have released it under a
// permissive open-source license.
//
// The foundation of PicoGK is a thin layer on top of the powerful open-source
// OpenVDB project, which in turn uses many other Free and Open Source Software
// libraries. We are grateful to be able to stand on the shoulders of giants.
//
// LEAP 71 licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, THE SOFTWARE IS
// PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System.Numerics;
using System.Diagnostics;
namespace PicoGK
{
public class Utils
{
/// <summary>
/// Strip quotes of a quoted path like "/usr/lib/" -> /usr/lib/
/// </summary>
/// <param name="strPath"></param>Path that is potentially quoted
/// <returns></returns>Unquoted path
static public string strStripQuotesFromPath(string strPath)
{
if (strPath.StartsWith("\"") && strPath.EndsWith("\""))
{
return strPath.Substring(1, strPath.Length - 2);
}
return strPath;
}
/// <summary>
/// Wait for a file's creation
/// </summary>
/// <param name="strFile"></param>
/// File to check for existence
/// <param name="fTimeOut"></param> T
/// imeout in seconds
/// <returns></returns>true if file exists, false if timeout
static public bool bWaitForFileExistence(string strFile, float fTimeOut=1000000f)
{
Stopwatch oWatch = new();
oWatch.Start();
long lTimeout = oWatch.ElapsedMilliseconds + (long) (fTimeOut * 1000);
while (oWatch.ElapsedMilliseconds < lTimeout)
{
if (File.Exists(strFile))
return true;
Thread.Sleep(100);
}
return false;
}
/// <summary>
/// Returns the path to the home folder (cross platform compatible)
/// </summary>
/// <returns>Path to the home folder</returns>
/// <exception cref="Exception">Excepts, if not found</exception>
static public string strHomeFolder()
{
string? str = null;
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
str = Environment.GetEnvironmentVariable("HOME");
}
else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
str = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
}
if (str is null)
{
throw new Exception("Could not find home folder for " + Environment.OSVersion.Platform.ToString());
}
return str;
}
/// <summary>
/// Returns the path to the documents folder (cross platform compatible)
/// </summary>
/// <returns>The path th documents folder</returns>
/// <exception cref="Exception">Excepts, if unable to find</exception>
static public string strDocumentsFolder()
{
string? str = null;
if (Environment.OSVersion.Platform == PlatformID.Unix)
{
str = Environment.GetEnvironmentVariable("HOME");
if (str is null)
{
throw new Exception("Could not find home folder for " + Environment.OSVersion.Platform.ToString());
}
return Path.Combine(str, "Documents");
}
else if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
str = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
}
if (str is null)
{
throw new Exception("Could not find home folder for " + Environment.OSVersion.Platform.ToString());
}
return str;
}
/// <summary>
/// Returns the path to the source folder of your project, under the
/// assumption that the executable .NET DLL is in its usual place.
///
/// This function is can be used to load files in a subdirectory
/// of your source code, such as the viewer environment or fonts.
/// </summary>
/// <returns>The assumed path to the source code root</returns>
static public string strProjectRootFolder()
{
string strPath = strStripQuotesFromPath(Environment.CommandLine);
for (int n = 0; n < 4; n++)
{
strPath = Path.GetDirectoryName(strPath) ?? "";
}
return strPath;
}
/// <summary>
/// Returns the path to the source folder of PicoGK, making the following
/// Assumptions:
/// - PicoGK is contained in a subfolder named "PicoGK" inside your
/// main project, so your path is MyProject/PicoGK
/// - The executable .NET DLL is in its usual place
/// This function is used mainly to load the viewer environment and
/// test files used in the PicoGK examples
/// </summary>
/// <returns>The assumed path to the PicoGK source code</returns>
static public string strPicoGKSourceCodeFolder()
{
return Path.Combine(strProjectRootFolder(), "PicoGK");
}
/// <summary>
/// Returns the path in which your current executable resides
/// </summary>
/// <returns>The folder in which your executable resides</returns>
static public string strExecutableFolder()
{
string strExePath = System.Reflection.Assembly.GetExecutingAssembly().Location ?? "";
return System.IO.Path.GetDirectoryName(strExePath) ?? "";
}
/// <summary>
/// Returns a file name in the form 20230930_134500 to be used in log files etc.
/// </summary>
/// <param name="strPrefix">Prepended before the date/time stamp</param>
/// <param name="strPostfix">Appended after the date/time stamp</param>
/// <returns></returns>
static public string strDateTimeFilename( in string strPrefix,
in string strPostfix)
{
return strPrefix + DateTimeOffset.Now.ToString("yyyyMMdd_HHmmss") + strPostfix;
}
/// <summary>
/// Shorted a string, IF it is too long. If it is not too long, nothing happens
/// C# is lacking such a function (all other functions throw exceptions)
/// </summary>
/// <param name="str">String to shorten (if too long)</param>
/// <param name="iMaxCharacters">Number of max characters in the string</param>
/// <returns></returns>
public static string strShorten(string str, int iMaxCharacters)
{
if (str.Length < iMaxCharacters)
return str;
// in their infinite wisdom, the coders of C# decided to
// throw an exception, if str is shorter than iMaxCharacters
// that's why this function is even necessary
return str[..iMaxCharacters];
}
static public void SetMatrixRow( ref Matrix4x4 mat, uint n,
float f1, float f2, float f3, float f4)
{
// An insane person wrote Matrix4x4
switch (n)
{
case 0:
mat.M11 = f1;
mat.M12 = f2;
mat.M13 = f3;
mat.M14 = f4;
break;
case 1:
mat.M21 = f1;
mat.M22 = f2;
mat.M23 = f3;
mat.M24 = f4;
break;
case 2:
mat.M31 = f1;
mat.M32 = f2;
mat.M33 = f3;
mat.M34 = f4;
break;
case 3:
mat.M41 = f1;
mat.M42 = f2;
mat.M43 = f3;
mat.M44 = f4;
break;
default:
throw new ArgumentOutOfRangeException("Matrix 4x4 row index i 0..3");
}
}
static public Matrix4x4 matLookAt( Vector3 vecEye,
Vector3 vecLookAt)
{
Vector3 vecZ = new(0.0f, 0.0f, 1.0f);
Vector3 vecView = Vector3.Normalize(vecEye - vecLookAt);
Vector3 vecRight = Vector3.Normalize(Vector3.Cross(vecZ, vecView));
Vector3 vecUp = Vector3.Cross(vecView, vecRight);
Matrix4x4 mat = new Matrix4x4();
SetMatrixRow(ref mat, 0, vecRight.X, vecUp.X, vecView.X, 0f);
SetMatrixRow(ref mat, 1, vecRight.Y, vecUp.Y, vecView.Y, 0f);
SetMatrixRow(ref mat, 2, vecRight.Z, vecUp.Z, vecView.Z, 0f);
SetMatrixRow(ref mat, 3, -Vector3.Dot(vecRight, vecEye),
-Vector3.Dot(vecUp, vecEye),
-Vector3.Dot(vecView, vecEye),
1.0f);
return mat;
}
static public Mesh mshCreateCube(BBox3 oBox)
{
return mshCreateCube(oBox.vecSize(), oBox.vecCenter());
}
static public Mesh mshCreateCube( Vector3? vecScale = null,
Vector3? vecOffsetMM = null)
{
Vector3 vecS = vecScale ?? new Vector3(1.0f);
Vector3 vecOffset = vecOffsetMM ?? new Vector3(0.0f);
Mesh oMesh = new Mesh();
Vector3[] cubeVertices =
{
new Vector3(-0.5f * vecS.X, -0.5f * vecS.Y, -0.5f * vecS.Z) + vecOffset,
new Vector3(-0.5f * vecS.X, -0.5f * vecS.Y, 0.5f * vecS.Z) + vecOffset,
new Vector3(-0.5f * vecS.X, 0.5f * vecS.Y, -0.5f * vecS.Z) + vecOffset,
new Vector3(-0.5f * vecS.X, 0.5f * vecS.Y, 0.5f * vecS.Z) + vecOffset,
new Vector3( 0.5f * vecS.X, -0.5f * vecS.Y, -0.5f * vecS.Z) + vecOffset,
new Vector3( 0.5f * vecS.X, -0.5f * vecS.Y, 0.5f * vecS.Z) + vecOffset,
new Vector3( 0.5f * vecS.X, 0.5f * vecS.Y, -0.5f * vecS.Z) + vecOffset,
new Vector3( 0.5f * vecS.X, 0.5f * vecS.Y, 0.5f * vecS.Z) + vecOffset
};
// Front face
oMesh.nAddTriangle(cubeVertices[0], cubeVertices[1], cubeVertices[3]);
oMesh.nAddTriangle(cubeVertices[0], cubeVertices[3], cubeVertices[2]);
// Back face
oMesh.nAddTriangle(cubeVertices[4], cubeVertices[6], cubeVertices[7]);
oMesh.nAddTriangle(cubeVertices[4], cubeVertices[7], cubeVertices[5]);
// Left face
oMesh.nAddTriangle(cubeVertices[0], cubeVertices[2], cubeVertices[6]);
oMesh.nAddTriangle(cubeVertices[0], cubeVertices[6], cubeVertices[4]);
// Right face
oMesh.nAddTriangle(cubeVertices[1], cubeVertices[5], cubeVertices[7]);
oMesh.nAddTriangle(cubeVertices[1], cubeVertices[7], cubeVertices[3]);
// Top face
oMesh.nAddTriangle(cubeVertices[2], cubeVertices[3], cubeVertices[7]);
oMesh.nAddTriangle(cubeVertices[2], cubeVertices[7], cubeVertices[6]);
// Bottom face
oMesh.nAddTriangle(cubeVertices[0], cubeVertices[4], cubeVertices[5]);
oMesh.nAddTriangle(cubeVertices[0], cubeVertices[5], cubeVertices[1]);
return oMesh;
}
static public Mesh mshCreateCylinder( Vector3? vecScale = null,
Vector3? vecOffsetMM = null,
int iSides = 0)
{
Vector3 vecS = vecScale ?? new Vector3(1.0f);
Vector3 vecOffset = vecOffsetMM ?? new Vector3(0.0f);
float fA = vecS.X * 0.5f;
float fB = vecS.Y * 0.5f;
if (iSides <= 0)
{
float fVoxA = fA / Library.fVoxelSizeMM;
float fVoxB = fB / Library.fVoxelSizeMM;
//Ramanujan's ellipse perimeter
//P ≈ π [ 3 (a + b) - √[(3a + b) (a + 3b) ]]
//P ≈ π(a + b) [ 1 + (3h) / (10 + √(4 - 3h) ) ], where h = (a - b)2/(a + b)2
float fP = float.Pi * (3.0f * (fVoxA + fVoxB)
- float.Sqrt((3.0f * fVoxA + fVoxB)
* (fVoxA + 3.0f * fVoxB)));
iSides = 2 * (int) float.Ceiling(fP);
}
if (iSides < 3)
{
iSides = 3;
}
Mesh oMesh = new Mesh();
Vector3 vecBottomCenter = vecOffset;
vecBottomCenter.Z -= vecS.Z * 0.5f;
Vector3 vecTopCenter = vecBottomCenter;
vecTopCenter.Z += vecS.Z;
Vector3 vecPrevBottom = new Vector3(fA, 0, 0) + vecBottomCenter;
Vector3 vecPrevTop = vecPrevBottom;
vecPrevTop.Z += vecS.Z;
float fStep = MathF.PI * 2.0f / iSides;
for (int i = 1; i <= iSides; ++i)
{
float fAngle = i * fStep;
Vector3 vecThisBottom = new Vector3(MathF.Cos(fAngle) * fA, MathF.Sin(fAngle) * fB, 0.0f) + vecBottomCenter;
Vector3 vecThisTop = vecThisBottom;
vecThisTop.Z += vecS.Z;
//top cap
oMesh.nAddTriangle(vecTopCenter, vecPrevTop, vecThisTop);
//side
oMesh.nAddTriangle(vecPrevBottom, vecThisBottom, vecPrevTop);
oMesh.nAddTriangle(vecThisBottom, vecThisTop, vecPrevTop);
//bottom cap
oMesh.nAddTriangle(vecBottomCenter, vecThisBottom, vecPrevBottom);
vecPrevBottom = vecThisBottom;
vecPrevTop = vecThisTop;
}
return oMesh;
}
static public Mesh mshCreateCone(Vector3? vecScale = null,
Vector3? vecOffsetMM = null,
int iSides = 0)
{
Vector3 vecS = vecScale ?? new Vector3(1.0f);
Vector3 vecOffset = vecOffsetMM ?? new Vector3(0.0f);
float fA = vecS.X * 0.5f;
float fB = vecS.Y * 0.5f;
if (iSides <= 0)
{
float fVoxA = fA / Library.fVoxelSizeMM;
float fVoxB = fB / Library.fVoxelSizeMM;
float fP = float.Pi * (3.0f * (fVoxA + fVoxB)
- float.Sqrt((3.0f * fVoxA + fVoxB)
* (fVoxA + 3.0f * fVoxB)));
iSides = 2 * (int)MathF.Ceiling(fP);
}
if (iSides < 3)
{
iSides = 3;
}
Mesh oMesh = new Mesh();
Vector3 vecBottomCenter = vecOffset;
vecBottomCenter.Z -= vecS.Z * 0.5f;
Vector3 vecTop = vecBottomCenter;
vecTop.Z += vecS.Z;
Vector3 vecPrevBottom = new Vector3(fA, 0, 0) + vecBottomCenter;
float fStep = MathF.PI * 2.0f / iSides;
for (int i = 1; i <= iSides; ++i)
{
float fAngle = i * fStep;
Vector3 vecThisBottom = new Vector3(MathF.Cos(fAngle) * fA, MathF.Sin(fAngle) * fB, 0.0f) + vecBottomCenter;
//side
oMesh.nAddTriangle(vecPrevBottom, vecThisBottom, vecTop);
//bottom cap
oMesh.nAddTriangle(vecBottomCenter, vecThisBottom, vecPrevBottom);
vecPrevBottom = vecThisBottom;
}
return oMesh;
}
static void GeoSphereTriangle(Vector3 vecA,
Vector3 vecB,
Vector3 vecC,
Vector3 vecOffset,
Vector3 vecRadii,
int iRecursionDepth,
Mesh oTarget)
{
if (iRecursionDepth > 0)
{
Vector3 vecAB = vecOffset + ((vecA + vecB) * 0.5f - vecOffset);
Vector3 vecBC = vecOffset + ((vecB + vecC) * 0.5f - vecOffset);
Vector3 vecCA = vecOffset + ((vecC + vecA) * 0.5f - vecOffset);
vecAB *= vecRadii / vecAB.Length();
vecBC *= vecRadii / vecBC.Length();
vecCA *= vecRadii / vecCA.Length();
GeoSphereTriangle(vecA, vecAB, vecCA, vecOffset, vecRadii, iRecursionDepth - 1, oTarget);
GeoSphereTriangle(vecAB, vecB, vecBC, vecOffset, vecRadii, iRecursionDepth - 1, oTarget);
GeoSphereTriangle(vecAB, vecBC, vecCA, vecOffset, vecRadii, iRecursionDepth - 1, oTarget);
GeoSphereTriangle(vecCA, vecBC, vecC, vecOffset, vecRadii, iRecursionDepth - 1, oTarget);
}
else
{
oTarget.nAddTriangle(vecA, vecB, vecC);
}
}
static float fSquared(float fX)
{
return fX * fX;
}
static float fApproxEllipsoidSurfaceArea(Vector3 vecABC)
{
return 4.0f * MathF.PI * MathF.Pow((
MathF.Pow(vecABC.X * vecABC.Y, 1.6f) +
MathF.Pow(vecABC.Y * vecABC.Z, 1.6f) +
MathF.Pow(vecABC.Z * vecABC.X, 1.6f)) / 3.0f, 1.0f / 1.6f);
}
static public Mesh mshCreateGeoSphere(Vector3? vecScale = null,
Vector3? vecOffsetMM = null,
int iSubdivisions = 0)
{
Vector3 vecS = vecScale ?? new Vector3(1.0f);
Vector3 vecOffset = vecOffsetMM ?? new Vector3(0.0f);
Mesh oMesh = new Mesh();
Vector3 vecRadii = vecS * 0.5f;
Vector3 vecRadii2 = vecRadii * vecRadii;
float fCoeff = fSquared(2.0f * MathF.Sin(MathF.PI * 0.2f));
Vector3 vecPenta = new Vector3(
(2.0f * MathF.Sqrt(fCoeff * vecRadii2.X - vecRadii2.X)) / fCoeff,
(2.0f * MathF.Sqrt(fCoeff * vecRadii2.Y - vecRadii2.Y)) / fCoeff,
(2.0f * MathF.Sqrt(fCoeff * vecRadii2.Z - vecRadii2.Z)) / fCoeff);
float fPentaDZ = MathF.Sqrt(vecRadii2.Z - fSquared(vecPenta.Z));
Vector3[] avecPOffs = new Vector3[5];
for (int i = 0; i < 5; i++)
{
float fAngle = 0.4f * MathF.PI * i;
avecPOffs[i] = new Vector3(vecPenta.X * MathF.Cos(fAngle), vecPenta.Y * MathF.Sin(fAngle), fPentaDZ);
}
//estimate the number of subdivisions based on the sphere or ellipsoid surface area
if (iSubdivisions <= 0)
{
int iTargetTriangles = (int) MathF.Ceiling(
fApproxEllipsoidSurfaceArea(vecRadii)
/ Library.fVoxelSizeMM
/ Library.fVoxelSizeMM);
iSubdivisions = 1;
int iTriangles = 80;
while (iSubdivisions < 8 && iTriangles < iTargetTriangles)
{
++iSubdivisions;
iTriangles = 20 * (1 << (2 * iSubdivisions));
}
}
//top cap
Vector3 vecCap = vecOffset;
vecCap.Z += vecRadii.Z;
for (int i = 0; i < 5; i++)
{
GeoSphereTriangle(vecCap, vecOffset + avecPOffs[i], vecOffset + avecPOffs[(i + 1) % 5], vecOffset, vecRadii, iSubdivisions, oMesh);
}
//10 triangles around
GeoSphereTriangle(vecOffset + avecPOffs[4], vecOffset - avecPOffs[2], vecOffset + avecPOffs[0], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[4], vecOffset - avecPOffs[1], vecOffset - avecPOffs[2], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[3], vecOffset - avecPOffs[1], vecOffset + avecPOffs[4], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[3], vecOffset - avecPOffs[0], vecOffset - avecPOffs[1], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[2], vecOffset - avecPOffs[0], vecOffset + avecPOffs[3], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[2], vecOffset - avecPOffs[4], vecOffset - avecPOffs[0], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[1], vecOffset - avecPOffs[4], vecOffset + avecPOffs[2], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[1], vecOffset - avecPOffs[3], vecOffset - avecPOffs[4], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[0], vecOffset - avecPOffs[3], vecOffset + avecPOffs[1], vecOffset, vecRadii, iSubdivisions, oMesh);
GeoSphereTriangle(vecOffset + avecPOffs[0], vecOffset - avecPOffs[2], vecOffset - avecPOffs[3], vecOffset, vecRadii, iSubdivisions, oMesh);
//bottom cap
vecCap.Z = vecOffset.Z - vecRadii.Z;
for (int i = 0; i < 5; i++)
{
GeoSphereTriangle(vecCap, vecOffset - avecPOffs[(i + 1) % 5], vecOffset - avecPOffs[i], vecOffset, vecRadii, iSubdivisions, oMesh);
}
return oMesh;
}
/// <summary>
/// Store given voxels as mesh to the output path.
/// </summary>
/// <param name="oVoxels">Voxels to be stored as mesh.</param>
/// <param name="sOutputPath">Output path.</param>
static public void StoreVoxelsToMesh(Voxels oVoxels, string sOutputPath){
Mesh msh = new Mesh(oVoxels);
msh.SaveToStlFile(sOutputPath);
}
}
/// <summary>
/// Creates a temporary folder with an arbitrary filename
/// in the system's default temp directory, which is guaranteed to be
/// writable (we don't check this, but the system should guarantee it)
/// Use the "using" syntax to automatically cleanup after the object
/// runs out of scope. It will clean up all the files inside and then
/// delete the temporary folder.
/// Note: It intentionally doesn't delete any subdirectories you may
/// create. If you create subdirs, please clean them up yourself.
/// We intentionally do not recursively wipe out everything out of an
/// abundance of caution.
///
/// Access the temp folder using oFolder.strFolder
///
/// </summary>
public class TempFolder : IDisposable
{
public TempFolder()
{
strFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(strFolder);
}
public string strFolder;
~TempFolder()
{
Dispose(false);
}
public void Dispose()
{
// Dispose of unmanaged resources.
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool bDisposing)
{
if (m_bDisposed)
{
return;
}
try
{
// We will cleanup all files, but will not cleanup subfolders
// this would require recursive delete, and I am afraid it
// could wipe out an entire disk, if used erroneously,
// however unlikely
string[] astrFiles = Directory.GetFiles(strFolder);
foreach (string strFile in astrFiles)
{
File.Delete(strFile);
}
// Remove the temp directory
Directory.Delete(strFolder);
}
catch (Exception)
{
// Failed to cleanup, hopefully the system will do it for us at some point
}
m_bDisposed = true;
}
bool m_bDisposed = false;
}
}