Skip to content

Commit

Permalink
Merge pull request #67 from ami-iit/example
Browse files Browse the repository at this point in the history
Added example and tested in CI
  • Loading branch information
S-Dafarra authored Apr 15, 2023
2 parents 390a88a + 58a0342 commit cf3ef39
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 9 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
fail-fast: false

steps:
- uses: actions/checkout@master
- uses: actions/checkout@v3

# Print environment variables to simplify development and debugging
- name: Environment Variables
Expand All @@ -41,7 +41,7 @@ jobs:
if: matrix.os == 'macOS-latest'
run: |
brew update
brew install libmatio
brew install libmatio eigen
- name: Dependencies [Ubuntu]
if: matrix.os == 'ubuntu-latest'
Expand All @@ -51,7 +51,7 @@ jobs:
- name: Cache Source-based Dependencies
id: cache-source-deps
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ${{ github.workspace }}/install/deps
# Including ${{ runner.temp }} is a workaround taken from https://github.com/robotology/whole-body-estimators/pull/62 to fix macos configuration failure on https://github.com/ami-iit/bipedal-locomotion-framework/pull/45
Expand Down Expand Up @@ -103,11 +103,21 @@ jobs:
shell: bash
run: |
cd build
cmake --build . --config ${{ matrix.build_type }}
cmake --build . --config ${{ matrix.build_type }} --target install
- name: Test
shell: bash
run: |
cd build
ctest --output-on-failure -C ${{ matrix.build_type }} .
- name: Test example
shell: bash
run: |
cd example
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH="${GITHUB_WORKSPACE}/install/deps;${GITHUB_WORKSPACE}/install" ..
cmake --build . --config ${{ matrix.build_type }}
./matiocpp_example
20 changes: 16 additions & 4 deletions .github/workflows/conda-forge-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: conda-incubator/setup-miniconda@v2
with:
Expand All @@ -40,24 +40,36 @@ jobs:
run: |
mkdir -p build
cd build
cmake -GNinja -DBUILD_TESTING:BOOL=ON -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} ..
cmake -GNinja -DBUILD_TESTING:BOOL=ON -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install ..
- name: Configure [Windows]
if: contains(matrix.os, 'windows')
shell: bash -l {0}
run: |
mkdir -p build
cd build
cmake -G"Visual Studio 16 2019" -DBUILD_TESTING:BOOL=ON -DDISABLE_PERMISSIVE:BOOL=ON -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} ..
cmake -G"Visual Studio 16 2019" -DBUILD_TESTING:BOOL=ON -DDISABLE_PERMISSIVE:BOOL=ON -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install ..
- name: Build
shell: bash -l {0}
run: |
cd build
cmake --build . --config ${{ matrix.build_type }}
cmake --build . --config ${{ matrix.build_type }} --target install
- name: Test
shell: bash -l {0}
run: |
cd build
ctest --output-on-failure -C ${{ matrix.build_type }}
- name: Test example
shell: bash -l {0}
run: |
cd example
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH="${GITHUB_WORKSPACE}/install/deps;${GITHUB_WORKSPACE}/install" -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install ..
cmake --build . --config ${{ matrix.build_type }} --target install
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}${GITHUB_WORKSPACE}/install/lib #Only for Ubuntu
export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH:+${DYLD_LIBRARY_PATH}:}${GITHUB_WORKSPACE}/install/lib #Only for macOS
${GITHUB_WORKSPACE}/install/bin/matiocpp_example
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# build folder
build/*
example/build/*

# emacs
*~
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- Added example. It is tested in CI. [#67](https://github.com/ami-iit/matio-cpp/pull/67)

## [0.2.2] - 2023-02-24
- Switch to use upstream repo of [`garbageslam/visit_struct`](https://github.com/garbageslam/visit_struct) instead of fork [`NikolausDemmel/visit_struct`](https://github.com/NikolausDemmel/visit_struct) [#58](https://github.com/ami-iit/matio-cpp/pull/58)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ It can be used for reading and writing binary MATLAB `.mat` files from C++, with
The depencies are [``CMake``](https://cmake.org/) (minimum version 3.10) and [``matio``](https://github.com/tbeu/matio). While we suggest to follow the build instructions provided in the [``matio`` home page](https://github.com/tbeu/matio), it can also installed from common package managers:
- Linux: ``sudo apt install libmatio-dev``
- macOS: ``brew install libmatio``
- Windows, via [``vcpkg``](https://github.com/microsoft/vcpkg): ``vcpkg install --triplet x64-windows matio``
- Windows (but also Linux and macOS), via [``conda``](https://docs.conda.io/en/latest/).

[`Eigen`](https://eigen.tuxfamily.org/index.php) is an optional dependency. If available, some conversions are defined.

Expand Down Expand Up @@ -175,6 +175,8 @@ testStruct s;
matioCpp::Struct automaticStruct = matioCpp::make_variable("testStruct", s);
```
# Example
You can check the example in the ``example`` folder on how to include and use ``matioCpp``.
# Known Limitations
- Complex arrays are not yet supported
Expand Down
10 changes: 10 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.5)
project(testMatioCpp CXX)

find_package(matioCpp REQUIRED)
find_package(Eigen3 REQUIRED)

add_executable(matiocpp_example main.cpp)
target_link_libraries(matiocpp_example PRIVATE matioCpp::matioCpp)

install(TARGETS matiocpp_example DESTINATION bin)
148 changes: 148 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#include <matioCpp/matioCpp.h>
struct testStruct{
double a;
double b;
std::vector<double> c;
};
VISITABLE_STRUCT(testStruct, a, b, c);

int main()
{
//Create a variable of type int named "hello_element"
matioCpp::Element<int> i("hello_element");
i = 8; //Set the value of the element

// Create a classical vector
std::vector<double> in = {2.0,4.0,6.0,8.0};
// Create a matioCpp vector
matioCpp::Vector<double> vector("test_vector");
vector = in; //It is possible to copy a standard vector into a matioCpp vector

std::vector<int> dataVec = {2,4,6,8};
matioCpp::MultiDimensionalArray<int> mdarray("md_array", {2,2}, dataVec.data()); //It is also possible to save vector data to multi-dimensional arrays, in this case of dimension 2x2. The data is parsed in column-major order

matioCpp::String testString("string_name"); //Create a string. The name of the string and the content are both "string_name"
testString = "string content"; //Change the content only to "string_content"

//Cell arrays are complex data structures including variables of different types. Each variable is stored without a name, and it can be accessed by index
//Cell arrays can be created given a vector of data to be inserted
std::vector<matioCpp::Variable> data;
data.emplace_back(matioCpp::Vector<double>("vector", 4));
data.emplace_back(matioCpp::Element<int>("element"));
data.emplace_back(matioCpp::Vector<double>());
data.emplace_back(matioCpp::MultiDimensionalArray<double>("array"));
data.emplace_back(matioCpp::String("name", "content"));
data.emplace_back(matioCpp::CellArray("otherCell"));
matioCpp::CellArray cell_array("test_cell", {1,2,3}, data); //Create a cell array with dimensions 1x2x3. The data is filled in column-major order
cell_array[{0,1,0}].asElement<int>() = 7; //edit the "element" integer
cell_array.setElement(2, vector); //edit the third element we added (the nameless vector of double). Since we needed to resize, we need to use the setElement method

//Struct is a container of different types of variables, with a given name
std::vector<matioCpp::Variable> dataStruct;
dataStruct.emplace_back(matioCpp::Vector<double>("vector", 4));
dataStruct.emplace_back(matioCpp::Element<int>("element"));
dataStruct.emplace_back(matioCpp::MultiDimensionalArray<double>("array"));
dataStruct.emplace_back(matioCpp::String("name", "content"));
dataStruct.emplace_back(matioCpp::Struct("otherStruct"));

matioCpp::Struct struct_test("struct", data);

struct_test["element"].asElement<int>() = 7; //The data can be edited given the name of the field.

//A struct array is a multidimensional array, where each element is a struct with the same fields (but different data).
std::vector<matioCpp::Struct> structVector(6, struct_test);
matioCpp::StructArray struct_array("struct_array", {1,2,3}, structVector); //Create a struct array of dimension 1x2x3

//
//make_variable is an handy way to directly convert a generic input to the corresponding matioCpp type
//

//If eigen is available, it is possible to easily convert vector and matrices
Eigen::Matrix3f eigenMatrix;
eigenMatrix << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;
auto toMatioMatrix = matioCpp::make_variable("eigenMatrix", eigenMatrix);

Eigen::Vector3i eigenVec;
eigenVec << 2, 4, 6;
auto toMatioEigenVec = matioCpp::make_variable("eigenVec", eigenVec);

//Conversions from common types
std::vector<double> stdVec = {1.0, 2.0, 3.0, 4.0, 5.0};
auto toMatioVec = matioCpp::make_variable("stdVec", stdVec);

std::array<float,3> array = {1.0, 2.0, 3.0};
auto toMatioArray = matioCpp::make_variable("array", array);

int classicalArray[] = {1, 2, 3};
auto toMatioClassic = matioCpp::make_variable("classicalArray", matioCpp::make_span(classicalArray, 3));

std::string string("something");
auto toMatioString = matioCpp::make_variable("string", string);

std::vector<bool> vecOfBool = {true, false, true};
auto toVecofBool = matioCpp::make_variable("vecOfBool", vecOfBool);

auto matioDouble = matioCpp::make_variable("double", 5.0);

auto matioBool = matioCpp::make_variable("bool", true);

auto matioInt = matioCpp::make_variable("int", 2);

auto matioChar = matioCpp::make_variable("char", 'f');

std::vector<std::string> stringVector = {"Huey", "Dewey", "Louie", ""};
auto matioCell = matioCpp::make_variable("stringVector", stringVector);

//It is possible to automatically convert a struct to a matioCpp::Struct
testStruct s;
matioCpp::Struct automaticStruct = matioCpp::make_variable("testStruct", s);

{
//Saving to file
matioCpp::File file = matioCpp::File::Create("test.mat"); //Create a new file called "test.mat"

//Saving to file
file.write(i);
file.write(testString);
file.write(vector);
file.write(cell_array);
file.write(struct_test);
file.write(struct_array);
file.write(mdarray);
file.write(toMatioMatrix);
file.write(toMatioEigenVec);
file.write(toMatioArray);
file.write(toMatioClassic);
file.write(toMatioString);
file.write(toVecofBool);
file.write(matioDouble);
file.write(matioBool);
file.write(matioInt);
file.write(matioChar);
file.write(matioCell);
file.write(automaticStruct);
//File is automatically closed at destruction. Alternatively, use file.close();.
}

//It is possible to read mat files too.

matioCpp::File input("test.mat");

//Read again the data that have been saved.
auto matioCppVector = input.read("test_vector").asVector<double>();
Eigen::VectorXd outEigenVector = matioCpp::to_eigen(matioCppVector);

auto matioCppMatrix = input.read("eigenMatrix").asMultiDimensionalArray<float>();
Eigen::MatrixXf outMatrix = matioCpp::to_eigen(matioCppMatrix);

int element = input.read("hello_element").asElement<int>();

std::string read_string = input.read("string_name").asString()();

matioCpp::Struct outStruct = input.read("struct").asStruct();
matioCpp::StructArray outStructArray = input.read("struct_array").asStructArray();

matioCpp::CellArray outCellArray = input.read("test_cell").asCellArray();

return EXIT_SUCCESS;
}

0 comments on commit cf3ef39

Please sign in to comment.