Wrapping Wire Round a Branch
The next post in the math sub-series, and the one the last post promised at its end. Same audience: you know what cos and sin do to an angle, and I’ll explain everything else as it comes up. Every number below I checked against the code or a unit test before writing it down.
The formula from last time, set moving
Last time a branch’s base was a point on a circle round the trunk, written in the trunk’s own directions:
base = centre + (cos(a) × normal + sin(a) × binormal) × r
The wire coil in the 3D viewer is the same formula with two things allowed to move. The centre slides along the branch from base to tip, and the angle a keeps increasing as it goes. A point that goes round and round while its circle travels traces a helix, the shape of a spring or a screw thread. That is the whole idea: a helix is a circle whose angle and centre both move.
The code is addWireCoil in src/viewer/treeMesh.ts, with the arithmetic in a helper called helixSample. Stripped to its core, for a fraction s of the way along the branch:
f = branchFrameAt(tree, branch, s)
θ = s × turns × 360
centre = f.pos + (cos θ × f.normal + sin θ × f.binormal) × offset
Three ingredients: a frame f at every point of the branch, a number of turns, and an offset, the distance from the branch’s centre line to the wire’s. I’ll take them in order, then come to the part the teaser called linear algebra earning its keep.
A frame on a curving branch
A frame is three unit arrows at right angles to each other: a tangent along the branch, and a normal and binormal across it. The trunk builds its frames by walking up and rotating; the branch gets away with a closed form, and here is where it comes from.
The branch-curvature post worked out that a branch with horizontal bend bend (and no vertical bend) points, at fraction t of its length, at compass angle a(t) = angle + bend × t, and climbs a quarter of a unit for every unit it travels. So its direction of travel is
(cos a(t), sin a(t), 0.25)
That is the tangent, except it is not unit length: its length is √(1 + 0.25²) = √1.0625. Divide by that and you have branchFrameAt’s tangent, with inv = 1 / √1.0625 = 0.970143 in the code.
For the normal you want any arrow at right angles to that. The cheapest is to stay horizontal and turn the compass direction a quarter turn: (-sin a, cos a, 0). Its dot product with the tangent is -sin a cos a + cos a sin a + 0 = 0, so it is square to the branch, and it is already unit length.
The binormal has to be square to both. That is exactly what a cross product makes: T × N is at right angles to T and to N. Written out,
T × N = (Ty Nz - Tz Ny, Tz Nx - Tx Nz, Tx Ny - Ty Nx)
= inv × (-0.25 cos a, -0.25 sin a, 1)
which is the binormal in src/sim/geometry.ts, line for line. It points mostly up, leaning back against the branch’s climb. When the branch also bends vertically, branchFrameAt takes the same three arrows at the base and rotates them (Rodrigues’ formula, vec3Rotate in src/sim/vec3.ts) instead, but the shape of the answer is the same: three square arrows that ride along the branch.
How far out, and how many turns
The wire has to lie on the bark, not in it. Its centre line therefore sits one branch radius plus one wire radius from the branch’s centre line. That is wireCoilOffset:
wireCoilOffset(branch, s) = branchRadiusAt(branch, s) + wireRadius(branch)
branchRadiusAt tapers the branch from its base radius to a fifth of it at the tip, but never below BRANCH_MIN_RADIUS, 0.005 units. The floor is what keeps the wire on the surface at the thin end.
The test fixture I’ll use for the rest of the post is the starter juniper with its second branch wired: 11 cm long, girth 3.3 mm, compass angle 210, wired to a horizontal bend of 40 degrees, starting 14 cm up a 25 cm trunk. The test sizes the coil on the fixture branch from girth, bend and length pins:
branch base radius = 3.3 / 1000 × 4 = 0.0132 units
wire gauge = lerp(0.15, 0.30, 40/60) = 0.25
wire radius = 0.0132 × 0.25 = 0.0033 units
offset at base = 0.0132 + 0.0033 = 0.0165 units
At the tip the unfloored taper would give 0.0132 × 0.2 = 0.00264, so the floor takes over and the offset is 0.005 + 0.0033 = 0.0083. Without the floor the wire’s inner edge would sit 0.00236 units inside the bark, over a third of its thickness buried. The test offsets the coil by the floored bark radius plus the wire radius checks both ends.
The number of turns comes from the pitch, the distance the wire advances per turn, which WIRE_PITCH_DIAMETERS sets to three wire diameters:
pitch = 3 × 0.0066 = 0.0198 units
length = 11 / 100 × 4 = 0.44 units
turns = 0.44 / 0.0198 = 22.22
bands = ceil(22.22 × 8) = 178
Eight bands per turn (WIRE_SEGS_PER_TURN), so the coil is 179 rings joined by 178 bands, and θ = s × 22.22 × 360 sweeps 8000 degrees from base to tip.
The wire’s own rings
So far that is a line, the centre line of the wire. To draw it the viewer needs a tube round that line: at each sample a small ring of six points (WIRE_RING_SEGMENTS), radius 0.0033, and then stitchRings joins ring to ring with two triangles per side, point i of one ring to point i of the next.
The ring has to lie square to the direction the wire is travelling. That is not the branch’s tangent. To find the wire’s direction, differentiate the centre formula with respect to s. The code has the branch’s position moving along T at length units per unit of s, and the angle term cos θ × N + sin θ × B turns at turns × 2π radians per unit of s, which moves the point at offset × turns × 2π in the direction -sin θ × N + cos θ × B:
velocity = T × 0.44 + (-sin θ × N + cos θ × B) × offset × turns × 2π
That is helixSample’s tangent before it normalises, and it is honest to say what it leaves out. Strictly, the branch’s position moves at 0.44 × √1.0625 = 0.4535 units per unit of s, 3 percent more than the length, because it climbs as it goes; and the formula leaves out the slow turning of the frame itself and the slow shrink of the offset, each under one percent of the 2.30 term below. None of them moves a ring visibly.
Now the worked example, at the base, s = 0, where θ = 0. The frame there, read out of branchFrameAt for the fixture:
pos = (-0.018956, -0.010944, 0.56)
tangent = (-0.840168, -0.485071, 0.242536)
normal = ( 0.5, -0.866025, 0)
binormal = ( 0.210042, 0.121268, 0.970143)
The wire’s centre is pos + 0.0165 × normal = (-0.010706, -0.025233, 0.56). The angle term’s speed is 0.0165 × 22.22 × 2π = 2.303835, so
velocity = 0.44 × tangent + 2.303835 × binormal
= (0.114228, 0.065950, 2.341764)
length = 2.345475
n = (0.048701, 0.028118, 0.998418)
The 2.30 beats the 0.44 by more than five to one. The wire is mostly going round the branch and only a little along it. The angle between n and the branch’s tangent is acos(0.44 / 2.345475) = 79.2 degrees, so a wire ring that is square to the wire is tilted 79 degrees away from a ring that is square to the branch.
buildOrientedRing(centre, radius, segments, n) builds the square ring with two cross products. First it needs any arrow that is not parallel to n. It uses world up, (0, 0, 1), unless n is itself nearly vertical (|n.z| ≥ 0.95), in which case world +x. Here n.z = 0.998, so it is +x. Then:
u = normalise(ref × n) = normalise(0, -0.998418, 0.028118) = (0, -0.999604, 0.028151)
v = n × u = (0.998813, -0.001371, -0.048682)
The first cross product gives an arrow square to n (and to the reference); the second gives one square to both n and u. u and v are the ring’s own normal and binormal, and the ring is last post’s circle once more, in that plane:
point k = centre + (cos(60k) × u + sin(60k) × v) × 0.0033
point 0 = (-0.010706, -0.028532, 0.560093)
point 1 = (-0.007851, -0.026887, 0.559907)
I recorded the vertices addWireCoil emits for the fixture, and the first two of its first ring are those, to every digit shown.
What goes wrong without it
Hand buildOrientedRing the branch’s tangent instead of the wire’s and you get a ring in the branch’s normal and binormal plane. Seen end on, looking down the wire, it keeps its full height in the normal direction, 0.0066 units sticking straight out of the bark. But the binormal is only 10.8 degrees from the wire’s direction, so that half of the circle is mostly pointing along the wire, and its width across the wire shrinks to 0.0066 × sin 10.8° = 0.0012 units. In real units: a blade 1.65 mm tall and 0.31 mm thick. A thread on a screw, not a wire round a branch. The tighter the pitch, the closer to 90 degrees the tilt and the thinner the blade, which is why the doc comment on addWireCoil says the rings are square to the helix “so the tight pitch does not skew them”.
What changed since the teaser
The last post ended its “what the code taught me” section by noting that the trunk’s radius had a floor in the bark and not in branchPoint or the wire anchor, and that “the trunk side has no such guard yet”. PR #228 closed that the same day. There is now one function, trunkRadiusAt in src/sim/geometry.ts, the girth halved and tapered and never below TRUNK_MIN_RADIUS (0.001 units), and the trunk’s rings, branchPoint and addWireAnchor all call it. The test wraps the anchor on the floored trunk radius at a fully tapered tip puts a wired branch at the very top of a fully tapered trunk and checks the anchor sits at 0.001 + wire radius. The same PR fixed the DISPLAY_SCALE comment I complained about: it now says one metre is four units.
What the code taught me while I checked it
The rings twist. u depends only on n and a fixed reference, so nothing ties one ring’s point 0 to the next ring’s. Through most of a turn the next ring’s point 0 is 4 to 5 degrees round from the last one’s. Where the wire climbs or drops down the side of the branch, close to vertical, it swings 53 to 58 degrees, and once a turn, where the reference flips from +z back to +x, about 123 degrees (143 at the worst band on this branch). Because stitchRings joins point i to point i, a band twisted by φ pinches in the middle to roughly cos(φ / 2) of its radius: under half the wire’s radius at 123 degrees, about a third at the worst. I measured that with a throwaway script, not a test. The prototype knew: its comment on build_ring_with_basis says the reference-axis switch “can flip vertex ordering between consecutive rings, twisting the geometry”, which is why the trunk uses frames carried from ring to ring. The wire does not yet. On a wire 1.65 mm thick it is a dark pinch you would have to look for, and the fix belongs in its own PR, not a blog post.
Branch girth is a radius. The trunk’s girthMm is a diameter and gets halved. A branch’s girth goes into branchRadiusAt unhalved, so the fixture’s “3.3 mm” branch has a 3.3 mm radius. That is the prototype’s arithmetic, ported unchanged; I’m noting it so nobody trips on it.
Coming next
Z-up in a y-down world, the deeper cut. Every frame in this post assumed z is up. The screen disagrees: its y runs down. The next post follows a point through the projection matrix’s sign flips and the look-at convention, and the bug where the trunk’s screen-relative wiring buttons came out Left/Right swapped because of exactly that flip.