Code

update 2geom
[inkscape.git] / src / 2geom / concepts.h
1 /*
2  * concepts.h - Declares various mathematical concepts, for restriction of template parameters
3  *
4  * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, output to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  */
31 #ifndef SEEN_CONCEPTS_H
32 #define SEEN_CONCEPTS_H
34 #include "sbasis.h"
35 #include "interval.h"
36 #include "point.h"
37 #include <vector>
38 #include <boost/concept_check.hpp>
40 namespace Geom {
42 //forward decls
43 template <typename T> class D2;
45 template <typename T> struct ResultTraits;
47 template <> struct ResultTraits<double> {
48   typedef Interval bounds_type;
49   typedef SBasis sb_type;
50 };
52 template <> struct ResultTraits<Point > {
53   typedef D2<Interval> bounds_type;
54   typedef D2<SBasis> sb_type;
55 };
57 //A concept for one-dimensional functions defined on [0,1]
58 template <typename T>
59 struct FragmentConcept {
60     typedef typename T::output_type                        OutputType;
61     typedef typename ResultTraits<OutputType>::bounds_type BoundsType;
62     typedef typename ResultTraits<OutputType>::sb_type     SbType;
63     T t;
64     double d;
65     OutputType o;
66     bool b;
67     BoundsType i;
68     Interval dom;
69     std::vector<OutputType> v;
70     unsigned u;
71     SbType sb;
72     void constraints() {
73         t = T(o);
74         b = t.isZero();
75         b = t.isConstant();
76         b = t.isFinite();
77         o = t.at0();
78         o = t.at1();
79         o = t.valueAt(d);
80         o = t(d);
81         v = t.valueAndDerivatives(d, u-1);
82                 //Is a pure derivative (ignoring others) accessor ever much faster?
83                 //u = number of values returned. first val is value.
84         sb = t.toSBasis();
85         t = reverse(t);
86         i = bounds_fast(t);
87         i = bounds_exact(t);
88         i = bounds_local(t, dom);
89         /*With portion, Interval makes some sense, but instead I'm opting for
90           doubles, for the following reasons:
91           A) This way a reversed portion may be specified
92           B) Performance might be a bit better for piecewise and such
93           C) Interval version provided below
94          */
95         t = portion(t, d, d);
96     }
97 };
99 template <typename T>
100 inline T portion(const T& t, const Interval& i) { return portion(t, i.min(), i.max()); }
102 template <typename T>
103 struct NearConcept {
104     T a, b;
105     double tol;
106     bool res;
107     void constraints() {
108         res = are_near(a, b, tol);
109     }
110 };
112 template <typename T>
113 struct OffsetableConcept {
114     T t;
115     typename T::output_type d;
116     void constraints() {
117         t = t + d; t += d;
118         t = t - d; t -= d;
119     }
120 };
122 template <typename T>
123 struct ScalableConcept {
124     T t;
125     typename T::output_type d;
126     void constraints() {
127         t = -t;
128         t = t * d; t *= d;
129         t = t / d; t /= d;
130     }
131 };
133 template <class T>
134 struct AddableConcept {
135     T i, j;
136     void constraints() {
137         i += j; i = i + j;
138         i -= j; i = i - j;
139     }
140 };
142 template <class T>
143 struct MultiplicableConcept {
144     T i, j;
145     void constraints() {
146         i *= j; i = i * j;
147     }
148 };
150 };
152 #endif //SEEN_CONCEPTS_H
154 /*
155   Local Variables:
156   mode:c++
157   c-file-style:"stroustrup"
158   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
159   indent-tabs-mode:nil
160   fill-column:99
161   End:
162 */
163 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :