3.7 Geometry

Sh provides several functions to make geometric operations convenient. Some of these are supported directly and very efficiently by GPU hardware, dot products being the most commonly used operation in this class. Use of these functions is encouraged, since this enables the compiler to perform domain-specific optimizations. Tables 3.12, 3.13 and 3.14 list these functions.





Operation r <-- a,b,c,...Description






/ E  ^   3 <-- 3,3 Cross product of a and b
  _ |              _| 
      a1b2 - a2b1
r<--  |_  a2b0 - a0b2  _| .
      a0b1 - a1b0



|  1 <-- N,N Dot product
            N sum -1
r <-- (a.b) =     aibi
             i=0



cross  3 <-- 3,3 Cross-product of a and b (equivalent to “^” operator)



dot  1 <-- N,N Dot product of a and b (equivalent to “|” operator)



 


Table 3.12: Cross and dot products. See also the matrix functions described in Section 3.8





Operation  r <-- a,b,c,...Description






/ E  distance  1 <-- N,N Euclidean (L2) distance between a and b:
v= a-- b,
          V~ -----
r<-- |v|2 =  v .v



/ E  distance_1  1 <-- N,N Manhattan (L1) distance between a and b:
v = a- b,
          N sum -1
 r<-- |v |1 =     |vi|
          i=0



/ E  distance_inf 1 <-- N,N Inf (L oo ) distance between a and b:
v= a - b,
   --  -- N -1
r<-- |v| oo  = max |vi|
           i=0



/ E  length  1 <-- N Euclidean (L2) length of a:
            V~ ---
r <--  |a|2 =  a .a



/ E  length_1  1 <-- N Manhattan (L1) length of a:
          N sum  -1
r <-- |a| 2 =     |ai|
           i=0



/ E  length_inf  1 <-- N Inf (L oo ) length of a:
r <-- |a |2 = Nma-x1|ai|
           i=0



 


Table 3.13: Length and distance functions. See also the matrix functions described in Section 3.8





Operation  r <-- a,b,c,...Description






/ E  faceforward N <-- N,N Make vector b face in same direction as a:
    {
       a   if (a .b) > 0,
r <--    -a  otherwise



/ E  lit  4 <-- 1,1,1 Compute lighting coefficients:
r0<--1

r1<--ma{x(a,  0)
      bc  if a > 0 and b < 0,
r2<--   0   otherwise

r3<--1



normalize  N <-- N Normalize vector to unit Euclidean length:
^r<--norm(a)  = a/|a| 2.



/ E  reflect  N <-- N,N Reflection of a on surface with (possibly unnormalized) normal b. The vector r is only normalized if a is.
        ( )
^b<--norm   b  ,
r<--2(a  .^b)^b - a



/ E  refract  N <-- N,N,1Refraction vector for incoming vector a on surface with normal b and relative index of refraction j = c



 


Table 3.14: Lighting functions. See also the matrix functions described in Section 3.8

For ShVectors and ShNormals, a short-hand notation for dot products is (v1|v2), which takes the dot product of v1 and v2. The cross product of two three-dimensional vectors may be computed as (v1^v2). If both arguments are ShVectors the cross product returns an ShNormal. If one of the components is an ShNormal it returns an ShVector.


Note: This manual is available as a bound book from AK Peters, including better formatting, in-depth examples, and about 200 pages not available on-line.