dymokomi/luce-cad
Source of release 0.9.5
0.9.5 / src / modeling / revolve.lucb
## Revolution: each profile region turned a full turn about an axis in its
## plane into a closed solid. Each profile edge sweeps a face and each vertex
## a circle (none on the axis, where the vertex is a pole):
##
## a line along the axis a cylinder a line across it a plane
## a line at an angle a cone a line on it nothing
## an arc centered on it a sphere an arc off it a torus
##
## The profile edge itself is its face's seam, used both ways, as CadSolids'
## cylinders, cones and tori have it; planes and sphere caps (which reach the
## axis) need none. The profile must not cross the axis.
## The profile's faces turned about the line through `origin` along `axis`
## (unit, in the plane), into `b`, named for feature `tag` (naming.lucb):
## a full turn, or `angle` radians (from the profile's plane toward its
## normal), closed by the profile at each end.
pub func revolve_into(b: BrepModel*, p: Profiles*, origin: Vector3, axis: Vector3, tol: f64, tag: str, angle: f64 = 2.0 * math.pi) -> !:
if not math.is_finite(angle) or angle <= angle_slack: error(invalid, "a revolve needs a positive angle")
let partial = angle < 2.0 * math.pi - angle_slack
# Turn the way that sweeps the profile's plane toward its normal at
# angle 0, so loops counterclockwise about the normal give outward faces.
var a = axis
var side = Vector3()
# Which side of the axis the profile is on: from its edges' middles
# (a half disc's vertices are all on the axis).
for e in p.edges.view():
let offset = (try p.edge_at(&e, 0.5)).subtract(origin)
let r = offset.subtract(axis.multiply_scalar(offset.dot(axis)))
if r.length() > tol * 10.0:
side = r
break
if side.length() == 0.0: error(invalid, "the profile lies on the axis")
if a.cross(side).dot(p.normal) < 0.0: a = axis.multiply_scalar(-1.0)
for e in p.edges.view():
let offset = e.start.subtract(origin)
let r = offset.subtract(axis.multiply_scalar(offset.dot(axis)))
if r.length() > tol * 10.0 and r.dot(side) < 0.0: error(invalid, "a revolved profile must lie on one side of its axis")
# Splines turn by parts of at most a half turn (each face's chart then
# opens without a seam), a full turn as two halves.
var splines = false
for e in p.edges.view():
if e.kind == profile_spline: splines = true
var edges = Growing[ProfileEdge]()
defer edges.close()
var halves = Growing[bool]()
defer halves.close()
var caps = Caps(Growing[i64](), Growing[i64](), Growing[i64]())
defer caps.close()
for region in 0..<p.region_count():
caps.clear()
for loop in p.region(region):
# An arc from the axis back to it (a sphere's) is two halves,
# each a cap.
edges.clear()
halves.clear()
for e in p.loop_edges(loop):
let ends_on_axis = axis_distance(e.start, origin, a).0 <= tol * 10.0 and axis_distance(e.end, origin, a).0 <= tol * 10.0
if e.kind == profile_arc and ends_on_axis:
let middle = try p.edge_at(&e, 0.5)
try edges.push(ProfileEdge(1, e.start, middle, e.center, e.radius, e.sweep * 0.5, -1, e.curve, e.piece))
try edges.push(ProfileEdge(1, middle, e.end, e.center, e.radius, e.sweep * 0.5, -1, e.curve, e.piece))
try halves.push(true)
try halves.push(true)
else:
try edges.push(e)
try halves.push(false)
if partial or splines:
try revolve_parts(b, p, edges.view(), origin, a, tol, tag, angle, partial, &caps)
else:
try revolve_loop(b, p, edges.view(), halves.view(), origin, a, tol, tag)
if partial: try close_part(b, p, &caps, origin, a, angle, tag, p.loop_edges(p.region(region)[0]))
## How far `point` is from the axis, and where it projects onto it.
func axis_distance(point: Vector3, origin: Vector3, a: Vector3) -> (f64, Vector3):
let foot = origin.add(a.multiply_scalar(point.subtract(origin).dot(a)))
return (point.subtract(foot).length(), foot)
## One loop's faces; `halves` marks the edges that are half an arc (named
## a and b in turn).
func revolve_loop(b: BrepModel*, p: const Profiles*, edges: const ProfileEdge[], halves: const bool[], origin: Vector3, a: Vector3, tol: f64, tag: str) -> !:
let count = edges.length
var name = Growing[u8]()
defer name.close()
var second_half = false
# Vertex i (edge i's start), its circle (-1 on the axis), each edge's seam.
var vertices = Growing[i64]()
defer vertices.close()
var circles = Growing[i64]()
defer circles.close()
for at in 0..<count:
let point = edges[at].start
let v = try b.add_vertex(point)
try vertices.push(v)
let (r, foot) = axis_distance(point, origin, a)
if r <= tol * 10.0: try circles.push(-1)
else: try circles.push(try b.add_circle(v, v, foot, a, point.subtract(foot), r, true))
for at in 0..<count:
let next = (at + 1) % count
let e = &edges[at]
let c0 = circles.items[at]
let c1 = circles.items[next]
if c0 < 0 and c1 < 0 and e.kind == profile_line: continue
# The face's outward normal at the seam: right of the edge.
let middle = try p.edge_at(e, 0.5)
let tangent = try (try p.edge_at(e, 0.501)).subtract(try p.edge_at(e, 0.499)).normalized()
let outward = tangent.cross(p.normal)
let (surface, cap) = try swept_surface(b, p, e, origin, a, tol)
let sense = (try surface_normal_at(b, surface, middle)).dot(outward) > 0.0
var uses = Growing[i64]()
defer uses.close()
var sizes = Growing[i64]()
defer sizes.close()
if cap:
# A plane or a sphere cap: its circles, no seam; the larger first.
let r0 = axis_distance(e.start, origin, a).0
let r1 = axis_distance(e.end, origin, a).0
if c1 >= 0 and (c0 < 0 or r1 >= r0):
try uses.push(c1 + 1)
try sizes.push(1)
if c0 >= 0:
try uses.push(-(c0 + 1))
try sizes.push(1)
else:
try uses.push(-(c0 + 1))
try sizes.push(1)
if c1 >= 0:
try uses.push(c1 + 1)
try sizes.push(1)
else:
let seam = (try edge_curve(b, p, e, Vector3(), p.normal, vertices.items[at], vertices.items[next])) + 1
try uses.push(seam)
if c1 >= 0: try uses.push(c1 + 1)
try uses.push(-seam)
if c0 >= 0: try uses.push(-(c0 + 1))
try sizes.push((i64)uses.count)
let face = try b.add_face(surface, uses.view(), sizes.view(), sense)
var suffix = ""
if halves[at]:
suffix = "b" if second_half else "a"
second_half = not second_half
try side_name(&name, tag, e, suffix)
try b.set_face_name(face, (str)name.view())
## The surface line or arc `e` sweeps about the axis, and whether its face is a cap
## (a plane, or a sphere reaching the axis: bounded by circles alone).
## A part revolve passes `pole`: a sphere's pole off the swept region.
func swept_surface(b: BrepModel*, p: const Profiles*, e: const ProfileEdge*, origin: Vector3, a: Vector3, tol: f64, pole: Vector3 = Vector3()) -> (i64, bool)!:
let (r0, f0) = axis_distance(e.start, origin, a)
let (r1, f1) = axis_distance(e.end, origin, a)
if e.kind == profile_line:
let h0 = e.start.subtract(origin).dot(a)
let h1 = e.end.subtract(origin).dot(a)
if math.abs(h1 - h0) <= tol:
return (try b.add_surface(0, e.start, a, e.start.subtract(f0) if r0 > tol else e.end.subtract(f1)), true)
if math.abs(r1 - r0) <= tol:
return (try b.add_surface(1, f0, a, e.start.subtract(f0), r0), false)
# A cone: its base where the edge is farther out, opening toward the other end.
let angle = math.atan((r1 - r0) / (h1 - h0))
if r0 >= r1: return (try b.add_surface(2, f0, a, e.start.subtract(f0), r0, angle), false)
return (try b.add_surface(2, f1, a, e.end.subtract(f1), r1, angle), false)
if e.kind == profile_arc:
let (rc, fc) = axis_distance(e.center, origin, a)
if rc <= tol * 10.0:
if pole.length() > 0.0:
return (try b.add_surface(4, e.center, pole, try across(pole), e.radius), false)
let reference = e.start.subtract(f0) if r0 > tol else e.end.subtract(f1)
return (try b.add_surface(4, e.center, a, reference, e.radius), r0 <= tol * 10.0 or r1 <= tol * 10.0)
if e.radius >= rc: error(invalid, "an arc revolved about an axis it reaches past would make a torus through itself")
return (try b.add_surface(5, fc, a, e.center.subtract(fc), rc, e.radius), false)
# Spline edges are turned by spline_revolution, never here.
error(invalid, "internal: swept_surface takes lines and arcs only")
## Surface `s` of `b`'s unit normal at `p`.
func surface_normal_at(b: const BrepModel*, s: i64, p: Vector3) -> Vector3!:
var record = try b.surface_record(s)
defer record.close()
return try normal_at(&record, p, b.cad_tolerances().resolution())
## A part revolve's end caps: each loop's edges at the start and at the end
## (in profile order) and the loops' sizes.
struct Caps:
var start: Growing[i64]
var end: Growing[i64]
var sizes: Growing[i64]
func close():
self.start.close()
self.end.close()
self.sizes.close()
func clear():
self.start.clear()
self.end.clear()
self.sizes.clear()
## `v` turned `angle` about the line through `origin` along unit `a`.
func turned_point(v: Vector3, origin: Vector3, a: Vector3, angle: f64) -> Vector3:
return origin.add(turned_direction(v.subtract(origin), a, angle))
## Direction `d` turned `angle` about unit `a` (Rodrigues).
func turned_direction(d: Vector3, a: Vector3, angle: f64) -> Vector3:
let c = math.cos(angle)
let s = math.sin(angle)
return d.multiply_scalar(c).add(a.cross(d).multiply_scalar(s)).add(a.multiply_scalar(a.dot(d) * (1.0 - c)))
## A loop's vertices and edges at one angle of a turn (its profile, or a
## part's end).
struct Ring:
var vertices: Growing[i64]
var edges: Growing[i64]
func close():
self.vertices.close()
self.edges.close()
func new_ring() -> Ring:
return Ring(Growing[i64](), Growing[i64]())
## The loop's ring at angle 0: its vertices and edges as they lie.
func profile_ring(b: BrepModel*, p: const Profiles*, edges: const ProfileEdge[], ring: Ring*) -> !:
ring.vertices.clear()
ring.edges.clear()
for e in edges: try ring.vertices.push(try b.add_vertex(e.start))
for at in 0..<edges.length:
try ring.edges.push(try edge_curve(b, p, &edges[at], Vector3(), p.normal, ring.vertices.items[at], ring.vertices.items[(at + 1) % edges.length]))
## One loop turned from angle `begin` (its ring `start` there) to `finish`:
## each vertex's arc (none on the axis), each edge turned to the end, and
## the face each sweeps. The end ring onto `end`, or (`closing`, a full
## turn's last part) the given ring there.
func revolve_turn(b: BrepModel*, p: const Profiles*, edges: const ProfileEdge[], origin: Vector3, a: Vector3, tol: f64, tag: str, begin: f64, finish: f64, start: const Ring*, closing: const Ring*?, end: Ring*) -> !:
let count = edges.length
end.vertices.clear()
end.edges.clear()
var arcs = Growing[i64]()
defer arcs.close()
for at in 0..<count:
let point = edges[at].start
let v0 = start.vertices.items[at]
let (r, foot) = axis_distance(point, origin, a)
if r <= tol * 10.0:
try end.vertices.push(v0)
try arcs.push(-1)
continue
var v1: i64 = -1
if let ring = closing: v1 = ring.vertices.items[at]
else: v1 = try b.add_vertex(turned_point(point, origin, a, finish))
try end.vertices.push(v1)
try arcs.push(try b.add_circle(v0, v1, foot, a, turned_direction(point.subtract(foot), a, begin), r, true))
var name = Growing[u8]()
defer name.close()
let normal_end = turned_direction(p.normal, a, finish)
for at in 0..<count:
let next = (at + 1) % count
let e = &edges[at]
let a0 = arcs.items[at]
let a1 = arcs.items[next]
let at_start = start.edges.items[at]
# A line on the axis stays where it is: both ends share it.
let on_axis = a0 < 0 and a1 < 0 and e.kind == profile_line
var at_end = at_start
if not on_axis:
if let ring = closing: at_end = ring.edges.items[at]
else: at_end = try turned_edge(b, p, e, origin, a, finish, normal_end, end.vertices.items[at], end.vertices.items[next])
try end.edges.push(at_end)
if on_axis: continue
let middle = try p.edge_at(e, 0.5)
let tangent = try (try p.edge_at(e, 0.501)).subtract(try p.edge_at(e, 0.499)).normalized()
let outward = turned_direction(tangent.cross(p.normal), a, begin)
let middle_here = turned_point(middle, origin, a, begin)
# A sphere's poles both off its face: the face (half a profile arc
# turned) lies on one side of the axis's middle plane and toward
# the turn's middle, so its pole leans along the axis that side
# and away from the turn's middle.
let radial = middle.subtract(axis_distance(middle, origin, a).1)
var pole = Vector3()
if e.kind == profile_arc and radial.length() > tol:
let side = 1.0 if middle.subtract(e.center).dot(a) >= 0.0 else -1.0
pole = try a.multiply_scalar(side).subtract(turned_direction(try radial.normalized(), a, (begin + finish) * 0.5)).normalized()
var surface: i64 = -1
if e.kind == profile_spline: surface = try spline_revolution(b, p, e, origin, a, begin, finish - begin)
else:
let (made, cap) = try swept_surface(b, p, e, origin, a, tol, pole)
_ = cap
surface = made
let sense = (try surface_normal_at(b, surface, middle_here)).dot(outward) > 0.0
var uses = Growing[i64]()
defer uses.close()
try uses.push(at_start + 1)
if a1 >= 0: try uses.push(a1 + 1)
try uses.push(-(at_end + 1))
if a0 >= 0: try uses.push(-(a0 + 1))
let sizes: i64[1] = [(i64)uses.count]
let face = try b.add_face(surface, uses.view(), sizes[..<1], sense)
try side_name(&name, tag, e, "")
try b.set_face_name(face, (str)name.view())
## Spline edge `e` turned from `begin` through `sweep` (at most a half turn)
## as a rational surface of revolution: u along the edge, v around in two
## spans of rational quadratic arcs (each a quarter turn at most, its middle
## weight the cosine of half its angle).
func spline_revolution(b: BrepModel*, p: const Profiles*, e: const ProfileEdge*, origin: Vector3, a: Vector3, begin: f64, sweep: f64) -> i64!:
if sweep > math.pi + angle_slack: error(invalid, "a spline is turned at most a half turn per face")
let (points, weights, knots, degree) = p.spline_net(e.spline)
let half = sweep * 0.25
let middle_weight = math.cos(half)
var net = Growing[Vector3]()
defer net.close()
var w = Growing[f64]()
defer w.close()
for at in 0..<points.length:
let start = turned_point(points[at], origin, a, begin)
let (r, foot) = axis_distance(start, origin, a)
let radial = start.subtract(foot)
for k in 0..<5:
let angle = half * (f64)k
let reach = 1.0 if k % 2 == 0 else 1.0 / middle_weight
_ = r
try net.push(foot.add(turned_direction(radial, a, angle).multiply_scalar(reach)))
try w.push(weights[at] * (1.0 if k % 2 == 0 else middle_weight))
# Knots from 0 to 1 both ways (inversion expects it).
var ku = Growing[f64]()
defer ku.close()
let low = knots[0]
let span = knots[knots.length - 1] - low
for k in knots: try ku.push((k - low) / span)
let kv: f64[8] = [0.0, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0, 1.0]
return try b.add_spline_surface(net.view(), w.view(), (i64)points.length, 5, degree, 2, ku.view(), kv[..<8])
## Profile edge `e` turned `angle` about the axis, between vertices
## `start` and `end` of `b` (its plane's normal turned: `normal`).
func turned_edge(b: BrepModel*, p: const Profiles*, e: const ProfileEdge*, origin: Vector3, a: Vector3, angle: f64, normal: Vector3, start: i64, end: i64) -> i64!:
if e.kind == profile_line: return try b.add_line(start, end)
if e.kind == profile_arc:
return try b.add_circle(start, end, turned_point(e.center, origin, a, angle), normal, turned_direction(e.start.subtract(e.center), a, angle), e.radius, e.sweep > 0.0)
let (points, weights, knots, degree) = p.spline_net(e.spline)
var moved = Growing[Vector3]()
defer moved.close()
for point in points: try moved.push(turned_point(point, origin, a, angle))
return try b.add_spline_edge(start, end, moved.view(), weights, degree, knots, true)
## A loop turned through `angle` in parts (a half turn at most each, with
## splines; one part otherwise), the start and end rings' edges onto `caps`
## when `partial` (a full turn's last part closes on its first ring).
func revolve_parts(b: BrepModel*, p: const Profiles*, edges: const ProfileEdge[], origin: Vector3, a: Vector3, tol: f64, tag: str, angle: f64, partial: bool, caps: Caps*) -> !:
var splines = false
for e in edges:
if e.kind == profile_spline: splines = true
let parts = (usize)math.max(1.0, math.ceil(angle / math.pi - angle_slack)) if splines else 1
var first = new_ring()
defer first.close()
try profile_ring(b, p, edges, &first)
var current = new_ring()
defer current.close()
var next = new_ring()
defer next.close()
try current.vertices.assign(first.vertices.view())
try current.edges.assign(first.edges.view())
for part in 0..<parts:
let begin = angle * (f64)part / (f64)parts
let finish = angle * (f64)(part + 1) / (f64)parts
let closing: const Ring*? = (&first) if not partial and part + 1 == parts else none
try revolve_turn(b, p, edges, origin, a, tol, tag, begin, finish, ¤t, closing, &next)
try current.vertices.assign(next.vertices.view())
try current.edges.assign(next.edges.view())
if partial:
for e in first.edges.view(): try caps.start.push(e)
for e in current.edges.view(): try caps.end.push(e)
try caps.sizes.push((i64)edges.length)
## A part revolve's caps: the profile region at the start (facing back
## against the turn) and turned to the end (facing on).
func close_part(b: BrepModel*, p: const Profiles*, caps: const Caps*, origin: Vector3, a: Vector3, angle: f64, tag: str, outer: const ProfileEdge[]) -> !:
var start = Growing[i64]()
defer start.close()
var at: usize = 0
for size in caps.sizes.view():
# Each loop backwards.
var k = (usize)size
while k > 0:
k -= 1
try start.push(-(caps.start.items[at + k] + 1))
at += (usize)size
var end = Growing[i64]()
defer end.close()
for e in caps.end.view(): try end.push(e + 1)
let reference = outer[0].end.subtract(outer[0].start) if outer[0].kind == profile_line else outer[0].start.subtract(outer[0].center)
var name = Growing[u8]()
defer name.close()
let back = try b.add_surface(0, outer[0].start, p.normal.multiply_scalar(-1.0), reference)
let first = try b.add_face(back, start.view(), caps.sizes.view())
try cap_name(&name, tag, "start", outer)
try b.set_face_name(first, (str)name.view())
let on = try b.add_surface(0, turned_point(outer[0].start, origin, a, angle), turned_direction(p.normal, a, angle), turned_direction(reference, a, angle))
let last = try b.add_face(on, end.view(), caps.sizes.view())
try cap_name(&name, tag, "end", outer)
try b.set_face_name(last, (str)name.view())