Compare commits

...

2 Commits

9 changed files with 385 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "vectorwrapper.hpp"
#include "size_type.hpp"
#include <iostream>
namespace vwr {
template <typename T, size_type S>
inline
std::ostream& operator<< (std::ostream& parStream, const Vec<T, S>& parVec) {
static_assert(S > 0, "Invalid size");
parStream << '<';
for (size_type z = 0; z < S - 1; ++z) {
parStream << parVec[z] << ',';
}
parStream << parVec[S - 1] << '>';
return parStream;
}
} //namespace vwr

32
samples/CMakeLists.txt Normal file
View File

@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(samples CXX)
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
find_package(GLM REQUIRED)
find_package(Eigen3 REQUIRED)
add_executable(${PROJECT_NAME}
main.cpp
one_way_conversion.cpp
dummy_functions.cpp
)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
target_link_libraries(${PROJECT_NAME}
)
target_compile_definitions(${PROJECT_NAME}
PRIVATE VWR_WITH_IMPLICIT_CONVERSIONS
PRIVATE VWR_EXTRA_ACCESSORS
)
target_include_directories(${PROJECT_NAME} SYSTEM
PRIVATE ${GLM_INCLUDE_DIRS}
PRIVATE ${EIGEN3_INCLUDE_DIR}
)
target_include_directories(${PROJECT_NAME}
PRIVATE ${CMAKE_SOURCE_DIR}/../include
)

View File

@ -0,0 +1,49 @@
# Try to find GLM : OpenGL Mathematics.
# This module defines
# - GLM_INCLUDE_DIRS
# - GLM_FOUND
#
# The following variables can be set as arguments for the module.
# - GLM_ROOT_DIR : Root library directory of GLM
#
# References:
# - https://github.com/Groovounet/glm/blob/master/util/FindGLM.cmake
# - https://bitbucket.org/alfonse/gltut/src/28636298c1c0/glm-0.9.0.7/FindGLM.cmake
#
# Additional modules
include(FindPackageHandleStandardArgs)
if (WIN32)
# Find include files
find_path(
GLM_INCLUDE_DIR
NAMES glm/glm.hpp
PATHS
$ENV{PROGRAMFILES}/include
${GLM_ROOT_DIR}/include
DOC "The directory where glm/glm.hpp resides")
else()
# Find include files
find_path(
GLM_INCLUDE_DIR
NAMES glm/glm.hpp
PATHS
/usr/include
/usr/local/include
/sw/include
/opt/local/include
${GLM_ROOT_DIR}/include
DOC "The directory where glm/glm.hpp resides")
endif()
# Handle REQUIRD argument, define *_FOUND variable
find_package_handle_standard_args(GLM DEFAULT_MSG GLM_INCLUDE_DIR)
# Define GLM_INCLUDE_DIRS
if (GLM_FOUND)
set(GLM_INCLUDE_DIRS ${GLM_INCLUDE_DIR})
endif()
# Hide some variables
mark_as_advanced(GLM_INCLUDE_DIR)

View File

