2012-01-29 17:39:56 +00:00
|
|
|
#include "tools.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
#include <stdlib.h>
|
2012-02-26 11:28:47 +00:00
|
|
|
#include <inttypes.h>
|
2012-03-29 20:19:50 +00:00
|
|
|
#include <math.h>
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-01-29 17:39:56 +00:00
|
|
|
#include "color.h"
|
|
|
|
#include "euclid.h"
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double toolsRandom()
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
return ((double)rand()) / (double)RAND_MAX;
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-08-28 21:13:35 +00:00
|
|
|
static inline double _cubicInterpolate(double p[4], double x)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
return p[1] + 0.5 * x * (p[2] - p[0] + x * (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] + x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
|
|
|
|
}
|
|
|
|
|
2012-08-28 21:13:35 +00:00
|
|
|
double toolsCubicInterpolate(double stencil[4], double x)
|
|
|
|
{
|
|
|
|
return _cubicInterpolate(stencil, x);
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double toolsBicubicInterpolate(double stencil[16], double x, double y)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double buf_cubic_y[4];
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-08-28 21:13:35 +00:00
|
|
|
buf_cubic_y[0] = _cubicInterpolate(stencil, x);
|
|
|
|
buf_cubic_y[1] = _cubicInterpolate(stencil + 4, x);
|
|
|
|
buf_cubic_y[2] = _cubicInterpolate(stencil + 8, x);
|
|
|
|
buf_cubic_y[3] = _cubicInterpolate(stencil + 12, x);
|
2011-12-10 13:25:22 +00:00
|
|
|
|
2012-08-28 21:13:35 +00:00
|
|
|
return _cubicInterpolate(buf_cubic_y, y);
|
2011-12-10 13:25:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
void toolsFloat2DMapCopy(double* src, double* dest, int src_xstart, int src_ystart, int dest_xstart, int dest_ystart, int xsize, int ysize, int src_xstep, int src_ystep, int dest_xstep, int dest_ystep)
|
2011-12-10 13:25:22 +00:00
|
|
|
{
|
|
|
|
/* TODO Optimize with memcpy if src_xstep == dest_xstep == 1 */
|
|
|
|
int x, y;
|
2012-06-17 09:40:40 +00:00
|
|
|
double* src_row;
|
|
|
|
double* dest_row;
|
2011-12-10 13:25:22 +00:00
|
|
|
src += src_ystart * src_ystep + src_xstart * src_xstep;
|
|
|
|
dest += dest_ystart * dest_ystep + dest_xstart * dest_xstep;
|
|
|
|
for (y = 0; y < ysize; y++)
|
|
|
|
{
|
|
|
|
src_row = src;
|
|
|
|
dest_row = dest;
|
|
|
|
for (x = 0; x < xsize; x++)
|
|
|
|
{
|
|
|
|
*dest = *src;
|
|
|
|
src += src_xstep;
|
|
|
|
dest += dest_xstep;
|
|
|
|
}
|
|
|
|
src = src_row + src_ystep;
|
|
|
|
dest = dest_row + dest_ystep;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 toolsGetNormalFromTriangle(Vector3 center, Vector3 bottom, Vector3 right)
|
|
|
|
{
|
|
|
|
Vector3 dx = v3Sub(right, center);
|
|
|
|
Vector3 dz = v3Sub(bottom, center);
|
|
|
|
return v3Normalize(v3Cross(dz, dx));
|
|
|
|
}
|
|
|
|
|
2012-06-17 09:40:40 +00:00
|
|
|
double toolsGetDistance2D(double x1, double y1, double x2, double y2)
|
2012-03-29 20:19:50 +00:00
|
|
|
{
|
2012-06-17 09:40:40 +00:00
|
|
|
double dx = x2 - x1;
|
|
|
|
double dy = y2 - y1;
|
2012-03-29 20:19:50 +00:00
|
|
|
return sqrt(dx * dx + dy * dy);
|
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void materialSave(PackStream* stream, SurfaceMaterial* material)
|
2012-01-28 18:32:08 +00:00
|
|
|
{
|
2012-04-22 17:12:39 +00:00
|
|
|
colorSave(stream, &material->base);
|
2012-06-17 09:40:40 +00:00
|
|
|
packWriteDouble(stream, &material->reflection);
|
|
|
|
packWriteDouble(stream, &material->shininess);
|
2012-01-28 18:32:08 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 17:12:39 +00:00
|
|
|
void materialLoad(PackStream* stream, SurfaceMaterial* material)
|
2012-01-28 18:32:08 +00:00
|
|
|
{
|
2012-04-22 17:12:39 +00:00
|
|
|
colorLoad(stream, &material->base);
|
2012-06-17 09:40:40 +00:00
|
|
|
packReadDouble(stream, &material->reflection);
|
|
|
|
packReadDouble(stream, &material->shininess);
|
2012-01-28 18:32:08 +00:00
|
|
|
}
|