HighMap library (C++)
Loading...
Searching...
No Matches
hmap::Tensor Class Reference

A class to represent a multi-dimensional tensor. More...

#include <tensor.hpp>

Collaboration diagram for hmap::Tensor:

Public Member Functions

 Tensor (Vec3< int > shape)
 Construct a new Tensor object.
 
 Tensor (Vec2< int > shape_xy, int shape_z)
 Construct a new Tensor object.
 
 Tensor (const std::string &fname)
 Constructs a new Tensor object from a file.
 
float & operator() (int i, int j, int k)
 Access an element of the tensor.
 
const float & operator() (int i, int j, int k) const
 Access an element of the tensor (const version).
 
Array get_slice (int k) const
 Get a 2D slice of the tensor along the z-axis.
 
float max () const
 Find the maximum value in the tensor.
 
float min () const
 Find the minimum value in the tensor.
 
void remap (float vmin=0.f, float vmax=1.f)
 Remap the tensor values to a new range.
 
Tensor resample_to_shape_xy (Vec2< int > new_shape_xy)
 Resamples the tensor to a new 2D shape (x, y), shape along z is not changed.
 
void set_slice (int k, const Array &slice)
 Set a 2D slice of the tensor along the z-axis.
 
cv::Mat to_cv_mat ()
 Convert the tensor to an OpenCV matrix.
 
std::vector< uint8_t > to_img_8bit ()
 Convert the tensor to an 8-bit image represented as a vector.
 
void to_png (const std::string &fname, int depth=CV_8U)
 Saves the Tensor as a PNG image file.
 

Public Attributes

Vec3< int > shape
 Shape of the tensor in 3D space.
 
std::vector< float > vector
 Flattened vector containing the tensor's elements.
 

Detailed Description

A class to represent a multi-dimensional tensor.

The Tensor class is used to store and manipulate multi-dimensional arrays (tensors) of floating-point numbers. It supports element access, basic operations such as finding the minimum and maximum values, remapping the tensor values, and exporting the data to images.

Constructor & Destructor Documentation

◆ Tensor() [1/3]

hmap::Tensor::Tensor ( Vec3< int >  shape)

Construct a new Tensor object.

Parameters
shape3D shape of the tensor.

◆ Tensor() [2/3]

hmap::Tensor::Tensor ( Vec2< int >  shape_xy,
int  shape_z 
)

Construct a new Tensor object.

Parameters
shape_xy2D shape (x, y) of the tensor.
shape_zSize along the z-axis.

◆ Tensor() [3/3]

hmap::Tensor::Tensor ( const std::string &  fname)

Constructs a new Tensor object from a file.

Parameters
fnameThe name of the file to load the tensor from.

Member Function Documentation

◆ operator()() [1/2]

float & hmap::Tensor::operator() ( int  i,
int  j,
int  k 
)

Access an element of the tensor.

Parameters
iIndex along the x-axis.
jIndex along the y-axis.
kIndex along the z-axis.
Returns
float& Reference to the element at position (i, j, k).

◆ operator()() [2/2]

const float & hmap::Tensor::operator() ( int  i,
int  j,
int  k 
) const

Access an element of the tensor (const version).

<This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
iIndex along the x-axis.
jIndex along the y-axis.
kIndex along the z-axis.
Returns
const float& Reference to the element at position (i, j, k).

◆ get_slice()

Array hmap::Tensor::get_slice ( int  k) const

Get a 2D slice of the tensor along the z-axis.

Parameters
kIndex along the z-axis.
Returns
An Array representing the 2D slice of the tensor at the specified z-axis index.

◆ max()

float hmap::Tensor::max ( ) const

Find the maximum value in the tensor.

Returns
float Maximum value in the tensor.

◆ min()

float hmap::Tensor::min ( ) const

Find the minimum value in the tensor.

Returns
float Minimum value in the tensor.

◆ remap()

void hmap::Tensor::remap ( float  vmin = 0.f,
float  vmax = 1.f 
)

Remap the tensor values to a new range.

Parameters
vminMinimum value of the new range (default is 0.0).
vmaxMaximum value of the new range (default is 1.0).

◆ resample_to_shape_xy()

Tensor hmap::Tensor::resample_to_shape_xy ( Vec2< int >  new_shape_xy)

Resamples the tensor to a new 2D shape (x, y), shape along z is not changed.

Parameters
new_shape_xyA 2D vector representing the new shape (x, y) of the tensor.
Returns
A new Tensor object with the resampled shape.

◆ set_slice()

void hmap::Tensor::set_slice ( int  k,
const Array slice 
)

Set a 2D slice of the tensor along the z-axis.

Parameters
kIndex along the z-axis.
sliceThe 2D array (slice) to set.

◆ to_cv_mat()

cv::Mat hmap::Tensor::to_cv_mat ( )

Convert the tensor to an OpenCV matrix.

Returns
cv::Mat OpenCV matrix representing the tensor.

◆ to_img_8bit()

std::vector< uint8_t > hmap::Tensor::to_img_8bit ( )

Convert the tensor to an 8-bit image represented as a vector.

Returns
std::vector<uint8_t> Vector containing the 8-bit image data.

◆ to_png()

void hmap::Tensor::to_png ( const std::string &  fname,
int  depth = CV_8U 
)

Saves the Tensor as a PNG image file.

This function converts the Tensor into a cv::Mat object and then saves it as a PNG image file. The image can be saved in either 8-bit or 16-bit depth, based on the depth parameter. The image is rotated 90 degrees counterclockwise before saving.

Parameters
fnameThe filename for the output PNG image, including the path.
depthThe bit depth of the output image. The default is 8-bit (CV_8U). Set to CV_16U for 16-bit output.

Member Data Documentation

◆ shape

Vec3<int> hmap::Tensor::shape

Shape of the tensor in 3D space.

◆ vector

std::vector<float> hmap::Tensor::vector

Flattened vector containing the tensor's elements.


The documentation for this class was generated from the following files: