Skip to content

Commit

Permalink
Store Quat as a primitive type
Browse files Browse the repository at this point in the history
Use a separate G3Quat derived class for serialization into a frame,
analogously to the simple G3Data quantities.
  • Loading branch information
arahlin committed Oct 18, 2024
1 parent e03964e commit f584fb3
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 500 deletions.
135 changes: 72 additions & 63 deletions core/include/core/G3Quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,103 @@
#include <G3Vector.h>
#include <G3Map.h>

class G3Quat : public G3FrameObject
class Quat
{
public:
G3Quat() : buf_{0, 0, 0, 0}, versor_(false) {}
G3Quat(double a, double b, double c, double d, bool versor=false) :
buf_{a, b, c, d}, versor_(false) {}
G3Quat(const G3Quat &q) : buf_{q.a(), q.b(), q.c(), q.d()},
versor_(q.versor_) {}
Quat() : buf_{0, 0, 0, 0} {}
Quat(double a, double b, double c, double d) :
buf_{a, b, c, d} {}
Quat(const Quat &q) : buf_{q.a(), q.b(), q.c(), q.d()} {}

double a() const { return buf_[0]; }
double b() const { return buf_[1]; }
double c() const { return buf_[2]; }
double d() const { return buf_[3]; }

bool is_versor() const { return versor_; }
G3Quat versor() const;

Quat versor() const;
double real() const;
G3Quat unreal() const;
G3Quat conj() const;
Quat unreal() const;
Quat conj() const;
double norm() const;
double abs() const;

G3Quat operator ~() const;
Quat operator ~() const;

G3Quat &operator +=(const G3Quat &);
G3Quat &operator -=(const G3Quat &);
G3Quat &operator *=(double);
G3Quat &operator *=(const G3Quat &);
G3Quat &operator /=(double);
G3Quat &operator /=(const G3Quat &);
Quat &operator +=(const Quat &);
Quat &operator -=(const Quat &);
Quat &operator *=(double);
Quat &operator *=(const Quat &);
Quat &operator /=(double);
Quat &operator /=(const Quat &);

G3Quat operator +(const G3Quat &) const;
G3Quat operator -(const G3Quat &) const;
G3Quat operator *(double) const;
G3Quat operator *(const G3Quat &) const;
G3Quat operator /(double) const;
G3Quat operator /(const G3Quat &) const;
Quat operator +(const Quat &) const;
Quat operator -(const Quat &) const;
Quat operator *(double) const;
Quat operator *(const Quat &) const;
Quat operator /(double) const;
Quat operator /(const Quat &) const;

bool operator ==(const G3Quat &) const;
bool operator !=(const G3Quat &) const;
bool operator ==(const Quat &) const;
bool operator !=(const Quat &) const;

template <class A> void serialize(A &ar, unsigned v);

void * buffer();

std::string Description() const;
std::string Summary() const { return Description(); }

private:
double buf_[4];
bool versor_;

void versor_inplace();

SET_LOGGER("G3Quat");
};

std::ostream& operator<<(std::ostream& os, const Quat &);

namespace cereal {
template <class A> struct specialize<A, G3Quat, cereal::specialization::member_serialize> {};
template <class A> struct specialize<A, Quat, cereal::specialization::member_serialize> {};
}

G3_POINTERS(G3Quat);
G3_SERIALIZABLE(G3Quat, 1);
G3_POINTERS(Quat);
CEREAL_CLASS_VERSION(Quat, 1);

Quat operator *(double, const Quat &);
Quat operator /(double, const Quat &);

inline double real(const Quat &q) { return q.real(); };
inline Quat unreal(const Quat &q) { return q.unreal(); };
inline Quat conj(const Quat &q) { return q.conj(); };
inline double norm(const Quat &q) { return q.norm(); }
inline double abs(const Quat &q) { return q.abs(); }

G3Quat operator *(double, const G3Quat &);
G3Quat operator /(double, const G3Quat &);
Quat pow(const Quat &, int);

