mirror of
https://github.com/JustinSDK/dotSCAD.git
synced 2025-08-11 17:24:20 +02:00
add tri_delaunay2voronoi
This commit is contained in:
23
src/experimental/_impl/_tri_delaunay2voronoi_impl.scad
Normal file
23
src/experimental/_impl/_tri_delaunay2voronoi_impl.scad
Normal file
@@ -0,0 +1,23 @@
|
||||
use <experimental/tri_delaunay.scad>;
|
||||
use <util/map/hashmap_get.scad>;
|
||||
use <util/find_index.scad>;
|
||||
|
||||
function delaunay_coords(d) = d[0];
|
||||
function delaunay_triangles(d) = d[1];
|
||||
function delaunay_circles(d) = d[2];
|
||||
|
||||
function indicesOfCell(iTris, triIndices) =
|
||||
let(
|
||||
vi = iTris[0][0],
|
||||
indices = [],
|
||||
leng = len(iTris)
|
||||
)
|
||||
_indicesOfCell(iTris, triIndices, leng, indices, vi);
|
||||
|
||||
function _indicesOfCell(iTris, triIndices, leng, indices, vi, i = 0) =
|
||||
i == leng ? indices :
|
||||
let(
|
||||
t = iTris[find_index(iTris, function(t) t[0] == vi)],
|
||||
nIndices = concat(indices, hashmap_get(triIndices, t))
|
||||
)
|
||||
_indicesOfCell(iTris, triIndices, leng, nIndices, t[1], i + 1);
|
22
src/experimental/demo/tri_delaunay2voronoi_demo.scad
Normal file
22
src/experimental/demo/tri_delaunay2voronoi_demo.scad
Normal file
@@ -0,0 +1,22 @@
|
||||
use <experimental/tri_delaunay.scad>;
|
||||
use <experimental/tri_delaunay2voronoi.scad>;
|
||||
use <hull_polyline2d.scad>;
|
||||
|
||||
points = [for(i = [0:20]) rands(-100, 100, 2)];
|
||||
|
||||
color("black")
|
||||
for(p = points) {
|
||||
translate(p)
|
||||
circle(2);
|
||||
}
|
||||
|
||||
%draw(tri_delaunay(points));
|
||||
|
||||
d = tri_delaunay(points, ret = "DELAUNAY");
|
||||
#draw(tri_delaunay2voronoi(d));
|
||||
|
||||
module draw(pointsOfTriangles) {
|
||||
for(t = pointsOfTriangles) {
|
||||
hull_polyline2d(concat(t, [t[0]]));
|
||||
}
|
||||
}
|
47
src/experimental/tri_delaunay2voronoi.scad
Normal file
47
src/experimental/tri_delaunay2voronoi.scad
Normal file
@@ -0,0 +1,47 @@
|
||||
use <experimental/_impl/_tri_delaunay2voronoi_impl.scad>;
|
||||
use <util/map/hashmap.scad>;
|
||||
use <util/map/hashmap_keys.scad>;
|
||||
use <util/map/hashmap_get.scad>;
|
||||
|
||||
function tri_delaunay2voronoi(d) =
|
||||
let(
|
||||
coords = delaunay_coords(d),
|
||||
coords_leng = len(coords),
|
||||
circles = delaunay_circles(d),
|
||||
tris = hashmap_keys(delaunay_triangles(d)),
|
||||
// circumcircle centers
|
||||
vertices = [for(t = tris) hashmap_get(circles, t)[0]],
|
||||
i_rts = [
|
||||
for(i = [0:len(tris) - 1])
|
||||
let(
|
||||
a = tris[i][0],
|
||||
b = tris[i][1],
|
||||
c = tris[i][2],
|
||||
rt1 = [b, c, a],
|
||||
rt2 = [c, a, b],
|
||||
rt3 = [a, b, c]
|
||||
)
|
||||
each [[a, rt1], [b, rt2], [c, rt3]]
|
||||
],
|
||||
connectedTris = [
|
||||
for(i = [0:coords_leng - 1])
|
||||
[for(i_rt = i_rts) if(i_rt[0] == i) i_rt[1]]
|
||||
],
|
||||
triIndices = hashmap([
|
||||
for(i = [0:len(tris) - 1])
|
||||
let(
|
||||
a = tris[i][0],
|
||||
b = tris[i][1],
|
||||
c = tris[i][2],
|
||||
rt1 = [b, c, a],
|
||||
rt2 = [c, a, b],
|
||||
rt3 = [a, b, c]
|
||||
)
|
||||
each [[rt1, i], [rt2, i], [rt3, i]]
|
||||
]),
|
||||
cells = [
|
||||
for(i = [4:coords_leng - 1])
|
||||
indicesOfCell(connectedTris[i], triIndices)
|
||||
]
|
||||
)
|
||||
[for(cell = cells) [for(i = cell) vertices[i]]];
|
Reference in New Issue
Block a user