Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
bezier-utils-test.cpp
Go to the documentation of this file.
1/*
2 * Test performance of bezier-util fitting code
3 *
4 * Copyright (C) authors 2013
5 * Authors:
6 * Johan Engelen <j.b.c.engelen@alumnus.utwente.nl>
7 *
8 * Released under GNU GPL, read the file 'COPYING' for more information
9 */
10
11
12#include <iostream>
13#include <ctime>
14
15#include "2geom/bezier-utils.h"
16#include "2geom/path.h"
17using namespace Geom;
18
19//static const Point data[] = { Point(0, 0), Point(1, 0), Point( 2, 0 ), Point( 1, 1 )};
20static const Point data[] = { Point( 0, 0 ), Point( 1, 0 ), Point( 2, 0 ), Point( 1, 1 ),
21 Point( 1, 2 ), Point( 1, 3 ), Point( 3, 0 ), Point( 4, 0 ),
22 Point( 2, 0 ), Point( 1, 1 ), Point( 1, 2 ), Point( 2, 0 ),
23 Point( 1, 0 ), Point( 2, 0 ), Point( 1, 3 ), Point( 1, 4 ),
24 Point( 1, 2 ), Point( 1, 1 ), Point( 0, 0 ), Point( 1, 0 ),
25 Point( 2, 0 ), Point( 1, 1 ), Point( 1, 2 ), Point( 1, 3 ),
26 Point( 4, 0 ), Point( 2, 0 ), Point( 1, 1 ), Point( 1, 2 ),
27 Point( 2, 0 ), Point( 1, 0 ), Point( 2, 0 ), Point( 1, 3 ),
28 Point( 1, 4 ), Point( 1, 2 ), Point( 1, 1 ), Point( 2, 1 ) };
29
30static const unsigned int data_len = sizeof(data)/sizeof(Point);
31
32
33
34// code test with 2geom types
35Path interpolateToPath(std::vector<Point> const &points, double tolerance_sq, unsigned max_beziers)
36{
37 Geom::Point *b = new Geom::Point[max_beziers*4];
38 Geom::Point *points_array = new Geom::Point[4 * points.size()]; // for safety, do a copy into a vector. I think it
39 // is possible to simply pass &points[0] as a
40 // Point[], but I am not sure
41 for (size_t i = 0; i < points.size(); ++i) {
42 points_array[i] = points.at(i);
43 }
44
45 int const n_segs = Geom::bezier_fit_cubic_r(b, points_array, points.size(), tolerance_sq, max_beziers);
46
47 Geom::Path fit;
48 if (n_segs > 0) {
49 fit.start(b[0]);
50 for (int c = 0; c < n_segs; c++) {
51 fit.appendNew<Geom::CubicBezier>(b[4 * c + 1], b[4 * c + 2], b[4 * c + 3]);
52 }
53 }
54
55 delete[] b;
56 delete[] points_array;
57
58 return fit;
59};
60
61
62Path interpolateToPath2(std::vector<Point> const &points, double tolerance_sq, unsigned max_beziers)
63{
64 std::vector<Point> b(max_beziers * 4);
65
66 int const n_segs = Geom::bezier_fit_cubic_r(b.data(), points.data(), points.size(), tolerance_sq, max_beziers);
67
68 Geom::Path fit;
69 if (n_segs > 0) {
70 fit.start(b[0]);
71 for (int c = 0; c < n_segs; c++) {
72 fit.appendNew<Geom::CubicBezier>(b[4 * c + 1], b[4 * c + 2], b[4 * c + 3]);
73 }
74 }
75
76 return fit;
77};
78
79
80int main()
81{
82 std::vector<Point> data_vector;
83 for (auto i : data) {
84 data_vector.push_back(i);
85 }
86
87 const int num_repeats = 2000;
88
89 unsigned max_beziers = data_len*2;
90 double tolerance_sq = 0.01;
91
92 for (int rep = 0; rep < 3; rep++) {
93 std::clock_t start = std::clock();
94 for (int i = 0; i < num_repeats; i++) {
95 Point *bezier = new Point[max_beziers*4]; // large array on stack = not good, so allocate on heap
96 int n_segs = bezier_fit_cubic_r(bezier, data, data_len, tolerance_sq, max_beziers);
97 (void) n_segs;
98 delete[] bezier;
99 }
100 std::clock_t stop = std::clock();
101 std::cout << "bezier_fit_cubic_r C-array (" << num_repeats << "x): " << (stop - start) * (1000. / CLOCKS_PER_SEC) << " ms "
102 << std::endl;
103 }
104
105 for (int rep = 0; rep < 3; rep++) {
106 std::clock_t start = std::clock();
107 for (int i = 0; i < num_repeats; i++) {
108 Path path = interpolateToPath(data_vector, tolerance_sq, max_beziers);
109 int n_segs = path.size();
110 (void) n_segs;
111 }
112 std::clock_t stop = std::clock();
113 std::cout << "bezier_fit_cubic_r 2Geom interoperability (" << num_repeats << "x): " << (stop - start) * (1000. / CLOCKS_PER_SEC) << " ms "
114 << std::endl;
115 }
116
117 for (int rep = 0; rep < 3; rep++) {
118 std::clock_t start = std::clock();
119 for (int i = 0; i < num_repeats; i++) {
120 Path path = interpolateToPath2(data_vector, tolerance_sq, max_beziers);
121 int n_segs = path.size();
122 (void) n_segs;
123 }
124 std::clock_t stop = std::clock();
125 std::cout << "bezier_fit_cubic_r 2Geom interoperability 2nd version (" << num_repeats
126 << "x): " << (stop - start) * (1000. / CLOCKS_PER_SEC) << " ms " << std::endl;
127 }
128
129}
130
131
132
133//
Path - a sequence of contiguous curves.
Bezier fitting algorithms.
Bezier curve with compile-time specified order.
Sequence of contiguous curves, aka spline.
Definition path.h:353
size_type size() const
Natural size of the path.
Definition path.h:490
void appendNew(Args &&... args)
Append a new curve to the path.
Definition path.h:804
void start(Point const &p)
Definition path.cpp:426
Two-dimensional point that doubles as a vector.
Definition point.h:66
double c[8][4]
Geom::Point start
Various utility functions.
Definition affine.h:22
int bezier_fit_cubic_r(Point bezier[], Point const data[], int len, double error, unsigned max_beziers)
Fit a multi-segment Bezier curve to a set of digitized points, with possible weedout of identical poi...
Path interpolateToPath2(std::vector< Point > const &points, double tolerance_sq, unsigned max_beziers)
static const unsigned int data_len
static const Point data[]
Path interpolateToPath(std::vector< Point > const &points, double tolerance_sq, unsigned max_beziers)
int main()