vector.cpp 4.79 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
6
#include <boost/shared_ptr.hpp>
#include <dlib/matrix.h>
7
#include <boost/python/slice.hpp>
Patrick Snape's avatar
Patrick Snape committed
8
#include <dlib/geometry/vector.h>
9
#include "indexing.h"
10
11
12
13
14
15
16
17
18
19
20
21
22
23


using namespace dlib;
using namespace std;
using namespace boost::python;

typedef matrix<double,0,1> cv;

void cv_set_size(cv& m, long s)
{
    m.set_size(s);
    m = 0;
}

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

29
string cv__str__(const cv& v)
30
31
{
    ostringstream sout;
32
33
34
35
36
37
38
39
40
41
42
43
44
    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([";
45
    for (long i = 0; i < v.size(); ++i)
46
47
48
49
50
51
    {
        sout << v(i);
        if (i+1 < v.size())
            sout << ", ";
    }
    sout << "])";
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    return sout.str();
}

boost::shared_ptr<cv> cv_from_object(object obj)
{
    extract<long> thesize(obj);
    if (thesize.check())
    {
        long nr = thesize;
        boost::shared_ptr<cv> temp(new cv(nr));
        *temp = 0;
        return temp;
    }
    else
    {
        const long nr = len(obj);
        boost::shared_ptr<cv> temp(new cv(nr));
        for ( long r = 0; r < nr; ++r)
        {
            (*temp)(r) = extract<double>(obj[r]);
        }
        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
89
90
91
        PyErr_SetString( PyExc_IndexError, "index out of range"
        );
        boost::python::throw_error_already_set();
92
93
94
95
96
97
98
99
100
101
    }
    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
102
103
104
        PyErr_SetString( PyExc_IndexError, "index out of range"
        );
        boost::python::throw_error_already_set();
105
106
107
108
109
    }
    return m(r);
}


110
111
112
cv cv__getitem2__(cv& m, slice r)
{
    slice::range<cv::iterator> bounds;
113
    bounds = r.get_indicies<>(m.begin(), m.end());
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    long num = (bounds.stop-bounds.start+1);
    // round num up to the next multiple of bounds.step.
    if ((num%bounds.step) != 0)
        num += bounds.step - num%bounds.step;

    cv temp(num/bounds.step);

    if (temp.size() == 0)
        return temp;
    long ii = 0;
    while(bounds.start != bounds.stop)
    {
        temp(ii++) = *bounds.start;
        std::advance(bounds.start, bounds.step);
    }
    temp(ii) = *bounds.start;
    return temp;
}

133
boost::python::tuple cv_get_matrix_size(cv& m)
134
{
135
    return boost::python::make_tuple(m.nr(), m.nc());
136
137
}

Patrick Snape's avatar
Patrick Snape committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// ----------------------------------------------------------------------------------------

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

// ----------------------------------------------------------------------------------------
158
159
void bind_vector()
{
Patrick Snape's avatar
Patrick Snape committed
160
161
    using boost::python::arg;
    {
162
    class_<cv>("vector", "This object represents the mathematical idea of a column vector.", init<>())
163
        .def("set_size", &cv_set_size)
164
        .def("resize", &cv_set_size)
165
        .def("__init__", make_constructor(&cv_from_object))
166
        .def("__repr__", &cv__repr__)
167
168
169
        .def("__str__", &cv__str__)
        .def("__len__", &cv__len__)
        .def("__getitem__", &cv__getitem__)
170
        .def("__getitem__", &cv__getitem2__)
171
        .def("__setitem__", &cv__setitem__)
172
173
        .add_property("shape", &cv_get_matrix_size)
        .def_pickle(serialize_pickle<cv>());
Patrick Snape's avatar
Patrick Snape committed
174
175

    def("dot", dotprod, "Compute the dot product between two dense column vectors.");
Patrick Snape's avatar
Patrick Snape committed
176
177
178
179
180
181
182
    }
    {
    typedef point type;
    class_<type>("point", "This object represents a single point of integer coordinates that maps directly to a dlib::point.")
            .def(init<long,long>((arg("x"), arg("y"))))
            .def("__repr__", &point__repr__)
            .def("__str__", &point__str__)
Patrick Snape's avatar
Patrick Snape committed
183
184
            .add_property("x", &point_x, "The x-coordinate of the point.")
            .add_property("y", &point_y, "The y-coordinate of the point.")
Patrick Snape's avatar
Patrick Snape committed
185
186
            .def_pickle(serialize_pickle<type>());
    }
Patrick Snape's avatar
Patrick Snape committed
187
188
189
190
191
192
193
194
    {
    typedef std::vector<point> type;
    class_<type>("points", "An array of point objects.")
        .def(vector_indexing_suite<type>())
        .def("clear", &type::clear)
        .def("resize", resize<type>)
        .def_pickle(serialize_pickle<type>());
    }
195
}