123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- /*
- * TI Voxel Lib component.
- *
- * Copyright (c) 2014 Texas Instruments Inc.
- */
- #ifndef VOXEL_FRAME_H
- #define VOXEL_FRAME_H
- #include <Common.h>
- #include <Point.h>
- #include "VideoMode.h"
- #include "SerializedObject.h"
- #include <sstream>
- namespace Voxel
- {
-
- /**
- * \defgroup Frm Frame related classes
- * @{
- */
- class VOXEL_EXPORT Frame
- {
- public:
- TimeStampType timestamp = 0; // Unix timestamp in micro-seconds
- int id = -1;
-
- inline operator String()
- {
- std::ostringstream s;
- s << id << "@" << timestamp;
- return s.str();
- }
-
- virtual Ptr<Frame> copy() const = 0;
- virtual Ptr<Frame> copyTo(Ptr<Frame> &other) const = 0;
- virtual Ptr<Frame> newFrame() const = 0;
-
- virtual bool isSameType(const Frame &other) const = 0;
- virtual bool isSameSize(const Frame &other) const = 0;
-
- virtual bool serialize(SerializedObject &object) const = 0;
-
- virtual bool deserialize(SerializedObject &object) = 0;
-
- virtual ~Frame() {}
- };
- typedef Ptr<Frame> FramePtr;
- class VOXEL_EXPORT DepthFrame: public Frame
- {
- public:
- Vector<float> depth; // depth frame row-wise. Unit: meters
- Vector<float> amplitude; // amplitude of each depth pixel normalized to value between 0 and 1
- FrameSize size;
- virtual Ptr<Frame> copy() const
- {
- Ptr<Frame> f(new DepthFrame());
- return copyTo(f);
- }
- virtual Ptr<Frame> copyTo(Ptr<Frame> &other) const
- {
- if(!other || !isSameType(*other))
- other = Ptr<Frame>(new DepthFrame());
- DepthFrame *d = dynamic_cast<DepthFrame *>(other.get());
- d->id = id;
- d->timestamp = timestamp;
- d->depth = depth;
- d->amplitude = amplitude;
- d->size = size;
- return other;
- }
-
- virtual bool serialize(SerializedObject &object) const
- {
- size_t s = sizeof(id) + sizeof(timestamp) + depth.size()*sizeof(float)*2 + sizeof(size_t)*2;
-
- object.resize(s);
-
- object.put((const char *)&id, sizeof(id));
- object.put((const char *)×tamp, sizeof(timestamp));
-
- object.put((const char *)&size.width, sizeof(size.width));
- object.put((const char *)&size.height, sizeof(size.height));
-
- object.put((const char *)depth.data(), sizeof(float)*depth.size());
- object.put((const char *)amplitude.data(), sizeof(float)*amplitude.size());
- return true;
- }
-
- virtual bool deserialize(SerializedObject &object)
- {
- if(!object.get((char *)&id, sizeof(id)) ||
- !object.get((char *)×tamp, sizeof(timestamp)) ||
- !object.get((char *)&size.width, sizeof(size.width)) ||
- !object.get((char *)&size.height, sizeof(size.height)))
- return false;
-
- depth.resize(size.width*size.height);
- amplitude.resize(size.width*size.height);
-
- if(!object.get((char *)depth.data(), sizeof(float)*depth.size()) ||
- !object.get((char *)amplitude.data(), sizeof(float)*amplitude.size()))
- return false;
-
- return true;
- }
-
- virtual bool isSameType(const Frame &other) const
- {
- const DepthFrame *f = dynamic_cast<const DepthFrame *>(&other);
- return f;
- }
-
- virtual bool isSameSize(const Frame &other) const
- {
- const DepthFrame *f = dynamic_cast<const DepthFrame *>(&other);
- return (f && size == f->size);
- }
-
- virtual Ptr<Frame> newFrame() const
- {
- DepthFrame *d = new DepthFrame();
- d->depth.resize(depth.size());
- d->amplitude.resize(amplitude.size());
- d->size = size;
- return FramePtr(d);
- }
-
- static Ptr<DepthFrame> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<DepthFrame>(ptr);
- }
-
- virtual ~DepthFrame() {}
- };
- typedef Ptr<DepthFrame> DepthFramePtr;
- class VOXEL_EXPORT RawFrame: public Frame
- {
- public:
- static Ptr<RawFrame> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<RawFrame>(ptr);
- }
-
- virtual ~RawFrame() {}
- };
- typedef Ptr<RawFrame> RawFramePtr;
- enum ToFFrameType {
- ToF_PHASE_AMPLITUDE,
- ToF_I_Q,
- ToF_QUAD
- };
- class VOXEL_EXPORT ToFRawFrame : public RawFrame
- {
- public:
- virtual const uint8_t *phase() const = 0;
- virtual uint8_t *phase() = 0;
- virtual SizeType phaseWordWidth() const = 0; // in bytes
- virtual const uint8_t *amplitude() const = 0;
- virtual uint8_t *amplitude() = 0;
- virtual SizeType amplitudeWordWidth() const = 0; // in bytes
- virtual const uint8_t *flags() const = 0;
- virtual uint8_t *flags() = 0;
- virtual SizeType flagsWordWidth() const = 0; // in bytes
- virtual const uint8_t *ambient() const = 0;
- virtual uint8_t *ambient() = 0;
- virtual SizeType ambientWordWidth() const = 0; // in bytes
- virtual const uint16_t *histogram() const = 0;
- virtual uint16_t *histogram() = 0;
- virtual SizeType histogramSize() const = 0; // number of elements in the histogram
- FrameSize size;
-
- static Ptr<ToFRawFrame> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<ToFRawFrame>(ptr);
- }
-
- virtual ~ToFRawFrame() {}
- };
- typedef Ptr<ToFRawFrame> ToFRawFramePtr;
- template <typename PhaseByteType, typename AmbientByteType>
- class ToFRawFrameTemplate : public ToFRawFrame
- {
- public:
- typedef PhaseByteType AmplitudeByteType;
- typedef AmbientByteType FlagsByteType;
-
- Vector<PhaseByteType> _phase;
- Vector<AmplitudeByteType> _amplitude;
- Vector<AmbientByteType> _ambient;
- Vector<FlagsByteType> _flags;
- Vector<uint16_t> _histogram;
- virtual const uint8_t *ambient() const
- {
- return (const uint8_t *)_ambient.data();
- }
-
- virtual uint8_t *ambient()
- {
- return (uint8_t *)_ambient.data();
- }
-
- virtual SizeType ambientWordWidth() const
- {
- return sizeof(AmbientByteType);
- }
- virtual const uint8_t *amplitude() const
- {
- return (const uint8_t *)_amplitude.data();
- }
-
- virtual uint8_t *amplitude()
- {
- return (uint8_t *)_amplitude.data();
- }
-
- virtual SizeType amplitudeWordWidth() const
- {
- return sizeof(AmplitudeByteType);
- }
- virtual const uint8_t *phase() const
- {
- return (const uint8_t *)_phase.data();
- }
-
- virtual uint8_t *phase()
- {
- return (uint8_t *)_phase.data();
- }
-
- virtual SizeType phaseWordWidth() const
- {
- return sizeof(PhaseByteType);
- }
- virtual const uint8_t *flags() const
- {
- return (const uint8_t *)_flags.data();
- }
-
- virtual uint8_t *flags()
- {
- return (uint8_t *)_flags.data();
- }
-
- virtual SizeType flagsWordWidth() const
- {
- return sizeof(FlagsByteType);
- }
-
- virtual const uint16_t *histogram() const
- {
- return _histogram.data();
- }
-
- virtual uint16_t *histogram()
- {
- return _histogram.data();
- }
-
- virtual SizeType histogramSize() const
- {
- return _histogram.size();
- }
- virtual Ptr<Frame> copy() const
- {
- Ptr<Frame> f(new ToFRawFrameTemplate<PhaseByteType, AmbientByteType>());
- return copyTo(f);
- }
- virtual Ptr<Frame> copyTo(Ptr<Frame> &other) const
- {
- if(!other || !isSameType(*other))
- other = Ptr<Frame>(new ToFRawFrameTemplate<PhaseByteType, AmbientByteType>());
- auto *t = dynamic_cast<ToFRawFrameTemplate<PhaseByteType, AmbientByteType> *>(other.get());
- t->id = id;
- t->timestamp = timestamp;
- t->_phase = _phase;
- t->_amplitude = _amplitude;
- t->_ambient = _ambient;
- t->_flags = _flags;
- t->_histogram = _histogram;
- t->size = size;
- return other;
- }
-
- virtual Ptr<Frame> newFrame() const
- {
- ToFRawFrameTemplate<PhaseByteType, AmbientByteType> *t = new ToFRawFrameTemplate<PhaseByteType, AmbientByteType>();
- t->_phase.resize(_phase.size());
- t->_amplitude.resize(_amplitude.size());
- t->_ambient.resize(_ambient.size());
- t->_flags.resize(_flags.size());
- t->_histogram.resize(_histogram.size());
- t->size = size;
- return FramePtr(t);
- }
-
- virtual bool isSameType(const Frame &other) const
- {
- const ToFRawFrameTemplate<PhaseByteType, AmbientByteType> *f = dynamic_cast<const ToFRawFrameTemplate<PhaseByteType, AmbientByteType> *>(&other);
- return f;
- }
-
- static Ptr<ToFRawFrameTemplate<PhaseByteType, AmbientByteType>> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<ToFRawFrameTemplate<PhaseByteType, AmbientByteType>>(ptr);
- }
-
- virtual bool isSameSize(const Frame &other) const
- {
- const ToFRawFrameTemplate<PhaseByteType, AmbientByteType> *f = dynamic_cast<const ToFRawFrameTemplate<PhaseByteType, AmbientByteType> *>(&other);
- return (f && size == f->size);
- }
-
- virtual bool serialize(SerializedObject &object) const
- {
- size_t s = sizeof(id) + sizeof(timestamp) +
- _phase.size()*sizeof(PhaseByteType) +
- _amplitude.size()*sizeof(AmplitudeByteType) +
- _ambient.size()*sizeof(AmbientByteType) +
- _flags.size()*sizeof(FlagsByteType) +
- _histogram.size()*sizeof(uint16_t) +
- sizeof(size_t)*2;
-
- object.resize(s);
-
- object.put((const char *)&id, sizeof(id));
- object.put((const char *)×tamp, sizeof(timestamp));
-
- size_t histogramSize = _histogram.size();
- object.put((const char *)&size.width, sizeof(size.width));
- object.put((const char *)&size.height, sizeof(size.height));
- object.put((const char *)&histogramSize, sizeof(histogramSize));
-
- object.put((const char *)_phase.data(), sizeof(PhaseByteType)*_phase.size());
- object.put((const char *)_amplitude.data(), sizeof(AmplitudeByteType)*_amplitude.size());
- object.put((const char *)_ambient.data(), sizeof(AmbientByteType)*_ambient.size());
- object.put((const char *)_flags.data(), sizeof(FlagsByteType)*_flags.size());
-
- if(histogramSize)
- object.put((const char *)_histogram.data(), sizeof(uint16_t)*_histogram.size());
- return true;
- }
-
- virtual bool deserialize(SerializedObject &object)
- {
- object.get((char *)&id, sizeof(id));
- object.get((char *)×tamp, sizeof(timestamp));
-
- size_t histogramSize;
- object.get((char *)&size.width, sizeof(size.width));
- object.get((char *)&size.height, sizeof(size.height));
- object.get((char *)&histogramSize, sizeof(histogramSize));
-
- unsigned int frameSize = size.width*size.height;
- _phase.resize(frameSize);
- _amplitude.resize(frameSize);
- _ambient.resize(frameSize);
- _flags.resize(frameSize);
-
- object.get((char *)_phase.data(), sizeof(PhaseByteType)*_phase.size());
- object.get((char *)_amplitude.data(), sizeof(AmplitudeByteType)*_amplitude.size());
- object.get((char *)_ambient.data(), sizeof(AmbientByteType)*_ambient.size());
- object.get((char *)_flags.data(), sizeof(FlagsByteType)*_flags.size());
-
- if(histogramSize)
- {
- _histogram.resize(histogramSize);
- object.get((char *)_histogram.data(), sizeof(uint16_t)*_histogram.size());
- }
- else
- _histogram.clear();
-
- return true;
- }
-
- virtual ~ToFRawFrameTemplate() {}
- };
- class VOXEL_EXPORT ToFRawIQFrame: public RawFrame
- {
- public:
- virtual const uint8_t *i() const = 0;
- virtual uint8_t *i() = 0;
- virtual const uint8_t *q() const = 0;
- virtual uint8_t *q() = 0;
- virtual SizeType wordWidth() const = 0; // in bytes
-
- FrameSize size;
-
- static Ptr<ToFRawIQFrame> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<ToFRawIQFrame>(ptr);
- }
-
- virtual ~ToFRawIQFrame() {}
- };
- typedef Ptr<ToFRawIQFrame> ToFRawIQFramePtr;
- template <typename ByteType>
- class ToFRawIQFrameTemplate : public ToFRawIQFrame
- {
- public:
- Vector<ByteType> _i;
- Vector<ByteType> _q;
-
- virtual const uint8_t *i() const
- {
- return (const uint8_t *)_i.data();
- }
-
- virtual uint8_t *i()
- {
- return (uint8_t *)_i.data();
- }
-
- virtual const uint8_t *q() const
- {
- return (const uint8_t *)_q.data();
- }
-
- virtual uint8_t *q()
- {
- return (uint8_t *)_q.data();
- }
-
- virtual SizeType wordWidth() const
- {
- return sizeof(ByteType);
- }
- virtual Ptr<Frame> copy() const
- {
- Ptr<Frame> f(new ToFRawIQFrameTemplate<ByteType>());
- return copyTo(f);
- }
- virtual Ptr<Frame> copyTo(Ptr<Frame> &other) const
- {
- if(!other || !isSameType(*other))
- other = Ptr<Frame>(new ToFRawIQFrameTemplate<ByteType>());
- auto *t = dynamic_cast<ToFRawIQFrameTemplate<ByteType> *>(other.get());
- t->id = id;
- t->timestamp = timestamp;
- t->_i = _i;
- t->_q = _q;
- t->size = size;
- return other;
- }
-
- virtual Ptr<Frame> newFrame() const
- {
- ToFRawIQFrameTemplate<ByteType> *t = new ToFRawIQFrameTemplate<ByteType>();
- t->_i.resize(_i.size());
- t->_q.resize(_q.size());
- t->size = size;
- return FramePtr(t);
- }
-
- virtual bool isSameType(const Frame &other) const
- {
- const ToFRawIQFrameTemplate<ByteType> *f = dynamic_cast<const ToFRawIQFrameTemplate<ByteType> *>(&other);
- return f;
- }
-
- static Ptr<ToFRawIQFrameTemplate<ByteType>> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<ToFRawIQFrameTemplate<ByteType>>(ptr);
- }
-
- virtual bool isSameSize(const Frame &other) const
- {
- const ToFRawIQFrameTemplate<ByteType> *f = dynamic_cast<const ToFRawIQFrameTemplate<ByteType> *>(&other);
- return (f && size == f->size);
- }
-
- virtual bool serialize(SerializedObject &object) const
- {
- size_t s = sizeof(id) + sizeof(timestamp) +
- _i.size()*sizeof(ByteType) +
- _q.size()*sizeof(ByteType) +
- sizeof(uint32_t)*2;
-
- object.resize(s);
-
- object.put((const char *)&id, sizeof(id));
- object.put((const char *)×tamp, sizeof(timestamp));
-
- uint32_t x;
- x = sizeof(ByteType);
- object.put((const char *)&x, sizeof(x));
-
- x = size.width;
- object.put((const char *)&x, sizeof(x));
- x = size.height;
- object.put((const char *)&x, sizeof(x));
-
- object.put((const char *)_i.data(), sizeof(ByteType)*_i.size());
- object.put((const char *)_q.data(), sizeof(ByteType)*_q.size());
- return true;
- }
-
- virtual bool deserialize(SerializedObject &object)
- {
- object.get((char *)&id, sizeof(id));
- object.get((char *)×tamp, sizeof(timestamp));
-
- uint32_t x;
- object.get((char *)&x, sizeof(x));
-
- if(x != sizeof(ByteType))
- return false;
-
- object.get((char *)&x, sizeof(x));
- size.width = x;
- object.get((char *)&x, sizeof(x));
- size.height = x;
-
- _i.resize(size.width*size.height);
- _q.resize(size.width*size.height);
-
- object.get((char *)_i.data(), sizeof(ByteType)*_i.size());
- object.get((char *)_q.data(), sizeof(ByteType)*_q.size());
-
- return true;
- }
-
- virtual ~ToFRawIQFrameTemplate() {}
- };
- class VOXEL_EXPORT RawDataFrame : public RawFrame
- {
- public:
- Vector<ByteType> data;
- virtual Ptr<Frame> copy() const
- {
- Ptr<Frame> f(new RawDataFrame());
- return copyTo(f);
- }
- virtual Ptr<Frame> copyTo(Ptr<Frame> &other) const
- {
- if(!other || !isSameType(*other))
- other = Ptr<Frame>(new RawDataFrame());
- auto *r = dynamic_cast<RawDataFrame *>(other.get());
- r->id = id;
- r->timestamp = timestamp;
- r->data = data;
- return other;
- }
-
- virtual Ptr<Frame> newFrame() const
- {
- RawDataFrame *r = new RawDataFrame();
- r->data.resize(data.size());
- return FramePtr(r);
- }
-
- virtual bool isSameType(const Frame &other) const
- {
- const RawDataFrame *f = dynamic_cast<const RawDataFrame *>(&other);
- return f;
- }
-
- virtual bool isSameSize(const Frame &other) const
- {
- const RawDataFrame *f = dynamic_cast<const RawDataFrame *>(&other);
- return f && data.size() == f->data.size();
- }
-
- static Ptr<RawDataFrame> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<RawDataFrame>(ptr);
- }
-
- virtual bool serialize(SerializedObject &object) const
- {
- size_t s = sizeof(id) + sizeof(timestamp) + data.size()*sizeof(ByteType) + sizeof(size_t);
-
- object.resize(s);
-
- object.put((const char *)&id, sizeof(id));
- object.put((const char *)×tamp, sizeof(timestamp));
-
- s = data.size();
- object.put((const char *)&s, sizeof(s));
-
- object.put((const char *)data.data(), sizeof(ByteType)*data.size());
- return true;
- }
-
- virtual bool deserialize(SerializedObject &object)
- {
- /** size_t is 4 in arm and 8 in x86. So used uint64_t**/
- uint64_t s;
- if(!object.get((char *)&id, sizeof(id)) ||
- !object.get((char *)×tamp, sizeof(timestamp)) ||
- !object.get((char *)&s, sizeof(s)))
- return false;
-
- data.resize(s);
-
- return object.get((char *)data.data(), sizeof(ByteType)*data.size());
- }
-
- virtual ~RawDataFrame() {}
- };
- typedef Ptr<RawDataFrame> RawDataFramePtr;
- class VOXEL_EXPORT PointCloudFrame : public Frame
- {
- public:
- virtual SizeType size() const = 0;
- virtual Point *operator [](IndexType index) = 0;
-
- static Ptr<PointCloudFrame> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<PointCloudFrame>(ptr);
- }
-
- virtual ~PointCloudFrame() {}
- };
- typedef Ptr<PointCloudFrame> PointCloudFramePtr;
- template <typename PointType>
- class PointCloudFrameTemplate : public PointCloudFrame
- {
- public:
- Vector<PointType> points;
-
- virtual SizeType size() const
- {
- return points.size();
- }
-
- virtual Point *operator[] (IndexType index)
- {
- if(index < points.size() && index >= 0)
- return &points[index];
- else
- return 0;
- }
- virtual Ptr<Frame> copy() const
- {
- Ptr<Frame> f(new PointCloudFrameTemplate<PointType>());
- return copyTo(f);
- }
- virtual Ptr<Frame> copyTo(Ptr<Frame> &other) const
- {
- if(!other || !isSameType(*other))
- other = Ptr<Frame>(new PointCloudFrameTemplate<PointType>());
- auto *p = dynamic_cast<PointCloudFrameTemplate<PointType> *>(other.get());
- p->id = id;
- p->timestamp = timestamp;
- p->points = points;
- return other;
- }
-
- virtual Ptr<Frame> newFrame() const
- {
- PointCloudFrameTemplate<PointType> *p = new PointCloudFrameTemplate<PointType>();
- p->points.resize(points.size());
- return FramePtr(p);
- }
-
- virtual bool isSameType(const Frame &other) const
- {
- const PointCloudFrameTemplate<PointType> *f = dynamic_cast<const PointCloudFrameTemplate<PointType> *>(&other);
- return f;
- }
-
- virtual bool isSameSize(const Frame &other) const
- {
- const PointCloudFrameTemplate<PointType> *f = dynamic_cast<const PointCloudFrameTemplate<PointType> *>(&other);
- return f && f->size() == size();
- }
-
- virtual bool serialize(SerializedObject &object) const
- {
- size_t s = sizeof(id) + sizeof(timestamp) + points.size()*sizeof(PointType) + sizeof(size_t);
-
- object.resize(s);
-
- object.put((const char *)&id, sizeof(id));
- object.put((const char *)×tamp, sizeof(timestamp));
-
- s = points.size();
- object.put((const char *)&s, sizeof(s));
-
- object.put((const char *)points.data(), sizeof(PointType)*points.size());
- return true;
- }
-
- virtual bool deserialize(SerializedObject &object)
- {
- object.get((char *)&id, sizeof(id));
- object.get((char *)×tamp, sizeof(timestamp));
-
- size_t s;
- object.get((char *)&s, sizeof(s));
-
- points.resize(s);
-
- object.get((char *)points.data(), sizeof(ByteType)*points.size());
- return true;
- }
-
- static Ptr<PointCloudFrameTemplate<PointType>> typeCast(FramePtr ptr)
- {
- return std::dynamic_pointer_cast<PointCloudFrameTemplate<PointType>>(ptr);
- }
-
- virtual ~PointCloudFrameTemplate() {}
- };
- typedef PointCloudFrameTemplate<Point> XYZPointCloudFrame;
- typedef PointCloudFrameTemplate<IntensityPoint> XYZIPointCloudFrame;
- typedef Ptr<XYZPointCloudFrame> XYZPointCloudFramePtr;
- /**
- * @}
- */
- }
- #endif // VOXEL_POINT_H
|