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
//! Input configuration.

use palette::{Gradient, LinSrgba};
use serde::Deserialize;
use std::{collections::HashMap, path::PathBuf};

use crate::{
    dom::{Surface, SurfaceBuilder, Tree, TreeBuilder},
    geom::Mesh,
    parse::{json, wavefront},
    render::{Attribute, AttributeBuilder, GradientBuilder, Settings, Shader, ShaderBuilder},
    rt::{Camera, CameraBuilder},
};

/// Input configuration.
#[derive(Deserialize)]
pub struct Parameters {
    /// Path to the top level resource directory.
    resources_dir: PathBuf,
    /// Oct-tree settings.
    tree: TreeBuilder,
    /// Technical settings.
    settings: Settings,
    /// Aesthetic settings.
    shader: ShaderBuilder,
    /// Main camera.
    camera: CameraBuilder,
    /// Surfaces.
    surfaces: Vec<SurfaceBuilder>,
}

impl Parameters {
    /// Get the names of the `Gradient`s used.
    #[inline]
    #[must_use]
    pub fn used_gradient_names(&self) -> Vec<String> {
        let mut gradient_names = Vec::new();

        gradient_names.extend(self.shader.used_gradient_names());
        gradient_names.extend(&mut self.used_attribute_names().iter().flat_map(|n| {
            json::load::<AttributeBuilder>(
                &self
                    .resources_dir
                    .join("attributes")
                    .join(n)
                    .with_extension("json"),
            )
            .used_gradient_names()
        }));

        gradient_names.sort();
        gradient_names.dedup();

        gradient_names
    }

    /// Get the names of the `Attribute`s used.
    #[inline]
    #[must_use]
    pub fn used_attribute_names(&self) -> Vec<String> {
        let mut names = Vec::new();

        for surf in &self.surfaces {
            names.push(surf.1.clone());
        }

        names.sort();
        names.dedup();

        names
    }

    /// Get the names of the `Meshes`s used.
    #[inline]
    #[must_use]
    pub fn used_mesh_names(&self) -> Vec<String> {
        let mut names = Vec::new();

        for surf in &self.surfaces {
            names.push(surf.0.clone());
        }

        names.sort();
        names.dedup();

        names
    }

    /// Load the dictionary of `Gradients`.
    #[inline]
    #[must_use]
    pub fn load_gradients(&self) -> HashMap<String, Gradient<LinSrgba>> {
        let mut grads = HashMap::new();

        for name in self.used_gradient_names() {
            let grad = json::load::<GradientBuilder>(
                &self
                    .resources_dir
                    .join("gradients")
                    .join(name.clone())
                    .with_extension("json"),
            )
            .build();
            grads.insert(name, grad);
        }

        grads
    }

    /// Load the dictionary of `Attributes`.
    #[inline]
    #[must_use]
    pub fn load_attributes<'a>(
        &self,
        grads: &'a HashMap<String, Gradient<LinSrgba>>,
    ) -> HashMap<String, Attribute<'a>> {
        let mut attrs = HashMap::new();

        for name in self.used_attribute_names() {
            let attr = json::load::<AttributeBuilder>(
                &self
                    .resources_dir
                    .join("attributes")
                    .join(name.clone())
                    .with_extension("json"),
            )
            .build(grads);
            attrs.insert(name, attr);
        }

        attrs
    }

    /// Load the dictionary of `Meshes`.
    #[inline]
    #[must_use]
    pub fn load_meshes(&self) -> HashMap<String, Mesh> {
        let mut meshes = HashMap::new();

        for name in self.used_mesh_names() {
            let mesh = wavefront::load(
                &self
                    .resources_dir
                    .join("meshes")
                    .join(name.clone())
                    .with_extension("obj"),
            );
            meshes.insert(name, mesh);
        }

        meshes
    }

    /// Load the `Surface`s.
    #[inline]
    #[must_use]
    pub fn load_surfaces<'a>(
        &self,
        meshes: &HashMap<String, Mesh>,
        attributes: &'a HashMap<String, Attribute>,
    ) -> Vec<Surface<Attribute<'a>>> {
        self.surfaces
            .iter()
            .map(|s| s.clone().build(meshes, attributes))
            .collect()
    }

    /// Build the `Shader`.
    #[inline]
    #[must_use]
    pub fn build_shader<'a>(&self, grads: &'a HashMap<String, Gradient<LinSrgba>>) -> Shader<'a> {
        self.shader.build(grads)
    }

    /// Build the `Settings`.
    #[inline]
    #[must_use]
    pub fn build_settings(&self) -> Settings {
        self.settings.clone()
    }

    /// Build the `Camera`s.
    #[inline]
    #[must_use]
    pub fn build_camera(&self) -> Camera {
        self.camera.clone().build()
    }

    /// Build the `Tree`.
    #[inline]
    #[must_use]
    pub fn build_tree<'a, T>(&self, surfs: &'a [Surface<T>]) -> Tree<'a, T> {
        self.tree.build(surfs)
    }
}