/
neighbors.c
153 lines (137 loc) · 3.83 KB
/
neighbors.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
145
146
147
148
149
150
151
152
/*
* neighbor.c
*
* Created on: 17.01.2017
* Author: hoene
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <float.h>
#include "mysofa_export.h"
#include "mysofa.h"
#include "tools.h"
MYSOFA_EXPORT struct MYSOFA_NEIGHBORHOOD *mysofa_neighborhood_init(struct MYSOFA_HRTF *hrtf,
struct MYSOFA_LOOKUP *lookup) {
return mysofa_neighborhood_init_withstepdefine(hrtf,lookup,MYSOFA_DEFAULT_NEIGH_STEP_ANGLE,MYSOFA_DEFAULT_NEIGH_STEP_RADIUS);
}
MYSOFA_EXPORT struct MYSOFA_NEIGHBORHOOD *mysofa_neighborhood_init_withstepdefine(struct MYSOFA_HRTF *hrtf,
struct MYSOFA_LOOKUP *lookup,
float angleStep,
float radiusStep) {
int i, index;
float *origin, *test;
float radius, radius2;
float theta, theta2;
float phi, phi2;
struct MYSOFA_NEIGHBORHOOD *neighbor = malloc(
sizeof(struct MYSOFA_NEIGHBORHOOD));
if (!neighbor)
return NULL;
neighbor->elements = hrtf->M;
neighbor->index = malloc(sizeof(int) * neighbor->elements * 6);
if (!neighbor->index) {
free(neighbor);
return NULL;
}
for (i = 0; i < neighbor->elements * 6; i++)
neighbor->index[i] = -1;
origin = malloc(sizeof(float)*hrtf->C);
test = malloc(sizeof(float)*hrtf->C);
for (i = 0; i < hrtf->M; i ++) {
memcpy(origin, hrtf->SourcePosition.values + i * hrtf->C, sizeof(float) * hrtf->C);
convertCartesianToSpherical(origin, hrtf->C);
phi = angleStep;
do {
phi2 = test[0] = origin[0] + phi;
test[1] = origin[1];
test[2] = origin[2];
convertSphericalToCartesian(test, 3);
index = mysofa_lookup(lookup, test);
if (index != i) {
neighbor->index[i * 6 + 0] = index;
break;
}
phi += angleStep;
} while (phi2 <= lookup->phi_max + angleStep);
phi = -angleStep;
do {
phi2 = test[0] = origin[0] + phi;
test[1] = origin[1];
test[2] = origin[2];
convertSphericalToCartesian(test,3);
index = mysofa_lookup(lookup, test);
if (index != i) {
neighbor->index[i * 6 + 1] = index;
break;
}
phi -= angleStep;
} while (phi2 >= lookup->phi_min - angleStep);
theta = angleStep;
do {
test[0] = origin[0];
theta2 = test[1] = origin[1] + theta;
test[2] = origin[2];
convertSphericalToCartesian(test, 3);
index = mysofa_lookup(lookup, test);
if (index != i) {
neighbor->index[i * 6 + 2] = index;
break;
}
theta += angleStep;
} while (theta2 <= lookup->theta_max + angleStep);
theta = -angleStep;
do {
test[0] = origin[0];
theta2 = test[1] = origin[1] + theta;
test[2] = origin[2];
convertSphericalToCartesian(test, 3);
index = mysofa_lookup(lookup, test);
if (index != i) {
neighbor->index[i * 6 + 3] = index;
break;
}
theta -= angleStep;
} while (theta2 >= lookup->theta_min - angleStep);
radius = radiusStep;
do {
test[0] = origin[0];
test[1] = origin[1];
radius2 = test[2] = origin[2] + radius;
convertSphericalToCartesian(test, 3);
index = mysofa_lookup(lookup, test);
if (index != i) {
neighbor->index[i * 6 + 4] = index;
break;
}
radius += radiusStep;
} while (radius2 <= lookup->radius_max + radiusStep);
radius = -radiusStep;
do {
test[0] = origin[0];
test[1] = origin[1];
radius2 = test[2] = origin[2] + radius;
convertSphericalToCartesian(test, 3);
index = mysofa_lookup(lookup, test);
if (index != i) {
neighbor->index[i * 6 + 5] = index;
break;
}
radius -= radiusStep;
} while (radius2 >= lookup->radius_min - radiusStep);
}
free(test);
free(origin);
return neighbor;
}
MYSOFA_EXPORT int* mysofa_neighborhood(struct MYSOFA_NEIGHBORHOOD *neighborhood, int index) {
if (index < 0 || index >= neighborhood->elements)
return NULL ;
return neighborhood->index + index * 6;
}
MYSOFA_EXPORT void mysofa_neighborhood_free(struct MYSOFA_NEIGHBORHOOD *neighborhood) {
if(neighborhood) {
free(neighborhood->index);
free(neighborhood);
}
}