Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
linear.h
Go to the documentation of this file.
1/*
5 * Authors:
6 * Nathan Hurst <njh@mail.csse.monash.edu.au>
7 * Michael Sloan <mgsloan@gmail.com>
8 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
9 *
10 * Copyright (C) 2006-2015 authors
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it either under the terms of the GNU Lesser General Public
14 * License version 2.1 as published by the Free Software Foundation
15 * (the "LGPL") or, at your option, under the terms of the Mozilla
16 * Public License Version 1.1 (the "MPL"). If you do not alter this
17 * notice, a recipient may use your version of this file under either
18 * the MPL or the LGPL.
19 *
20 * You should have received a copy of the LGPL along with this library
21 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * You should have received a copy of the MPL along with this library
24 * in the file COPYING-MPL-1.1
25 *
26 * The contents of this file are subject to the Mozilla Public License
27 * Version 1.1 (the "License"); you may not use this file except in
28 * compliance with the License. You may obtain a copy of the License at
29 * http://www.mozilla.org/MPL/
30 *
31 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
32 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
33 * the specific language governing rights and limitations.
34 */
35
36#ifndef LIB2GEOM_SEEN_LINEAR_H
37#define LIB2GEOM_SEEN_LINEAR_H
38
39#include <2geom/interval.h>
40#include <2geom/math-utils.h>
41
42namespace Geom {
43
44class SBasis;
45
50class Linear
51 : boost::additive< Linear
52 , boost::arithmetic< Linear, Coord
53 , boost::equality_comparable< Linear
54 > > >
55{
56public:
57 Coord a[2];
58 Linear() {a[0]=0; a[1]=0;}
59 Linear(Coord aa, Coord b) {a[0] = aa; a[1] = b;}
60 Linear(Coord aa) {a[0] = aa; a[1] = aa;}
61
62 Coord operator[](unsigned i) const {
63 assert(i < 2);
64 return a[i];
65 }
66 Coord &operator[](unsigned i) {
67 assert(i < 2);
68 return a[i];
69 }
70
71 //IMPL: FragmentConcept
73 bool isZero(Coord eps=EPSILON) const { return are_near(a[0], 0., eps) && are_near(a[1], 0., eps); }
74 bool isConstant(Coord eps=EPSILON) const { return are_near(a[0], a[1], eps); }
75 bool isFinite() const { return std::isfinite(a[0]) && std::isfinite(a[1]); }
76
77 Coord at0() const { return a[0]; }
78 Coord &at0() { return a[0]; }
79 Coord at1() const { return a[1]; }
80 Coord &at1() { return a[1]; }
81
82 Coord valueAt(Coord t) const { return lerp(t, a[0], a[1]); }
83 Coord operator()(Coord t) const { return valueAt(t); }
84
85 // not very useful, but required for FragmentConcept
86 std::vector<Coord> valueAndDerivatives(Coord t, unsigned n) {
87 std::vector<Coord> result(n+1, 0.0);
88 result[0] = valueAt(t);
89 if (n >= 1) {
90 result[1] = a[1] - a[0];
91 }
92 return result;
93 }
94
95 //defined in sbasis.h
96 inline SBasis toSBasis() const;
97
98 OptInterval bounds_exact() const { return Interval(a[0], a[1]); }
99 OptInterval bounds_fast() const { return bounds_exact(); }
100 OptInterval bounds_local(double u, double v) const { return Interval(valueAt(u), valueAt(v)); }
101
102 double tri() const {
103 return a[1] - a[0];
104 }
105 double hat() const {
106 return (a[1] + a[0])/2;
107 }
108
109 // addition of other Linears
110 Linear &operator+=(Linear const &other) {
111 a[0] += other.a[0];
112 a[1] += other.a[1];
113 return *this;
114 }
115 Linear &operator-=(Linear const &other) {
116 a[0] -= other.a[0];
117 a[1] -= other.a[1];
118 return *this;
119 }
120
121 //
123 a[0] += x; a[1] += x;
124 return *this;
125 }
127 a[0] -= x; a[1] -= x;
128 return *this;
129 }
131 a[0] *= x; a[1] *= x;
132 return *this;
133 }
135 a[0] /= x; a[1] /= x;
136 return *this;
137 }
139 Linear ret(-a[0], -a[1]);
140 return ret;
141 }
142
143 bool operator==(Linear const &other) const {
144 return a[0] == other.a[0] && a[1] == other.a[1];
145 }
146};
147
148inline Linear reverse(Linear const &a) { return Linear(a[1], a[0]); }
149inline Linear portion(Linear const &a, Coord from, Coord to) {
150 Linear result(a.valueAt(from), a.valueAt(to));
151 return result;
152}
153
154} // end namespace Geom
155
156#endif //LIB2GEOM_SEEN_LINEAR_H
157
158/*
159 Local Variables:
160 mode:c++
161 c-file-style:"stroustrup"
162 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
163 indent-tabs-mode:nil
164 fill-column:99
165 End:
166*/
167// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Range of real numbers that is never empty.
Definition interval.h:59
Function that interpolates linearly between two values.
Definition linear.h:55
Coord operator()(Coord t) const
Definition linear.h:83
Linear & operator+=(Linear const &other)
Definition linear.h:110
double hat() const
Definition linear.h:105
Linear(Coord aa)
Definition linear.h:60
Linear & operator-=(Coord x)
Definition linear.h:126
Coord valueAt(Coord t) const
Definition linear.h:82
OptInterval bounds_local(double u, double v) const
Definition linear.h:100
SBasis toSBasis() const
Definition sbasis.h:258
Linear operator-() const
Definition linear.h:138
Coord operator[](unsigned i) const
Definition linear.h:62
Linear & operator*=(Coord x)
Definition linear.h:130
Coord & at1()
Definition linear.h:80
Coord & operator[](unsigned i)
Definition linear.h:66
bool isZero(Coord eps=EPSILON) const
Definition linear.h:73
std::vector< Coord > valueAndDerivatives(Coord t, unsigned n)
Definition linear.h:86
bool operator==(Linear const &other) const
Definition linear.h:143
Linear & operator+=(Coord x)
Definition linear.h:122
Coord output_type
Definition linear.h:72
double tri() const
Definition linear.h:102
bool isConstant(Coord eps=EPSILON) const
Definition linear.h:74
OptInterval bounds_fast() const
Definition linear.h:99
Linear & operator-=(Linear const &other)
Definition linear.h:115
Coord at1() const
Definition linear.h:79
Coord a[2]
Definition linear.h:57
Coord at0() const
Definition linear.h:77
Coord & at0()
Definition linear.h:78
bool isFinite() const
Definition linear.h:75
Linear & operator/=(Coord x)
Definition linear.h:134
OptInterval bounds_exact() const
Definition linear.h:98
Linear(Coord aa, Coord b)
Definition linear.h:59
Range of real numbers that can be empty.
Definition interval.h:199
Polynomial in symmetric power basis.
Definition sbasis.h:70
Css & result
constexpr Coord lerp(Coord t, Coord a, Coord b)
Numerically stable linear interpolation.
Definition coord.h:97
double Coord
Floating point type used to store coordinates.
Definition coord.h:76
constexpr Coord EPSILON
Default "acceptably small" value.
Definition coord.h:84
Simple closed interval class.
Low level math functions and compatibility wrappers.
Various utility functions.
Definition affine.h:22
Geom::SBasisOf< double > SBasis
Definition sbasis-of.h:54
Bezier reverse(const Bezier &a)
Definition bezier.h:342
Bezier portion(const Bezier &a, double from, double to)
Definition bezier.cpp:250
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)