Changeset c62013e for libcfa/src/vec/vec.hfa
- Timestamp:
- Jul 14, 2026, 9:26:24 PM (4 hours ago)
- Branches:
- master
- Children:
- a12816e7
- Parents:
- f41b161
- File:
-
- 1 edited
-
libcfa/src/vec/vec.hfa (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/vec/vec.hfa
rf41b161 rc62013e 59 59 }; 60 60 forall(T) 61 trait sqrt {61 trait sqrt { 62 62 T sqrt(T); 63 63 }; 64 64 65 65 static inline { 66 // int67 int ?=?(int& n, zero_t) { return n = 0.f; }68 // unsigned int69 int ?=?(unsigned int& n, zero_t) { return n = 0.f; }70 /* float */ 71 void ?{}(float& a, int b) { a = b; }72 float ?=?(float& n, zero_t) { return n = 0.f; }73 /* double */ 74 void ?{}(double& a, int b) { a = b; }75 double ?=?(double& n, zero_t) { return n = 0L; }76 // long double77 void ?{}(long double& a, int b) { a = b; }78 long double ?=?(long double& n, zero_t) { return n = 0L; }79 } 66 // int 67 int ?=?( int& n, zero_t ) { return n = 0.f; } 68 // unsigned int 69 int ?=?( unsigned int& n, zero_t ) { return n = 0.f; } 70 // float 71 void ?{}( float& a, int b ) { a = b; } 72 float ?=?( float& n, zero_t ) { return n = 0.f; } 73 // double 74 void ?{}( double& a, int b ) { a = b; } 75 double ?=?( double& n, zero_t ) { return n = 0L; } 76 // long double 77 void ?{}( long double& a, int b ) { a = b; } 78 long double ?=?( long double& n, zero_t ) { return n = 0L; } 79 } // static inline 80 80 81 forall( V, T)81 forall( V, T ) 82 82 trait dottable { 83 T dot( V, V);83 T dot( V, V ); 84 84 }; 85 85 86 86 static inline { 87 forall( T | sqrt( T ), V | dottable( V, T ) ) 88 T length( V v ) { 89 return sqrt( dot( v, v ) ); 90 } 87 91 88 forall(T | sqrt(T), V | dottable(V, T))89 T length(V v) {90 return sqrt(dot(v, v));91 }92 forall( T, V | dottable( V, T ) ) 93 T length_squared( V v ) { 94 return dot( v, v ); 95 } 92 96 93 forall(T, V | dottable(V, T))94 T length_squared(V v) {95 return dot(v, v);96 }97 forall( T, V | { T length( V ); } | subtract( V ) ) 98 T distance( V v1, V v2 ) { 99 return length( v1 - v2 ); 100 } 97 101 98 forall(T, V | { T length(V); } | subtract(V))99 T distance(V v1, V v2) {100 return length(v1 - v2);101 }102 forall( T, V | { T length( V ); V ?/?( V, T ); }) 103 V normalize( V v ) { 104 return v / length( v ); 105 } 102 106 103 forall(T, V | { T length(V); V ?/?(V, T); }) 104 V normalize(V v) { 105 return v / length(v); 106 } 107 // Project vector u onto vector v 108 forall( T, V | dottable( V, T ) | { V normalize( V ); V ?*?( V, T ); }) 109 V project( V u, V v ) { 110 V v_norm = normalize( v ); 111 return v_norm * dot( u, v_norm ); 112 } 107 113 108 // Project vector u onto vector v 109 forall(T, V | dottable(V, T) | { V normalize(V); V ?*?(V, T); }) 110 V project(V u, V v) { 111 V v_norm = normalize(v); 112 return v_norm * dot(u, v_norm); 113 } 114 // Reflect incident vector v with respect to surface with normal n 115 forall( T | fromint( T ), V | { V project( V, V ); V ?*?( T, V ); V ?-?( V,V ); }) 116 V reflect( V v, V n ) { 117 return v - ( T ){2} * project( v, n ); 118 } 114 119 115 // Reflect incident vector v with respect to surface with normal n 116 forall(T | fromint(T), V | { V project(V, V); V ?*?(T, V); V ?-?(V,V); }) 117 V reflect(V v, V n) { 118 return v - (T){2} * project(v, n); 119 } 120 #pragma GCC diagnostic push 121 // FIX ME: false positive with gcc > 11, so disable. 122 #pragma GCC diagnostic ignored "-Wdangling-pointer" 120 123 121 // Refract incident vector v with respect to surface with normal n 122 // eta is the ratio of indices of refraction between starting material and 123 // entering material (i.e., from air to water, eta = 1/1.33) 124 // v and n must already be normalized 125 forall(T | fromint(T) | subtract(T) | multiply(T) | add(T) | lessthan(T) | sqrt(T), 126 V | dottable(V, T) | { V ?*?(T, V); V ?-?(V,V); void ?{}(V&, zero_t); }) 127 V refract(V v, V n, T eta) { 128 T dotValue = dot(n, v); 129 T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue); 130 if (k < (T){0}) { 131 return 0; 132 } 133 return eta * v - (eta * dotValue + sqrt(k)) * n; 134 } 124 // Refract incident vector v with respect to surface with normal n eta is the ratio of indices of refraction between 125 // starting material and entering material ( i.e., from air to water, eta = 1/1.33 ) v and n must already be 126 // normalized 127 forall( T | fromint( T ) | subtract( T ) | multiply( T ) | add( T ) | lessthan( T ) | sqrt( T ), 128 V | dottable( V, T ) | { V ?*?( T, V ); V ?-?( V,V ); void ?{}( V&, zero_t ); }) 129 V refract( V v, V n, T eta ) { 130 T dotValue = dot( n, v ); 131 T k = (T){1} - eta * eta * ((T){1} - dotValue * dotValue ); 132 if ( k < (T){0}) { 133 return 0; 134 } 135 return eta * v - ( eta * dotValue + sqrt( k ) ) * n; 136 } 135 137 136 // Given a perturbed normal and a geometric normal, 137 // flip the perturbed normal if the geometric normal is pointing away 138 // from the observer. 139 // n is the perturbed vector that we want to align 140 // i is the incident vector 141 // ng is the geometric normal of the surface 142 forall(T | lessthan(T) | zeroinit(T), V | dottable(V, T) | negate(V)) 143 V faceforward(V n, V i, V ng) { 144 return dot(ng, i) < (T){0} ? n : -n; 145 } 138 #pragma GCC diagnostic pop 146 139 147 } // inline 140 // Given a perturbed normal and a geometric normal, flip the perturbed normal if the geometric normal is pointing 141 // away from the observer. n is the perturbed vector that we want to align i is the incident vector ng is the 142 // geometric normal of the surface 143 forall( T | lessthan( T ) | zeroinit( T ), V | dottable( V, T ) | negate( V ) ) 144 V faceforward( V n, V i, V ng ) { 145 return dot( ng, i ) < (T){0} ? n : -n; 146 } 147 } // static inline
Note:
See TracChangeset
for help on using the changeset viewer.