private static Mesh CreateMeshFromRays(List <Vector3> finalIsovistPoints, Circle maxCircle, int rayCount) { var badColor = new Color(0, 0, 1.0, 0.8); var goodColor = new Color(0, 1.0, 0.8, 0.8); var meshOut = new Mesh(); var cwXform = new Transform(); cwXform.Move(-1 * maxCircle.Center); cwXform.Rotate(Vector3.ZAxis, 360.0 / (rayCount * 2)); cwXform.Move(maxCircle.Center); var ccwXForm = new Transform(); ccwXForm.Move(-1 * maxCircle.Center); ccwXForm.Rotate(Vector3.ZAxis, 360.0 / (rayCount * -2)); ccwXForm.Move(maxCircle.Center); for (int i = 0; i < rayCount; i++) { var pt = finalIsovistPoints[i]; var distNormalized = pt.DistanceTo(maxCircle.Center) / maxCircle.Radius; distNormalized = Math.Min(distNormalized, 0.999); var color = badColor.Lerp(goodColor, distNormalized); var A = new Elements.Geometry.Vertex(maxCircle.Center, Vector3.ZAxis, color); var B = new Elements.Geometry.Vertex(ccwXForm.OfPoint(pt), Vector3.ZAxis, color); var C = new Elements.Geometry.Vertex(cwXform.OfPoint(pt), Vector3.ZAxis, color); meshOut.AddVertex(A); meshOut.AddVertex(B); meshOut.AddVertex(C); meshOut.AddTriangle(A, B, C); } return(meshOut); }
public void IntersectsAtVertex() { var a = new Vertex(new Vector3(-0.5, -0.5, 1.0)); var b = new Vertex(new Vector3(0.5, -0.5, 1.0)); var c = new Vertex(new Vector3(0, 0.5, 1.0)); var t = new Triangle(a, b, c); var r = new Ray(new Vector3(-0.5, -0.5, 0.0), Vector3.ZAxis); Vector3 xsect; Assert.True(r.Intersects(t, out xsect)); }
public void IsParallelTo() { var a = new Vertex(new Vector3(-0.5, -0.5, 1.0)); var b = new Vertex(new Vector3(0.5, -0.5, 1.0)); var c = new Vertex(new Vector3(0, 0.5, 1.0)); var t = new Triangle(a, b, c); var r = new Ray(Vector3.Origin, Vector3.XAxis); Vector3 xsect; Assert.False(r.Intersects(t, out xsect)); }
public void AddInvalidTriangle() { var a = new Vertex(new Vector3(0, 0, 0)); var b = new Vertex(new Vector3(1, 1, 1)); var mesh = new Mesh(); Assert.Throws <ArgumentException>(() => mesh.AddTriangle(a, b, b)); Assert.Throws <ArgumentException>(() => mesh.AddTriangle(a, a, b)); Assert.Throws <ArgumentException>(() => mesh.AddTriangle(new Triangle(new List <Vertex> { b, a, b }, Vector3.ZAxis))); }
public void TriangleIntersection() { var a = new Vertex(new Vector3(-0.5, -0.5, 1.0)); var b = new Vertex(new Vector3(0.5, -0.5, 1.0)); var c = new Vertex(new Vector3(0, 0.5, 1.0)); var t = new Triangle(a, b, c); var r = new Ray(Vector3.Origin, Vector3.ZAxis); Vector3 xsect; Assert.True(r.Intersects(t, out xsect)); r = new Ray(Vector3.Origin, Vector3.ZAxis.Negate()); Assert.False(r.Intersects(t, out xsect)); }
public void MeshElement() { this.Name = "Elements_MeshElement"; // <example> var mesh = new Mesh(); var gridSize = 10; for (var u = 0; u < gridSize; u += 1) { for (var v = 0; v < gridSize; v += 1) { var sinu = Math.Sin(-Math.PI + 2 * ((double)u / (double)gridSize * Math.PI)); var sinv = Math.Sin(-Math.PI + 2 * ((double)v / (double)gridSize * Math.PI)); var z = sinu + sinv; var vertex = new Vertex(new Vector3(u, v, z), color: Colors.Mint); mesh.AddVertex(vertex); if (u > 0 && v > 0) { var index = u * gridSize + v; var a = mesh.Vertices[index]; var b = mesh.Vertices[index - gridSize]; var c = mesh.Vertices[index - 1]; var d = mesh.Vertices[index - gridSize - 1]; var tri1 = new Triangle(a, b, c); var tri2 = new Triangle(c, b, d); mesh.AddTriangle(tri1); mesh.AddTriangle(tri2); } } } mesh.ComputeNormals(); var meshElement = new MeshElement(mesh, new Material("Lime", Colors.Lime)); //</example> this.Model.AddElement(meshElement); }
private void CreateSidesAndBottomMesh(Mesh mesh, int rowWidth, double depth, double cellHeight, double cellWidth, Vector3 origin) { // Track the last created "low" // point so that we can merge // vertices in the side meshes. Vertex lastL = null; Vertex lastR = null; Vertex lastT = null; Vertex lastB = null; (Vector3 U, Vector3 V)basisLeft = (default(Vector3), default(Vector3)); (Vector3 U, Vector3 V)basisRight = (default(Vector3), default(Vector3)); (Vector3 U, Vector3 V)basisTop = (default(Vector3), default(Vector3)); (Vector3 U, Vector3 V)basisBottom = (default(Vector3), default(Vector3)); this._baseVerts.Clear(); for (var u = 0; u < rowWidth - 1; u++) { if (u == 0) { basisLeft = Vector3.XAxis.Negate().ComputeDefaultBasisVectors(); basisRight = Vector3.XAxis.ComputeDefaultBasisVectors(); basisTop = Vector3.YAxis.ComputeDefaultBasisVectors(); basisBottom = Vector3.YAxis.Negate().ComputeDefaultBasisVectors(); } // Left side Vertex l1 = null; var i1 = u * rowWidth; var v1Existing = mesh.Vertices[i1]; var v1 = mesh.AddVertex(v1Existing.Position, normal: Vector3.XAxis.Negate()); if (lastL != null) { l1 = lastL; } else { var p = new Vector3(v1.Position.X, v1.Position.Y, depth); l1 = mesh.AddVertex(p); _baseVerts.Add(l1); } var i2 = i1 + rowWidth; var v2Existing = mesh.Vertices[i2]; var v2 = mesh.AddVertex(v2Existing.Position); var pl2 = new Vector3(v2.Position.X, v2.Position.Y, depth); var l2 = mesh.AddVertex(pl2); _baseVerts.Add(l2); lastL = l2; mesh.AddTriangle(l1, v1, v2); mesh.AddTriangle(l2, l1, v2); // Right side Vertex l3 = null; var i3 = u * (rowWidth) + (rowWidth - 1); var v3Existing = mesh.Vertices[i3]; var v3 = mesh.AddVertex(v3Existing.Position, normal: Vector3.XAxis); if (lastR != null) { l3 = lastR; } else { var p = new Vector3(v3.Position.X, v3.Position.Y, depth); l3 = mesh.AddVertex(p); _baseVerts.Add(l3); } var i4 = i3 + rowWidth; var v4Existing = mesh.Vertices[i4]; var v4 = mesh.AddVertex(v4Existing.Position); var pl4 = new Vector3(v4.Position.X, v4.Position.Y, depth); var l4 = mesh.AddVertex(pl4); _baseVerts.Add(l4); lastR = l4; mesh.AddTriangle(l3, v4, v3); mesh.AddTriangle(l3, l4, v4); // Top side Vertex l5 = null; var i5 = u; var v5Existing = mesh.Vertices[i5]; var v5 = mesh.AddVertex(v5Existing.Position, normal: Vector3.YAxis); if (lastT != null) { l5 = lastT; } else { var p = new Vector3(v5.Position.X, v5.Position.Y, depth); l5 = mesh.AddVertex(p); _baseVerts.Add(l5); } var i6 = i5 + 1; var v6Existing = mesh.Vertices[i6]; var v6 = mesh.AddVertex(v6Existing.Position); var pl6 = new Vector3(v6.Position.X, v6.Position.Y, depth); var l6 = mesh.AddVertex(pl6); _baseVerts.Add(l6); lastT = l6; mesh.AddTriangle(l5, v6, v5); mesh.AddTriangle(l5, l6, v6); // Bottom side Vertex l7 = null; var i7 = rowWidth * rowWidth - u - 1; var v7Existing = mesh.Vertices[i7]; var v7 = mesh.AddVertex(v7Existing.Position, normal: Vector3.YAxis.Negate()); if (lastB != null) { l7 = lastB; } else { var p = new Vector3(v7.Position.X, v7.Position.Y, depth); l7 = mesh.AddVertex(p); _baseVerts.Add(l7); } var i8 = i7 - 1; var v8Existing = mesh.Vertices[i8]; var v8 = mesh.AddVertex(v8Existing.Position); var pl8 = new Vector3(v8.Position.X, v8.Position.Y, depth); var l8 = mesh.AddVertex(pl8); _baseVerts.Add(l8); lastB = l8; mesh.AddTriangle(l7, v8, v7); mesh.AddTriangle(l7, l8, v8); } // Add the bottom var bb1 = mesh.AddVertex(origin + new Vector3(0, 0, depth)); var bb2 = mesh.AddVertex(origin + new Vector3((rowWidth - 1) * cellWidth, 0, depth)); var bb3 = mesh.AddVertex(origin + new Vector3((rowWidth - 1) * cellWidth, (rowWidth - 1) * cellHeight, depth)); var bb4 = mesh.AddVertex(origin + new Vector3(0, (rowWidth - 1) * cellHeight, depth)); _baseVerts.AddRange(new[] { bb1, bb2, bb3, bb4 }); mesh.AddTriangle(bb1, bb3, bb2); mesh.AddTriangle(bb1, bb4, bb3); mesh.ComputeNormals(); }
/// <summary> /// Add a vertex to the mesh. /// </summary> /// <param name="v">The vertex to add.</param> public Vertex AddVertex(Vertex v) { this.Vertices.Add(v); v.Index = (this.Vertices.Count) - 1; return(v); }