Skip to content

Commit 4846ec9

Browse files
committed
utility things
1 parent 0c20018 commit 4846ec9

File tree

9 files changed

+97
-4
lines changed

9 files changed

+97
-4
lines changed

io/MeshIO.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public class ReadOptions
3535
{
3636
public bool ReadMaterials;
3737

38+
// format readers will inevitably have their own settings, we
39+
// can use this to pass arguments to them
40+
public CommandArgumentSet CustomFlags = new CommandArgumentSet();
41+
3842
public ReadOptions()
3943
{
4044
ReadMaterials = false;

io/STLReader.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace g3
1010
{
11-
class STLReader : IMeshReader
11+
public class STLReader : IMeshReader
1212
{
1313

1414
public enum Strategy
1515
{
16-
NoProcessing,
17-
IdenticalVertexWeld
16+
NoProcessing = 0,
17+
IdenticalVertexWeld = 1
1818
}
1919
public Strategy RebuildStrategy = Strategy.IdenticalVertexWeld;
2020

@@ -27,6 +27,18 @@ public enum Strategy
2727
Dictionary<string, int> warningCount = new Dictionary<string, int>();
2828

2929

30+
31+
public const string StrategyFlag = "-stl-weld-strategy";
32+
void ParseArguments(CommandArgumentSet args)
33+
{
34+
if ( args.Integers.ContainsKey(StrategyFlag) ) {
35+
RebuildStrategy = (Strategy)args.Integers[StrategyFlag];
36+
}
37+
}
38+
39+
40+
41+
3042
protected class STLSolid
3143
{
3244
public string Name;
@@ -57,6 +69,9 @@ struct stl_triangle
5769

5870
public IOReadResult Read(BinaryReader reader, ReadOptions options, IMeshBuilder builder)
5971
{
72+
if ( options.CustomFlags != null )
73+
ParseArguments(options.CustomFlags);
74+
6075
/*byte[] header = */reader.ReadBytes(80);
6176
int totalTris = reader.ReadInt32();
6277

@@ -97,6 +112,9 @@ public IOReadResult Read(BinaryReader reader, ReadOptions options, IMeshBuilder
97112

98113
public IOReadResult Read(TextReader reader, ReadOptions options, IMeshBuilder builder)
99114
{
115+
if ( options.CustomFlags != null )
116+
ParseArguments(options.CustomFlags);
117+
100118
// format is just this, with facet repeated N times:
101119
//solid "stl_ascii"
102120
// facet normal 0.722390830517 -0.572606861591 0.387650430202

io/STLWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public IOWriteResult Write(TextWriter writer, List<WriteMesh> vMeshes, WriteOpti
9595

9696
string solid_name = string.Format("mesh_{0}", mi);
9797
if (options.bCombineMeshes == false) {
98-
if (vMeshes[mi].Name.Length > 0)
98+
if (vMeshes[mi].Name != null && vMeshes[mi].Name.Length > 0)
9999
solid_name = vMeshes[mi].Name;
100100
writer.WriteLine("solid \"{0}\"", solid_name);
101101
}

math/IndexUtil.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,45 @@ public static bool is_same_triangle(int a, int b, int c, ref Index3i tri)
112112
return false;
113113
}
114114

115+
116+
public static void cycle_indices_minfirst(ref Index3i tri)
117+
{
118+
if (tri.b < tri.a && tri.b < tri.c) {
119+
int a = tri.a, b = tri.b, c = tri.c;
120+
tri.a = b;
121+
tri.b = c;
122+
tri.c = a;
123+
} else if (tri.c < tri.a && tri.c < tri.b) {
124+
int a = tri.a, b = tri.b, c = tri.c;
125+
tri.a = c;
126+
tri.b = a;
127+
tri.c = b;
128+
}
129+
}
130+
131+
132+
public static void sort_indices(ref Index3i tri)
133+
{
134+
// possibly this can be re-ordered to have fewer tests? ...
135+
if ( tri.a < tri.b && tri.a < tri.c ) {
136+
if (tri.b > tri.c) {
137+
int b = tri.b; tri.b = tri.c; tri.c = b;
138+
}
139+
} else if ( tri.b < tri.a && tri.b < tri.c ) {
140+
if ( tri.a < tri.c ) {
141+
int b = tri.b; tri.b = tri.a; tri.a = b;
142+
} else {
143+
int a = tri.a, b = tri.b, c = tri.c;
144+
tri.a = b; tri.b = c; tri.c = a;
145+
}
146+
} else if ( tri.c < tri.a && tri.c < tri.b ) {
147+
if ( tri.b < tri.a ) {
148+
int c = tri.c; tri.c = tri.a; tri.a = c;
149+
} else {
150+
int a = tri.a, b = tri.b, c = tri.c;
151+
tri.a = c; tri.b = a; tri.c = b;
152+
}
153+
}
154+
}
115155
}
116156
}

math/MathUtil.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,12 @@ public static double IsLeft( Vector2d P0, Vector2d P1, Vector2d P2 )
364364

365365

366366

367+
368+
static readonly int[] powers_of_10 = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
369+
public static int PowerOf10(int n) {
370+
return powers_of_10[n];
371+
}
372+
373+
367374
}
368375
}

math/Vector2d.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public bool IsFinite
7676
get { double f = x + y; return double.IsNaN(f) == false && double.IsInfinity(f) == false; }
7777
}
7878

79+
public void Round(int nDecimals) {
80+
x = Math.Round(x, nDecimals);
81+
y = Math.Round(y, nDecimals);
82+
}
7983

8084

8185
public double Dot(Vector2d v2)

math/Vector2f.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public bool IsFinite
7979
get { float f = x + y; return float.IsNaN(f) == false && float.IsInfinity(f) == false; }
8080
}
8181

82+
public void Round(int nDecimals) {
83+
x = (float)Math.Round(x, nDecimals);
84+
y = (float)Math.Round(y, nDecimals);
85+
}
86+
8287
public float Dot(Vector2f v2)
8388
{
8489
return x * v2.x + y * v2.y;

math/Vector3d.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public bool IsFinite
9595
get { double f = x + y + z; return double.IsNaN(f) == false && double.IsInfinity(f) == false; }
9696
}
9797

98+
public void Round(int nDecimals) {
99+
x = Math.Round(x, nDecimals);
100+
y = Math.Round(y, nDecimals);
101+
z = Math.Round(z, nDecimals);
102+
}
103+
104+
98105
public double Dot(Vector3d v2)
99106
{
100107
return x * v2.x + y * v2.y + z * v2.z;

math/Vector3f.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ public bool IsFinite
9898
get { float f = x + y + z; return float.IsNaN(f) == false && float.IsInfinity(f) == false; }
9999
}
100100

101+
102+
public void Round(int nDecimals) {
103+
x = (float)Math.Round(x, nDecimals);
104+
y = (float)Math.Round(y, nDecimals);
105+
z = (float)Math.Round(z, nDecimals);
106+
}
107+
108+
101109
public float Dot(Vector3f v2)
102110
{
103111
return x * v2[0] + y * v2[1] + z * v2[2];

0 commit comments

Comments
 (0)