graph_cairo_draw.cc 75 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2017 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
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
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "graph.hh"

#ifdef HAVE_CAIROMM

#include "graph_filtering.hh"

#include <boost/python.hpp>
#include <boost/utility/enable_if.hpp>

#include "graph_selectors.hh"
#include "graph_properties.hh"

#include <iostream>
31
32
#include <array>
#include <chrono>
Tiago Peixoto's avatar
Tiago Peixoto committed
33

34
#include "hash_map_wrap.hh"
35
#include "demangle.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
36

37
38
39
#include "coroutine.hh"
#include "graph_python_interface.hh"

Tiago Peixoto's avatar
Tiago Peixoto committed
40
41
42
#include <cairommconfig.h>
#include <cairomm/context.h>
#include <cairomm/surface.h>
43
#include PYCAIRO_HEADER
Tiago Peixoto's avatar
Tiago Peixoto committed
44
45
46
47
48
49
50
51
52
53
54
55

#include <boost/mpl/map/map50.hpp>

using namespace std;
using namespace boost;
using namespace graph_tool;

enum vertex_attr_t {
    VERTEX_SHAPE = 100,
    VERTEX_COLOR,
    VERTEX_FILL_COLOR,
    VERTEX_SIZE,
56
    VERTEX_ASPECT,
57
    VERTEX_ROTATION,
58
    VERTEX_ANCHOR,
Tiago Peixoto's avatar
Tiago Peixoto committed
59
60
61
    VERTEX_PENWIDTH,
    VERTEX_HALO,
    VERTEX_HALO_COLOR,
62
    VERTEX_HALO_SIZE,
Tiago Peixoto's avatar
Tiago Peixoto committed
63
64
65
    VERTEX_TEXT,
    VERTEX_TEXT_COLOR,
    VERTEX_TEXT_POSITION,
66
67
    VERTEX_TEXT_ROTATION,
    VERTEX_TEXT_OFFSET,
Tiago Peixoto's avatar
Tiago Peixoto committed
68
69
70
    VERTEX_FONT_FAMILY,
    VERTEX_FONT_SLANT,
    VERTEX_FONT_WEIGHT,
71
    VERTEX_FONT_SIZE,
72
73
    VERTEX_SURFACE,
    VERTEX_PIE_FRACTIONS,
74
    VERTEX_PIE_COLORS,
Tiago Peixoto's avatar
Tiago Peixoto committed
75
76
77
78
79
80
81
82
83
};

enum edge_attr_t {
    EDGE_COLOR = 200,
    EDGE_PENWIDTH,
    EDGE_START_MARKER,
    EDGE_MID_MARKER,
    EDGE_END_MARKER,
    EDGE_MARKER_SIZE,
84
    EDGE_MID_MARKER_POSITION,
85
    EDGE_CONTROL_POINTS,
86
    EDGE_DASH_STYLE,
Tiago Peixoto's avatar
Tiago Peixoto committed
87
    EDGE_GRADIENT,
88
89
90
91
92
93
94
95
    EDGE_TEXT,
    EDGE_TEXT_COLOR,
    EDGE_TEXT_DISTANCE,
    EDGE_TEXT_PARALLEL,
    EDGE_FONT_FAMILY,
    EDGE_FONT_SLANT,
    EDGE_FONT_WEIGHT,
    EDGE_FONT_SIZE,
96
97
    EDGE_SLOPPY,
    EDGE_SEAMLESS
Tiago Peixoto's avatar
Tiago Peixoto committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
};

enum vertex_shape_t {
    SHAPE_CIRCLE = 300,
    SHAPE_TRIANGLE,
    SHAPE_SQUARE,
    SHAPE_PENTAGON,
    SHAPE_HEXAGON,
    SHAPE_HEPTAGON,
    SHAPE_OCTAGON,
    SHAPE_DOUBLE_CIRCLE,
    SHAPE_DOUBLE_TRIANGLE,
    SHAPE_DOUBLE_SQUARE,
    SHAPE_DOUBLE_PENTAGON,
    SHAPE_DOUBLE_HEXAGON,
    SHAPE_DOUBLE_HEPTAGON,
114
    SHAPE_DOUBLE_OCTAGON,
115
116
    SHAPE_PIE,
    SHAPE_NONE
Tiago Peixoto's avatar
Tiago Peixoto committed
117
118
119
120
121
122
123
124
125
126
127
128
};

enum edge_marker_t {
    MARKER_SHAPE_NONE = 400,
    MARKER_SHAPE_ARROW,
    MARKER_SHAPE_CIRCLE,
    MARKER_SHAPE_SQUARE,
    MARKER_SHAPE_DIAMOND,
    MARKER_SHAPE_BAR
};

typedef pair<double, double> pos_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
129
typedef std::tuple<double, double, double, double> color_t;
130
typedef gt_hash_map<int, boost::any> attrs_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
131

