Changeset 3376ec9 for libcfa


Ignore:
Timestamp:
Dec 11, 2019, 4:13:29 PM (4 years ago)
Author:
Dmitry Kobets <dkobets@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
aca6a54c
Parents:
2444324
Message:

make vector an interface, allowing for shared code between dimensions

Location:
libcfa/src/vec
Files:
1 added
2 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/vec/vec2.hfa

    r2444324 r3376ec9  
    11#pragma once
    2 #include <math.hfa>
    3 #include <iostream.hfa>
    42
    5 trait zeroassn(otype T) {
    6     T ?=?(T&, zero_t);
    7 };
    8 trait fromint(otype T) {
    9     void ?{}(T&, int);
    10 };
    11 trait zero_assign(otype T) {
    12     T ?=?(T&, zero_t);
    13 };
    14 trait subtract(otype T) {
    15     T ?-?(T, T);
    16     T -?(T);
    17 };
    18 trait add(otype T) {
    19     T ?+?(T, T);
    20 };
    21 trait multiply(otype T) {
    22     T ?*?(T, T);
    23 };
    24 trait divide(otype T) {
    25     T ?/?(T, T);
    26 };
    27 trait lessthan(otype T) {
    28     int ?<?(T, T);
    29 };
    30 trait equality(otype T) {
    31     int ?==?(T, T);
    32 };
    33 trait sqrt(otype T) {
    34     T sqrt(T);
    35 };
     3#include "vec.hfa"
    364
    37 static inline {
    38 // int
    39 int ?=?(int& n, zero_t) { return n = 0.f; }
    40 /* float */
    41 void ?{}(float& a, int b) { a = b; }
    42 float ?=?(float& n, zero_t) { return n = 0.f; }
    43 /* double */
    44 void ?{}(double& a, int b) { a = b; }
    45 double ?=?(double& n, zero_t) { return n = 0L; }
    46 // long double
    47 void ?{}(long double& a, int b) { a = b; }
    48 long double ?=?(long double& n, zero_t) { return n = 0L; }
    49 }
    50 
    51 forall(otype T) {
     5forall (otype T) {
    526    struct vec2 {
    537        T x, y;
     
    559}
    5610
    57 forall(otype T) {
     11
     12forall (otype T) {
    5813    static inline {
    59 
    60     // Constructors
    6114
    6215    void ?{}(vec2(T)& v, T x, T y) {
     
    9851        return u;
    9952    }
     53    }
     54
     55    forall(| negate(T)) {
    10056    vec2(T) -?(vec2(T)& v) with (v) {
    10157        return [-x, -y];
     
    149105    }
    150106
     107    // Geometric functions
    151108    forall(| add(T) | multiply(T))
    152109    T dot(vec2(T) u, vec2(T) v) {
     
    154111    }
    155112
    156     forall(| sqrt(T) | add(T) | multiply(T))
    157     T length(vec2(T) v) {
    158        return sqrt(dot(v, v));
    159     }
    160 
    161     forall(| add(T) | multiply(T))
    162     T length_squared(vec2(T) v) {
    163        return dot(v, v);
    164     }
    165 
    166     forall(| subtract(T) | sqrt(T) | add(T) | multiply(T))
    167     T distance(vec2(T) v1, vec2(T) v2) {
    168         return length(v1 - v2);
    169     }
    170 
    171     forall(| sqrt(T) | divide(T) | add(T) | multiply(T))
    172     vec2(T) normalize(vec2(T) v) {
    173         return v / sqrt(dot(v, v));
    174     }
    175 
    176     // Project vector u onto vector v
    177     forall(| sqrt(T) | divide(T) | add(T) | multiply(T))
    178     vec2(T) project(vec2(T) u, vec2(T) v) {
    179         vec2(T) v_norm = normalize(v);
    180         return v_norm * dot(u, v_norm);
    181     }
    182 
    183     // Reflect incident vector v with respect to surface with normal n
    184     forall(| sqrt(T) | divide(T) | add(T) | multiply(T) | subtract(T) | fromint(T))
    185     vec2(T) reflect(vec2(T) v, vec2(T) n) {
    186         return v - (T){2} * project(v, n);
    187     }
    188 
    189     // Refract incident vector v with respect to surface with normal n
    190     // eta is the ratio of indices of refraction between starting material and
    191     // entering material (i.e., from air to water, eta = 1/1.33)
    192     // v and n must already be normalized
    193     forall(| sqrt(T) | add(T) | multiply(T) | subtract(T) | fromint(T) | lessthan(T) | zeroassn(T))
    194     vec2(T) refract(vec2(T) v, vec2(T) n, T eta) {
    195         T dotValue = dot(n, v);
    196         T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue);
    197         if (k < (T){0}) {
    198             return 0;
    199         }
    200         return eta * v - (eta * dotValue + sqrt(k)) * n;
    201     }
    202 
    203     // Given a perturbed normal and a geometric normal,
    204     // flip the perturbed normal if the geometric normal is pointing away
    205     // from the observer.
    206     // n is the perturbed vector that we want to align
    207     // i is the incident vector
    208     // ng is the geometric normal of the surface
    209     forall(| add(T) | multiply(T) | lessthan(T) | fromint(T) | subtract(T))
    210     vec2(T) faceforward(vec2(T) n, vec2(T) i, vec2(T) ng) {
    211         return dot(ng, i) < (T){0} ? n : -n;
    212     }
    213 
    214     }
     113    } // static inline
    215114}
    216 
    217 forall(dtype ostype, otype T | writeable(T, ostype)) {
    218     ostype & ?|?( ostype & os, vec2(T) v) with (v) {
    219         return os | '<' | x | ',' | y | '>';
    220     }
    221     void ?|?( ostype & os, vec2(T) v ) with (v) {
    222         (ostype &)(os | v); ends(os);
    223     }
    224 }
Note: See TracChangeset for help on using the changeset viewer.