adtf_file_library  0.13.1
Trigonometry Reader

Description

Example reader which shows how to implement a adtf_file::SeekableReader to create specifics streams for a .adtfdat.

Usage

adtfdat_tool --plugin trigonometry_reader.adtffileplugin --create <adtfdat> --stream <name> --readerid trigonometry [--property duration=<value> --property period_length=<value> --property interval=<value>]

Source

#pragma once
#define _USE_MATH_DEFINES
#include <memory>
#include <chrono>
class TrigonometryReader : public adtf_file::SeekableReader
{
public:
TrigonometryReader();
std::string getReaderIdentifier() const override;
void open(const std::string& filename,
std::shared_ptr<adtf_file::SampleFactory> sample_factory,
std::shared_ptr<adtf_file::StreamTypeFactory> stream_type_factory) override;
uint32_t getFileVersion() const override;
std::vector<adtf_file::Stream> getStreams() const override;
std::optional<uint64_t> getItemCount() const override;
uint64_t getItemIndexForTimeStamp(std::chrono::nanoseconds time_stamp) override;
uint64_t getItemIndexForStreamItemIndex(uint16_t stream_id, uint64_t stream_item_index) override;
std::shared_ptr<const adtf_file::StreamType> getStreamTypeBefore(uint64_t item_index, uint16_t stream_id) override;
void seekTo(uint64_t item_index) override;
private:
std::shared_ptr<adtf_file::SampleFactory> _sample_factory;
std::vector<adtf_file::Stream> _streams;
using double_seconds = std::chrono::duration<double>;
std::chrono::nanoseconds _last_timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(double_seconds(10.0));
double_seconds _period_length = double_seconds(1.0);
double_seconds _interval = double_seconds(1.0);
uint64_t _item_count = {};
uint64_t _current_item_index = 0;
};
class to create or read a file item. This file item is either a sample, streamtype or trigger.
Definition: reader.h:156
virtual uint32_t getFileVersion() const
Definition: reader.h:211
virtual std::optional< uint64_t > getItemCount() const
Get the Item Count. This gets the overall count of all items (samples, stream types and triggers) of ...
Definition: reader.h:248
virtual std::string getReaderIdentifier() const =0
Get the Reader Identifier of the Reader.
virtual std::vector< Stream > getStreams() const
Get the Streams.
Definition: reader.h:237
virtual FileItem getNextItem()=0
virtual void open(const std::string &filename, std::shared_ptr< SampleFactory > sample_factory, std::shared_ptr< StreamTypeFactory > stream_type_factory)=0
opens a file by the given filename. The given factories must be used to create samples and streamtype...
Interface for seekable Reader, where the file position can be adapted.
Definition: reader.h:267
virtual uint64_t getItemIndexForStreamItemIndex(uint16_t stream_id, uint64_t stream_item_index)=0
virtual std::shared_ptr< const StreamType > getStreamTypeBefore(uint64_t item_index, uint16_t stream_id)=0
virtual void seekTo(uint64_t item_index)=0
virtual uint64_t getItemIndexForTimeStamp(std::chrono::nanoseconds time_stamp)=0
#ifdef WIN32
#define _USE_MATH_DEFINES
#endif
#include <cmath>
#include "trigonometry_reader.h"
#include <sstream>
#include <string>
#include <regex>
std::string build_ddl(const std::string& name)
{
std::ostringstream struct_definition;
struct_definition << R"(<struct name=")" << name << R"(" alignment="1" version="1">)" <<
R"(
<element name="value" type="tFloat64" arraysize="1">
<deserialized alignment="1"/>
<serialized bytepos="0" byteorder="LE"/>
</element>
</struct>
)";
return struct_definition.str();
}
std::shared_ptr<adtf_file::StreamType> build_type(adtf_file::StreamTypeFactory& stream_type_factory, const std::string& name)
{
auto type = stream_type_factory.build();
auto property_type = std::dynamic_pointer_cast<adtf_file::PropertyStreamType>(type);
property_type->setMetaType("adtf/default");
property_type->setProperty("md_definitions", "cString", build_ddl(name));
property_type->setProperty("md_struct", "cString", name);
property_type->setProperty("md_data_serialized", "tBool", "false");
return type;
}
TrigonometryReader::TrigonometryReader()
{
// use the default CTOR to create the set the default configuration of the reader
setConfiguration({ { "duration", { double_seconds(_last_timestamp).count()}},
{ "period_length", { _period_length.count() }},
{ "interval", { _interval.count() }}
});
}
std::string TrigonometryReader::getReaderIdentifier() const
{
// this must be a "unique" identifier for your reader
return "trigonometry";
}
void TrigonometryReader::open(const std::string&,
std::shared_ptr<adtf_file::SampleFactory> sample_factory,
std::shared_ptr<adtf_file::StreamTypeFactory> stream_type_factory)
{
_sample_factory = sample_factory;
// read the configuration
const auto& configuration = getConfiguration();
_last_timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(double_seconds(adtf_file::getPropertyValue(configuration, "duration", 10.0)));
_period_length = double_seconds(adtf_file::getPropertyValue(configuration, "period_length", 1.0));
_interval = double_seconds(adtf_file::getPropertyValue(configuration, "interval", 1.0));
auto interval_count = static_cast<uint64_t>(_last_timestamp / _interval);
// stream ids have to start at 1, 0 is reserved as "all streams" id
_streams.push_back({1,
"sine",
interval_count,
std::chrono::seconds(0),
_last_timestamp,
build_type(*stream_type_factory, "sine")});
_streams.push_back({2,
"cosine",
interval_count,
std::chrono::seconds(0),
_last_timestamp,
build_type(*stream_type_factory, "cosine")});
_streams.push_back({3,
"tangent",
interval_count,
std::chrono::seconds(0),
_last_timestamp,
build_type(*stream_type_factory, "tangent")});
_item_count = interval_count * _streams.size();
}
uint32_t TrigonometryReader::getFileVersion() const
{
return ifhd::v201_v301::version_id; // We identify as an ADTF 2 Dat File. This way we do not have to emit trigger items as well.
}
std::vector<adtf_file::Stream> TrigonometryReader::getStreams() const
{
return _streams;
}
std::optional<uint64_t> TrigonometryReader::getItemCount() const
{
return _item_count;
}
uint64_t TrigonometryReader::getItemIndexForTimeStamp(std::chrono::nanoseconds time_stamp)
{
return static_cast<uint64_t>(time_stamp / _interval * _streams.size());
}
uint64_t TrigonometryReader::getItemIndexForStreamItemIndex(uint16_t stream_id, uint64_t stream_item_index)
{
return stream_item_index * 3 + (stream_id - 1);
}
std::shared_ptr<const adtf_file::StreamType> TrigonometryReader::getStreamTypeBefore(uint64_t /*item_index*/,
uint16_t stream_id)
{
return _streams.at(stream_id - 1).initial_type;
}
void TrigonometryReader::seekTo(uint64_t item_index)
{
if (item_index >= _item_count)
{
throw std::runtime_error("invalid item index: " + std::to_string(item_index));
}
_current_item_index = item_index;
}
adtf_file::FileItem TrigonometryReader::getNextItem()
{
if (_current_item_index == _item_count)
{
}
// calculate the value
auto timestamp = _interval * (_current_item_index / 3);
auto stream_id = (_current_item_index % _streams.size()) + 1;
double angle = timestamp / _period_length * M_PI * 2;
double value = 0.0;
switch (stream_id)
{
case 1: value = sin(angle); break;
case 2: value = cos(angle); break;
case 3: value = tan(angle); break;
default: throw std::runtime_error("unexpected stream id");
}
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(timestamp);
// create a new sample and copy the value to it.
auto sample = _sample_factory->build();
auto read_sample = std::dynamic_pointer_cast<adtf_file::ReadSample>(sample);
read_sample->setTimeStamp(nanoseconds);
auto buffer = read_sample->beginBufferWrite(sizeof(double));
*static_cast<double*>(buffer) = value;
read_sample->endBufferWrite();
++_current_item_index;
return {static_cast<uint16_t>(stream_id), nanoseconds, sample};
}
Base class for a streamtype factory to create streamtypes used by the Reader.
Definition: reader.h:78
virtual std::shared_ptr< StreamType > build() const =0
Creates a streamtype.
T getPropertyValue(const Configuration &configuration, const std::string &property_name)
Definition: configuration.h:237
utils5ext::exceptions::EndOfFile EndOfFile
Exception to indicate the end of file was reached.
Definition: indexedfile_types.h:114

Copyright © CARIAD SE.
Generated on Fri Apr 19 2024 by doxygen 1.9.1
GIT Commit Hash: 82d535f82776c20b12fc60740bdae991b62444a7