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 #ifndef SOLVER_H
7 #define SOLVER_H
8 
9 #include <time.h>
10 
11 #include "astrometry/starutil.h"
12 #include "astrometry/starxy.h"
13 #include "astrometry/kdtree.h"
14 #include "astrometry/bl.h"
15 #include "astrometry/matchobj.h"
16 #include "astrometry/quadfile.h"
17 #include "astrometry/starkd.h"
18 #include "astrometry/codekd.h"
19 #include "astrometry/index.h"
20 #include "astrometry/verify.h"
21 #include "astrometry/sip.h"
22 #include "astrometry/an-bool.h"
23 
24 enum {
25     PARITY_NORMAL,
26     PARITY_FLIP,
27     PARITY_BOTH
28 };
29 
30 #define DEFAULT_CODE_TOL .01
31 #define DEFAULT_PARITY PARITY_BOTH
32 #define DEFAULT_TWEAK_ABORDER 3
33 #define DEFAULT_TWEAK_ABPORDER 3
34 #define DEFAULT_DISTRACTOR_RATIO 0.25
35 #define DEFAULT_VERIFY_PIX 1.0
36 #define DEFAULT_BAIL_THRESHOLD 1e-100
37 
38 struct verify_field_t;
39 struct solver_t {
40 
41     // FIELDS REQUIRED FROM THE CALLER BEFORE CALLING SOLVER_RUN
42     // =========================================================
43 
44     // The set of indexes.  Caller must add with solver_add_index()
45     pl* indexes;
46 
47     // The field to solve
48     starxy_t* fieldxy;
49 
50     // Distortion pattern to apply before solving.
51     sip_t* predistort;
52 
53     // Limits on the image pixel scale in [arcsec per pixel].
54     double funits_lower;
55     double funits_upper;
56 
57     double logratio_toprint;
58     double logratio_tokeep;
59 
60     double logratio_totune;
61 
62     // Callback; called for each match found whose log-odds ratio is above
63     // "logratio_record_threshold".  The second parameter is "userdata".
64     anbool (*record_match_callback)(MatchObj*, void*);
65 
66     // User data passed to the callbacks
67     void* userdata;
68 
69     // Assume that stars far from the matched quad will have larger positional
70     // variance?
71     anbool distance_from_quad_bonus;
72 
73     anbool verify_uniformize;
74     anbool verify_dedup;
75 
76     anbool do_tweak;
77 
78     int tweak_aborder;
79     int tweak_abporder;
80 
81 
82     // OPTIONAL FIELDS WITH SENSIBLE DEFAULTS
83     // ======================================
84 
85     // The positional noise in the field, in pixels.
86     double verify_pix;
87 
88     // Fraction of distractors in [0,1].
89     double distractor_ratio;
90 
91     // Code tolerance in 4D codespace L2 distance.
92     double codetol;
93 
94     // Minimum size of field quads to try, in pixels.
95     double quadsize_min;
96     // Maximum size of field quads to try, in pixels.
97     double quadsize_max;
98 
99     // The first and last field objects to look at; default is all of them.
100     int startobj;
101     int endobj;
102 
103     // One of PARITY_NORMAL, PARITY_FLIP, or PARITY_BOTH.  Are the X and Y axes of
104     // the image flipped?  Default PARITY_BOTH.
105     int parity;
106 
107     // Only accept matches within a radius of a given RA,Dec position?
108     anbool use_radec;
109     double centerxyz[3];
110     double r2;
111 
112     // During verification, if the log-odds ratio drops to this level, we bail out and
113     // assume it's not a match.  Default log(1e-100).
114     double logratio_bail_threshold;
115 
116     // During verification, if the log-odds ratio rises above this level, we accept the
117     // match and bail out.  Default: HUGE_VAL (ie, don't bail out: keep going to find the
118     // maximum Bayes factor value).
119     double logratio_stoplooking;
120 
121     // Number of field quads to try or zero for no limit.
122     int maxquads;
123     // Number of quad matches to try or zero for no limit.
124     int maxmatches;
125 
126     // Force CRPIX to be the given point "crpix", or the center of the image?
127     anbool set_crpix;
128     anbool set_crpix_center;
129     double crpix[2];
130 
131     // MatchObj template: if non-NULL, whenever a match is found, we first memcpy()
132     // this template, then set the fields that describe the match.
133     MatchObj* mo_template;
134 
135     // Called after a delay in seconds; returns how long to wait before
136     // calling again.  The parameter is "userdata".
137     time_t (*timer_callback)(void*);
138 
139     // FIELDS THAT AFFECT THE RUNNING SOLVER ON CALLBACK
140     // =================================================
141 
142     // Bail out ASAP.
143     anbool quit_now;
144 
145     // SOLVER OUTPUTS
146     // ==============
147     // NOTE: these are only incremented, not initialized.  It's up to you to set
148     // them to zero before calling, if you're starting from scratch.
149     // See solver_reset_counters().
150     int numtries;
151     int nummatches;
152     int numscaleok;
153     // the last field object examined
154     int last_examined_object;
155     // number of quads skipped because of cxdx constraints.
156     int num_cxdx_skipped;
157     int num_meanx_skipped;
158     // number of matches skipped due to RA,Dec bounds constraints.
159     int num_radec_skipped;
160     //
161     int num_abscale_skipped;
162     // The number of times we ran verification on a quad.
163     int num_verified;
164 
165     // INTERNAL PARAMETERS; DO NOT MODIFY
166     // ==================================
167     // The index we're currently dealing with.
168     index_t* index;
169 
170     // The extreme limits of quad size, for all indexes, in pixels^2.
171     double minminAB2;
172     double maxmaxAB2;
173 
174     // The relative noise of the current index, squared:
175     // square( index->index_jitter / index->index_scale_lower )
176     double rel_index_noise2;
177 
178     // The relative noise of the current quad, squared:
179     double rel_field_noise2;
180 
181     double abscale_low;
182     double abscale_high;
183 
184     // Field limits, in pixels.
185     double field_minx, field_maxx, field_miny, field_maxy;
186     // Distance in pixels across the diagonal of the field
187     double field_diag;
188 
189     // If the index has the property that cx <= dx, then how much of a margin do we
190     // have to add before we can safely assume that a permutation of a quad's code
191     // can't be in the index?
192     double cxdx_margin;
193 
194     // How long has this been going on? (CPU time)
195     double starttime;
196     double timeused;
197 
198     // Best match so far
199     double   best_logodds;
200     MatchObj best_match;
201     index_t* best_index;
202     anbool     best_match_solves;
203     anbool     have_best_match;
204 
205     // Cached data about this field, for verify_hit().
206     verify_field_t* vf;
207 };
208 typedef struct solver_t solver_t;
209 
210 solver_t* solver_new();
211 
212 void solver_set_default_values(solver_t* solver);
213 
214 /**
215  Returns the assumed field positional uncertainty ("jitter")
216  in pixels.
217  */
218 double solver_get_field_jitter(const solver_t* solver);
219 
220 /**
221  Sets the log-odds ratio for "recording" a proposed solution.  NOTE,
222  'recording' means calling your callback, where you can decide what to
223  do with it; thus it's probably your "solve" threshold.
224 
225  Default: 0, which means your callback gets called for every match.
226 
227  Suggested value: log(1e9) or log(1e12).
228  */
229 void solver_set_keep_logodds(solver_t* solver, double logodds);
230 
231 // back-compate
232 #define solver_set_record_logodds solver_set_keep_logodds
233 
234 /**
235  Sets the "parity" or "flip" of the image.
236 
237  PARITY_NORMAL: det(CD) < 0
238  PARITY_FLIP:   det(CD) > 0
239  PARITY_BOTH (default): try both.
240 
241  Return 0 on success, -1 on invalid parity value.
242  */
243 int solver_set_parity(solver_t* solver, int parity);
244 
245 /**
246  Returns the pixel position of the center of the field, defined
247  to be the mean of the min and max position.
248  (field_maxx + field_maxy)/2
249  */
250 void solver_get_field_center(const solver_t* solver, double* px, double* py);
251 
252 /**
253  Returns the maximum field size expected, in arcsec.
254 
255  This is simply the maximum pixel scale * maximum radius (on the
256  diagonal)
257  */
258 double solver_get_max_radius_arcsec(const solver_t* solver);
259 
260 /**
261  Returns the best match found after a solver_run() call.
262 
263  This is just &(solver->best_match)
264  */
265 MatchObj* solver_get_best_match(solver_t* solver);
266 
267 /**
268  Did the best match solve?
269 
270  Returns solver->best_match_solved.
271  */
272 anbool solver_did_solve(const solver_t* solver);
273 
274 /**
275  Returns solver->best_index->indexname.
276 
277  (Should be equal to solver->best_match->index->indexname.
278  */
279 const char* solver_get_best_match_index_name(const solver_t* solver);
280 
281 /**
282  Returns the lower/upper bounds of pixel scale that will be searched,
283  in arcsec/pixel.
284  */
285 double solver_get_pixscale_low(const solver_t* solver);
286 double solver_get_pixscale_high(const solver_t* solver);
287 
288 /**
289  Sets the range of quad sizes to try in the image.  This will be
290  further tightened, if possible, given the range of quad sizes in the
291  index and the pixel scale estimates.
292 
293  This avoids looking at tiny quads in the image, because if matches
294  are generated they are difficult to verify (a tiny bit of noise in
295  the quad position translates to a lot of positional noise in stars
296  far away).
297 
298  Min and max are in pixels.
299 
300  Sets quadsize_min, quadsize_max fields.
301 
302  Recommended: ~ 10% of smaller image dimension; 100% of image diagonal.
303  */
304 void solver_set_quad_size_range(solver_t* solver, double qmin, double qmax);
305 
306 /**
307  Same as solver_set_quad_size_range(), but specified in terms of
308  fraction of the smaller image dimension (for lower) and the diagonal
309  (for upper).
310 
311  Recommended: min=0.1, max=1
312  */
313 void solver_set_quad_size_fraction(solver_t* solver, double qmin, double qmax);
314 
315 /**
316  Returns the range of quad sizes that could be matched, given the
317  current settings of pixel scale and image quad size.
318 
319  Returns minimum pixel scale * minimum quad size and
320  maximum pixel scale * maximum quad size.
321  */
322 void solver_get_quad_size_range_arcsec(const solver_t* solver, double* qmin, double* qmax);
323 
324 void solver_free(solver_t*);
325 
326 /**
327  Tells the solver which field of stars it's going to be solving.
328 
329  The solver_t* takes ownership of the *field*; it will be freed upon
330  solver_free() or solver_cleanup_field() or a new solver_set_field().
331  */
332 void solver_set_field(solver_t* s, starxy_t* field);
333 
334 starxy_t* solver_get_field(solver_t* solver);
335 
336 void solver_reset_field_size(solver_t* s);
337 
338 /**
339  Tells the solver to only accept matches within "radius_deg" (in
340  degrees) of the given "ra","dec" point (also in degrees).
341 
342  This is, each star comprising the quad must be within that circle.
343  */
344 void solver_set_radec(solver_t* s, double ra, double dec, double radius_deg);
345 
346 void solver_clear_radec(solver_t* s);
347 
348 /**
349  Tells the solver the pixel coordinate range of the image to be
350  solved.  If not set, this will be computed based on the bounds of the
351  stars within the field (an underestimate).
352  */
353 void solver_set_field_bounds(solver_t* s, double xlo, double xhi, double ylo, double yhi);
354 
355 /**
356  Reset everything associated with solving a particular field.
357 
358  (renamed from solver_new_field)
359  */
360 void solver_cleanup_field(solver_t*);
361 
362 /**
363  get field w,h
364  */
365 double solver_field_width(const solver_t* t);
366 double solver_field_height(const solver_t* t);
367 
368 
369 void solver_add_index(solver_t* solver, index_t* index);
370 void solver_clear_indexes(solver_t* solver);
371 
372 int solver_n_indices(const solver_t* solver);
373 index_t* solver_get_index(const solver_t* solver, int i);
374 
375 void solver_verify_sip_wcs(solver_t* solver, sip_t* sip); //, MatchObj* mo);
376 
377 void solver_run(solver_t* solver);
378 
379 #define SOLVER_TWEAK2_AVAILABLE 1
380 void solver_tweak2(solver_t* solver, MatchObj* mo, int order, sip_t* verifysip);
381 
382 void solver_cleanup(solver_t* solver);
383 
384 // Call this before solver_inject_match(), solver_verify_sip_wcs() or solver_run().
385 // (or it will get called automatically)
386 void solver_preprocess_field(solver_t* sp);
387 // Call this after solver_inject_match() or solver_run().
388 // (or it will get called when you solver_free())
389 void solver_free_field(solver_t* sp);
390 
391 void solver_inject_match(solver_t* solver, MatchObj* mo, sip_t* sip);
392 void solver_compute_quad_range(const solver_t* solver, const index_t* index, double*, double*);
393 
394 /**
395  Resets the "numtries", "nummatches", etc counters, as well as
396  "quitnow".
397  */
398 void solver_reset_counters(solver_t* t);
399 
400 /**
401  Clears the "best_match_solves", "have_best_match", etc fields.
402  */
403 void solver_reset_best_match(solver_t* sp);
404 
405 void solver_print_to(const solver_t* sp, FILE* stream);
406 
407 void solver_log_params(const solver_t* sp);
408 
409 #endif
410