/* * d2.h - Lifts one dimensional objects into 2d * * Copyright 2007 Michael Sloan * * This library is free software; you can redistribute it and/or * modify it either under the terms of the GNU Lesser General Public * License version 2.1 as published by the Free Software Foundation * (the "LGPL") or, at your option, under the terms of the Mozilla * Public License Version 1.1 (the "MPL"). If you do not alter this * notice, a recipient may use your version of this file under either * the MPL or the LGPL. * * You should have received a copy of the LGPL along with this library * in the file COPYING-LGPL-2.1; if not, output to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * You should have received a copy of the MPL along with this library * in the file COPYING-MPL-1.1 * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY * OF ANY KIND, either express or implied. See the LGPL or the MPL for * the specific language governing rights and limitations. * */ #ifndef _2GEOM_D2 //If this is change, change the guard in rect.h as well. #define _2GEOM_D2 #include "point.h" #include "interval.h" #include "matrix.h" #include #include "concepts.h" namespace Geom{ template class D2{ //BOOST_CLASS_REQUIRE(T, boost, AssignableConcept); private: T f[2]; public: D2() {f[X] = f[Y] = T();} explicit D2(Point const &a) { f[X] = T(a[X]); f[Y] = T(a[Y]); } D2(T const &a, T const &b) { f[X] = a; f[Y] = b; } //TODO: ask mental about operator= as seen in Point T& operator[](unsigned i) { return f[i]; } T const & operator[](unsigned i) const { return f[i]; } //IMPL: FragmentConcept typedef Point output_type; bool isZero() const { boost::function_requires >(); return f[X].isZero() && f[Y].isZero(); } bool isConstant() const { boost::function_requires >(); return f[X].isConstant() && f[Y].isConstant(); } bool isFinite() const { boost::function_requires >(); return f[X].isFinite() && f[Y].isFinite(); } Point at0() const { boost::function_requires >(); return Point(f[X].at0(), f[Y].at0()); } Point at1() const { boost::function_requires >(); return Point(f[X].at1(), f[Y].at1()); } Point valueAt(double t) const { boost::function_requires >(); return (*this)(t); } std::vector valueAndDerivatives(double t, unsigned count) const { std::vector x = f[X].valueAndDerivatives(t, count), y = f[Y].valueAndDerivatives(t, count); std::vector res; for(unsigned i = 0; i < count; i++) { res.push_back(Point(x[i], y[i])); } return res; } D2 toSBasis() const { boost::function_requires >(); return D2(f[X].toSBasis(), f[Y].toSBasis()); } Point operator()(double t) const; Point operator()(double x, double y) const; }; template inline D2 reverse(const D2 &a) { boost::function_requires >(); return D2(reverse(a[X]), reverse(a[Y])); } template inline D2 portion(const D2 &a, Coord f, Coord t) { boost::function_requires >(); return D2(portion(a[X], f, t), portion(a[Y], f, t)); } //IMPL: boost::EqualityComparableConcept template inline bool operator==(D2 const &a, D2 const &b) { boost::function_requires >(); return a[0]==b[0] && a[1]==b[1]; } template inline bool operator!=(D2 const &a, D2 const &b) { boost::function_requires >(); return a[0]!=b[0] || a[1]!=b[1]; } //IMPL: NearConcept template inline bool are_near(D2 const &a, D2 const &b, double tol) { boost::function_requires >(); return are_near(a[0], b[0]) && are_near(a[1], b[1]); } //IMPL: AddableConcept template inline D2 operator+(D2 const &a, D2 const &b) { boost::function_requires >(); D2 r; for(unsigned i = 0; i < 2; i++) r[i] = a[i] + b[i]; return r; } template inline D2 operator-(D2 const &a, D2 const &b) { boost::function_requires >(); D2 r; for(unsigned i = 0; i < 2; i++) r[i] = a[i] - b[i]; return r; } template inline D2 operator+=(D2 &a, D2 const &b) { boost::function_requires >(); for(unsigned i = 0; i < 2; i++) a[i] += b[i]; return a; } template inline D2 operator-=(D2 &a, D2 const & b) { boost::function_requires >(); for(unsigned i = 0; i < 2; i++) a[i] -= b[i]; return a; } //IMPL: ScalableConcept template inline D2 operator-(D2 const & a) { boost::function_requires >(); D2 r; for(unsigned i = 0; i < 2; i++) r[i] = -a[i]; return r; } template inline D2 operator*(D2 const & a, Point const & b) { boost::function_requires >(); D2 r; for(unsigned i = 0; i < 2; i++) r[i] = a[i] * b[i]; return r; } template inline D2 operator/(D2 const & a, Point const & b) { boost::function_requires >(); //TODO: b==0? D2 r; for(unsigned i = 0; i < 2; i++) r[i] = a[i] / b[i]; return r; } template inline D2 operator*=(D2 &a, Point const & b) { boost::function_requires >(); for(unsigned i = 0; i < 2; i++) a[i] *= b[i]; return a; } template inline D2 operator/=(D2 &a, Point const & b) { boost::function_requires >(); //TODO: b==0? for(unsigned i = 0; i < 2; i++) a[i] /= b[i]; return a; } template inline D2 operator*(D2 const & a, double b) { return D2(a[0]*b, a[1]*b); } template inline D2 operator*=(D2 & a, double b) { a[0] *= b; a[1] *= b; return a; } template inline D2 operator/(D2 const & a, double b) { return D2(a[0]/b, a[1]/b); } template inline D2 operator/=(D2 & a, double b) { a[0] /= b; a[1] /= b; return a; } template D2 operator*(D2 const &v, Matrix const &m) { boost::function_requires >(); boost::function_requires >(); D2 ret; for(unsigned i = 0; i < 2; i++) ret[i] = v[X] * m[i] + v[Y] * m[i + 2] + m[i + 4]; return ret; } //IMPL: OffsetableConcept template inline D2 operator+(D2 const & a, Point b) { boost::function_requires >(); D2 r; for(unsigned i = 0; i < 2; i++) r[i] = a[i] + b[i]; return r; } template inline D2 operator-(D2 const & a, Point b) { boost::function_requires >(); D2 r; for(unsigned i = 0; i < 2; i++) r[i] = a[i] - b[i]; return r; } template inline D2 operator+=(D2 & a, Point b) { boost::function_requires >(); for(unsigned i = 0; i < 2; i++) a[i] += b[i]; return a; } template inline D2 operator-=(D2 & a, Point b) { boost::function_requires >(); for(unsigned i = 0; i < 2; i++) a[i] -= b[i]; return a; } template inline T dot(D2 const & a, D2 const & b) { boost::function_requires >(); boost::function_requires >(); T r; for(unsigned i = 0; i < 2; i++) r += a[i] * b[i]; return r; } template inline T cross(D2 const & a, D2 const & b) { boost::function_requires >(); boost::function_requires >(); return a[1] * b[0] - a[0] * b[1]; } //equivalent to cw/ccw, for use in situations where rotation direction doesn't matter. template inline D2 rot90(D2 const & a) { boost::function_requires >(); return D2(-a[Y], a[X]); } //TODO: concepterize the following functions template inline D2 compose(D2 const & a, T const & b) { D2 r; for(unsigned i = 0; i < 2; i++) r[i] = compose(a[i],b); return r; } template inline D2 compose_each(D2 const & a, D2 const & b) { D2 r; for(unsigned i = 0; i < 2; i++) r[i] = compose(a[i],b[i]); return r; } template inline D2 compose_each(T const & a, D2 const & b) { D2 r; for(unsigned i = 0; i < 2; i++) r[i] = compose(a,b[i]); return r; } template inline Point D2::operator()(double t) const { Point p; for(unsigned i = 0; i < 2; i++) p[i] = (*this)[i](t); return p; } //TODO: we might want to have this take a Point as the parameter. template inline Point D2::operator()(double x, double y) const { Point p; for(unsigned i = 0; i < 2; i++) p[i] = (*this)[i](x, y); return p; } template D2 derivative(D2 const & a) { return D2(derivative(a[X]), derivative(a[Y])); } template D2 integral(D2 const & a) { return D2(integral(a[X]), integral(a[Y])); } } //end namespace Geom #include "rect.h" #include "d2-sbasis.h" namespace Geom{ //Some D2 Fragment implementation which requires rect: template Rect bounds_fast(const D2 &a) { boost::function_requires >(); return Rect(bounds_fast(a[X]), bounds_fast(a[Y])); } template Rect bounds_exact(const D2 &a) { boost::function_requires >(); return Rect(bounds_exact(a[X]), bounds_exact(a[Y])); } template Rect bounds_local(const D2 &a, const Interval &t) { boost::function_requires >(); return Rect(bounds_local(a[X], t), bounds_local(a[Y], t)); } }; /* Local Variables: mode:c++ c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) indent-tabs-mode:nil fill-column:99 End: */ // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : #endif