dymokomi/luce-geocore
Source of release 0.7.6
0.7.6 / src / geometries / shading / smooth_normals.lucb
## Smooth shading normals, the kernels of ShadingCache: each corner averages
## the normals of the faces around its point, weighted by their angle there,
## over the faces within the cusp angle of its own face (60 degrees, as
## Houdini's default), so hard edges stay hard. Where no two faces at a point
## lie past the cusp of each other, all its corners agree and one normal per
## point serves them. All passes run in parallel and share nothing.
from luce_std import math
import memory
from luce_std import parallel
from core.float3 import Float3
from math.vector import Vector3
import core.ranges
## cos(60 degrees): faces meeting at a sharper angle keep separate normals.
let cusp_cosine: f64 = 0.5
## The arrays smooth normals read.
pub struct Surface:
pub let positions: const Float3[]
pub let offsets: const i32[]
pub let corners: const i32[]
pub let normals: const Float3[]
pub let incident_offsets: const i32[]
pub let incident_faces: const i32[]
## Every point's normal into `output`, and whether each point's corners split
## at a cusp (`split`); true when any point does.
pub func point_normals(shape: const Surface*, output: Float3[], split: bool[]) -> bool:
var work = PointWork(shape, output, split)
ranges.each_range(shape.positions.length, 2048, (void*)&work, points_range)
return work.splits.load(.acquire) != 0
## Every corner's normal: its point's where the point does not split, else
## the average over the faces within the cusp of the corner's own face.
pub func corner_normals(shape: const Surface*, points: const Float3[], split: const bool[], output: Float3[]):
var work = CornerWork(shape, points, split, output)
ranges.each_range(shape.offsets.length - 1, 2048, (void*)&work, corners_range)
## Every point of a face around a moved point; the caller frees the list.
pub func affected_points(shape: const Surface*, moved: const u32[]) -> u32[]!:
let points = shape.positions.length
let marked = try new bool[points] --- in memory.heap
defer free(marked) in memory.heap
memory.set(u8[]((u8*)marked.data, points), 0)
var count: usize = 0
for point in moved:
for at in (usize)shape.incident_offsets[(usize)point]..<(usize)shape.incident_offsets[(usize)point + 1]:
let face = (usize)shape.incident_faces[at]
for corner in (usize)shape.offsets[face]..<(usize)shape.offsets[face + 1]:
let p = (usize)shape.corners[corner]
if not marked[p]:
marked[p] = true
count += 1
let affected = try new u32[count] --- in memory.heap
var written: usize = 0
for point in 0..<points:
if marked[point]:
affected[written] = (u32)point
written += 1
return affected
## Redo the per-point normals of `points`, marking in `split` those that now
## split at a cusp; true when any does (per-point storage no longer holds).
pub func refresh_points(shape: const Surface*, points: const u32[], output: Float3[], split: bool[]) -> bool:
var work = RefreshWork(shape, points, output, split)
ranges.each_range(points.length, 1024, (void*)&work, refresh_range)
return work.splits.load(.acquire) != 0
## Redo the per-corner normals at `points`.
pub func refresh_corners(shape: const Surface*, points: const u32[], output: Float3[]):
var none_split: bool[]
var work = RefreshWork(shape, points, output, none_split)
ranges.each_range(points.length, 1024, (void*)&work, corners_at_range)
struct PointWork:
let shape: const Surface*
let output: Float3[]
let split: bool[]
var splits: @u32
func points_range(context: void*, first: usize, end: usize, worker: usize):
let work = (PointWork*)context
for point in first..<end:
let result = point_normal(work.shape, point)
work.output[point] = Float3.of(result.0)
work.split[point] = result.1
if result.1: work.splits.store(1, .relaxed)
struct CornerWork:
let shape: const Surface*
let points: const Float3[]
let split: const bool[]
let output: Float3[]
func corners_range(context: void*, first: usize, end: usize, worker: usize):
let work = (CornerWork*)context
for face in first..<end:
for corner in (usize)work.shape.offsets[face]..<(usize)work.shape.offsets[face + 1]:
let point = (usize)work.shape.corners[corner]
work.output[corner] = Float3.of(corner_normal(work.shape, face, corner)) if work.split[point] else work.points[point]
struct RefreshWork:
let shape: const Surface*
let points: const u32[]
let output: Float3[]
let split: bool[]
var splits: @u32
func refresh_range(context: void*, first: usize, end: usize, worker: usize):
let work = (RefreshWork*)context
for at in first..<end:
let point = (usize)work.points[at]
let result = point_normal(work.shape, point)
work.output[point] = Float3.of(result.0)
work.split[point] = result.1
if result.1: work.splits.store(1, .relaxed)
func corners_at_range(context: void*, first: usize, end: usize, worker: usize):
let work = (RefreshWork*)context
let shape = work.shape
for at in first..<end:
let point = work.points[at]
for index in (usize)shape.incident_offsets[(usize)point]..<(usize)shape.incident_offsets[(usize)point + 1]:
let face = (usize)shape.incident_faces[index]
for corner in (usize)shape.offsets[face]..<(usize)shape.offsets[face + 1]:
if (u32)shape.corners[corner] == point:
work.output[corner] = Float3.of(corner_normal(shape, face, corner))
## A point's angle-weighted average over all its faces, and whether any two
## of them lie past the cusp of each other.
pub func point_normal(shape: const Surface*, point: usize) -> (Vector3, bool):
let first = (usize)shape.incident_offsets[point]
let end = (usize)shape.incident_offsets[point + 1]
var sum = Vector3()
var split = false
for index in first..<end:
let face = (usize)shape.incident_faces[index]
let normal = shape.normals[face].vector()
for other in first..<index:
split = split or normal.dot(shape.normals[(usize)shape.incident_faces[other]].vector()) < cusp_cosine
sum = sum.add(normal.multiply_scalar(corner_angle(shape, face, point)))
let length = sum.length()
if length > 1e-20: return (sum.multiply_scalar(1.0 / length), split)
if end > first: return (shape.normals[(usize)shape.incident_faces[first]].vector(), split)
return (Vector3(0.0, 1.0, 0.0), split)
## The angle-weighted normal of `corner` (of `face`) over the faces around its
## point within the cusp angle; the face normal when they cancel.
pub func corner_normal(shape: const Surface*, face: usize, corner: usize) -> Vector3:
let own = shape.normals[face].vector()
let point = (usize)shape.corners[corner]
var sum = Vector3()
for index in (usize)shape.incident_offsets[point]..<(usize)shape.incident_offsets[point + 1]:
let other = (usize)shape.incident_faces[index]
let normal = shape.normals[other].vector()
if normal.dot(own) < cusp_cosine: continue
sum = sum.add(normal.multiply_scalar(corner_angle(shape, other, point)))
let length = sum.length()
return sum.multiply_scalar(1.0 / length) if length > 1e-20 else own
## The interior angle of `face` at `point` (its first corner there).
func corner_angle(shape: const Surface*, face: usize, point: usize) -> f64:
let start = (usize)shape.offsets[face]
let count = (usize)shape.offsets[face + 1] - start
for at in 0..<count:
if (usize)shape.corners[start + at] != point: continue
let here = shape.positions[point].vector()
let previous = shape.positions[(usize)shape.corners[start + (at + count - 1) % count]].vector().subtract(here)
let next = shape.positions[(usize)shape.corners[start + (at + 1) % count]].vector().subtract(here)
let lengths = previous.length() * next.length()
if lengths <= 1e-30: return 0.0
return math.acos(math.max(-1.0, math.min(1.0, previous.dot(next) / lengths)))
return 0.0