use super::bed::{BedPoint, Circle, End, Start, advance, bed_point, nonnegative};
use super::chain::arc_candidate;
use crate::structures::numerics::culvert::arcs::ArcBand;
use crate::structures::numerics::culvert::arcs::cap::Shared;
use crate::structures::numerics::culvert::{Arithmetic, BarrelClient};
use crate::structures::tensor::culvert::{ArcRows, Bounds, SeriesTensors};
use numr::dtype::DType;
use numr::ops::{BinaryOps, CompareOps, ConditionalOps, ScalarOps};
use numr::runtime::Runtime;
use numr::tensor::Tensor;
use solvr::common::arithmetic::{ArithmeticResult, Interval};
pub(crate) struct FloorInputs<'i, R: Runtime<DType = DType>> {
pub(crate) floored: &'i Tensor<R>,
pub(crate) circular: &'i Tensor<R>,
pub(crate) boxed: &'i Tensor<R>,
pub(crate) bed: &'i Tensor<R>,
pub(crate) top: &'i Tensor<R>,
pub(crate) ratio: &'i Tensor<R>,
pub(crate) rise: &'i Tensor<R>,
pub(crate) span: &'i Tensor<R>,
pub(crate) arcs: Option<&'i ArcRows<R>>,
}
pub(crate) struct FloorLayout<R: Runtime<DType = DType>> {
pub(crate) bands: [ArcBand<R>; 3],
pub(crate) start_widths: [Bounds<R>; 3],
pub(crate) middles: [Tensor<R>; 3],
pub(crate) bed_width: Bounds<R>,
pub(crate) weight: Bounds<R>,
pub(crate) weighted_bed: Bounds<R>,
pub(crate) widest_depth: Bounds<R>,
pub(crate) widest_width: Tensor<R>,
pub(crate) full_area: Bounds<R>,
pub(crate) full_wall: Bounds<R>,
pub(crate) full_moment: Bounds<R>,
}
pub(super) struct Candidate<R: Runtime<DType = DType>> {
pub(super) bands: [ArcBand<R>; 3],
pub(super) widths: [Bounds<R>; 3],
pub(super) bed_width: Bounds<R>,
pub(super) widest_depth: Bounds<R>,
pub(super) widest_width: Tensor<R>,
pub(super) full_area: Bounds<R>,
pub(super) full_wall: Bounds<R>,
pub(super) full_moment: Bounds<R>,
}
pub(super) fn ends<R: Runtime<DType = DType>>(x: &Interval<R>) -> Bounds<R> {
[x.lower().clone(), x.upper().clone()]
}
pub(super) fn pick<R>(
c: &R::Client,
mask: &Tensor<R>,
[on_lower, on_upper]: &Bounds<R>,
[off_lower, off_upper]: &Bounds<R>,
) -> numr::error::Result<Bounds<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
Ok([
c.where_cond(mask, on_lower, off_lower)?,
c.where_cond(mask, on_upper, off_upper)?,
])
}
pub(super) fn pick_band<R>(
c: &R::Client,
mask: &Tensor<R>,
on: &ArcBand<R>,
off: &ArcBand<R>,
) -> numr::error::Result<ArcBand<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
Ok(ArcBand {
centre_x: pick(c, mask, &on.centre_x, &off.centre_x)?,
centre_y: pick(c, mask, &on.centre_y, &off.centre_y)?,
radius: pick(c, mask, &on.radius, &off.radius)?,
start_angle: pick(c, mask, &on.start_angle, &off.start_angle)?,
start_sin: pick(c, mask, &on.start_sin, &off.start_sin)?,
start_cos: pick(c, mask, &on.start_cos, &off.start_cos)?,
start_depth: pick(c, mask, &on.start_depth, &off.start_depth)?,
start_area: pick(c, mask, &on.start_area, &off.start_area)?,
start_perimeter: pick(c, mask, &on.start_perimeter, &off.start_perimeter)?,
start_moment: pick(c, mask, &on.start_moment, &off.start_moment)?,
circle_low: pick(c, mask, &on.circle_low, &off.circle_low)?,
circle_high: pick(c, mask, &on.circle_high, &off.circle_high)?,
})
}
pub(super) fn copy_band<R: Runtime<DType = DType>>(band: &ArcBand<R>) -> ArcBand<R> {
ArcBand {
centre_x: band.centre_x.clone(),
centre_y: band.centre_y.clone(),
radius: band.radius.clone(),
start_angle: band.start_angle.clone(),
start_sin: band.start_sin.clone(),
start_cos: band.start_cos.clone(),
start_depth: band.start_depth.clone(),
start_area: band.start_area.clone(),
start_perimeter: band.start_perimeter.clone(),
start_moment: band.start_moment.clone(),
circle_low: band.circle_low.clone(),
circle_high: band.circle_high.clone(),
}
}
pub(crate) fn layout<R>(
a: &mut Arithmetic<'_, R>,
inputs: &FloorInputs<'_, R>,
) -> ArithmeticResult<FloorLayout<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let like = inputs.rise;
let series = SeriesTensors::constant(like.shape()[0], like.device())?;
let shared = Shared::new(a, like, &series)?;
let circle = circle_candidate(a, &shared, inputs)?;
let open = match inputs.arcs {
Some(rows) => {
let arcs = arc_candidate(a, &shared, rows, inputs.floored, inputs.bed)?;
select(c, inputs.circular, &circle, &arcs)?
}
None => circle,
};
let boxed = inputs.boxed;
let span = a.point(inputs.span)?;
let top = a.point(inputs.top)?;
let box_area = a.mul(&span, &top)?;
let walls = a.mul(&shared.two, &top)?;
let box_wall = a.add(&walls, &span)?;
let lifted = a.mul(&box_area, &top)?;
let box_moment = a.mul(&lifted, &shared.half)?;
let span_bounds = [inputs.span.clone(), inputs.span.clone()];
let bed_width = pick(c, boxed, &span_bounds, &open.bed_width)?;
let full_area = pick(c, boxed, &ends(&box_area), &open.full_area)?;
let full_wall = pick(c, boxed, &ends(&box_wall), &open.full_wall)?;
let full_moment = pick(c, boxed, &ends(&box_moment), &open.full_moment)?;
let widest_width = c.where_cond(boxed, inputs.span, &open.widest_width)?;
let ratio = a.point(inputs.ratio)?;
let root = a.sqrt_nonnegative(&ratio)?;
let weight = a.mul(&ratio, &root)?;
let width = a.bounds(&bed_width[0], &bed_width[1])?;
let weighted_bed = a.mul(&weight, &width)?;
let middles = middles(c, &open.bands, inputs.top)?;
Ok(FloorLayout {
bands: open.bands,
start_widths: open.widths,
middles,
bed_width,
weight: ends(&weight),
weighted_bed: ends(&weighted_bed),
widest_depth: open.widest_depth,
widest_width,
full_area,
full_wall,
full_moment,
})
}
fn select<R>(
c: &R::Client,
circular: &Tensor<R>,
circle: &Candidate<R>,
arcs: &Candidate<R>,
) -> numr::error::Result<Candidate<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let [c0, c1, c2] = &circle.bands;
let [a0, a1, a2] = &arcs.bands;
let [w0, w1, w2] = &circle.widths;
let [v0, v1, v2] = &arcs.widths;
Ok(Candidate {
bands: [
pick_band(c, circular, c0, a0)?,
pick_band(c, circular, c1, a1)?,
pick_band(c, circular, c2, a2)?,
],
widths: [
pick(c, circular, w0, v0)?,
pick(c, circular, w1, v1)?,
pick(c, circular, w2, v2)?,
],
bed_width: pick(c, circular, &circle.bed_width, &arcs.bed_width)?,
widest_depth: pick(c, circular, &circle.widest_depth, &arcs.widest_depth)?,
widest_width: c.where_cond(circular, &circle.widest_width, &arcs.widest_width)?,
full_area: pick(c, circular, &circle.full_area, &arcs.full_area)?,
full_wall: pick(c, circular, &circle.full_wall, &arcs.full_wall)?,
full_moment: pick(c, circular, &circle.full_moment, &arcs.full_moment)?,
})
}
fn middles<R>(
c: &R::Client,
bands: &[ArcBand<R>; 3],
top: &Tensor<R>,
) -> numr::error::Result<[Tensor<R>; 3]>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let [bottom, corner, crown] = bands;
let half_top = c.mul_scalar(top, 0.5)?;
let middle = |start: &Bounds<R>, end: &Tensor<R>| -> numr::error::Result<Tensor<R>> {
let value = c.mul_scalar(&c.add(&start[1], end)?, 0.5)?;
let zero = c.mul_scalar(&value, 0.0)?;
c.where_cond(&c.gt(&value, &zero)?, &value, &half_top)
};
Ok([
middle(&bottom.start_depth, &corner.start_depth[0])?,
middle(&corner.start_depth, &crown.start_depth[0])?,
middle(&crown.start_depth, top)?,
])
}
fn circle_candidate<R>(
a: &mut Arithmetic<'_, R>,
shared: &Shared<'_, R>,
inputs: &FloorInputs<'_, R>,
) -> ArithmeticResult<Candidate<R>>
where
R: Runtime<DType = DType>,
R::Client: BarrelClient<R>,
{
let c = a.client();
let rows = c.mul(inputs.floored, inputs.circular)?;
let quarter = c.mul_scalar(inputs.rise, 0.25)?;
let three_quarters = c.mul_scalar(inputs.rise, 0.75)?;
let depth = c.where_cond(&rows, inputs.bed, &quarter)?;
let open = c.where_cond(&rows, inputs.top, &three_quarters)?;
let half_rise = c.mul_scalar(inputs.rise, 0.5)?;
let h = a.point(&depth)?;
let d_open = a.point(&open)?;
let radius = a.point(&half_rise)?;
let zero = a.point(&shared.zero)?;
let centre_y = a.sub(&radius, &h)?;
let low = a.sub(&zero, &h)?;
let diameter = a.point(inputs.rise)?;
let point = bed_point(
a,
shared,
&Circle {
centre_x: &zero,
centre_y: &radius,
radius: &radius,
low: &zero,
high: &diameter,
},
&h,
)?;
let BedPoint {
sin,
cos,
angle,
reach,
width,
} = point;
let reached = advance(
a,
shared,
&radius,
&Start {
sin: &sin,
cos: &cos,
reach: &reach,
width: &width,
area: &zero,
wall: &zero,
moment: &zero,
},
&End {
sin: &zero,
width: &zero,
rise: &d_open,
},
)?;
let band = ArcBand {
centre_x: ends(&zero),
centre_y: ends(¢re_y),
radius: ends(&radius),
start_angle: ends(&angle),
start_sin: ends(&sin),
start_cos: ends(&cos),
start_depth: ends(&zero),
start_area: ends(&zero),
start_perimeter: ends(&zero),
start_moment: ends(&zero),
circle_low: ends(&low),
circle_high: ends(&d_open),
};
let widest = nonnegative(a, shared, ¢re_y)?;
let below_centre = c.le(&depth, &half_rise)?;
let widest_width = c.where_cond(&below_centre, inputs.rise, width.upper())?;
let bed_width = ends(&width);
Ok(Candidate {
bands: [copy_band(&band), copy_band(&band), band],
widths: [bed_width.clone(), bed_width.clone(), bed_width.clone()],
bed_width,
widest_depth: ends(&widest),
widest_width,
full_area: ends(&reached.area),
full_wall: ends(&reached.wall),
full_moment: ends(&reached.moment),
})
}