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-vec3.h
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2663 - (download) (as text) (annotate)
Fri May 30 18:02:41 2014 UTC (5 years, 4 months ago) by cchiw
File size: 2716 byte(s)
added decl
/*! \file inline-vec3.h
 *
 * \author John Reppy
 *
 * \brief inline functions for 3D vectors
 */

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

#ifndef _DIDEROT_INLINE_VEC3_H_
#define _DIDEROT_INLINE_VEC3_H_

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

STATIC_INLINE Diderot_vec3_t vec3 (const Diderot_real_t a, Diderot_real_t b, Diderot_real_t c)
{
    return __extension__ (Diderot_vec3_t){ a, b, c, 0.0f };
}

STATIC_INLINE Diderot_ivec3_t vec3rtoi (Diderot_vec3_t v)
{
    Diderot_union3_t u;
    u.v = v;
    return __extension__ (Diderot_ivec3_t){
            (Diderot_int_t)u.r[0],
            (Diderot_int_t)u.r[1],
            (Diderot_int_t)u.r[2],
            0
        };
}

STATIC_INLINE Diderot_vec3_t vec3itor (Diderot_ivec3_t v)
{
    union3i_t u;
    u.v = v;
    return __extension__ (Diderot_vec3_t){ (Diderot_real_t)u.i[0], (Diderot_real_t)u.i[1], (Diderot_real_t)u.i[2], 0 };
}

STATIC_INLINE Diderot_vec3_t scale3 (Diderot_real_t s, Diderot_vec3_t v)
{
    return vec3(s, s, s) * v;
}

STATIC_INLINE Diderot_vec3_t clamp3 (Diderot_vec3_t lo, Diderot_vec3_t hi, Diderot_vec3_t v)
{
  // FIXME: there is probably a vectorized way to compute this
    Diderot_union3_t a, b, c;
    a.v = lo; b.v = hi; c.v = v;
    return vec3(
	clamp(a.r[0], b.r[0], c.r[0]),
	clamp(a.r[1], b.r[1], c.r[1]),
	clamp(a.r[2], b.r[2], c.r[2]));
}

STATIC_INLINE Diderot_vec3_t lerp3 (Diderot_vec3_t a, Diderot_vec3_t b, Diderot_real_t t)
{
    return a + scale3(t, b - a);
}

STATIC_INLINE Diderot_vec3_t floor3 (Diderot_vec3_t v)
{
    Diderot_union3_t u;
    u.v = v;
    return vec3(FLOOR(u.r[0]), FLOOR(u.r[1]), FLOOR(u.r[2]));
}

STATIC_INLINE Diderot_ivec3_t truncToInt3 (Diderot_vec3_t v)
{
    Diderot_union3_t t;
    t.v = v;
    return __extension__ (Diderot_ivec3_t){
	(Diderot_int_t)TRUNC(t.r[0]),
	(Diderot_int_t)TRUNC(t.r[1]),
	(Diderot_int_t)TRUNC(t.r[2]),
	0 };
}

STATIC_INLINE Diderot_real_t dot3 (Diderot_vec3_t u, Diderot_vec3_t v)
{
    Diderot_union3_t uv = __extension__ (Diderot_union3_t)(u*v);
    return uv.r[0] + uv.r[1] + uv.r[2];
}

STATIC_INLINE Diderot_real_t length3 (Diderot_vec3_t v)
{
    return SQRT(dot3(v, v));
}

STATIC_INLINE Diderot_vec3_t normalize3 (Diderot_vec3_t v)
{
    return scale3(1.0 / length3(v), v);
}

STATIC_INLINE Diderot_vec3_t cross3 (Diderot_vec3_t u, Diderot_vec3_t v)
{
    Diderot_union3_t uu = (Diderot_union3_t)u;
    Diderot_union3_t uv = (Diderot_union3_t)v;
    return __extension__ (Diderot_vec3_t){
	uu.r[1]*uv.r[2] - uu.r[2]*uv.r[1],
	uu.r[2]*uv.r[0] - uu.r[0]*uv.r[2],
        uu.r[0]*uv.r[1] - uu.r[1]*uv.r[0],
	0 };
}

#endif /* !_DIDEROT_INLINE_VEC3_H_ */

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