Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/charisee/src/include/Diderot/inline-vec2.h
ViewVC logotype

View of /branches/charisee/src/include/Diderot/inline-vec2.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1295 - (download) (as text) (annotate)
Thu Jun 9 06:42:54 2011 UTC (8 years, 3 months ago) by jhr
Original Path: trunk/src/include/Diderot/inline-vec2.h
File size: 2262 byte(s)
  Added support for clamp to trunk
/*! \file inline-vec2.h
 *
 * \author John Reppy
 *
 * \brief inline functions for 2D vectors
 */

/*
 * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#ifndef _DIDEROT_INLINE_VEC2_H_
#define _DIDEROT_INLINE_VEC2_H_

#ifndef _DIDEROT_TYPES_H_
#include "Diderot/types.h"
#endif

STATIC_INLINE vec2f_t vec2f (float a, float b)
{
#ifdef VEC2_OK
    return __extension__ (vec2f_t){ a, b };
#else
    return __extension__ (vec2f_t){ a, b, 0.0, 0.0 };
#endif
}

STATIC_INLINE vec2i_t vec2ftoi (vec2f_t v)
{
    union2f_t u;
    u.v = v;
#ifdef VEC2_OK
    return __extension__ (vec2i_t){ (int32_t)u.r[0], (int32_t)u.r[1] };
#else
    return __extension__ (vec2i_t){ (int32_t)u.r[0], (int32_t)u.r[1], 0, 0 };
#endif
}

STATIC_INLINE vec2f_t vec2itof (vec2i_t v)
{
    union2i_t u;
    u.v = v;
#ifdef VEC2_OK
    return __extension__ (vec2f_t){ (float)u.i[0], (float)u.i[1] };
#else
    return __extension__ (vec2f_t){ (float)u.i[0], (float)u.i[1], (float)u.i[2], 0 };
#endif
}

STATIC_INLINE vec2f_t scale2f (float s, vec2f_t v)
{
    return vec2f(s, s) * v;
}

STATIC_INLINE vec2f_t clamp2f (vec2f_t lo, vec2f_t hi, vec2f_t v)
{
  // FIXME: there is probably a vectorized way to compute this
    union2f_t a, b, c;
    a.v = lo; b.v = hi; c.v = v;
    return vec2f(
	clampf(a.r[0], b.r[0], c.r[0]),
	clampf(a.r[1], b.r[1], c.r[1]));
}

STATIC_INLINE vec2f_t lerp2f (vec2f_t a, vec2f_t b, float t)
{
    return a + scale2f(t, b - a);
}

STATIC_INLINE vec2f_t floor2f (vec2f_t v)
{
    union2f_t u;
    u.v = v;
    return vec2f(floorf(u.r[0]), floorf(u.r[1]));
}

STATIC_INLINE vec2i_t truncToInt2f (vec2f_t v)
{
    union2f_t t;
    t.v = v;
#ifdef VEC2_OK
    return __extension__ (vec2i_t){
	(int32_t)truncf(t.r[0]),
	(int32_t)truncf(t.r[1]) };
#else
    return __extension__ (vec2i_t){
	(int32_t)truncf(t.r[0]),
	(int32_t)truncf(t.r[1]),
	0,
	0 };
#endif
}

STATIC_INLINE float dot2f (vec2f_t u, vec2f_t v)
{
    union2f_t uv = __extension__ (union2f_t)(u*v);
    return uv.r[0] + uv.r[1];
}

STATIC_INLINE float length2f (vec2f_t v)
{
    return sqrtf(dot2f(v, v));
}

STATIC_INLINE vec2f_t normalize2f (vec2f_t v)
{
    return scale2f(1.0 / length2f(v), v);
}

#endif /* !_DIDEROT_INLINE_VEC2_H_ */

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0