vector.cpp 6.34 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

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

using namespace dlib;
using namespace std;

typedef matrix<double,0,1> cv;

16

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
// ----------------------------------------------------------------------------------------

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();
}

Davis King's avatar
Davis King committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
string dpoint__repr__ (const dpoint& p)
{
    std::ostringstream sout;
    sout << "dpoint(" << p.x() << ", " << p.y() << ")";
    return sout.str();
}

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

Patrick Snape's avatar
Patrick Snape committed
155
156
long point_x(const point& p) { return p.x(); }
long point_y(const point& p) { return p.y(); }
Davis King's avatar
Davis King committed
157
158
double dpoint_x(const dpoint& p) { return p.x(); }
double dpoint_y(const dpoint& p) { return p.y(); }
Patrick Snape's avatar
Patrick Snape committed
159
160

// ----------------------------------------------------------------------------------------
161
void bind_vector(py::module& m)
162
{
Patrick Snape's avatar
Patrick Snape committed
163
    {
164
165
    py::class_<cv, std::shared_ptr<cv>>(m, "vector", "This object represents the mathematical idea of a column vector.")
        .def(py::init())
166
        .def("set_size", &cv_set_size)
167
        .def("resize", &cv_set_size)
168
        .def(py::init(&cv_from_object))
169
        .def("__repr__", &cv__repr__)
170
171
172
        .def("__str__", &cv__str__)
        .def("__len__", &cv__len__)
        .def("__getitem__", &cv__getitem__)
173
        .def("__getitem__", &cv__getitem2__)
174
        .def("__setitem__", &cv__setitem__)
175
176
        .def_property_readonly("shape", &cv_get_matrix_size)
        .def(py::pickle(&getstate<cv>, &setstate<cv>));
Patrick Snape's avatar
Patrick Snape committed
177

178
    m.def("dot", &dotprod, "Compute the dot product between two dense column vectors.");
Patrick Snape's avatar
Patrick Snape committed
179
180
181
    }
    {
    typedef point type;
182
183
    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"))
184
            .def(py::init<dpoint>(), py::arg("p"))
Patrick Snape's avatar
Patrick Snape committed
185
186
            .def("__repr__", &point__repr__)
            .def("__str__", &point__str__)
187
188
            .def_property("x", &point_x, [](point& p, long x){p.x()=x;}, "The x-coordinate of the point.")
            .def_property("y", &point_y, [](point& p, long y){p.x()=y;}, "The y-coordinate of the point.")
189
            .def(py::pickle(&getstate<type>, &setstate<type>));
Patrick Snape's avatar
Patrick Snape committed
190
    }
Patrick Snape's avatar
Patrick Snape committed
191
192
    {
    typedef std::vector<point> type;
193
    py::bind_vector<type>(m, "points", "An array of point objects.")
Patrick Snape's avatar
Patrick Snape committed
194
195
        .def("clear", &type::clear)
        .def("resize", resize<type>)
196
197
        .def("extend", extend_vector_with_python_list<point>)
        .def(py::pickle(&getstate<type>, &setstate<type>));
Patrick Snape's avatar
Patrick Snape committed
198
    }
Davis King's avatar
Davis King committed
199
200
201
202
203

    {
    typedef dpoint type;
    py::class_<type>(m, "dpoint", "This object represents a single point of floating point coordinates that maps directly to a dlib::dpoint.")
            .def(py::init<double,double>(), py::arg("x"), py::arg("y"))
204
            .def(py::init<point>(), py::arg("p"))
Davis King's avatar
Davis King committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
            .def("__repr__", &dpoint__repr__)
            .def("__str__", &dpoint__str__)
            .def_property("x", &dpoint_x, [](dpoint& p, double x){p.x()=x;}, "The x-coordinate of the dpoint.")
            .def_property("y", &dpoint_y, [](dpoint& p, double y){p.x()=y;}, "The y-coordinate of the dpoint.")
            .def(py::pickle(&getstate<type>, &setstate<type>));
    }
    {
    typedef std::vector<dpoint> type;
    py::bind_vector<type>(m, "dpoints", "An array of dpoint objects.")
        .def("clear", &type::clear)
        .def("resize", resize<type>)
        .def("extend", extend_vector_with_python_list<dpoint>)
        .def(py::pickle(&getstate<type>, &setstate<type>));
    }
219
220
221
222
223

    m.def("length", [](const point& p){return length(p); }, 
        "returns the distance from p to the origin, i.e. the L2 norm of p.", py::arg("p"));
    m.def("length", [](const dpoint& p){return length(p); }, 
        "returns the distance from p to the origin, i.e. the L2 norm of p.", py::arg("p"));
224
}