1.3.72
 
Loading...
Searching...
No Matches
OptiX8Math.h
Go to the documentation of this file.
1
10#ifndef OPTIX8_MATH_H
11#define OPTIX8_MATH_H
12
13#include <vector_types.h> // float3, float2, int2, etc. (from CUDA runtime)
14
15// ---------------------------------------------------------------------------
16// float3 arithmetic operators
17// ---------------------------------------------------------------------------
18
19__host__ __device__ __forceinline__ float3 operator+(const float3 &a, const float3 &b) {
20 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
21}
22__host__ __device__ __forceinline__ float3 operator-(const float3 &a, const float3 &b) {
23 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
24}
25__host__ __device__ __forceinline__ float3 operator*(const float3 &a, float s) {
26 return make_float3(a.x * s, a.y * s, a.z * s);
27}
28__host__ __device__ __forceinline__ float3 operator*(float s, const float3 &a) {
29 return make_float3(a.x * s, a.y * s, a.z * s);
30}
31__host__ __device__ __forceinline__ float3 operator*(const float3 &a, const float3 &b) {
32 return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
33}
34__host__ __device__ __forceinline__ float3 operator/(const float3 &a, float s) {
35 return make_float3(a.x / s, a.y / s, a.z / s);
36}
37__host__ __device__ __forceinline__ float3 operator-(const float3 &a) {
38 return make_float3(-a.x, -a.y, -a.z);
39}
40__host__ __device__ __forceinline__ float3 &operator+=(float3 &a, const float3 &b) {
41 a.x += b.x; a.y += b.y; a.z += b.z; return a;
42}
43__host__ __device__ __forceinline__ float3 &operator-=(float3 &a, const float3 &b) {
44 a.x -= b.x; a.y -= b.y; a.z -= b.z; return a;
45}
46__host__ __device__ __forceinline__ float3 &operator*=(float3 &a, float s) {
47 a.x *= s; a.y *= s; a.z *= s; return a;
48}
49
50// ---------------------------------------------------------------------------
51// float2 arithmetic operators
52// ---------------------------------------------------------------------------
53
54__host__ __device__ __forceinline__ float2 operator+(const float2 &a, const float2 &b) {
55 return make_float2(a.x + b.x, a.y + b.y);
56}
57__host__ __device__ __forceinline__ float2 operator-(const float2 &a, const float2 &b) {
58 return make_float2(a.x - b.x, a.y - b.y);
59}
60__host__ __device__ __forceinline__ float2 operator*(const float2 &a, float s) {
61 return make_float2(a.x * s, a.y * s);
62}
63__host__ __device__ __forceinline__ float2 operator*(float s, const float2 &a) {
64 return make_float2(a.x * s, a.y * s);
65}
66
67// ---------------------------------------------------------------------------
68// Math functions on float3
69// ---------------------------------------------------------------------------
70
71__host__ __device__ __forceinline__ float dot(const float3 &a, const float3 &b) {
72 return a.x * b.x + a.y * b.y + a.z * b.z;
73}
74
75__host__ __device__ __forceinline__ float3 cross(const float3 &a, const float3 &b) {
76 return make_float3(
77 a.y * b.z - a.z * b.y,
78 a.z * b.x - a.x * b.z,
79 a.x * b.y - a.y * b.x);
80}
81
82__host__ __device__ __forceinline__ float length(const float3 &v) {
83 return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
84}
85
86__host__ __device__ __forceinline__ float3 normalize(const float3 &v) {
87 float inv_len = 1.0f / length(v);
88 return v * inv_len;
89}
90
91__host__ __device__ __forceinline__ float dot(const float2 &a, const float2 &b) {
92 return a.x * b.x + a.y * b.y;
93}
94
95#endif // OPTIX8_MATH_H