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 *
9 * Copyright (C) 2006-2007 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_LINEAR_H
36 #define SEEN_LINEAR_H
37 #include <2geom/interval.h>
38 #include <2geom/isnan.h>
41 //#define USE_SBASIS_OF
43 #ifdef USE_SBASIS_OF
45 #include "linear-of.h"
47 #else
49 namespace Geom{
51 inline double lerp(double t, double a, double b) { return a*(1-t) + b*t; }
53 class SBasis;
55 class Linear{
56 public:
57 double a[2];
58 Linear() {}
59 Linear(double aa, double b) {a[0] = aa; a[1] = b;}
60 Linear(double aa) {a[0] = aa; a[1] = aa;}
62 double operator[](const int i) const {
63 assert(i >= 0);
64 assert(i < 2);
65 return a[i];
66 }
67 double& operator[](const int i) {
68 assert(i >= 0);
69 assert(i < 2);
70 return a[i];
71 }
73 //IMPL: FragmentConcept
74 typedef double output_type;
75 inline bool isZero() const { return a[0] == 0 && a[1] == 0; }
76 inline bool isConstant() const { return a[0] == a[1]; }
77 inline bool isFinite() const { return IS_FINITE(a[0]) && IS_FINITE(a[1]); }
79 inline double at0() const { return a[0]; }
80 inline double at1() const { return a[1]; }
82 inline double valueAt(double t) const { return lerp(t, a[0], a[1]); }
83 inline double operator()(double t) const { return valueAt(t); }
85 //defined in sbasis.h
86 inline SBasis toSBasis() const;
88 inline OptInterval bounds_exact() const { return Interval(a[0], a[1]); }
89 inline OptInterval bounds_fast() const { return bounds_exact(); }
90 inline OptInterval bounds_local(double u, double v) const { return Interval(valueAt(u), valueAt(v)); }
92 double tri() const {
93 return a[1] - a[0];
94 }
95 double hat() const {
96 return (a[1] + a[0])/2;
97 }
98 };
100 inline Linear reverse(Linear const &a) { return Linear(a[1], a[0]); }
102 //IMPL: AddableConcept
103 inline Linear operator+(Linear const & a, Linear const & b) {
104 return Linear(a[0] + b[0], a[1] + b[1]);
105 }
106 inline Linear operator-(Linear const & a, Linear const & b) {
107 return Linear(a[0] - b[0], a[1] - b[1]);
108 }
109 inline Linear& operator+=(Linear & a, Linear const & b) {
110 a[0] += b[0]; a[1] += b[1];
111 return a;
112 }
113 inline Linear& operator-=(Linear & a, Linear const & b) {
114 a[0] -= b[0]; a[1] -= b[1];
115 return a;
116 }
117 //IMPL: OffsetableConcept
118 inline Linear operator+(Linear const & a, double b) {
119 return Linear(a[0] + b, a[1] + b);
120 }
121 inline Linear operator-(Linear const & a, double b) {
122 return Linear(a[0] - b, a[1] - b);
123 }
124 inline Linear& operator+=(Linear & a, double b) {
125 a[0] += b; a[1] += b;
126 return a;
127 }
128 inline Linear& operator-=(Linear & a, double b) {
129 a[0] -= b; a[1] -= b;
130 return a;
131 }
132 //IMPL: boost::EqualityComparableConcept
133 inline bool operator==(Linear const & a, Linear const & b) {
134 return a[0] == b[0] && a[1] == b[1];
135 }
136 inline bool operator!=(Linear const & a, Linear const & b) {
137 return a[0] != b[0] || a[1] != b[1];
138 }
139 //IMPL: ScalableConcept
140 inline Linear operator-(Linear const &a) {
141 return Linear(-a[0], -a[1]);
142 }
143 inline Linear operator*(Linear const & a, double b) {
144 return Linear(a[0]*b, a[1]*b);
145 }
146 inline Linear operator/(Linear const & a, double b) {
147 return Linear(a[0]/b, a[1]/b);
148 }
149 inline Linear operator*=(Linear & a, double b) {
150 a[0] *= b; a[1] *= b;
151 return a;
152 }
153 inline Linear operator/=(Linear & a, double b) {
154 a[0] /= b; a[1] /= b;
155 return a;
156 }
158 }
159 #endif
161 #endif //SEEN_LINEAR_H
163 /*
164 Local Variables:
165 mode:c++
166 c-file-style:"stroustrup"
167 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
168 indent-tabs-mode:nil
169 fill-column:99
170 End:
171 */
172 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :