private static List <Box> Cut(List <Box> list, Box toCut, Vec3.Component c, float val) { List <Box> newList = new List <Box>(); foreach (Box b in list) { b.Cut(toCut, c, val, newList); } return(newList); }
public static Vec3 ProjectPointToPlane(Vec3 point, Vec3 planeOrigin, Vec3 planeAxis1, Vec3 planeAxis2, Vec3.Component projectionDirection) { Vec2 orig = planeOrigin.ProjectTo(projectionDirection); Vec2 ax1 = planeAxis1.ProjectTo(projectionDirection); Vec2 ax2 = planeAxis2.ProjectTo(projectionDirection); Vec2 p = point.ProjectTo(projectionDirection); Vec2 dummy; double t1, t2; Vec2.Intersection(orig, orig + ax1, p, p + ax2, out t1, out dummy); Vec2.Intersection(orig, orig + ax2, p, p + ax1, out t2, out dummy); return(planeOrigin + t1 * planeAxis1 + t2 * planeAxis2); }
static Dictionary <Vec2, int> OrthoProject(List <Vec3> list, Vec3.Component direction) { Dictionary <Vec2, int> dic = new Dictionary <Vec2, int>(); int count = 0; foreach (Vec3 v in list) { Vec2 v2 = v.ProjectTo(direction); if (!dic.ContainsKey(v2)) { dic.Add(v2, count); } ++count; } return(dic); }
private void Cut(Box toCut, Vec3.Component c, float val, List <Box> newList) { if (val <= min.GetComponent(c) || val >= max.GetComponent(c)) { newList.Add(new Box(min, max)); return; } Box b1 = new Box(min, max); b1.max.SetComponent(c, val); Box b2 = new Box(min, max); b2.min.SetComponent(c, val); newList.Add(b1); newList.Add(b2); }
public static List <List <Vec3> > PolygonToTriangleStrips(List <Vec3> polygon) { Vec3 normal = (polygon[1] - polygon[0]).Cross(polygon[2] - polygon[0]); normal.Normalize(); double max = Math.Max(Math.Abs(normal.X), Math.Max(Math.Abs(normal.Y), Math.Abs(normal.Z))); Vec3.Component dir = Vec3.Component.X; if (Math.Abs(normal.Y) == max) { dir = Vec3.Component.Y; } else if (Math.Abs(normal.Z) == max) { dir = Vec3.Component.Z; } polygon.RemoveAt(polygon.Count - 1); Dictionary <Vec2, int> proj = OrthoProject(polygon, dir); List <List <Vec2> > poly2d = PolygonToTriangleStrips(proj.Keys); List <List <Vec3> > triStrips = new List <List <Vec3> >(); foreach (List <Vec2> vlist in poly2d) { List <Vec3> strip = new List <Vec3>(); foreach (Vec2 v in vlist) { if (!proj.ContainsKey(v)) { strip.Add(Vec3.ProjectPointToPlane(Vec2.CreateVec3(v, dir), polygon[0], polygon[1] - polygon[0], polygon[2] - polygon[0], dir)); } else { strip.Add(polygon[proj[v]]); } } triStrips.Add(strip); } return(triStrips); }
public Vec3 GetPointWithComponentAt(Vec3.Component component, float p) { float x1 = component == Vec3.Component.X ? p1.Y : p1.X; float x2 = component == Vec3.Component.X ? p2.Y : p2.X; float y1 = component == Vec3.Component.Z ? p1.Y : p1.Z; float y2 = component == Vec3.Component.Z ? p2.Y : p2.Z; float t1 = p1.GetComponent(component); float t2 = p2.GetComponent(component); if (t1 == t2) { return(null); } float factor = (p - t1) / (t2 - t1); float x = x1 + factor * (x2 - x1); float y = y1 + factor * (y2 - y1); Vec3 ret = null; switch (component) { case Vec3.Component.X: ret = new Vec3(p, x, y); break; case Vec3.Component.Y: ret = new Vec3(x, p, y); break; case Vec3.Component.Z: ret = new Vec3(x, y, p); break; } return(ret); }