## istereo2

### view libs/vmath/vmath.inl @ 13:ea928c313344

foo
author John Tsiombikas Mon, 28 Sep 2015 19:04:50 +0300
line source
1 /*
2 libvmath - a vector math library
3 Copyright (C) 2004-2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <stdlib.h>
21 /** Generates a random number in [0, range) */
22 static inline scalar_t frand(scalar_t range)
23 {
24 return range * (scalar_t)rand() / (scalar_t)RAND_MAX;
25 }
27 /** Generates a random vector on the surface of a sphere */
28 static inline vec3_t sphrand(scalar_t rad)
29 {
30 scalar_t u = (scalar_t)rand() / RAND_MAX;
31 scalar_t v = (scalar_t)rand() / RAND_MAX;
33 scalar_t theta = 2.0 * M_PI * u;
34 scalar_t phi = acos(2.0 * v - 1.0);
36 vec3_t res;
37 res.x = rad * cos(theta) * sin(phi);
38 res.y = rad * sin(theta) * sin(phi);
39 res.z = rad * cos(phi);
40 return res;
41 }
43 /** linear interpolation */
44 static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t)
45 {
46 return a + (b - a) * t;
47 }