/* * Copyright (c) 2002-2021, NVIDIA CORPORATION. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-FileCopyrightText: Copyright (c) 2002-2021 NVIDIA CORPORATION * SPDX-License-Identifier: Apache-2.0 */ /// \nodoc (keyword to exclude this file from automatic README.md generation) #ifndef _nvmath_h_ #define _nvmath_h_ #include "nvmath_types.h" #include namespace nvmath { // clang-format off template const vector2 operator+(const vector2& u, const vector2& v); template const vector2 operator+(const vector2& u, const T s); template const vector2 operator-(const vector2& u, const vector2& v); template const vector2 operator-(const vector2& u, const T s); template const vector2 operator*(const T s, const vector2& u); template const vector2 operator*(const vector2& u, const T s); template const vector2 operator/(const vector2& u, const T s); template const vector2 operator*(const vector2&u, const vector2&v); template const vector2 operator/(const vector2&u, const vector2&v); template vector3 mult(const matrix3& M, const vector3& v); template vector3 mult(const matrix4& M, const vector3& v); template const vector3 operator+(const vector3& u, const vector3& v); template const vector3 operator-(const vector3& u, const vector3& v); template const vector3 operator+(const vector3& u, const T v); template const vector3 operator-(const vector3& u, const T v); template const vector3 operator^(const vector3& u, const vector3& v); template const vector3 operator*(const T s, const vector3& u); template const vector3 operator*(const vector3& u, const T s); template const vector3 operator/(const vector3& u, const T s); template const vector3 operator*(const vector3& u, const vector3& v); template const vector3 operator/(const vector3& u, const vector3& v); template const vector4 operator+(const vector4& u, const vector4& v); template const vector4 operator-(const vector4& u, const vector4& v); template const vector4 operator+(const vector4& u, const T s); template const vector4 operator-(const vector4& u, const T s); template const vector4 operator*(const T s, const vector4& u); template const vector4 operator*(const vector4& u, const T s); template const vector4 operator/(const vector4& u, const T s); template const vector4 operator*(const vector4& u, const vector4& v); template const vector4 operator/(const vector4& u, const vector4& v); template const vector3 operator*(const matrix3&, const vector3&); template const vector3 operator*(const vector3&, const matrix3&); template matrix4 mult(const matrix4& A, const matrix4& B); template const vector4 operator*(const matrix4&, const vector4&); template const vector4 operator*(const matrix4&, const vector3&); template const vector4 operator*(const vector4&, const matrix4&); template matrix4 scale_mat4(const vector3 &t); template matrix4 translation_mat4(const vector3 &t); template matrix4 translation_mat4(T x, T y, T z); template matrix4 rotation_mat4_x(T a); template matrix4 rotation_mat4_y(T a); template matrix4 rotation_mat4_z(T a); template const quaternion operator*(const quaternion&, const quaternion&); template const quaternion mul(const quaternion&, const quaternion&); template quaternion add_quats(const quaternion & q1, const quaternion & q2); template T dot(const quaternion & p, const quaternion & q); template quaternion slerp_quats(T s, const quaternion & q1, const quaternion & q2); template quaternion axis_to_quat(const vector3 & a, const T phi); template quaternion mat_2_quat(const matrix3 &M); template quaternion normalize(const quaternion & p); template quaternion conj(const quaternion & q); template matrix3 quat_2_mat(const quaternion &q ); template quaternion mat_2_quat(const matrix4 &M); // constant algebraic values static const nv_scalar array16_id[] = { nv_one, nv_zero, nv_zero, nv_zero, nv_zero, nv_one, nv_zero, nv_zero, nv_zero, nv_zero, nv_one, nv_zero, nv_zero, nv_zero, nv_zero, nv_one}; static const nv_scalar array16_zero[] = { nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero, nv_zero}; static const nv_scalar array16_scale_bias[] = { nv_one_half, nv_zero, nv_zero, nv_zero, nv_zero, nv_one_half, nv_zero, nv_zero, nv_zero, nv_zero, nv_one_half, nv_zero, nv_one_half, nv_one_half, nv_one_half, nv_one}; static const nv_scalar array9_id[] = { nv_one, nv_zero, nv_zero, nv_zero, nv_one, nv_zero, nv_zero, nv_zero, nv_one}; static const vector2 vec2f_zero(nv_zero,nv_zero); static const vector4 vec4f_one(nv_one,nv_one,nv_one,nv_one); static const vector3 vec3f_one(nv_one,nv_one,nv_one); static const vector3 vec3f_zero(nv_zero,nv_zero,nv_zero); static const vector3 vec3f_x(nv_one,nv_zero,nv_zero); static const vector3 vec3f_y(nv_zero,nv_one,nv_zero); static const vector3 vec3f_z(nv_zero,nv_zero,nv_one); static const vector3 vec3f_neg_x(-nv_one,nv_zero,nv_zero); static const vector3 vec3f_neg_y(nv_zero,-nv_one,nv_zero); static const vector3 vec3f_neg_z(nv_zero,nv_zero,-nv_one); static const vector4 vec4f_zero(nv_zero,nv_zero,nv_zero,nv_zero); static const vector4 vec4f_x(nv_one,nv_zero,nv_zero,nv_zero); static const vector4 vec4f_neg_x(-nv_one,nv_zero,nv_zero,nv_zero); static const vector4 vec4f_y(nv_zero,nv_one,nv_zero,nv_zero); static const vector4 vec4f_neg_y(nv_zero,-nv_one,nv_zero,nv_zero); static const vector4 vec4f_z(nv_zero,nv_zero,nv_one,nv_zero); static const vector4 vec4f_neg_z(nv_zero,nv_zero,-nv_one,nv_zero); static const vector4 vec4f_w(nv_zero,nv_zero,nv_zero,nv_one); static const vector4 vec4f_neg_w(nv_zero,nv_zero,nv_zero,-nv_one); static const quaternion quat_id(nv_zero,nv_zero,nv_zero,nv_one); static const matrix4 mat4f_id(array16_id); static const matrix3 mat3f_id(array9_id); static const matrix4 mat4f_zero(array16_zero); static const matrix4 mat4f_scale_bias(array16_scale_bias); // normalizes a vector template vector2 normalize(const vector2 & u); template vector3 normalize(const vector3 & u); template vector4 normalize(const vector4 & u); // Computes the squared magnitude template T nv_sq_norm(const vector2 & n); template T nv_sq_norm(const vector3 & n); template T nv_sq_norm(const vector4 & n); // Computes the magnitude template T nv_norm(const vector2 & n); template T nv_norm(const vector3 & n); template T nv_norm(const vector4 & n); template T length(const vector2 & n); template T length(const vector3 & n); template T length(const vector4 & n); // computes the cross product ( v cross w) and stores the result in u // i.e. u = v cross w template vector3 cross(const vector3 & v, const vector3 & w); // computes the dot product ( v dot w) and stores the result in u // i.e. u = v dot w template T dot(const vector3 & v, const vector3 & w); template T dot(const vector4 & v, const vector4 & w); template T dot(const vector3 & v, const vector4 & w); template T dot(const vector4 & v, const vector3 & w); template T dot(const vector2 & v, const vector2 & w); // clamp template T clamp(T x, T minVal, T maxVal); template vector3 clamp(const vector3& x, const vector3& minVal, const vector3& maxVal); // min / max template T min(T minVal, T maxVal); template T max(T minVal, T maxVal); template vector3 max(const vector3& minVal, const vector3& maxVal); // mix template T mix(T x, T y, T a); template vector3 mix(const vector3& x, const vector3& y, T a); template vector3 mix(const vector3& x, const vector3& y, const vector3& a); // Pow template vector3 pow(const vector3& base, const vector3& exponent); // sqrt template vector3 sqrt(const vector3& x); // radians template T radians(T x); // sin template vector3 sin(const vector3& x); // mod template T mod(T a, T b); template vector2 mod(const vector2& a, T b); // fract template T fract(T x); // compute the reflected vector R of L w.r.t N - vectors need to be // normalized // // R N L // _ _ // |\ ^ /| // \ | / // \ | / // \|/ // + template vector3 reflect(const vector3 & n, const vector3 & l); // Computes u = v * lambda + u template vector3 madd(const vector3 & v, const T & lambda); // Computes u = v * lambda template vector3 mult(const vector3 & v, const T & lambda); // Computes u = v * w template vector3 mult(const vector3 & v, const vector3 & w); // Computes u = v + w template vector3 add(const vector3 & v, const vector3 & w); // Computes u = v - w template vector3 sub(const vector3 & v, const vector3 & w); // Computes u = pow(v, exponemt) template vector3 pow(const vector3& v, const T & e); // Computes u = u * s template vector2 scale(const vector2 & u, const T s); template vector3 scale(const vector3 & u, const T s); template vector4 scale(const vector4 & u, const T s); // Computes u = M * v template vector3 mult(const matrix3 & M, const vector3 & v); template vector4 mult(const matrix4 & M, const vector4 & v); // Computes u = v * M template vector3 mult(const vector3 & v, const matrix3 & M); template vector4 mult(const vector4 & v, const matrix4 & M); // Computes u = M(4x4) * v and divides by w template vector3 mult_pos(const matrix4 & M, const vector3 & v); // Computes u = M(4x4) * v template vector3 mult_dir(const matrix4 & M, const vector3 & v); // Computes u = M(4x4) * v and does not divide by w (assumed to be 1) template vector3 mult(const matrix4& M, const vector3& v); // Computes u = v * M(4x4) and divides by w template vector3 mult_pos(const vector3 & v, const matrix4 & M); // Computes u = v * M(4x4) template vector3 mult_dir(const vector3 & v, const matrix4 & M); // Computes u = v * M(4x4) and does not divide by w (assumed to be 1) template vector3 mult(const vector3& v, const matrix4& M); // Computes C = A + B template matrix4 add(const matrix4 & A, const matrix4 & B); template matrix3 add(const matrix3 & A, const matrix3 & B); // Computes C = A * B template matrix4 mult(const matrix4 & A, const matrix4 & B); template matrix3 mult(const matrix3 & A, const matrix3 & B); // Compute M = -M template matrix4 negate(const matrix4 & M); template matrix3 negate(const matrix3 & M); // Computes B = Transpose(A) // T // B = A template matrix3 transpose(const matrix3 & A); template matrix4 transpose(const matrix4 & A); // Computes B = inverse(A) // -1 // B = A template matrix4 invert(const matrix4 & A, bool& valid); template matrix4 invert(const matrix4 & A); template matrix3 invert(const matrix3 & A); template matrix4 inverse(const matrix4& A); template matrix3 inverse(const matrix3& A); // Computes B = inverse(A) // T T // (R t) (R -R t) // assuming that A = (0 1) so that B = (0 1) // B = A template matrix4 invert_rot_trans(const matrix4 & A); template matrix4 look_at(const vector3 & eye, const vector3 & center, const vector3 & up); template matrix4 frustum(const T l, const T r, const T b, const T t, const T n, const T f); template matrix4 frustum01(const T l, const T r, const T b, const T t, const T n, const T f); template matrix4 frustum01Rev(const T l, const T r, const T b, const T t, const T n, const T f); template matrix4 perspective(const T fovy, const T aspect, const T n, const T f); template matrix4 perspective01(const T fovy, const T aspect, const T n, const T f); template matrix4 perspective01Rev(const T fovy, const T aspect, const T n, const T f); template matrix4 ortho(const T left, const T right, const T bottom, const T top, const T n, const T f); /* Decompose Affine Matrix * A = TQS, where * A is the affine transform * T is the translation vector * Q is the rotation (quaternion) * S is the scale vector * f is the sign of the determinant */ template void decomp_affine(const matrix4 & A, vector3 & v3, quaternion & Q, quaternion & U, vector3 & S, T & f); // surface properties template matrix3 & tangent_basis(const vector3 & v0,const vector3 & v1,const vector3 & v2,const vector2 & t0,const vector2 & t1,const vector2 & t2, const vector3 & n); // linear interpolation #ifdef NVP_SUPPORTS_OPTIX #pragma message("**WARNING** nvmath.h : Canceling the lerp() function here : already declared in OptiX") #else template T lerp(const T t, const T a, const T b); template vector2 lerp(const T & t, const vector2 & u, const vector2 & v); template vector3 lerp(const T & t, const vector3 & u, const vector3 & v); template vector4 lerp(const T & t, const vector4 & u, const vector4 & v); #endif // utilities template T nv_random(); template quaternion trackball(vector2 & pt1, vector2 & pt2, T trackballsize); template vector3 cube_map_normal(int i, int x, int y, int cubesize, const vector3 & v); // Componentwise template T nv_min(const T & lambda, const T & n); template vector2 nv_min(const vector2 & vFirst, const vector2 & vSecond); template vector3 nv_min(const vector3 & vFirst, const vector3 & vSecond); template vector4 nv_min(const vector4 & vFirst, const vector4 & vSecond); template T nv_max(const T & lambda, const T & n); template vector3 nv_max(const vector3 & vFirst, const vector3 & vSecond); template vector2 nv_max(const vector2 & vFirst, const vector2 & vSecond); template vector4 nv_max(const vector4 & vFirst, const vector4 & vSecond); template T nv_clamp(const T u, const T min, const T max); template vector2 nv_clamp(const vector2& u, const T min, const T max); template vector3 nv_clamp(const vector3& u, const T min, const T max); template vector4 nv_clamp(const vector4& u, const T min, const T max); template vector2 nv_floor(const vector2& u); template vector3 nv_floor(const vector3& u); template vector4 nv_floor(const vector4& u); template T nv_abs(const T u); template vector2 nv_abs(const vector2 & u); template vector3 nv_abs(const vector3 & u); template vector4 nv_abs(const vector4 & u); template T smoothstep(T edge0, T edge1, T x); // geometry // computes the area of a triangle template T nv_area(const vector3 & v1, const vector3 & v2, const vector3 &v3); // computes the perimeter of a triangle template T nv_perimeter(const vector3 & v1, const vector3 & v2, const vector3 &v3); // find the inscribed circle template T nv_find_in_circle( vector3 & center, const vector3 & v1, const vector3 & v2, const vector3 &v3); // find the circumscribed circle template T nv_find_circ_circle( vector3 & center, const vector3 & v1, const vector3 & v2, const vector3 &v3); // fast cosine functions template T fast_cos(const T x); template T ffast_cos(const T x); // determinant template T det(const matrix3 & A); template T det(const matrix4 & A); template void nv_is_valid(const vector3& v); template void nv_is_valid(T lambda); // TL : v1 and v2 MUST be normalized. Not done inot this to avoid redundant work... template T get_angle(const vector3 & v1, const vector3 & v2); template vector3 rotate_by(const vector3 & src, const quaternion& q); template matrix4 rotation_yaw_pitch_roll( const T yaw , const T pitch , const T roll ); // clang-format on } //namespace nvmath #include "nvmath.inl" #endif //_nvmath_h_