Skip to content

Commit

Permalink
converted all core types to structs
Browse files Browse the repository at this point in the history
  • Loading branch information
rms80 committed Dec 8, 2016
1 parent b1b2917 commit 5850c8a
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 80 deletions.
17 changes: 9 additions & 8 deletions math/Colorf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@

namespace g3
{
public class Colorf
public struct Colorf
{
public float[] v = { 0, 0, 0, 1 };
private float[] v;

public Colorf() { }
public Colorf(float greylevel, float a = 1) { v[0] = v[1] = v[2] = greylevel; v[3] = 1.0f; }
public Colorf(float r, float g, float b, float a = 1) { v[0] = r; v[1] = g; v[2] = b; v[3] = a; }
public Colorf(float greylevel, float a = 1) { v = new float[4]; v[0] = v[1] = v[2] = greylevel; v[3] = 1.0f; }
public Colorf(float r, float g, float b, float a = 1) { v = new float[4]; v[0] = r; v[1] = g; v[2] = b; v[3] = a; }
public Colorf(int r, int g, int b, int a = 255) {
v = new float[4];
v[0] = MathUtil.Clamp((float)r, 0.0f, 255.0f) / 255.0f;
v[1] = MathUtil.Clamp((float)g, 0.0f, 255.0f) / 255.0f;
v[2] = MathUtil.Clamp((float)b, 0.0f, 255.0f) / 255.0f;
v[3] = MathUtil.Clamp((float)a, 0.0f, 255.0f) / 255.0f;
}
public Colorf(float[] v2) { v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; v[3] = v2[3]; }
public Colorf(Colorf copy) { v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; v[3] = copy.v[3]; }
public Colorf(float[] v2) { v = new float[4]; v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; v[3] = v2[3]; }
public Colorf(Colorf copy) { v = new float[4]; v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; v[3] = copy.v[3]; }
public Colorf(Colorf copy, float newAlpha) { v = new float[4]; v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; v[3] = newAlpha; }


public float r
Expand Down Expand Up @@ -130,7 +131,7 @@ public override string ToString()
{
return string.Format("{0:F8} {1:F8} {2:F8} {3:F8}", v[0], v[1], v[2], v[3]);
}
public virtual string ToString(string fmt)
public string ToString(string fmt)
{
return string.Format("{0} {1} {2} {3}", v[0].ToString(fmt), v[1].ToString(fmt), v[2].ToString(fmt), v[3].ToString(fmt));
}
Expand Down
17 changes: 13 additions & 4 deletions math/Matrix3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

namespace g3
{
public class Matrix3f
public struct Matrix3f
{
public float[] m = { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
private float[] m;

public Matrix3f() { }
public Matrix3f(bool bIdentity) {
if (bIdentity)
m = new float[9] { 1, 0, 0, 0, 1, 0, 0, 0, 1 };
else
m = new float[9] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
}


public static readonly Matrix3f Identity = new Matrix3f(true);
public static readonly Matrix3f Zero = new Matrix3f(false);


public float this[int r, int c] {
Expand All @@ -32,7 +41,7 @@ public override string ToString() {
return string.Format("[{0:F8} {1:F8} {2:F8}] [{3:F8} {4:F8} {5:F8}] [{6:F8} {7:F8} {8:F8}]",
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
}
public virtual string ToString(string fmt) {
public string ToString(string fmt) {
return string.Format("[{0} {1} {2}] [{3} {4} {5}] [{6} {7} {8}]",
m[0].ToString(fmt), m[1].ToString(fmt), m[2].ToString(fmt),
m[3].ToString(fmt), m[4].ToString(fmt), m[5].ToString(fmt),
Expand Down
16 changes: 9 additions & 7 deletions math/Quaternionf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@

namespace g3
{
public class Quaternionf
public struct Quaternionf
{
public float[] v = { 0, 0, 0, 1 };
private float[] v;

public Quaternionf() { }
public Quaternionf(float x, float y, float z, float w) { v[0] = x; v[1] = y; v[2] = z; v[3] = w; }
public Quaternionf(float[] v2) { v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; v[3] = v2[3]; }
public Quaternionf(Quaternionf q2) { v[0] = q2.v[0]; v[1] = q2.v[1]; v[2] = q2.v[2]; v[3] = q2.v[3]; }
public Quaternionf(float x, float y, float z, float w) { v = new float[4]; v[0] = x; v[1] = y; v[2] = z; v[3] = w; }
public Quaternionf(float[] v2) { v = new float[4]; v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; v[3] = v2[3]; }
public Quaternionf(Quaternionf q2) { v = new float[4]; v[0] = q2.v[0]; v[1] = q2.v[1]; v[2] = q2.v[2]; v[3] = q2.v[3]; }

public Quaternionf(Vector3f axis, float AngleDeg) {
v = new float[4];
SetAxisAngleD(axis, AngleDeg);
}
public Quaternionf(Vector3f vFrom, Vector3f vTo) {
v = new float[4];
SetFromTo(vFrom, vTo);
}
public Quaternionf(Quaternionf p, Quaternionf q, float t) {
v = new float[4];
SetToSlerp(p, q, t);
}

Expand Down Expand Up @@ -256,7 +258,7 @@ public static Quaternionf Slerp(Quaternionf p, Quaternionf q, float t) {
public override string ToString() {
return string.Format("{0:F8} {1:F8} {2:F8} {3:F8}", v[0], v[1], v[2], v[3]);
}
public virtual string ToString(string fmt) {
public string ToString(string fmt) {
return string.Format("{0} {1} {2} {3}", v[0].ToString(fmt), v[1].ToString(fmt), v[2].ToString(fmt), v[3].ToString(fmt));
}

Expand Down
23 changes: 11 additions & 12 deletions math/Vector2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@

namespace g3
{
public class Vector2d
public struct Vector2d
{
public double[] v = { 0, 0 };

public Vector2d() { }
public Vector2d(double f) { v[0] = v[1] = f; }
public Vector2d(double x, double y) { v[0] = x; v[1] = y; }
public Vector2d(double[] v2) { v[0] = v2[0]; v[1] = v2[1]; }
public Vector2d(float f) { v[0] = v[1] = f; }
public Vector2d(float x, float y) { v[0] = x; v[1] = y; }
public Vector2d(float[] v2) { v[0] = v2[0]; v[1] = v2[1]; }
public Vector2d(Vector2d copy) { v[0] = copy[0]; v[1] = copy[1]; }
public Vector2d(Vector2f copy) { v[0] = copy[0]; v[1] = copy[1]; }
private double[] v;

public Vector2d(double f) { v = new double[2]; v[0] = v[1] = f; }
public Vector2d(double x, double y) { v = new double[2]; v[0] = x; v[1] = y; }
public Vector2d(double[] v2) { v = new double[2]; v[0] = v2[0]; v[1] = v2[1]; }
public Vector2d(float f) { v = new double[2]; v[0] = v[1] = f; }
public Vector2d(float x, float y) { v = new double[2]; v[0] = x; v[1] = y; }
public Vector2d(float[] v2) { v = new double[2]; v[0] = v2[0]; v[1] = v2[1]; }
public Vector2d(Vector2d copy) { v = new double[2]; v[0] = copy[0]; v[1] = copy[1]; }
public Vector2d(Vector2f copy) { v = new double[2]; v[0] = copy[0]; v[1] = copy[1]; }


static public readonly Vector2d Zero = new Vector2d(0.0f, 0.0f);
Expand Down
23 changes: 11 additions & 12 deletions math/Vector2f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@

namespace g3
{
public class Vector2f
public struct Vector2f
{
public float[] v = { 0, 0 };

public Vector2f() { }
public Vector2f(float f) { v[0] = v[1] = f; }
public Vector2f(float x, float y) { v[0] = x; v[1] = y; }
public Vector2f(float[] v2) { v[0] = v2[0]; v[1] = v2[1]; }
public Vector2f(double f) { v[0] = v[1] = (float)f; }
public Vector2f(double x, double y) { v[0] = (float)x; v[1] = (float)y; }
public Vector2f(double[] v2) { v[0] = (float)v2[0]; v[1] = (float)v2[1]; }
public Vector2f(Vector2f copy) { v[0] = copy[0]; v[1] = copy[1]; }
public Vector2f(Vector2d copy) { v[0] = (float)copy[0]; v[1] = (float)copy[1]; }
private float[] v;

public Vector2f(float f) { v = new float[2]; v[0] = v[1] = f; }
public Vector2f(float x, float y) { v = new float[2]; v[0] = x; v[1] = y; }
public Vector2f(float[] v2) { v = new float[2]; v[0] = v2[0]; v[1] = v2[1]; }
public Vector2f(double f) { v = new float[2]; v[0] = v[1] = (float)f; }
public Vector2f(double x, double y) { v = new float[2]; v[0] = (float)x; v[1] = (float)y; }
public Vector2f(double[] v2) { v = new float[2]; v[0] = (float)v2[0]; v[1] = (float)v2[1]; }
public Vector2f(Vector2f copy) { v = new float[2]; v[0] = copy[0]; v[1] = copy[1]; }
public Vector2f(Vector2d copy) { v = new float[2]; v[0] = (float)copy[0]; v[1] = (float)copy[1]; }


static public readonly Vector2f Zero = new Vector2f(0.0f, 0.0f);
Expand Down
11 changes: 5 additions & 6 deletions math/Vector2i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace g3
{
public class Vector2i
public struct Vector2i
{
public int[] v = { 0, 0 };
private int[] v;

public Vector2i() { }
public Vector2i(int f) { v[0] = v[1] = f; }
public Vector2i(int x, int y) { v[0] = x; v[1] = y;}
public Vector2i(int[] v2) { v[0] = v2[0]; v[1] = v2[1];}
public Vector2i(int f) { v = new int[2]; v[0] = v[1] = f; }
public Vector2i(int x, int y) { v = new int[2]; v[0] = x; v[1] = y;}
public Vector2i(int[] v2) { v = new int[2]; v[0] = v2[0]; v[1] = v2[1];}

static public readonly Vector2i Zero = new Vector2i(0, 0);

Expand Down
37 changes: 28 additions & 9 deletions math/Vector3d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@

namespace g3
{
public class Vector3d
public struct Vector3d
{
public double[] v = { 0, 0, 0 };
private double[] v;

public Vector3d() {}
public Vector3d(double f) { v[0] = v[1] = v[2] = f; }
public Vector3d(double x, double y, double z) { v[0] = x; v[1] = y; v[2] = z; }
public Vector3d(double[] v2) { v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; }
public Vector3d(Vector3d copy) { v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; }
public Vector3d(Vector3f copy) { v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; }
public Vector3d(double f) { v = new double[3]; v[0] = v[1] = v[2] = f; }
public Vector3d(double x, double y, double z) { v = new double[3]; v[0] = x; v[1] = y; v[2] = z; }
public Vector3d(double[] v2) { v = new double[3]; v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; }
public Vector3d(Vector3d copy) { v = new double[3]; v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; }
public Vector3d(Vector3f copy) { v = new double[3]; v[0] = copy[0]; v[1] = copy[1]; v[2] = copy[2]; }

static public readonly Vector3d Zero = new Vector3d(0.0f, 0.0f, 0.0f);
static public readonly Vector3d One = new Vector3d(1.0f, 1.0f, 1.0f);
Expand Down Expand Up @@ -196,6 +195,26 @@ public void Subtract(Vector3d o)
}



public static bool operator ==(Vector3d a, Vector3d b)
{
return (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]);
}
public static bool operator !=(Vector3d a, Vector3d b)
{
return (a[0] != b[0] || a[1] != b[1] || a[2] != b[2]);
}
public override bool Equals(object obj)
{
return this == (Vector3d)obj;
}
public override int GetHashCode()
{
return v.GetHashCode();
}



public static Vector3d Lerp(Vector3d a, Vector3d b, double t)
{
double s = 1 - t;
Expand All @@ -207,7 +226,7 @@ public static Vector3d Lerp(Vector3d a, Vector3d b, double t)
public override string ToString() {
return string.Format("{0:F8} {1:F8} {2:F8}", v[0], v[1], v[2]);
}
public virtual string ToString(string fmt) {
public string ToString(string fmt) {
return string.Format("{0} {1} {2}", v[0].ToString(fmt), v[1].ToString(fmt), v[2].ToString(fmt));
}

Expand Down
41 changes: 28 additions & 13 deletions math/Vector3f.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@

namespace g3
{
public class Vector3f
public struct Vector3f
{
public float[] v = { 0, 0, 0 };
private float[] v;

public Vector3f() { }
public Vector3f(float f) { v[0] = v[1] = v[2] = f; }
public Vector3f(float x, float y, float z) { v[0] = x; v[1] = y; v[2] = z; }
public Vector3f(float[] v2) { v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; }
public Vector3f(Vector3f copy) { v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; }
public Vector3f(float f) { v = new float[3]; v[0] = v[1] = v[2] = f; }
public Vector3f(float x, float y, float z) { v = new float[3]; v[0] = x; v[1] = y; v[2] = z; }
public Vector3f(float[] v2) { v = new float[3]; v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; }
public Vector3f(Vector3f copy) { v = new float[3]; v[0] = copy.v[0]; v[1] = copy.v[1]; v[2] = copy.v[2]; }

public Vector3f(double f) { v[0] = v[1] = v[2] = (float)f; }
public Vector3f(double x, double y, double z) { v[0] = (float)x; v[1] = (float)y; v[2] = (float)z; }
public Vector3f(double[] v2) { v[0] = (float)v2[0]; v[1] = (float)v2[1]; v[2] = (float)v2[2]; }
public Vector3f(Vector3d copy) { v[0] = (float)copy.v[0]; v[1] = (float)copy.v[1]; v[2] = (float)copy.v[2]; }
public Vector3f(double f) { v = new float[3]; v[0] = v[1] = v[2] = (float)f; }
public Vector3f(double x, double y, double z) { v = new float[3]; v[0] = (float)x; v[1] = (float)y; v[2] = (float)z; }
public Vector3f(double[] v2) { v = new float[3]; v[0] = (float)v2[0]; v[1] = (float)v2[1]; v[2] = (float)v2[2]; }
public Vector3f(Vector3d copy) { v = new float[3]; v[0] = (float)v[0]; v[1] = (float)v[1]; v[2] = (float)v[2]; }

static public readonly Vector3f Zero = new Vector3f(0.0f, 0.0f, 0.0f);
static public readonly Vector3f One = new Vector3f(1.0f, 1.0f, 1.0f);
Expand Down Expand Up @@ -197,6 +196,23 @@ public void Subtract(Vector3f o)
}


public static bool operator ==(Vector3f a, Vector3f b)
{
return (a[0] == b[0] && a[1] == b[1] && a[2] == b[2]);
}
public static bool operator !=(Vector3f a, Vector3f b)
{
return (a[0] != b[0] || a[1] != b[1] || a[2] != b[2]);
}
public override bool Equals(object obj)
{
return this == (Vector3f)obj;
}
public override int GetHashCode()
{
return v.GetHashCode();
}


public static Vector3f Lerp(Vector3f a, Vector3f b, float t)
{
Expand All @@ -206,11 +222,10 @@ public static Vector3f Lerp(Vector3f a, Vector3f b, float t)




public override string ToString() {
return string.Format("{0:F8} {1:F8} {2:F8}", v[0], v[1], v[2]);
}
public virtual string ToString(string fmt) {
public string ToString(string fmt) {
return string.Format("{0} {1} {2}", v[0].ToString(fmt), v[1].ToString(fmt), v[2].ToString(fmt));
}

Expand Down
14 changes: 8 additions & 6 deletions math/Vector3i.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace g3
{
public class Vector3i
public struct Vector3i
{
public int[] v = { 0, 0, 0 };
private int[] v;

public Vector3i() {}
public Vector3i(int f) { v[0] = v[1] = v[2] = f; }
public Vector3i(int x, int y, int z) { v[0] = x; v[1] = y; v[2] = z; }
public Vector3i(int[] v2) { v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; }
public Vector3i(int f) { v = new int[3]; v[0] = v[1] = v[2] = f; }
public Vector3i(int x, int y, int z) { v = new int[3]; v[0] = x; v[1] = y; v[2] = z; }
public Vector3i(int[] v2) { v = new int[3]; v[0] = v2[0]; v[1] = v2[1]; v[2] = v2[2]; }

static public readonly Vector3i Zero = new Vector3i(0, 0, 0);
static public readonly Vector3i AxisX = new Vector3i(1, 0, 0);
Expand All @@ -24,6 +23,9 @@ public int this[int key]
set { v[key] = value; }
}

public int[] array {
get { return v; }
}

public void Add(int s) { v[0] += s; v[1] += s; v[2] += s; }

Expand Down
6 changes: 3 additions & 3 deletions mesh/DMesh3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ Vector2i GetEdgeOpposingV(int eID)
{
int a = edges[4 * eID], b = edges[4 * eID + 1];
int t0 = edges[4 * eID + 2], t1 = edges[4 * eID + 3];
int c = IndexUtil.orient_tri_edge_and_find_other_vtx(ref a, ref b, GetTriangle(t0).v);
int c = IndexUtil.orient_tri_edge_and_find_other_vtx(ref a, ref b, GetTriangle(t0).array);
if (t1 != InvalidID) {
int d = IndexUtil.find_tri_other_vtx(a, b, GetTriangle(t1).v);
int d = IndexUtil.find_tri_other_vtx(a, b, GetTriangle(t1).array);
return new Vector2i(c, d);
} else
return new Vector2i(c, InvalidID);
Expand Down Expand Up @@ -464,7 +464,7 @@ public bool CheckValidity() {

// also check that nbr edge has opposite orientation
Vector3i othertv = GetTriangle(tOther);
int found = IndexUtil.find_tri_ordered_edge(b, a, othertv.v);
int found = IndexUtil.find_tri_ordered_edge(b, a, othertv.array);
DMESH_CHECK_OR_FAIL(found != InvalidID);
}
}
Expand Down

0 comments on commit 5850c8a

Please sign in to comment.