Skip to content

Commit

Permalink
NEW: views to convert between arrays and arrays of quaternions
Browse files Browse the repository at this point in the history
  • Loading branch information
nunupeke committed Nov 8, 2019
1 parent d2f33bf commit 11fa864
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/xquaternion.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <iostream>
#include "xtensor/xadapt.hpp"
#include "xtensor/xoffset_view.hpp"

namespace xt
Expand Down Expand Up @@ -95,6 +96,31 @@ namespace xt

#define offset_view(X, N) offset_view_impl<offsetof(typename decltype(X)::value_type, N)>(X)

// view an n-d xcontainer of quaternions as a "plain" (n+1)-d xcontainer
// (the last dimension of the returned view will be 4)
template <class E>
auto raw_view(E&& in) {
using value_type = typename std::decay_t<E>::value_type::value_type;
auto shape = in.shape();
std::vector<std::size_t> vshape(shape.begin(), shape.end());
vshape.push_back(4);
return xt::adapt(reinterpret_cast<value_type*>(in.data()),
in.size()*4, xt::no_ownership(), vshape);
}

// view an n-d xcontainer as an (n-1)-d xcontainer of quaternions
// (the last dimension of the input container must be 4)
template <class E>
auto quat_view(E&& in) {
using value_type = typename std::decay_t<E>::value_type;
auto shape = in.shape();
std::vector<std::size_t> vshape(shape.begin(), shape.end());
assert(vshape.back() == 4);
vshape.pop_back();
return xt::adapt(reinterpret_cast<xt::quaternion<value_type>*>(in.data()),
in.size()/4, xt::no_ownership(), vshape);
}

namespace math
{
namespace detail
Expand Down

0 comments on commit 11fa864

Please sign in to comment.