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
//! Rhai bindings to Fidget
//!
//! The [`engine`] function lets you construct a [`rhai::Engine`] with
//! Fidget-specific bindings. The rest of this documentation explains the
//! behavior of those bindings; for low-level details, see the [`engine`]
//! docstring.
//!
//! # Introduction
//! Rhai is a general-purpose scripting language embedded in Rust. When used
//! for Fidget scripting, we use the Rhai script to capture a math expression.
//! This math expression can then be processed by Fidget's optimized evaluators.
//!
//! It's important to distinguish between the Rhai script and the target math
//! expression:
//!
//! - The Rhai script is general-purpose, evaluated a single time to compute
//! math expressions, and supports language features like conditionals and
//! loops
//! - The math expression is closed-form arithmetic, evaluated _many_ times
//! over the course of rendering, and only supports operations from
//! [`TreeOp`](fidget_core::context::TreeOp)
//!
//! The purpose of evaluating the Rhai script is to capture a _trace_ of a math
//! expression. Evaluating `x + y` in a Rhai script does not actually do any
//! arithmetic; instead, it creates a math expression `Add(Var::X, Var::Y)`.
//!
//! Operator overloading makes this ergonomic, but can mask the fact that the
//! Rhai script is not the math expression!
//!
//! # Trees
//! The basic type for math expressions is a `Tree`, which is equivalent to
//! [`fidget_core::context::Tree`]. Trees are typically built from `(x, y, z)`
//! primitives, which can be constructed with the `axes()` function:
//!
//! ```
//! # fidget_rhai::engine().run("
//! let xyz = axes();
//! xyz.x + xyz.y
//! # ").unwrap();
//! ```
//!
//! `x, y, z` variables are also automatically injected into the `Engine`'s
//! context before evaluation.
//!
//! # Mathematical constants
//! The Rhai context includes common mathematical constants that can be used
//! in expressions:
//!
//! ```
//! # fidget_rhai::engine().run("
//! // Use PI for angular calculations
//! let angle = PI / 4.0;
//! let radius = 2.0;
//! circle(#{ center: [radius * cos(angle), radius * sin(angle)], radius: 1.0 })
//! # ").unwrap();
//! ```
//!
//! Available constants include:
//! - `PI` - π (3.14159...)
//! - `E` - Euler's number (2.71828...)
//! - `TAU` - 2π (6.28318...)
//! - `PHI` / `GOLDEN_RATIO` - Golden ratio (1.61803...)
//! - `SQRT_2`, `SQRT_3` - Square roots
//! - `FRAC_PI_2`, `FRAC_PI_4`, etc. - Fractions of π
//! - `LN_2`, `LN_10` - Natural logarithms
//! - And many more mathematical constants
//!
//! # Vector types
//! The Rhai context includes `vec2`, `vec3`, and `vec4` types, which are
//! roughly analogous to their GLSL equivalents (for floating-point only).
//! Many (but not all) functions are implemented and overloaded for these types;
//! if you encounter something missing, feel free to open an issue.
//!
//! # Shapes
//! In Rhai scripts, shapes can be constructed using object map notation:
//! ```
//! # fidget_rhai::engine().run("
//! circle(#{ center: vec2(1.0, 2.0), radius: 3.0 })
//! # ").unwrap();
//! ```
//!
//! This works for any object type; in addition, there are a bunch of ergonomic
//! improvements on top of this low-level syntax.
//!
//! ## Type coercions
//! Shapes are built from a set of Rust primitives, with generous conversions
//! from Rhai's native types:
//!
//! - Scalar values (`f64`)
//! - Both floating-point and integer Rhai values will be accepted
//! - Vectors (`vec2` and `vec3`)
//! - These may be explicitly constructed with `vec2(x, y)` and
//! `vec3(x, y, z)`
//! - Appropriately-sized arrays of numbers will be automatically converted
//! - A `vec2` (or something convertible into a `vec2`) will be converted
//! into a `vec3` with a default `z` value. This default value is
//! shape-specific, e.g. it will be 0 for a position and 1 for a scale.
//! ```
//! # fidget_rhai::engine().run("
//! // array -> vec2
//! let c = circle(#{ center: [1, 2], radius: 3 });
//!
//! // array -> vec3
//! let s = sphere(#{ center: [1, 2, 4], radius: 3 });
//!
//! // array -> vec2 -> vec3
//! move(#{ shape: c, offset: [1, 1] });
//! # ").unwrap();
//! ```
//!
//! ## Default values
//! Many shape fields have sensibly-defined default values; these are usually
//! either 0 or 1 (or the equivalent `VecX` values). Fields with default values
//! may be omitted from the map:
//!
//! ```
//! # fidget_rhai::engine().run("
//! let c = circle(#{ center: [1, 2] }); // radius = 1
//! let s = sphere(#{ radius: 3 }); // center = [0, 0, 0]
//! # ").unwrap();
//! ```
//!
//! ## Uniquely typed functions
//! Any shape with unique arguments may skip the object map and pass arguments
//! directly; order doesn't matter, because the type is unambiguous.
//!
//! ```
//! # fidget_rhai::engine().run("
//! // array -> vec2
//! let c1 = circle([1, 2], 3);
//! let c2 = circle(3, [1, 2]); // order doesn't matter!
//! # ").unwrap();
//! ```
//!
//! In addition, fields with default values may be skipped:
//!
//! ```
//! # fidget_rhai::engine().run("
//! // array -> vec2
//! let c1 = circle([1, 2]); // radius = 1
//! let c2 = circle(); // center = [0, 0], radius = 1
//! # ").unwrap();
//! ```
//!
//! `vec2 -> vec3` coercion also works in this regime, if the `vec3` has a
//! default value:
//!
//! ```
//! # fidget_rhai::engine().run("
//! // array -> vec2 -> vec3
//! let c1 = sphere([1, 1], 4); // z = 0
//! # ").unwrap();
//! ```
//!
//! ## Function chaining
//! Shapes with a single initial `Tree` member are typically transforms (e.g.
//! `move` from above). These functions may be called with the tree as their
//! first (unnamed) argument, followed by an object map of remaining parameters.
//!
//! ```
//! # fidget_rhai::engine().run("
//! let c = circle(#{ center: [1, 2], radius: 3 });
//! move(c, #{ offset: [1, 1] });
//! # ").unwrap();
//! ```
//!
//! Given Rhai's dispatch strategy, this can also be written as a function
//! chain, which is more ergonomic for a string of transforms:
//!
//! ```
//! # fidget_rhai::engine().run("
//! circle(#{ center: [1, 2], radius: 3 })
//! .move(#{ offset: [1, 1] });
//! # ").unwrap();
//! ```
//!
//! A transform which only take a single argument may skip the object map:
//!
//! ```
//! # fidget_rhai::engine().run("
//! circle(#{ center: [1, 2], radius: 3 })
//! .move([1, 1]);
//! # ").unwrap();
//! ```
//!
//! ## Functions of two trees
//! Shapes which take two trees can be called with two (unnamed) arguments:
//!
//! ```
//! # fidget_rhai::engine().run("
//! let a = circle(#{ center: [0, 0], radius: 1 });
//! let b = circle(#{ center: [1, 0], radius: 0.5 });
//! difference(a, b);
//! # ").unwrap();
//! ```
//!
//! ## Tree reduction functions
//! Any function which takes a single `Vec<Tree>` will accept both an array of
//! trees or individual tree arguments (up to an 8-tuple).
//!
//! ```
//! # fidget_rhai::engine().run("
//! let a = circle(#{ center: [1, 1], radius: 3 });
//! let b = circle(#{ center: [2, 2], radius: 3 });
//! let c = circle(#{ center: [3, 3], radius: 3 });
//! union([a, b, c]);
//! union(a, b, c);
//! union(a, b, c, a, b, c, a, b);
//! # ").unwrap();
//! ```
//!
//! ## Automatic tree reduction
//! Any shape which takes a `Tree` will also accept an array of trees, which are
//! automatically reduced with a union operation.
//!
//! ```
//! # fidget_rhai::engine().run("
//! [
//! circle(#{ center: [0, 0], radius: 3 }),
//! circle(#{ center: [2, 2], radius: 3 }),
//! ]
//! .move(#{ offset: [1, 1] });
//! # ").unwrap();
//! ```
use Tree;
/// Build a new engine with Fidget-specific bindings and settings
///
/// - Mathematical constants (PI, E, TAU, etc.), provided by the [`resolver`] function
/// - `Tree`-specific type, overloads, and `axes()` ([`tree::register`])
/// - Custom types (e.g. GLSL-style vectors), provided by [`types::register`]
/// - Shapes and transforms ([`shapes::register`])
/// - An `on_progress` limit of 50,000 steps (chosen arbitrarily)
/// - Max expression and function expression depths of 64 and 32 (also chosen
/// arbitrarily)
/// - [`set_fail_on_invalid_map_property`](rhai::Engine::set_fail_on_invalid_map_property)
/// set to `true`, so that missing map items raise an error
/// - A custom resolver ([`resolver`]) which provides fallbacks for `x`, `y`,
/// `z`, and mathematical constants (if not defined)
/// Variable resolver which provides `x`, `y`, `z` and mathematical constants if not found
////////////////////////////////////////////////////////////////////////////////
/// Helper trait to go from a Rhai dynamic object to a particular type
////////////////////////////////////////////////////////////////////////////////