Scribus
Open source desktop publishing at your fingertips
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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  */
30 
31 #ifndef SEEN_CONCEPTS_H
32 #define SEEN_CONCEPTS_H
33 
34 #include "sbasis.h"
35 #include "interval.h"
36 #include "point.h"
37 #include <vector>
38 #include <boost/concept_check.hpp>
39 
40 namespace Geom {
41 
42 //forward decls
43 template <typename T> class D2;
44 
45 template <typename T> struct ResultTraits;
46 
47 template <> struct ResultTraits<double> {
48  typedef Interval bounds_type;
49  typedef SBasis sb_type;
50 };
51 
52 template <> struct ResultTraits<Point > {
53  typedef D2<Interval> bounds_type;
54  typedef D2<SBasis> sb_type;
55 };
56 
57 //A concept for one-dimensional functions defined on [0,1]
58 template <typename T>
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);
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 };
98 
99 template <typename T>
100 inline T portion(const T& t, const Interval& i) { return portion(t, i.min(), i.max()); }
101 
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 };
111 
112 template <typename T>
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 };
121 
122 template <typename T>
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 };
132 
133 template <class T>
135  T i, j;
136  void constraints() {
137  i += j; i = i + j;
138  i -= j; i = i - j;
139  }
140 };
141 
142 template <class T>
144  T i, j;
145  void constraints() {
146  i *= j; i = i * j;
147  }
148 };
149 
150 };
151 
152 #endif //SEEN_CONCEPTS_H
Definition: angle.h:38
Definition: concepts.h:59
Definition: concepts.h:113
Definition: concepts.h:123
Definition: sbasis.h:48
Definition: concepts.h:143
Cartesian point.
Definition: point.h:20
Definition: concepts.h:103
Definition: concepts.h:45
Definition: concepts.h:43
Definition: interval.h:56
Definition: concepts.h:134