optionstratlib_math/geometrics/construction/types.rs
1/******************************************************************************
2 Author: Joaquín Béjar García
3 Email: jb@taunais.com
4 Date: 26/8/24
5******************************************************************************/
6
7use rust_decimal::Decimal;
8use std::collections::BTreeSet;
9
10/// Parameters for constructing geometric objects in different dimensions.
11///
12/// This enum provides configuration options for different dimensional spaces
13/// when generating geometric objects through parametric equations or sampling.
14/// It supports both 2D curves (with a single parameter t) and 3D surfaces
15/// (with two parameters x and y).
16#[derive(Debug, Clone)]
17#[cfg_attr(feature = "schema", derive(utoipa::ToSchema))]
18pub enum ConstructionParams {
19 /// Parameters for constructing a 2D curve with a single parameter t.
20 D2 {
21 /// Starting value for the parameter t
22 t_start: Decimal,
23
24 /// Ending value for the parameter t
25 t_end: Decimal,
26
27 /// Number of sampling points between t_start and t_end
28 steps: usize,
29 },
30
31 /// Parameters for constructing a 3D surface with parameters x and y.
32 D3 {
33 /// Start parameter for x
34 x_start: Decimal,
35
36 /// End parameter for x
37 x_end: Decimal,
38
39 /// Start parameter for y
40 y_start: Decimal,
41
42 /// End parameter for y
43 y_end: Decimal,
44
45 /// Number of steps in x direction
46 x_steps: usize,
47
48 /// Number of steps in y direction
49 y_steps: usize,
50 },
51}
52
53/// Defines methods for constructing geometric objects.
54///
55/// This enum provides two primary approaches to creating geometric objects:
56/// - Direct construction from a set of predefined points
57/// - Parametric generation using a mathematical function
58///
59/// The generic parameters allow flexibility in the types of points and input parameters used:
60/// - `Point`: The type representing a coordinate in the geometric space
61/// - `Input`: The parameter type passed to parametric functions (typically `Decimal` for 2D or `(Decimal, Decimal)` for 3D)
62/// - `Error`: The error the parametric generator reports.
63/// [`crate::geometrics::GeometricObject::construct`] binds it to the
64/// object's own `Self::Error`, so a generator for a
65/// [`crate::curves::Curve`] returns [`crate::error::CurveError`] and one
66/// for a [`crate::surfaces::Surface`] returns
67/// [`crate::error::SurfaceError`]. The generator's own failure travels in
68/// that type's `Generator` variant, which keeps the original cause as its
69/// `source` rather than flattening it at the construction boundary.
70pub enum ConstructionMethod<Point, Input, Error> {
71 /// Construct a geometric object from an explicit set of points.
72 ///
73 /// This method uses a sorted collection of points to directly define
74 /// the geometry without requiring any computation.
75 FromData {
76 /// Ordered collection of points that define the geometric object
77 points: BTreeSet<Point>,
78 },
79
80 /// Construct a geometric object using a parametric function.
81 ///
82 /// This method generates points by evaluating a mathematical function
83 /// at specific parameter values defined by the construction parameters.
84 Parametric {
85 /// Function that maps from parameter space to point coordinates.
86 /// Must be thread-safe to support parallel computation.
87 ///
88 /// It reports the constructed object's own error type, so a failure
89 /// reaches the caller as the variant the generator raised instead of
90 /// being flattened into a string at the construction boundary.
91 f: Box<dyn Fn(Input) -> Result<Point, Error> + Send + Sync>,
92
93 /// Parameters defining the domain for sampling the parametric function
94 params: ConstructionParams,
95 },
96}