LOST  0.0.1
LOST: Open-source Star Tracker
star-utils.cpp
Go to the documentation of this file.
1 #include "star-utils.hpp"
2 
3 #include <math.h>
4 #include <assert.h>
5 #include <algorithm>
6 #include <set>
7 
8 #include "serialize-helpers.hpp"
9 
10 namespace lost {
11 
12 // brightest star first
14  return a.magnitude < b.magnitude;
15 }
16 
17 Catalog NarrowCatalog(const Catalog &catalog, int maxMagnitude, int maxStars, decimal minSeparation) {
18  Catalog result;
19  for (int i = 0; i < (int)catalog.size(); i++) {
20  if (catalog[i].magnitude <= maxMagnitude) {
21  result.push_back(catalog[i]);
22  }
23  }
24 
25  // remove stars that are too close to each other
26  std::set<int> tooCloseIndices;
27  // filter out stars that are too close together
28  // easy enough to n^2 brute force, the catalog isn't that big
29  for (int i = 0; i < (int)result.size(); i++) {
30  for (int j = i+1; j < (int)result.size(); j++) {
31  if (AngleUnit(result[i].spatial, result[j].spatial) < minSeparation) {
32  tooCloseIndices.insert(i);
33  tooCloseIndices.insert(j);
34  }
35  }
36  }
37 
38  // Erase all the stars whose indices are in tooCloseIndices from the result.
39  // Loop backwards so indices don't get messed up as we iterate.
40  for (auto it = tooCloseIndices.rbegin(); it != tooCloseIndices.rend(); it++) {
41  result.erase(result.begin() + *it);
42  }
43 
44  // and finally limit to n brightest stars
45  if (maxStars < (int)result.size()) {
46  std::sort(result.begin(), result.end(), CatalogStarMagnitudeCompare);
47  result.resize(maxStars);
48  }
49 
50  return result;
51 }
52 
54 Catalog::const_iterator FindNamedStar(const Catalog &catalog, int name) {
55  for (auto it = catalog.cbegin(); it != catalog.cend(); ++it) {
56  if (it->name == name) {
57  return it;
58  }
59  }
60  return catalog.cend();
61 }
62 
70 void SerializeCatalogStar(SerializeContext *ser, const CatalogStar &catalogStar, bool inclMagnitude, bool inclName) {
71  SerializeVec3(ser, catalogStar.spatial);
72  if (inclMagnitude) {
73  SerializePrimitive<decimal>(ser, catalogStar.magnitude);
74  }
75  if (inclName) {
76  // TODO: double check that bools aren't some special bitwise thing in C++
77  SerializePrimitive<int16_t>(ser, catalogStar.name);
78  }
79 }
80 
86 CatalogStar DeserializeCatalogStar(DeserializeContext *des, bool inclMagnitude, bool inclName) {
87  CatalogStar result;
88  result.spatial = DeserializeVec3(des);
89  if (inclMagnitude) {
90  result.magnitude = DeserializePrimitive<decimal>(des);
91  } else {
92  result.magnitude = -424242; // TODO, what to do about special values, since there's no good ones for ints.
93  }
94  if (inclName) {
95  result.name = DeserializePrimitive<int16_t>(des);
96  } else {
97  result.name = -1;
98  }
99  return result;
100 }
101 
107 void SerializeCatalog(SerializeContext *ser, const Catalog &catalog, bool inclMagnitude, bool inclName) {
108  SerializePrimitive<int16_t>(ser, catalog.size());
109 
110  // flags
111  int8_t flags = (inclMagnitude) | (inclName << 1);
112  SerializePrimitive<int8_t>(ser, flags);
113 
114  for (const CatalogStar &catalogStar : catalog) {
115  SerializeCatalogStar(ser, catalogStar, inclMagnitude, inclName);
116  }
117 }
118 
123 Catalog DeserializeCatalog(DeserializeContext *des, bool *inclMagnitudeReturn, bool *inclNameReturn) {
124  bool inclName, inclMagnitude;
125  Catalog result;
126 
127  int16_t numStars = DeserializePrimitive<int16_t>(des);
128 
129  int8_t flags = DeserializePrimitive<int8_t>(des);
130  inclMagnitude = (flags) & 1;
131  inclName = (flags>>1) & 1;
132 
133  if (inclMagnitudeReturn != NULL) {
134  *inclMagnitudeReturn = inclMagnitude;
135  }
136  if (inclNameReturn != NULL) {
137  *inclNameReturn = inclName;
138  }
139 
140  for (int i = 0; i < numStars; i++) {
141  result.push_back(DeserializeCatalogStar(des, inclMagnitude, inclName));
142  }
143 
144  return result;
145 }
146 
148  return DECIMAL_POW(DECIMAL(10.0), -mag/DECIMAL(250.0));
149 }
150 
151 }
A star from the Bright Star Catalog.
Definition: star-utils.hpp:12
int name
A unique number which unambiguously identifies the catalog star.
Definition: star-utils.hpp:42
int magnitude
The magnitude of the star, with the decimal point shifted two places right.
Definition: star-utils.hpp:37
Vec3 spatial
The point on the unit sphere where the star lies.
Definition: star-utils.hpp:32
double decimal
Definition: decimal.hpp:11
#define DECIMAL(x)
Definition: decimal.hpp:21
#define DECIMAL_POW(base, power)
Definition: decimal.hpp:39
LOST starting point.
void SerializeCatalogStar(SerializeContext *ser, const CatalogStar &catalogStar, bool inclMagnitude, bool inclName)
Serialize a CatalogStar into a byte buffer.
Definition: star-utils.cpp:70
void SerializeCatalog(SerializeContext *ser, const Catalog &catalog, bool inclMagnitude, bool inclName)
Serialize the catalog to buffer.
Definition: star-utils.cpp:107
Catalog DeserializeCatalog(DeserializeContext *des, bool *inclMagnitudeReturn, bool *inclNameReturn)
Deserialize a catalog.
Definition: star-utils.cpp:123
decimal MagToBrightness(int mag)
returns some relative brightness measure, which is proportional to the total number of photons receiv...
Definition: star-utils.cpp:147
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)
Definition: star-utils.cpp:13
CatalogStar DeserializeCatalogStar(DeserializeContext *des, bool inclMagnitude, bool inclName)
Deserialize a catalog star.
Definition: star-utils.cpp:86
Catalog NarrowCatalog(const Catalog &catalog, int maxMagnitude, int maxStars, decimal minSeparation)
Remove unwanted stars from an unfiltered catalog.
Definition: star-utils.cpp:17
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.
Definition: star-utils.cpp:54
std::vector< CatalogStar > Catalog
Definition: star-utils.hpp:100