Fences join like walls, fence designs, draw runs corner by corner
Deploy web Studio / deploy (push) Successful in 3m52s

Joints: line_joint (util.rhai) applies the house-wall rules to fences and
site walls, so pieces never overlap and the depth sort stays clean.
- A corner is closed: the thicker piece (or, if equal, the one along
  world x) runs through, and the other stops at its face.
- On a straight run, two pieces share one post.
- At a T, the stem stops at the other piece's face without a post of its
  own.
- A fence that ends at a site or house wall stops at the wall's face.

Fence designs (prop style): mesh, wooden boards, steel bars, mesh on a
stone base, cobble wall, and brick wall with pillars. Board, stone and
brick joints are drawn on the face turned to the viewer. Brick pillars
are separate sprites.

Studio: line prefabs (walls, fences, hedges, roads) can be drawn as a
run by clicking corner after corner. Double-click or Enter ends the run,
clicking the first corner closes it, and Backspace drops the last
corner. A run is one undo step. A drag still draws one piece. Corners
and ends snap onto the ends and lines of the pieces they meet, and a
marker shows where the next corner will snap.

The house demo gets a brick front wall with gate pillars and wooden side
fences in place of the old east fence, which ran over the road. Tests
cover every design's corners, Ts, straight runs and wall stops, with no
depth-sort cycles in any rotation, plus site walls. check_studio draws
and closes a fence run, undoes it in one step, snaps a dragged spur,
and picks a design.
This commit is contained in:
grabowski committed 2026-10-24 18:07:40 +07:00
1 parent 7962249191
commit e4017ce2a8
14 files changed
+559 -27

No files matched your search

