NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Math.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "base/Math.hpp"
29 
30 using namespace FW;
31 
32 //------------------------------------------------------------------------
33 
35 {
36  return Vec4f(
37  (F32)(abgr & 0xFF) * (1.0f / 255.0f),
38  (F32)((abgr >> 8) & 0xFF) * (1.0f / 255.0f),
39  (F32)((abgr >> 16) & 0xFF) * (1.0f / 255.0f),
40  (F32)(abgr >> 24) * (1.0f / 255.0f));
41 }
42 
43 //------------------------------------------------------------------------
44 
45 U32 Vec4f::toABGR(void) const
46 {
47  return
48  ((((U32)(((U64)(FW::clamp(x, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 0) |
49  ((((U32)(((U64)(FW::clamp(y, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 8) |
50  ((((U32)(((U64)(FW::clamp(z, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 16) |
51  ((((U32)(((U64)(FW::clamp(w, 0.0f, 1.0f) * exp2(56)) * 255) >> 55) + 1) >> 1) << 24);
52 }
53 
54 //------------------------------------------------------------------------
55 
56 Mat3f Mat4f::getXYZ(void) const
57 {
58  Mat3f r;
59  for (int i = 0; i < 3; i++)
60  r.col(i) = Vec4f(col(i)).getXYZ();
61  return r;
62 }
63 
64 //------------------------------------------------------------------------
65 
66 Mat4f Mat4f::fitToView(const Vec2f& pos, const Vec2f& size, const Vec2f& viewSize)
67 {
68  FW_ASSERT(size.x != 0.0f && size.y != 0.0f);
69  FW_ASSERT(viewSize.x != 0.0f && viewSize.y != 0.0f);
70 
71  return
72  Mat4f::scale(Vec3f(Vec2f(2.0f) / viewSize, 1.0f)) *
73  Mat4f::scale(Vec3f((viewSize / size).min(), 1.0f)) *
74  Mat4f::translate(Vec3f(-pos - size * 0.5f, 0.0f));
75 }
76 
77 //------------------------------------------------------------------------
78 
79 Mat4f Mat4f::perspective(F32 fov, F32 nearDist, F32 farDist)
80 {
81  // Camera points towards -z. 0 < near < far.
82  // Matrix maps z range [-near, -far] to [-1, 1], after homogeneous division.
83  F32 f = rcp(tan(fov * FW_PI / 360.0f));
84  F32 d = rcp(nearDist - farDist);
85 
86  Mat4f r;
87  r.setRow(0, Vec4f( f, 0.0f, 0.0f, 0.0f ));
88  r.setRow(1, Vec4f( 0.0f, f, 0.0f, 0.0f ));
89  r.setRow(2, Vec4f( 0.0f, 0.0f, (nearDist + farDist) * d, 2.0f * nearDist * farDist * d ));
90  r.setRow(3, Vec4f( 0.0f, 0.0f, -1.0f, 0.0f ));
91  return r;
92 }
93 
94 //------------------------------------------------------------------------
95 
96 Mat3f Mat3f::rotation(const Vec3f& axis, F32 angle)
97 {
98  Mat3f R;
99  F32 cosa = cosf(angle);
100  F32 sina = sinf(angle);
101  R(0,0) = cosa + sqr(axis.x)*(1.0f-cosa); R(0,1) = axis.x*axis.y*(1.0f-cosa) - axis.z*sina; R(0,2) = axis.x*axis.z*(1.0f-cosa) + axis.y*sina;
102  R(1,0) = axis.x*axis.y*(1.0f-cosa) + axis.z*sina; R(1,1) = cosa + sqr(axis.y)*(1.0f-cosa); R(1,2) = axis.y*axis.z*(1.0f-cosa) - axis.x*sina;
103  R(2,0) = axis.z*axis.x*(1.0f-cosa) - axis.y*sina; R(2,1) = axis.z*axis.y*(1.0f-cosa) + axis.x*sina; R(2,2) = cosa + sqr(axis.z)*(1.0f-cosa);
104  return R;
105 }
106 
107 Mat3d Mat3d::rotation(const Vec3d& axis, F64 angle)
108 {
109  Mat3d R;
110  F64 cosa = cos(angle);
111  F64 sina = sin(angle);
112  R(0,0) = cosa + sqr(axis.x)*(1.0-cosa); R(0,1) = axis.x*axis.y*(1.0-cosa) - axis.z*sina; R(0,2) = axis.x*axis.z*(1.0-cosa) + axis.y*sina;
113  R(1,0) = axis.x*axis.y*(1.0-cosa) + axis.z*sina; R(1,1) = cosa + sqr(axis.y)*(1.0-cosa); R(1,2) = axis.y*axis.z*(1.0-cosa) - axis.x*sina;
114  R(2,0) = axis.z*axis.x*(1.0-cosa) - axis.y*sina; R(2,1) = axis.z*axis.y*(1.0-cosa) + axis.x*sina; R(2,2) = cosa + sqr(axis.z)*(1.0-cosa);
115  return R;
116 }
FW_CUDA_FUNC void setRow(int r, const VectorBase< T, L, V > &v)
FW_CUDA_FUNC F64 cos(F64 a)
Definition: Math.hpp:49
FW_CUDA_FUNC T sqr(const T &a)
Definition: Math.hpp:113
#define FW_PI
Definition: Defs.hpp:121
unsigned __int64 U64
Definition: Defs.hpp:97
double F64
Definition: Defs.hpp:90
U32 toABGR(void) const
Definition: Math.cpp:45
static Mat3f rotation(const Vec3f &axis, F32 angle)
Definition: Math.cpp:96
static FW_CUDA_FUNC S scale(const VectorBase< T, L-1, V > &v)
FW_CUDA_FUNC F64 tan(F64 a)
Definition: Math.hpp:50
FW_CUDA_FUNC Vec3f getXYZ(void) const
Definition: Math.hpp:365
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat x
Definition: DLLImports.inl:363
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat GLfloat z
Definition: DLLImports.inl:363
float F32
Definition: Defs.hpp:89
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat y
Definition: DLLImports.inl:363
static FW_CUDA_FUNC S translate(const VectorBase< T, L-1, V > &v)
static Mat4f perspective(F32 fov, F32 nearDist, F32 farDist)
Definition: Math.cpp:79
FW_CUDA_FUNC T min(const VectorBase< T, L, S > &v)
Definition: Math.hpp:461
#define FW_ASSERT(X)
Definition: Defs.hpp:67
FW_CUDA_FUNC T rcp(const T &a)
Definition: Math.hpp:114
FW_CUDA_FUNC const Vector< T, L > & col(int c) const
Definition: Math.hpp:526
static Mat4f fitToView(const Vec2f &pos, const Vec2f &size, const Vec2f &viewSize)
Definition: Math.cpp:66
F32 exp2(F32 a)
Definition: Math.hpp:87
unsigned int U32
Definition: Defs.hpp:85
FW_CUDA_FUNC Vec4f(void)
Definition: Math.hpp:351
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
Definition: DLLImports.inl:88
static Mat3d rotation(const Vec3d &axis, F64 angle)
Definition: Math.cpp:107
static Vec4f fromABGR(U32 abgr)
Definition: Math.cpp:34
FW_CUDA_FUNC F64 sin(F64 a)
Definition: Math.hpp:48
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
Mat3f getXYZ(void) const
Definition: Math.cpp:56