| 1 |
/* |
| 2 |
# This file is part of the Astrometry.net suite. |
| 3 |
# Licensed under a 3-clause BSD style license - see LICENSE |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <math.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <string.h> |
| 9 |
#include <assert.h> |
| 10 |
#include <stdlib.h> |
| 11 |
|
| 12 |
#include "mathutil.h" |
| 13 |
#include "keywords.h" |
| 14 |
|
| 15 |
#define InlineDefine InlineDefineC |
| 16 |
#include "mathutil.inc" |
| 17 |
#undef InlineDefine |
| 18 |
|
| 19 |
#include "bl.h" |
| 20 |
|
| 21 |
/** |
| 22 |
Returns 1 if the given point is inside the given polygon |
| 23 |
(listed as x0,y0, x1,y1, etc). |
| 24 |
*/ |
| 25 |
int point_in_polygon(double x, double y, const dl* polygon) { |
| 26 |
size_t i; |
| 27 |
size_t N = dl_size(polygon) / 2; |
| 28 |
int inside = 0; |
| 29 |
for (i=0; i<N; i++) { |
| 30 |
size_t j = (i - 1 + N) % N; |
| 31 |
double xi, xj, yi, yj; |
| 32 |
yi = dl_get_const(polygon, i*2+1); |
| 33 |
yj = dl_get_const(polygon, j*2+1); |
| 34 |
if (yi == yj) |
| 35 |
continue; |
| 36 |
xi = dl_get_const(polygon, i*2+0); |
| 37 |
xj = dl_get_const(polygon, j*2+0); |
| 38 |
if ( |
| 39 |
( ((yi <= y) && (y < yj)) || |
| 40 |
((yj <= y) && (y < yi)) ) && |
| 41 |
(x < (xi + ((xj - xi) * (y - yi) / (yj - yi)))) |
| 42 |
) { |
| 43 |
// toggle |
| 44 |
inside = 1-inside; |
| 45 |
} |
| 46 |
} |
| 47 |
return inside; |
| 48 |
} |
| 49 |
|
| 50 |
int get_output_image_size(int W, int H, int S, |
| 51 |
int edgehandling, |
| 52 |
int* newW, int* newH) { |
| 53 |
int outw, outh; |
| 54 |
if (S < 2) { |
| 55 |
printf("Need scale >= 2"); |
| 56 |
return -1; |
| 57 |
} |
| 58 |
if (edgehandling == 0) { |
| 59 |
// truncate. |
| 60 |
outw = W / S; |
| 61 |
outh = H / S; |
| 62 |
} else if (edgehandling == 1) { |
| 63 |
// average |
| 64 |
outw = (W + S-1) / S; |
| 65 |
outh = (H + S-1) / S; |
| 66 |
} else { |
| 67 |
printf("Unknown edge handling code %i", edgehandling); |
| 68 |
return -1; |
| 69 |
} |
| 70 |
if (newW) |
| 71 |
*newW = outw; |
| 72 |
if (newH) |
| 73 |
*newH = outh; |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
float* average_image_f(const float* image, int W, int H, |
| 78 |
int S, int edgehandling, |
| 79 |
int* newW, int* newH, |
| 80 |
float* output) { |
| 81 |
return average_weighted_image_f(image, NULL, W, H, S, edgehandling, |
| 82 |
newW, newH, output, 0.0); |
| 83 |
} |
| 84 |
|
| 85 |
float* average_weighted_image_f(const float* image, const float* weight, |
| 86 |
int W, int H, int S, int edgehandling, |
| 87 |
int* newW, int* newH, float* output, float nilval) { |
| 88 |
int outw, outh; |
| 89 |
int i,j; |
| 90 |
if (get_output_image_size(W, H, S, edgehandling, &outw, &outh)) |
| 91 |
return NULL; |
| 92 |
if (output == NULL) { |
| 93 |
output = malloc(outw * outh * sizeof(float)); |
| 94 |
if (!output) { |
| 95 |
printf("Failed to allocate %i x %i floats", outw, outh); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
} |
| 99 |
for (j=0; j<outh; j++) { |
| 100 |
for (i=0; i<outw; i++) { |
| 101 |
float sum = 0.0; |
| 102 |
float wsum = 0.0; |
| 103 |
int I, J; |
| 104 |
int ii; |
| 105 |
for (J=0; J<S; J++) { |
| 106 |
if (j*S + J >= H) |
| 107 |
break; |
| 108 |
for (I=0; I<S; I++) { |
| 109 |
if (i*S + I >= W) |
| 110 |
break; |
| 111 |
ii = (j*S + J)*W + (i*S + I); |
| 112 |
if (weight) { |
| 113 |
wsum += weight[ii]; |
| 114 |
sum += image[ii] * weight[ii]; |
| 115 |
} else { |
| 116 |
wsum += 1.0; |
| 117 |
sum += image[ii]; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
if (wsum == 0.0) |
| 122 |
output[j * outw + i] = nilval; |
| 123 |
else |
| 124 |
output[j * outw + i] = sum / wsum; |
| 125 |
} |
| 126 |
} |
| 127 |
if (newW) |
| 128 |
*newW = outw; |
| 129 |
if (newH) |
| 130 |
*newH = outh; |
| 131 |
return output; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
// "borrowed" from <linux/bitops.h> from linux-2.4 |
| 137 |
static unsigned int my_hweight32(unsigned int w) { |
| 138 |
unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); |
| 139 |
res = (res & 0x33333333) + ((res >> 2) & 0x33333333); |
| 140 |
res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); |
| 141 |
res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); |
| 142 |
return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); |
| 143 |
} |
| 144 |
|
| 145 |
int invert_2by2_arr(const double* A, double* Ainv) { |
| 146 |
double det; |
| 147 |
double inv_det; |
| 148 |
det = A[0] * A[3] - A[1] * A[2]; |
| 149 |
if (det == 0.0) |
| 150 |
return -1; |
| 151 |
inv_det = 1.0 / det; |
| 152 |
Ainv[0] = A[3] * inv_det; |
| 153 |
Ainv[1] = -A[1] * inv_det; |
| 154 |
Ainv[2] = -A[2] * inv_det; |
| 155 |
Ainv[3] = A[0] * inv_det; |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
int invert_2by2(const double A[2][2], double Ainv[2][2]) { |
| 160 |
double det; |
| 161 |
double inv_det; |
| 162 |
det = A[0][0] * A[1][1] - A[0][1] * A[1][0]; |
| 163 |
if (det == 0.0) |
| 164 |
return -1; |
| 165 |
inv_det = 1.0 / det; |
| 166 |
Ainv[0][0] = A[1][1] * inv_det; |
| 167 |
Ainv[0][1] = -A[0][1] * inv_det; |
| 168 |
Ainv[1][0] = -A[1][0] * inv_det; |
| 169 |
Ainv[1][1] = A[0][0] * inv_det; |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
void tan_vectors(const double* pt, double* vec1, double* vec2) { |
| 174 |
double etax, etay, etaz, xix, xiy, xiz, eta_norm; |
| 175 |
double inv_en; |
| 176 |
// eta is a vector perpendicular to pt |
| 177 |
etax = -pt[1]; |
| 178 |
etay = pt[0]; |
| 179 |
etaz = 0.0; |
| 180 |
eta_norm = hypot(etax, etay); //sqrt(etax * etax + etay * etay); |
| 181 |
inv_en = 1.0 / eta_norm; |
| 182 |
etax *= inv_en; |
| 183 |
etay *= inv_en; |
| 184 |
|
| 185 |
vec1[0] = etax; |
| 186 |
vec1[1] = etay; |
| 187 |
vec1[2] = etaz; |
| 188 |
|
| 189 |
// xi = pt cross eta |
| 190 |
xix = -pt[2] * etay; |
| 191 |
xiy = pt[2] * etax; |
| 192 |
xiz = pt[0] * etay - pt[1] * etax; |
| 193 |
// xi has unit length since pt and eta have unit length. |
| 194 |
|
| 195 |
vec2[0] = xix; |
| 196 |
vec2[1] = xiy; |
| 197 |
vec2[2] = xiz; |
| 198 |
} |
| 199 |
|
| 200 |
int is_power_of_two(unsigned int x) { |
| 201 |
return (my_hweight32(x) == 1); |
| 202 |
} |
| 203 |
|
| 204 |
double vector_length_3(double* v) { |
| 205 |
return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); |
| 206 |
} |
| 207 |
|
| 208 |
double vector_length_squared_3(double* v) { |
| 209 |
return v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; |
| 210 |
} |
| 211 |
|
| 212 |
double dot_product_3(double* va, double* vb) { |
| 213 |
return va[0]*vb[0] + va[1]*vb[1] + va[2]*vb[2]; |
| 214 |
} |
| 215 |
|
| 216 |
void matrix_matrix_3(double* ma, double* mb, double* result) { |
| 217 |
result[0] = ma[0]*mb[0] + ma[1]*mb[3] + ma[2]*mb[6]; |
| 218 |
result[3] = ma[3]*mb[0] + ma[4]*mb[3] + ma[5]*mb[6]; |
| 219 |
result[6] = ma[6]*mb[0] + ma[7]*mb[3] + ma[8]*mb[6]; |
| 220 |
|
| 221 |
result[1] = ma[0]*mb[1] + ma[1]*mb[4] + ma[2]*mb[7]; |
| 222 |
result[4] = ma[3]*mb[1] + ma[4]*mb[4] + ma[5]*mb[7]; |
| 223 |
result[7] = ma[6]*mb[1] + ma[7]*mb[4] + ma[8]*mb[7]; |
| 224 |
|
| 225 |
result[2] = ma[0]*mb[2] + ma[1]*mb[5] + ma[2]*mb[8]; |
| 226 |
result[5] = ma[3]*mb[2] + ma[4]*mb[5] + ma[5]*mb[8]; |
| 227 |
result[8] = ma[6]*mb[2] + ma[7]*mb[5] + ma[8]*mb[8]; |
| 228 |
} |
| 229 |
|
| 230 |
void matrix_vector_3(double* m, double* v, double* result) { |
| 231 |
result[0] = m[0]*v[0] + m[1]*v[1] + m[2]*v[2]; |
| 232 |
result[1] = m[3]*v[0] + m[4]*v[1] + m[5]*v[2]; |
| 233 |
result[2] = m[6]*v[0] + m[7]*v[1] + m[8]*v[2]; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
#define GAUSSIAN_SAMPLE_INVALID -1e300 |
| 238 |
|
| 239 |
double gaussian_sample(double mean, double stddev) { |
| 240 |
// from http://www.taygeta.com/random/gaussian.html |
| 241 |
// Algorithm by Dr. Everett (Skip) Carter, Jr. |
| 242 |
static double y2 = GAUSSIAN_SAMPLE_INVALID; |
| 243 |
double x1, x2, w, y1; |
| 244 |
|
| 245 |
// this algorithm generates random samples in pairs; the INVALID |
| 246 |
// jibba-jabba stores the second value until the next time the |
| 247 |
// function is called. |
| 248 |
|
| 249 |
if (y2 != GAUSSIAN_SAMPLE_INVALID) { |
| 250 |
y1 = y2; |
| 251 |
y2 = GAUSSIAN_SAMPLE_INVALID; |
| 252 |
return mean + y1 * stddev; |
| 253 |
} |
| 254 |
do { |
| 255 |
x1 = uniform_sample(-1, 1); |
| 256 |
x2 = uniform_sample(-1, 1); |
| 257 |
w = x1 * x1 + x2 * x2; |
| 258 |
} while ( w >= 1.0 ); |
| 259 |
|
| 260 |
w = sqrt( (-2.0 * log(w)) / w ); |
| 261 |
y1 = x1 * w; |
| 262 |
y2 = x2 * w; |
| 263 |
return mean + y1 * stddev; |
| 264 |
} |
| 265 |
|
| 266 |
double uniform_sample(double low, double high) { |
| 267 |
if (low == high) return low; |
| 268 |
return low + (high - low)*((double)rand() / (double)RAND_MAX); |
| 269 |
} |
| 270 |
|
| 271 |
/* computes IN PLACE the inverse of a 3x3 matrix stored as a 9-vector |
| 272 |
with the first ROW of the matrix in positions 0-2, the second ROW |
| 273 |
in positions 3-5, and the last ROW in positions 6-8. */ |
| 274 |
double inverse_3by3(double *matrix) |
| 275 |
{ |
| 276 |
double det; |
| 277 |
double a11, a12, a13, a21, a22, a23, a31, a32, a33; |
| 278 |
double b11, b12, b13, b21, b22, b23, b31, b32, b33; |
| 279 |
|
| 280 |
a11 = matrix[0]; |
| 281 |
a12 = matrix[1]; |
| 282 |
a13 = matrix[2]; |
| 283 |
a21 = matrix[3]; |
| 284 |
a22 = matrix[4]; |
| 285 |
a23 = matrix[5]; |
| 286 |
a31 = matrix[6]; |
| 287 |
a32 = matrix[7]; |
| 288 |
a33 = matrix[8]; |
| 289 |
|
| 290 |
det = a11 * ( a22 * a33 - a23 * a32 ) + |
| 291 |
a12 * ( a23 * a31 - a21 * a33 ) + |
| 292 |
a13 * ( a21 * a32 - a22 * a31 ); |
| 293 |
|
| 294 |
if (det == 0.0) { |
| 295 |
return det; |
| 296 |
} |
| 297 |
|
| 298 |
b11 = + ( a22 * a33 - a23 * a32 ) / det; |
| 299 |
b12 = - ( a12 * a33 - a13 * a32 ) / det; |
| 300 |
b13 = + ( a12 * a23 - a13 * a22 ) / det; |
| 301 |
|
| 302 |
b21 = - ( a21 * a33 - a23 * a31 ) / det; |
| 303 |
b22 = + ( a11 * a33 - a13 * a31 ) / det; |
| 304 |
b23 = - ( a11 * a23 - a13 * a21 ) / det; |
| 305 |
|
| 306 |
b31 = + ( a21 * a32 - a22 * a31 ) / det; |
| 307 |
b32 = - ( a11 * a32 - a12 * a31 ) / det; |
| 308 |
b33 = + ( a11 * a22 - a12 * a21 ) / det; |
| 309 |
|
| 310 |
matrix[0] = b11; |
| 311 |
matrix[1] = b12; |
| 312 |
matrix[2] = b13; |
| 313 |
matrix[3] = b21; |
| 314 |
matrix[4] = b22; |
| 315 |
matrix[5] = b23; |
| 316 |
matrix[6] = b31; |
| 317 |
matrix[7] = b32; |
| 318 |
matrix[8] = b33; |
| 319 |
|
| 320 |
//fprintf(stderr,"matrix determinant = %g\n",det); |
| 321 |
|
| 322 |
return (det); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
void image_to_xyz(double uu, double vv, double* s, double* transform) { |
| 327 |
double x, y, z; |
| 328 |
double length; |
| 329 |
assert(s); |
| 330 |
assert(transform); |
| 331 |
|
| 332 |
x = uu * transform[0] + |
| 333 |
vv * transform[1] + |
| 334 |
transform[2]; |
| 335 |
|
| 336 |
y = uu * transform[3] + |
| 337 |
vv * transform[4] + |
| 338 |
transform[5]; |
| 339 |
|
| 340 |
z = uu * transform[6] + |
| 341 |
vv * transform[7] + |
| 342 |
transform[8]; |
| 343 |
|
| 344 |
length = sqrt(x*x + y*y + z*z); |
| 345 |
|
| 346 |
x /= length; |
| 347 |
y /= length; |
| 348 |
z /= length; |
| 349 |
|
| 350 |
s[0] = x; |
| 351 |
s[1] = y; |
| 352 |
s[2] = z; |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
star = trans * (field; 1) |
| 357 |
|
| 358 |
star is 3 x N |
| 359 |
field is 2 x N and is supplemented by a row of ones. |
| 360 |
trans is 3 x 3 |
| 361 |
|
| 362 |
"star" and "field" are stored in lexicographical order, so the element at |
| 363 |
(r, c) is stored at array[r + c*H], where H is the "height" of the |
| 364 |
matrix: 3 for "star", 2 for "field". |
| 365 |
|
| 366 |
"trans" is stored in the transposed order (to be compatible with |
| 367 |
image_to_xyz and the previous version of this function). |
| 368 |
|
| 369 |
S = T F |
| 370 |
S F' = T F F' |
| 371 |
S F' (F F')^-1 = T |
| 372 |
*/ |
| 373 |
void fit_transform(double* star, double* field, int N, double* trans) { |
| 374 |
int r, c, k; |
| 375 |
double FFt[9]; |
| 376 |
double det; |
| 377 |
double* R; |
| 378 |
double* F; |
| 379 |
|
| 380 |
// build F = (field; ones) |
| 381 |
F = malloc(3 * N * sizeof(double)); |
| 382 |
for (c=0; c<N; c++) { |
| 383 |
// row 0 |
| 384 |
F[0 + c*3] = field[0 + c*2]; |
| 385 |
// row 1 |
| 386 |
F[1 + c*3] = field[1 + c*2]; |
| 387 |
// row 2 |
| 388 |
F[2 + c*3] = 1.0; |
| 389 |
} |
| 390 |
|
| 391 |
// first compute FFt = F * F' |
| 392 |
// (3x3) = (3xN) * (Nx3) |
| 393 |
for (r=0; r<3; r++) |
| 394 |
for (c=0; c<3; c++) { |
| 395 |
// FFt(r,c) = sum_k F(r,k) * F'(k,c) |
| 396 |
// = sum_k F(r,k) * F(c,k) |
| 397 |
// FFt[r+c*3] = sum_k F[r + k*3] * F[c + k*3]. |
| 398 |
double acc = 0.0; |
| 399 |
for (k=0; k<N; k++) |
| 400 |
acc += F[r + k*3] * F[c + k*3]; |
| 401 |
FFt[r + c*3] = acc; |
| 402 |
} |
| 403 |
|
| 404 |
// invert FFt in-place. |
| 405 |
det = inverse_3by3(FFt); |
| 406 |
|
| 407 |
if (det < 0) |
| 408 |
fprintf(stderr, "WARNING (fit_transform) -- determinant<0\n"); |
| 409 |
|
| 410 |
if (det == 0.0) { |
| 411 |
fprintf(stderr, "ERROR (fit_transform) -- determinant zero\n"); |
| 412 |
return; |
| 413 |
} |
| 414 |
|
| 415 |
R = malloc(N * 3 * sizeof(double)); |
| 416 |
|
| 417 |
// compute R = F' * FFt = F' inv(F F') |
| 418 |
// (Nx3) = (Nx3) * (3x3) |
| 419 |
for (r=0; r<N; r++) |
| 420 |
for (c=0; c<3; c++) { |
| 421 |
// R(r,c) = sum_k F'(r,k) * FFt(k,c) |
| 422 |
// = sum_k F(k,r) * FFt(k,c) |
| 423 |
// = sum_k F[k + r*3] * FFt[k + c*3] |
| 424 |
double acc = 0.0; |
| 425 |
for (k=0; k<3; k++) |
| 426 |
acc += F[k + r*3] * FFt[k + c*3]; |
| 427 |
R[r + c*N] = acc; |
| 428 |
} |
| 429 |
|
| 430 |
// finally, compute T = S * R = S F' inv(F F') |
| 431 |
// (3x3) = (3xN) * (Nx3) |
| 432 |
for (r=0; r<3; r++) |
| 433 |
for (c=0; c<3; c++) { |
| 434 |
// T(r,c) = sum_k S(r,k) * R(k,c) |
| 435 |
// = sum_k S[r + k*3] * R[k + c*N] |
| 436 |
double acc = 0.0; |
| 437 |
for (k=0; k<N; k++) |
| 438 |
acc += star[r + k*3] * R[k + c*N]; |
| 439 |
// "trans" is stored in transposed order. |
| 440 |
trans[c + r*3] = acc; |
| 441 |
} |
| 442 |
|
| 443 |
free(F); |
| 444 |
free(R); |
| 445 |
} |