+116
View File
@@ -399,6 +399,122 @@ fn walls_meeting_at_a_t_or_in_line_do_not_overlap() {
assert!((n1[0] + 2.075).abs() < 1e-6 && (n2[3] - 2.075).abs() < 1e-6);
}
/// A 10 x 6 m fence perimeter, a fence across it (a T at both ends; at the
/// north it meets the joint of the north fence's two pieces) and a spur from
/// the east fence to a site wall.
fn fence_doc(style: &str) -> Doc {
let mut doc = Doc::default();
for (id, x, y, rot, w) in [
("south", 0.0, -3.0, 0.0, 10.0),
("north", -2.5, 3.0, 0.0, 5.0),
("north2", 2.5, 3.0, 0.0, 5.0),
("west", -5.0, 0.0, 90.0, 6.0),
("east", 5.0, 0.0, 90.0, 6.0),
("inner", 0.0, 0.0, 90.0, 6.0),
("spur", 6.5, 0.0, 0.0, 3.0),
] {
let mut o = obj(id, "fence", x, y, json!({ "w": w, "style": style }));
o.rot = rot;
doc.objects.push(o);
}
let mut wall = obj("wall", "wall", 8.0, 0.0, json!({"w": 10.0}));
wall.rot = 90.0;
doc.objects.push(wall);
doc
}
#[test]
fn fences_join_like_walls_in_every_design() {
let r = Runtime::new(&Bundle::default()).unwrap();
for (style, ht) in [
("mesh", 0.04),
("wood", 0.05),
("bars", 0.04),
("half stone", 0.16),
("cobble", 0.22),
("brick", 0.18),
] {
let doc = fence_doc(style);
let b = r.build("site", 0, &cfg(), &doc).unwrap();
assert!(
b.scene.warnings.is_empty(),
"{style}: {:?}",
b.scene.warnings
);
let bx = |id: &str| b.boxes[id];
let near = |a: f64, e: f64| (a - e).abs() < 1e-6;
// corners: the fence along x runs through, the other stops at its face
let (s, w) = (bx("south"), bx("west"));
assert!(
near(s[0], -5.0 - ht) && near(s[3], 5.0 + ht),
"{style} south {s:?}"
);
assert!(
near(w[1], -3.0 + ht) && near(w[4], 3.0 - ht),
"{style} west {w:?}"
);
// a T at the south, the end of a straight run at the north: it stops at the faces
let i = bx("inner");
assert!(
near(i[1], -3.0 + ht) && near(i[4], 3.0 - ht),
"{style} inner {i:?}"
);
// in line: the lower piece runs through the shared post, the other starts at it
let (n1, n2) = (bx("north"), bx("north2"));
assert!(
near(n1[3], ht) && near(n2[0], ht),
"{style} north {n1:?} {n2:?}"
);
// the spur stops at the east fence and at the site wall's face
let sp = bx("spur");
assert!(
near(sp[0], 5.0 + ht) && near(sp[3], 8.0 - 0.12),
"{style} spur {sp:?}"
);
for rot in 0..4u8 {
let b = r.build("site", rot, &cfg(), &doc).unwrap();
let out = render(&b.scene, rot, &RenderOpts::default());
assert_eq!(out.cycles, 0, "{style}/{rot}: depth-sort cycles");
}
}
// the brick wall has pillars as their own sprites, with brick courses on its faces
let b = r.build("site", 0, &cfg(), &fence_doc("brick")).unwrap();
assert!(sprites_of(&b, "south") > 4);
assert!(render(&b.scene, 0, &RenderOpts::default())
.html
.contains("iso-seam"));
}
#[test]
fn site_walls_join_at_corners() {
let r = Runtime::new(&Bundle::default()).unwrap();
let mut doc = Doc::default();
for (id, x, y, rot, w) in [
("s", 0.0, -3.0, 0.0, 10.0),
("n", 0.0, 3.0, 0.0, 10.0),
("w", -5.0, 0.0, 90.0, 6.0),
("e", 5.0, 0.0, 90.0, 6.0),
] {
let mut o = obj(id, "wall", x, y, json!({ "w": w }));
o.rot = rot;
doc.objects.push(o);
}
let b = r.build("site", 0, &cfg(), &doc).unwrap();
let (s, w) = (b.boxes["s"], b.boxes["w"]);
assert!(
(s[0] + 5.12).abs() < 1e-6 && (s[3] - 5.12).abs() < 1e-6,
"{s:?}"
);
assert!(
(w[1] + 2.88).abs() < 1e-6 && (w[4] - 2.88).abs() < 1e-6,
"{w:?}"
);
for rot in 0..4u8 {
let b = r.build("site", rot, &cfg(), &doc).unwrap();
assert_eq!(render(&b.scene, rot, &RenderOpts::default()).cycles, 0);
}
}
#[test]
fn cut_levels_lower_the_walls_in_front_of_rooms() {
let r = Runtime::new(&Bundle::default()).unwrap();
+157 -14
View File
@@ -91,6 +91,7 @@ fn prefab_parking(o) {
fn meta_wall() {
#{ label: "Wall", category: "Site", icon: "▬", shape: "line",
help: "Drag along the map, or click corner after corner for a run around a plot. Walls that meet join at corners and T-junctions.",
props: [
#{ key: "w", type: "float", def: 20.0, min: 0.5, max: 500.0, step: 0.5, label: "Length (m)" },
#{ key: "h", type: "float", def: 2.2, min: 0.3, max: 8.0, step: 0.1, label: "Height (m)" },
@@ -98,37 +99,179 @@ fn meta_wall() {
] }
}
fn prefab_wall(o) {
// long walls in pieces of at most 30 m, each its own sprite for the depth sort
// ends joined to the walls they meet; long walls in pieces of at most 30 m,
// each its own sprite for the depth sort
let p = o.p;
let n = (p.w / 30.0).ceiling().to_int();
let L = p.w / n;
let x0 = -p.w / 2.0 - line_joint(o, -1.0, ["wall"], []).ext;
let x1 = p.w / 2.0 + line_joint(o, 1.0, ["wall"], []).ext;
let n = ((x1 - x0) / 30.0).ceiling().to_int();
let L = (x1 - x0) / n;
for i in 0..n {
let x0 = -p.w / 2.0 + i * L;
sprite().box(x0, -p.t / 2.0, x0 + L, p.t / 2.0, 0.0, p.h, "wallp", #{ top: "wallt" });
let a = x0 + i * L;
sprite().box(a, -p.t / 2.0, a + L, p.t / 2.0, 0.0, p.h, "wallp", #{ top: "wallt" });
}
}
fn fence_styles() { ["mesh", "wood", "bars", "half stone", "cobble", "brick"] }
/// Half the thickness of a fence design: its posts, base or pillars.
fn fence_ht(style) {
switch style { "wood" => 0.05, "half stone" => 0.16, "cobble" => 0.22, "brick" => 0.18, _ => 0.04 }
}
fn meta_fence() {
#{ label: "Fence", category: "Site", icon: "┼", shape: "line",
help: "Drag along the map, or click corner after corner for a run around a plot. Fences that meet join at corners and T-junctions and stop at walls. Designs: mesh, wooden boards, steel bars, mesh on a stone base, cobble wall, brick wall with pillars.",
props: [
#{ key: "w", type: "float", def: 20.0, min: 1.0, max: 500.0, step: 0.5, label: "Length (m)" },
#{ key: "h", type: "float", def: 1.8, min: 0.5, max: 4.0, step: 0.1, label: "Height (m)" },
#{ key: "style", type: "choice", def: "mesh", choices: fence_styles(), label: "Design" },
] }
}
fn prefab_fence(o) {
let p = o.p;
let n = (p.w / 30.0).ceiling().to_int();
let L = p.w / n;
for i in 0..n {
let x0 = -p.w / 2.0 + i * L;
let s = sprite();
s.box(x0, -0.03, x0 + L, 0.03, 0.0, p.h, "fence");
let k = (L / 2.5).floor().to_int();
let st = p.style;
let ea = line_joint(o, -1.0, ["fence"], ["wall", "house_wall"]);
let eb = line_joint(o, 1.0, ["fence"], ["wall", "house_wall"]);
let x0 = -p.w / 2.0 - ea.ext;
let x1 = p.w / 2.0 + eb.ext;
if x1 - x0 < 0.05 { return; }
// posts (pillars) evenly along the length; the end ones only where this
// fence draws them, kept inside the stretch it covers
let ph = fence_ht(st);
let gap = if st == "brick" { 3.0 } else { 2.5 };
let k = fmax(1.0, (p.w / gap).round()).to_int();
let posts = [];
if st != "cobble" {
for j in 0..(k + 1) {
let x = x0 + j * L / fmax(k, 1);
s.box(x - 0.04, -0.04, x + 0.04, 0.04, 0.0, p.h + 0.05, "steel");
if (j == 0 && !ea.post) || (j == k && !eb.post) { continue; }
posts.push(fmin(fmax(-p.w / 2.0 + j * p.w / k, x0 + ph), x1 - ph));
}
}
if st == "brick" { brick_fence(x0, x1, posts, p.h); return; }
// pieces of at most 30 m, each its own sprite for the depth sort
let n = ((x1 - x0) / 30.0).ceiling().to_int();
let L = (x1 - x0) / n;
for i in 0..n {
let a = x0 + i * L;
let b = a + L;
let ps = [];
for x in posts {
if x >= a - 1e-6 && (x < b - 1e-6 || i == n - 1) { ps.push(fmin(fmax(x, a + ph), b - ph)); }
}
fence_piece(sprite(), a, b, ps, p.h, st, i);
}
}
/// One stretch a..b of a fence (not brick) with its posts ps.
fn fence_piece(s, a, b, ps, h, st, seed) {
let L = b - a;
if st == "wood" {
s.box(a, -0.025, b, 0.025, 0.0, h, "wood");
let boards = [];
let m = fmax(1.0, (L / 0.2).round()).to_int();
for j in 0..m { boards.push([j * L / m, (j + 1) * L / m, 0.04, h - 0.02]); }
s.open([a, -0.025], [b, -0.025], boards, "seam");
s.open([b, 0.025], [a, 0.025], boards, "seam");
for x in ps { s.box(x - 0.05, -0.05, x + 0.05, 0.05, 0.0, h + 0.06, "woodd"); }
} else if st == "bars" {
let bars = [];
let m = fmax(2.0, (L / 0.16).round()).to_int();
for j in 1..m { let u = j * L / m; bars.push([u - 0.014, u + 0.014, 0.0, h - 0.05]); }
s.open([a, 0.0], [b, 0.0], bars, "steel");
s.open([b, 0.0], [a, 0.0], bars, "steel");
s.box(a, -0.025, b, 0.025, 0.1, 0.15, "steel");
s.box(a, -0.025, b, 0.025, h - 0.06, h, "steel");
for x in ps { s.box(x - 0.04, -0.04, x + 0.04, 0.04, 0.0, h + 0.05, "steel"); }
} else if st == "half stone" {
let hb = fmin(0.6, h * 0.4);
s.box(a, -0.16, b, 0.16, 0.0, hb, "cobble", #{ top: "capst" });
let j = stone_joints(L, 0.0, hb, seed);
s.open([a, -0.16], [b, -0.16], j, "seam");
s.open([b, 0.16], [a, 0.16], j, "seam");
s.box(a, -0.03, b, 0.03, hb, h, "fence");
for x in ps { s.box(x - 0.04, -0.04, x + 0.04, 0.04, hb, h + 0.05, "steel"); }
} else if st == "cobble" {
let hc = fmax(0.2, h - 0.08);
s.box(a, -0.2, b, 0.2, 0.0, hc, "cobble");
s.box(a, -0.22, b, 0.22, hc, h, "capst");
let j = stone_joints(L, 0.0, hc, seed);
s.open([a, -0.2], [b, -0.2], j, "seam");
s.open([b, 0.2], [a, 0.2], j, "seam");
} else {
// mesh
s.box(a, -0.03, b, 0.03, 0.0, h, "fence");
for x in ps { s.box(x - 0.04, -0.04, x + 0.04, 0.04, 0.0, h + 0.05, "steel"); }
}
}
/// A brick wall between pillars: pillars and wall stretches are separate
/// sprites (the pillars are thicker), with a coping on top and brick courses.
fn brick_fence(x0, x1, posts, h) {
let hw = fmax(0.3, h - 0.06);
let u = x0;
for x in posts + [x1 + 1.0] {
let b = fmin(x - 0.18, x1);
if b > u + 0.02 {
let n = ((b - u) / 30.0).ceiling().to_int();
let L = (b - u) / n;
for k in 0..n {
let s = sprite();
let a = u + k * L;
s.box(a, -0.12, a + L, 0.12, 0.0, hw, "brick");
s.box(a, -0.14, a + L, 0.14, hw, h, "plinth");
let j = brick_joints(L, 0.0, hw, a);
s.open([a, -0.12], [a + L, -0.12], j, "seam");
s.open([a + L, 0.12], [a, 0.12], j, "seam");
}
}
if x <= x1 {
let s = sprite();
s.box(x - 0.17, -0.17, x + 0.17, 0.17, 0.0, h + 0.1, "brick");
s.box(x - 0.18, -0.18, x + 0.18, 0.18, h + 0.1, h + 0.17, "plinth");
u = x + 0.18;
}
}
}
/// Joints of a stone face L long from z0 to z1: rows of stones of varying length.
fn stone_joints(L, z0, z1, seed) {
let out = [];
let rows = fmax(1.0, ((z1 - z0) / 0.22).round()).to_int();
let rh = (z1 - z0) / rows;
for r in 0..rows {
let k = seed * 31 + r * 17 + 7;
let u = 0.0;
while u < L - 0.05 {
k = (k * 1103 + 12345) % 9973;
let u1 = fmin(L, u + 0.28 + (k % 100) / 100.0 * 0.34);
if L - u1 < 0.12 { u1 = L; }
out.push([u, u1, z0 + r * rh, z0 + (r + 1) * rh]);
u = u1;
}
}
out
}
/// Brick courses on a face L long from z0 to z1, in running bond; `at`
/// shifts the bond so neighbouring stretches line up.
fn brick_joints(L, z0, z1, at) {
let out = [];
let rows = fmax(1.0, ((z1 - z0) / 0.25).round()).to_int();
let rh = (z1 - z0) / rows;
for r in 0..rows {
let off = if r % 2 == 0 { 0.0 } else { 0.25 };
let u = 0.0;
let first = 0.5 - ((at + off) % 0.5 + 0.5) % 0.5;
let u1 = if first < 0.05 { first + 0.5 } else { first };
while u < L - 0.02 {
let e = fmin(L, u1);
out.push([u, e, z0 + r * rh, z0 + (r + 1) * rh]);
u = e;
u1 = e + 0.5;
}
}
out
}
fn meta_lamp() {
-2
View File
@@ -135,8 +135,6 @@ fn prefab_house_wall(o) {
if end > x + 0.01 { wall_run(x, end, t, top, p.mat, cap); }
}
fn along_world_x(rot) { ((rot / 90.0).round().to_int() % 2) == 0 }
/// How far wall o reaches past end e (-1 or 1) of its centre line. Alone:
/// half its thickness, so a corner closes. Where other walls meet it,
/// the walls must not overlap (the depth sort needs them apart): along a
+63
View File
@@ -57,3 +57,66 @@ fn drive(dx, dy, dur, delay) {
this.begin("a drive", `--dx:${v[0]}px;--dy:${v[1]}px;animation-duration:${dur}s;animation-delay:-${delay}s`);
true
}
/// True when a piece turned by rot runs along world x (rot 0 or 180).
fn along_world_x(rot) { ((rot / 90.0).round().to_int() % 2) == 0 }
/// Half the thickness of a fence or wall piece: what the joints keep apart.
fn line_ht(w) {
if w.type == "fence" { fence_ht(w.p.style) } else { w.p.t / 2.0 }
}
/// How line piece o (a fence or wall along its local x, length p.w) meets
/// other pieces at its end e (-1 or 1): #{ ext, post }, how far it reaches
/// past the end and whether it draws the end post. `same`: the types it joins
/// with; `stops`: types it ends against. The rules of the house walls, so
/// pieces never overlap (the depth sort needs them apart):
/// - alone it reaches half its thickness past the end, flush with its post;
/// - on a straight run the lower piece runs through and draws the shared
/// post, the other starts at that post;
/// - at a corner the thicker piece (equal: the one along world x) runs
/// through, the other stops at its face;
/// - at a T it stops at the other's face without an end post (the other
/// piece has its own there); against a `stops` piece it keeps its post.
fn line_joint(o, e, same, stops) {
let ht = line_ht(o);
let p = to_world(e * o.p.w / 2.0, 0.0);
let c = to_world(0.0, 0.0);
let q = to_world(e * (o.p.w / 2.0 - 0.05), 0.0);
let ox = along_world_x(o.rot);
let face = 0.0;
let has_face = false;
let face_post = false;
let run = #{};
let corner = [];
for w in objects_on(same + stops, p[0], p[1], 0.5) {
if w.id == o.id || (w.z - o.z).abs() > 0.5 { continue; }
let wt = line_ht(w);
if w.v.abs() > wt + 0.02 { continue; }
let at_end = (w.u.abs() - w.p.w / 2.0).abs() < 0.02;
let wx = along_world_x(w.rot);
let side = if local_of(w, q[0], q[1])[1] >= w.v { 1.0 } else { -1.0 };
let stop = -(wt - side * w.v);
if !same.contains(w.type) || (!at_end && wx != ox) {
face = stop;
has_face = true;
face_post = !same.contains(w.type);
} else if wx == ox {
if at_end { run = #{ lower: if ox { c[0] < w.x } else { c[1] < w.y }, wt: wt }; }
} else {
corner.push(#{ wt: wt, stop: stop });
}
}
if has_face { return #{ ext: face, post: face_post }; }
if run.len() > 0 {
return if run.lower { #{ ext: ht, post: true } } else { #{ ext: -run.wt, post: false } };
}
// two pieces end here from both sides: a straight run this one meets as a T
if corner.len() > 1 { return #{ ext: corner[0].stop, post: false }; }
if corner.len() == 1 {
let k = corner[0];
let mine = ht > k.wt + 0.005 || ((ht - k.wt).abs() <= 0.005 && ox);
return if mine { #{ ext: ht, post: true } } else { #{ ext: k.stop, post: false } };
}
#{ ext: ht, post: true }
}