Skip to content

Commit

Permalink
finish tetra serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
zeddie888 committed Nov 5, 2023
1 parent 2f6c369 commit 35e2f84
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 46 deletions.
22 changes: 10 additions & 12 deletions src/databases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace lost {

const int32_t PairDistanceKVectorDatabase::kMagicValue = 0x2536f009;
const int32_t TetraDatabase::kMagicValue = 0x26683787;

struct KVectorPair {
int16_t index1;
Expand Down Expand Up @@ -344,27 +345,23 @@ std::pair<std::vector<uint16_t>, std::vector<uint16_t>> TetraPreparePattCat(cons
}

TetraDatabase::TetraDatabase(DeserializeContext *des) {
// maxAngle_ = *(float*)buffer;
// buffer += sizeof(float);
// catalogSize_ = *(int32_t *)buffer;
maxAngle_ = DeserializePrimitive<float>(des);
catalogSize_ = DeserializePrimitive<int32_t>(des);
pattCatSize_ = DeserializePrimitive<uint64_t>(des);
tetraStarCatSize_ = DeserializePrimitive<uint64_t>(des);
pattCats_ = DeserializeArray<uint16_t>(des, 4 * pattCatSize_);
starCatInds_ = DeserializeArray<uint16_t>(des, tetraStarCatSize_);
}

TetraPatt TetraDatabase::GetPattern(int index) const {
std::vector<int> res;
const unsigned char *p = buffer_ + headerSize;
TetraPatt res;
for (int i = 0; i < 4; i++) {
res.push_back(*((uint16_t *)p + 4 * index + i));
res.push_back(pattCats_[4*index + i]);
}

return res;
}

uint16_t TetraDatabase::GetTrueCatInd(int tetraInd) const {
// TODO: don't harcode this 4
const unsigned char *p = buffer_ + headerSize + PattCatSize() * 4 * sizeof(uint16_t);
return *((uint16_t *)p + tetraInd);
return starCatInds_[tetraInd];
}

std::vector<TetraPatt> TetraDatabase::GetPatternMatches(int index) const {
Expand Down Expand Up @@ -471,7 +468,7 @@ std::vector<float> PairDistanceKVectorDatabase::StarDistances(int16_t star,

///////////////////// Tetra database //////////////////////

void SerializeTetraDatabase(SerializeContext *ser, Catalog &catalog, float maxFovDeg,
void SerializeTetraDatabase(SerializeContext *ser, const Catalog &catalog, float maxFovDeg,
const std::vector<uint16_t> &pattStarIndices,
const std::vector<uint16_t> &catIndices) {
const float maxFovRad = DegToRad(maxFovDeg);
Expand Down Expand Up @@ -631,6 +628,7 @@ void SerializeTetraDatabase(SerializeContext *ser, Catalog &catalog, float maxFo
}
SerializePrimitive<float>(ser, maxFovDeg);
SerializePrimitive<uint64_t>(ser, pattCatalog.size());
SerializePrimitive<uint64_t>(ser, catIndices.size());
for (Pattern patt : pattCatalog) {
for (int i = 0; i < pattSize; i++) {
SerializePrimitive<uint16_t>(ser, patt[i]);
Expand Down
36 changes: 20 additions & 16 deletions src/databases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,28 @@ Pre-processing for Tetra star-id algorithm
std::pair<std::vector<uint16_t>, std::vector<uint16_t>> TetraPreparePattCat(const Catalog &,
const float maxFovDeg);

void SerializeTetraDatabase(SerializeContext *, Catalog &, float maxFovDeg,
const std::vector<uint16_t>& pattStarIndices,
const std::vector<uint16_t>& catIndices);
void SerializeTetraDatabase(SerializeContext *, const Catalog &, float maxFovDeg,
const std::vector<uint16_t> &pattStarIndices,
const std::vector<uint16_t> &catIndices);

/// Tetra star pattern = vector of 4 star IDs
using TetraPatt = std::vector<int>;
using TetraPatt = std::vector<uint16_t>;

/**
* A database storing Tetra star patterns
* TODO: implement something to prevent cycling in quadratic probe
* (or guarantee load factor < 0.5)
*
* Layout:
* | size (bytes) | name | description |
* |-----------------+--------------+-------------------------------------------------------------| |
* | sizeof float | maxFov | max angle (degrees) allowed between any 2 stars |
* | | | in the same pattern |
* | 8 | pattCatSize | number of rows in pattern catalog |
* | 4*pattCatSize*2 | pattCat | hash table for Tetra star patternss |
* | 2*tetraCatSize | tetraStarCat | list of catalog indices to use for Tetra star-id algo |
* | size (bytes) | name | description |
* |----------------------------------+--------------+-------------------------------------------------------------| |
* | sizeof float | maxFov | max angle (degrees) allowed between any 2 stars |
* | | | in the same pattern |
* | sizeof(uint64_t) | pattCatSize | number of rows in pattern catalog |
* | sizeof(uint64_t) | tetraCatSize | number of Tetra catalog indices |
* | 4*pattCatSize * sizeof(uint16_t) | pattCat | hash table for Tetra star patternss |
* | tetraCatSize * sizeof(uint16_t) | tetraStarCat | list of catalog indices to use for Tetra star-id algo |
* |----------------------------------+--------------+-------------------------------------------------------------|
*/
class TetraDatabase {
public:
Expand All @@ -115,7 +117,7 @@ class TetraDatabase {

/// Number of rows in pattern catalog
// With load factor of just under 0.5, size = numPatterns*2 + 1
int PattCatSize() const {return catalogSize_;}
uint64_t PattCatSize() const {return pattCatSize_;}

/// Get the 4-tuple pattern at row=index, 0-based
TetraPatt GetPattern(int index) const;
Expand All @@ -129,13 +131,15 @@ class TetraDatabase {
// TODO: should probably have a field describing number of indices for future updates to db

/// Magic value to use when storing inside a MultiDatabase
static const int32_t kMagicValue = 0xDEADBEEF;
static const int headerSize = sizeof(float) + sizeof(uint64_t);
static const int32_t kMagicValue;
// static const int headerSize = sizeof(float) + sizeof(uint64_t);

private:
// const unsigned char *buffer_;
float maxAngle_;
uint32_t catalogSize_;
uint64_t pattCatSize_;
uint16_t tetraStarCatSize_;
const uint16_t* pattCats_;
const uint16_t* starCatInds_;
};

// /**
Expand Down
24 changes: 23 additions & 1 deletion src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,36 @@ MultiDatabaseDescriptor GenerateDatabases(const Catalog &catalog, const Database
SerializeCatalog(&catalogSer, catalog, false, true);
dbEntries.emplace_back(kCatalogMagicValue, catalogSer.buffer);

bool dbProvided = false;

if (values.kvector) {
dbProvided = true;
float minDistance = DegToRad(values.kvectorMinDistance);
float maxDistance = DegToRad(values.kvectorMaxDistance);
long numBins = values.kvectorNumDistanceBins;
SerializeContext ser = serFromDbValues(values);
SerializePairDistanceKVector(&ser, catalog, minDistance, maxDistance, numBins);
dbEntries.emplace_back(PairDistanceKVectorDatabase::kMagicValue, ser.buffer);
} else {
}

if (values.tetra) {
dbProvided = true;
float maxAngleDeg = values.tetraMaxAngle;
std::cerr << "Tetra max angle: " << maxAngleDeg << std::endl;

auto tetraPrepRes = TetraPreparePattCat(catalog, maxAngleDeg);
std::vector<uint16_t> catIndices = tetraPrepRes.first;
std::vector<uint16_t> pattStarsInds = tetraPrepRes.second;

std::cerr << "Tetra processed catalog has " << catIndices.size() << " stars." << std::endl;
std::cerr << "Number of pattern stars: " << pattStarsInds.size() << std::endl;

SerializeContext ser = serFromDbValues(values);
SerializeTetraDatabase(&ser, catalog, maxAngleDeg, pattStarsInds, catIndices);
dbEntries.emplace_back(TetraDatabase::kMagicValue, ser.buffer);
}

if (!dbProvided) {
std::cerr << "No database builder selected -- no database generated." << std::endl;
exit(1);
}
Expand Down
8 changes: 4 additions & 4 deletions src/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ SerializeContext serFromDbValues(const DatabaseOptions &values);
/// @sa SerializeMultiDatabase
MultiDatabaseDescriptor GenerateDatabases(const Catalog &, const DatabaseOptions &values);

// TODO: can we avoid the split?
void GenerateTetraDatabases(MultiDatabaseBuilder *, const Catalog &, const DatabaseOptions &values,
const std::vector<uint16_t> &pattStars,
const std::vector<uint16_t> &catIndices);
// // TODO: can we avoid the split?
// void GenerateTetraDatabases(MultiDatabaseBuilder *, const Catalog &, const DatabaseOptions &values,
// const std::vector<uint16_t> &pattStars,
// const std::vector<uint16_t> &catIndices);

/////////////////////
// INSPECT CATALOG //
Expand Down
9 changes: 5 additions & 4 deletions src/star-id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ StarIdentifiers TetraStarIdAlgorithm::Go(const unsigned char *database, const St
std::cerr << "Could not get to Tetra database" << std::endl;
return result;
}
TetraDatabase tetraDatabase(databaseBuffer);
DeserializeContext des(databaseBuffer);
TetraDatabase tetraDatabase(&des);

const long long catLength = tetraDatabase.PattCatSize();
const float maxFov = tetraDatabase.MaxAngle();
Expand Down Expand Up @@ -219,7 +220,7 @@ StarIdentifiers TetraStarIdAlgorithm::Go(const unsigned char *database, const St
int hashIndex = KeyToIndex(code, numPattBins, catLength);
// Get a list of Pattern Catalog rows with hash code == hashIndex
// One of these Patterns in the database could be a match to our constructed Pattern
std::vector<Pattern> matches = tetraDatabase.GetPatternMatches(hashIndex);
std::vector<TetraPatt> matches = tetraDatabase.GetPatternMatches(hashIndex);

if ((int)matches.size() == 0) {
// std::cerr << "Alert: matches size = 0, continuing" << std::endl;
Expand All @@ -228,7 +229,7 @@ StarIdentifiers TetraStarIdAlgorithm::Go(const unsigned char *database, const St

bool alrFoundMatch = false;
int numMatches = 0;
for (std::vector<int> matchRow : matches) {
for (TetraPatt matchRow : matches) {
// Construct the pattern we found in the Pattern Catalog
std::vector<int> catStarInds;
std::vector<Vec3> catStarVecs;
Expand Down Expand Up @@ -573,7 +574,7 @@ std::vector<int16_t> ConsumeInvolvingIterator(PairDistanceInvolvingIterator it)
/**
* Given the result of a pair-distance kvector query, build a hashmultimap of stars to other stars
* that appeared with it in the query.
*
*
* The resulting map is "symmetrical" in the sense that if a star B is in the map for star A, then
* star A is also in the map for star B.
*/
Expand Down
9 changes: 0 additions & 9 deletions src/star-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,8 @@ Catalog::const_iterator FindNamedStar(const Catalog &catalog, int name) {
* @param inclName Whether to include the (numerical) name of the star.
* @param buffer[out] Where the serialized star is stored.
*/
<<<<<<< HEAD
// TODO: make inclusion of name/magnitude true by default?
// Actually why give the option in the first place, algos like Tetra need this to work
void SerializeCatalogStar(const CatalogStar &catalogStar, bool inclMagnitude, bool inclName,
unsigned char *buffer) {
SerializeVec3(catalogStar.spatial, buffer);
buffer += SerializeLengthVec3();
=======
void SerializeCatalogStar(SerializeContext *ser, const CatalogStar &catalogStar, bool inclMagnitude, bool inclName) {
SerializeVec3(ser, catalogStar.spatial);
>>>>>>> master
if (inclMagnitude) {
SerializePrimitive<float>(ser, catalogStar.magnitude);
}
Expand Down

0 comments on commit 35e2f84

Please sign in to comment.