Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
path-extrema.cpp
Go to the documentation of this file.
1
3/* An algorithm to find the points on a path where a given coordinate
4 * attains its minimum and maximum values.
5 *
6 * Authors:
7 * RafaƂ Siejakowski <rs@rs-math.net>
8 *
9 * Copyright 2022 the Authors.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it either under the terms of the GNU Lesser General Public
13 * License version 2.1 as published by the Free Software Foundation
14 * (the "LGPL") or, at your option, under the terms of the Mozilla
15 * Public License Version 1.1 (the "MPL"). If you do not alter this
16 * notice, a recipient may use your version of this file under either
17 * the MPL or the LGPL.
18 *
19 * You should have received a copy of the LGPL along with this library
20 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * You should have received a copy of the MPL along with this library
23 * in the file COPYING-MPL-1.1
24 *
25 * The contents of this file are subject to the Mozilla Public License
26 * Version 1.1 (the "License"); you may not use this file except in
27 * compliance with the License. You may obtain a copy of the License at
28 * http://www.mozilla.org/MPL/
29 *
30 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
31 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
32 * the specific language governing rights and limitations.
33 */
34
35#include <2geom/curve.h>
36#include <2geom/path.h>
37#include <2geom/point.h>
38
39namespace Geom {
40
42inline static float sign(double number)
43{
44 if (number > 0.0) {
45 return 1.0;
46 } else if (number < 0.0) {
47 return -1.0;
48 }
49 return 0.0;
50}
51
59static float find_direction_of_travel(Path const &path, PathTime const &time, Dim2 d)
60{
61 if (time.t == 0.0) { // We're at a node point
62 if (time.curve_index == 0) { // Starting point of the path.
63 if (path.closed()) {
64 return sign(path.initialUnitTangent()[d] + path.finalUnitTangent()[d]);
65 } else {
66 return sign(path.initialUnitTangent()[d]);
67 }
68 } else if (time.curve_index == path.size()) { // End point of the path.
69 if (path.closed()) {
70 return sign(path.initialUnitTangent()[d] + path.finalUnitTangent()[d]);
71 } else {
72 return sign(path.finalUnitTangent()[d]);
73 }
74 }
75
76 // Otherwise, check the average of the two unit tangent vectors.
77 auto const outgoing_tangent = path.curveAt(time.curve_index).unitTangentAt(0.0);
78 auto const incoming_tangent = path.curveAt(time.curve_index - 1).unitTangentAt(1.0);
79 return sign(outgoing_tangent[d] + incoming_tangent[d]);
80 }
81 // We're in the middle of a curve
82 return sign(path.curveAt(time.curve_index).unitTangentAt(time.t)[d]);
83}
84
85/* Find information about the points on the path where the specified
86 * coordinate attains its minimum and maximum values.
87 */
89{
90 auto const ZERO_TIME = PathTime(0, 0);
91
92 // Handle the trivial case of an empty path.
93 if (empty()) {
94 auto const origin = initialPoint();
95 return PathExtrema{
97 .max_point = origin,
98 .glance_direction_at_min = 0.0,
99 .glance_direction_at_max = 0.0,
100 .min_time = ZERO_TIME,
101 .max_time = ZERO_TIME
102 };
103 }
104
105 // Set up the simultaneous min-max search
106 Point min_point = initialPoint(), max_point = min_point;
107 auto min_time = ZERO_TIME, max_time = ZERO_TIME;
108 unsigned curve_counter = 0;
109
111 auto const update_minmax = [&](Point const &new_point, Coord t) {
112 if (new_point[d] < min_point[d]) {
113 min_point = new_point;
114 min_time = PathTime(curve_counter, t);
115 } else if (new_point[d] > max_point[d]) {
116 max_point = new_point;
117 max_time = PathTime(curve_counter, t);
118 }
119 };
120
121 // Iterate through the curves, searching for min and max.
122 for (auto const &curve : _data->curves) {
123 // Check the starting node first
124 update_minmax(curve.initialPoint(), 0.0);
125
126 // Check the critical points (zeroes of the derivative).
127 std::unique_ptr<Curve> const derivative{curve.derivative()};
128 for (auto root : derivative->roots(0.0, d)) {
129 update_minmax(curve.pointAt(root), root);
130 }
131 curve_counter++;
132 }
133
134 auto const other = other_dimension(d);
135 return PathExtrema{
136 .min_point = min_point,
137 .max_point = max_point,
138 .glance_direction_at_min = find_direction_of_travel(*this, min_time, other),
139 .glance_direction_at_max = find_direction_of_travel(*this, max_time, other),
140 .min_time = min_time,
141 .max_time = max_time
142 };
143}
144
145} // namespace Geom
146
147/*
148 Local Variables:
149 mode:c++
150 c-file-style:"stroustrup"
151 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
152 indent-tabs-mode:nil
153 fill-column:99
154 End:
155*/
156// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Abstract curve type.
Path - a sequence of contiguous curves.
Cartesian point / 2D vector and related operations.
Point origin
Definition aa.cpp:227
std::vector< Coord > roots() const
Definition bezier.cpp:105
virtual Point unitTangentAt(Coord t, unsigned n=3) const
Compute a vector tangent to the curve.
Definition curve.cpp:201
Sequence of contiguous curves, aka spline.
Definition path.h:353
bool closed() const
Check whether the path is closed.
Definition path.h:503
PathExtrema extrema(Dim2 d) const
Find the extrema of the specified coordinate.
std::shared_ptr< PathData > _data
Definition path.h:860
bool empty() const
Check whether path is empty.
Definition path.h:500
Point finalUnitTangent() const
Get the unit tangent vector at the end of the path, or the zero vector if undefined.
Definition path.h:724
Point initialPoint() const
Get the first point in the path.
Definition path.h:705
Point initialUnitTangent() const
Get the unit tangent vector at the start of the path, or the zero vector if undefined.
Definition path.h:713
size_type size() const
Natural size of the path.
Definition path.h:490
Curve const & curveAt(Coord t, Coord *rest=NULL) const
Get the curve at the specified time value.
Definition path.cpp:440
Two-dimensional point that doubles as a vector.
Definition point.h:66
RootCluster root
Dim2
2D axis enumeration (X or Y).
Definition coord.h:48
constexpr Dim2 other_dimension(Dim2 d)
Get the other (perpendicular) dimension.
Definition coord.h:52
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
Various utility functions.
Definition affine.h:22
static float sign(double number)
Returns +1 for positive numbers, -1 for negative numbers, and 0 otherwise.
static float find_direction_of_travel(Path const &path, PathTime const &time, Dim2 d)
Determine whether the d-coordinate increases or decreases at the given path time.
Bezier derivative(Bezier const &a)
Definition bezier.cpp:282
Stores information about the extremum points on a path, with respect to one of the coordinate axes.
Definition path.h:292
Point min_point
Points with the minimum and maximum value of a coordinate.
Definition path.h:294
Generalized time value in the path.
Definition path.h:139
size_type curve_index
Index of the curve in the path.
Definition path.h:143
Coord t
Time value in the curve.
Definition path.h:142
Definition curve.h:24