mln-rs-expression 0.0.15

Part of mln-rs, a Rust port of the MapLibre Native map rendering core. Depend on mln-rs instead.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use feature::Value as MbglValue;
use mln_conversion::Convertible;
use mln_style_types::VariableAnchorOffsetCollection;
use mln_util::{Color, ColorSpace, Padding};

use crate::expression::{EvalResult, EvaluationContext, EvaluationError, EvaluationResult, Expr, Node};
use crate::interpolator::{CubicBezierInterpolator, ExponentialInterpolator, Interpolator};
use crate::parsing_context::{ParseResult, ParsingContext};
use crate::range::Range;
use crate::stops::{Stops, get_covering_stops};
use crate::type_::{self, Type};
use crate::value::{Value, type_of};

/// The `T` of `InterpolateImpl<T>`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, mln_wire::Wire)]
enum InterpolateOutput {
    Number,
    Color,
    Padding,
    ProjectionDefinition,
    VariableAnchorOffsetCollection,
    Array,
}

impl InterpolateOutput {
    fn expression_type(self) -> Type {
        match self {
            InterpolateOutput::Number => Type::Number,
            InterpolateOutput::Color => Type::Color,
            InterpolateOutput::Padding => Type::Padding,
            InterpolateOutput::ProjectionDefinition => Type::ProjectionDefinition,
            InterpolateOutput::VariableAnchorOffsetCollection => Type::VariableAnchorOffsetCollection,
            InterpolateOutput::Array => Type::array(Type::Value),
        }
    }

    fn holds(self, v: &Value) -> bool {
        matches!(
            (self, v),
            (InterpolateOutput::Number, Value::Number(_))
                | (InterpolateOutput::Color, Value::Color(_))
                | (InterpolateOutput::Padding, Value::Padding(_))
                | (InterpolateOutput::ProjectionDefinition, Value::ProjectionDefinition(_))
                | (InterpolateOutput::VariableAnchorOffsetCollection, Value::VariableAnchorOffsetCollection(_))
                | (InterpolateOutput::Array, Value::Array(_))
        )
    }
}

fn interpolate_float(a: f32, b: f32, t: f64) -> f32 {
    (f64::from(a) * (1.0 - t) + f64::from(b) * t) as f32
}

/// The generic `Interpolator<double>`.
fn interpolate_double(a: f64, b: f64, t: f64) -> f64 {
    a * (1.0 - t) + b * t
}

/// `["interpolate", interpolation, input, label, output, ...]` and its color
/// space variants.
#[derive(Clone, PartialEq, mln_wire::Wire)]
pub struct Interpolate {
    type_: Type,
    interpolator: Interpolator,
    color_space: ColorSpace,
    input: Expr,
    stops: Stops,
    output: InterpolateOutput,
}

/// The stops an input falls between.
enum Located<'a> {
    At(&'a Expr),
    Between(&'a Expr, &'a Expr, f64),
}

impl Interpolate {
    pub(crate) fn get_type(&self) -> &Type {
        &self.type_
    }

    #[must_use]
    pub(crate) fn get_input(&self) -> &Expr {
        &self.input
    }

    #[must_use]
    pub fn get_stop_count(&self) -> usize {
        self.stops.len()
    }

    pub fn each_stop(&self, mut visit: impl FnMut(f64, &Expr)) {
        for (k, v) in self.stops.iter() {
            visit(k, v);
        }
    }

    /// Return the smallest range of stops that covers the interval
    /// [lower, upper].
    #[must_use]
    pub fn get_covering_stops(&self, lower: f64, upper: f64) -> Range<f32> {
        get_covering_stops(&self.stops, lower, upper)
    }

    #[must_use]
    pub fn interpolation_factor(&self, input_levels: Range<f64>, input_value: f64) -> f64 {
        self.interpolator.interpolation_factor(input_levels, input_value)
    }

