1 /**
2 * \file
3 * \brief Declares various mathematical concepts, for restriction of template parameters
4 *
5 * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
14 *
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, output to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
20 *
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
25 *
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
29 *
30 */
32 #ifndef SEEN_CONCEPTS_H
33 #define SEEN_CONCEPTS_H
35 #include <2geom/sbasis.h>
36 #include <2geom/interval.h>
37 #include <2geom/point.h>
38 #include <vector>
39 #include <boost/concept_check.hpp>
40 #include <2geom/forward.h>
42 namespace Geom {
44 //forward decls
45 template <typename T> struct ResultTraits;
47 template <> struct ResultTraits<double> {
48 typedef OptInterval bounds_type;
49 typedef SBasis sb_type;
50 };
52 template <> struct ResultTraits<Point > {
53 typedef OptRect 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 :