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
|
use std::{path::PathBuf, str::FromStr};
use config::File;
use serde::Deserialize;
use structopt::StructOpt;
use crate::{error::TracerError, vec3::Vec3};
#[derive(StructOpt, Debug)]
#[structopt(name = "racer-tracer")]
pub struct Args {
#[structopt(
short = "c",
long = "config",
default_value = "./config.yml",
env = "CONFIG"
)]
pub config: String,
#[structopt(short = "s", long = "scene")]
pub scene: Option<String>,
#[structopt(long = "image-action")]
pub image_action: Option<ImageAction>,
}
impl TryFrom<Args> for Config {
type Error = TracerError;
fn try_from(args: Args) -> Result<Self, TracerError> {
Config::from_file(args.config).and_then(|mut cfg| {
if let Some(image_action) = args.image_action {
cfg.image_action = image_action;
}
if let Some(scene) = args.scene {
if scene == "random" {
cfg.loader = SceneLoader::Random;
} else {
let path = PathBuf::from(scene);
cfg.loader = path
.extension()
.map(|s| s.to_string_lossy())
.ok_or_else(|| {
TracerError::ArgumentParsingError(format!(
"Could not get extension from scene file: {}",
path.display()
))
})
.and_then(|p| match p.as_ref() {
"yml" => Ok(SceneLoader::Yml { path: path.clone() }),
_ => Err(TracerError::ArgumentParsingError(format!(
"Could not find a suitable scene loader for file: {}",
path.display()
))),
})?;
};
}
Ok(cfg)
})
}
}
#[derive(Default, Clone, Debug, Deserialize)]
pub struct Screen {
pub height: usize,
pub width: usize,
}
#[derive(Default, Clone, Debug, Deserialize)]
pub struct RenderConfigData {
pub samples: usize,
pub max_depth: usize,
pub num_threads_width: usize,
pub num_threads_height: usize,
pub scale: usize,
}
#[derive(Clone, Debug, Deserialize)]
pub struct CameraConfig {
#[serde(default = "CameraConfig::default_vfov")]
pub vfov: f64,
#[serde(default = "CameraConfig::default_aperture")]
pub aperture: f64,
#[serde(default = "CameraConfig::default_focus_distance")]
pub focus_distance: f64,
#[serde(default = "CameraConfig::default_pos")]
pub pos: Vec3,
#[serde(default)]
pub look_at: Vec3,
}
impl CameraConfig {
fn default_vfov() -> f64 {
20.0
}
fn default_aperture() -> f64 {
0.1
}
fn default_focus_distance() -> f64 {
10.0
}
fn default_pos() -> Vec3 {
Vec3::new(0.0, 2.0, 10.0)
}
}
impl Default for CameraConfig {
fn default() -> Self {
Self {
vfov: CameraConfig::default_vfov(),
aperture: CameraConfig::default_aperture(),
focus_distance: CameraConfig::default_focus_distance(),
pos: CameraConfig::default_pos(),
look_at: Vec3::default(),
}
}
}
#[derive(StructOpt, Debug, Clone, Deserialize, Default)]
pub enum SceneLoader {
#[default]
None,
Yml {
path: PathBuf,
},
Random,
}
#[derive(StructOpt, Debug, Clone, Deserialize, Default)]
pub enum ImageAction {
#[default]
WaitForSignal,
SavePng,
}
#[derive(StructOpt, Debug, Clone, Deserialize, Default)]
pub enum ConfigSceneController {
#[default]
Interactive,
}
#[derive(StructOpt, Debug, Clone, Deserialize, Default)]
pub enum Renderer {
#[default]
Cpu,
CpuPreview,
}
fn default_preview() -> Renderer {
Renderer::CpuPreview
}
impl FromStr for ImageAction {
type Err = TracerError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"png" => Ok(ImageAction::SavePng),
"show" => Ok(ImageAction::WaitForSignal),
_ => Ok(ImageAction::WaitForSignal),
}
}
}
#[derive(Default, Clone, Debug, Deserialize)]
pub struct Config {
#[serde(default)]
pub preview: RenderConfigData,
#[serde(default)]
pub render: RenderConfigData,
#[serde(default)]
pub screen: Screen,
#[serde(default)]
pub loader: SceneLoader,
#[serde(default)]
pub image_action: ImageAction,
#[serde(default)]
pub image_output_dir: Option<PathBuf>,
#[serde(default)]
pub scene_controller: ConfigSceneController,
#[serde(default)]
pub renderer: Renderer,
#[serde(default = "default_preview")]
pub preview_renderer: Renderer,
#[serde(default)]
pub camera: CameraConfig,
}
impl Config {
pub fn from_file(file: String) -> Result<Self, TracerError> {
config::Config::builder()
.add_source(File::from(file.as_ref()))
.build()
.map_err(|e| TracerError::Configuration(file.clone(), e.to_string()))?
.try_deserialize()
.map_err(|e| TracerError::Configuration(file, e.to_string()))
}
}
|