/// <summary> /// Adds a vertex to the buffer. /// </summary> /// <param name="item"></param> public void Add(VertexWrap item) { EnsureCapacity(); items[count++] = item; }
/// <summary> /// Check if the vertex is "visible" from the face. /// The vertex is "over face" if the return value is > Constants.PlaneDistanceTolerance. /// </summary> /// <param name="v"></param> /// <param name="f"></param> /// <returns>The vertex is "over face" if the result is positive.</returns> public double GetVertexDistance(VertexWrap v, ConvexFaceInternal f) { double[] normal = f.Normal; double[] p = v.PositionData; double distance = f.Offset; for (int i = 0; i < Dimension; i++) distance += normal[i] * p[i]; return distance; }
/// <summary> /// Finds normal vector of a hyper-plane given by vertices. /// Stores the results to normalData. /// </summary> /// <param name="vertices"></param> /// <param name="normalData"></param> public void FindNormalVector(VertexWrap[] vertices, double[] normalData) { switch (Dimension) { case 2: FindNormalVector2D(vertices, normalData); break; case 3: FindNormalVector3D(vertices, normalData); break; case 4: FindNormalVector4D(vertices, normalData); break; default: { for (var i = 0; i < Dimension; i++) nDNormalSolveVector[i] = 1.0; for (var i = 0; i < Dimension; i++) { var row = jaggedNDMatrix[i]; var pos = vertices[i].Vertex.Position; for (int j = 0; j < Dimension; j++) row[j] = pos[j]; } GaussElimination(Dimension, jaggedNDMatrix, nDNormalSolveVector, normalData); Normalize(normalData); break; } } }
/// <summary> /// Finds 2D normal vector. /// </summary> /// <param name="vertices"></param> /// <param name="normal"></param> void FindNormalVector2D(VertexWrap[] vertices, double[] normal) { SubtractFast(vertices[1].PositionData, vertices[0].PositionData, ntX); var x = ntX; var nx = -x[1]; var ny = x[0]; double norm = System.Math.Sqrt(nx * nx + ny * ny); double f = 1.0 / norm; normal[0] = f * nx; normal[1] = f * ny; }
/// <summary> /// Finds 3D normal vector. /// </summary> /// <param name="vertices"></param> /// <param name="normal"></param> void FindNormalVector3D(VertexWrap[] vertices, double[] normal) { SubtractFast(vertices[1].PositionData, vertices[0].PositionData, ntX); SubtractFast(vertices[2].PositionData, vertices[1].PositionData, ntY); var x = ntX; var y = ntY; var nx = x[1] * y[2] - x[2] * y[1]; var ny = x[2] * y[0] - x[0] * y[2]; var nz = x[0] * y[1] - x[1] * y[0]; double norm = System.Math.Sqrt(nx * nx + ny * ny + nz * nz); double f = 1.0 / norm; normal[0] = f * nx; normal[1] = f * ny; normal[2] = f * nz; }
/// <summary> /// Finds 4D normal vector. /// </summary> /// <param name="vertices"></param> /// <param name="normal"></param> void FindNormalVector4D(VertexWrap[] vertices, double[] normal) { SubtractFast(vertices[1].PositionData, vertices[0].PositionData, ntX); SubtractFast(vertices[2].PositionData, vertices[1].PositionData, ntY); SubtractFast(vertices[3].PositionData, vertices[2].PositionData, ntZ); var x = ntX; var y = ntY; var z = ntZ; // This was generated using Mathematica var nx = x[3] * (y[2] * z[1] - y[1] * z[2]) + x[2] * (y[1] * z[3] - y[3] * z[1]) + x[1] * (y[3] * z[2] - y[2] * z[3]); var ny = x[3] * (y[0] * z[2] - y[2] * z[0]) + x[2] * (y[3] * z[0] - y[0] * z[3]) + x[0] * (y[2] * z[3] - y[3] * z[2]); var nz = x[3] * (y[1] * z[0] - y[0] * z[1]) + x[1] * (y[0] * z[3] - y[3] * z[0]) + x[0] * (y[3] * z[1] - y[1] * z[3]); var nw = x[2] * (y[0] * z[1] - y[1] * z[0]) + x[1] * (y[2] * z[0] - y[0] * z[2]) + x[0] * (y[1] * z[2] - y[2] * z[1]); double norm = System.Math.Sqrt(nx * nx + ny * ny + nz * nz + nw * nw); double f = 1.0 / norm; normal[0] = f * nx; normal[1] = f * ny; normal[2] = f * nz; normal[3] = f * nw; }