LOST 0.0.1
LOST: Open-source Star Tracker
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
7#include <assert.h>
8#include <sys/types.h>
9#include <unistd.h>
10#include <getopt.h>
11
12#include <bitset>
13#include <string>
14#include <iostream>
15#include <fstream>
16#include <chrono>
17#include <cstring>
18#include <map>
19
20#include "databases.hpp"
21#include "centroiders.hpp"
22#include "decimal.hpp"
23#include "io.hpp"
24#include "man-database.h"
25#include "man-pipeline.h"
26
27namespace lost {
28
30static void DatabaseBuild(const DatabaseOptions &values) {
31 Catalog narrowedCatalog = NarrowCatalog(CatalogRead(), (int) (values.minMag * 100), values.maxStars, DegToRad(values.minSeparation));
32 std::cerr << "Narrowed catalog has " << narrowedCatalog.size() << " stars." << std::endl;
33
35 SerializeContext ser = serFromDbValues(values);
36
37 // Create & Set Flags.
38 uint32_t dbFlags = 0;
39 dbFlags |= typeid(decimal) == typeid(float) ? MULTI_DB_FLOAT_FLAG : 0;
40
41 // Serialize Flags
43
44 std::cerr << "Generated database with " << ser.buffer.size() << " bytes" << std::endl;
45 std::cerr << "Database flagged with " << std::bitset<8*sizeof(dbFlags)>(dbFlags) << std::endl;
46
47 UserSpecifiedOutputStream pos = UserSpecifiedOutputStream(values.outputPath, true);
48 pos.Stream().write((char *) ser.buffer.data(), ser.buffer.size());
49
50}
51
53static void PipelineRun(const PipelineOptions &values) {
55 Pipeline pipeline = SetPipeline(values);
56 std::vector<PipelineOutput> outputs = pipeline.Go(input);
58}
59
60// DO NOT DELETE
61// static void PipelineBenchmark() {
62// PipelineInputList input = PromptPipelineInput();
63// Pipeline pipeline = PromptPipeline();
64// int iterations = Prompt<int>("Times to run the pipeline");
65// std::cerr << "Benchmarking..." << std::endl;
66
67// // TODO: we can do better than this :| maybe include mean time, 99% time, or allow a vector of
68// // input and determine which one took the longest
69// auto startTime = std::chrono::high_resolution_clock::now();
70// for (int i = 0; i < iterations; i++) {
71// pipeline.Go(input);
72// }
73// auto endTime = std::chrono::high_resolution_clock::now();
74// auto totalTime = std::chrono::duration<double, std::milli>(endTime - startTime);
75// std::cout << "total_ms " << totalTime.count() << std::endl;
76// }
77
78// static void EstimateCamera() {
79// std::cerr << "Enter estimated camera details when prompted." << std::endl;
80// PipelineInputList inputs = PromptPngPipelineInput();
81// float baseFocalLength = inputs[0]->InputCamera()->FocalLength();
82// float deviationIncrement = Prompt<float>("Focal length increment (base: " + std::to_string(baseFocalLength) + ")");
83// float deviationMax = Prompt<float>("Maximum focal length deviation to attempt");
84// Pipeline pipeline = PromptPipeline();
85
86// while (inputs[0]->InputCamera()->FocalLength() - baseFocalLength <= deviationMax) {
87// std::cerr << "Attempt focal length " << inputs[0]->InputCamera()->FocalLength() << std::endl;
88// std::vector<PipelineOutput> outputs = pipeline.Go(inputs);
89// if (outputs[0].nice) {
90// std::cout << "camera_identified true" << std::endl << *inputs[0]->InputCamera();
91// return;
92// }
93
94// Camera camera(*inputs[0]->InputCamera());
95// if (camera.FocalLength() - baseFocalLength > 0) {
96// // yes i know this expression can be simplified shut up
97// camera.SetFocalLength(camera.FocalLength() - 2*(camera.FocalLength() - baseFocalLength));
98// } else {
99// camera.SetFocalLength(camera.FocalLength() + 2*(baseFocalLength - camera.FocalLength()) + deviationIncrement);
100// }
101// ((PngPipelineInput *)(inputs[0].get()))->SetCamera(camera);
102// }
103// std::cout << "camera_identified false" << std::endl;
104// }
105
107bool atobool(const char *cstr) {
108 std::string str(cstr);
109 if (str == "1" || str == "true") {
110 return true;
111 }
112 if (str == "0" || str == "false") {
113 return false;
114 }
115 assert(false);
116}
117
122#define LOST_OPTIONAL_OPTARG() \
123 ((optarg == NULL && optind < argc && argv[optind][0] != '-') \
124 ? (bool) (optarg = argv[optind++]) \
125 : (optarg != NULL))
126
127// This is separate from `main` just because it's in the `lost` namespace
128static int LostMain(int argc, char **argv) {
129
130 if (argc == 1) {
131 std::cout << "Usage: ./lost database or ./lost pipeline" << std::endl
132 << "Use --help flag on those commands for further help" << std::endl;
133 return 0;
134 }
135
136 std::string command(argv[1]);
137 optind = 2;
138
139 if (command == "database") {
140
141 enum class DatabaseCliOption {
142#define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) prop,
143#include "database-options.hpp"
144#undef LOST_CLI_OPTION
145 help
146 };
147
148 static struct option long_options[] = {
149#define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) \
150 {name, \
151 defaultArg == 0 ? required_argument : optional_argument, \
152 0, \
153 (int)DatabaseCliOption::prop},
154#include "database-options.hpp" // NOLINT
155#undef LOST_CLI_OPTION
156 {"help", no_argument, 0, (int) DatabaseCliOption::help},
157 {0}
158 };
159
160 DatabaseOptions databaseOptions;
161 int index;
162 int option;
163
164 while ((option = getopt_long(argc, argv, "", long_options, &index)) != -1) {
165 switch (option) {
166#define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) \
167 case (int)DatabaseCliOption::prop : \
168 if (defaultArg == 0) { \
169 databaseOptions.prop = converter; \
170 } else { \
171 if (LOST_OPTIONAL_OPTARG()) { \
172 databaseOptions.prop = converter; \
173 } else { \
174 databaseOptions.prop = defaultArg; \
175 } \
176 } \
177 break;
178#include "database-options.hpp" // NOLINT
179#undef LOST_CLI_OPTION
180 case (int) DatabaseCliOption::help :std::cout << documentation_database_txt << std::endl;
181 return 0;
182 break;
183 default :std::cout << "Illegal flag" << std::endl;
184 exit(1);
185 }
186 }
187
188 lost::DatabaseBuild(databaseOptions);
189
190 } else if (command == "pipeline") {
191
192 enum class PipelineCliOption {
193#define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) prop,
194#include "pipeline-options.hpp"
195#undef LOST_CLI_OPTION
196 help
197 };
198
199 static struct option long_options[] = {
200#define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) \
201 {name, \
202 defaultArg == 0 ? required_argument : optional_argument, \
203 0, \
204 (int)PipelineCliOption::prop},
205#include "pipeline-options.hpp" // NOLINT
206#undef LOST_CLI_OPTION
207
208 // DATABASES
209 {"help", no_argument, 0, (int) PipelineCliOption::help},
210 {0, 0, 0, 0}
211 };
212
214 int index;
215 int option;
216
217 while ((option = getopt_long(argc, argv, "", long_options, &index)) != -1) {
218 switch (option) {
219#define LOST_CLI_OPTION(name, type, prop, defaultVal, converter, defaultArg) \
220 case (int)PipelineCliOption::prop : \
221 if (defaultArg == 0) { \
222 pipelineOptions.prop = converter; \
223 } else { \
224 if (LOST_OPTIONAL_OPTARG()) { \
225 pipelineOptions.prop = converter; \
226 } else { \
227 pipelineOptions.prop = defaultArg; \
228 } \
229 } \
230 break;
231#include "pipeline-options.hpp" // NOLINT
232#undef LOST_CLI_OPTION
233 case (int) PipelineCliOption::help :std::cout << documentation_pipeline_txt << std::endl;
234 return 0;
235 break;
236 default :std::cout << "Illegal flag" << std::endl;
237 exit(1);
238 }
239 }
240
241 lost::PipelineRun(pipelineOptions);
242
243 } else {
244 std::cout << "Usage: ./lost database or ./lost pipeline" << std::endl
245 << "Use --help flag on those commands for further help" << std::endl;
246 }
247 return 0;
248}
249
250}
251
252int main(int argc, char **argv) {
253 return lost::LostMain(argc, argv);
254}
The command line options passed when running a pipeline.
Definition io.hpp:81
#define MULTI_DB_FLOAT_FLAG
A database that contains multiple databases This is almost always the database that is actually passe...
double decimal
Definition decimal.hpp:11
int main(int argc, char **argv)
Definition main.cpp:252
LOST starting point.
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
decimal DegToRad(decimal deg)
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)
std::vector< MultiDatabaseEntry > MultiDatabaseDescriptor
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 SerializeMultiDatabase(SerializeContext *ser, const MultiDatabaseDescriptor &dbs, uint32_t flags)
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
bool atobool(const char *cstr)
Convert string to boolean.
Definition main.cpp:107
std::vector< CatalogStar > Catalog