[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1 Vectors and points

Class: <vector4f>
Class: <point4f>

4x1 column vectors. Vector4f is intended to be used to represent a vector, and point4f is to a point, but as far as OpenGL concerns, both are just an array of four floats, x, y, z and w.

These classes inherit <sequence> and <collection> classes. So if you import gauche.sequence module, you can use generic function such as ref and (setter ref) to get/set individual elements. The generic version of map and for-each can also be used on the vector4f and point4f instances.

Aside from the type, the only difference is that the default value of w component— it's 0.0 for vectors, and 1.0 for points. So usual transformation produces expected results; for example point plus vector becomes point, vector plus vector becomes vector, and translating point changes its coordinates but translating vectors doesn't, and so on. However, you can set w component to other value to do nontrivial operations.

Reader syntax: #,(vector4f x y z w)
Reader syntax: #,(point4f x y z w)

These SRFI-10 syntax can be used to denote literal <vector4f> and <point4f> instance, respectively.

The write methods are defined so that the instance is written out in this form, and can be read back later.

Function: vector4f? obj
Function: point4f? obj

Returns true iff obj is vector4f and point4f, respectively.

Function: vector4f x y z &optional (w 0.0)
Function: point4f x y z &optional (w 1.0)

Creates a vector4f and point4f instance with given values, respectively.

Function: make-vector4f
Function: make-point4f

Another way to create a vector4f and a point4f. The first returns #,(vector4f 0.0 0.0 0.0 0.0), and the latter returns #,(point4f 0.0 0.0 0.0 1.0).

Function: list->vector4f l
Function: list->point4f l

Convert a list of three or four real numbers to a vector4f and a point4f, respectively. If l is not a list of three or four real numbers, an error is signalled.

 
(list->vector4f l)
  ≡ (apply vector4f l)
  ≡ (coerce-to <vector4f> l)
Function: vector4f->list v
Function: point4f->list p

Convert a vector4f and a point4f to a list of four real numbers, respectively.

 
(vector4f->list v)
  ≡ (coerce-to <list> v)
  ≡ (map (cut ref v <>) (iota 4))
Function: f32vector->vector4f v &optional start
Function: f32vector->point4f v &optional start

Creates a vector4f or a point4f, initializing by the elements of f32vector v. V must be longer than 4, and the first four elements are used to initialize the created vector or point.

If optional start argument is given, it specifies an index of v from which the initial values are taken; that is, start, start+1, start+2 and start+3-th elements are used to create a vector or a point. This allows to create vectors from plain float array:

 
(map (lambda (off) (f32vector->vector4f vec (* off 4)))
     (iota (/ (size-of vec) 4)))

The conversion can be done using coerce-to, as well.

 
(f32vector->vector4f vec)
  ≡ (coerce-to <vector4f> vec)
Function: vector4f->f32vector v
Function: point4f->f32vector p

Convert a vector4f v or a point4f p to four-element f32vector.

 
(vector4f->f32vector v)
 ≡ (coerce-to <f32vector> v)
Function: vector4f-copy v
Function: point4f-copy p

Returns a new copy of vector4f v or point4f p, respectively.

Function: vector4f-copy! dstv srcv
Function: point4f-copy! dstp srcp

Destructively sets the content of srcv or srcp to dstv or dstp, respectively.

Function: vector4f-set! v k value
Function: point4f-set! p k value

Sets a real number value to k-th element of a vector4f v or a point4f p.

 
(vector4f-set! v k value)
  ≡ (set! (ref v k) value)
Function: vector4f-ref v k &optional fallback
Function: point4f-ref p k &optional fallback

Gets a value of k-th element of a vector4f v or a point4f p. If k is out of range, an error is signalled, unless fallback is provided, in such a case fallback is returned.

 
(vector4f-ref v k)
  ≡ (ref v k)
Function: vector4f-dot x y

Returns a dot product of two vector4fs, x and y.

Function: vector4f-cross x y

Returns a cross product of two vector4fs, x and y. (w element is ignored).

Function: vector4f-norm v

Returns the norm (length) of the vector v.

Function: vector4f-normalize x
Function: vector4f-normalize! x

Returns a normalized vector of vector4f x. Vector4f-normalize allocates a new vector, while vector4f-normalize! modifies the original vector.

As a special case, if x is a vector of length 0, a vector of length 0 is returned.

Function: vector4f-add x y
Function: vector4f-sub x y
Function: vector4f-add! x y
Function: vector4f-sub! x y

Returns a sum of two vector4fs, x and y. The destructive version modifies x.

Function: point4f-add x y
Function: point4f-add! x y

Adds a point4f x and a vector4f y, and returns a translated point. The destructive version modifies x.

Function: point4f-sub x y

Subtracts either a vector4f or a point4f y from a point4f x. If y is a vector4f, returns a translated point. If y is a point4f, returns a vector4f from point y to point x.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on June, 7 2008 using texi2html 1.78.