    pub(crate) fn get_operator(&self) -> &'static str {
        match self.color_space {
            ColorSpace::Lab => "interpolate-lab",
            ColorSpace::Hcl => "interpolate-hcl",
            ColorSpace::Oklch => "interpolate-oklch",
            ColorSpace::Oklab => "interpolate-oklab",
            ColorSpace::Rgb => "interpolate",
        }
    }

    pub(crate) fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a Expr)) {
        visit(&self.input);
        self.stops.values().for_each(visit);
    }

    /// The stop outputs the input selects, and how far it is between them.
    fn locate(&self, params: &EvaluationContext<'_>) -> EvalResult<Located<'_>> {
        let x = self.input.eval_num(params)? as f32;
        if x.is_nan() {
            return Err(EvaluationError::new("Input is not a number."));
        }

        if self.stops.is_empty() {
            return Err(EvaluationError::new("No stops in exponential curve."));
        }

        let it = self.stops.upper_bound(f64::from(x));
        if it == self.stops.len() {
            return Ok(Located::At(self.stops.output(self.stops.len() - 1)));
        }
        if it == 0 {
            return Ok(Located::At(self.stops.output(0)));
        }
        let t = self.interpolation_factor(Range::new(self.stops.key(it - 1), self.stops.key(it)), f64::from(x));
        let (lower, upper) = (self.stops.output(it - 1), self.stops.output(it));
        Ok(if t == 0.0 {
            Located::At(lower)
        } else if t == 1.0 {
            Located::At(upper)
        } else {
            Located::Between(lower, upper, t)
        })
    }

    pub(crate) fn eval_num(&self, params: &EvaluationContext<'_>) -> EvalResult<f64> {
        match self.locate(params)? {
            Located::At(output) => output.eval_num(params),
            Located::Between(lower, upper, t) => {
                Ok(interpolate_double(lower.eval_num(params)?, upper.eval_num(params)?, t))
            }
        }
    }

    pub(crate) fn eval_color(&self, params: &EvaluationContext<'_>) -> EvalResult<Color> {
        match self.locate(params)? {
            Located::At(output) => output.eval_color(params),
            Located::Between(lower, upper, t) => {
                let (lower, upper) = (lower.eval_color(params)?, upper.eval_color(params)?);
                Ok(Color::interpolate(&lower, &upper, t, self.color_space))
            }
        }
    }

    pub(crate) fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
        let (lower_expr, upper_expr, t) = match self.locate(params)? {
            Located::At(output) => return output.evaluate(params),
            Located::Between(lower, upper, t) => (lower, upper, t),
        };
        let lower = lower_expr.evaluate(params)?;
        let upper = upper_expr.evaluate(params)?;

        if !self.output.holds(&lower) {
            return Err(EvaluationError::new(format!(
                "Expected value to be of type {}, but found {} instead.",
                type_::to_string(&self.output.expression_type()),
                type_::to_string(&type_of(&lower))
            )));
        }

        if !self.output.holds(&upper) {
            return Err(EvaluationError::new(format!(
                "Expected value to be of type {}, but found {} instead.",
                type_::to_string(&self.output.expression_type()),
                type_::to_string(&type_of(&upper))
            )));
        }
        self.interpolate_outputs(&lower, &upper, t)
    }

    fn interpolate_outputs(&self, lower: &Value, upper: &Value, t: f64) -> EvaluationResult {
        Ok(match (lower, upper) {
            (Value::Number(a), Value::Number(b)) => Value::Number(interpolate_double(*a, *b, t)),
            (Value::Color(a), Value::Color(b)) => Value::Color(Color::interpolate(a, b, t, self.color_space)),
            (Value::Padding(a), Value::Padding(b)) => Value::Padding(Padding::new(
                interpolate_float(a.top, b.top, t),
                interpolate_float(a.right, b.right, t),
                interpolate_float(a.bottom, b.bottom, t),
                interpolate_float(a.left, b.left, t),
            )),
            (Value::ProjectionDefinition(a), Value::ProjectionDefinition(b)) => {
                Value::ProjectionDefinition(mln_util::interpolate::interpolate(a, b, t))
            }
            (Value::VariableAnchorOffsetCollection(a), Value::VariableAnchorOffsetCollection(b)) => {
                // Interpolator<VariableAnchorOffsetCollection> only has a
                // float overload. It throws std::runtime_error on mismatched
                // collections, which propagates out of evaluate() in C++.
                // Here it is an evaluation error.
                match VariableAnchorOffsetCollection::interpolate(a, b, t as f32) {
                    Ok(v) => Value::VariableAnchorOffsetCollection(v),
                    Err(e) => return Err(EvaluationError::new(e)),
                }
            }
            (Value::Array(a), Value::Array(b)) => {
                // Interpolator<std::vector<Value>>
                debug_assert_eq!(a.len(), b.len());
                if a.is_empty() {
                    return Ok(Value::Array(Vec::new()));
                }
                let mut result = Vec::with_capacity(a.len());
                for (ai, bi) in a.iter().zip(b) {
                    let (Value::Number(ai), Value::Number(bi)) = (ai, bi) else {
                        let item = if matches!(ai, Value::Number(_)) { bi } else { ai };
                        return Err(EvaluationError::type_mismatch(&Type::Number, item));
                    };
                    result.push(Value::Number(interpolate_double(*ai, *bi, t)));
                }
                Value::Array(result)
            }
            _ => return Err(EvaluationError::type_mismatch(&type_of(lower), upper)),
        })
    }

    pub(crate) fn serialize(&self) -> MbglValue {
        let mut serialized = vec![MbglValue::String(self.get_operator().to_owned())];

        match &self.interpolator {
            Interpolator::Exponential(exponential) => {
                if exponential.base == 1.0 {
                    serialized.push(MbglValue::Array(vec![MbglValue::String("linear".to_owned())]));
                } else {
                    serialized.push(MbglValue::Array(vec![
                        MbglValue::String("exponential".to_owned()),
                        MbglValue::Double(exponential.base),
                    ]));
                }
            }
            Interpolator::CubicBezier(cubic_bezier) => {
                // `ub.getP1()`, `ub.getP2()` in C++ (see `control_points`).
                let [x1, y1, x2, y2] = cubic_bezier.control_points();
                serialized.push(MbglValue::Array(vec![
                    MbglValue::String("cubic-bezier".to_owned()),
                    MbglValue::Double(x1),
                    MbglValue::Double(y1),
                    MbglValue::Double(x2),
                    MbglValue::Double(y2),
                ]));
            }
        }
        serialized.push(self.input.serialize());
        for (k, v) in self.stops.iter() {
            serialized.push(MbglValue::Double(k));
            serialized.push(v.serialize());
        }
        MbglValue::Array(serialized)
    }
}

