-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecalc.c
144 lines (113 loc) · 3.1 KB
/
precalc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
precalc.c - precalculation of floting point values
(c) 2003 by Ole Reinhardt <ole.reinhardt@kernelconcepts.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "rosetta.h"
static short angle[PRECALC_ANGLE_SIZE_X][PRECALC_ANGLE_SIZE_Y];
void precalc(void)
{
int x;
int y;
int xcalc = 0;
int ycalc = 0;
double val;
for (x = 0; x < PRECALC_ANGLE_SIZE_X; x++)
for (y = 0; y < PRECALC_ANGLE_SIZE_Y; y++)
{
xcalc = x - PRECALC_ANGLE_OFFS_X;
ycalc = y - PRECALC_ANGLE_OFFS_Y;
val = atan(ycalc/(xcalc+0.0001));
if (xcalc<0) val = M_PI + val;
if (val<0) val = 2*M_PI + val;
if (val>M_PI) val = -2*M_PI + val;
angle[x][y] = val / M_PI * NORM_ERROR;
}
}
int load_precalc(char *filename)
{
int fd;
int count;
struct stat filestat;
fd = open(filename, O_RDONLY);
if (fd==-1)
{
perror("Error loading precalculations: ");
return FALSE;
}
fstat(fd, &filestat);
if (filestat.st_size != sizeof(angle))
{
fprintf(stderr, "Error loading precalculations: Wrong filesize\n");
close(fd);
return FALSE;
}
for (count=0; count<PRECALC_ANGLE_SIZE_X; count++)
{
if (sizeof(short)*PRECALC_ANGLE_SIZE_Y != read(fd, angle[count], sizeof(short)*PRECALC_ANGLE_SIZE_Y))
{
perror("Error loading precalculations: ");
close(fd);
return FALSE;
};
}
close(fd);
return TRUE;
}
int save_precalc(char *filename)
{
int fd;
int count;
fd=open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd==-1)
{
perror("Error writing precalculations: ");
return FALSE;
}
for (count=0; count<PRECALC_ANGLE_SIZE_X; count++)
{
if (sizeof(short)*PRECALC_ANGLE_SIZE_Y != write(fd, angle[count], sizeof(short)*PRECALC_ANGLE_SIZE_Y))
{
perror("Error writing precalculations: ");
close(fd);
return FALSE;
};
}
close(fd);
return TRUE;
}
inline int get_angle(int x1, int y1, int x2, int y2)
{
int xcalc = 0;
int ycalc = 0;
double val;
// printf("%d\n", angle[x2-x1+PRECALC_ANGLE_OFFS][y1-y2+PRECALC_ANGLE_OFFS]);
if (flag_use_precalc) return(angle[x2-x1+PRECALC_ANGLE_OFFS_X][y1-y2+PRECALC_ANGLE_OFFS_Y]); else
{
xcalc = x2-x1;
ycalc = y1-y2;
val = atan(ycalc/(xcalc+0.0001));
if (xcalc<0) val = M_PI + val;
if (val<0) val = 2*M_PI + val;
if (val>M_PI) val = -2*M_PI + val;
return val / M_PI * NORM_ERROR;
}
}