Skip to content

Views and conversions

2 components

as_vec2

as_vec2(other, dtype = None)

View other as a vec2 with given dtype.

Parameters:

Name Type Description Default
other np.ndarray

Array to consider

required
dtype n.dtype

Dtype of results (force)

None

Returns:

Type Description
vec2

view or copy of other.

Source code in glm/vec234.py
def as_vec2(other, dtype = None):
    """ View other as a vec2 with given dtype.

    Args:

        other (np.ndarray):
            Array to consider

        dtype (n.dtype):
            Dtype of results (force)

    Returns:

        (vec2):  view or copy of other.
    """

    V = np.asanyarray(other).reshape(-1,2)
    if dtype is None:
        return V.view(ndarray.vec2)
    else:
        return V.astype(dtype).view(ndarray.vec2)

to_vec2

to_vec2(other, dtype = None)

Convert other to a vec2 with following convention:

  • v -> vec2 : v[::2] -> v[i],v[i+1]
  • vec2 -> vec2 : x,y -> x,y
  • vec3 -> vec2 : x,y,z -> x,y
  • vec4 -> vec2 : x,y,z,w -> x/w,y/w

Parameters:

Name Type Description Default
other np.ndarray

Array to consider

required
dtype n.dtype

Dtype of results (force)

None

Returns:

Type Description
vec2

copy of other.

Source code in glm/vec234.py
def to_vec2(other, dtype  = None):
    """ Convert other to a vec2 with following convention:

    - v    -> vec2 : v[::2] -> v[i],v[i+1]
    - vec2 -> vec2 : x,y     -> x,y
    - vec3 -> vec2 : x,y,z   -> x,y
    - vec4 -> vec2 : x,y,z,w -> x/w,y/w

    Args:

        other (np.ndarray):
            Array to consider

        dtype (n.dtype):
            Dtype of results (force)

    Returns:

        (vec2): copy of other.
    """

    V = np.array(other)
    dtype = dtype if dtype is not None else V.dtype

    if len(V.shape) == 1 and V.size % 2 == 0:
        _V = ndarray.vec2(V.size//2, dtype)
        _V.ravel()[...] = V
        return _V        
    elif len(V.shape) == 2:
        _V = ndarray.vec2(len(V), dtype)
        _V[...] = V[:,:2]
        if V.shape[1] == 2:
            return _V
        elif V.shape[1] == 3:
            return _V
        elif V.shape[1] == 4:
            _V /= V[:,3, np.newaxis]
            return _V

    raise TypeError("Cannot convert %s to vec2" % other) 

3 components

as_vec3

as_vec3(other, dtype = None)

View other as a vec3 with given dtype

Parameters:

Name Type Description Default
other np.array)

Array to consider

required
dtype np.dtype

Dtype of the results

None

Returns:

Type Description
vec3

view or copy of other.

Source code in glm/vec234.py
def as_vec3(other, dtype = None):
    """ View other as a vec3 with given dtype

    Args:

        other (np.array) :
            Array to consider

        dtype (np.dtype):
            Dtype of the results

    Returns:

        (vec3): view or copy of other.
    """    

    V = np.asanyarray(other).reshape(-1,3)
    if dtype is None:
        return V.view(ndarray.vec3)
    else:
        return V.astype(dtype).view(ndarray.vec3)

to_vec3

to_vec3(other, dtype = None)

Convert other to a vec3 with followign convention:

  • v -> vec3 : v[::3] -> v[i],v[i+1],v[i+2]
  • vec2 -> vec3 : x,y -> x,y,0
  • vec3 -> vec3 : x,y,z -> x,y,z
  • vec4 -> vec3 : x,y,z,w -> x/w,y/w,z/w

Parameters:

Name Type Description Default
other np.ndarray

Array to consider

required
dtype np.dtype

Dtype of the results

None

Returns:

Type Description
vec3

copy of other.

