GCC Code Coverage Report


Directory: ./
File: include/2geom/linear.h
Date: 2024-03-18 17:01:34
Exec Total Coverage
Lines: 38 48 79.2%
Functions: 16 25 64.0%
Branches: 8 16 50.0%

Line Branch Exec Source
1 /**
2 * \file
3 * \brief Linear fragment function class
4 *//*
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
42 namespace Geom {
43
44 class SBasis;
45
46 /**
47 * @brief Function that interpolates linearly between two values.
48 * @ingroup Fragments
49 */
50 class Linear
51 : boost::additive< Linear
52 , boost::arithmetic< Linear, Coord
53 , boost::equality_comparable< Linear
54 > > >
55 {
56 public:
57 Coord a[2];
58 2376453 Linear() {a[0]=0; a[1]=0;}
59 3172896 Linear(Coord aa, Coord b) {a[0] = aa; a[1] = b;}
60 5549871 Linear(Coord aa) {a[0] = aa; a[1] = aa;}
61
62 1410600 Coord operator[](unsigned i) const {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1410600 times.
1410600 assert(i < 2);
64 1410600 return a[i];
65 }
66 35108346 Coord &operator[](unsigned i) {
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35108346 times.
35108346 assert(i < 2);
68 35108346 return a[i];
69 }
70
71 //IMPL: FragmentConcept
72 typedef Coord output_type;
73
4/4
✓ Branch 1 taken 1603267 times.
✓ Branch 2 taken 2809009 times.
✓ Branch 4 taken 1213347 times.
✓ Branch 5 taken 389920 times.
4412276 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
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 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 9607002 double tri() const {
103 9607002 return a[1] - a[0];
104 }
105 48006 double hat() const {
106 48006 return (a[1] + a[0])/2;
107 }
108
109 // addition of other Linears
110 5853823 Linear &operator+=(Linear const &other) {
111 5853823 a[0] += other.a[0];
112 5853823 a[1] += other.a[1];
113 5853823 return *this;
114 }
115 1231190 Linear &operator-=(Linear const &other) {
116 1231190 a[0] -= other.a[0];
117 1231190 a[1] -= other.a[1];
118 1231190 return *this;
119 }
120
121 //
122 11700 Linear &operator+=(Coord x) {
123 11700 a[0] += x; a[1] += x;
124 11700 return *this;
125 }
126 59111 Linear &operator-=(Coord x) {
127 59111 a[0] -= x; a[1] -= x;
128 59111 return *this;
129 }
130 2089732 Linear &operator*=(Coord x) {
131 2089732 a[0] *= x; a[1] *= x;
132 2089732 return *this;
133 }
134 184 Linear &operator/=(Coord x) {
135 184 a[0] /= x; a[1] /= x;
136 184 return *this;
137 }
138 613026 Linear operator-() const {
139 613026 Linear ret(-a[0], -a[1]);
140 613026 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
148 inline Linear reverse(Linear const &a) { return Linear(a[1], a[0]); }
149 inline 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 :
168