LOST  0.0.1
LOST: Open-source Star Tracker
io.hpp
Go to the documentation of this file.
1 // I/O stuff, such as Cairo and star catalog interactions.
2 
3 #ifndef IO_H
4 #define IO_H
5 
6 #include <cairo/cairo.h>
7 
8 #include <random>
9 #include <vector>
10 #include <map>
11 #include <utility>
12 #include <string>
13 #include <sstream>
14 #include <iostream>
15 #include <memory>
16 
17 
18 #ifndef CAIRO_HAS_PNG_FUNCTIONS
19 #error LOST requires Cairo to be compiled with PNG support
20 #endif
21 
22 #include "centroiders.hpp"
23 #include "star-utils.hpp"
24 #include "star-id.hpp"
25 #include "camera.hpp"
26 #include "attitude-utils.hpp"
27 #include "attitude-estimators.hpp"
28 #include "databases.hpp"
29 
30 namespace lost {
31 
32 const char kNoDefaultArgument = 0;
33 
36 public:
37  explicit UserSpecifiedOutputStream(std::string filePath, bool isBinary);
39 
41  std::ostream &Stream() { return *stream; };
42 
43 private:
44  bool isFstream;
45  std::ostream *stream;
46 };
47 
48 // use the environment variable LOST_BSC_PATH, or read from ./bright-star-catalog.tsv
49 const Catalog &CatalogRead();
50 // Convert a cairo surface to array of grayscale bytes
51 unsigned char *SurfaceToGrayscaleImage(cairo_surface_t *cairoSurface);
52 cairo_surface_t *GrayscaleImageToSurface(const unsigned char *, const int width, const int height);
53 
54 // take an astrometry download from the bash script, and parse it into stuff.
55 // void v_astrometry_parse(std::string
56 // cairo_surface_t **pcairoSurface, // image data
57 // Star **ppx_centroids, // centroids according to astrometry
58 // int *pi_centroids_length); // TODO: fov, actual angle, etc
59 
60 // type for functions that create a centroid algorithm (by prompting the user usually)
61 
63 class Image {
64 public:
69  unsigned char *image;
70 
71  int width;
72  int height;
73 };
74 
76 // PIPELINE INPUT //
78 
79 
82 public:
83 #define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) \
84  type prop = defaultVal;
85 #include "./pipeline-options.hpp"
86 #undef LOST_CLI_OPTION
87 };
88 
95 public:
96  virtual ~PipelineInput(){};
97 
98  virtual const Image *InputImage() const { return NULL; };
100  virtual const Catalog &GetCatalog() const = 0;
101  virtual const Stars *InputStars() const { return NULL; };
104  virtual const StarIdentifiers *InputStarIds() const { return NULL; };
106  virtual const Attitude *InputAttitude() const { return NULL; };
107  virtual const Camera *InputCamera() const { return NULL; };
109  cairo_surface_t *InputImageSurface() const;
110 
111  virtual const Stars *ExpectedStars() const { return InputStars(); };
117  virtual const StarIdentifiers *ExpectedStarIds() const { return InputStarIds(); };
118  virtual const Attitude *ExpectedAttitude() const { return InputAttitude(); };
119 };
120 
126 public:
127  GeneratedPipelineInput(const Catalog &, Attitude, Camera, std::default_random_engine *,
128 
129  bool centroidsOnly,
130  decimal observedReferenceBrightness, decimal starSpreadStdDev,
131  decimal sensitivity, decimal darkCurrent, decimal readNoiseStdDev,
132  Attitude motionBlurDirection, decimal exposureTime, decimal readoutTime,
133  bool shotNoise, int oversampling,
134  int numFalseStars, int falseMinMagnitude, int falseMaxMagnitude,
135  int cutoffMag,
136  decimal perturbationStddev);
137 
138 
139  const Image *InputImage() const override { return &image; };
140  const Stars *InputStars() const override { return &inputStars; };
141  const Stars *ExpectedStars() const override { return &expectedStars; };
142  const Camera *InputCamera() const override { return &camera; };
143  const StarIdentifiers *InputStarIds() const override { return &inputStarIds; };
144  const StarIdentifiers *ExpectedStarIds() const override { return &expectedStarIds; };
145  const Attitude *InputAttitude() const override { return &attitude; };
146  const Catalog &GetCatalog() const override { return catalog; };
147 
148 private:
149  std::vector<unsigned char> imageData;
150  Image image;
152  Stars expectedStars;
154  Stars inputStars;
155  Camera camera;
156  Attitude attitude;
157  const Catalog &catalog;
158  StarIdentifiers inputStarIds;
159  StarIdentifiers expectedStarIds;
160 };
161 
162 typedef std::vector<std::unique_ptr<PipelineInput>> PipelineInputList;
163 
165 
168 public:
169  PngPipelineInput(cairo_surface_t *, Camera, const Catalog &);
171 
172  const Image *InputImage() const override { return &image; };
173  const Camera *InputCamera() const override { return &camera; };
174  const Catalog &GetCatalog() const override { return catalog; };
175 
176 private:
177  Image image;
178  Camera camera;
179  const Catalog &catalog;
180 };
181 
183 // PIPELINE OUTPUT //
185 
191  std::unique_ptr<Stars> stars = nullptr;
192  std::unique_ptr<StarIdentifiers> starIds = nullptr;
193  std::unique_ptr<Attitude> attitude = nullptr;
194 
197  long long centroidingTimeNs = -1;
198  long long starIdTimeNs = -1;
199  long long attitudeEstimationTimeNs = -1;
200 
206 };
207 
213 
218 
220  int numTotal;
221 };
222 
223 std::ostream &operator<<(std::ostream &, const Camera &);
224 
226 // PIPELINE //
228 
233 class Pipeline {
234  friend Pipeline SetPipeline(const PipelineOptions &values);
235 
236 public:
237  Pipeline() = default;
240  std::vector<PipelineOutput> Go(const PipelineInputList &);
241 
242 private:
243  std::unique_ptr<CentroidAlgorithm> centroidAlgorithm;
244 
245  // next two options are for magnitude filter:
246  int centroidMinMagnitude = 0;
247  int centroidMinStars = 0;
248 
249  std::unique_ptr<StarIdAlgorithm> starIdAlgorithm;
250  std::unique_ptr<AttitudeEstimationAlgorithm> attitudeEstimationAlgorithm;
251  std::unique_ptr<unsigned char[]> database;
252 };
253 
254 Pipeline SetPipeline(const PipelineOptions &values);
255 
256 // TODO: rename. Do something with the output
257 void PipelineComparison(const PipelineInputList &expected,
258  const std::vector<PipelineOutput> &actual,
259  const PipelineOptions &values);
260 
275 StarIdComparison StarIdsCompare(const StarIdentifiers &expected, const StarIdentifiers &actual,
276  // use these to map indices to names for the respective lists of StarIdentifiers
277  const Catalog &expectedCatalog, const Catalog &actualCatalog,
278  decimal centroidThreshold,
279  const Stars &expectedStars, const Stars &inputStars);
280 
282 // DB BUILDER //
284 
287 public:
288 #define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) \
289  type prop = defaultVal;
290 #include "database-options.hpp"
291 #undef LOST_CLI_OPTION
292 };
293 
294 SerializeContext serFromDbValues(const DatabaseOptions &values);
295 
298 MultiDatabaseDescriptor GenerateDatabases(const Catalog &, const DatabaseOptions &values);
299 
301 // INSPECT CATALOG //
303 
305 
306 }
307 
308 #endif
An attitude estimation algorithm estimates the orientation of the camera based on identified stars.
The attitude (orientation) of a spacecraft.
A full description of a camera. Enough information to reconstruct the camera matrix and then some.
Definition: camera.hpp:9
An algorithm that detects the (x,y) coordinates of bright points in an image, called "centroids".
Definition: centroiders.hpp:12
Commannd line options when using the database command.
Definition: io.hpp:286
A pipeline input which is generated (fake image).
Definition: io.hpp:125
const StarIdentifiers * InputStarIds() const override
The centroid indices in the StarIdentifiers returned from InputStarIds should be indices into InputSt...
Definition: io.hpp:143
const Image * InputImage() const override
Definition: io.hpp:139
const Stars * InputStars() const override
Definition: io.hpp:140
const Attitude * InputAttitude() const override
Only used in tracking mode, in which case it is an estimate of the current attitude based on the last...
Definition: io.hpp:145
const Stars * ExpectedStars() const override
Definition: io.hpp:141
const Catalog & GetCatalog() const override
The catalog to which catalog indexes returned from other methods refer.
Definition: io.hpp:146
GeneratedPipelineInput(const Catalog &, Attitude, Camera, std::default_random_engine *, bool centroidsOnly, decimal observedReferenceBrightness, decimal starSpreadStdDev, decimal sensitivity, decimal darkCurrent, decimal readNoiseStdDev, Attitude motionBlurDirection, decimal exposureTime, decimal readoutTime, bool shotNoise, int oversampling, int numFalseStars, int falseMinMagnitude, int falseMaxMagnitude, int cutoffMag, decimal perturbationStddev)
Create a generated pipeline input.
Definition: io.cpp:485
const Camera * InputCamera() const override
Definition: io.hpp:142
const StarIdentifiers * ExpectedStarIds() const override
Centroid indices in the StarIdentifiers returned from ExpectedStarIds should be indices into Expected...
Definition: io.hpp:144
An 8-bit grayscale 2d image.
Definition: io.hpp:63
unsigned char * image
The raw pixel data in the image.
Definition: io.hpp:69
int height
Definition: io.hpp:72
int width
Definition: io.hpp:71
A set of algorithms that describes all or part of the star-tracking "pipeline".
Definition: io.hpp:233
friend Pipeline SetPipeline(const PipelineOptions &values)
Create a pipeline from command line options.
Definition: io.cpp:842
Pipeline()=default
PipelineOutput Go(const PipelineInput &)
Run all stages of a pipeline.
Definition: io.cpp:913
Represents the input and expected outputs of a pipeline run.
Definition: io.hpp:94
virtual const Attitude * InputAttitude() const
Only used in tracking mode, in which case it is an estimate of the current attitude based on the last...
Definition: io.hpp:106
virtual const Catalog & GetCatalog() const =0
The catalog to which catalog indexes returned from other methods refer.
virtual const StarIdentifiers * InputStarIds() const
The centroid indices in the StarIdentifiers returned from InputStarIds should be indices into InputSt...
Definition: io.hpp:104
virtual const Stars * ExpectedStars() const
Definition: io.hpp:111
virtual const Image * InputImage() const
Definition: io.hpp:98
cairo_surface_t * InputImageSurface() const
Convert the InputImage() output into a cairo surface.
Definition: io.cpp:340
virtual const StarIdentifiers * ExpectedStarIds() const
Centroid indices in the StarIdentifiers returned from ExpectedStarIds should be indices into Expected...
Definition: io.hpp:117
virtual const Stars * InputStars() const
Definition: io.hpp:101
virtual ~PipelineInput()
Definition: io.hpp:96
virtual const Attitude * ExpectedAttitude() const
Definition: io.hpp:118
virtual const Camera * InputCamera() const
Definition: io.hpp:107
The command line options passed when running a pipeline.
Definition: io.hpp:81
A pipeline input created by reading a PNG from a file on disk.
Definition: io.hpp:167
const Camera * InputCamera() const override
Definition: io.hpp:173
const Image * InputImage() const override
Definition: io.hpp:172
const Catalog & GetCatalog() const override
The catalog to which catalog indexes returned from other methods refer.
Definition: io.hpp:174
PngPipelineInput(cairo_surface_t *, Camera, const Catalog &)
A pipeline input coming from an image with no extra metadata.
Definition: io.cpp:366
A star idenification algorithm.
Definition: star-id.hpp:16
An output stream which might be a file or stdout.
Definition: io.hpp:35
UserSpecifiedOutputStream(std::string filePath, bool isBinary)
Create a PromptedOutputStream which will output to the given file.
Definition: io.cpp:34
std::ostream & Stream()
return the inner output stream, suitable for use with <<
Definition: io.hpp:41
double decimal
Definition: decimal.hpp:11
LOST starting point.
unsigned char * SurfaceToGrayscaleImage(cairo_surface_t *cairoSurface)
Convert a colored Cairo image surface into a row-major array of grayscale pixels.
Definition: io.cpp:127
std::vector< StarIdentifier > StarIdentifiers
Definition: star-utils.hpp:102
const Catalog & CatalogRead()
Read and parse the full catalog from disk. If called multiple times, will re-use the first result.
Definition: io.cpp:99
std::vector< Star > Stars
Definition: star-utils.hpp:101
StarIdComparison StarIdsCompare(const StarIdentifiers &expected, const StarIdentifiers &actual, const Catalog &expectedCatalog, const Catalog &actualCatalog, decimal centroidThreshold, const Stars &expectedStars, const Stars &inputStars)
Compare expected and actual star identifications.
Definition: io.cpp:1133
std::vector< MultiDatabaseEntry > MultiDatabaseDescriptor
Definition: databases.hpp:123
MultiDatabaseDescriptor GenerateDatabases(const Catalog &catalog, const DatabaseOptions &values)
Appropriately create descriptors for all requested databases according to command-line options.
Definition: io.cpp:280
SerializeContext serFromDbValues(const DatabaseOptions &values)
Definition: io.cpp:276
std::vector< std::unique_ptr< PipelineInput > > PipelineInputList
Definition: io.hpp:162
PipelineInputList GetPipelineInput(const PipelineOptions &values)
Come up with a list of pipeline inputs based on command line options.
Definition: io.cpp:808
void PipelineComparison(const PipelineInputList &expected, const std::vector< PipelineOutput > &actual, const PipelineOptions &values)
Print or otherwise analyze the results of (perhaps multiple) runs of a star tracking pipeline.
Definition: io.cpp:1703
Pipeline SetPipeline(const PipelineOptions &values)
Create a pipeline from command line options.
Definition: io.cpp:842
void InspectCatalog()
const char kNoDefaultArgument
Definition: io.hpp:32
std::vector< CatalogStar > Catalog
Definition: star-utils.hpp:100
cairo_surface_t * GrayscaleImageToSurface(const unsigned char *image, const int width, const int height)
Definition: io.cpp:156
std::ostream & operator<<(std::ostream &os, const Camera &camera)
Print information about the camera in machine and human-readable form.
Definition: io.cpp:304
The result of running a pipeline.
Definition: io.hpp:190
Catalog catalog
The catalog that the indices in starIds refer to.
Definition: io.hpp:205
long long centroidingTimeNs
How many nanoseconds the centroiding stage of the pipeline took.
Definition: io.hpp:197
std::unique_ptr< Stars > stars
Definition: io.hpp:191
long long starIdTimeNs
Definition: io.hpp:198
long long attitudeEstimationTimeNs
Definition: io.hpp:199
std::unique_ptr< Attitude > attitude
Definition: io.hpp:193
std::unique_ptr< StarIdentifiers > starIds
Definition: io.hpp:192
The result of comparing an actual star identification with the true star idenification,...
Definition: io.hpp:209
int numCorrect
The number of centroids in the image which are close to an expected centroid that had an expected ide...
Definition: io.hpp:212
int numTotal
The number of centroids sufficiently close to a true expected star.
Definition: io.hpp:220
int numIncorrect
The number of centroids which were either:
Definition: io.hpp:217