Source code in glm/vec234.py
def to_vec3(other, dtype = None):
    """ Convert other to a vec3 with followign convention:

    - v    -> vec3 : v[::3]  -> v[i],v[i+1],v[i+2]
    - vec2 -> vec3 : x,y     -> x,y,0
    - vec3 -> vec3 : x,y,z   -> x,y,z
    - vec4 -> vec3 : x,y,z,w -> x/w,y/w,z/w

    Args:

        other (np.ndarray):
            Array to consider

        dtype (np.dtype):
            Dtype of the results

    Returns:

        (vec3): copy of other.
    """

    V = np.array(other)
    dtype = dtype if dtype is not None else V.dtype

    if len(V.shape) == 1 and V.size % 3 == 0:
        _V = ndarray.vec3(V.size//3, dtype)
        _V.ravel()[...] = V
        return _V        
    elif len(V.shape) == 2:
        _V = ndarray.vec3(len(V), dtype)
        if V.shape[1] == 2:
            _V[:,:2] = V
            _V[:, 2] = 0
            return _V
        elif V.shape[1] == 3:
            _V[...] = V
            return _V        
        elif V.shape[1] == 4:
            _V[...] = V[:,:3] / V[:,3, np.newaxis]
            return _V

    raise TypeError("Cannot convert %s to vec3" % other) 

4 components

as_vec4

as_vec4(other, dtype = None)

View other as a vec4 with given dtype

Parameters:

Name Type Description Default
other np.ndarray

Array to consider

required
dtype np.dtype

Dtype of the results (force)

None

Returns:

Type Description
vec4

view or copy of other.

Source code in glm/vec234.py
def as_vec4(other, dtype = None):
    """ View other as a vec4 with given dtype

    Args:

        other (np.ndarray):
            Array to consider

        dtype (np.dtype):
            Dtype of the results (force)

    Returns:

        (vec4): view or copy of other.
    """    

    V = np.asanyarray(other).reshape(-1,4)
    if dtype is None:
        return V.view(ndarray.vec4)
    else:
        return V.astype(dtype).view(ndarray.vec4)

to_vec4

to_vec4(other, dtype = None)

Convert other to a vec4 with following convention

  • v -> vec4 : v[::4] -> v[i],v[i+1],v[i+2],v[i+3]
  • vec2 -> vec4 : x,y -> x,y,0,1
  • vec3 -> vec4 : x,y,z -> x,y,z,1
  • vec4 -> vec4 : x,y,z,w -> x,y,z,w

Parameters:

Name Type Description Default
other np.ndarray

Array to consider

required
dtype np.dtype

Dtype of the results (force)

None

Returns:

Type Description
vec4

copy of other

Source code in glm/vec234.py
def to_vec4(other, dtype = None):
    """ Convert other to a vec4 with following convention

    - v    -> vec4 : v[::4] -> v[i],v[i+1],v[i+2],v[i+3]
    - vec2 -> vec4 : x,y     -> x,y,0,1
    - vec3 -> vec4 : x,y,z   -> x,y,z,1
    - vec4 -> vec4 : x,y,z,w -> x,y,z,w

    Args:

        other (np.ndarray):
            Array to consider

        dtype (np.dtype):
            Dtype of the results (force)

    Returns:

        (vec4): copy of other
    """

    V = np.array(other)
    dtype = dtype if dtype is not None else V.dtype

    if len(V.shape) == 1 and V.size % 4 == 0:
        _V = ndarray.vec4(V.size//4, dtype)
        _V.ravel()[...] = V
        return _V        
    elif len(V.shape) == 2:
        _V = ndarray.vec4(len(V), dtype)
        if V.shape[1] == 2:
            _V[:,:2] = V
            _V[:, 2] = 0
            _V[:, 3] = 1
            return _V
        elif V.shape[1] == 3:
            _V[:,:3] = V
            _V[:, 3] = 1
            return _V        
        elif V.shape[1] == 4:
            _V[...] = V
            return _V

    raise TypeError("Cannot convert %s to vec3" % other)