From e12882732265ab1075bb2751b22cf51c17d48910 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 28 Jul 2017 10:10:55 +0100 Subject: [PATCH] Add sample code --- samples/CMakeLists.txt | 32 ++++++++++ samples/cmake/modules/FindGLM.cmake | 49 ++++++++++++++++ samples/dummy_functions.cpp | 28 +++++++++ samples/dummy_functions.hpp | 26 +++++++++ samples/main.cpp | 42 +++++++++++++ samples/one_way_conversion.cpp | 59 +++++++++++++++++++ samples/one_way_conversion.hpp | 22 +++++++ samples/vec.hpp | 91 +++++++++++++++++++++++++++++ 8 files changed, 349 insertions(+) create mode 100644 samples/CMakeLists.txt create mode 100644 samples/cmake/modules/FindGLM.cmake create mode 100644 samples/dummy_functions.cpp create mode 100644 samples/dummy_functions.hpp create mode 100644 samples/main.cpp create mode 100644 samples/one_way_conversion.cpp create mode 100644 samples/one_way_conversion.hpp create mode 100644 samples/vec.hpp diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt new file mode 100644 index 0000000..bf75a0d --- /dev/null +++ b/samples/CMakeLists.txt @@ -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 +) diff --git a/samples/cmake/modules/FindGLM.cmake b/samples/cmake/modules/FindGLM.cmake new file mode 100644 index 0000000..5e4af3a --- /dev/null +++ b/samples/cmake/modules/FindGLM.cmake @@ -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) \ No newline at end of file diff --git a/samples/dummy_functions.cpp b/samples/dummy_functions.cpp new file mode 100644 index 0000000..3828150 --- /dev/null +++ b/samples/dummy_functions.cpp @@ -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 + +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"; +} diff --git a/samples/dummy_functions.hpp b/samples/dummy_functions.hpp new file mode 100644 index 0000000..7b3b5f0 --- /dev/null +++ b/samples/dummy_functions.hpp @@ -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 +#include + +void print_eigen_vector3 (const Eigen::Vector3f& parVec); +void print_glm_vector3 (const glm::vec3& parVec); + +#endif diff --git a/samples/main.cpp b/samples/main.cpp new file mode 100644 index 0000000..7decf34 --- /dev/null +++ b/samples/main.cpp @@ -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 + +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(); + //you can expect them to have the same address in this case + assert(reinterpret_cast(&glm_ref) == reinterpret_cast(&eigen_wrapped)); + print_glm_vector3(glm_ref.data()); + + //more examples here + one_way_conversion(); + + return 0; +} diff --git a/samples/one_way_conversion.cpp b/samples/one_way_conversion.cpp new file mode 100644 index 0000000..cc3caf5 --- /dev/null +++ b/samples/one_way_conversion.cpp @@ -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 + +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> : + is_castable_to, + is_castable_to + { + enum { dimensions = 3 }; + typedef float scalar_type; + typedef std::vector 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>; +} //unnamed namespace + +void one_way_conversion() { + vvec3 heap_vector(10.0f, 11.0f, 12.0f); + + std::cout << "wrapped std::vector = " << heap_vector << '\n'; + + //casting to glm and eigen is possible since we explicitly allowed it + auto& eigen_ref = heap_vector.cast(); + + //...but the other way around won't compile + //vvec3& not_working = eigen_ref.cast(); + + print_eigen_vector3(eigen_ref.data()); +} diff --git a/samples/one_way_conversion.hpp b/samples/one_way_conversion.hpp new file mode 100644 index 0000000..d1f5d08 --- /dev/null +++ b/samples/one_way_conversion.hpp @@ -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 diff --git a/samples/vec.hpp b/samples/vec.hpp new file mode 100644 index 0000000..63603fd --- /dev/null +++ b/samples/vec.hpp @@ -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 +#include + +namespace vwr { + //Setup wrappers for Eigen vectors + template <> + struct VectorWrapperInfo : + is_castable_to + { + 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 : + is_castable_to + { + 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 { + 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 { + 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 evec2; +typedef vwr::Vec evec3; +typedef vwr::Vec gvec2; +typedef vwr::Vec gvec3; + +#endif