Code

update to 2geom rev. 1168
[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.isFinite();
76         o = t.at0();
77         o = t.at1();
78         o = t.valueAt(d);
79         o = t(d);
80         v = t.valueAndDerivatives(d, u);
81                 //Is a pure derivative (ignoring others) accessor ever much faster?
82                 //u = number of values returned. first val is value.
83         sb = t.toSBasis();
84         t = reverse(t);
85         i = bounds_fast(t);
86         i = bounds_exact(t);
87         i = bounds_local(t, dom);
88         /*With portion, Interval makes some sense, but instead I'm opting for
89           doubles, for the following reasons:
90           A) This way a reversed portion may be specified
91           B) Performance might be a bit better for piecewise and such
92           C) Interval version provided below
93          */
94         t = portion(t, d, d);
95     }
96 };
98 template <typename T>
99 inline T portion(const T& t, const Interval& i) { return portion(t, i.min(), i.max()); }
101 template <typename T>
102 struct NearConcept {
103     T a, b;
104     double tol;
105     bool res;
106     void constraints() {
107         res = are_near(a, b, tol);
108     }
109 };
111 template <typename T>
112 struct OffsetableConcept {
113     T t;
114     typename T::output_type d;
115     void constraints() {
116         t = t + d; t += d;
117         t = t - d; t -= d;
118     }
119 };
121 template <typename T>
122 struct ScalableConcept {
123     T t;
124     typename T::output_type d;
125     void constraints() {
126         t = -t;
127         t = t * d; t *= d;
128         t = t / d; t /= d;
129     }
130 };
132 template <class T>
133 struct AddableConcept {
134     T i, j;
135     void constraints() {
136         i += j; i = i + j;
137         i -= j; i = i - j;
138     }
139 };
141 template <class T>
142 struct MultiplicableConcept {
143     T i, j;
144     void constraints() {
145         i *= j; i = i * j;
146     }
147 };
149 };
151 #endif //SEEN_CONCEPTS_H