Skip to content

Commit

Permalink
Merge pull request #56 from diegoferigo/WB3.0
Browse files Browse the repository at this point in the history
WB-Toolbox 3.0
  • Loading branch information
diegoferigo authored Jan 25, 2018
2 parents 3f8895a + 198e44b commit be84398
Show file tree
Hide file tree
Showing 98 changed files with 8,889 additions and 11,821 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Copyright (C) 2013-2015 Instituto Italiano di Tecnologia
# Author: Jorhabib Eljaik, Francesco Romano
# Copyright (C) 2013-2017 iCub Facility - Istituto Italiano di Tecnologia
# Author: Jorhabib Eljaik, Francesco Romano, Diego Ferigo
# CopyPolicy: Released under the terms of the GNU GPL v2.0.

cmake_minimum_required(VERSION 2.8.11)

cmake_minimum_required(VERSION 3.3)
project(WB-Toolbox)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(YCM REQUIRED)
include(YCMDefaultDirs)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(WB-TOOLBOX_SHARE_DIR "${CMAKE_INSTALL_PREFIX}/share/WB-Toolbox")

add_subdirectory(MxAnyType)
add_subdirectory(toolbox)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/images DESTINATION ${WB-TOOLBOX_SHARE_DIR})
Expand Down
51 changes: 51 additions & 0 deletions MxAnyType/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
cmake_policy(SET CMP0048 NEW)
project(MxAnyType LANGUAGES CXX VERSION 0.1)
cmake_minimum_required(VERSION 3.0.2)

# Configure the project
# =====================
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Use a general prefix for the project
set(VARS_PREFIX ${PROJECT_NAME})
# set(VARS_PREFIX "MxAnyType")

# Build the library
# =================

# Export the includes needed to who inherits this library
# Set this up properly for handling either BUILD and INSTALL trees
set(${VARS_PREFIX}_INCLUDE_BUILD ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(${VARS_PREFIX}_INCLUDE_INSTALL ${CMAKE_INSTALL_INCLUDEDIR}/${VARS_PREFIX})

# Add the target
add_library(${VARS_PREFIX} STATIC ${CMAKE_CURRENT_SOURCE_DIR}/include/AnyType.h
${CMAKE_CURRENT_SOURCE_DIR}/include/MxAnyType.h
${CMAKE_CURRENT_SOURCE_DIR}/src/MxAnyType.cpp)


# TODO: temporary, waiting the library becomes a shared
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
set_target_properties(${VARS_PREFIX} PROPERTIES COMPILE_FLAGS "-fPIC")
endif()

# Find Matlab resources
find_package(Matlab REQUIRED MX_LIBRARY)
target_include_directories(${VARS_PREFIX} SYSTEM PUBLIC "${Matlab_INCLUDE_DIRS}")

# Setup the include directories
# TODO why in the how to was INTERFACE?
target_include_directories(${VARS_PREFIX} PUBLIC
$<BUILD_INTERFACE:${${VARS_PREFIX}_INCLUDE_BUILD}>
$<INSTALL_INTERFACE:${${VARS_PREFIX}_INCLUDE_INSTALL}>)

# Assign some useful properties
# set_target_properties(${VARS_PREFIX} PROPERTIES VERSION ${PROJECT_VERSION}
# PUBLIC_HEADER MxAnyType.h)

# Build tests
# ===========
# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
61 changes: 61 additions & 0 deletions MxAnyType/include/AnyType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef ANYTYPE_H
#define ANYTYPE_H

#include <vector>
#include <unordered_map>
#include <memory>

class AnyType;

typedef std::shared_ptr<AnyType> AnyTypeSPtr;
typedef std::vector<AnyTypeSPtr> AnyCell;
typedef std::unordered_map<std::string, AnyTypeSPtr> AnyStruct;

class AnyType
{
protected:

public:
AnyType() = default;
virtual ~AnyType() = default;

// Integers
virtual bool asInt(int& i) = 0;
// virtual bool asInt8(int8_t& i) = 0;
// virtual bool asInt16(int16_t& i) = 0;
virtual bool asInt32(int32_t& i) = 0;
// virtual bool asInt64(int64_t& i) = 0;

// Unsigned Integers
virtual bool asUInt(unsigned& i) = 0;
// virtual bool asUInt8(uint8_t& i) = 0;
// virtual bool asUInt16(uint16_t& i) = 0;
// virtual bool asUInt32(uint32_t& i) = 0;
// virtual bool asUInt64(uint64_t& i) = 0;

// Boolean
virtual bool asBool(bool& b) = 0;

// Floating-point
// virtual bool asFloat(float& f) = 0;
virtual bool asDouble(double& d) = 0;

// Characters
virtual bool asString(std::string& s) = 0;

// Struct
virtual bool asAnyStruct(AnyStruct& map) = 0;

// Cell array
virtual bool asAnyCell(AnyCell& map) = 0;

// Matrix
// TODO: constraint max 2-dimension
// virtual bool asMatrixFloat(Eigen::MatrixXf mat) = 0;
// virtual bool asMatrixDouble(Eigen::MatrixXd mat) = 0;

// Vector
virtual bool asVectorDouble(std::vector<double>& vec) = 0;
};

#endif /* ANYTYPE_H */
98 changes: 98 additions & 0 deletions MxAnyType/include/MxAnyType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef MXANYTYPE_H
#define MXANYTYPE_H

#include <vector>
#include <string>
#include <unordered_map>
#include <cassert>
#include "AnyType.h"
#include "matrix.h"

class MxAnyType;

// If needed in the future
// class MxAnyCell : public AnyCell {};
// class MxAnyStruct : public AnyStruct {};

struct MxArrayMetadata {
mxClassID id;
bool isScalar;
size_t rows;
size_t cols;
size_t nElem;
size_t nDims;
std::vector<size_t> dims;
};

class MxAnyType : public AnyType
{
private:
const mxArray* mx;
MxArrayMetadata md;
bool validate;

// TODO: https://it.mathworks.com/help/matlab/apiref/mxgetscalar.html returns a double always
bool asScalar(double& d);
bool validateClassId(mxClassID id1, mxClassID id2);

// mxArray* getMxArrayPtr() const { return static_cast<mxArray*>(getAnyDataPtr()); }

public:
// void* getAnyDataPtr() const override {
// return (void*) mx;
// }

MxAnyType();
MxAnyType(const mxArray* m, bool validateId = false);
~MxAnyType() = default;
MxAnyType(const MxAnyType& mxAnyType);

void enableClassIDValidation();

// STRING / CHARS
// ==============

bool asString(std::string& s) override;

// SCALAR TYPES
// ============

// Generic casting
// ---------------

bool asInt(int& i) override;
bool asUInt(unsigned& i) override;

// Specific casting
// ----------------

bool asInt32(int32_t& i) override;

// TODO: complete with all the other scalar types
// bool asInt64(int64_t& i) override
// {
// double buffer;
// if (!asScalar(buffer)) return false;
// i = static_cast<int64_t>(buffer);
// return validateClassId(md.id, mxINT64_CLASS);
// }

bool asDouble(double& d) override;
bool asBool(bool& b) override;

// COMPOSITE DATA TYPES
// ====================

bool asAnyStruct(AnyStruct& s) override;
bool asAnyCell(AnyCell& cell) override;

// MATRIX
// ======

// VECTOR
// ======

bool asVectorDouble(std::vector<double>& vec) override;
};

#endif /* MXANYTYPE_H */
Loading

0 comments on commit be84398

Please sign in to comment.