dymokomi/luce-geocore
Source of release 0.7.8
0.7.8 / src / geometries / shading / curvature.lucb
## Discrete principal curvatures of a polygon mesh's points (Meyer, Desbrun,
## Schröder and Barr 2003) over its display triangles: the mean curvature from
## the cotangent Laplacian, the Gaussian from the angle defect, both over the
## mixed Voronoi area, and k1 >= k2 from them. Positive where the surface
## bends away from its normals (a sphere with outward normals: 1/r). Boundary
## points, where neither estimate holds, read zero. Computed in parallel over
## points; each triangle is visited by its three points, so nothing is shared.
from luce_std import math
from luce_std import parallel
from core.float3 import Float3
from math.vector import Vector3
import core.ranges
## The arrays the estimate reads.
pub struct CurvatureShape:
pub let positions: const Float3[]
pub let offsets: const i32[]
pub let corners: const i32[]
pub let triangles: const i32[]
pub let normals: const Float3[]
pub let incident_offsets: const i32[]
pub let incident_faces: const i32[]
## Whether each point lies on an open boundary.
pub let boundary: const bool[]
## k1 and k2 of every point into `out` (two per point).
pub func point_curvatures(shape: const CurvatureShape*, out: f32[]):
var work = Work(shape, out)
ranges.each_range(shape.positions.length, 2048, (void*)&work, points_range)
struct Work:
let shape: const CurvatureShape*
let out: f32[]
func points_range(context: void*, first: usize, end: usize, worker: usize):
let work = (Work*)context
for point in first..<end:
let k = principal(work.shape, point)
work.out[point * 2] = (f32)k.x
work.out[point * 2 + 1] = (f32)k.y
## (k1, k2, 0) at `point`.
func principal(shape: const CurvatureShape*, point: usize) -> Vector3:
if shape.boundary[point]: return Vector3()
let p = shape.positions[point].vector()
var laplacian = Vector3()
var normal = Vector3()
var defect: f64 = 0.0
var area: f64 = 0.0
let first = (usize)shape.incident_offsets[point]
let end = (usize)shape.incident_offsets[point + 1]
for index in first..<end:
let face = (usize)shape.incident_faces[index]
# A face at the point twice (a pinch) is listed twice; count it once.
var seen = false
for earlier in first..<index:
seen = seen or (usize)shape.incident_faces[earlier] == face
if seen: continue
normal = normal.add(shape.normals[face].vector())
let start = (usize)shape.offsets[face]
let count = (usize)shape.offsets[face + 1] - start
let base = (start - face * 2) * 3
for triangle in 0..<count - 2:
var ids: usize[3] = [0, 0, 0]
for at in 0..<(usize)3: ids[at] = (usize)shape.corners[(usize)shape.triangles[base + triangle * 3 + at]]
var turn: usize = 3
for at in 0..<(usize)3:
if ids[at] == point: turn = at
if turn == 3: continue
let q = shape.positions[ids[(turn + 1) % 3]].vector()
let r = shape.positions[ids[(turn + 2) % 3]].vector()
let pq = q.subtract(p)
let pr = r.subtract(p)
let twice = pq.cross(pr).length()
if twice <= 1e-30: continue
let cot_q = cotangent(p.subtract(q), r.subtract(q))
let cot_r = cotangent(p.subtract(r), q.subtract(r))
laplacian = laplacian.add(pq.multiply_scalar(-cot_r)).add(pr.multiply_scalar(-cot_q))
let angle = math.atan2(twice, pq.dot(pr))
defect += angle
let half_pi = math.pi / 2.0
if angle > half_pi:
area += twice * 0.25
elif cot_q < 0.0 or cot_r < 0.0:
area += twice * 0.125
else:
area += (pr.dot(pr) * cot_q + pq.dot(pq) * cot_r) * 0.125
if area <= 1e-30: return Vector3()
# Mean curvature normal K_H = laplacian / (2 area); H = |K_H| / 2.
var mean = laplacian.length() / (4.0 * area)
if laplacian.dot(normal) < 0.0: mean = -mean
let gaussian = (2.0 * math.pi - defect) / area
let spread = math.sqrt(math.max(0.0, mean * mean - gaussian))
return Vector3(mean + spread, mean - spread, 0.0)
## cot of the angle between `a` and `b`.
func cotangent(a: Vector3, b: Vector3) -> f64:
let sine = a.cross(b).length()
return a.dot(b) / sine if sine > 1e-30 else 0.0