Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / sketch / shadows.lucb

## A projection's shadows carried as text: what CadProjection casts (each
## edge's samples on the plane) with each edge's reference, without the
## body. A cast made where the body is (the editor's compute worker) is
## written with CadProjection.shadows_text, and read where it is not
## (CadShadows.parse): there the editor draws the shadows, finds the one
## under the pointer and projects it (CadSketch.project_shadow), linked by
## its reference as a projection made from the body is.
##
## The body's faces come too, for Project's whole face and body outline
## (shadow_faces.lucb): each face's name and its edges; and after the
## edges, the curved faces' silhouettes (silhouettes.lucb), shadows like
## the edges' with their own references. Each shadow says whether it bounds
## the body's outline, told where the body is.
##
## The text: a line per edge, then per silhouette,
## `e <reference> <height> <bounds> <x> <y> …`, the reference escaped as a
## sketch text's words, its mean height off the plane, 1 when it bounds the
## outline (else 0) and `projection_per_edge` x, y pairs; then a line per
## face, `f <name> <edge> …`, the name escaped alike.

pub struct CadShadows:
    var samples: Growing[f64]
    var heights: Growing[f64]
    ## Whether each shadow bounds the body's outline.
    var bounds: Growing[bool]
    ## Each edge's reference, a run of `names`.
    var starts: Growing[usize]
    var names: Growing[u8]
    ## Each face's name (a run of `face_chars`) and edges (a run of
    ## `face_edges`).
    var face_starts: Growing[usize]
    var face_chars: Growing[u8]
    var edge_starts: Growing[usize]
    var face_edges: Growing[i64]

    pub func close():
        self.samples.close()
        self.heights.close()
        self.bounds.close()
        self.starts.close()
        self.names.close()
        self.face_starts.close()
        self.face_chars.close()
        self.edge_starts.close()
        self.face_edges.close()

    func trace(visit: func(ownership.Object*, void*) -> unit, context: void*):
        _ = visit
        _ = context

    ## The shadows `text` describes (empty: none).
    pub static func parse(text: str) -> interop.Reference[CadShadows]!:
        var made = CadShadows(Growing[f64](), Growing[f64](), Growing[bool](), Growing[usize](), Growing[u8](), Growing[usize](), Growing[u8](), Growing[usize](), Growing[i64]())
        errdefer made.close()
        let per = projection_per_edge()
        var fields: str[72] = ---
        for at in 0..<(usize)72: fields[at] = ""
        let bytes = text.bytes
        var start: usize = 0
        while start < bytes.length:
            var end = start
            while end < bytes.length and bytes[end] != 10: end += 1
            let line = (str)bytes[start..<end]
            start = end + 1
            if line.length > 1 and line.bytes[0] == 102 and line.bytes[1] == 32:
                try made.read_face(line)
                continue
            let count = split(line, fields[0..])
            if count == 0: continue
            if fields[0] != "e" or count != 4 + 2 * per or made.face_starts.count > 0: error(invalid, "a shadows text has a line it does not know")
            try made.starts.push(made.names.count)
            try unescape(fields[1], &made.names)
            try made.heights.push(try strings.parse_f64(fields[2]))
            try made.bounds.push(fields[3] == "1")
            for at in 0..<2 * per:
                try made.samples.push(try strings.parse_f64(fields[4 + at]))
        try made.starts.push(made.names.count)
        try made.face_starts.push(made.face_chars.count)
        try made.edge_starts.push(made.face_edges.count)
        return try interop.Reference[CadShadows].adopt(new CadShadows(made.samples, made.heights, made.bounds, made.starts, made.names, made.face_starts, made.face_chars, made.edge_starts, made.face_edges) in memory.heap, cad_shadows_type, memory.heap)

    ## A face's line, `f <name> <edge> …`, read (after the edges).
    func read_face(line: str) -> !:
        try self.face_starts.push(self.face_chars.count)
        try self.edge_starts.push(self.face_edges.count)
        let bytes = line.bytes
        var at: usize = 2
        var word = 0
        while at < bytes.length:
            while at < bytes.length and bytes[at] == 32: at += 1
            if at >= bytes.length: break
            var end = at
            while end < bytes.length and bytes[end] != 32: end += 1
            let field = (str)bytes[at..<end]
            if word == 0: try unescape(field, &self.face_chars)
            else:
                let edge = strings.parse_i64(field) else error(invalid, "a shadows text's face names an edge that is not a whole number")
                if edge < 0 or edge >= self.edge_count(): error(invalid, "a shadows text's face names an edge it does not have")
                try self.face_edges.push(edge)
            word += 1
            at = end
        if word < 2: error(invalid, "a shadows text's face has no name or no edges")

    pub func edge_count() -> i64:
        return (i64)(self.samples.count // (2 * projection_per_edge()))

    ## The edge whose shadow passes nearest (`x`, `y`), within `reach`, or -1.
    pub func nearest(x: f64, y: f64, reach: f64) -> i64:
        return nearest_run(self.samples.view(), x, y, reach)

    ## Edge `edge`'s reference.
    pub func reference(edge: i64) -> interop.Owned[str]!:
        if edge < 0 or edge >= self.edge_count(): error(invalid, "the shadows have no such edge")
        return try interop.copy_text((str)self.names.view()[self.starts.items[(usize)edge]..<self.starts.items[(usize)edge + 1]])

    ## How many faces the body has.
    pub func face_count() -> i64:
        return (i64)self.face_starts.count - 1 if self.face_starts.count > 0 else 0

    ## Face `face`'s name.
    pub func face_name(face: i64) -> interop.Owned[str]!:
        if face < 0 or face >= self.face_count(): error(invalid, "the shadows have no such face")
        return try interop.copy_text(self.name_of(face))

    ## How many edges face `face`'s loops have, and the `at`th of them.
    pub func face_edge_count(face: i64) -> i64:
        if face < 0 or face >= self.face_count(): return 0
        return (i64)self.edges_of(face).length

    pub func face_edge(face: i64, at: i64) -> i64:
        if at < 0 or at >= self.face_edge_count(face): return -1
        return self.edges_of(face)[(usize)at]

    ## The face whose shadow holds (`x`, `y`), the one standing highest
    ## toward `toward` (+1: the plane's normal side, where the camera is;
    ## -1 the other) when several do (a box's top over its bottom); -1 for
    ## none. A face seen edge-on holds nothing.
    pub func face_at(x: f64, y: f64, toward: f64) -> i64:
        var best: i64 = -1
        var best_height = 0.0
        for f in 0..<self.face_count():
            if not self.holds(f, x, y): continue
            let height = self.face_height(f) * toward
            if best < 0 or height > best_height:
                best = f
                best_height = height
        return best

    ## `projection`'s shadows as text, every edge's reference with its
    ## height, whether it bounds the outline and its samples, then every
    ## silhouette's alike, then every face's name and edges (made where the
    ## body is).
    pub static func text_of(projection: interop.Reference[CadProjection]) -> interop.Owned[str]!:
        let p = try projection.get()
        let per = projection_per_edge()
        let model = p.model.value()
        var w = try shape_of(model)
        defer w.close()
        let normal = p.u.cross(p.v)
        let mesh = try model.tessellate(silhouette_divisions)
        defer mesh.release()
        # The silhouettes after the edges: samples, heights, references and
        # their outer sides.
        var samples = Growing[f64]()
        defer samples.close()
        try samples.assign(projection_samples(p))
        let edges = samples.count // (2 * per)
        var heights = Growing[f64]()
        defer heights.close()
        try heights.assign(p.heights.view())
        var faces = Growing[i64]()
        defer faces.close()
        var points = Growing[Vector3]()
        defer points.close()
        try silhouettes_of(&w, mesh.value(), normal, per, &faces, &points)
        var outward = Growing[f64]()
        defer outward.close()
        var names = Growing[u8]()
        defer names.close()
        var name_starts = Growing[usize]()
        defer name_starts.close()
        var reference = Growing[u8]()
        defer reference.close()
        for s in 0..<faces.count:
            let run = points.view()[s * per..<(s + 1) * per]
            var height = 0.0
            for q in run:
                let d = q.subtract(p.origin)
                try samples.push(d.dot(p.u))
                try samples.push(d.dot(p.v))
                height += d.dot(normal)
            try heights.push(height / (f64)per)
            let n = try w.face_normal((usize)faces.items[s], run[per // 2])
            try outward.push(n.dot(p.u))
            try outward.push(n.dot(p.v))
            try silhouette_reference(&w, (usize)faces.items[s], run, &reference)
            try name_starts.push(names.count)
            for byte in reference.view(): try names.push(byte)
        try name_starts.push(names.count)
        var bounds = Growing[bool]()
        defer bounds.close()
        try outline_flags(mesh.value(), samples.view(), per, edges, outward.view(), p.origin, p.u, p.v, &bounds)
        var out = Growing[u8]()
        defer out.close()
        for e in 0..<samples.count // (2 * per):
            # A seam (a face meeting itself) is no edge to Fusion: no
            # reference, no part of the outline.
            let seam = e < edges and (w.sides.items[e * 2] < 0 or w.sides.items[e * 2 + 1] < 0)
            if seam:
                reference.clear()
                try reference.push(45)
                bounds.items[e] = false
            elif e < edges:
                try references_of(&w, [(i64)e], &reference)
            else:
                reference.clear()
                for byte in names.view()[name_starts.items[e - edges]..<name_starts.items[e - edges + 1]]: try reference.push(byte)
            try put_text(&out, "e ")
            try put_escaped(&out, (str)reference.view())
            try put_number(&out, heights.items[e])
            try put_text(&out, " 1" if bounds.items[e] else " 0")
            for value in samples.view()[e * 2 * per..<(e + 1) * 2 * per]:
                try put_number(&out, value)
            try out.push(10)
        for f in 0..<w.faces.count:
            try put_text(&out, "f ")
            try put_escaped(&out, w.face_name(f))
            for use in w.face_uses(f):
                try put_index(&out, (i64)edge_index(use))
            try out.push(10)
        return try interop.copy_text((str)out.view())

    ## Edge `edge`'s reference, as kept.
    func reference_of(edge: i64) -> str:
        return (str)self.names.view()[self.starts.items[(usize)edge]..<self.starts.items[(usize)edge + 1]]

    ## Face `face`'s name, edges and mean height.
    func name_of(face: i64) -> str:
        return (str)self.face_chars.view()[self.face_starts.items[(usize)face]..<self.face_starts.items[(usize)face + 1]]

    func edges_of(face: i64) -> const i64[]:
        return self.face_edges.view()[self.edge_starts.items[(usize)face]..<self.edge_starts.items[(usize)face + 1]]

    func face_height(face: i64) -> f64:
        var sum = 0.0
        let edges = self.edges_of(face)
        for e in edges: sum += self.heights.items[(usize)e]
        return sum / (f64)(edges.length if edges.length > 0 else 1)

    ## Whether face `face`'s shadow holds (`x`, `y`): its loops' samples
    ## crossed an odd number of times by the ray to +x.
    func holds(face: i64, x: f64, y: f64) -> bool:
        var inside = false
        let per = projection_per_edge()
        for e in self.edges_of(face):
            let run = self.run(e)
            for k in 0..<per - 1:
                let ay = run[2 * k + 1]
                let by = run[2 * k + 3]
                if (ay > y) == (by > y): continue
                let ax = run[2 * k]
                let bx = run[2 * k + 2]
                if ax + (y - ay) / (by - ay) * (bx - ax) > x: inside = not inside
        return inside

    ## Edge `edge`'s samples (x, y pairs on the plane).
    func run(edge: i64) -> const f64[]:
        let per = projection_per_edge()
        return self.samples.view()[(usize)edge * 2 * per..<((usize)edge + 1) * 2 * per]

pub let cad_shadows_type: interop.Type[CadShadows] = interop.Type[CadShadows]("CadShadows", CadShadows.close, CadShadows.trace, closeable = true)

## The samples of shadows (x, y pairs, projection_per_edge an edge), for
## the editor's drawing.
pub func shadow_samples(s: const CadShadows*) -> const f64[]:
    return s.samples.view()

## The run (of `projection_per_edge` x, y pairs) in `samples` passing
## nearest (`x`, `y`) within `reach`, an edge seen end-on skipped; -1 for
## none.
func nearest_run(samples: const f64[], x: f64, y: f64, reach: f64) -> i64:
    let per = projection_per_edge()
    var best: i64 = -1
    var best_distance = reach
    for e in 0..<samples.length // (2 * per):
        let run = samples[e * 2 * per..<(e + 1) * 2 * per]
        if end_on(run, 1e-9 * math.max(1.0, reach)): continue
        for k in 0..<per - 1:
            let d = segment_distance(x, y, run[2 * k], run[2 * k + 1], run[2 * k + 2], run[2 * k + 3])
            if d <= best_distance:
                best_distance = d
                best = (i64)e
    return best