deepstone

diff src/record.c @ 38:17a5107b6fa4

- added perspective correct interpolation - added recording functionality - added video capture functionality
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 10 Mar 2014 17:24:42 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/record.c	Mon Mar 10 17:24:42 2014 +0200
     1.3 @@ -0,0 +1,135 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <errno.h>
     1.8 +#include "record.h"
     1.9 +
    1.10 +struct event {
    1.11 +	unsigned long tm;
    1.12 +	float x, y, z;
    1.13 +	float theta, phi;
    1.14 +
    1.15 +	struct event *next;
    1.16 +};
    1.17 +
    1.18 +static struct event *head, *tail;
    1.19 +static struct event *iter;
    1.20 +static int num_events;
    1.21 +
    1.22 +void rec_reset(void)
    1.23 +{
    1.24 +	while(head) {
    1.25 +		iter = head;
    1.26 +		head = head->next;
    1.27 +		free(iter);
    1.28 +	}
    1.29 +	num_events = 0;
    1.30 +}
    1.31 +
    1.32 +int rec_load(const char *fname)
    1.33 +{
    1.34 +	FILE *fp;
    1.35 +	int i;
    1.36 +
    1.37 +	if(!(fp = fopen(fname, "rb"))) {
    1.38 +		fprintf(stderr, "failed to open recording: %s: %s\n", fname, strerror(errno));
    1.39 +		return -1;
    1.40 +	}
    1.41 +	fread(&num_events, sizeof num_events, 1, fp);
    1.42 +
    1.43 +	head = tail = 0;
    1.44 +
    1.45 +	for(i=0; i<num_events; i++) {
    1.46 +		struct event *ev = malloc(sizeof *ev);
    1.47 +		if(!ev) {
    1.48 +			perror("failed to allocate event structure");
    1.49 +			fclose(fp);
    1.50 +			return -1;
    1.51 +		}
    1.52 +		if(fread(ev, sizeof *ev, 1, fp) < 1) {
    1.53 +			perror("unexpected end of file");
    1.54 +			fclose(fp);
    1.55 +			return -1;
    1.56 +		}
    1.57 +
    1.58 +		if(!head) {
    1.59 +			head = tail = ev;
    1.60 +		} else {
    1.61 +			tail->next = ev;
    1.62 +			tail = ev;
    1.63 +		}
    1.64 +		ev->next = 0;
    1.65 +	}
    1.66 +	iter = 0;
    1.67 +
    1.68 +	fclose(fp);
    1.69 +	return 0;
    1.70 +}
    1.71 +
    1.72 +int rec_save(const char *fname)
    1.73 +{
    1.74 +	FILE *fp;
    1.75 +	struct event *ev;
    1.76 +
    1.77 +	if(!(fp = fopen(fname, "wb"))) {
    1.78 +		fprintf(stderr, "failed to open recording: %s: %s\n", fname, strerror(errno));
    1.79 +		return -1;
    1.80 +	}
    1.81 +	fwrite(&num_events, sizeof num_events, 1, fp);
    1.82 +
    1.83 +	ev = head;
    1.84 +	while(ev) {
    1.85 +		fwrite(ev, sizeof *ev, 1, fp);
    1.86 +		ev = ev->next;
    1.87 +	}
    1.88 +
    1.89 +	fclose(fp);
    1.90 +	return 0;
    1.91 +}
    1.92 +
    1.93 +void rec_add(unsigned long tm, float x, float y, float z, float theta, float phi)
    1.94 +{
    1.95 +	struct event *ev = malloc(sizeof *ev);
    1.96 +	if(!ev) {
    1.97 +		fprintf(stderr, "failed to append rec-event: %s\n", strerror(errno));
    1.98 +		return;
    1.99 +	}
   1.100 +	ev->tm = tm;
   1.101 +	ev->x = x;
   1.102 +	ev->y = y;
   1.103 +	ev->z = z;
   1.104 +	ev->theta = theta;
   1.105 +	ev->phi = phi;
   1.106 +
   1.107 +	ev->next = 0;
   1.108 +	if(head) {
   1.109 +		tail->next = ev;
   1.110 +		tail = ev;
   1.111 +	} else {
   1.112 +		head = tail = ev;
   1.113 +	}
   1.114 +
   1.115 +	num_events++;
   1.116 +}
   1.117 +
   1.118 +void rec_get(unsigned long tm, float *x, float *y, float *z, float *theta, float *phi)
   1.119 +{
   1.120 +	if(!iter || iter->tm > tm) {
   1.121 +		iter = head;
   1.122 +	}
   1.123 +
   1.124 +	while(iter && iter->next) {
   1.125 +		if(iter->next->tm > tm) {
   1.126 +			break;
   1.127 +		}
   1.128 +		iter = iter->next;
   1.129 +	}
   1.130 +
   1.131 +	if(iter) {
   1.132 +		*x = iter->x;
   1.133 +		*y = iter->y;
   1.134 +		*z = iter->z;
   1.135 +		*theta = iter->theta;
   1.136 +		*phi = iter->phi;
   1.137 +	}
   1.138 +}