Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use super::metadata::{StructMeta};
- fn impl_new_function(s: &StructMeta) -> quote::Tokens {
- let initializers = s.fields.iter().map(|field| {
- match field.kind.as_ref() {
- "Vector4" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector4::new(0.0, 0.0, 0.0, 0.0)
- }
- },
- "Vector3" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector3::new(0.0, 0.0, 0.0)
- }
- },
- "Vector2" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector2::new(0.0, 0.0)
- }
- },
- _ => panic!(format!("Cannot interpolate type '{}'", field.kind))
- }
- });
- let name = quote::Ident::from(s.name.clone());
- quote!(
- #[inline(always)]
- fn new() -> #name {
- #name {
- #( #initializers ),*
- }
- }
- )
- }
- fn impl_correct_function(s: &StructMeta) -> quote::Tokens {
- let initializers = s.fields.iter().map(|field| {
- match field.kind.as_ref() {
- "Vector4" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector4::new(
- v.#field_name.x / w,
- v.#field_name.y / w,
- v.#field_name.z / w,
- v.#field_name.w / w,
- )
- }
- },
- "Vector3" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector3::new(
- v.#field_name.x / w,
- v.#field_name.y / w,
- v.#field_name.z / w,
- )
- }
- },
- "Vector2" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector2::new(
- v.#field_name.x / w,
- v.#field_name.y / w,
- )
- }
- },
- _ => panic!(format!("Cannot interpolate type '{}'", field.kind))
- }
- });
- let name = quote::Ident::from(s.name.clone());
- quote!(
- #[inline(always)]
- fn correct(v: &#name, w: &f32) -> #name {
- #name {
- #( #initializers ),*
- }
- }
- )
- }
- fn impl_interpolate_function(s: &StructMeta) -> quote::Tokens {
- let initializers = s.fields.iter().map(|field| {
- match field.kind.as_ref() {
- "Vector4" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector4::new(
- ((w0 * v0.#field_name.x) + (w1 * v1.#field_name.x) + (w2 * v2.#field_name.x)) / w,
- ((w0 * v0.#field_name.y) + (w1 * v1.#field_name.y) + (w2 * v2.#field_name.y)) / w,
- ((w0 * v0.#field_name.z) + (w1 * v1.#field_name.z) + (w2 * v2.#field_name.z)) / w,
- ((w0 * v0.#field_name.w) + (w1 * v1.#field_name.w) + (w2 * v2.#field_name.w)) / w,
- )
- }
- },
- "Vector3" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector3::new(
- ((w0 * v0.#field_name.x) + (w1 * v1.#field_name.x) + (w2 * v2.#field_name.x)) / w,
- ((w0 * v0.#field_name.y) + (w1 * v1.#field_name.y) + (w2 * v2.#field_name.y)) / w,
- ((w0 * v0.#field_name.z) + (w1 * v1.#field_name.z) + (w2 * v2.#field_name.z)) / w,
- )
- }
- },
- "Vector2" => {
- let field_name = quote::Ident::from(field.name.clone());
- quote! {
- #field_name: Vector2::new(
- ((w0 * v0.#field_name.x) + (w1 * v1.#field_name.x) + (w2 * v2.#field_name.x)) / w,
- ((w0 * v0.#field_name.y) + (w1 * v1.#field_name.y) + (w2 * v2.#field_name.y)) / w,
- )
- }
- },
- _ => panic!(format!("Cannot interpolate type '{}'", field.kind))
- }
- });
- let name = quote::Ident::from(s.name.clone());
- quote!(
- #[inline(always)]
- fn interpolate(v0: &#name, v1: &#name, v2: &#name, w0: &f32, w1: &f32, w2: &f32, w: &f32) -> #name {
- #name {
- #( #initializers ),*
- }
- }
- )
- }
- pub fn impl_interpolate(s: &StructMeta) -> quote::Tokens {
- let kind = quote::Ident::from(s.name.clone());
- let new_function = impl_new_function(&s);
- let correct_function = impl_correct_function(&s);
- let interpolate_function = impl_interpolate_function(&s);
- let implementation = quote! {
- impl Interpolate for #kind {
- #new_function
- #correct_function
- #interpolate_function
- }
- };
- // println!("{}", implementation);
- implementation
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement