vector.cpp 4.6 KB
Newer Older
1
2
// Copyright (C) 2013  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
3

4
#include <dlib/python.h>
5
#include <dlib/matrix.h>
Patrick Snape's avatar
Patrick Snape committed
6
#include <dlib/geometry/vector.h>
7
#include <pybind11/stl_bind.h>
8
#include "indexing.h"
9
10
11
12
13
14

using namespace dlib;
using namespace std;

typedef matrix<double,0,1> cv;

15
16
PYBIND11_MAKE_OPAQUE(std::vector<point>);

17
18
19
20
21
22
void cv_set_size(cv& m, long s)
{
    m.set_size(s);
    m = 0;
}

23
24
25
26
27
double dotprod ( const cv& a, const cv& b)
{
    return dot(a,b);
}

28
string cv__str__(const cv& v)
29
30
{
    ostringstream sout;
31
32
33
34
35
36
37
38
39
40
41
42
43
    for (long i = 0; i < v.size(); ++i)
    {
        sout << v(i);
        if (i+1 < v.size())
            sout << "\n";
    }
    return sout.str();
}

string cv__repr__ (const cv& v)
{
    std::ostringstream sout;
    sout << "dlib.vector([";
44
    for (long i = 0; i < v.size(); ++i)
45
46
47
48
49
50
    {
        sout << v(i);
        if (i+1 < v.size())
            sout << ", ";
    }
    sout << "])";
51
52
53
    return sout.str();
}

54
std::shared_ptr<cv> cv_from_object(py::object obj)
55
{
56
57
58
    try {
        long nr = obj.cast<long>();
        auto temp = std::make_shared<cv>(nr);
59
60
        *temp = 0;
        return temp;
61
62
    } catch(py::cast_error &e) {
        py::list li = obj.cast<py::list>();
63
        const long nr = len(obj);
64
        auto temp = std::make_shared<cv>(nr);
65
66
        for ( long r = 0; r < nr; ++r)
        {
67
            (*temp)(r) = li[r].cast<double>();
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        }
        return temp;
    }
}

long cv__len__(cv& c)
{
    return c.size();
}


void cv__setitem__(cv& c, long p, double val)
{
    if (p < 0) {
        p = c.size() + p; // negative index
    }
    if (p > c.size()-1) {
Patrick Snape's avatar
Patrick Snape committed
85
86
        PyErr_SetString( PyExc_IndexError, "index out of range"
        );
87
        throw py::error_already_set();
88
89
90
91
92
93
94
95
96
97
    }
    c(p) = val;
}

double cv__getitem__(cv& m, long r)
{
    if (r < 0) {
        r = m.size() + r; // negative index
    }
    if (r > m.size()-1 || r < 0) {
Patrick Snape's avatar
Patrick Snape committed
98
99
        PyErr_SetString( PyExc_IndexError, "index out of range"
        );
100
        throw py::error_already_set();
101
102
103
104
105
    }
    return m(r);
}


106
cv cv__getitem2__(cv& m, py::slice r)
107
{
108
109
110
    size_t start, stop, step, slicelength;
    if (!r.compute(m.size(), &start, &stop, &step, &slicelength))
        throw py::error_already_set();
111

112
    cv temp(slicelength);
113

114
115
    for (size_t i = 0; i < slicelength; ++i) {
         temp(i) = m(start); start += step;
116
117
118
119
    }
    return temp;
}

120
py::tuple cv_get_matrix_size(cv& m)
121
{
122
    return py::make_tuple(m.nr(), m.nc());
123
124
}

Patrick Snape's avatar
Patrick Snape committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// ----------------------------------------------------------------------------------------

string point__repr__ (const point& p)
{
    std::ostringstream sout;
    sout << "point(" << p.x() << ", " << p.y() << ")";
    return sout.str();
}

string point__str__(const point& p)
{
    std::ostringstream sout;
    sout << "(" << p.x() << ", " << p.y() << ")";
    return sout.str();
}

long point_x(const point& p) { return p.x(); }
long point_y(const point& p) { return p.y(); }

// ----------------------------------------------------------------------------------------
145
void bind_vector(py::module& m)
146
{
Patrick Snape's avatar
Patrick Snape committed
147
    {
148
149
    py::class_<cv, std::shared_ptr<cv>>(m, "vector", "This object represents the mathematical idea of a column vector.")
        .def(py::init())
150
        .def("set_size", &cv_set_size)
151
        .def("resize", &cv_set_size)
152
        .def(py::init(&cv_from_object))
153
        .def("__repr__", &cv__repr__)
154
155
156
        .def("__str__", &cv__str__)
        .def("__len__", &cv__len__)
        .def("__getitem__", &cv__getitem__)
157
        .def("__getitem__", &cv__getitem2__)
158
        .def("__setitem__", &cv__setitem__)
159
160
        .def_property_readonly("shape", &cv_get_matrix_size)
        .def(py::pickle(&getstate<cv>, &setstate<cv>));
Patrick Snape's avatar
Patrick Snape committed
161

162
    m.def("dot", &dotprod, "Compute the dot product between two dense column vectors.");
Patrick Snape's avatar
Patrick Snape committed
163
164
165
    }
    {
    typedef point type;
166
167
    py::class_<type>(m, "point", "This object represents a single point of integer coordinates that maps directly to a dlib::point.")
            .def(py::init<long,long>(), py::arg("x"), py::arg("y"))
Patrick Snape's avatar
Patrick Snape committed
168
169
            .def("__repr__", &point__repr__)
            .def("__str__", &point__str__)
170
171
172
            .def_property_readonly("x", &point_x, "The x-coordinate of the point.")
            .def_property_readonly("y", &point_y, "The y-coordinate of the point.")
            .def(py::pickle(&getstate<type>, &setstate<type>));
Patrick Snape's avatar
Patrick Snape committed
173
    }
Patrick Snape's avatar
Patrick Snape committed
174
175
    {
    typedef std::vector<point> type;
176
    py::bind_vector<type>(m, "points", "An array of point objects.")
Patrick Snape's avatar
Patrick Snape committed
177
178
        .def("clear", &type::clear)
        .def("resize", resize<type>)
179
180
        .def("extend", extend_vector_with_python_list<point>)
        .def(py::pickle(&getstate<type>, &setstate<type>));
Patrick Snape's avatar
Patrick Snape committed
181
    }
182
}