internal void FitInside(Box box, Vector anchor) { var scale = box.Size().Div(BoundingBox().Size()).MinComponent(); var extra = box.Size().Sub(BoundingBox().Size().MulScalar(scale)); var matrix = Matrix.Identity; matrix = matrix.Translate(BoundingBox().Min.Negate()).Mul(matrix); matrix = matrix.Scale(new Vector(scale, scale, scale)).Mul(matrix); matrix = matrix.Translate(box.Min.Add(extra.Mul(anchor))).Mul(matrix); Transform(matrix); }
internal static Mesh NewSDFMesh(SDF sdf, Box box, double step) { var min = box.Min; var size = box.Size(); var nx = (int)Math.Ceiling(size.X / step); var ny = (int)Math.Ceiling(size.Y / step); var nz = (int)Math.Ceiling(size.Z / step); var sx = size.X / nx; var sy = size.Y / ny; var sz = size.Z / nz; List <Triangle> triangles = new List <Triangle>(); for (int x = 0; x < nx - 1; x++) { for (int y = 0; y < ny - 1; y++) { for (int z = 0; z < nz - 1; z++) { (var x0, var y0, var z0) = ((double)x * sx + min.X, (double)y * sy + min.Y, (double)z * sz + min.Z); (var x1, var y1, var z1) = (x0 + sx, y0 + sy, z0 + sz); var p = new Vector[8] { new Vector(x0, y0, z0), new Vector(x1, y0, z0), new Vector(x1, y1, z0), new Vector(x0, y1, z0), new Vector(x0, y0, z1), new Vector(x1, y0, z1), new Vector(x1, y1, z1), new Vector(x0, y1, z1) }; double[] v = new double[8]; for (int i = 0; i < 8; i++) { v[i] = sdf.Evaluate(p[i]); } if (mcPolygonize(p, v, 0) == null) { continue; } else { triangles.AddRange(mcPolygonize(p, v, 0)); } } } } return(Mesh.NewMesh(triangles.ToArray())); }