42192c4458178444b6970aab08d3c6a40704652e
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"
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 void constraints() {
70 t = T(o);
71 b = t.isZero();
72 b = t.isFinite();
73 o = t.at0();
74 o = t.at1();
75 o = t.valueAt(d);
76 o = t(d);
77 SbType sb = t.toSBasis();
78 t = reverse(t);
79 i = bounds_fast(t);
80 i = bounds_exact(t);
81 i = bounds_local(t, dom);
82 /*With portion, Interval makes some sense, but instead I'm opting for
83 doubles, for the following reasons:
84 A) This way a reversed portion may be specified
85 B) Performance might be a bit better for piecewise and such
86 C) Interval version provided below
87 */
88 t = portion(t, d, d);
89 }
90 };
92 template <typename T>
93 inline T portion(const T& t, const Interval& i) { return portion(t, i.min(), i.max()); }
95 template <typename T>
96 struct NearConcept {
97 T a, b;
98 double tol;
99 bool res;
100 void constraints() {
101 res = near(a, b, tol);
102 }
103 };
105 template <typename T>
106 struct OffsetableConcept {
107 T t;
108 typename T::output_type d;
109 void constraints() {
110 t = t + d; t += d;
111 t = t - d; t -= d;
112 }
113 };
115 template <typename T>
116 struct ScalableConcept {
117 T t;
118 typename T::output_type d;
119 void constraints() {
120 t = -t;
121 t = t * d; t *= d;
122 t = t / d; t /= d;
123 }
124 };
126 template <class T>
127 struct AddableConcept {
128 T i, j;
129 void constraints() {
130 i += j; i = i + j;
131 i -= j; i = i - j;
132 }
133 };
135 template <class T>
136 struct MultiplicableConcept {
137 T i, j;
138 void constraints() {
139 i *= j; i = i * j;
140 }
141 };
143 };
145 #endif //SEEN_CONCEPTS_H