Skip to content

Commit

Permalink
triangle side updaters
Browse files Browse the repository at this point in the history
  • Loading branch information
statespacedev committed Mar 29, 2024
1 parent f87f95d commit aebbfc7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 98 deletions.
16 changes: 8 additions & 8 deletions libstarid/triangles/nomad_triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ void StartriangleNOMAD::from_parent(Startriangleside2& side2_, int starb_, int s

void StartriangleNOMAD::chk1() {
cands ok1, ok2, ok3;
for (auto & a1 : side1.stars) {
for (auto& a1 : side1.stars) {
if (!side3.stars.contains(a1.first)) continue;
for (auto & b1 : a1.second) {
for (auto& b1 : a1.second) {
if (!side2.stars.contains(b1)) continue;
for (auto & c2 : side2.stars[b1]) {
for (auto& c2 : side2.stars[b1]) {
if (!side3.stars[a1.first].contains(c2)) continue;
ok1.insert(a1.first); ok1.insert(b1); ok2.insert(b1); ok2.insert(c2); ok3.insert(c2); ok3.insert(a1.first);}}}
side1.update_side(ok1); side2.update_side(ok2); side3.update_side(ok3);}

void StartriangleNOMAD::chk2(StartriangleNOMAD& other) {
auto adside = Startriangleside2(sv1, other.sv3, starpairs, .00685);
cands ok1, ok2, ok3, tmp;
for (auto & a1 : side1.stars) {
for (auto& a1 : side1.stars) {
if (!side3.stars.contains(a1.first)) continue;
for (auto & b1 : a1.second) {
for (auto& b1 : a1.second) {
if (!side2.stars.contains(b1)) continue;
for (auto & c2 : side2.stars[b1]) {
for (auto& c2 : side2.stars[b1]) {
if (!side3.stars[a1.first].contains(c2)) continue;
if (!adside.stars.contains(c2)) continue;
if (!adside.stars.contains(a1.first) || !other.side2.stars.contains(c2)) continue;
Expand All @@ -55,8 +55,8 @@ void StartriangleNOMAD::chk2(StartriangleNOMAD& other) {

bool StartriangleNOMAD::stop() {
cands c1, c3, tmp;
for (auto & a1 : side1.stars) c1.insert(a1.first);
for (auto & a3 : side3.stars) c3.insert(a3.first);
for (auto& a1 : side1.stars) c1.insert(a1.first);
for (auto& a3 : side3.stars) c3.insert(a3.first);
std::set_intersection(c1.begin(), c1.end(), c3.begin(), c3.end(), std::inserter(tmp, tmp.begin()));
acands = tmp;
if (acands.size() > 1) return false;
Expand Down
80 changes: 6 additions & 74 deletions libstarid/triangles/settler.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,15 @@
#include "settler.h"
#include "../sky/geometry.h"

SETTLER::SETTLER(Starpairs &starpairs) : starpairs(starpairs) {}
SETTLER::SETTLER(Starpairs &starpairs) : starpairs(starpairs), sva{0., 0., 1.} {}

int SETTLER::run(MatrixXd &pixels) {
starvecs = pixels_to_starvecs(pixels);
for (ndxb = 1; ndxb < starvecs.rows(); ++ndxb) { // absides
uveca = starvecs.row(0);
uvecb = starvecs.row(ndxb);
int prev_stars = 0;
int repeatcnt = 0;
bool converged = false;
Startriangleside abside(std::acos(uveca.transpose() * uvecb), starpairs); // abside to investigate

for (ndxc = 1; ndxc < starvecs.rows(); ++ndxc) { // abca triangles
if (converged || !get_angs_c()) continue;
std::vector<StartriangleSETTLER> triangles;
StartriangleSETTLER abca(angs_c[0], angs_c[1], angs_c[2], starpairs, starvecs.row(ndxc).transpose());
abca.side1.stars = abside.stars;
abca.constrain_abca();
abside.update(abca.side1);
triangles.push_back(abca);

for (ndxd = 1; ndxd < starvecs.rows(); ++ndxd) { // abda triangles
if (converged || !get_angs_d()) continue;
StartriangleSETTLER abda(angs_d[0], angs_d[4], angs_d[3], starpairs, starvecs.row(ndxd).transpose());
abda.side1.stars = abside.stars;
abda.constrain_abda(triangles, starpairs);
abside.update(abda.side1);
triangles.push_back(abda);

if (prev_stars == abside.stars.size()) ++repeatcnt; else repeatcnt = 0;
if (repeatcnt > 3) converged = true;
prev_stars = abside.stars.size();
if (abside.stars.size() == 1) break;
} // abda stop
if (abside.stars.size() == 1) break;
} // abca stop
if (abside.stars.size() == 1) {
auto starsit = abside.stars.begin();
return starsit->first;
} // only one candidate abside star pair remains
absides.push_back(abside);
for (Vector3d svb : starvecs.rowwise()) {
Startriangleside2 abside(sva, svb, starpairs);
if (!acands.empty()) abside.update_acands(acands.back());
for (Vector3d svc : starvecs.rowwise()) {
if (svb == svc) continue;
}
return -1;
}

bool SETTLER::get_angs_d() {
if (ndxd == ndxb || ndxd == ndxc) return false;
bool angsok = true;
angs_d = angs_c;
uvecd = starvecs.row(ndxd);
angs_d.push_back(std::acos(uvecd.transpose() * uveca));
angs_d.push_back(std::acos(uvecd.transpose() * uvecb));
angs_d.push_back(std::acos(uvecd.transpose() * uvecc));
if (angs_d[3] < min_ang) angsok = false; // da
if (angs_d[4] < min_ang) angsok = false; // db
if (angs_d[5] < min_ang) angsok = false; // dc
if (std::abs(angs_d[4] - angs_d[3]) < min_ang) angsok = false; // db-da
//if (std::abs(angs_d[4]-angs_d[0]) < min_ang) angsok = false; // db-ab
//if (std::abs(angs_d[4]-angs_d[5]) < min_ang) angsok = false; // db-dc
return angsok;
}

bool SETTLER::get_angs_c() {
min_ang = 3000.0 * arcseconds_to_radians;
if (ndxc == ndxb) return false;
bool angsok = true;
angs_c.clear();
uvecc = starvecs.row(ndxc);
angs_c.push_back(std::acos(uveca.transpose() * uvecb));
angs_c.push_back(std::acos(uvecb.transpose() * uvecc));
angs_c.push_back(std::acos(uvecc.transpose() * uveca));
if (angs_c[0] < min_ang) angsok = false; // ab
if (angs_c[1] < min_ang) angsok = false; // bc
if (angs_c[2] < min_ang) angsok = false; // ca
if (std::abs(angs_c[0] - angs_c[1]) < min_ang) angsok = false; // ab-bc
if (std::abs(angs_c[0] - angs_c[2]) < min_ang) angsok = false; // ab-ca
if (std::abs(angs_c[1] - angs_c[2]) < min_ang) angsok = false; // bc-ca
return angsok;
};

9 changes: 3 additions & 6 deletions libstarid/triangles/settler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@
#include "settler_triangle.h"
using namespace starid;
using namespace Eigen;
using cands = std::set<int>;

namespace starid {
class SETTLER {
public:
explicit SETTLER(Starpairs &pairs);
int run(MatrixXd &pixels);
private:
bool get_angs_c();
bool get_angs_d();
Starpairs starpairs;
MatrixXd starvecs;
double min_ang;
std::vector<double> angs_c, angs_d;
Vector3d uveca, uvecb, uvecc, uvecd;
int ndxb, ndxc, ndxd;
Vector3d sva;
std::vector<std::set<int>> acands;
};
}

Expand Down
22 changes: 22 additions & 0 deletions libstarid/triangles/star_triangle_side.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ void Startriangleside2::update_side(std::set<int>& ok) {
log_star_count.push_back(stars.size());
log_pair_count.push_back(countpairs());}

void Startriangleside2::update_abside(Startriangleside2& side) {
starsdict2 tmp;
for (auto& star : side.stars) {
if (!stars.contains(star.first)) continue;
std::set<int> tmp2;
std::set_intersection(stars[star.first].begin(), stars[star.first].end(),
star.second.begin(), star.second.end(), std::inserter(tmp2, tmp2.begin()));
if (tmp2.empty()) continue;
tmp[star.first] = tmp2;
}
stars = tmp;
log_star_count.push_back(stars.size());
log_pair_count.push_back(countpairs());}

void Startriangleside2::update_acands(std::set<int>& acands) {
std::vector<int> drops;
for (auto star : stars) {
if (!acands.contains(star.first)) drops.push_back(star.first);}
for (auto star : drops) stars.erase(star);
log_star_count.push_back(stars.size());
log_pair_count.push_back(countpairs());}

int Startriangleside2::countpairs() {
int result = 0;
for (auto & star : stars) result += star.second.size();
Expand Down
2 changes: 2 additions & 0 deletions libstarid/triangles/star_triangle_side.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace starid {
Startriangleside2(Vector3d, Vector3d, Starpairs&, float angtol = .003);
Startriangleside2();
void update_side(std::set<int>&);
void update_abside(Startriangleside2&);
void update_acands(std::set<int>&);
int countpairs();
Vector3d sv1, sv2;
double ang;
Expand Down
11 changes: 6 additions & 5 deletions starid/triangles/settler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ class SETTLER:

def __init__(self, starpairs):
self.starpairs = starpairs
self.acands = []

def run(self, image):
"""recognize target star from the starvecs of image pixels."""
starvecs, acands = image.starvecs(), []
starvecs = image.starvecs()
for b, svb in enumerate(starvecs): # abside
abside, bypass = Startriangleside(np.array([0., 0., 1.]), svb, self.starpairs), False
if acands: abside.update_acands(acands[-1])
if self.acands: abside.update_acands(self.acands[-1])
for c, svc in enumerate(starvecs): # abca triangle
if bypass or c == b: continue
tri = [StartriangleSETTLER(svb, svc, self.starpairs, abside)]
Expand All @@ -31,8 +32,8 @@ def run(self, image):
tri.append(StartriangleSETTLER(svb, svd, self.starpairs, abside))
tri[-1].chk2(tri[:-1])
if len(abside.starcnt) > 3 and len(set(abside.starcnt[-3:])) == 1: bypass = True
acands.append(set(abside.stars.keys()))
if not len(acands[-1]) > 1: break
result = acands[-1]
self.acands.append(set(abside.stars.keys()))
if not len(self.acands[-1]) > 1: break
result = self.acands[-1]
return result

11 changes: 6 additions & 5 deletions starid/triangles/star_triangle_side.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ def update_abside(self, side):
new info."""
tmp = dict()
for acand in side.stars.keys():
if acand in self.stars: tmp[acand] = set.intersection(side.stars[acand], self.stars[acand])
if acand in self.stars:
tmp2 = set.intersection(side.stars[acand], self.stars[acand])
if not tmp2: continue
tmp[acand] = tmp2
self.stars = tmp
self.starcnt.append(len(self.stars))
self.paircnt.append(self.count_pairs())

def update_acands(self, acands):
"""this is an abside and we're given a 'new info' set of a stars. shrink our a star possibilities."""
tmp = dict()
for acand in acands:
if acand in self.stars: tmp[acand] = self.stars[acand]
self.stars = tmp
drops = [k for k in self.stars if k not in acands]
for k in drops: self.stars.pop(k, None)
self.starcnt.append(len(self.stars))
self.paircnt.append(self.count_pairs())
return

0 comments on commit aebbfc7

Please sign in to comment.