1 /*
2 * PathVector - std::vector containing Geom::Path
3 * This file provides a set of operations that can be performed on PathVector,
4 * e.g. an affine transform.
5 *
6 * Authors:
7 * Johan Engelen <goejendaagh@zonnet.nl>
8 *
9 * Copyright 2008 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 */
35 #ifndef SEEN_GEOM_PATHVECTOR_CPP
36 #define SEEN_GEOM_PATHVECTOR_CPP
38 #include <2geom/pathvector.h>
40 #include <2geom/path.h>
41 #include <2geom/matrix.h>
43 namespace Geom {
45 // TODO: see which of these functions can be inlined for optimization
47 /**
48 * Reverses all Paths and the order of paths in the vector as well
49 **/
50 PathVector reverse_paths_and_order (PathVector const & path_in)
51 {
52 PathVector path_out;
53 for (PathVector::const_reverse_iterator it = path_in.rbegin(); it != path_in.rend(); ++it) {
54 path_out.push_back( (*it).reverse() );
55 }
56 return path_out;
57 }
59 // FIXME: this function does not work for empty paths, because Rect cannot be initialized empty yet. check rect.h
60 Rect bounds_fast( PathVector const& pv )
61 {
62 typedef PathVector::const_iterator const_iterator;
64 Rect bound;
65 if (pv.empty()) return bound;
67 bound = (pv.begin())->boundsFast();
68 for (const_iterator it = ++(pv.begin()); it != pv.end(); ++it)
69 {
70 bound.unionWith(it->boundsFast());
71 }
72 return bound;
73 }
75 // FIXME: this function does not work for empty paths, because Rect cannot be initialized empty yet. check rect.h
76 Rect bounds_exact( PathVector const& pv )
77 {
78 typedef PathVector::const_iterator const_iterator;
80 Rect bound;
81 if (pv.empty()) return bound;
83 bound = (pv.begin())->boundsExact();
84 for (const_iterator it = ++(pv.begin()); it != pv.end(); ++it)
85 {
86 bound.unionWith(it->boundsExact());
87 }
88 return bound;
89 }
91 /* Note: undefined for empty pathvectors or pathvectors with empty paths.
92 * */
93 boost::optional<PathVectorPosition> nearestPoint(PathVector const & path_in, Point const& _point, double *distance_squared)
94 {
95 boost::optional<PathVectorPosition> retval;
97 double mindsq = infinity();
98 unsigned int i = 0;
99 for (Geom::PathVector::const_iterator pit = path_in.begin(); pit != path_in.end(); ++pit) {
100 double dsq;
101 double t = pit->nearestPoint(_point, &dsq);
102 //std::cout << t << "," << dsq << std::endl;
103 if (dsq < mindsq) {
104 mindsq = dsq;
105 retval = PathVectorPosition(i, t);
106 }
108 ++i;
109 }
111 if (distance_squared) {
112 *distance_squared = mindsq;
113 }
114 return retval;
115 }
117 std::vector<PathVectorPosition> allNearestPoints(PathVector const & path_in, Point const& _point, double *distance_squared)
118 {
119 std::vector<PathVectorPosition> retval;
121 double mindsq = infinity();
122 unsigned int i = 0;
123 for (Geom::PathVector::const_iterator pit = path_in.begin(); pit != path_in.end(); ++pit) {
124 double dsq;
125 double t = pit->nearestPoint(_point, &dsq);
126 if (dsq < mindsq) {
127 mindsq = dsq;
128 retval.push_back(PathVectorPosition(i, t));
129 }
131 ++i;
132 }
134 if (distance_squared) {
135 *distance_squared = mindsq;
136 }
137 return retval;
139 }
141 } // namespace Geom
143 #endif // SEEN_GEOM_PATHVECTOR_CPP
145 /*
146 Local Variables:
147 mode:c++
148 c-file-style:"stroustrup"
149 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
150 indent-tabs-mode:nil
151 fill-column:99
152 End:
153 */
154 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :