protected void setup() { int i = 0; var indexLookup = new Dictionary <int, Vector2f>(pathPoints.Count); for (i = 0; i < pathPoints.Count; ++i) { indexLookup.Add(i, pathPoints[i]); } horizontal = new int[pathPoints.Count]; vertical = new int[pathPoints.Count]; var keyValues = indexLookup.ToList(); i = 0; foreach (var kv in keyValues.OrderBy((_kv) => _kv.Value.x)) { horizontal[i++] = kv.Key; } i = 0; foreach (var kv in keyValues.OrderBy((_kv) => _kv.Value.y)) { vertical[i++] = kv.Key; } bounds = new Box2f() { min = new Vector2f(leftMost.x, lowest.y), max = new Vector2f(rightMost.x, highest.y) }; setupEnterExitEdgeFromLeft(); }
public static Vector2f ClosestPointOnBox(Box2f box, Vector2f p) { Vector2f c; if (p.x < box.Min.x) { c.x = box.Min.x; } else if (p.x > box.Max.x) { c.x = box.Max.x; } else { c.x = p.x; } if (p.y < box.Min.y) { c.y = box.Min.y; } else if (p.y > box.Max.y) { c.y = box.Max.y; } else { c.y = p.y; } return(c); }
private PenDrawingPath pathForBoxDBUG(Box2f box) { PenDrawingPath pp = new PenDrawingPath(); pp.Add(new PenMove() { destination = box.min * viewBoxToPaperScale, color = Color.red }); pp.Add(new PenMove() { destination = box.lowerRight * viewBoxToPaperScale }); pp.Add(new PenMove() { destination = box.max * viewBoxToPaperScale }); pp.Add(new PenMove() { destination = box.upperLeft * viewBoxToPaperScale }); pp.Add(new PenMove() { destination = box.min * viewBoxToPaperScale, color = Color.black }); return(pp); }
public static TriTree <P> TreeToContainBox <P>(Box2f box, int splitMax = 20) where P : IsoTriData { double slope = -Math.Tan(Math.PI / 3d); double size = box.size.y - (box.size.x / 2f) * slope; float si = (int)(size + 1.5f); return(new TriTree <P>(box.center - Vector2f.AxisY * box.size.y / 2f, new Vector2f(si * 2f / TriUtil.RootThree, si))); }
public static SierpinskiVectorTree TreeToContainBox(Box2f box, int splitMax = 20) { double slope = -Math.Tan(Math.PI / 3d); double size = box.size.y - (box.size.x / 2f) * slope; int si = (int)(size + 1.5f); return(new SierpinskiVectorTree(box.center - Vector2f.AxisY * box.size.y / 2f, new Vector2i(si, si), splitMax)); }
public static bool BoxIntersectsBox(Box2f box0, Box2f box1) { if (box0.Max.x < box1.Min.x || box0.Min.x > box1.Max.x) { return(false); } if (box0.Max.y < box1.Min.y || box0.Min.y > box1.Max.y) { return(false); } return(true); }
public static bool BoxContainsPoint(Box2f box, Vector2f p) { if (p.x > box.Max.x || p.x < box.Min.x) { return(false); } if (p.y > box.Max.y || p.y < box.Min.y) { return(false); } return(true); }
public static void Triangulate <MESH>(Polygon2f polygon, IMeshConstructor <MESH> constructor, ConformingCriteria crit = new ConformingCriteria()) { InsertPolygon(polygon); InsertSeeds(crit.seeds); Box2f bounds = Box2f.CalculateBounds(polygon.Positions); CGAL_InsertSeed(bounds.Min - 0.1f); MeshDescriptor des = Triangulate(crit.iterations, crit.angBounds, crit.lenBounds); CreateMesh(constructor, des); CGAL_Clear(); }
public static bool BoxIntersectsCircle(Box2f box, Circle2f circle) { if (circle.Center.x - circle.Radius > box.Max.x || circle.Center.x + circle.Radius < box.Min.x) { return(false); } if (circle.Center.y - circle.Radius > box.Max.y || circle.Center.y + circle.Radius < box.Min.y) { return(false); } return(true); }
private PenDrawingPath dotAtDBUG(Vector2f v, Matrix2f rot, Color c, float size = 5f) { var pp = new PenDrawingPath(); v = rot * v; Box2f box = new Box2f() { min = v, max = v + Vector2f.One * size }; foreach (Vector2f l in box.getHorizontalLines()) { pp.Add(new PenMove() { destination = l * viewBoxToPaperScale, color = c }); } pp.Add(new PenMove() { destination = v * viewBoxToPaperScale, up = true }); return(pp); }
public static float SqrDistanceFromBox(Box2f box, Vector2f p) { float sqDist = 0.0f; if (p.x < box.Min.x) { sqDist += (box.Min.x - p.x) * (box.Min.x - p.x); } if (p.x > box.Max.x) { sqDist += (p.x - box.Max.x) * (p.x - box.Max.x); } if (p.y < box.Min.y) { sqDist += (box.Min.y - p.y) * (box.Min.y - p.y); } if (p.y > box.Max.y) { sqDist += (p.y - box.Max.y) * (p.y - box.Max.y); } return(sqDist); }
private void OnDrawGizmosSelected() { Box2f box = new Box2f() { min = new Vector2f(-16f, 0f), max = new Vector2f(16f, 32f) }; Gizmos.color = Color.cyan; Box2f.GizmosDraw(box); stree = null; if (stree == null) { stree = SierpinskiVectorTree.TreeToContainBox(box.size, 16); foreach (var v in box.size.GridPoints(4f)) { var add = v * 1.5f + box.min - Vector2f.One * 4f; if (stree.root.Contains(add)) { Gizmos.color = Color.red; } else { Gizmos.color = Color.blue; } stree.Add(add); } } var lists = stree.GetAll(); float lerp = 0f; float lerpIncr = 1f / 8f; var tris = stree.GetChildren(); Vector3 zOff = Vector3.zero; foreach (var tri in tris) { Gizmos.color = Color.Lerp(tri.inverted ? Color.red : Color.yellow, tri.inverted ? Color.blue : Color.white, lerp); var triData = tri.GetData().ToList(); if (triData.Count == 0) { continue; } foreach (var v in triData) { Gizmos.DrawSphere(v + zOff, .3f); } var points = tri.GetTriPoints(); for (int i = 1; i <= points.Count; ++i) { Gizmos.DrawLine(points[i - 1] + zOff, points[i % points.Count] + zOff); } zOff.z += .3f; lerp += lerpIncr; lerp = lerp > 1 ? 0 : lerp; } }
public void Write(Box2f x) { Write(x.Min); Write(x.Max); }
public TSPPointSets getPoints(float scale = 1f) { TSPPointSets psets = new TSPPointSets(); Box2f imageBox = new Box2f { min = new Vector2f(0f, 0f), max = new Vector2f(texture.width, texture.height) * scale }; var sierTree = SierpinskiVectorTree.TreeToContainBox(imageBox, tspProbMaxCities); var tex = texture; if (tex == null) { Debug.Log("null. bmap name: " + bmapName); return(null); } Color pix; Vector2f v; for (int x = 0; x < tex.width; ++x) { for (int y = 0; y < tex.height; ++y) { pix = tex.GetPixel(x, y); if (pix.grayscale < .2f) { v = new Vector2f(x * scale, y * scale); sierTree.Add(v); } } } //Debug.Log("this many dark enough points: " + points.Count + " out of: " + (tex.width * tex.height)); List <List <Vector2f> > lists; lists = sierTree.GetAll().ToList(); // early out if (lists.Count == 0) { return(psets); } var aggregateShortLists = lists[0]; for (int i = 1; i < lists.Count; ++i) { var pointList = lists[i]; if (splitPointSets && pointList.Count > 3) { psets.Add(pointList); } else { foreach (var p in pointList) { aggregateShortLists.Add(p); } } } psets.Add(aggregateShortLists); return(psets); }
public TriTree <PixelTriData> getPixelTriTree(float scale = 1f) { //TSPPointSets psets = new TSPPointSets(); Box2f imageBox = new Box2f { min = new Vector2f(0f, 0f), max = new Vector2f(texture.width, texture.height) * scale }; var triTree = TriTree <PixelTriData> .TreeToContainBox <PixelTriData>(imageBox, tspProbMaxCities); //triTree.deviationThreshold = deviationThreshold; var tex = texture; if (tex == null) { Debug.Log("null. bmap name: " + bmapName); return(null); } Color pix; Vector2f v; for (int x = 0; x < tex.width; ++x) { for (int y = 0; y < tex.height; ++y) { pix = tex.GetPixel(x, y); // // TODO: handle grey scale images // Tricky: define regions based on blobs of pixels of similar grey-ness // Would help if: triangle regions (or square regions) could join themselves into sets (of similar grey levels)? // Try: divide picture into 'a lot' of triangles // At some point, each triangle should check it's members for tonal homogeneity. // If not enough homogeneity, divide again. // This would include white pixels! // With this set of homogeneous tone triangles // cull the points in each set based on median tone. // lighter tone, more points get culled // cull by again dividing the triangle, fewer times for lighter // each sub triangle gives only one point. (perhaps the average) // Sufficiently dark tonal regions can skip the second divide: we just want all of their points. // Sufficiently filled triangles can skip the averaging, just provide the center point. // //if(pix.grayscale < MaxGrayScale) //{ v = new Vector2f(x * scale, y * scale); var tri = triTree.root.Add(new PixelTriData(new PixelV { pos = v, color = pix })); if (tri != null) { tri.DivideR((IsoTriangle <PixelTriData> _tri) => { return(TriShouldSplit(_tri)); }); } //} } } return(triTree); }