rectangles.cpp 5.93 KB
Newer Older
1
2
3
4
5
// Copyright (C) 2015 Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.

#include <dlib/python.h>
#include <dlib/geometry.h>
6
#include <pybind11/stl_bind.h>
7
#include "indexing.h"
Davis King's avatar
Davis King committed
8
#include "opaque_types.h"
9
10
11

using namespace dlib;
using namespace std;
12
13
14

namespace py = pybind11;

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
66
67
68
69
70

// ----------------------------------------------------------------------------------------

long left(const rectangle& r) { return r.left(); }
long top(const rectangle& r) { return r.top(); }
long right(const rectangle& r) { return r.right(); }
long bottom(const rectangle& r) { return r.bottom(); }
long width(const rectangle& r) { return r.width(); }
long height(const rectangle& r) { return r.height(); }
unsigned long area(const rectangle& r) { return r.area(); }

double dleft(const drectangle& r) { return r.left(); }
double dtop(const drectangle& r) { return r.top(); }
double dright(const drectangle& r) { return r.right(); }
double dbottom(const drectangle& r) { return r.bottom(); }
double dwidth(const drectangle& r) { return r.width(); }
double dheight(const drectangle& r) { return r.height(); }
double darea(const drectangle& r) { return r.area(); }

template <typename rect_type>
bool is_empty(const rect_type& r) { return r.is_empty(); }

template <typename rect_type>
point center(const rect_type& r) { return center(r); }

template <typename rect_type>
point dcenter(const rect_type& r) { return dcenter(r); }

template <typename rect_type>
bool contains(const rect_type& r, const point& p) { return r.contains(p); }

template <typename rect_type>
bool contains_xy(const rect_type& r, const long x, const long y) { return r.contains(point(x, y)); }

template <typename rect_type>
bool contains_rec(const rect_type& r, const rect_type& r2) { return r.contains(r2); }

template <typename rect_type>
rect_type intersect(const rect_type& r, const rect_type& r2) { return r.intersect(r2); }

template <typename rect_type>
string print_rectangle_str(const rect_type& r)
{
    std::ostringstream sout;
    sout << r;
    return sout.str();
}

template <typename rect_type>
string print_rectangle_repr(const rect_type& r)
{
    std::ostringstream sout;
    sout << "rectangle(" << r.left() << "," << r.top() << "," << r.right() << "," << r.bottom() << ")";
    return sout.str();
}

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
rectangle add_point_to_rect(const rectangle& r, const point& p)
{
    return r + p;
}

rectangle add_rect_to_rect(const rectangle& r, const rectangle& p)
{
    return r + p;
}

rectangle& iadd_point_to_rect(rectangle& r, const point& p)
{
    r += p;
    return r;
}

rectangle& iadd_rect_to_rect(rectangle& r, const rectangle& p)
{
    r += p;
    return r;
}



95
96
// ----------------------------------------------------------------------------------------

97
void bind_rectangles(py::module& m)
98
99
100
{
    {
    typedef rectangle type;
101
102
    py::class_<type>(m, "rectangle", "This object represents a rectangular area of an image.")
        .def(py::init<long,long,long,long>(), py::arg("left"),py::arg("top"),py::arg("right"),py::arg("bottom"))
103
104
105
106
107
108
109
110
111
112
        .def("area",   &::area)
        .def("left",   &::left)
        .def("top",    &::top)
        .def("right",  &::right)
        .def("bottom", &::bottom)
        .def("width",  &::width)
        .def("height", &::height)
        .def("is_empty", &::is_empty<type>)
        .def("center", &::center<type>)
        .def("dcenter", &::dcenter<type>)
113
114
115
116
        .def("contains", &::contains<type>, py::arg("point"))
        .def("contains", &::contains_xy<type>, py::arg("x"), py::arg("y"))
        .def("contains", &::contains_rec<type>, py::arg("rectangle"))
        .def("intersect", &::intersect<type>, py::arg("rectangle"))
117
118
        .def("__str__", &::print_rectangle_str<type>)
        .def("__repr__", &::print_rectangle_repr<type>)
119
120
121
122
        .def("__add__", &::add_point_to_rect)
        .def("__add__", &::add_rect_to_rect)
        .def("__iadd__", &::iadd_point_to_rect)
        .def("__iadd__", &::iadd_rect_to_rect)
123
124
125
        .def(py::self == py::self)
        .def(py::self != py::self)
        .def(py::pickle(&getstate<type>, &setstate<type>));
126
127
128
    }
    {
    typedef drectangle type;
129
130
    py::class_<type>(m, "drectangle", "This object represents a rectangular area of an image with floating point coordinates.")
        .def(py::init<double,double,double,double>(), py::arg("left"), py::arg("top"), py::arg("right"), py::arg("bottom"))
131
132
133
134
135
136
137
138
139
140
        .def("area",   &::darea)
        .def("left",   &::dleft)
        .def("top",    &::dtop)
        .def("right",  &::dright)
        .def("bottom", &::dbottom)
        .def("width",  &::dwidth)
        .def("height", &::dheight)
        .def("is_empty", &::is_empty<type>)
        .def("center", &::center<type>)
        .def("dcenter", &::dcenter<type>)
141
142
143
144
        .def("contains", &::contains<type>, py::arg("point"))
        .def("contains", &::contains_xy<type>, py::arg("x"), py::arg("y"))
        .def("contains", &::contains_rec<type>, py::arg("rectangle"))
        .def("intersect", &::intersect<type>, py::arg("rectangle"))
145
146
        .def("__str__", &::print_rectangle_str<type>)
        .def("__repr__", &::print_rectangle_repr<type>)
147
148
149
        .def(py::self == py::self)
        .def(py::self != py::self)
        .def(py::pickle(&getstate<type>, &setstate<type>));
150
    }
Davis King's avatar
Davis King committed
151

152
153
    {
    typedef std::vector<rectangle> type;
154
    py::bind_vector<type>(m, "rectangles", "An array of rectangle objects.")
155
156
        .def("clear", &type::clear)
        .def("resize", resize<type>)
157
158
        .def("extend", extend_vector_with_python_list<rectangle>)
        .def(py::pickle(&getstate<type>, &setstate<type>));
159
    }
Davis King's avatar
Davis King committed
160
161
162
163
164
165
166
167
168

    {
    typedef std::vector<std::vector<rectangle>> type;
    py::bind_vector<type>(m, "rectangless", "An array of arrays of rectangle objects.")
        .def("clear", &type::clear)
        .def("resize", resize<type>)
        .def("extend", extend_vector_with_python_list<rectangle>)
        .def(py::pickle(&getstate<type>, &setstate<type>));
    }
169
170
171
}

// ----------------------------------------------------------------------------------------