2017-09-30 08:38:03 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::env;
|
|
|
|
use std::string::String;
|
|
|
|
|
|
|
|
/// Generate the name of the swizzle function and what it returns.
|
|
|
|
/// NOTE: This function assumes that variables are in ASCII format
|
2017-09-30 21:41:17 +00:00
|
|
|
fn gen_swizzle_nth<'a>(variables: &'a str, mut i: usize) -> Option<(String, String)> {
|
|
|
|
debug_assert!(i > 0); // zeroth permutation is empty
|
2017-09-30 08:38:03 +00:00
|
|
|
let mut swizzle_impl = String::new();
|
|
|
|
let mut swizzle = String::new();
|
2017-09-30 21:41:17 +00:00
|
|
|
let n = variables.len()+1;
|
|
|
|
for _ in 0..variables.len() {
|
|
|
|
if i == 0 { break; }
|
|
|
|
if i % n == 0 { return None; }
|
|
|
|
let c = variables.as_bytes()[i%n - 1] as char;
|
2017-09-30 08:38:03 +00:00
|
|
|
swizzle.push(c);
|
|
|
|
swizzle_impl.push_str(&format!("self.{}, ", c));
|
|
|
|
i = i/n;
|
|
|
|
}
|
2017-09-30 21:41:17 +00:00
|
|
|
Some((swizzle, swizzle_impl))
|
2017-09-30 08:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// NOTE: This function assumes that variables are in ASCII format
|
|
|
|
fn gen_swizzle_functions(variables: &'static str) -> String {
|
|
|
|
let n = variables.len();
|
|
|
|
let mut result = String::new();
|
2017-09-30 21:41:17 +00:00
|
|
|
let nn = (n+1).pow(n as u32);
|
|
|
|
for i in 1..nn {
|
|
|
|
if let Some((swizzle_name, swizzle_impl)) = gen_swizzle_nth(variables, i) {
|
|
|
|
let vec_type = format!("$vector_type{}", swizzle_name.len());
|
|
|
|
result.push_str(
|
|
|
|
&format!(" #[inline] pub fn {0}(&self) -> {2}<$S> {{ {2}::new({1}) }}\n",
|
|
|
|
swizzle_name, swizzle_impl, vec_type));
|
|
|
|
}
|
2017-09-30 08:38:03 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2017-09-30 21:41:17 +00:00
|
|
|
/// This script generates the macro for building swizzle operators for multidimensional
|
|
|
|
/// vectors and points. This macro is included in macros.rs
|
2017-09-30 08:38:03 +00:00
|
|
|
fn main() {
|
2017-09-30 21:41:17 +00:00
|
|
|
// save the file to output directory
|
2017-09-30 08:38:03 +00:00
|
|
|
let out_dir = env::var("OUT_DIR").unwrap();
|
|
|
|
let swizzle_file_path = Path::new(&out_dir).join("swizzle_operator_macro.rs");
|
|
|
|
|
2017-09-30 21:41:17 +00:00
|
|
|
// This is the string representing the generated macro
|
2017-09-30 08:38:03 +00:00
|
|
|
let data = format!(
|
|
|
|
"/// Generate glm/glsl style swizzle operators
|
|
|
|
macro_rules! impl_swizzle_functions {{
|
2017-09-30 21:41:17 +00:00
|
|
|
($vector_type1:ident, $S:ident, x) => {{
|
|
|
|
{x}
|
|
|
|
}};
|
|
|
|
($vector_type1:ident, $vector_type2:ident, $S:ident, xy) => {{
|
2017-09-30 08:38:03 +00:00
|
|
|
{xy}
|
|
|
|
}};
|
2017-09-30 21:41:17 +00:00
|
|
|
($vector_type1:ident, $vector_type2:ident, $vector_type3:ident, $S:ident, xyz) => {{
|
2017-09-30 08:38:03 +00:00
|
|
|
{xyz}
|
|
|
|
}};
|
2017-09-30 21:41:17 +00:00
|
|
|
($vector_type1:ident, $vector_type2:ident, $vector_type3:ident, $vector_type4:ident, $S:ident, xyzw) => {{
|
2017-09-30 08:38:03 +00:00
|
|
|
{xyzw}
|
|
|
|
}};
|
2017-09-30 21:41:17 +00:00
|
|
|
}}", x=gen_swizzle_functions("x"),
|
|
|
|
xy=gen_swizzle_functions("xy"),
|
2017-09-30 08:38:03 +00:00
|
|
|
xyz=gen_swizzle_functions("xyz"),
|
|
|
|
xyzw=gen_swizzle_functions("xyzw"));
|
|
|
|
let mut f = File::create(swizzle_file_path)
|
|
|
|
.expect("Unable to create file that defines the swizzle operator macro.");
|
|
|
|
f.write_all(data.as_bytes()).expect("Unable to write swizzle operator macro.");
|
|
|
|
}
|