132
typedef boost::mpl::map43<
Tiago Peixoto's avatar
Tiago Peixoto committed
133
134
135
136
137
    boost::mpl::pair<boost::mpl::int_<VERTEX_SHAPE>, vertex_shape_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FILL_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_ASPECT>, double>,
138
    boost::mpl::pair<boost::mpl::int_<VERTEX_ROTATION>, double>,
Tiago Peixoto's avatar
Tiago Peixoto committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    boost::mpl::pair<boost::mpl::int_<VERTEX_ANCHOR>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_PENWIDTH>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_HALO>, uint8_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_HALO_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_HALO_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT>, string>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_POSITION>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_ROTATION>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_TEXT_OFFSET>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_FAMILY>, string>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_SLANT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_WEIGHT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_FONT_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_SURFACE>, boost::python::object>,
    boost::mpl::pair<boost::mpl::int_<VERTEX_PIE_FRACTIONS>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<VERTEX_PIE_COLORS>, vector<color_t> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_PENWIDTH>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_START_MARKER>, edge_marker_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_MID_MARKER>, edge_marker_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_END_MARKER>, edge_marker_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_MARKER_SIZE>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_MID_MARKER_POSITION>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_CONTROL_POINTS>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_DASH_STYLE>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_GRADIENT>, vector<double> >,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT>, string>,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_COLOR>, color_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_DISTANCE>, double>,
    boost::mpl::pair<boost::mpl::int_<EDGE_TEXT_PARALLEL>, uint8_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_FAMILY>, string>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_SLANT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_WEIGHT>, int32_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_FONT_SIZE>, double>,
174
175
    boost::mpl::pair<boost::mpl::int_<EDGE_SLOPPY>, uint8_t>,
    boost::mpl::pair<boost::mpl::int_<EDGE_SEAMLESS>, uint8_t> >
Tiago Peixoto's avatar
Tiago Peixoto committed
176
177
        attr_types;

178
namespace std
Tiago Peixoto's avatar
Tiago Peixoto committed
179
180
181
{
ostream& operator<<(ostream& out, const color_t& c)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
182
    out << std::get<0>(c) << " " << std::get<1>(c) << " " << std::get<2>(c) << " " << std::get<3>(c);
Tiago Peixoto's avatar
Tiago Peixoto committed
183
184
185
    return out;
}

186
istream& operator>>(istream& in, color_t& c)
Tiago Peixoto's avatar
Tiago Peixoto committed
187
{
Tiago Peixoto's avatar
Tiago Peixoto committed
188
    in >> std::get<0>(c) >> std::get<1>(c) >> std::get<2>(c) >> std::get<3>(c);
Tiago Peixoto's avatar
Tiago Peixoto committed
189
190
191
192
    return in;
}
}

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
istream& operator>>(istream& in, vertex_shape_t& c)
{
    int tmp;
    in >> tmp;
    c = vertex_shape_t(tmp);
    return in;
}

istream& operator>>(istream& in, edge_marker_t& c)
{
    int tmp;
    in >> tmp;
    c = edge_marker_t(tmp);
    return in;
}

209
210
211
212
213
214
215
216
217
namespace boost
{
template <>
string lexical_cast<string,python::object>(const python::object& val)
{
    return python::extract<string>(val);
}
}

Tiago Peixoto's avatar
Tiago Peixoto committed
218
219
220
221
222
223
224
225
template <class T1, class T2>
struct specific_convert;

