ADTF File Library
Loading...
Searching...
No Matches
trigonometry_reader

Readers

trigonometry

Trigonometry File Reader to generate trigonometry data.

Properties

Name Default Value Type Description
duration 10.000000 float Time duration for which trigonometry data is to be read.
interval 1.000000 float Time interval between samples on the respective stream in seconds. Fractions of a second can be specified in decimal places (i.e. 0.007 for 7 milliseconds).
period_length 1.000000 float Period duration for the signal values in seconds. Fractions of a second can be specified in decimal places (i.e. 0.007 for 7 milliseconds).

Plugin Attributes

Attribute Value
Plugin Name trigonometry_reader.adtffileplugin
License MPL-2.0
Support Mail suppo.nosp@m.rt@d.nosp@m.igita.nosp@m.lwer.nosp@m.k.net
Homepage URL https://adtf.dev/

Usage

adtfdat_tool --plugin trigonometry_reader.adtffileplugin --create <adtfdat> --input "" --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 adtf_file::ExplicitReader
{
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;
};
Definition reader.h:345
class to create or read a file item. This file item is either a sample, streamtype or trigger.
Definition reader.h:172
virtual std::vector< Stream > getStreams() const
Get the Streams.
Definition reader.h:258
virtual uint32_t getFileVersion() const
Definition reader.h:232
virtual std::string getReaderIdentifier() const =0
Get the Reader Identifier of the Reader.
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...
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:269
Interface for seekable Reader, where the file position can be adapted.
Definition reader.h:288
virtual uint64_t getItemIndexForStreamItemIndex(uint16_t stream_id, uint64_t stream_item_index)=0
virtual void seekTo(uint64_t item_index)=0
virtual uint64_t getItemIndexForTimeStamp(std::chrono::nanoseconds time_stamp)=0
virtual std::shared_ptr< const StreamType > getStreamTypeBefore(uint64_t item_index, uint16_t stream_id)=0
#ifdef _WIN32
#define _USE_MATH_DEFINES
#endif
#include <cmath>
#include "trigonometry_reader.h"
#include <sstream>
#include <string>
#include <regex>
using namespace std::literals;
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"s);
property_type->setProperty("md_definitions"s, "cString"s, build_ddl(name));
property_type->setProperty("md_struct"s, "cString"s, name);
property_type->setProperty("md_data_serialized"s, "tBool"s, "false"s);
return type;
}
TrigonometryReader::TrigonometryReader()
{
FILE_PLUGIN_META_INFORMATION(BUILD_VERSION, "Trigonometry File Reader to generate trigonometry data."s);
// use the default CTOR to create the set the default configuration of the reader
setConfiguration(adtf_file::PropertyBuilder("duration"s, double_seconds(_last_timestamp).count())
.setDescription("Time duration for which trigonometry data is to be read."s) +
adtf_file::PropertyBuilder("period_length"s, _period_length.count())
.setDescription("Period duration for the signal values in seconds. "
"Fractions of a second can be specified in decimal places "
"(i.e. 0.007 for 7 milliseconds)."s) +
adtf_file::PropertyBuilder("interval"s, _interval.count())
.setDescription("Time interval between samples on the respective stream in seconds. "
"Fractions of a second can be specified in decimal places "
"(i.e. 0.007 for 7 milliseconds)."s));
}
std::string TrigonometryReader::getReaderIdentifier() const
{
// this must be a "unique" identifier for your reader
return "trigonometry"s;
}
void TrigonometryReader::open(const std::string& filename,
std::shared_ptr<adtf_file::SampleFactory> sample_factory,
std::shared_ptr<adtf_file::StreamTypeFactory> stream_type_factory)
{
if (filename != "trigonometry"s)
{
throw std::runtime_error("The trigonometry reader can only be used with a filename equal to 'trigonometry'."s);
}
_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"s, 10.0)));
_period_length = double_seconds(adtf_file::getPropertyValue(configuration, "period_length"s, 1.0));
_interval = double_seconds(adtf_file::getPropertyValue(configuration, "interval"s, 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"s, interval_count, std::chrono::seconds(0), _last_timestamp,
build_type(*stream_type_factory, "sine"s)});
_streams.push_back({2, "cosine"s, interval_count, std::chrono::seconds(0), _last_timestamp,
build_type(*stream_type_factory, "cosine"s)});
_streams.push_back({3, "tangent"s, interval_count, std::chrono::seconds(0), _last_timestamp,
build_type(*stream_type_factory, "tangent"s)});
_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: "s + 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"s);
}
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};
}
The PropertyBuilder class is an easy way to define a property with additional meta information....
Definition configuration.h:482
PropertyBuilder & setDescription(const std::string &description)
Sets a description for the property.
Definition configuration.h:501
Base class for a streamtype factory to create streamtypes used by the Reader.
Definition reader.h:94
virtual std::shared_ptr< StreamType > build() const =0
Creates a streamtype.
#define FILE_PLUGIN_META_INFORMATION(version, description)
Convenience to set version and description to adtffileplugin.
Definition configuration.h:551
T getPropertyValue(const Configuration &configuration, const std::string &property_name)
Definition configuration.h:244
utils5ext::exceptions::EndOfFile EndOfFile
Exception to indicate the end of file was reached.
Definition indexedfile_types.h:114