//#endfor instanced to 'Double3' //#foreach instanced to 'Float2' /// <summary> /// Gets triangle. /// </summary> /// <param name="index">The triangle triangle index.</param> /// <remarks>This is not performance wise getter.</remarks> /// <returns></returns> public Triangle2f Get2f(uint index) { Triangle2f t = new Triangle2f(); Get(CommonComponents.Position, index, t); return(t); }
/// <summary> /// Gets triangle. /// </summary> /// <param name="index">The triangle index.</param> /// <param name="positionComponent">The component where we look for position, must be /// correct format.</param> /// <remarks>This is not performance wise getter.</remarks> public Triangle2f Get2f(string positionComponent, uint index) { Triangle2f t = new Triangle2f(); Get(positionComponent, index, t); return(t); }
/// <summary> /// An array getter, optimized by reusing mappings. /// </summary> /// <param name="triangleIndex">The triangle index offset.</param> /// <param name="positionComponent">The position component.</param> /// <param name="storage">Where to store result.</param> public void Get(string positionComponent, uint triangleIndex, Triangle2f[] storage) { if (triangleIndex + storage.Length >= shapeCount) { throw new ArgumentException("Index out of range, not that many triangles."); } if (indexBuffer != null) { uint[] indices = indexBuffer.Getui(triangleIndex * 3, (uint)storage.Length * 3); Vector2f[] data = query.Get2f(positionComponent, indices); for (int i = 0; i < storage.Length; i++) { storage[i] = new Triangle2f(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]); } } else { Vector2f[] data = query.Get2f(positionComponent, triangleIndex * 3, (uint)storage.Length * 3); for (int i = 0; i < storage.Length; i++) { storage[i] = new Triangle2f(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]); } } }
public void Set(Triangle2f t) { xpoints[0] = t.xpoints[0]; xpoints[1] = t.xpoints[1]; xpoints[2] = t.xpoints[2]; ypoints[0] = t.ypoints[0]; ypoints[1] = t.ypoints[1]; ypoints[2] = t.ypoints[2]; }
public void DrawTriangle(Triangle2f t, float x, float y, Color c) { if (t == null) { return; } float[] xpos = new float[3]; float[] ypos = new float[3]; xpos[0] = x + t.xpoints[0]; xpos[1] = x + t.xpoints[1]; xpos[2] = x + t.xpoints[2]; ypos[0] = y + t.ypoints[0]; ypos[1] = y + t.ypoints[1]; ypos[2] = y + t.ypoints[2]; DrawPolygon(xpos, ypos, 3, c); }
/// <summary> /// Fills a triangle at index. /// </summary> /// <param name="index">The index.</param> /// <param name="triangle">The triangle to be filled.</param> public void Get(uint index, [NotNull] Triangle2f triangle) { int idx = (int)index; if (IsIndexed) { triangle.A = vertices[(int)indices[idx * 3]]; triangle.B = vertices[(int)indices[idx * 3 + 1]]; triangle.C = vertices[(int)indices[idx * 3 + 2]]; } else { triangle.A = vertices[3 * idx]; triangle.B = vertices[3 * idx + 1]; triangle.C = vertices[3 * idx + 2]; } }
public static bool TriangleContainsPointCCW(Triangle2f triangle, Vector2f p) { if (Vector2f.Cross(p - triangle.A, triangle.B - triangle.A) > 0.0) { return(false); } if (Vector2f.Cross(p - triangle.B, triangle.C - triangle.B) > 0.0) { return(false); } if (Vector2f.Cross(p - triangle.C, triangle.A - triangle.C) > 0.0) { return(false); } return(true); }
public void Triangulate() { Polygon2f polygon = CreatePolygon2.FromBox(new Vector2f(-1), new Vector2f(1)); Mesh2f mesh = ConstraintedTriangulation2.Triangulate(polygon); Assert.AreEqual(4, mesh.VerticesCount); Assert.AreEqual(6, mesh.IndicesCount); for (int i = 0; i < mesh.IndicesCount / 3; i++) { Vector2f a = mesh.Positions[mesh.Indices[i * 3 + 0]]; Vector2f b = mesh.Positions[mesh.Indices[i * 3 + 1]]; Vector2f c = mesh.Positions[mesh.Indices[i * 3 + 2]]; Triangle2f tri = new Triangle2f(a, b, c); Assert.IsTrue(tri.SignedArea > 0); } }
public static bool TriangleContainsPoint(Triangle2f triangle, Vector2f p) { float pab = Vector2f.Cross(p - triangle.A, triangle.B - triangle.A); float pbc = Vector2f.Cross(p - triangle.B, triangle.C - triangle.B); if (Math.Sign(pab) != Math.Sign(pbc)) { return(false); } float pca = Vector2f.Cross(p - triangle.C, triangle.A - triangle.C); if (Math.Sign(pab) != Math.Sign(pca)) { return(false); } return(true); }
/// <summary> /// Gets triangle. /// </summary> /// <param name="index">The triangle triangle index.</param> /// <remarks>This accessor is not to be used in performance critical parts of applications.</remarks> /// <returns></returns> public void Get(string positionComponent, uint index, Triangle2f storage) { if (index >= shapeCount) { throw new ArgumentException("Index out of range, not that many triangles."); } if (indexBuffer != null) { uint[] indices = indexBuffer.Getui(index * 3, 3); storage.A = query.Get2f(positionComponent, indices[0]); storage.B = query.Get2f(positionComponent, indices[1]); storage.C = query.Get2f(positionComponent, indices[2]); } else { storage.A = query.Get2f(positionComponent, index * 3); storage.B = query.Get2f(positionComponent, index * 3 + 1); storage.C = query.Get2f(positionComponent, index * 3 + 2); } }
/// <summary> /// Obtains a collection of triangles. /// </summary> /// <param name="index">The base index.</param> /// <param name="count">Number of triangles.</param> /// <returns>The triangle collection.</returns> public Triangle2f[] Get(uint index, uint count) { Triangle2f[] data = new Triangle2f[count]; Get(index, data); return(data); }
public void DrawTriangle(Triangle2f t, Color c) { DrawTriangle(t, 0, 0, c); }
public void FillTriangle(Triangle2f t, Color c) { FillTriangle(t, 0, 0, c); }
/// <summary> /// An array getter, optimized by reusing mappings. /// </summary> /// <param name="triangleIndex">The triangle index offset.</param> /// <param name="positionComponent">The position component.</param> /// <param name="count">Number of triangles.</param> public Triangle2f[] Get2f(string positionComponent, uint triangleIndex, uint count) { Triangle2f[] data = new Triangle2f[count]; Get(positionComponent, triangleIndex, data); return(data); }
public static Vector2f ClosestPointOnTriangle(Triangle2f triangle, Vector2f p) { Vector2f ab = triangle.B - triangle.A; Vector2f ac = triangle.C - triangle.A; Vector2f ap = p - triangle.A; // Check if P in vertex region outside A float d1 = Vector2f.Dot(ab, ap); float d2 = Vector2f.Dot(ac, ap); if (d1 <= 0.0 && d2 <= 0.0) { // barycentric coordinates (1,0,0) return(triangle.A); } float v, w; // Check if P in vertex region outside B Vector2f bp = p - triangle.B; float d3 = Vector2f.Dot(ab, bp); float d4 = Vector2f.Dot(ac, bp); if (d3 >= 0.0 && d4 <= d3) { // barycentric coordinates (0,1,0) return(triangle.B); } // Check if P in edge region of AB, if so return projection of P onto AB float vc = d1 * d4 - d3 * d2; if (vc <= 0.0 && d1 >= 0.0f && d3 <= 0.0) { v = d1 / (d1 - d3); // barycentric coordinates (1-v,v,0) return(triangle.A + v * ab); } // Check if P in vertex region outside C Vector2f cp = p - triangle.C; float d5 = Vector2f.Dot(ab, cp); float d6 = Vector2f.Dot(ac, cp); if (d6 >= 0.0 && d5 <= d6) { // barycentric coordinates (0,0,1) return(triangle.C); } // Check if P in edge region of AC, if so return projection of P onto AC float vb = d5 * d2 - d1 * d6; if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) { w = d2 / (d2 - d6); // barycentric coordinates (1-w,0,w) return(triangle.A + w * ac); } // Check if P in edge region of BC, if so return projection of P onto BC float va = d3 * d6 - d5 * d4; if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) { w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); // barycentric coordinates (0,1-w,w) return(triangle.B + w * (triangle.C - triangle.B)); } // P inside face region. Compute Q through its barycentric coordinates (u,v,w) float denom = 1.0f / (va + vb + vc); v = vb * denom; w = vc * denom; // = u*a + v*b + w*c, u = va * denom = 1.0f - v - w return(triangle.A + ab * v + ac * w); }
/// <summary> /// Gets triangle. /// </summary> /// <param name="index">The triangle index.</param> /// <param name="positionComponent">The component where we look for position, must be /// Vector2f format.</param> /// <remarks>This is not performance wise getter.</remarks> public void Get(uint index, Triangle2f storage) { Get(CommonComponents.Position, index, storage); }