Vector

xiaoxiao2021-02-28  37

https://en.wikibooks.org/wiki/Cg_Programming/Vector_and_Matrix_Operations

1.

Multiplying a row vector from the left to a matrix corresponds to multiplying a column vector to the transposed matrix from the right:

{\displaystyle \mathbf {v} ^{T}\mathrm {M} =\left(\mathrm {M} ^{T}\mathbf {v} \right)^{T}}

In components:

{\displaystyle \mathbf {v} ^{T}\mathrm {M} =\left[{\begin{matrix}v_{1}&v_{2}\end{matrix}}\right]\left[{\begin{matrix}m_{1,1}&m_{1,2}\\m_{2,1}&m_{2,2}\end{matrix}}\right]}   {\displaystyle =\left[{\begin{matrix}v_{1}m_{1,1}+v_{2}m_{2,1}&v_{1}m_{1,2}+v_{2}m_{2,2}\end{matrix}}\right]}   {\displaystyle =\left(\left[{\begin{matrix}m_{1,1}&m_{2,1}\\m_{1,2}&m_{2,2}\end{matrix}}\right]\left[{\begin{matrix}v_{1}\\v_{2}\end{matrix}}\right]\right)^{T}}   {\displaystyle =\left(\mathrm {M} ^{T}\mathbf {v} \right)^{T}}

Thus, multiplying a vector from the left to a matrix corresponds to multiplying it from the right to the transposed matrix without explicitly computing the transposed matrix.

There is also a function transpose to compute the transposed matrix:

float2x2 m = float2x2(1., 2., 3., 4.); float2x2 n = transpose(m); // = float2x2(1., 3., 2., 4)

2.

float3 cross(float3 a, float3 b) // = float3(a[1] * b[2] - a[2] * b[1], // a[2] * b[0] - a[0] * b[2], // a[0] * b[1] - a[1] * b[0]) float dot(TYPE a, TYPE b) // = a[0] * b[0] + a[1] * b[1] + ... float length(TYPE a) // = sqrt(dot(a, a))float distance(TYPE a, TYPE b) // = length(a - b)TYPE normalize(TYPE a) // = a / length(a)TYPE faceforward(TYPE n, TYPE i, TYPE nRef) // returns n if dot(nRef, i) < 0, -n otherwiseTYPE reflect(TYPE i, TYPE n) // = i - 2. * dot(n, i) * n // this computes the reflection of vector 'i' // at a plane of normalized(!) normal vector 'n'

TYPE refract(TYPE i, TYPE n, float r)

//float d = 1.0 - r * r * (1.0 - dot(n, i) * dot(n, i)); //if (d < 0.0) return TYPE(0.0, 0.0, ...); // total internal reflection //return r * i - (r * dot(n, i) + sqrt(d)) * n;

转载请注明原文地址: https://www.6miu.com/read-2612900.html

最新回复(0)