/// The label of an interpolate/step stop (`toValue` of a JSON number).
pub(crate) fn parse_stop_label(value: &Convertible<'_>) -> Option<f64> {
    value.to_double()
}

pub(crate) fn parse_interpolate(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
    debug_assert!(value.is_array());

    let length = value.array_length();

    if length < 2 {
        ctx.error("Expected an interpolation type expression.".to_owned());
        return None;
    }

    let interp = value.array_member(1);
    if !interp.is_array() || interp.array_length() == 0 {
        ctx.error("Expected an interpolation type expression.".to_owned());
        return None;
    }

    let op = value.array_member(0).to_string();
    let color_space = match op.as_deref() {
        Some("interpolate-lab") => ColorSpace::Lab,
        Some("interpolate-hcl") => ColorSpace::Hcl,
        Some("interpolate-oklch") => ColorSpace::Oklch,
        Some("interpolate-oklab") => ColorSpace::Oklab,
        _ => ColorSpace::Rgb,
    };

    let mut interpolator: Option<Interpolator> = None;

    let interp_name = interp.array_member(0).to_string();
    if interp_name.as_deref() == Some("linear") {
        interpolator = Some(ExponentialInterpolator::new(1.0).into());
    } else if interp_name.as_deref() == Some("exponential") {
        let mut base = None;
        if interp.array_length() == 2 {
            base = interp.array_member(1).to_double();
        }
        let Some(base) = base else {
            ctx.error_at2("Exponential interpolation requires a numeric base.".to_owned(), 1, 1);
            return None;
        };
        interpolator = Some(ExponentialInterpolator::new(base).into());
    } else if interp_name.as_deref() == Some("cubic-bezier") {
        let (mut x1, mut y1, mut x2, mut y2) = (None, None, None, None);
        if interp.array_length() == 5 {
            x1 = interp.array_member(1).to_double();
            y1 = interp.array_member(2).to_double();
            x2 = interp.array_member(3).to_double();
            y2 = interp.array_member(4).to_double();
        }
        let in_unit = |v: Option<f64>| v.filter(|&v| !(v < 0.0 || v > 1.0));
        let (Some(x1), Some(y1), Some(x2), Some(y2)) = (in_unit(x1), in_unit(y1), in_unit(x2), in_unit(y2)) else {
            ctx.error_at(
                "Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.".to_owned(),
                1,
            );
            return None;
        };
        interpolator = Some(CubicBezierInterpolator::new(x1, y1, x2, y2).into());
    }

    let Some(interpolator) = interpolator else {
        ctx.error_at2(format!("Unknown interpolation type {}", interp_name.as_deref().unwrap_or("")), 1, 0);
        return None;
    };

    let min_args = 4;
    if length - 1 < min_args {
        ctx.error(format!("Expected at least 4 arguments, but found only {}.", length - 1));
        return None;
    }

    // [interpolation, interp_type, input, 2 * (n pairs)...]
    if !(length - 1).is_multiple_of(2) {
        ctx.error("Expected an even number of arguments.".to_owned());
        return None;
    }

    let input = ctx.parse(&value.array_member(2), 2, Some(Type::Number), None)?;

    let mut stops = Stops::new();
    let mut output_type: Option<Type> = None;
    if color_space != ColorSpace::Rgb {
        output_type = Some(Type::Color);
    } else if let Some(expected) = ctx.get_expected()
        && expected != Type::Value
    {
        output_type = Some(expected);
    }

    let mut previous = f64::NEG_INFINITY;

    let mut i = 3;
    while i + 1 < length {
        let Some(label) = parse_stop_label(&value.array_member(i)) else {
            ctx.error_at(
                r#"Input/output pairs for "interpolate" expressions must be defined using literal numeric values (not computed expressions) for the input values."#
                    .to_owned(),
                i,
            );
            return None;
        };

        if label <= previous {
            ctx.error_at(
                r#"Input/output pairs for "interpolate" expressions must be arranged with input values in strictly ascending order."#
                    .to_owned(),
                i,
            );
            return None;
        }
        previous = label;

        let output = ctx.parse(&value.array_member(i + 1), i + 1, output_type.clone(), None)?;
        if output_type.is_none() {
            output_type = Some(output.get_type());
        }

        stops.emplace(label, output);
        i += 2;
    }

    let Some(output_type) = output_type else {
        ctx.error(format!("Expected at least 4 arguments, but found only {}.", length - 1));
        return None;
    };

    create_interpolate(output_type, interpolator, color_space, input, stops, ctx)
}

pub(crate) fn create_interpolate(
    type_: Type,
    interpolator: Interpolator,
    color_space: ColorSpace,
    input: Expr,
    stops: Stops,
    ctx: &mut ParsingContext,
) -> ParseResult {
    debug_assert!(color_space == ColorSpace::Rgb || type_ == Type::Color);
    let output = match &type_ {
        Type::Number => InterpolateOutput::Number,
        Type::Color => InterpolateOutput::Color,
        Type::Padding => InterpolateOutput::Padding,
        Type::ProjectionDefinition => InterpolateOutput::ProjectionDefinition,
        Type::VariableAnchorOffsetCollection => InterpolateOutput::VariableAnchorOffsetCollection,
        Type::Array(array_type) => {
            if array_type.item_type != Type::Number || array_type.n.is_none() {
                ctx.error(format!("Type {} is not interpolatable.", type_::to_string(&type_)));
                return None;
            }
            InterpolateOutput::Array
        }
        _ => {
            ctx.error(format!("Type {} is not interpolatable.", type_::to_string(&type_)));
            return None;
        }
    };
    assert_eq!(input.get_type(), Type::Number);
    Some(Expr::new(Node::Interpolate(Box::new(Interpolate { type_, interpolator, color_space, input, stops, output }))))
}