Code

Merge from fe-moved
[inkscape.git] / src / 2geom / linear.h
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 Hat{
56 public:
57     Hat () {}
58     Hat(double d) :d(d) {}
59     operator double() const { return d; }
60     double d;
61 };
63 class Tri{
64 public:
65     Tri () {}
66     Tri(double d) :d(d) {}
67     operator double() const { return d; }
68     double d;
69 };
71 class Linear{
72 public:
73     double a[2];
74     Linear() {}
75     Linear(double aa, double b) {a[0] = aa; a[1] = b;}
76     Linear(Hat h, Tri t) {
77         a[0] = double(h) - double(t)/2; 
78         a[1] = double(h) + double(t)/2;
79     }
81     Linear(Hat h) {
82         a[0] = double(h); 
83         a[1] = double(h);
84     }
86     double operator[](const int i) const {
87         assert(i >= 0);
88         assert(i < 2);
89         return a[i];
90     }
91     double& operator[](const int i) {
92         assert(i >= 0);
93         assert(i < 2);
94         return a[i];
95     }
97     //IMPL: FragmentConcept
98     typedef double output_type;
99     inline bool isZero() const { return a[0] == 0 && a[1] == 0; }
100     inline bool isConstant() const { return a[0] == a[1]; }
101     inline bool isFinite() const { return IS_FINITE(a[0]) && IS_FINITE(a[1]); }
103     inline double at0() const { return a[0]; }
104     inline double at1() const { return a[1]; }
106     inline double valueAt(double t) const { return lerp(t, a[0], a[1]); }
107     inline double operator()(double t) const { return valueAt(t); }
109     //defined in sbasis.h
110     inline SBasis toSBasis() const;
112     inline OptInterval bounds_exact() const { return Interval(a[0], a[1]); }
113     inline OptInterval bounds_fast() const { return bounds_exact(); }
114     inline OptInterval bounds_local(double u, double v) const { return Interval(valueAt(u), valueAt(v)); }
116     operator Tri() const {
117         return a[1] - a[0];
118     }
119     operator Hat() const {
120         return (a[1] + a[0])/2;
121     }
122 };
124 inline Linear reverse(Linear const &a) { return Linear(a[1], a[0]); }
126 //IMPL: AddableConcept
127 inline Linear operator+(Linear const & a, Linear const & b) {
128     return Linear(a[0] + b[0], a[1] + b[1]);
130 inline Linear operator-(Linear const & a, Linear const & b) {
131     return Linear(a[0] - b[0], a[1] - b[1]);
133 inline Linear& operator+=(Linear & a, Linear const & b) {
134     a[0] += b[0]; a[1] += b[1];
135     return a;
137 inline Linear& operator-=(Linear & a, Linear const & b) {
138     a[0] -= b[0]; a[1] -= b[1];
139     return a;
141 //IMPL: OffsetableConcept
142 inline Linear operator+(Linear const & a, double b) {
143     return Linear(a[0] + b, a[1] + b);
145 inline Linear operator-(Linear const & a, double b) {
146     return Linear(a[0] - b, a[1] - b);
148 inline Linear& operator+=(Linear & a, double b) {
149     a[0] += b; a[1] += b;
150     return a;
152 inline Linear& operator-=(Linear & a, double b) {
153     a[0] -= b; a[1] -= b;
154     return a;
156 //IMPL: boost::EqualityComparableConcept
157 inline bool operator==(Linear const & a, Linear const & b) {
158     return a[0] == b[0] && a[1] == b[1];
160 inline bool operator!=(Linear const & a, Linear const & b) {
161     return a[0] != b[0] || a[1] != b[1];
163 //IMPL: ScalableConcept
164 inline Linear operator-(Linear const &a) {
165     return Linear(-a[0], -a[1]);
167 inline Linear operator*(Linear const & a, double b) {
168     return Linear(a[0]*b, a[1]*b);
170 inline Linear operator/(Linear const & a, double b) {
171     return Linear(a[0]/b, a[1]/b);
173 inline Linear operator*=(Linear & a, double b) {
174     a[0] *= b; a[1] *= b;
175     return a;
177 inline Linear operator/=(Linear & a, double b) {
178     a[0] /= b; a[1] /= b;
179     return a;
183 #endif
185 #endif //SEEN_LINEAR_H
187 /*
188   Local Variables:
189   mode:c++
190   c-file-style:"stroustrup"
191   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
192   indent-tabs-mode:nil
193   fill-column:99
194   End:
195 */
196 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :