LOST  0.0.1
LOST: Open-source Star Tracker
databases.hpp
Go to the documentation of this file.
1 #ifndef DATABASE_BUILDER_H
2 #define DATABASE_BUILDER_H
3 
4 #include <stdlib.h>
5 #include <inttypes.h>
6 #include <vector>
7 
8 #include "star-utils.hpp"
9 #include "serialize-helpers.hpp"
10 
11 namespace lost {
12 
13 const int32_t kCatalogMagicValue = 0xF9A283BC;
14 
15 inline bool isFlagSet(uint32_t dbFlags, uint32_t flag);
16 
22 // TODO: QueryConservative, QueryExact, QueryTrapezoidal?
23 class KVectorIndex {
24 public:
25  explicit KVectorIndex(DeserializeContext *des);
26 
27  long QueryLiberal(decimal minQueryDistance, decimal maxQueryDistance, long *upperIndex) const;
28 
30  long NumValues() const { return numValues; };
31  long NumBins() const { return numBins; };
33  decimal Max() const { return max; };
34  // Lower bound on elements
35  decimal Min() const { return min; };
36 private:
37  long BinFor(decimal dist) const;
38 
39  long numValues;
40  decimal min;
41  decimal max;
42  decimal binWidth;
43  long numBins;
44  const int32_t *bins;
45 };
46 
47 void SerializePairDistanceKVector(SerializeContext *, const Catalog &, decimal minDistance, decimal maxDistance, long numBins);
48 
55 public:
57 
58  const int16_t *FindPairsLiberal(decimal min, decimal max, const int16_t **end) const;
59  const int16_t *FindPairsExact(const Catalog &, decimal min, decimal max, const int16_t **end) const;
60  std::vector<decimal> StarDistances(int16_t star, const Catalog &) const;
61 
63  decimal MaxDistance() const { return index.Max(); };
65  decimal MinDistance() const { return index.Min(); };
67  long NumPairs() const;
68 
70  static const int32_t kMagicValue; // 0x2536f009
71  // apparently you're supposed to not actually put the value of the static variables here, but
72  // rather in a cpp implementation file.
73 private:
74  KVectorIndex index;
75  // TODO: endianness
76  const int16_t *pairs;
77 };
78 
79 // /**
80 // * @brief Stores "inner angles" between star triples
81 // * @details Unsensitive to first-order error in basic camera
82 // * parameters (eg, wrong FOV or principal point), can be sensitive to second-order errors (eg,
83 // * camera distortion, which may cause the effective FOV or principal point to be different in
84 // * different parts of the image). Used for Mortari's Non-Dimensional Star-ID
85 // */
86 // class TripleInnerKVectorDatabase {
87 // public:
88 // explicit TripleInnerKVectorDatabase(const unsigned char *databaseBytes);
89 
90 // void FindTriplesLiberal(decimal min, decimal max, long **begin, long **end) const;
91 // private:
92 // KVectorIndex index;
93 // int16_t *triples;
94 // };
95 
102 #define MULTI_DB_FLOAT_FLAG 0x0001 // By default, our DB is in double mode.
103 
105 public:
107  explicit MultiDatabase(const unsigned char *buffer) : buffer(buffer) { };
108  const unsigned char *SubDatabasePointer(int32_t magicValue) const;
109 private:
110  const unsigned char *buffer;
111 };
112 
114 public:
115  MultiDatabaseEntry(int32_t magicValue, std::vector<unsigned char> bytes) // I wonder if making `bytes` a reference would avoid making two copies, or maybe it would be worse by preventing copy-elision
117 
118  int32_t magicValue;
119  uint32_t flags;
120  std::vector<unsigned char> bytes;
121 };
122 
123 typedef std::vector<MultiDatabaseEntry> MultiDatabaseDescriptor;
124 
125 void SerializeMultiDatabase(SerializeContext *, const MultiDatabaseDescriptor &dbs, uint32_t flags);
126 
127 }
128 
129 #endif
A data structure enabling constant-time range queries into fixed numerical data.
Definition: databases.hpp:23
decimal Min() const
Definition: databases.hpp:35
KVectorIndex(DeserializeContext *des)
Construct from serialized buffer.
Definition: databases.cpp:110
long QueryLiberal(decimal minQueryDistance, decimal maxQueryDistance, long *upperIndex) const
Finds all the entries in the given range, and possibly a few just outside the range on the ends.
Definition: databases.cpp:129
long NumBins() const
Definition: databases.hpp:31
decimal Max() const
Upper bound on elements.
Definition: databases.hpp:33
long NumValues() const
The number of data points in the data referred to by the kvector.
Definition: databases.hpp:30
Definition: databases.hpp:113
std::vector< unsigned char > bytes
Definition: databases.hpp:120
MultiDatabaseEntry(int32_t magicValue, std::vector< unsigned char > bytes)
Definition: databases.hpp:115
uint32_t flags
Definition: databases.hpp:119
int32_t magicValue
Definition: databases.hpp:118
MultiDatabase(const unsigned char *buffer)
Create a multidatabase from a serialized multidatabase.
Definition: databases.hpp:107
const unsigned char * SubDatabasePointer(int32_t magicValue) const
MultiDatabase memory layout:
Definition: databases.cpp:316
A database storing distances between pairs of stars.
Definition: databases.hpp:54
long NumPairs() const
Exact number of stored pairs.
Definition: databases.cpp:282
std::vector< decimal > StarDistances(int16_t star, const Catalog &) const
Return the distances from the given star to each star it's paired with in the database (for debugging...
Definition: databases.cpp:287
decimal MinDistance() const
Lower bound on stored star pair distances.
Definition: databases.hpp:65
PairDistanceKVectorDatabase(DeserializeContext *des)
Create the database from a serialized buffer.
Definition: databases.cpp:221
static const int32_t kMagicValue
Magic value to use when storing inside a MultiDatabase.
Definition: databases.hpp:70
const int16_t * FindPairsExact(const Catalog &, decimal min, decimal max, const int16_t **end) const
Definition: databases.cpp:248
const int16_t * FindPairsLiberal(decimal min, decimal max, const int16_t **end) const
Return at least all the star pairs whose inter-star distance is between min and max.
Definition: databases.cpp:237
decimal MaxDistance() const
Upper bound on stored star pair distances.
Definition: databases.hpp:63
double decimal
Definition: decimal.hpp:11
LOST starting point.
const int32_t kCatalogMagicValue
Definition: databases.hpp:13
void SerializePairDistanceKVector(SerializeContext *ser, const Catalog &catalog, decimal minDistance, decimal maxDistance, long numBins)
Serialize a pair-distance KVector into buffer.
Definition: databases.cpp:198
bool isFlagSet(uint32_t dbFlags, uint32_t flag)
Definition: databases.cpp:19
std::vector< MultiDatabaseEntry > MultiDatabaseDescriptor
Definition: databases.hpp:123
void SerializeMultiDatabase(SerializeContext *ser, const MultiDatabaseDescriptor &dbs, uint32_t flags)
Definition: databases.cpp:353
std::vector< CatalogStar > Catalog
Definition: star-utils.hpp:100