-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more primitive shapes; Bug fixes;
- Loading branch information
Showing
12 changed files
with
291 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.IO; | ||
using Qkmaxware.Geometry; | ||
using Qkmaxware.Geometry.IO; | ||
using Qkmaxware.Geometry.Primitives; | ||
|
||
namespace Astro.Testing { | ||
|
||
[TestClass] | ||
public class CapsuleTest : PrimitiveTest { | ||
[TestMethod] | ||
public void TestCapsule() { | ||
var geom = new Capsule(0.5, 2, Vec3.Zero, 16, 16); | ||
SaveGeometry("capsule", geom); | ||
} | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
Geometry.Test/suites/Geometry/Primitives/Hemisphere.test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.IO; | ||
using Qkmaxware.Geometry; | ||
using Qkmaxware.Geometry.IO; | ||
using Qkmaxware.Geometry.Primitives; | ||
|
||
namespace Astro.Testing { | ||
|
||
[TestClass] | ||
public class HemisphereTest : PrimitiveTest { | ||
[TestMethod] | ||
public void TestHemisphere() { | ||
var geom = new Hemisphere(1, Vec3.Zero, 16, 16); | ||
SaveGeometry("hemisphere", geom); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace Qkmaxware.Geometry.Primitives { | ||
|
||
public class Capsule : Mesh { | ||
|
||
private static Vec3 ToCartesian(double zrot, double inc, double r) { | ||
double sTheta = Math.Sin(inc); | ||
return new Vec3( | ||
r * sTheta * Math.Cos(zrot), | ||
r * sTheta * Math.Sin(zrot), | ||
r * Math.Cos(inc) | ||
); | ||
} | ||
|
||
private static List<Triangle> GenerateHemisphere( | ||
double radius, | ||
Vec3 centre, | ||
int horiResolution, | ||
int vertResolution | ||
) { | ||
List<Triangle> triangles = new List<Triangle>(); | ||
|
||
double xStep = (2 * Math.PI) / horiResolution; | ||
double yStep = (0.5 * Math.PI) / (vertResolution - 1); | ||
|
||
/* | ||
top | ||
/ \ | ||
te-ti | ||
| /| | ||
------- | ||
*/ | ||
|
||
for (int i = 1; i <= horiResolution; i++) { | ||
double prevXAngle = (i - 1) * xStep; | ||
double xAngle = i * xStep; | ||
|
||
for(int j = 1; j < vertResolution; j++) { | ||
if (j == 1) { | ||
// Top Triangle | ||
double yAngle = (j) * yStep; | ||
|
||
Vec3 top = new Vec3(0, 0, radius) + centre; | ||
Vec3 be = ToCartesian(xAngle, yAngle, radius) + centre; | ||
Vec3 bi = ToCartesian(prevXAngle, yAngle, radius) + centre; | ||
|
||
triangles.Add(new Triangle(bi, be, top)); | ||
} else { | ||
double yAngle = (j) * yStep; | ||
double prevYAngle = (j - 1) * yStep; | ||
|
||
// Middle rectangle | ||
{ | ||
Vec3 te = ToCartesian(xAngle, yAngle, radius) + centre; | ||
Vec3 ti = ToCartesian(prevXAngle, yAngle, radius) + centre; | ||
|
||
Vec3 be = ToCartesian(xAngle, prevYAngle, radius) + centre; | ||
Vec3 bi = ToCartesian(prevXAngle, prevYAngle, radius) + centre; | ||
|
||
triangles.Add(new Triangle(ti, te, be)); | ||
triangles.Add(new Triangle(bi, ti, be)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return triangles; | ||
} | ||
|
||
private static List<Triangle> GenerateCylinder(double upperRadius, double lowerRadius, double h, Vec3 centre, int resolution) { | ||
List<Triangle> triangles = new List<Triangle>(); | ||
double step = 2 * Math.PI / resolution; | ||
double hStep = h / 2; | ||
|
||
for (int i = 1; i <= resolution; i++) { | ||
double prevAngle = (i - 1) * step; | ||
double xi = Math.Cos(prevAngle); | ||
double yi = Math.Sin(prevAngle); | ||
|
||
double angle = i * step; | ||
double xe = Math.Cos(angle); | ||
double ye = Math.Sin(angle); | ||
|
||
/* | ||
te -- ti | ||
| / | | ||
| / | | ||
be -- bi | ||
*/ | ||
// Create top points | ||
Vec3 te = new Vec3(xe * upperRadius, ye * upperRadius, hStep) + centre; | ||
Vec3 ti = new Vec3(xi * upperRadius, yi * upperRadius, hStep) + centre; | ||
// Create bottom points | ||
Vec3 be = new Vec3(xe * lowerRadius, ye * lowerRadius, -hStep) + centre; | ||
Vec3 bi = new Vec3(xi * lowerRadius, yi * lowerRadius, -hStep) + centre; | ||
|
||
// Create triangles | ||
triangles.Add(new Triangle(be, te, ti)); | ||
triangles.Add(new Triangle(be, ti, bi)); | ||
} | ||
|
||
return triangles; | ||
} | ||
|
||
public Capsule(double radius, double height, Vec3 centre, int horizontalResolution = 8, int verticalResolution = 8) { | ||
height = Math.Max(height, 2*radius); // height must be greater than 2*radius or else its a squashed sphere | ||
|
||
// Capsule is a cylinder | ||
var cylinderHeight = height - 2 * radius; | ||
var cylinder = GenerateCylinder(radius, radius, cylinderHeight, centre, horizontalResolution); | ||
this.AppendRange(cylinder); | ||
|
||
// Plus 2 spheres | ||
Vec3 delta = cylinderHeight * 0.5 * Vec3.K; | ||
var topSphere = GenerateHemisphere(radius, centre + delta, horizontalResolution, verticalResolution); | ||
this.AppendRange(topSphere); | ||
this.AppendRange(topSphere.Select(tri => new Triangle(tri.Item1.Flipped, tri.Item3.Flipped ,tri.Item2.Flipped))); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Qkmaxware.Geometry.Primitives { | ||
|
||
/// <summary> | ||
/// Half sphere mesh | ||
/// </summary> | ||
public class Hemisphere : Mesh { | ||
|
||
private static Vec3 ToCartesian(double zrot, double inc, double r) { | ||
double sTheta = Math.Sin(inc); | ||
return new Vec3( | ||
r * sTheta * Math.Cos(zrot), | ||
r * sTheta * Math.Sin(zrot), | ||
r * Math.Cos(inc) | ||
); | ||
} | ||
|
||
private static List<Triangle> Generate( | ||
double radius, | ||
Vec3 centre, | ||
int horiResolution, | ||
int vertResolution, | ||
bool useEndCap = true | ||
) { | ||
List<Triangle> triangles = new List<Triangle>(); | ||
|
||
double xStep = (2 * Math.PI) / horiResolution; | ||
double yStep = (0.5 * Math.PI) / (vertResolution - 1); | ||
|
||
/* | ||
top | ||
/ \ | ||
te-ti | ||
| /| | ||
------- | ||
*/ | ||
|
||
for (int i = 1; i <= horiResolution; i++) { | ||
double prevXAngle = (i - 1) * xStep; | ||
double xAngle = i * xStep; | ||
|
||
for(int j = 1; j < vertResolution; j++) { | ||
if (j == 1) { | ||
// Top Triangle | ||
double yAngle = (j) * yStep; | ||
|
||
Vec3 top = new Vec3(0, 0, radius) + centre; | ||
Vec3 be = ToCartesian(xAngle, yAngle, radius) + centre; | ||
Vec3 bi = ToCartesian(prevXAngle, yAngle, radius) + centre; | ||
|
||
triangles.Add(new Triangle(bi, be, top)); | ||
} else { | ||
double yAngle = (j) * yStep; | ||
double prevYAngle = (j - 1) * yStep; | ||
|
||
// Bottom Triangle | ||
if (useEndCap && j == vertResolution - 1) { | ||
Vec3 bottom = new Vec3(0, 0, 0) + centre; | ||
Vec3 te = ToCartesian(xAngle, yAngle, radius) + centre; | ||
Vec3 ti = ToCartesian(prevXAngle, yAngle, radius) + centre; | ||
|
||
triangles.Add(new Triangle(ti, bottom, te)); | ||
} | ||
|
||
// Middle rectangle | ||
{ | ||
Vec3 te = ToCartesian(xAngle, yAngle, radius) + centre; | ||
Vec3 ti = ToCartesian(prevXAngle, yAngle, radius) + centre; | ||
|
||
Vec3 be = ToCartesian(xAngle, prevYAngle, radius) + centre; | ||
Vec3 bi = ToCartesian(prevXAngle, prevYAngle, radius) + centre; | ||
|
||
triangles.Add(new Triangle(ti, te, be)); | ||
triangles.Add(new Triangle(bi, ti, be)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return triangles; | ||
} | ||
|
||
/// <summary> | ||
/// Create a hemisphere | ||
/// </summary> | ||
/// <param name="radius">radius</param> | ||
/// <param name="centre">centre point</param> | ||
/// <param name="horizontalResolution">longitude subdivision levels</param> | ||
/// <param name="verticalResolution">latitude subdivision level</param> | ||
public Hemisphere(double radius, Vec3 centre, int horizontalResolution = 8, int verticalResolution = 8) : base(Generate(radius, centre, horizontalResolution, verticalResolution)) {} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.