wrapper_vec.h 1.76 KB
Newer Older
1 2 3
/* ALTA --- Analysis of Bidirectional Reflectance Distribution Functions

   Copyright (C) 2015 Université de Montréal
4
   Copyright (C) 2018 Unity
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

   This file is part of ALTA.

   This Source Code Form is subject to the terms of the Mozilla Public
   License, v. 2.0.  If a copy of the MPL was not distributed with this
   file, You can obtain one at http://mozilla.org/MPL/2.0/.  */

#pragma once

// Pybind11 includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;

// ALTA include
#include <core/common.h>

// STL include
#include <iostream>

/* Specific function call to set a vector's element
 */
static void vec_set_item(vec& x, int i, double a) {
	x[i] = a;
}

/* Operators on vec
 */
static vec vec_add(const vec& a, const vec& b) {
   return a + b;
}
static vec vec_sub(const vec& a, const vec& b) {
   return a - b;
}

/* Specific convert a vec to a string
 */
static std::string vec_str(const vec& x) {
   std::stringstream stream;
   stream << "[";
   for(int i=0; i<x.size(); ++i) {
      if(i > 0) { stream << ", "; }
      stream << x[i];
   }
   stream << "]";
   return stream.str();
}

/* Register the vector class to python
 */
inline void register_vec(py::module& m) {

	py::class_<vec>(m, "vec")
        .def(py::init<vec>())
        .def(py::init([]( const std::vector<double>& a) {
            vec v(a.size());
            memcpy((void*)v.data(), (const void*)a.data(), a.size()*sizeof(double));
            return v;
        }))
		.def("__add__", &vec_add)
		.def("__sub__", &vec_sub)
66
		// .def("__len__", &vec::size)
67
        .def("__getitem__", [](const vec &s, unsigned int i) {
68 69 70 71 72 73
            if (i >= s.size()) throw py::index_error();
            return s[i];
        })
		.def("__setitem__", &vec_set_item)
		.def("__str__", &vec_str);
}