concepts.h revision 8001ba81cb851b38d86650a2fef5817facffb763
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * concepts.h - Declares various mathematical concepts, for restriction of template parameters
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Copyright 2007 Michael Sloan <mgsloan@gmail.com>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This library is free software; you can redistribute it and/or
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * modify it either under the terms of the GNU Lesser General Public
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * License version 2.1 as published by the Free Software Foundation
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * (the "LGPL") or, at your option, under the terms of the Mozilla
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Public License Version 1.1 (the "MPL"). If you do not alter this
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * notice, a recipient may use your version of this file under either
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * the MPL or the LGPL.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * You should have received a copy of the LGPL along with this library
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * in the file COPYING-LGPL-2.1; if not, output to the Free Software
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * You should have received a copy of the MPL along with this library
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * in the file COPYING-MPL-1.1
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * The contents of this file are subject to the Mozilla Public License
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * Version 1.1 (the "License"); you may not use this file except in
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * compliance with the License. You may obtain a copy of the License at
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * http://www.mozilla.org/MPL/
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * OF ANY KIND, either express or implied. See the LGPL or the MPL for
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * the specific language governing rights and limitations.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen *
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#ifndef SEEN_CONCEPTS_H
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#define SEEN_CONCEPTS_H
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/sbasis.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/interval.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/point.h>
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen#include <vector>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#include <boost/concept_check.hpp>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Geom {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//forward decls
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T> class D2;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T> struct ResultTraits;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <> struct ResultTraits<double> {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef Interval bounds_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef SBasis sb_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <> struct ResultTraits<Point > {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef D2<Interval> bounds_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef D2<SBasis> sb_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//A concept for one-dimensional functions defined on [0,1]
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct FragmentConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef typename T::output_type OutputType;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef typename ResultTraits<OutputType>::bounds_type BoundsType;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef typename ResultTraits<OutputType>::sb_type SbType;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T t;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen OutputType o;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen BoundsType i;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Interval dom;
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen std::vector<OutputType> v;
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen unsigned u;
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen SbType sb;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = T(o);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen b = t.isZero();
bacf092e612ad1911c5a4db58261865e54953b34johanengelen b = t.isConstant();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen b = t.isFinite();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t.at0();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t.at1();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t.valueAt(d);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t(d);
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen v = t.valueAndDerivatives(d, u-1);
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen //Is a pure derivative (ignoring others) accessor ever much faster?
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen //u = number of values returned. first val is value.
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen sb = t.toSBasis();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = reverse(t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i = bounds_fast(t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i = bounds_exact(t);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i = bounds_local(t, dom);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen /*With portion, Interval makes some sense, but instead I'm opting for
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen doubles, for the following reasons:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen A) This way a reversed portion may be specified
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen B) Performance might be a bit better for piecewise and such
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen C) Interval version provided below
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen */
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = portion(t, d, d);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline T portion(const T& t, const Interval& i) { return portion(t, i.min(), i.max()); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct NearConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T a, b;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen double tol;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen bool res;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
e6bdf746e2d9e775704a475a29cc1bb167ec271cjohanengelen res = are_near(a, b, tol);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct OffsetableConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T t;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typename T::output_type d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = t + d; t += d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = t - d; t -= d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct ScalableConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T t;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typename T::output_type d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = -t;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = t * d; t *= d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen t = t / d; t /= d;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <class T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct AddableConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T i, j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i += j; i = i + j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i -= j; i = i - j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <class T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct MultiplicableConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T i, j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i *= j; i = i * j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen#endif //SEEN_CONCEPTS_H
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen/*
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen Local Variables:
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen mode:c++
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen c-file-style:"stroustrup"
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen indent-tabs-mode:nil
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen fill-column:99
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen End:
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen*/
63267518b4ce196caab66ef8cbdcfc0921206b3djohanengelen// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :