/* * Finds the area of the triangle formed by 3 vectors. */ public static double TriangleArea(Vect3 point1, Vect3 point2, Vect3 point3) { Vect3 triangleBase = (point1 - point2); Vect3 side = (point1 - point3); Vect3 height = side - side.ProjectOnVector(triangleBase); return((triangleBase.Length() * height.Length()) / 2); }
/* * Fills VoxelGrid from component faces using a digital differential analyzer method. */ public void CreateShellFromFaces(Component component) { foreach (Face face in component.faces) { List <Vect3> trianglePoints = ConvertFaceToLocal(face, component); Vect3 v1 = trianglePoints[0] / resolution; Vect3 v2 = trianglePoints[1] / resolution; Vect3 v3 = trianglePoints[2] / resolution; // v2->v3 should be shortest triangle side double v2v1length = (v2 - v1).Length(); double v3v2length = (v3 - v2).Length(); if (v3v2length > v2v1length) { Vect3 temp = v1; v1 = v3; v3 = temp; } double v3v1length = (v3 - v1).Length(); if (v3v2length > v3v1length) { Vect3 temp = v2; v2 = v1; v1 = temp; } Vect3 lp1 = v1; Vect3 lp2 = v1; Vect3 d2 = v2 - v1; Vect3 d3 = v3 - v1; double d2step = d2.Length(); double d3step = d3.Length(); double outerstep = d2step > d3step ? d2step : d3step; outerstep *= 2; d2 /= outerstep; d3 /= outerstep; int xi, yi, zi; xi = Convert.ToInt32(lp1.x); yi = Convert.ToInt32(lp1.y); zi = Convert.ToInt32(lp1.z); coordinateGrid[xi][yi][zi] = true; lp1 += d2; lp2 += d3; int k = 0; while (k <= outerstep) { Vect3 d = lp2 - lp1; double step, x, y, z; int i; step = d.Length(); step *= 2; if (step != 0) { d /= step; } x = lp1.x; y = lp1.y; z = lp1.z; i = 0; yi = Convert.ToInt32(y); zi = Convert.ToInt32(z); while (i <= step) { xi = Convert.ToInt32(x); coordinateGrid[xi][yi][zi] = true; yi = Convert.ToInt32(y); coordinateGrid[xi][yi][zi] = true; zi = Convert.ToInt32(z); coordinateGrid[xi][yi][zi] = true; x += d.x; y += d.y; z += d.z; i += 1; } lp1 += d2; lp2 += d3; k += 1; } } }