Skip to content

Commit 79a60a3

Browse files
authored
feat: Create Geometry classes (#20)
This is the first step to move away from using triangles inside the shapes.
1 parent b4db92b commit 79a60a3

13 files changed

Lines changed: 330 additions & 232 deletions

File tree

‎src/CSG/Geometry/Cone.cs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace CSG.Geometry
2+
{
3+
using System;
4+
using System.Numerics;
5+
6+
public static class Cone
7+
{
8+
public static void CreateSolid(this IGeometryBuilder builder)
9+
=> CreateSolid(builder, 32);
10+
11+
public static void CreateSolid(this IGeometryBuilder builder, int tessellation)
12+
{
13+
Vector3 offset = new Vector3(0, -0.25f, 0);
14+
15+
builder.AddVertex(Vector3.UnitY + offset, Vector3.UnitY);
16+
builder.AddVertex(Vector3.Zero + offset, -Vector3.UnitY);
17+
18+
for (int i = 0; i < tessellation; ++i)
19+
{
20+
float angle = i * Algorithms.Helpers.TwoPi / tessellation;
21+
22+
float dx = MathF.Cos(angle);
23+
float dz = MathF.Sin(angle);
24+
25+
Vector3 normal = new Vector3(dx, 0, dz);
26+
27+
builder.AddVertex(normal + offset, normal);
28+
29+
builder.AddIndex(0);
30+
builder.AddIndex(2 + i);
31+
builder.AddIndex(2 + (i + 1) % tessellation);
32+
33+
builder.AddIndex(1);
34+
builder.AddIndex(2 + (i + 1) % tessellation);
35+
builder.AddIndex(2 + i);
36+
}
37+
}
38+
}
39+
}

‎src/CSG/Geometry/Cube.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace CSG.Geometry
2+
{
3+
using System.Numerics;
4+
5+
public static class Cube
6+
{
7+
private static readonly Vector3[] normals = new[]
8+
{
9+
new Vector3(+0, +0, +1),
10+
new Vector3(+0, +0, -1),
11+
new Vector3(+1, +0, +0),
12+
new Vector3(-1, +0, +0),
13+
new Vector3(+0, +1, +0),
14+
new Vector3(+0, -1, +0),
15+
};
16+
17+
public static void CreateSolid(this IGeometryBuilder builder, Vector3 size)
18+
{
19+
for (int i = 0; i < normals.Length; i++)
20+
{
21+
var normal = normals[i];
22+
23+
Vector3 side1 = new Vector3(normal.Y, normal.Z, normal.X);
24+
Vector3 side2 = Vector3.Cross(normal, side1);
25+
26+
builder.AddIndex(builder.CurrentVertex + 0);
27+
builder.AddIndex(builder.CurrentVertex + 1);
28+
builder.AddIndex(builder.CurrentVertex + 2);
29+
30+
builder.AddIndex(builder.CurrentVertex + 0);
31+
builder.AddIndex(builder.CurrentVertex + 2);
32+
builder.AddIndex(builder.CurrentVertex + 3);
33+
34+
builder.AddVertex(((normal - side1 - side2) / 2.0f) * size, normal, Vector2.Zero);
35+
builder.AddVertex(((normal - side1 + side2) / 2.0f) * size, normal, Vector2.UnitX);
36+
builder.AddVertex(((normal + side1 + side2) / 2.0f) * size, normal, Vector2.One);
37+
builder.AddVertex(((normal + side1 - side2) / 2.0f) * size, normal, Vector2.UnitY);
38+
}
39+
}
40+
}
41+
}

‎src/CSG/Geometry/Cylinder.cs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace CSG.Geometry
2+
{
3+
using System;
4+
using System.Numerics;
5+
6+
public static class Cylinder
7+
{
8+
public static void CreateSolid(this IGeometryBuilder builder, float radius)
9+
=> CreateSolid(builder, Vector3.UnitY, -Vector3.UnitY, radius, 32);
10+
11+
public static void CreateSolid(this IGeometryBuilder builder, float radius, int tessellation)
12+
=> CreateSolid(builder, Vector3.UnitY, -Vector3.UnitY, radius, tessellation);
13+
14+
public static void CreateSolid(this IGeometryBuilder builder, Vector3 start, Vector3 end, float radius, int tessellation)
15+
{
16+
builder.AddVertex(start * 0.5f, start);
17+
builder.AddVertex(-end * 0.5f, -end);
18+
19+
float diameter = radius / 2.0f;
20+
for (int i = 0; i < tessellation; ++i)
21+
{
22+
float angle = i * Algorithms.Helpers.TwoPi / tessellation;
23+
24+
float dx = MathF.Cos(angle);
25+
float dz = MathF.Sin(angle);
26+
27+
Vector3 normal = new Vector3(dx, 0.0f, dz);
28+
29+
builder.AddVertex(normal + (diameter * start), normal);
30+
builder.AddVertex(normal - (diameter * start), normal);
31+
32+
builder.AddIndex(0);
33+
builder.AddIndex(2 + (i * 2));
34+
builder.AddIndex(2 + (((i * 2) + 2) % (tessellation * 2)));
35+
36+
builder.AddIndex(2 + (i * 2));
37+
builder.AddIndex(2 + (i * 2) + 1);
38+
builder.AddIndex(2 + (((i * 2) + 2) % (tessellation * 2)));
39+
40+
builder.AddIndex(1);
41+
builder.AddIndex(2 + (((i * 2) + 3) % (tessellation * 2)));
42+
builder.AddIndex(2 + (i * 2) + 1);
43+
44+
builder.AddIndex(2 + (i * 2) + 1);
45+
builder.AddIndex(2 + (((i * 2) + 3) % (tessellation * 2)));
46+
builder.AddIndex(2 + (((i * 2) + 2) % (tessellation * 2)));
47+
}
48+
}
49+
}
50+
}

‎src/CSG/Geometry/Sphere.cs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
namespace CSG.Geometry
2+
{
3+
using System;
4+
using System.Numerics;
5+
6+
public static class Sphere
7+
{
8+
public static void CreateSolid(this IGeometryBuilder builder)
9+
=> CreateSolid(builder, 1.0f, 12);
10+
11+
public static void CreateSolid(this IGeometryBuilder builder, float radius)
12+
=> CreateSolid(builder, radius, 12);
13+
14+
public static void CreateSolid(this IGeometryBuilder builder, float radius, int tessellation)
15+
{
16+
int verticalSegments = tessellation;
17+
int horizontalSegments = tessellation * 2;
18+
19+
// Start with a single vertex at the bottom of the sphere.
20+
builder.AddVertex(Vector3.UnitY, -Vector3.UnitY);
21+
22+
// Create rings of vertices at progressively higher latitudes.
23+
for (int i = 0; i < verticalSegments - 1; ++i)
24+
{
25+
float latitude = ((i + 1.0f) * Algorithms.Helpers.Pi / verticalSegments) - Algorithms.Helpers.PiOver2;
26+
27+
float dy = MathF.Sin(latitude);
28+
float dxz = MathF.Cos(latitude);
29+
30+
// Create a single ring of vertices at this latitude.
31+
for (int j = 0; j < horizontalSegments; j++)
32+
{
33+
float longitude = j * Algorithms.Helpers.TwoPi / horizontalSegments;
34+
35+
float dx = MathF.Cos(longitude) * dxz;
36+
float dz = MathF.Sin(longitude) * dxz;
37+
38+
Vector3 normal = new Vector3(dx, dy, dz) * radius;
39+
40+
builder.AddVertex(normal, normal);
41+
}
42+
}
43+
44+
// Finish with a single vertex at the top of the sphere.
45+
builder.AddVertex(Vector3.UnitY, Vector3.UnitY);
46+
47+
// Create a fan connecting the bottom vertex to the bottom latitude ring.
48+
for (int i = 0; i < horizontalSegments; ++i)
49+
{
50+
builder.AddIndex(0);
51+
builder.AddIndex(1 + ((i + 1) % horizontalSegments));
52+
builder.AddIndex(1 + i);
53+
}
54+
55+
// Fill the sphere body with triangles joining each pair of latitude rings.
56+
for (int i = 0; i < verticalSegments - 2; ++i)
57+
{
58+
for (int j = 0; j < horizontalSegments; j++)
59+
{
60+
int nextI = i + 1;
61+
int nextJ = (j + 1) % horizontalSegments;
62+
63+
builder.AddIndex(1 + (i * horizontalSegments) + j);
64+
builder.AddIndex(1 + (i * horizontalSegments) + nextJ);
65+
builder.AddIndex(1 + (nextI * horizontalSegments) + j);
66+
67+
builder.AddIndex(1 + (i * horizontalSegments) + nextJ);
68+
builder.AddIndex(1 + (nextI * horizontalSegments) + nextJ);
69+
builder.AddIndex(1 + (nextI * horizontalSegments) + j);
70+
}
71+
}
72+
73+
// Create a fan connecting the top vertex to the top latitude ring.
74+
for (int i = 0; i < horizontalSegments; ++i)
75+
{
76+
builder.AddIndex(builder.CurrentVertex - 1);
77+
builder.AddIndex(builder.CurrentVertex - 2 - ((i + 1) % horizontalSegments));
78+
builder.AddIndex(builder.CurrentVertex - 2 - i);
79+
}
80+
}
81+
}
82+
}

‎src/CSG/Geometry/Torus.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace CSG.Geometry
2+
{
3+
using System;
4+
using System.Numerics;
5+
6+
public static class Torus
7+
{
8+
public static void CreateSolid(this IGeometryBuilder builder)
9+
=> CreateSolid(builder, 32);
10+
11+
public static void CreateSolid(this IGeometryBuilder builder, int tessellation)
12+
{
13+
// First we loop around the main ring of the torus.
14+
for (int i = 0; i < tessellation; ++i)
15+
{
16+
float outerAngle = i * Algorithms.Helpers.TwoPi / tessellation;
17+
18+
// Create a transform matrix that will align geometry to
19+
// slice perpendicularly though the current ring position.
20+
var transform = Matrix4x4.CreateTranslation(1, 0, 0) *
21+
Matrix4x4.CreateRotationY(outerAngle);
22+
23+
// Now we loop along the other axis, around the side of the tube.
24+
for (int j = 0; j < tessellation; j++)
25+
{
26+
float innerAngle = j * Algorithms.Helpers.TwoPi / tessellation;
27+
28+
float dx = MathF.Cos(innerAngle);
29+
float dy = MathF.Sin(innerAngle);
30+
31+
// Create a vertex.
32+
Vector3 normal = new Vector3(dx, dy, 0);
33+
Vector3 position = normal / 2.0f;
34+
35+
position = Vector3.Transform(position, transform);
36+
normal = Vector3.TransformNormal(normal, transform);
37+
38+
builder.AddVertex(position, normal);
39+
40+
// And create indices for two triangles.
41+
int nextI = (i + 1) % tessellation;
42+
int nextJ = (j + 1) % tessellation;
43+
44+
builder.AddIndex(i * tessellation + j);
45+
builder.AddIndex(i * tessellation + nextJ);
46+
builder.AddIndex(nextI * tessellation + j);
47+
48+
builder.AddIndex(i * tessellation + nextJ);
49+
builder.AddIndex(nextI * tessellation + nextJ);
50+
builder.AddIndex(nextI * tessellation + j);
51+
}
52+
}
53+
}
54+
}
55+
}

‎src/CSG/IGeometryBuilder.cs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace CSG
2+
{
3+
using System.Numerics;
4+
5+
public interface IGeometryBuilder
6+
{
7+
/// <summary>
8+
/// Gets the current vertex index.
9+
/// </summary>
10+
int CurrentVertex { get; }
11+
12+
/// <summary>
13+
/// Add a new index.
14+
/// </summary>
15+
/// <param name="index">The index.</param>
16+
void AddIndex(int index);
17+
18+
/// <summary>
19+
/// Add a new vertex.
20+
/// </summary>
21+
/// <param name="vertex"></param>
22+
void AddVertex(Vertex vertex);
23+
24+
/// <summary>
25+
/// Add a new vertex with position and normal.
26+
/// </summary>
27+
/// <param name="position">The vertex position.</param>
28+
/// <param name="normal">The vertex normal.</param>
29+
void AddVertex(Vector3 position, Vector3 normal);
30+
31+
/// <summary>
32+
/// Add a new vertex with position, normal and color.
33+
/// </summary>
34+
/// <param name="position">The vertex position.</param>
35+
/// <param name="normal">The vertex normal.</param>
36+
/// <param name="color">The vertex color.</param>
37+
void AddVertex(Vector3 position, Vector3 normal, Vector4 color);
38+
39+
/// <summary>
40+
/// Add a new vertex with position, normal, texture coordinate.
41+
/// </summary>
42+
/// <param name="position">The vertex position.</param>
43+
/// <param name="normal">The vertex normal.</param>
44+
/// <param name="texCoords">The vertex texture coordinate.</param>
45+
void AddVertex(Vector3 position, Vector3 normal, Vector2 texCoords);
46+
47+
/// <summary>
48+
/// Add a new vertex with position, normal, texture coordinate and color.
49+
/// </summary>
50+
/// <param name="position">The vertex position.</param>
51+
/// <param name="normal">The vertex normal.</param>
52+
/// <param name="texCoords">The vertex texture coordinate.</param>
53+
/// <param name="color">The vertex color.</param>
54+
void AddVertex(Vector3 position, Vector3 normal, Vector2 texCoords, Vector4 color);
55+
}
56+
}

‎src/CSG/IShapeBuilder.cs‎

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
using System.ComponentModel;
44
using System.Numerics;
55

6-
public interface IShapeBuilder
6+
public interface IShapeBuilder : IGeometryBuilder
77
{
8-
/// <summary>
9-
/// Gets the current vertex index.
10-
/// </summary>
11-
int CurrentVertex { get; }
12-
138
/// <summary>
149
/// Gets or sets the default color.
1510
/// </summary>
@@ -25,50 +20,6 @@ public interface IShapeBuilder
2520
/// </summary>
2621
Vector3 LocalScale { get; set; }
2722

28-
/// <summary>
29-
/// Add a new vertex.
30-
/// </summary>
31-
/// <param name="vertex"></param>
32-
void AddVertex(Vertex vertex);
33-
34-
/// <summary>
35-
/// Add a new vertex with position and normal.
36-
/// </summary>
37-
/// <param name="position">The vertex position.</param>
38-
/// <param name="normal">The vertex normal.</param>
39-
void AddVertex(Vector3 position, Vector3 normal);
40-
41-
/// <summary>
42-
/// Add a new vertex with position, normal and color.
43-
/// </summary>
44-
/// <param name="position">The vertex position.</param>
45-
/// <param name="normal">The vertex normal.</param>
46-
/// <param name="color">The vertex color.</param>
47-
void AddVertex(Vector3 position, Vector3 normal, Vector4 color);
48-
49-
/// <summary>
50-
/// Add a new vertex with position, normal, texture coordinate.
51-
/// </summary>
52-
/// <param name="position">The vertex position.</param>
53-
/// <param name="normal">The vertex normal.</param>
54-
/// <param name="texCoords">The vertex texture coordinate.</param>
55-
void AddVertex(Vector3 position, Vector3 normal, Vector2 texCoords);
56-
57-
/// <summary>
58-
/// Add a new vertex with position, normal, texture coordinate and color.
59-
/// </summary>
60-
/// <param name="position">The vertex position.</param>
61-
/// <param name="normal">The vertex normal.</param>
62-
/// <param name="texCoords">The vertex texture coordinate.</param>
63-
/// <param name="color">The vertex color.</param>
64-
void AddVertex(Vector3 position, Vector3 normal, Vector2 texCoords, Vector4 color);
65-
66-
/// <summary>
67-
/// Add a new index.
68-
/// </summary>
69-
/// <param name="index">The index.</param>
70-
void AddIndex(int index);
71-
7223
/// <summary>
7324
/// Clear the current vertices and indicies.
7425
/// </summary>

0 commit comments

Comments
 (0)