source: libcfa/src/vec/vec.hfa @ 470618c0

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 470618c0 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2021 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// io/types.hfa --
8//
9// Author           : Dimitry Kobets
10// Created On       :
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16#pragma once
17
18#include <math.hfa>
19
20trait fromint(T) {
21    void ?{}(T&, int);
22};
23trait zeroinit(T) {
24    void ?{}(T&, zero_t);
25};
26trait zero_assign(T) {
27    T ?=?(T&, zero_t);
28};
29trait subtract(T) {
30    T ?-?(T, T);
31};
32trait negate(T) {
33    T -?(T);
34};
35trait add(T) {
36    T ?+?(T, T);
37};
38trait multiply(T) {
39    T ?*?(T, T);
40};
41trait divide(T) {
42    T ?/?(T, T);
43};
44trait lessthan(T) {
45    int ?<?(T, T);
46};
47trait equality(T) {
48    int ?==?(T, T);
49};
50trait sqrt(T) {
51    T sqrt(T);
52};
53
54static inline {
55// int
56int ?=?(int& n, zero_t) { return n = 0.f; }
57// unsigned int
58int ?=?(unsigned int& n, zero_t) { return n = 0.f; }
59/* float */
60void ?{}(float& a, int b) { a = b; }
61float ?=?(float& n, zero_t) { return n = 0.f; }
62/* double */
63void ?{}(double& a, int b) { a = b; }
64double ?=?(double& n, zero_t) { return n = 0L; }
65// long double
66void ?{}(long double& a, int b) { a = b; }
67long double ?=?(long double& n, zero_t) { return n = 0L; }
68}
69
70trait dottable(V, T) {
71    T dot(V, V);
72};
73
74static inline {
75
76forall(T | sqrt(T), V | dottable(V, T))
77T length(V v) {
78   return sqrt(dot(v, v));
79}
80
81forall(T, V | dottable(V, T))
82T length_squared(V v) {
83   return dot(v, v);
84}
85
86forall(T, V | { T length(V); } | subtract(V))
87T distance(V v1, V v2) {
88    return length(v1 - v2);
89}
90
91forall(T, V | { T length(V); V ?/?(V, T); })
92V normalize(V v) {
93    return v / length(v);
94}
95
96// Project vector u onto vector v
97forall(T, V | dottable(V, T) | { V normalize(V); V ?*?(V, T); })
98V project(V u, V v) {
99    V v_norm = normalize(v);
100    return v_norm * dot(u, v_norm);
101}
102
103// Reflect incident vector v with respect to surface with normal n
104forall(T | fromint(T), V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); })
105V reflect(V v, V n) {
106    return v - (T){2} * project(v, n);
107}
108
109// Refract incident vector v with respect to surface with normal n
110// eta is the ratio of indices of refraction between starting material and
111// entering material (i.e., from air to water, eta = 1/1.33)
112// v and n must already be normalized
113forall(T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T),
114       V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); })
115V refract(V v, V n, T eta) {
116    T dotValue = dot(n, v);
117    T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue);
118    if (k < (T){0}) {
119        return 0;
120    }
121    return eta * v - (eta * dotValue + sqrt(k)) * n;
122}
123
124// Given a perturbed normal and a geometric normal,
125// flip the perturbed normal if the geometric normal is pointing away
126// from the observer.
127// n is the perturbed vector that we want to align
128// i is the incident vector
129// ng is the geometric normal of the surface
130forall(T | lessthan(T) | zeroinit(T), V | dottable(V, T) | negate(V))
131V faceforward(V n, V i, V ng) {
132    return dot(ng, i) < (T){0} ? n : -n;
133}
134
135} // inline
Note: See TracBrowser for help on using the repository browser.