diff --git a/docs/images/saddle_region.asy b/docs/images/saddle_region.asy new file mode 100644 index 0000000..f4ad105 --- /dev/null +++ b/docs/images/saddle_region.asy @@ -0,0 +1,267 @@ +settings.outformat = "svg"; +settings.render = 0; +settings.prc = false; + +import three; + +real px = 0.75bp; + +pen ink = rgb("231f20"); +pen meshPen = ink + 0.6px; +pen regionFill = rgb("f2d4d0"); +pen regionPen = rgb("a8322a") + 1.0px; +pen ridgeColor = rgb("c26a17"); +pen gullyColor = rgb("1f5c99"); +pen sliceFill = gray(0.93) + opacity(0.75); +pen guidePen = gray(0.55) + 0.5px + linetype(new real[] {4, 3}); +pen hullPen = ink + 0.7px + linetype(new real[] {5, 3}); + +real arrowSize = 5px; +triple lightDirection = unit((1, 4, 2)); + +// The Saddle Vertex sample hub, spokes alternate 8 degree ridges and 32 degree gullies +int spokeCount = 6; +real fanRadius = 1.5; +real[] drops = {8, 32, 8, 32, 8, 32}; + +// The slice plane sits this far above the hub. Planes through the hub cut it in lines. +real sliceHeight = 1.6; +real sliceRadius = sliceHeight * Tan(40); + +triple[] spokes; +for (int i = 0; i < spokeCount; ++i) +{ + real angle = 60 * i; + spokes.push(fanRadius * (Cos(angle), -Tan(drops[i]), Sin(angle))); +} + +bool IsRidge(int i) +{ + return i % 2 == 0; +} + +pen SpokePen(int i) +{ + return IsRidge(i) ? ridgeColor : gullyColor; +} + +// Gnomonic coordinates: a normal n maps to (n.x, n.z) / n.y on the slice, so every plane through +// the hub maps to a line and n . e <= 0 maps to a half plane +pair Gnomonic(triple n) +{ + return (n.x / n.y, n.z / n.y); +} + +// Half plane of spoke i on the unit slice: dot(p, u) <= offset +pair SpokeDirection(int i) +{ + return (Cos(60 * i), Sin(60 * i)); +} + +real SpokeOffset(int i) +{ + return Tan(drops[i]); +} + +// Sutherland Hodgman clip of a convex polygon against dot(p, u) <= offset +pair[] ClipPolygon(pair[] poly, pair u, real offset) +{ + pair[] result; + int n = poly.length; + for (int k = 0; k < n; ++k) + { + pair a = poly[k]; + pair b = poly[(k + 1) % n]; + real da = dot(a, u) - offset; + real db = dot(b, u) - offset; + if (da <= 0) + result.push(a); + if (da * db < 0) + result.push(a + da / (da - db) * (b - a)); + } + return result; +} + +pair[] region = { (-10, -10), (10, -10), (10, 10), (-10, 10) }; +for (int i = 0; i < spokeCount; ++i) + region = ClipPolygon(region, SpokeDirection(i), SpokeOffset(i)); + +// Each face normal is perpendicular to its two spokes, so it lies on both of their lines +pair[] faceNormals; +for (int i = 0; i < spokeCount; ++i) +{ + triple n = cross(spokes[(i + 1) % spokeCount], spokes[i]); + if (n.y < 0) + n = -n; + faceNormals.push(Gnomonic(n)); +} + +// A face normal leans past the gully it borders, so sort by angle before drawing the hull +pair[] hullPoints = sort(faceNormals, new bool(pair a, pair b) { return degrees(a, false) < degrees(b, false); }); + +// End points of the line of spoke i, clipped to a disk of the given radius on the unit slice +pair[] SpokeLine(int i, real radius) +{ + pair u = SpokeDirection(i); + pair t = (-u.y, u.x); + real c = SpokeOffset(i); + real half = sqrt(radius * radius - c * c); + return new pair[] { c * u - half * t, c * u + half * t }; +} + +pen Shade(path3 face) +{ + triple n = unit(cross(point(face, 1) - point(face, 0), point(face, 2) - point(face, 0))); + real k = 0.74 + 0.26 * abs(dot(n, lightDirection)); + return rgb(k * 0.843, k * 0.890, k * 0.957); +} + +triple Lift(pair p) +{ + return sliceHeight * (p.x, 1, p.y); +} + +frame[] panels; +string[] titles; + +// 3D view: the fan, the slice above it and the cone of the region +{ + currentprojection = orthographic((3.4, 3.0, 2.6), up = Y); + picture pic; + size(pic, 250px, 250px); + triple hub = (0, 0, 0); + + for (int i = 0; i < spokeCount; ++i) + { + path3 t = hub -- spokes[i] -- spokes[(i + 1) % spokeCount] -- cycle; + draw(pic, surface(t), Shade(t), nolight); + draw(pic, t, meshPen); + } + + triple offset[] = {(0.1, 0, 0.1), (0.3, 0, 0.3), (0, 0, 0), (-0.1, 0, 0), (0, 0, -0.3), (0, 0, 0)}; + for (int i = 0; i < spokeCount; ++i) + { + draw(pic, hub -- spokes[i], SpokePen(i) + 1.4px); + label(pic, "$e_" + string(i) + "$", spokes[i] + offset[i], unit(Gnomonic((spokes[i].x, 1, 4 * spokes[i].z))), SpokePen(i)); + } + + // The boundary plane of one ridge spoke, it holds the hub and its line on the slice + pair[] line0 = SpokeLine(0, Tan(40)); + path3 wall = hub -- Lift(line0[0]) -- Lift(line0[1]) -- cycle; + draw(pic, surface(wall), ridgeColor + opacity(0.18), nolight); + draw(pic, hub -- Lift(line0[0]), ridgeColor + 0.5px + linetype(new real[] {4, 3})); + draw(pic, hub -- Lift(line0[1]), ridgeColor + 0.5px + linetype(new real[] {4, 3})); + + path3 slice = shift((0, sliceHeight, 0)) * rotate(90, X) * scale3(sliceRadius) * unitcircle3; + draw(pic, surface(slice), sliceFill, nolight); + draw(pic, slice, gray(0.6) + 0.5px); + + for (int i = 0; i < spokeCount; ++i) + { + pair[] ends = SpokeLine(i, Tan(40)); + draw(pic, Lift(ends[0]) -- Lift(ends[1]), SpokePen(i) + 0.8px); + } + + path3 top = Lift(region[0]); + for (int k = 1; k < region.length; ++k) + top = top -- Lift(region[k]); + top = top -- cycle; + draw(pic, surface(top), regionFill, nolight); + draw(pic, top, regionPen); + + for (pair p : region) + draw(pic, hub -- Lift(p), regionPen); + + dot(pic, hub, ink + 3px); + label(pic, "slice", Lift((0, -Tan(40))) + (0, 0.1, 0), N, gray(0.35)); + + panels.push(pic.fit()); + titles.push("Saddle Vertex Voronoi"); +} + +// Top view of the slice, in units of tan(tilt) +{ + picture pic; + unitsize(pic, 190px); + real radius = Tan(40); + + // Looking down on the slice: x to the right, z down the page + pair View(pair p) + { + return (p.x, -p.y); + } + + path hull = View(hullPoints[0]); + for (int i = 1; i < spokeCount; ++i) + hull = hull -- View(hullPoints[i]); + hull = hull -- cycle; + fill(pic, hull, gray(0.92)); + + // Excluded side of each line, a band inside the disk + real band = 0.035; + for (int i = 0; i < spokeCount; ++i) + { + pair u = SpokeDirection(i); + pair[] ends = SpokeLine(i, radius - band); + path strip = View(ends[0]) -- View(ends[1]) -- View(ends[1] + band * u) -- View(ends[0] + band * u) -- cycle; + fill(pic, strip, SpokePen(i) + opacity(0.25)); + } + + for (int i = 0; i < spokeCount; ++i) + { + pair[] ends = SpokeLine(i, radius); + draw(pic, View(ends[0]) -- View(ends[1]), SpokePen(i) + 1.0px); + pair u = SpokeDirection(i); + label(pic, "$e_" + string(i) + "$", View((SpokeOffset(i) + (IsRidge(i) ? 0.11 : 0.075)) * u), SpokePen(i)); + } + + draw(pic, hull, hullPen); + + path area = View(region[0]); + for (int k = 1; k < region.length; ++k) + area = area -- View(region[k]); + area = area -- cycle; + filldraw(pic, area, regionFill, regionPen); + + // Tilt from up, drawn over the fills so they read through the region + for (real tilt : new real[] {10, 20, 30, 40}) + { + draw(pic, scale(Tan(tilt)) * unitcircle, tilt == 40 ? gray(0.6) + 0.5px : guidePen); + label(pic, string(tilt) + "$^\circ$", Tan(tilt) * dir(90), N, gray(0.45) + fontsize(7pt)); + } + + for (pair n : faceNormals) + dot(pic, View(n), ink + 3.5px); + label(pic, "face normals", View(faceNormals[1]), NE, ink + fontsize(8pt)); + + dot(pic, (0, 0), ink + 2.5px); + label(pic, "up", (0, 0), SW, ink + fontsize(8pt)); + + frame f = pic.fit(); + panels.push(f); + titles.push("Top View"); +} + +real gap = 36px; +real x = 0; +real bottom = 0; +for (int i = 0; i < panels.length; ++i) +{ + frame f = panels[i]; + pair lo = min(f); + pair hi = max(f); + add(shift(x - lo.x, -lo.y) * f); + label(titles[i], (x + 0.5 * (hi.x - lo.x), -8px), S, ink); + x += hi.x - lo.x + gap; +} + +// Legend +pair legend = (0, -40px); +draw((legend.x, legend.y) -- (legend.x + 18px, legend.y), ridgeColor + 1.4px); +label("ridge spoke, 8$^\circ$ drop", (legend.x + 22px, legend.y), E, ink + fontsize(8pt)); +draw((legend.x + 150px, legend.y) -- (legend.x + 168px, legend.y), gullyColor + 1.4px); +label("gully spoke, 32$^\circ$ drop", (legend.x + 172px, legend.y), E, ink + fontsize(8pt)); +filldraw(shift(legend.x + 300px, legend.y - 5px) * scale(10px) * unitsquare, regionFill, regionPen); +label("vertex region", (legend.x + 314px, legend.y), E, ink + fontsize(8pt)); +draw((legend.x + 410px, legend.y) -- (legend.x + 428px, legend.y), hullPen); +label("cone of face normals", (legend.x + 432px, legend.y), E, ink + fontsize(8pt)); diff --git a/docs/images/saddle_region.svg b/docs/images/saddle_region.svg new file mode 100644 index 0000000..d2c1307 --- /dev/null +++ b/docs/images/saddle_region.svg @@ -0,0 +1,442 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/images/vertex_regions.asy b/docs/images/vertex_regions.asy index 484328d..07c8975 100644 --- a/docs/images/vertex_regions.asy +++ b/docs/images/vertex_regions.asy @@ -8,7 +8,9 @@ real px = 0.75bp; pen ink = rgb("231f20"); pen meshPen = ink + 0.6px; -pen regionFill = rgb("f2d4d0"); +// Half opacity over white matches the opaque f2d4d0 of the other figures +pen regionFill = rgb("e5a9a1") + opacity(0.5); +pen regionShade = rgb("7a1f18"); pen regionPen = rgb("a8322a") + 0.9px; pen rayPen = rgb("a8322a") + 0.6px; pen spokePen = rgb("1f5c99") + 1.0px; @@ -74,81 +76,108 @@ triple Arc(triple a, triple b, real t) return unit((1 - t) * a + t * b); } -// Extreme rays are face or cliff normals, drawn as arrows leaving the rim +// Extreme rays of the region, drawn as arrows leaving the rim void DrawRayTips(picture pic, triple v, triple[] rays, real radius) { for (triple r : rays) draw(pic, (v + radius * r) -- (v + (radius + 0.3) * r), regionPen, Arrow3(arrowSize)); } -// The region is a convex cone, so drawing only the faces that face the camera needs no sorting +// Convex hull by monotone chain +pair[] ConvexHull(pair[] points) +{ + pair[] p = sort(points, new bool(pair a, pair b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }); + pair[] hull; + for (int pass = 0; pass < 2; ++pass) + { + int base = hull.length; + for (pair q : p) + { + while (hull.length >= base + 2 && cross(hull[hull.length - 1] - hull[hull.length - 2], q - hull[hull.length - 2]) <= 0) + hull.pop(); + hull.push(q); + } + hull.pop(); + p = reverse(p); + } + return hull; +} + +// The region is convex, so its outline is the hull of its projected points. Filling that once +// avoids the seams that overlapping translucent patches would show. +void FillProjected(picture pic, triple v, triple[] points) +{ + triple view = unit(currentprojection.camera - currentprojection.target); + triple u1 = unit(cross(currentprojection.up, view)); + triple u2 = cross(view, u1); + + pair[] flat; + for (triple q : points) + flat.push((dot(q - v, u1), dot(q - v, u2))); + + pair[] hull = ConvexHull(flat); + path3 outline = v + hull[0].x * u1 + hull[0].y * u2; + for (int k = 1; k < hull.length; ++k) + outline = outline -- (v + hull[k].x * u1 + hull[k].y * u2); + draw(pic, surface(outline -- cycle, planar = true), regionFill, nolight); +} + void DrawRegion(picture pic, triple v, triple[] rays, real radius) { int arcSteps = 12; int n = rays.length; - triple view = unit(currentprojection.camera - currentprojection.target); - - if (n == 2) - { - path3 arc = v + radius * rays[0]; - for (int k = 1; k <= arcSteps; ++k) - arc = arc -- (v + radius * Arc(rays[0], rays[1], k / arcSteps)); - draw(pic, surface(v -- arc -- cycle), regionFill, nolight); - draw(pic, arc, regionPen); - for (triple r : rays) - draw(pic, v -- (v + radius * r), regionPen); - DrawRayTips(pic, v, rays, radius); - return; - } triple axis = (0, 0, 0); for (triple r : rays) axis += r; axis = unit(axis); - int rings = 6; - surface cap; + triple[] points = { v }; path3 rim; for (int i = 0; i < n; ++i) { + if (n == 2 && i == 1) + break; + triple a = rays[i]; triple b = rays[(i + 1) % n]; - - path3 arc = v + radius * a; - for (int k = 1; k <= arcSteps; ++k) - arc = arc -- (v + radius * Arc(a, b, k / arcSteps)); - - triple side = cross(a, b); - if (dot(side, axis) > 0) - side = -side; - if (dot(side, view) > 0) + for (int k = 0; k <= arcSteps; ++k) { - draw(pic, surface(v -- arc -- cycle), regionFill, nolight); - draw(pic, v -- arc -- cycle, rayPen); - } + triple p = v + radius * Arc(a, b, k / arcSteps); + rim = i == 0 && k == 0 ? p : rim -- p; + points.push(p); - for (int k = 0; k < arcSteps; ++k) - { - triple p0 = Arc(a, b, k / arcSteps); - triple p1 = Arc(a, b, (k + 1) / arcSteps); - rim = i == 0 && k == 0 ? v + radius * p0 : rim -- (v + radius * p0); - for (int j = 0; j < rings; ++j) - { - real u0 = j / rings; - real u1 = (j + 1) / rings; - triple q00 = Arc(axis, p0, u0); - triple q10 = Arc(axis, p0, u1); - triple q11 = Arc(axis, p1, u1); - triple q01 = Arc(axis, p1, u0); - if (dot(q00 + q11, view) > 0) - cap.append(surface(v + radius * q00 -- v + radius * q10 -- v + radius * q11 -- v + radius * q01 -- cycle)); - } + // Interior points for the silhouette of the spherical cap + for (int j = 1; j < 6 && n > 2; ++j) + points.push(v + radius * Arc(axis, Arc(a, b, k / arcSteps), j / 6)); } } - // The mesh pen covers the hairline seams between patches - draw(pic, cap, surfacepen = regionFill, meshpen = regionFill + 0.5px, nolight); - draw(pic, rim -- cycle, regionPen); + FillProjected(pic, v, points); + + // Darken the flat sides with the same light as the mesh. Sides on the near half of the cone never + // overlap each other in projection, nor do sides on the far half, so each adds one layer. + for (int i = 0; i < n; ++i) + { + if (n == 2 && i == 1) + break; + + triple a = rays[i]; + triple b = rays[(i + 1) % n]; + triple side = unit(cross(a, b)); + + path3 sector = v + radius * a; + for (int k = 1; k <= arcSteps; ++k) + sector = sector -- (v + radius * Arc(a, b, k / arcSteps)); + sector = v -- sector -- cycle; + + real shade = 1 - abs(dot(side, lightDirection)); + draw(pic, surface(sector, planar = true), regionShade + opacity(0.35 * shade), nolight); + } + + draw(pic, n == 2 ? rim : rim -- cycle, regionPen); + for (triple r : rays) + draw(pic, v -- (v + radius * r), rayPen); DrawRayTips(pic, v, rays, radius); } diff --git a/docs/images/vertex_regions.svg b/docs/images/vertex_regions.svg index 2a6994d..e192437 100644 --- a/docs/images/vertex_regions.svg +++ b/docs/images/vertex_regions.svg @@ -67,1305 +67,201 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + @@ -1500,34 +396,61 @@ - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -1648,1377 +571,150 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + @@ -3111,1305 +807,201 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + diff --git a/src/mesh.c b/src/mesh.c index 393d0ca..9628ac0 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -1545,7 +1545,7 @@ bool b3IsInMeshVertexRing( const b3Mesh* mesh, int triangleIndex, int vertexInde int byteCount = starts[vertex + 1] - starts[vertex]; if ( byteCount == 0 ) { - // todo comment + // Flat vertex const b3Vec3* vertices = b3GetMeshVertices( data ); b3Vec3 faceNormal = b3MakeNormalFromPoints( vertices[triangle.index1], vertices[triangle.index2], vertices[triangle.index3] ); @@ -1852,8 +1852,7 @@ static void b3IdentifyEdges( b3MeshData* mesh ) B3_FREE( edges, b3MeshEdge, edgeCount ); } -// Build the ring of neighbor normal data at a vertex. -// If ring is NULL then only compute the count. +// Build the ring of neighbor normal data at a vertex. If ring is null then only compute the byte count. static int b3BuildVertexRing( const b3MeshData* mesh, const int* fan, int fanCount, int vertex, int* stamps, int stamp, b3VertexRing* ring ) { @@ -1912,6 +1911,9 @@ static int b3BuildVertexRing( const b3MeshData* mesh, const int* fan, int fanCou stamps[other] = stamp; if ( entries != NULL ) { + // This encodes every edge. The intersection of all planes perpendicular to the edge axis builds + // the Voronoi region. That process can identify edges that don't contribute to the Voronoi region. + // That culling could save storage but is skipped for simplicity. b3Vec3 edgeAxis = b3Normalize( b3Sub( vertices[other], vertices[vertex] ) ); entries[entryCount] = b3EncodeRingEntry( edgeAxis, tangent1, tangent2, centralAxis ); } @@ -1940,7 +1942,9 @@ static int b3BuildVertexRing( const b3MeshData* mesh, const int* fan, int fanCou { b3Vec3 normal = b3MakeNormalFromPoints( vertices[triangle.index1], vertices[triangle.index2], vertices[triangle.index3] ); - entries[entryCount] = b3EncodeRingEntry( b3Neg( normal ), tangent1, tangent2, centralAxis ); + // The cliff is a 90 degree drop. + b3Vec3 cliffEdgeAxis = b3Neg( normal ); + entries[entryCount] = b3EncodeRingEntry( cliffEdgeAxis, tangent1, tangent2, centralAxis ); } entryCount += 1; @@ -2026,6 +2030,7 @@ static b3MeshData* b3AppendVertexRings( b3MeshData* mesh ) ringStarts[i] = (int)offset; if ( vertexFlags[i] & b3_flatVertex ) { + // Flat vertices don't store ring data. continue; } diff --git a/src/mesh_contact.c b/src/mesh_contact.c index 3f00ffa..e47afbb 100644 --- a/src/mesh_contact.c +++ b/src/mesh_contact.c @@ -184,7 +184,8 @@ static int b3FindFarthestCullPoint( const b3Point2D* points, int count, int inde } float score = b3DistanceSquared2( origin, points[i].p ); - if ( b3IsBetterCullCandidate( score, points[i].separation, bestScore, bestSeparation, scoreTol, separationTol ) ) + if ( b3IsBetterCullCandidate( score, points[i].separation, bestScore, bestSeparation, scoreTol, + separationTol ) ) { bestIndex = i; bestScore = score; @@ -223,7 +224,8 @@ static int b3CullPoints( b3Point2D* points, int count, bool sweep ) for ( int i = 1; i < count; ++i ) { float score = b3Dot2( axis, points[i].p ); - if ( b3IsBetterCullCandidate( score, points[i].separation, startScore, startSeparation, tol, separationTol ) ) + if ( b3IsBetterCullCandidate( score, points[i].separation, startScore, startSeparation, tol, + separationTol ) ) { startIndex = i; startScore = score; @@ -414,14 +416,8 @@ static int b3DropInteriorPoints( b3Point2D* points, int count, float margin ) { int extremes[8] = { 0 }; float bests[8] = { - points[0].p.x, - points[0].p.x + points[0].p.y, - points[0].p.y, - points[0].p.y - points[0].p.x, - -points[0].p.x, - -points[0].p.x - points[0].p.y, - -points[0].p.y, - points[0].p.x - points[0].p.y, + points[0].p.x, points[0].p.x + points[0].p.y, points[0].p.y, points[0].p.y - points[0].p.x, + -points[0].p.x, -points[0].p.x - points[0].p.y, -points[0].p.y, points[0].p.x - points[0].p.y, }; for ( int i = 1; i < count; ++i ) @@ -567,22 +563,25 @@ typedef enum b3NeighborResult // 1.0027 degrees #define B3_NEIGHBOR_TOLERANCE 0.0175f -static bool b3GetNeighborDirection( const b3Shape* shape, int triangleIndex, const b3Vec3* vertices, - b3Matrix3 meshToLocal, int edgeIndex, b3Vec3* direction ) +// Cross product of triangle edge with neighbor triangle normal. A vector pointing away from the edge +// in the neighbor triangle plane towards its center. +// tangent = cross(e, n) +static bool b3GetNeighborTangent( const b3Shape* shape, int triangleIndex, const b3Vec3* vertices, + b3Matrix3 meshToLocal, int edgeIndex, b3Vec3* tangent ) { b3Vec3 neighborNormal; bool haveNeighbor = b3GetNeighborNormal( shape, triangleIndex, edgeIndex, &neighborNormal ); if ( haveNeighbor == false ) { - // todo comment - *direction = b3Neg( b3MakeNormalFromPoints( vertices[0], vertices[1], vertices[2] ) ); + // Cliff edge axis along negative triangle normal. + *tangent = b3Neg( b3MakeNormalFromPoints( vertices[0], vertices[1], vertices[2] ) ); return false; } neighborNormal = b3MulMV( meshToLocal, neighborNormal ); b3Vec3 edge = b3Normalize( b3Sub( vertices[( edgeIndex + 1 ) % 3], vertices[edgeIndex] ) ); - *direction = b3Cross( edge, neighborNormal ); + *tangent = b3Cross( edge, neighborNormal ); return true; } @@ -679,7 +678,7 @@ static bool b3AdmitsVertexNormal( const b3Shape* shape, int triangleIndex, const // Voronoi test of an edge or vertex normal against the neighbor normals. static b3NeighborResult b3TestNeighbors( const b3Shape* shape, int triangleIndex, const b3Vec3* vertices, - b3Matrix3 meshToLocal, b3TriangleFeature feature, b3Vec3 normal, + b3Matrix3 meshToLocal, b3TriangleFeature feature, b3Vec3 candidateNormal, int* ownerEdge ) { *ownerEdge = B3_NULL_INDEX; @@ -734,41 +733,46 @@ static b3NeighborResult b3TestNeighbors( const b3Shape* shape, int triangleIndex for ( int i = 0; i < edgeCount; ++i ) { - b3Vec3 direction; - bool haveNeighbor = b3GetNeighborDirection( shape, triangleIndex, vertices, meshToLocal, edges[i], &direction ); + // neighborTangent = cross(edgeAxis, neighborTriangleNormal) + b3Vec3 neighborTangent; + bool haveNeighbor = + b3GetNeighborTangent( shape, triangleIndex, vertices, meshToLocal, edges[i], &neighborTangent ); - bool admitted = b3Dot( normal, direction ) <= tolerance; + // Does the candidate normal lean into the neighbor's face region? + bool admitted = b3Dot( candidateNormal, neighborTangent ) <= tolerance; if ( admitted == false && haveNeighbor && *ownerEdge == B3_NULL_INDEX ) { *ownerEdge = edges[i]; } admit = admit && admitted; - convex = convex || b3Dot( triangleNormal, direction ) < -tolerance; + + // Convex if at least one edge is convex. + convex = convex || b3Dot( triangleNormal, neighborTangent ) < -tolerance; } if ( admit ) { if ( vertexIndex == B3_NULL_INDEX || - b3IsInVertexCone( shape, triangleIndex, vertexIndex, meshToLocal, normal ) ) + b3IsInVertexCone( shape, triangleIndex, vertexIndex, meshToLocal, candidateNormal ) ) { return b3_neighborAdmit; } - // todo comment - return b3IsInVertexRegion( vertices, vertexIndex, normal ) ? b3_neighborSkip : b3_neighborSnap; + bool inVertexRegion = b3IsInVertexRegion( vertices, vertexIndex, candidateNormal ); + return inVertexRegion ? b3_neighborSkip : b3_neighborSnap; } // Check vertex area first to avoid a saddle tunnel. - if ( vertexIndex != B3_NULL_INDEX && b3IsInVertexRegion( vertices, vertexIndex, normal ) && - b3IsInVertexCone( shape, triangleIndex, vertexIndex, meshToLocal, normal ) ) + if ( vertexIndex != B3_NULL_INDEX && b3IsInVertexRegion( vertices, vertexIndex, candidateNormal ) && + b3IsInVertexCone( shape, triangleIndex, vertexIndex, meshToLocal, candidateNormal ) ) { return b3_neighborAdmit; } if ( convex ) { - // todo comment + // Edge is convex. return *ownerEdge == B3_NULL_INDEX ? b3_neighborDrop : b3_neighborSkip; } @@ -806,8 +810,7 @@ static b3NeighborResult b3TestHullFaceNeighbors( const b3Shape* shape, int trian float linearSlop = B3_LINEAR_SLOP; - // If the hull overlaps this triangle then determine if the deepest point - // is close to a vertex. + // If the hull overlaps this triangle then determine if the deepest point is close to a vertex. if ( deepestPoint->separation < 0.0f ) { for ( int i = 0; i < 3; ++i ) @@ -836,15 +839,15 @@ static b3NeighborResult b3TestHullFaceNeighbors( const b3Shape* shape, int trian { b3Vec3 vertex = vertices[i]; b3Vec3 edge = b3Normalize( b3Sub( vertices[i < 2 ? i + 1 : 0], vertex ) ); - b3Vec3 inward = b3Cross( triangleNormal, edge ); - if ( b3Dot( inward, b3Sub( point, vertex ) ) <= linearSlop ) + b3Vec3 inwardTangent = b3Cross( triangleNormal, edge ); + if ( b3Dot( inwardTangent, b3Sub( point, vertex ) ) <= linearSlop ) { nearEdges[nearCount] = i; nearCount += 1; // An edge or vertex normal cone tilts outward from the triangle and an edge cone is perpendicular to the // edge, a hull face normal need be neither. - inwardTilt = inwardTilt || b3Dot( normal, inward ) > tolerance; + inwardTilt = inwardTilt || b3Dot( normal, inwardTangent ) > tolerance; alongEdge = alongEdge || b3AbsFloat( b3Dot( normal, edge ) ) > tolerance; } } @@ -1156,6 +1159,8 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape manifold->normal, &ownerEdge ); } + // todo why consider snap if the the next section doesn't? + // todo why compute min separation if we just need a deep result? bool deep = false; if ( result == b3_neighborSkip || result == b3_neighborSnap ) { @@ -1170,27 +1175,30 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape if ( result == b3_neighborSkip && deep ) { - // In deep overlap the owning neighbor may apply backside culling. Experiments - // show the that in this case the neighbor should snap the normal. - if ( shapeB->type != b3_sphereShape ) + // Handle deep overlap. + if ( shapeB->type == b3_sphereShape ) { - result = b3_neighborSnap; - } - else - { - // todo comment + // No way to snap a deep sphere normal. b3Vec3 neighborNormal; if ( ownerEdge != B3_NULL_INDEX && b3GetNeighborNormal( shapeA, triangleIndex, ownerEdge, &neighborNormal ) ) { neighborNormal = b3MulMV( relativeMatrix, neighborNormal ); - float offset = b3Dot( neighborNormal, b3Sub( shapeB->sphere.center, vertices[ownerEdge] ) ); - if ( offset < 0.0f ) + float neighborSeparation = + b3Dot( neighborNormal, b3Sub( shapeB->sphere.center, vertices[ownerEdge] ) ); + if ( neighborSeparation < 0.0f ) { + // Neighbor would cull, so better keep this one. result = b3_neighborAdmit; } } } + else + { + // In deep overlap the owning neighbor may apply backside culling. Experiments show that in this + // case the neighbor should snap the normal. + result = b3_neighborSnap; + } } if ( result == b3_neighborSnap ) @@ -1252,9 +1260,8 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape if ( hullB != NULL && validateNeighbors ) { - // Cull triangles fully below the hull face when it rests on a triangle and has - // a tilted manifold normal. This helps when a box slides across a chamfered - // channel. + // Cull triangles fully below the hull face when it rests on a triangle and has a tilted manifold normal. This + // helps when a box slides across a chamfered channel. float tolerance = B3_NEIGHBOR_TOLERANCE; float cosSquaredTolerance = 1.0f - tolerance * tolerance; const b3LocalManifold* resting = NULL; @@ -1669,7 +1676,8 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact // todo comment static int b3CollideMeshAndShape( b3Manifold* manifolds, int capacity, const b3Mesh* mesh, const b3Shape* shapeB, - b3AABB localBounds, b3Transform transformBtoA, bool enableSpeculative, bool handleGhosts ) + b3AABB localBounds, b3Transform transformBtoA, bool enableSpeculative, + bool handleGhosts ) { if ( capacity <= 0 ) { @@ -1710,8 +1718,8 @@ static int b3CollideMeshAndShape( b3Manifold* manifolds, int capacity, const b3M b3Transform transformAtoB = b3InvertTransform( transformBtoA ); b3Cluster* clusters; - int clusterCount = b3CollideMeshTriangles( &clusters, &meshShape, shapeB, transformAtoB, triangleCaches, triangleCount, - enableSpeculative, handleGhosts, false, NULL, &arena ); + int clusterCount = b3CollideMeshTriangles( &clusters, &meshShape, shapeB, transformAtoB, triangleCaches, + triangleCount, enableSpeculative, handleGhosts, false, NULL, &arena ); int count = b3MinInt( clusterCount, capacity ); b3Matrix3 matrixB = b3MakeMatrixFromQuat( transformBtoA.q );