Ignore:
Timestamp:
Jul 14, 2026, 9:26:24 PM (4 hours ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
a12816e7
Parents:
f41b161
Message:

formatting, turn off -Wdangling-pointer around small code fragments because of false positive

File:
1 edited

Legend:

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

    rf41b161 rc62013e  
    5959};
    6060forall(T)
    61 trait sqrt {
     61        trait sqrt {
    6262    T sqrt(T);
    6363};
    6464
    6565static inline {
    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 }
     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
    8080
    81 forall(V, T)
     81forall( V, T )
    8282trait dottable {
    83     T dot(V, V);
     83    T dot( V, V );
    8484};
    8585
    8686static inline {
     87        forall( T | sqrt( T ), V | dottable( V, T ) )
     88        T length( V v ) {
     89                return sqrt( dot( v, v ) );
     90        }
    8791
    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        }
    9296
    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        }
    97101
    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        }
    102106
    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        }
    107113
    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        }
    114119
    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"
    120123
    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        }
    135137
    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
    146139
    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.