template <class Type1, class Type2>
struct Converter
{
    Type1 operator()(const Type2& v) const
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
226
        return do_convert(v, std::is_convertible<Type2,Type1>());
Tiago Peixoto's avatar
Tiago Peixoto committed
227
228
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
229
    Type1 do_convert(const Type2& v, std::true_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
230
231
232
233
    {
        return Type1(v);
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
234
    Type1 do_convert(const Type2& v, std::false_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
235
    {
236
237
238
239
240
241
        try
        {
            return specific_convert<Type1,Type2>()(v);
        }
        catch (bad_lexical_cast&)
        {
242
243
            string name1 = name_demangle(typeid(Type1).name());
            string name2 = name_demangle(typeid(Type2).name());
244
245
246
247
248
249
250
251
252
253
254
255
            string val_name;
            try
            {
                val_name = lexical_cast<string>(v);
            }
            catch (bad_lexical_cast)
            {
                val_name = "<no lexical cast available>";
            }
            throw GraphException("error converting from type '" + name2 +
                                 "' to type '" + name1 + "', val: " + val_name);
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
256
257
258
259
260
261
262
    }

    template <class T1, class T2, class Enable = void>
    struct specific_convert
    {
        T1 operator()(const T2& v) const
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
263
            return dispatch(v, typename std::is_convertible<T2, T1>::type());
Tiago Peixoto's avatar
Tiago Peixoto committed
264
265
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
266
        T1 dispatch(const T2& v, std::true_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
267
268
269
270
        {
            return T1(v);
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
271
        T1 dispatch(const T2& v, std::false_type) const
Tiago Peixoto's avatar
Tiago Peixoto committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
        {
            return lexical_cast<T1>(v);
        }
    };

    template <class T1> // noop
    struct specific_convert<T1, T1>
    {
        T1 operator()(const T1& v) const
        {
            return v;
        }
    };

    // specific specializations
    // string
    template <class T1>
    struct specific_convert<T1, string>
    {
        T1 operator()(const string& v) const
        {
            //uint8_t is not char, it is bool!
Tiago Peixoto's avatar
Tiago Peixoto committed
294
            if (std::is_same<T1, uint8_t>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
295
296
297
298
299
300
                return convert<T1,int>()(lexical_cast<int>(v));
            else
                return lexical_cast<T1>(v);
        }
    };

Tiago Peixoto's avatar
Tiago Peixoto committed
301
    // boost::python::object
Tiago Peixoto's avatar
Tiago Peixoto committed
302
    template <class T1>
Tiago Peixoto's avatar
Tiago Peixoto committed
303
    struct specific_convert<T1,boost::python::object>
Tiago Peixoto's avatar
Tiago Peixoto committed
304
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
305
        T1 operator()(const boost::python::object& v) const
Tiago Peixoto's avatar
Tiago Peixoto committed
306
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
307
            boost::python::extract<T1> x(v);
Tiago Peixoto's avatar
Tiago Peixoto committed
308
309
310
311
312
313
314
315
316
            if (x.check())
                return x();
            else
                throw bad_lexical_cast();
        }
    };

    template <class T2>
    struct specific_convert<string, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
317
318
319
                            typename std::enable_if
                            <boost::mpl::not_<
                                 std::is_same<T2,boost::python::object> >::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
320
321
322
323
    {
        string operator()(const T2& v) const
        {
            //uint8_t is not char, it is bool!
Tiago Peixoto's avatar
Tiago Peixoto committed
324
            if (std::is_same<T2, uint8_t>::value)
Tiago Peixoto's avatar
Tiago Peixoto committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
                return convert<string, int>()(lexical_cast<int>(v));
            else
                return lexical_cast<string>(v);
        }
    };

    // vectors
    template <class T1, class T2>
    struct specific_convert<vector<T1>, vector<T2> >
    {
        vector<T1> operator()(const vector<T2>& v) const
        {
            vector<T1> v2(v.size());
            convert<T1,T2> c;
            for (size_t i = 0; i < v.size(); ++i)
                v2[i] = c(v[i]);
            return v2;
        }
    };

345
    // color_t
Tiago Peixoto's avatar
Tiago Peixoto committed
346
347
348
349
350
351
    template <class T2>
    struct specific_convert<color_t, vector<T2> >
    {
        specific_convert<double, T2> c;
        color_t operator()(const vector<T2>& cv) const
        {
352
353
            if (cv.size() < 3)
                return std::make_tuple(0., 0., 0., 0.);
Tiago Peixoto's avatar
Tiago Peixoto committed
354
            if (cv.size() < 4)
355
                return std::make_tuple(c(cv[0]), c(cv[1]), c(cv[2]), 1.);
Tiago Peixoto's avatar
Tiago Peixoto committed
356
            return std::make_tuple(c(cv[0]), c(cv[1]), c(cv[2]), c(cv[3]));
Tiago Peixoto's avatar
Tiago Peixoto committed
357
358
359
        }
    };

360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
    // vector<color_t>
    template <class T2>
    struct specific_convert<vector<color_t>, vector<T2> >
    {
        specific_convert<double, T2> c;
        vector<color_t> operator()(const vector<T2>& cv) const
        {
            if (cv.size() < 4)
                throw bad_lexical_cast();
            vector<color_t> color;
            for (size_t i = 0; i < cv.size() / 4; ++i)
            {
                if (4 * i + 3 >= cv.size())
                    throw bad_lexical_cast();
                color.push_back(std::make_tuple(c(cv[4*i]), c(cv[4*i + 1]),
                                                c(cv[4*i + 2]), c(cv[4*i + 3])));
            }
            return color;
        }
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
380
381
382
383

    // vertex_shape_t
    template <class T2>
    struct specific_convert<vertex_shape_t, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
384
385
386
387
388
                            typename std::enable_if
                            <boost::mpl::and_<typename boost::mpl::not_<
                                                  std::is_same<T2,string> >::type,
                                              typename boost::mpl::not_<
                                                  std::is_same<T2,boost::python::object> >::type>::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
389
390
391
392
393
394
395
396
397
398
399
    {
        specific_convert<int, T2> c;
        vertex_shape_t operator()(const T2& v) const
        {
            return static_cast<vertex_shape_t>(c(v));
        }
    };

    // edge_marker_t
    template <class T2>
    struct specific_convert<edge_marker_t, T2,
Tiago Peixoto's avatar
Tiago Peixoto committed
400
401
402
403
404
                            typename std::enable_if
                            <boost::mpl::and_<typename boost::mpl::not_<
                                                  std::is_same<T2,string> >::type,
                                              typename boost::mpl::not_<
                                                  std::is_same<T2,boost::python::object> >::type>::type::value>::type>
Tiago Peixoto's avatar
Tiago Peixoto committed
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    {
        specific_convert<int, T2> c;
        edge_marker_t operator()(const T2& v) const
        {
            return static_cast<edge_marker_t>(c(v));
        }
    };
};


template <class Descriptor>
class AttrDict
{
public:
    AttrDict(Descriptor descriptor, attrs_t& attrs, attrs_t& defaults)
        : _descriptor(descriptor), _attrs(attrs), _defaults(defaults) {}

    template <class Value>
    Value get(int k)
    {
425
        auto iter = _attrs.find(k);
Tiago Peixoto's avatar
Tiago Peixoto committed
426
427
428
429
430
431
432
433
434
435
436
437
438
        if (iter != _attrs.end())
        {
            typedef DynamicPropertyMapWrap<Value, Descriptor, Converter> pmap_t;
            pmap_t pmap(any_cast<pmap_t>(iter->second));
            return pmap.get(_descriptor);
        }
        try
        {
            return any_cast<Value>(_defaults[k]);
        }
        catch (bad_any_cast&)
        {
            throw ValueException("Error getting attribute " + lexical_cast<string>(k) +
439
440
                                 ", wanted: " + name_demangle(typeid(Value).name()) +
                                 ", got: " + name_demangle(_defaults[k].type().name()));
Tiago Peixoto's avatar
Tiago Peixoto committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
        }
    }

private:
    Descriptor _descriptor;
    attrs_t& _attrs;
    attrs_t& _defaults;
};

void draw_polygon(size_t N, double radius, Cairo::Context& cr)
{
    cr.save();
    cr.rotate(M_PI * (1. / 2 - 1. / N));
    cr.move_to(radius, 0);
    for (size_t i = 0; i < N; ++i)
    {
        double angle = (2 * M_PI * (i + 1)) / N;
        cr.line_to(radius * cos(angle), radius * sin(angle));
    }
    cr.close_path();
    cr.restore();
}

double get_polygon_anchor(size_t N, double radius, double angle)
{
    double theta = angle - M_PI * (1. / 2 - 1. / N);
    if (N % 2 == 0)
        theta += M_PI / N;
    if (theta > 2 * M_PI)
        theta -= 2 * M_PI;
    if (theta < 2 * M_PI)
        theta += 2 * M_PI;
    theta = fmod(theta, 2. * M_PI / N);
    if (theta > M_PI / N)
        theta -= 2. * M_PI / N;
    return radius * cos(M_PI / N) / cos(theta);
}

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509

void draw_pie(double radius, const vector<double>& f,
              const vector<color_t>& colors, Cairo::Context& cr)
{
    if (colors.empty())
        throw ValueException("No pie colors!");
    double s = 0;
    for (size_t i = 0; i < f.size(); ++i)
        s += f[i];
    double last = 0;
    double pos = 0;
    cr.save();
    cr.begin_new_path();
    for (size_t i = 0; i < f.size(); ++i)
    {
        pos += f[i];
        double angle = (2 * pos * M_PI) / s;
        cr.move_to(0, 0);
        cr.arc(0, 0, radius, last, angle);
        last = angle;
        size_t j = i % colors.size();
        cr.set_source_rgba(get<0>(colors[j]),
                           get<1>(colors[j]),
                           get<2>(colors[j]),
                           get<3>(colors[j]));
        cr.fill();
    }
    cr.restore();
}


Tiago Peixoto's avatar
Tiago Peixoto committed
510
511
512
513
514
515
516
517
518
519
void move_radially(pos_t& pos, const pos_t& origin, double dr)
{
    double angle = atan2(pos.second - origin.second,
                         pos.first - origin.first);
    if (angle < 0)
        angle += 2 * M_PI;
    pos.first += dr * cos(angle);
    pos.second += dr * sin(angle);
}

520
double get_user_dist(Cairo::Context& cr, double norm = 1.)
Tiago Peixoto's avatar
Tiago Peixoto committed
521
{
522
    double x = norm / sqrt(2.), y = norm / sqrt(2.);
Tiago Peixoto's avatar
Tiago Peixoto committed
523
524
525
526
    cr.device_to_user_distance(x, y);
    return sqrt(x * x + y * y);
}

527
528
529
530
531
532
533
534
535
536
537
538
void get_surface_size(Cairo::RefPtr<Cairo::Surface> sfc,
                      double& width, double& height)
{
    Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(sfc);
    double x1, x2, y1, y2;

    cr->get_clip_extents(x1, y1, x2, y2);

    width = x2 - x1;
    height = y2 - y1;
}

539
540
541
542
double dist(const pos_t& p1, const pos_t& p2)
{
    return sqrt(pow(p1.first - p2.first, 2) + pow(p1.second - p2.second, 2));
};
Tiago Peixoto's avatar
Tiago Peixoto committed
543

544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
double get_spline_len(const vector<double>& cts)
{
    double len = 0;
    for (size_t i = 0; i + 7 < cts.size(); i += 6)
    {
        double dx = cts[i + 6] - cts[i];
        double dy = cts[i + 6 + 1] - cts[i + 1];
        len += sqrt(dx * dx + dy * dy);
    }
    return len;
}

pos_t get_spline_point(const vector<double>& cts, double d)
{
    pos_t p;
    double pos = 0;
    for (size_t i = 0; i + 7 < cts.size(); i += 6)
    {
        double dx = cts[i + 6] - cts[i];
        double dy = cts[i + 6 + 1] - cts[i + 1];
        double l = sqrt(dx * dx + dy * dy);
        if (l < 1e-8)
            continue;
        if (pos + l >= d || i + 13 >= cts.size())
        {
            double t = 1 - (pos + l - d) / l;

            p.first = pow(1 - t, 3) * cts[i] +
                3 * t * pow(1 - t, 2) * cts[i + 2] +
                3 * t * t * (1 - t) * cts[i + 4] +
                t * t * t * cts[i + 6];

            p.second = pow(1 - t, 3) * cts[i + 1] +
                3 * t * pow(1 - t, 2) * cts[i + 3] +
                3 * t * t * (1 - t) * cts[i + 5] +
                t * t * t * cts[i + 7];
            break;
        }
        pos += l;
    }
    return p;
}

pos_t get_spline_diff(const vector<double>& cts, double d)
{
    pos_t diff;
    double pos = 0;
    for (size_t i = 0; i + 7 < cts.size(); i += 6)
    {
        double dx = cts[i + 6] - cts[i];
        double dy = cts[i + 6 + 1] - cts[i + 1];
        double l = sqrt(dx * dx + dy * dy);
        if (l < 1e-8)
            continue;
        if (pos + l >= d || i + 13 >= cts.size())
        {
            double t = 1 - (pos + l - d) / l;

            diff.first = -3 * pow(1 - t, 2) * cts[i] +
                (3 * pow(1 - t, 2) - 6 * t * (1-t)) * cts[i + 2] +
                (-3 * t * t + 6 * t * (1 - t)) * cts[i + 4] +
                3 * t * t * cts[i + 6];

            diff.second= -3 * pow(1 - t, 2) * cts[i + 1] +
                (3 * pow(1 - t, 2) - 6 * t * (1-t)) * cts[i + 3] +
                (-3 * t * t + 6 * t * (1 - t)) * cts[i + 5] +
                3 * t * t * cts[i + 7];
            break;
        }
        pos += l;
    }
    return diff;
}
Tiago Peixoto's avatar
Tiago Peixoto committed
617
618
619
620
621
622
623
624
625
626
627
628

template <class Descriptor>
class VertexShape
{
public:
    VertexShape(pos_t& pos, AttrDict<Descriptor> attrs)
        : _pos(pos), _attrs(attrs)

    {}

    double get_size(Cairo::Context& cr)
    {
629
        double size = _attrs.template get<double>(VERTEX_SIZE);
630
        size = get_user_dist(cr, size);
Tiago Peixoto's avatar
Tiago Peixoto committed
631

632
        string text = _attrs.template get<string>(VERTEX_TEXT);
Tiago Peixoto's avatar
Tiago Peixoto committed
633
634
        if (text != "")
        {
635
            double text_pos = _attrs.template get<double>(VERTEX_TEXT_POSITION);
636
            if (text_pos == -1)
Tiago Peixoto's avatar
Tiago Peixoto committed
637
            {
638
639
640
                cr.select_font_face(_attrs.template get<string>(VERTEX_FONT_FAMILY),
                                    static_cast<Cairo::FontSlant>(_attrs.template get<int32_t>(VERTEX_FONT_SLANT)),
                                    static_cast<Cairo::FontWeight>(_attrs.template get<int32_t>(VERTEX_FONT_WEIGHT)));
641
                cr.set_font_size(get_user_dist(cr, _attrs.template get<double>(VERTEX_FONT_SIZE)));
Tiago Peixoto's avatar
Tiago Peixoto committed
642
643
644
                Cairo::TextExtents extents;
                cr.get_text_extents(text, extents);
                double s = max(extents.width, extents.height) * 1.4;
645
                vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
646
                if (shape >= SHAPE_DOUBLE_CIRCLE && shape != SHAPE_PIE)
Tiago Peixoto's avatar
Tiago Peixoto committed
647
648
                {
                    s /= 0.7;
649
                    double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
650
                    pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
651
652
653
654
655
656
657
658
                    s += pw;
                }
                size = max(size, s);
            }
        }
        return size;
    }

659
660
    pos_t get_anchor(const pos_t& origin, Cairo::Context& cr,
                     bool force_border=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
661
    {
662
        int anchor_type =_attrs.template get<int32_t>(VERTEX_ANCHOR);
663
        if (anchor_type == 0 && !force_border)
664
665
            return _pos;

Tiago Peixoto's avatar
Tiago Peixoto committed
666
667
        double angle = atan2(_pos.second - origin.second,
                             _pos.first - origin.first);
668
669
        double rot = _attrs.template get<double>(VERTEX_ROTATION);

Tiago Peixoto's avatar
Tiago Peixoto committed
670
671
        if (angle < 0)
            angle += 2 * M_PI;
672

Tiago Peixoto's avatar
Tiago Peixoto committed
673
674
675
        double r = get_size(cr) / 2;
        double dr = r;

676
        double pw = _attrs.template get<double>(VERTEX_PENWIDTH);
677
        pw = get_user_dist(cr, pw);
Tiago Peixoto's avatar
Tiago Peixoto committed
678
679
680
681
        r += pw / 2.5;

        pos_t anchor;
        size_t nsides = 0;
682
        vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
Tiago Peixoto's avatar
Tiago Peixoto committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
        switch (shape)
        {
        case SHAPE_TRIANGLE:
        case SHAPE_SQUARE:
        case SHAPE_PENTAGON:
        case SHAPE_HEXAGON:
        case SHAPE_HEPTAGON:
        case SHAPE_OCTAGON:
        case SHAPE_DOUBLE_TRIANGLE:
        case SHAPE_DOUBLE_SQUARE:
        case SHAPE_DOUBLE_PENTAGON:
        case SHAPE_DOUBLE_HEXAGON:
        case SHAPE_DOUBLE_HEPTAGON:
        case SHAPE_DOUBLE_OCTAGON:
            nsides = shape - SHAPE_TRIANGLE + 3;
            if (nsides > 8)
                nsides -= 7;
700
            dr = get_polygon_anchor(nsides, r, angle - rot);
Tiago Peixoto's avatar
Tiago Peixoto committed
701
702
703
            break;
        case SHAPE_CIRCLE:
        case SHAPE_DOUBLE_CIRCLE:
704
        case SHAPE_PIE:
Tiago Peixoto's avatar
Tiago Peixoto committed
705
706
            dr = r;
            break;
707
708
        case SHAPE_NONE:
            break;
Tiago Peixoto's avatar
Tiago Peixoto committed
709
710
        default:
            throw ValueException("Invalid vertex shape: " +
711
                                 lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
Tiago Peixoto's avatar
Tiago Peixoto committed
712
713
        }

714
715
        double aspect = _attrs.template get<double>(VERTEX_ASPECT);

Tiago Peixoto's avatar
Tiago Peixoto committed
716
        anchor = _pos;
717
        anchor.first -= dr * cos(angle) * aspect;
Tiago Peixoto's avatar
Tiago Peixoto committed
718
        anchor.second -= dr * sin(angle);
719

Tiago Peixoto's avatar
Tiago Peixoto committed
720
721
722
        return anchor;
    }

723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
    std::pair<pos_t, double> get_anchor_spline(const vector<double>& cts,
                                               Cairo::Context& cr,
                                               bool loop = false,
                                               bool src = false)
    {
        double len = get_spline_len(cts);
        double x, one;
        pos_t p0 = get_spline_point(cts, 0);
        pos_t p1 = get_spline_point(cts, len);
        if (dist(p0, _pos) < dist(p1, _pos) || (loop && src))
        {
            x = 1;
            one = 1;
        }
        else
        {
            x = 0;
            one = -1;
        }

        int anchor_type =_attrs.template get<int32_t>(VERTEX_ANCHOR);
        if (anchor_type == 0)
            return make_pair(_pos, x);

        if (loop)
            x = 0.5;

        double dl = 0.5;
        pos_t pos, anchor;
        pos = get_spline_point(cts, len * x);
        anchor = get_anchor(pos, cr);
        if (dist(anchor, _pos) == 0 || dist(pos, _pos) < dist(anchor, _pos))
            return make_pair(_pos, x * len);

        size_t i = 0;
        while (abs(dist(pos, anchor)) > 1e-6)
        {
760
            double nx = min(max(x - dl * one, 0.), 1.);
761
762
            pos = get_spline_point(cts, len * nx);
            anchor = get_anchor(pos, cr);
763
            size_t j = 0;
764
765
766
            while (dist(pos, _pos) < dist(anchor, _pos)) // x is inside
            {
                dl /= 2;
767
                nx = min(max(x - dl * one, 0.), 1.);
768
769
770
                pos = get_spline_point(cts, len * nx);
                anchor = get_anchor(pos, cr);
                j++;
771
                if (nx == 0. || nx == 1. || j > 100)
772
773
                    break;
            }
774

775
            x = nx;
776
            dl = min(dist(pos, anchor) / len, 0.5);
777
778
779
780
781
782
783
            i++;
            if (i > 1000)
                break;
        }
        return make_pair(pos, x * len);
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
784
785
786
787
788
    pos_t get_pos()
    {
        return _pos;
    }

789
    void draw(Cairo::Context& cr, bool outline=false)
Tiago Peixoto's avatar
Tiago Peixoto committed
790
791
    {
        color_t color, fillcolor;
792
        double size, pw;
Tiago Peixoto's avatar
Tiago Peixoto committed
793
        size = get_size(cr);
794
795
796
797
798
799
800

        std::array<double, 4> clip;
        cr.get_clip_extents(clip[0], clip[1], clip[2], clip[3]);
        if ((_pos.first + 2 * size < clip[0] && _pos.second + 2 * size < clip[1]) ||
            (_pos.first - 2 * size > clip[2] && _pos.second - 2 * size > clip[3]))
            return;

801
802
        double aspect = _attrs.template get<double>(VERTEX_ASPECT);
        double rot = _attrs.template get<double>(VERTEX_ROTATION);
803

804
805
        if (!outline)
            cr.save();
Tiago Peixoto's avatar
Tiago Peixoto committed
806
        cr.translate(_pos.first, _pos.second);
807
        cr.rotate(rot);
Tiago Peixoto's avatar
Tiago Peixoto committed
808

809
        if (!outline && _attrs.template get<uint8_t>(VERTEX_HALO))
Tiago Peixoto's avatar
Tiago Peixoto committed
810
        {
811
            color_t c = _attrs.template get<color_t>(VERTEX_HALO_COLOR);
812
            double hs = _attrs.template get<double>(VERTEX_HALO_SIZE);
Tiago Peixoto's avatar
Tiago Peixoto committed
813
            cr.set_source_rgba(get<0>(c), get<1>(c), get<2>(c), get<3>(c));
814
815
816
817
818
            if (aspect != 1.)
            {
                cr.save();
                cr.scale(aspect, 1.0);
            }
819
            cr.arc(0, 0, size * hs / 2, 0, 2 * M_PI);
Tiago Peixoto's avatar
Tiago Peixoto committed
820
            cr.fill();
821
822
            if (aspect != 1.)
                cr.restore();
Tiago Peixoto's avatar
Tiago Peixoto committed
823
824
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
825
        boost::python::object osrc = _attrs.template get<boost::python::object>(VERTEX_SURFACE);
826
827
828
829
830
831

        pw =_attrs.template get<double>(VERTEX_PENWIDTH);
        pw = get_user_dist(cr, pw);
        cr.set_line_width(pw);

        if (!outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
832
        {
833
834
835
836
            color = _attrs.template get<color_t>(VERTEX_COLOR);
            cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                               get<3>(color));
        }
837

838
839
840
841
842
843
844
        size_t nsides = 0;
        vertex_shape_t shape = _attrs.template get<vertex_shape_t>(VERTEX_SHAPE);
        switch (shape)
        {
        case SHAPE_CIRCLE:
        case SHAPE_DOUBLE_CIRCLE:
            if (aspect != 1.)
845
            {
846
847
                cr.save();
                cr.scale(aspect, 1.0);
848
            }
849
850
851
852
853
            cr.arc(0, 0, size / 2., 0, 2 * M_PI);
            cr.close_path();
            if (aspect != 1.)
                cr.restore();
            if (shape == SHAPE_DOUBLE_CIRCLE && !outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
854
            {
855
                cr.stroke();
856
857
858
859
860
                if (aspect != 1.)
                {
                    cr.save();
                    cr.scale(aspect, 1.0);
                }
861
862
863
                cr.arc(0, 0, min(size / 2 - 2 * pw,
                                 size * 0.8 / 2),
                       0, 2 * M_PI);
864
865
                if (aspect != 1.)
                    cr.restore();
866
867
868
869
870
            }
            break;
        case SHAPE_PIE:
            {
                if (!outline)
871
                {
872
873
874
                    vector<double> f = _attrs.template get<vector<double> >(VERTEX_PIE_FRACTIONS);
                    vector<color_t> fcolors = _attrs.template get<vector<color_t> >(VERTEX_PIE_COLORS);
                    draw_pie(size / 2 + pw / 2, f, fcolors, cr);
875
                }
876
                else
877
                {
878
879
                    cr.arc(0, 0, size / 2., 0, 2 * M_PI);
                    cr.close_path();
880
                }
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
            }
            break;
        case SHAPE_TRIANGLE:
        case SHAPE_SQUARE:
        case SHAPE_PENTAGON:
        case SHAPE_HEXAGON:
        case SHAPE_HEPTAGON:
        case SHAPE_OCTAGON:
        case SHAPE_DOUBLE_TRIANGLE:
        case SHAPE_DOUBLE_SQUARE:
        case SHAPE_DOUBLE_PENTAGON:
        case SHAPE_DOUBLE_HEXAGON:
        case SHAPE_DOUBLE_HEPTAGON:
        case SHAPE_DOUBLE_OCTAGON:
            nsides = shape - SHAPE_TRIANGLE + 3;
            if (nsides > 8)
                nsides -= 7;
            if (aspect != 1.)
            {
                cr.save();
                cr.scale(aspect, 1.0);
            }
            draw_polygon(nsides, size / 2, cr);
            if (aspect != 1.)
                cr.restore();
            if (shape >= SHAPE_DOUBLE_TRIANGLE && !outline)
            {
                cr.stroke();
909
910
911
912
913
                if (aspect != 1.)
                {
                    cr.save();
                    cr.scale(aspect, 1.0);
                }
914
915
                draw_polygon(nsides, min(size / 2 - 2 * pw,
                                         size * 0.8 / 2), cr);
916
917
                if (aspect != 1.)
                    cr.restore();
Tiago Peixoto's avatar
Tiago Peixoto committed
918
            }
919
920
921
922
923
924
925
            break;
        case SHAPE_NONE:
            break;
        default:
            throw ValueException("Invalid vertex shape: " +
                                 lexical_cast<string>(int(_attrs.template get<vertex_shape_t>(VERTEX_SHAPE))));
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
926

927
928
929
930
931
932
        if (!outline && shape != SHAPE_PIE)
        {
            fillcolor = _attrs.template get<color_t>(VERTEX_FILL_COLOR);
            cr.set_source_rgba(get<0>(fillcolor), get<1>(fillcolor),
                               get<2>(fillcolor), get<3>(fillcolor));
            cr.fill_preserve();
Tiago Peixoto's avatar
Tiago Peixoto committed
933

934
935
936
            cr.set_source_rgba(get<0>(color), get<1>(color), get<2>(color),
                               get<3>(color));
            cr.stroke();
937
        }
938
939

        if (osrc != boost::python::object() && !outline)
940
        {
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
            double swidth, sheight;
            PycairoSurface* src = (PycairoSurface*) osrc.ptr();
            Cairo::RefPtr<Cairo::Surface> surface(new Cairo::Surface(src->surface));
            get_surface_size(surface, swidth, sheight);
            Cairo::RefPtr<Cairo::SurfacePattern> pat(Cairo::SurfacePattern::create(surface));
            //pat->set_extend(Cairo::EXTEND_REPEAT);

            double r = size / sqrt(2);
            double scale = r / max(swidth / aspect, sheight);

            Cairo::Matrix m = Cairo::identity_matrix();
            m.translate(swidth / 2, sheight / 2);
            m.scale(1. / scale, 1. / scale);
            pat->set_matrix(m);

            cr.set_source(pat);
            cr.rectangle(-r * aspect / 2, -r / 2, r * aspect, r);
            cr.fill();
959
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
960

961
        if (!outline)
Tiago Peixoto's avatar
Tiago Peixoto committed
962
        {
963
964
965
966
            string text = _attrs.template get<string>(VERTEX_TEXT);
            if (text != "")
            {
                cr.save();
967

968
969
970
                double text_pos = 0;
                double text_rotation = 0;
                vector<double> text_offset;
971

972
973
974
975
976
977
978
979
                cr.select_font_face(_attrs.template get<string>(VERTEX_FONT_FAMILY),
                                    static_cast<Cairo::FontSlant>(_attrs.template get<int32_t>(VERTEX_FONT_SLANT)),
                                    static_cast<Cairo::FontWeight>(_attrs.template get<int32_t>(VERTEX_FONT_WEIGHT)));
                cr.set_font_size(get_user_dist(cr, _attrs.template get<double>(VERTEX_FONT_SIZE)));
                text_pos = _attrs.template get<double>(VERTEX_TEXT_POSITION);
                text_rotation = _attrs.template get<double>(VERTEX_TEXT_ROTATION);
                text_offset = _attrs.template get<vector<double> >(VERTEX_TEXT_OFFSET);
                text_offset.resize(2, 0.0);
980

981
                cr.rotate(text_rotation);
982

983
984
985
986
                Cairo::TextExtents extents;
                cr.get_text_extents(text, extents);
                Cairo::FontExtents fextents;
                cr.get_font_extents(fextents);
Tiago Peixoto's avatar
Tiago Peixoto committed
987

988
989
990
991
992
993
994
995
996
997
                if (text_pos < 0)
                {
                    cr.translate(-extents.width / 2 - extents.x_bearing + text_offset[0],
                                 extents.height / 2 + text_offset[1]);
                }
                else
                {
                    pos_t origin;
                    origin.first = _pos.first + size * cos(text_pos);
                    origin.second = _pos.second + size * sin(text_pos);
998

999
1000
                    pos_t anchor = get_anchor(origin, cr, true);
                    double angle = atan2(_pos.second - anchor.second,
For faster browsing, not all history is shown. View entire blame