Mercurial > hg > dmlib
view dmvecmat.h @ 244:00785510b743
Constify.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 09 Oct 2012 17:22:52 +0300 |
parents | 07c4f1a7ddc6 |
children | 159264c27929 |
line wrap: on
line source
/* * DMLib * -- Vector and matrix functions * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2011 Tecnic Software productions (TNSP) */ #ifndef DMVECMAT_H #define DMVECMAT_H #include "dmlib.h" #include <math.h> #ifdef __cplusplus extern "C" { #endif #define DM_MATRIX_SIZE (4) typedef struct { DMFloat x, y, z, W; } DMVector; typedef struct { DMFloat m[DM_MATRIX_SIZE][DM_MATRIX_SIZE]; } DMMatrix; void dm_vector_mul_by_mat_n(DMVector *list, const int nlist, const DMMatrix *mat); void dm_matrix_unit(DMMatrix *mat); void dm_matrix_transpose(DMMatrix *mat1, const DMMatrix *mat2); void dm_matrix_mul(DMMatrix *mat1, const DMMatrix *mat2); void dm_matrix_mul_n(DMMatrix *list, const int nlist, const DMMatrix *mat); void dm_matrix_rot(DMMatrix *mat, const DMFloat sx, const DMFloat sy, const DMFloat sz, const DMFloat cx, const DMFloat cy, const DMFloat cz); static inline void dm_vector_copy(DMVector *vd, const DMVector *vs) { memcpy(vd, vs, sizeof(DMVector)); } /* Basic vector operations */ static inline void dm_vector_add(DMVector *vr, const DMVector *v2) { #ifdef DM_USE_SIMD asm("movups %2, %%xmm1\n" "movups %1, %%xmm2\n" "addps %%xmm2, %%xmm1\n" "movups %%xmm1, %0\n" : "=m" (*vr) : "m" (*vr), "m" (*v2) : "memory", "%xmm1", "%xmm2"); #else vr->x += v2->x; vr->y += v2->y; vr->z += v2->z; #endif } static inline void dm_vector_add_r(DMVector *vr, const DMVector *v1, const DMVector *v2) { #ifdef DM_USE_SIMD asm("movups %2, %%xmm1\n" "movups %1, %%xmm2\n" "addps %%xmm2, %%xmm1\n" "movups %%xmm1, %0\n" : "=m" (*vr) : "m" (*v1), "m" (*v2) : "memory", "%xmm1", "%xmm2"); #else vr->x = v1->x + v2->x; vr->y = v1->y + v2->y; vr->z = v1->z + v2->z; #endif } static inline void dm_vector_sub(DMVector *vr, const DMVector *v2) { #ifdef DM_USE_SIMD asm("movups %2, %%xmm1\n" "movups %1, %%xmm2\n" "subps %%xmm2, %%xmm1\n" "movups %%xmm1, %0\n" : "=m" (*vr) : "m" (*vr), "m" (*v2) : "memory", "%xmm1", "%xmm2"); #else vr->x -= v2->x; vr->y -= v2->y; vr->z -= v2->z; #endif } static inline void dm_vector_sub_r(DMVector *vr, const DMVector *v1, const DMVector *v2) { #ifdef DM_USE_SIMD asm("movups %2, %%xmm1\n" "movups %1, %%xmm2\n" "subps %%xmm2, %%xmm1\n" "movups %%xmm1, %0\n" : "=m" (*vr) : "m" (*v1), "m" (*v2) : "memory", "%xmm1", "%xmm2"); #else vr->x = v1->x - v2->x; vr->y = v1->y - v2->y; vr->z = v1->z - v2->z; #endif } /* Returns dot-product of two given vectors */ static inline DMFloat dm_vector_dot(const DMVector *v1, const DMVector *v2) { return (v1->x * v2->x) + (v1->y * v2->y) + (v1->z * v2->z); } /* Return vector length */ static inline DMFloat dm_vector_length(const DMVector *vs) { return sqrt((vs->x * vs->x) + (vs->y * vs->y) + (vs->z * vs->z)); } /* Normalize vector */ static inline void dm_vector_normalize(DMVector *vec) { DMFloat l = dm_vector_length(vec); if (l > 0.0f) { l = 1.0f / l; vec->x *= l; vec->y *= l; vec->z *= l; } } /* Scale given vector */ static inline void dm_vector_scale(DMVector * vec, const DMFloat k) { vec->x *= k; vec->y *= k; vec->z *= k; } /* Returns cross-product of two given vectors */ static inline void dm_vector_cross(DMVector *vr, const DMVector *v1, const DMVector *v2) { vr->x = (v1->y * v2->z) - (v1->z * v2->y); vr->y = (v1->z * v2->x) - (v1->x * v2->z); vr->z = (v1->x * v2->y) - (v1->y * v2->x); } /* Multiply given vector with a matrix */ static inline void dm_vector_mul_by_mat(DMVector *vd, const DMVector *vs, const DMMatrix *mat) { vd->x = (vs->x * mat->m[0][0]) + (vs->y * mat->m[1][0]) + (vs->z * mat->m[2][0]); vd->y = (vs->x * mat->m[0][1]) + (vs->y * mat->m[1][1]) + (vs->z * mat->m[2][1]); vd->z = (vs->x * mat->m[0][2]) + (vs->y * mat->m[1][2]) + (vs->z * mat->m[2][2]); } /* Make rotation matrix from given angles (radians) */ static inline void dm_matrix_rot_a(DMMatrix *mat, const DMFloat ax, const DMFloat ay, const DMFloat az) { dm_matrix_rot(mat, sin(ax), sin(ay), sin(az), cos(ax), cos(ay), cos(az)); } #ifdef __cplusplus } #endif #endif // DMVECMAT_H