Sensing#

#include "navground/core/buffer.h"
using navground::core::BufferShape = std::vector<ssize_t>#

The shape of a multi-dimensional buffer.

using navground::core::BufferType = std::variant<float, double, int64_t, int32_t, int16_t, int8_t, uint64_t, uint32_t, uint16_t, uint8_t>#

The type of the data stored in a multi-dimensional buffer.

using navground::core::BufferData = std::variant<std::valarray<float>, std::valarray<double>, std::valarray<int64_t>, std::valarray<int32_t>, std::valarray<int16_t>, std::valarray<int8_t>, std::valarray<uint64_t>, std::valarray<uint32_t>, std::valarray<uint16_t>, std::valarray<uint8_t>>#

The data container for multi-dimensional buffer

struct BufferDescription#

Describes a typed, bounded, multi-dimensional buffer.

Mimics Gymnasium Box spaces (https://gymnasium.farama.org/api/spaces/).

Public Functions

inline explicit BufferDescription(const BufferShape &shape = BufferShape{}, const std::string &type = "", double low = std::numeric_limits<double>::min(), double high = std::numeric_limits<double>::max(), bool categorical = false)#

Constructs a new instance.

Parameters:
  • shape[in] The shape

  • type[in] The type

  • low[in] The low

  • high[in] The high

  • categorical[in] The categorical

inline std::vector<size_t> get_strides() const#

Gets the strides.

Returns:

The strides.

Public Members

BufferShape shape#

The shape of the buffer

std::string type#

A code that identify the type of data.

double low#

The lower limits

double high#

The upper limits

bool categorical#

Whether the [integer] data is categorical or not.

Public Static Functions

template<typename T>
static inline BufferDescription make(const BufferShape &shape, double low = std::numeric_limits<double>::min(), double high = std::numeric_limits<double>::max(), bool categorical = false)#

Constructs a new instance for a given type.

Parameters:
  • shape[in] The shape

  • low[in] The low

  • high[in] The high

  • categorical[in] The categorical

Template Parameters:

T – The type of the data

Returns:

The buffer description.

class Buffer#

A typed, bounded, multi-dimensional array, similar to numpy arrays.

Public Functions

inline Buffer(const BufferDescription &desc, BufferType value)#

Constructs a new instance with data set to a uniform value.

Parameters:
  • desc[in] The description

  • value[in] The value to assign to all of the buffer.

inline explicit Buffer(const BufferDescription &desc)#

Constructs a new instance, with data set to zero.

Parameters:

desc[in] The description

inline Buffer(const BufferDescription &desc, const BufferData &value)#

Constructs a new instance with data.

Parameters:
  • desc[in] The description

  • value[in] The data

inline explicit Buffer(const BufferData &value = std::valarray<ng_float_t>(0))#

Constructs an unbounded, flat buffer of data.

Parameters:

value[in] The data

inline size_t size() const#

Returns the buffer size.

Returns:

The number of elements in the buffer

inline std::string type() const#

Return the buffer type.

Returns:

The code of type of data stored in the buffer

template<typename T>
inline const std::valarray<T> *get_data() const#

Gets the data stored in the buffer.

Template Parameters:

T – The desired type

Returns:

The data or an null pointer if the type is different.

template<typename T>
inline const T *get_typed_ptr() const#

Gets the data stored in the buffer.

Template Parameters:

T – The desired type

Returns:

The data pointer or null if the type is different.

inline const BufferData &get_data_container() const#

Gets the data container.

Returns:

The data container.

template<typename T>
inline bool has_type() const#

Determines if the data has a given type.

Template Parameters:

T – The type

Returns:

True if the data has the given type, False otherwise.

inline bool set_data(BufferData value, bool force = false)#

Sets the data in the buffer.

If force is set, it will set the data and possibly change type and shape of the buffer when no compatible. If force is not set, it won’t change the data when the shape has a different size or the type is different.

Parameters:
  • value[in] The new data

  • force[in] Whenever shape and type should be changed to match the new data.

Returns:

Whether the data was set or not.

inline const void *get_ptr() const#

Gets the pointer to the data.

Returns:

A pointer to the first element of the buffer.

inline bool set_ptr(void *ptr, const BufferShape &shape, const std::string &_type, bool force = false)#

Sets data from a pointer.

If force is set, it will set the data and possibly change type and shape of the buffer when no compatible. If force is not set, it won’t change the data when the shape has a different size or the type is different.

Parameters:
  • ptr – A pointer to the first element of the new data

  • shape[in] The new shape

  • _type[in] The new type

  • force[in] Whenever shape and type should be changed to match the new data.

Returns:

Whether the data was set or not.

inline const BufferDescription &get_description() const#

Gets the description.

Returns:

The description.

inline bool set_description(BufferDescription value, bool force = false)#

Sets the description.

If force is set, it will set the description and possibly reset the buffer if the type or size have changed. If force is not set, it won’t allow changing shape to a different size or type to a different type.

Parameters:
  • value[in] The value

  • force[in] Whenever data may be reset.

Returns:

Whether the description was set or not.

inline double get_low() const#

Gets the lower bound.

Returns:

The lower bound.

inline double get_high() const#

Gets the upper bound.

Returns:

The upper bound.

inline bool get_categorical() const#

Gets whether buffer is categorical.

Returns:

Whether categorical or not.

inline const BufferShape &get_shape() const#

Gets the shape.

Returns:

The shape.

inline void set_low(double value)#

Sets the lower bound.

Parameters:

value[in] The value

inline void set_high(double value)#

Sets the upper bound.

Parameters:

value[in] The value

inline void set_categorical(bool value)#

Sets whether the buffer is categorical.

Parameters:

value[in] The value

inline bool set_shape(BufferShape value, bool force = true)#

Sets the shape.

If force is set, it will set the shape and possibly reset the buffer if the size has changed. If force is not set, it won’t allow changing shape to a different size.

Parameters:
  • value[in] The value

  • force[in] Whenever data may be reset.

Returns:

Whether the shape was set or not.

template<typename T>
inline bool set_type(bool force = true)#

Sets the type.

If force is set, it will set the shape and possibly reset the buffer if the type has changed. If force is not set, it won’t allow changing type.

Parameters:

force[in] Whenever data may be reset.

Template Parameters:

T – The desired type

Returns:

Whether the shape was set or not.

inline bool set_type(const std::string &value, bool force = true)#

Sets the type.

If force is set, it will set the shape and possibly reset the buffer if the type has changed. If force is not set, it won’t allow changing type.

Parameters:
  • force[in] Whenever data may be reset.

  • value[in] The desired type code.

Returns:

Whether the shape was set or not.

#include "navground/core/states/sensing.h"
class SensingState : public navground::core::EnvironmentState#

Generic state to hold data from sensors in keyed buffers.

Public Functions

inline SensingState()#

Construct an instance

inline const std::map<std::string, Buffer> &get_buffers() const#

Gets the buffers.

Returns:

The buffers.

inline Buffer *init_buffer(const std::string &key, const BufferDescription &desc, BufferType value)#

Initializes a buffer with a description and a uniform value.

Parameters:
  • key[in] The key

  • desc[in] The description of the buffer

  • value[in] The value to assign to the buffer

Returns:

The buffer if successfully initialized else a null pointer

inline Buffer *init_buffer(const std::string &key, const BufferDescription &desc)#

Initializes a buffer with description, with data set to zero.

Parameters:
  • key[in] The key

  • desc[in] The description of the buffer

Returns:

The buffer if successfully initialized else a null pointer

inline Buffer *init_buffer(const std::string &key, const BufferData &value)#

Initializes a flat buffer with data.

Parameters:
  • key[in] The key

  • value[in] The data

Returns:

The buffer if successfully initialized else a null pointer

inline void set_buffer(const std::string &key, const Buffer &value)#

Assign a buffer to a key.

Parameters:
  • key[in] The key

  • value[in] The value

inline Buffer *get_buffer(const std::string &key)#

Gets the buffer assigned to a key.

Parameters:

key[in] The key

Returns:

The buffer.