LOST 0.0.1
LOST: Open-source Star Tracker
Loading...
Searching...
No Matches
star-utils.cpp
Go to the documentation of this file.
1#include "star-utils.hpp"
2
3#include <assert.h>
4#include <math.h>
5
6#include <cstdint>
7#include <algorithm>
8#include <set>
9#include <vector>
10
11#include "serialize-helpers.hpp"
12
13namespace lost {
14
15
16// brightest star first
18 return a.magnitude < b.magnitude;
19}
20
21Catalog NarrowCatalog(const Catalog &catalog, int maxMagnitude, int maxStars, decimal minSeparation) {
22 Catalog result;
23 for (int i = 0; i < (int)catalog.size(); i++) {
24 // Higher magnitude implies the star is dimmer
25 if (catalog[i].magnitude <= maxMagnitude) {
26 result.push_back(catalog[i]);
27 }
28 }
29
30 std::sort(result.begin(), result.end(), CatalogStarMagnitudeCompare);
31
32 // remove stars that are too close to each other
33 std::set<int> tooCloseIndices;
34 // filter out stars that are too close together
35 // easy enough to n^2 brute force, the catalog isn't that big
36 for (int i = 0; i < (int)result.size(); i++) {
37 for (int j = i+1; j < (int)result.size(); j++) {
38 if (AngleUnit(result[i].spatial, result[j].spatial) < minSeparation) {
39 tooCloseIndices.insert(i);
40 tooCloseIndices.insert(j);
41 }
42 }
43 }
44
45 // Erase all the stars whose indices are in tooCloseIndices from the result.
46 // Loop backwards so indices don't get messed up as we iterate.
47 for (auto it = tooCloseIndices.rbegin(); it != tooCloseIndices.rend(); it++) {
48 result.erase(result.begin() + *it);
49 }
50
51 // and finally limit to n brightest stars
52 if (maxStars < (int)result.size()) {
53 result.resize(maxStars);
54 }
55
56 return result;
57}
58
59size_t TetraFindPatternIndex(const int* patternEdgeRatios,
60 size_t numRatios,
61 int numEdgeRatioBins,
62 size_t tableSize) {
63 const size_t kGoldenRatioHash = 2654435761;
64 size_t index = 0;
65 size_t factor = 1;
66 for (size_t i = 0; i < numRatios; i++) {
67 index += patternEdgeRatios[i] * factor;
68 factor *= numEdgeRatioBins;
69 }
70
71 return ((index % tableSize) * (kGoldenRatioHash % tableSize)) % tableSize;
72}
73
75Catalog::const_iterator FindNamedStar(const Catalog &catalog, int name) {
76 for (auto it = catalog.cbegin(); it != catalog.cend(); ++it) {
77 if (it->name == name) {
78 return it;
79 }
80 }
81 return catalog.cend();
82}
83
91void SerializeCatalogStar(SerializeContext *ser, const CatalogStar &catalogStar, bool inclMagnitude, bool inclName) {
92 SerializeVec3(ser, catalogStar.spatial);
93 if (inclMagnitude) {
94 SerializePrimitive<decimal>(ser, catalogStar.magnitude);
95 }
96 if (inclName) {
97 // TODO: double check that bools aren't some special bitwise thing in C++
98 SerializePrimitive<int16_t>(ser, catalogStar.name);
99 }
100}
101
107CatalogStar DeserializeCatalogStar(DeserializeContext *des, bool inclMagnitude, bool inclName) {
108 CatalogStar result;
109 result.spatial = DeserializeVec3(des);
110 if (inclMagnitude) {
112 } else {
113 result.magnitude = -424242; // TODO, what to do about special values, since there's no good ones for ints.
114 }
115 if (inclName) {
117 } else {
118 result.name = -1;
119 }
120 return result;
121}
122
128void SerializeCatalog(SerializeContext *ser, const Catalog &catalog, bool inclMagnitude, bool inclName) {
129 SerializePrimitive<int16_t>(ser, catalog.size());
130
131 // flags
132 int8_t flags = (inclMagnitude) | (inclName << 1);
133 SerializePrimitive<int8_t>(ser, flags);
134
135 for (const CatalogStar &catalogStar : catalog) {
136 SerializeCatalogStar(ser, catalogStar, inclMagnitude, inclName);
137 }
138}
139
144Catalog DeserializeCatalog(DeserializeContext *des, bool *inclMagnitudeReturn, bool *inclNameReturn) {
145 bool inclName, inclMagnitude;
146 Catalog result;
147
148 int16_t numStars = DeserializePrimitive<int16_t>(des);
149
150 int8_t flags = DeserializePrimitive<int8_t>(des);
151 inclMagnitude = (flags) & 1;
152 inclName = (flags>>1) & 1;
153
154 if (inclMagnitudeReturn != NULL) {
155 *inclMagnitudeReturn = inclMagnitude;
156 }
157 if (inclNameReturn != NULL) {
158 *inclNameReturn = inclName;
159 }
160
161 for (int i = 0; i < numStars; i++) {
162 result.push_back(DeserializeCatalogStar(des, inclMagnitude, inclName));
163 }
164
165 return result;
166}
167
169 return DECIMAL_POW(DECIMAL(10.0), -mag/DECIMAL(250.0));
170}
171
172} // namespace lost
A star from the Bright Star Catalog.
int name
A unique number which unambiguously identifies the catalog star.
int magnitude
The magnitude of the star, with the decimal point shifted two places right.
Vec3 spatial
The point on the unit sphere where the star lies.
double decimal
Definition decimal.hpp:8
#define DECIMAL(x)
Definition decimal.hpp:18
#define DECIMAL_POW(base, power)
Definition decimal.hpp:36
LOST starting point.
void SerializeCatalogStar(SerializeContext *ser, const CatalogStar &catalogStar, bool inclMagnitude, bool inclName)
Serialize a CatalogStar into a byte buffer.
void SerializeCatalog(SerializeContext *ser, const Catalog &catalog, bool inclMagnitude, bool inclName)
Serialize the catalog to buffer.
size_t TetraFindPatternIndex(const int *patternEdgeRatios, size_t numRatios, int numEdgeRatioBins, size_t tableSize)
Catalog DeserializeCatalog(DeserializeContext *des, bool *inclMagnitudeReturn, bool *inclNameReturn)
Deserialize a catalog.
decimal MagToBrightness(int mag)
returns some relative brightness measure, which is proportional to the total number of photons receiv...
decimal AngleUnit(const Vec3 &vec1, const Vec3 &vec2)
Calculate the inner angle, in radians, between two /unit/ vectors.
bool CatalogStarMagnitudeCompare(const CatalogStar &a, const CatalogStar &b)
CatalogStar DeserializeCatalogStar(DeserializeContext *des, bool inclMagnitude, bool inclName)
Deserialize a catalog star.
T DeserializePrimitive(DeserializeContext *des)
Catalog NarrowCatalog(const Catalog &catalog, int maxMagnitude, int maxStars, decimal minSeparation)
Remove unwanted stars from an unfiltered catalog.
void SerializePrimitive(SerializeContext *ser, const T &val)
void SerializeVec3(SerializeContext *ser, const Vec3 &vec)
Serialize a Vec3 to buffer. Takes up space according to SerializeLengthVec3.
Vec3 DeserializeVec3(DeserializeContext *des)
Catalog::const_iterator FindNamedStar(const Catalog &catalog, int name)
Return a pointer to the star with the given name, or NULL if not found.
std::vector< CatalogStar > Catalog