Permalink
Browse files

Added tests for prisms

Made prism detect incorrect winding

Signed-off-by: Martin Evans <martindevans@gmail.com>
  • Loading branch information...
martindevans committed Mar 28, 2012
1 parent 4ac2b2e commit 68144059956213e7e5c8e509ea4d21dc56690948
@@ -49,6 +49,7 @@
<Compile Include="Bounds.cs" />
<Compile Include="Descriptions.cs" />
<Compile Include="PolygonTest.cs" />
+ <Compile Include="PrismTest.cs" />
<Compile Include="Raycast.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SphereTest.cs" />
@@ -45,23 +45,23 @@ public void CylinderDescription()
public void PrismDescription()
{
Prism c = new Prism(3, new Vector2[]
- {
- new Vector2(0, 10),
- new Vector2(3, 2),
+ {
new Vector2(-1, 17),
+ new Vector2(3, 2),
+ new Vector2(0, 10),
});
Assert.AreEqual(9, c.Description.Count());
Assert.AreEqual("prism", c.Description.First());
Assert.AreEqual((float)3, c.Description.Skip(1).First());
Assert.AreEqual((uint)3, c.Description.Skip(2).First());
- Assert.AreEqual((float)0, c.Description.Skip(3).First());
- Assert.AreEqual((float)10, c.Description.Skip(4).First());
+ Assert.AreEqual((float)-1, c.Description.Skip(3).First());
+ Assert.AreEqual((float)17, c.Description.Skip(4).First());
Assert.AreEqual((float)3, c.Description.Skip(5).First());
Assert.AreEqual((float)2, c.Description.Skip(6).First());
- Assert.AreEqual((float)-1, c.Description.Skip(7).First());
- Assert.AreEqual((float)17, c.Description.Skip(8).First());
+ Assert.AreEqual((float)0, c.Description.Skip(7).First());
+ Assert.AreEqual((float)10, c.Description.Skip(8).First());
}
[TestMethod]
@@ -0,0 +1,26 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Xna.Csg.Primitives;
+using Microsoft.Xna.Framework;
+
+namespace CsgTests
+{
+ [TestClass]
+ public class PrismTest
+ {
+ [TestMethod]
+ [ExpectedException(typeof(ArgumentException))]
+ public void IncorrectWinding()
+ {
+ Prism c = new Prism(1, new Vector2[]
+ {
+ new Vector2(0, 0),
+ new Vector2(1, 1),
+ new Vector2(-1, 1),
+ });
+ }
+ }
+}
@@ -58,28 +58,23 @@ protected override void Initialize()
var c = new Cylinder(10).Transform(Matrix.CreateScale(5f * scale, 20 * scale, 5f * scale) * Matrix.CreateRotationZ(MathHelper.PiOver2));
var d = new Cube().Transform(Matrix.CreateScale(15f * scale));
- var e = new Sphere(2).Transform(Matrix.CreateScale(8 * scale));
var abc = a.Clone();
abc.Union(b);
abc.Union(c);
- var de = d.Clone();
- de.Union(e);
+ shapeToRender = d.Clone();
+ shapeToRender.Subtract(abc);
- shapeToRender = new Sphere(0).Transform(Matrix.CreateScale(2));
- //shapeToRender.Subtract(new Cube().Transform(Matrix.CreateScale(3f, 3f, 3f) * Matrix.CreateTranslation(0, 0, 0)));
-
- //shapeToRender = new Prism(1, new Vector2[]
- //{
- // new Vector2(-1.2f, -1.2f),
- // new Vector2(1.2f, -1.2f),
- // new Vector2(1.2f, 1.2f),
- // new Vector2(-1.2f, 1.2f),
- //});
- //shapeToRender.Union(new Cylinder(1));
-
- sphere = new Sphere(2).Transform(Matrix.CreateScale(1f));
+ shapeToRender.Intersect(new Prism(1, new Vector2[]
+ {
+ new Vector2(-1.2f, -1.2f),
+ new Vector2(1.2f, -1.2f),
+ new Vector2(1.2f, 1.2f),
+ new Vector2(0f, 2f),
+ new Vector2(-1.2f, 1.2f),
+ }));
+ //shapeToRender.Intersect(new Cylinder(1));
base.Initialize();
}
@@ -100,7 +95,7 @@ protected override void LoadContent()
wireframeState = new RasterizerState()
{
FillMode = Microsoft.Xna.Framework.Graphics.FillMode.WireFrame,
- CullMode = CullMode.CullCounterClockwiseFace,
+ CullMode = CullMode.CullClockwiseFace,
};
}
@@ -151,7 +146,7 @@ protected override void Draw(GameTime gameTime)
GraphicsDevice.RasterizerState = wireframeState;
//new RasterizerState()
// {
- // CullMode = CullMode.CullCounterClockwiseFace,
+ // CullMode = CullMode.CullClockwiseFace,
// };
GraphicsDevice.DepthStencilState = new DepthStencilState()
{
@@ -28,7 +28,7 @@ private static Vector2[] CalculatePoints(uint rotations, float initialAngle, flo
for (int i = 0; i < rotations; i++)
{
float angle = MathHelper.TwoPi / rotations * i + initialAngle;
- positions[rotations - i - 1] = new Vector2((float)Math.Sin(angle), (float)Math.Cos(angle)) * radius;
+ positions[i] = new Vector2((float)Math.Sin(angle), (float)Math.Cos(angle)) * radius;
}
return positions;
@@ -31,6 +31,10 @@ internal static IEnumerable<Polygon> CreatePolygons(float height, Vector2[] poin
//top circle
var top = new Polygon(points.Select(a => new Vector3(a.X, height / 2, a.Y)).Select(a => new Vertex(a, Vector3.Zero)));
top.CalculateVertexNormals();
+
+ if (top.Plane.D > 0)
+ throw new ArgumentException("Prism wound wrong way");
+
yield return top;
//bottom circle

0 comments on commit 6814405

Please sign in to comment.