inline double real(const G3Quat &q) { return q.real(); };
inline G3Quat unreal(const G3Quat &q) { return q.unreal(); };
inline G3Quat conj(const G3Quat &q) { return q.conj(); };
inline double norm(const G3Quat &q) { return q.norm(); }
inline double abs(const G3Quat &q) { return q.abs(); }
Quat cross3(const Quat &a, const Quat &b);
double dot3(const Quat &a, const Quat &b);

G3Quat pow(const G3Quat &, int);
// Frame object data wrapper

G3Quat cross3(const G3Quat &a, const G3Quat &b);
double dot3(const G3Quat &a, const G3Quat &b);
class G3Quat : public G3FrameObject {
public:
Quat value;

G3Quat() {}
G3Quat(const Quat &val) : value(val) {}

template <class A> void serialize(A &ar, unsigned v);
std::string Description() const { return value.Description(); };
bool operator==(const G3Quat & other) const {return value == other.value;}
};

G3_POINTERS(G3Quat);
G3_SERIALIZABLE(G3Quat, 1);

G3VECTOR_SPLIT(G3Quat, G3VectorQuat, 2);
G3VECTOR_OF(Quat, G3VectorQuat);

class G3TimestreamQuat : public G3VectorQuat
{
public:
G3TimestreamQuat() : G3VectorQuat() {}
G3TimestreamQuat(std::vector<G3Quat>::size_type s) : G3VectorQuat(s) {}
G3TimestreamQuat(std::vector<G3Quat>::size_type s,
const G3Quat &val) : G3VectorQuat(s, val) {}
G3TimestreamQuat(std::vector<Quat>::size_type s) : G3VectorQuat(s) {}
G3TimestreamQuat(std::vector<Quat>::size_type s,
const Quat &val) : G3VectorQuat(s, val) {}
G3TimestreamQuat(const G3TimestreamQuat &r) : G3VectorQuat(r),
start(r.start), stop(r.stop) {}
G3TimestreamQuat(const G3VectorQuat &r) : G3VectorQuat(r) {}
Expand Down Expand Up @@ -119,18 +128,18 @@ G3VectorQuat operator * (const G3VectorQuat &, double);
G3VectorQuat &operator *= (G3VectorQuat &, double);
G3VectorQuat operator / (const G3VectorQuat &, double);
G3VectorQuat operator / (double, const G3VectorQuat &);
G3VectorQuat operator / (const G3VectorQuat &, const G3Quat &);
G3VectorQuat operator / (const G3Quat &, const G3VectorQuat &);
G3VectorQuat operator / (const G3VectorQuat &, const Quat &);
G3VectorQuat operator / (const Quat &, const G3VectorQuat &);
G3VectorQuat operator / (const G3VectorQuat &, const G3VectorQuat &);
G3VectorQuat &operator /= (G3VectorQuat &, double);
G3VectorQuat &operator /= (G3VectorQuat &, const G3Quat &);
G3VectorQuat &operator /= (G3VectorQuat &, const Quat &);
G3VectorQuat &operator /= (G3VectorQuat &, const G3VectorQuat &);
G3VectorQuat operator * (const G3VectorQuat &, const G3VectorQuat &);
G3VectorQuat &operator *= (G3VectorQuat &, const G3VectorQuat &);
G3VectorQuat operator * (double, const G3VectorQuat &);
G3VectorQuat operator * (const G3VectorQuat &, const G3Quat &);
G3VectorQuat operator * (const G3Quat &, const G3VectorQuat &);
G3VectorQuat &operator *= (G3VectorQuat &, const G3Quat &);
G3VectorQuat operator * (const G3VectorQuat &, const Quat &);
G3VectorQuat operator * (const Quat &, const G3VectorQuat &);
G3VectorQuat &operator *= (G3VectorQuat &, const Quat &);

G3VectorQuat pow(const G3VectorQuat &a, int b);

Expand All @@ -139,22 +148,22 @@ G3TimestreamQuat operator * (const G3TimestreamQuat &, double);
G3TimestreamQuat operator * (double, const G3TimestreamQuat &);
G3TimestreamQuat operator / (const G3TimestreamQuat &, double);
G3TimestreamQuat operator / (double, const G3TimestreamQuat &);
G3TimestreamQuat operator / (const G3TimestreamQuat &, const G3Quat &);
G3TimestreamQuat operator / (const G3Quat &, const G3TimestreamQuat &);
G3TimestreamQuat operator / (const G3TimestreamQuat &, const Quat &);
G3TimestreamQuat operator / (const Quat &, const G3TimestreamQuat &);
G3TimestreamQuat operator / (const G3TimestreamQuat &, const G3VectorQuat &);
G3TimestreamQuat &operator /= (G3TimestreamQuat &, double);
G3TimestreamQuat &operator /= (G3TimestreamQuat &, const G3Quat &);
G3TimestreamQuat &operator /= (G3TimestreamQuat &, const Quat &);
G3TimestreamQuat &operator /= (G3TimestreamQuat &, const G3VectorQuat &);
G3TimestreamQuat operator * (const G3TimestreamQuat &, const G3VectorQuat &);
G3TimestreamQuat &operator *= (G3TimestreamQuat &, const G3VectorQuat &);
G3TimestreamQuat operator * (double, const G3TimestreamQuat &);
G3TimestreamQuat operator * (const G3TimestreamQuat &, const G3Quat &);
G3TimestreamQuat operator * (const G3Quat &, const G3TimestreamQuat &);
G3TimestreamQuat &operator *= (G3TimestreamQuat &, const G3Quat &);
G3TimestreamQuat operator * (const G3TimestreamQuat &, const Quat &);
G3TimestreamQuat operator * (const Quat &, const G3TimestreamQuat &);
G3TimestreamQuat &operator *= (G3TimestreamQuat &, const Quat &);

G3TimestreamQuat pow(const G3TimestreamQuat &a, int b);

G3MAP_OF(std::string, G3VectorQuat, G3MapVectorQuat);
G3MAP_SPLIT(std::string, G3Quat, G3MapQuat, 2);
G3MAP_OF(std::string, Quat, G3MapQuat);

#endif
6 changes: 3 additions & 3 deletions core/python/quatextensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from . import G3Quat, G3VectorQuat, G3TimestreamQuat
from . import Quat, G3VectorQuat, G3TimestreamQuat

__all__ = []

Expand All @@ -12,10 +12,10 @@ def quat_ufunc(self, ufunc, method, *inputs, **kwargs):
if ufunc.__name__ == "conjugate":
return self.conj()
if ufunc.__name__ == "reciprocal":
return G3Quat(1, 0, 0, 0) / self
return Quat(1, 0, 0, 0) / self
return NotImplemented


G3Quat.__array_ufunc__ = quat_ufunc
Quat.__array_ufunc__ = quat_ufunc
G3VectorQuat.__array_ufunc__ = quat_ufunc
G3TimestreamQuat.__array_ufunc__ = quat_ufunc
9 changes: 9 additions & 0 deletions core/src/G3Frame.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <G3Frame.h>
#include <G3Data.h>
#include <G3Quat.h>
#include <serialization.h>
#include <pybindings.h>

Expand Down Expand Up @@ -499,6 +500,12 @@ static void g3frame_python_put(G3Frame &f, std::string name, bp::object obj)
return;
}

bp::extract<Quat> extquat(obj);
if (extquat.check()) {
f.Put(name, boost::make_shared<G3Quat>(extquat()));
return;
}

bp::extract<std::string> extstr(obj);
if (extstr.check())
f.Put(name, boost::make_shared<G3String>(extstr()));
Expand Down Expand Up @@ -526,6 +533,8 @@ static bp::object g3frame_python_get(G3Frame &f, std::string name)
return bp::object(boost::dynamic_pointer_cast<const G3String>(element)->value);
else if (!!boost::dynamic_pointer_cast<const G3Bool>(element))
return bp::object(boost::dynamic_pointer_cast<const G3Bool>(element)->value);
else if (!!boost::dynamic_pointer_cast<const G3Quat>(element))
return bp::object(boost::dynamic_pointer_cast<const G3Quat>(element)->value);
else
return bp::object(boost::const_pointer_cast<G3FrameObject>(element));
}
Expand Down
Loading

0 comments on commit f584fb3

Please sign in to comment.