@ -0,0 +1,28 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dummy_functions.hpp"
#include <iostream>
void print_eigen_vector3 (const Eigen::Vector3f& parVec) {
std::cout << "Eigen::Vector3f = <" <<
parVec.x() << ',' << parVec.y() << ',' << parVec.z() << ">\n";
}
void print_glm_vector3 (const glm::vec3& parVec) {
std::cout << "glm::vec3 = <" <<
parVec.x << ',' << parVec.y << ',' << parVec.z << ">\n";
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef id4330EF2B340B4216A53428E3A1050717
#define id4330EF2B340B4216A53428E3A1050717
#include <Eigen/Core>
#include <glm/glm.hpp>
void print_eigen_vector3 (const Eigen::Vector3f& parVec);
void print_glm_vector3 (const glm::vec3& parVec);
#endif

42
samples/main.cpp Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "vec.hpp"
#include "dummy_functions.hpp"
#include "one_way_conversion.hpp"
#include <cassert>
int main() {
//just a 2D GLM vector
gvec2 sample_2d(0.0f);
//use sizzling to convert to a 3D vector and assign to an Eigen vector
evec3 eigen_wrapped = sample_2d.xy1();
//invoke some dummy function that takes an Eigen vector
print_eigen_vector3(eigen_wrapped.data());
//let's call the GLM dummy function with the same data now
const gvec3& glm_ref = eigen_wrapped.cast<gvec3>();
//you can expect them to have the same address in this case
assert(reinterpret_cast<intptr_t>(&glm_ref) == reinterpret_cast<intptr_t>(&eigen_wrapped));
print_glm_vector3(glm_ref.data());
//more examples here
one_way_conversion();
return 0;
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "one_way_conversion.hpp"
#include "vectorwrapper/vector_ostream.hpp"
#include "dummy_functions.hpp"
#include "vec.hpp"
#include <vector>
namespace vwr {
//if you want to go crazy you can do one way inplace casting even if
//the wrapped type's storage is not on the stack
template <>
struct VectorWrapperInfo<std::vector<float>> :
is_castable_to<Eigen::Vector3f>,
is_castable_to<glm::vec3>
{
enum { dimensions = 3 };
typedef float scalar_type;
typedef std::vector<float> vector_type;
static scalar_type& get_at (size_type parIndex, vector_type& parVector) {
if (parVector.empty())
parVector.resize(3);
return parVector[parIndex];
}
};
} //namespace vwr
namespace {
using vvec3 = vwr::Vec<std::vector<float>>;
} //unnamed namespace
void one_way_conversion() {
vvec3 heap_vector(10.0f, 11.0f, 12.0f);
std::cout << "wrapped std::vector<float> = " << heap_vector << '\n';
//casting to glm and eigen is possible since we explicitly allowed it
auto& eigen_ref = heap_vector.cast<evec3>();
//...but the other way around won't compile
//vvec3& not_working = eigen_ref.cast<vvec3>();
print_eigen_vector3(eigen_ref.data());
}

View File

@ -0,0 +1,22 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef idDB273852F16D4044AE0ED6C72E192527
#define idDB273852F16D4044AE0ED6C72E192527
void one_way_conversion();
#endif

91
samples/vec.hpp Normal file
View File

@ -0,0 +1,91 @@
/*
* Copyright 2015-2017 Michele "King_DuckZ" Santullo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef idD03A8672B1F74408AC6F3B11409CB786
#define idD03A8672B1F74408AC6F3B11409CB786
#include "vectorwrapper/vectorwrapper.hpp"
#include <Eigen/Core>
#include <glm/glm.hpp>
namespace vwr {
//Setup wrappers for Eigen vectors
template <>
struct VectorWrapperInfo<Eigen::Vector2f> :
is_castable_to<glm::vec2>
{
enum { dimensions = 2 };
typedef float scalar_type;
typedef Eigen::Vector3f higher_vector_type;
typedef Eigen::Vector2f vector_type;
static scalar_type& get_at (size_type parIndex, vector_type& parVector) {
return parVector.coeffRef(parIndex);
}
};
template <>
struct VectorWrapperInfo<Eigen::Vector3f> :
is_castable_to<glm::vec3>
{
enum { dimensions = 3 };
typedef float scalar_type;
typedef Eigen::Vector2f lower_vector_type;
typedef Eigen::Vector3f vector_type;
static scalar_type& get_at (size_type parIndex, vector_type& parVector) {
return parVector.coeffRef(parIndex);
}
};
//Setup wrappers for GLM vectors
template <>
struct VectorWrapperInfo<glm::vec2> {
enum { dimensions = 2 };
typedef float scalar_type;
typedef glm::vec3 higher_vector_type;
typedef glm::vec2 vector_type;
enum {
offset_x = offsetof(glm::vec3, x),
offset_y = offsetof(glm::vec3, y)
};
};
template <>
struct VectorWrapperInfo<glm::vec3> {
enum { dimensions = 3 };
typedef float scalar_type;
typedef glm::vec2 lower_vector_type;
typedef glm::vec3 vector_type;
enum {
offset_x = offsetof(glm::vec3, x),
offset_y = offsetof(glm::vec3, y),
offset_z = offsetof(glm::vec3, z)
};
};
} //namespace vwr
typedef vwr::Vec<Eigen::Vector2f> evec2;
typedef vwr::Vec<Eigen::Vector3f> evec3;
typedef vwr::Vec<glm::vec2> gvec2;
typedef vwr::Vec<glm::vec3> gvec3;
#endif