Make scalar_type and vector_type public in Vec.

- Add a dot product implementation.
- Test code to demonstrate dot product.
- Test code to demonstrate wrapping structs with unordered properties.
This commit is contained in:
King_DuckZ 2015-07-25 19:41:16 +02:00
parent 40ca5cd388
commit 958de67849
4 changed files with 71 additions and 3 deletions

View File

@ -0,0 +1,22 @@
#ifndef id8949C80C36BA42CABC49EA4C1DB54BC7
#define id8949C80C36BA42CABC49EA4C1DB54BC7
#include "vectorwrapper.hpp"
#include <type_traits>
#include <cstddef>
namespace vwr {
template <typename V1, typename V2, std::size_t S>
typename std::common_type<typename Vec<V1>::scalar_type, typename Vec<V2>::scalar_type>::type dot ( const Vec<V1, S>& parLeft, const Vec<V2, S>& parRight );
template <typename V1, typename V2, std::size_t S>
inline typename std::common_type<typename Vec<V1>::scalar_type, typename Vec<V2>::scalar_type>::type dot (const Vec<V1, S>& parLeft, const Vec<V2, S>& parRight) {
auto retval = parLeft.x() * parRight.x();
for (int z = 1; z < S; ++z) {
retval += parLeft[z] * parRight[z];
}
return retval;
}
} //namespace vwr
#endif

View File

@ -292,9 +292,9 @@ namespace vwr {
template <typename V>
class Vec<V, 1> : public implem::VecBase<V>, public implem::VecAccessors<V, 1> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::vector_type vector_type;
typedef typename implem::VecBase<V>::scalar_type scalar_type;
public:
enum {
dimensions = 1
};
@ -317,9 +317,9 @@ namespace vwr {
class Vec<V, 2> : public implem::VecBase<V>, public implem::VecAccessors<V, 2> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
static_assert(std::is_standard_layout<implem::VecAccessors<V, 2>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::scalar_type scalar_type;
typedef typename implem::VecBase<V>::vector_type vector_type;
public:
enum {
dimensions = 2
};
@ -344,9 +344,9 @@ namespace vwr {
class Vec<V, 3> : public implem::VecBase<V>, public implem::VecAccessors<V, 3> {
static_assert(std::is_standard_layout<implem::VecBase<V>>::value, "Base class must be a standard layout type");
static_assert(std::is_standard_layout<implem::VecAccessors<V, 3>>::value, "Base class must be a standard layout type");
public:
typedef typename implem::VecBase<V>::scalar_type scalar_type;
typedef typename implem::VecBase<V>::vector_type vector_type;
public:
enum {
dimensions = 3
};

View File

@ -3,6 +3,7 @@ project(unit CXX)
add_executable(${PROJECT_NAME}
${GTEST_MAIN_CPP}
test_conversions.cpp
test_ops.cpp
)
target_link_libraries(${PROJECT_NAME}

45
test/unit/test_ops.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "sample_vectors.hpp"
#include "vectorwrapper/vectorops.hpp"
#include <gtest/gtest.h>
struct UnorderedVector {
int64_t y;
int64_t z;
int64_t x;
};
namespace vwr {
template <>
struct VectorWrapperInfo<UnorderedVector> {
enum { dimensions = 3 };
typedef int64_t scalar_type;
enum {
offset_x = offsetof(UnorderedVector, x),
offset_y = offsetof(UnorderedVector, y),
offset_z = offsetof(UnorderedVector, z)
};
};
} //namespace vwr
typedef vwr::Vec<UnorderedVector> uvec3i;
TEST(vwr, ops) {
using namespace vwr;
ivec3 a(1);
ivec3 b(1);
EXPECT_EQ(3, dot(a, b));
EXPECT_EQ(3, dot(a, a));
a = ivec3{5, 2, 8};
b = ivec3{6, 9, 3};
EXPECT_EQ(6*5+2*9+8*3, dot(a, b));
uvec3i c(7, 2, 9);
EXPECT_EQ(7, c.x());
EXPECT_EQ(2, c.y());
EXPECT_EQ(9, c.z());
EXPECT_EQ(7*5+2*2+9*8, dot(a, c));
EXPECT_EQ(7*6+2*9+9*3, dot(b, c));
}