01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould/**
01d27eab5fca2dcb8e883011f8be77ae6b78a11cTed Gould * \file
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * \brief Template concepts used by 2Geom
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *//*
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
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#ifndef LIB2GEOM_SEEN_CONCEPTS_H
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#define LIB2GEOM_SEEN_CONCEPTS_H
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/sbasis.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/interval.h>
8001ba81cb851b38d86650a2fef5817facffb763johanengelen#include <2geom/point.h>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <2geom/rect.h>
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński#include <2geom/intersection.h>
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen#include <vector>
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński#include <boost/concept/assert.hpp>
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould#include <2geom/forward.h>
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński#include <2geom/transforms.h>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelennamespace Geom {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen//forward decls
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T> struct ResultTraits;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <> struct ResultTraits<double> {
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould typedef OptInterval bounds_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen typedef SBasis sb_type;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskitemplate <> struct ResultTraits<Point> {
6c3e745a94ef6b25a4ef9f018d350a7535aa45afTed Gould typedef OptRect 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);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński b = t.isZero(d);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński b = t.isConstant(d);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen b = t.isFinite();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t.at0();
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t.at1();
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński t.at0() = o;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński t.at1() = o;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t.valueAt(d);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen o = t(d);
6492fcbc5fbe9a07962f989247731b6435fd72b9johanengelen v = t.valueAndDerivatives(d, u-1);
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński //Is a pure derivative (ignoring others) accessor ever much faster?
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński //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
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskitemplate <typename T>
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskistruct ShapeConcept {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński typedef typename ShapeTraits<T>::TimeType Time;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński typedef typename ShapeTraits<T>::IntervalType Interval;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński typedef typename ShapeTraits<T>::AffineClosureType AffineClosure;
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński typedef typename ShapeTraits<T>::IntersectionType Isect;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński T shape, other;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Time t;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Point p;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński AffineClosure ac;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Affine m;
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński Translate tr;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Coord c;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool bool_;
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński std::vector<Isect> ivec;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński void constraints() {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński p = shape.pointAt(t);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński c = shape.valueAt(t, X);
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński ivec = shape.intersect(other);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński t = shape.nearestTime(p);
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński shape *= tr;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński ac = shape;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński ac *= m;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool_ = (shape == shape);
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński bool_ = (shape != other);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool_ = shape.isDegenerate();
e4369b05aaa20df73a37f4d319ce456865cc74f3Krzysztof Kosiński //bool_ = are_near(shape, other, c);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński};
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński
981b809bc6ed10a21e89444d9447e5475801874fjohanengelentemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline T portion(const T& t, const Interval& i) { return portion(t, i.min(), i.max()); }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosińskitemplate <typename T>
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosińskistruct EqualityComparableConcept {
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński T a, b;
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński bool bool_;
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński void constaints() {
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński bool_ = (a == b);
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński bool_ = (a != b);
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński }
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński};
00f9ca0b3aa57e09f3c3f3632c5427fc03499df5Krzysztof Kosiński
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
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskitemplate <typename 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
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskitemplate <typename T>
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstruct MultiplicableConcept {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T i, j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen void constraints() {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen i *= j; i = i * j;
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen }
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen};
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński} // end namespace Geom
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#endif // LIB2GEOM_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*/
a4030d5ca449e7e384bc699cd249ee704faaeab0Chris Morgan// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :