/* * d2.cpp - 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. * */ #include "d2.h" namespace Geom { SBasis L2(D2 const & a, unsigned k) { return sqrt(dot(a, a), k); } double L2(D2 const & a) { return hypot(a[0], a[1]); } D2 multiply(Linear const & a, D2 const & b) { return D2(multiply(a, b[X]), multiply(a, b[Y])); } D2 multiply(SBasis const & a, D2 const & b) { return D2(multiply(a, b[X]), multiply(a, b[Y])); } D2 truncate(D2 const & a, unsigned terms) { return D2(truncate(a[X], terms), truncate(a[Y], terms)); } unsigned sbasis_size(D2 const & a) { return std::max((unsigned) a[0].size(), (unsigned) a[1].size()); } //TODO: Is this sensical? shouldn't it be like pythagorean or something? double tail_error(D2 const & a, unsigned tail) { return std::max(a[0].tailError(tail), a[1].tailError(tail)); } Piecewise > sectionize(D2 > const &a) { Piecewise x = partition(a[0], a[1].cuts), y = partition(a[1], a[0].cuts); assert(x.size() == y.size()); Piecewise > ret; for(unsigned i = 0; i < x.size(); i++) ret.push_seg(D2(x[i], y[i])); ret.cuts.insert(ret.cuts.end(), x.cuts.begin(), x.cuts.end()); return ret; } D2 > make_cuts_independant(Piecewise > const &a) { D2 > ret; for(unsigned d = 0; d < 2; d++) { for(unsigned i = 0; i < a.size(); i++) ret[d].push_seg(a[i][d]); ret[d].cuts.insert(ret[d].cuts.end(), a.cuts.begin(), a.cuts.end()); } return ret; } Piecewise > rot90(Piecewise > const &M){ Piecewise > result; if (M.empty()) return M; result.push_cut(M.cuts[0]); for (unsigned i=0; i dot(Piecewise > const &a, Piecewise > const &b){ Piecewise result; if (a.empty() || b.empty()) return result; Piecewise > aa = partition(a,b.cuts); Piecewise > bb = partition(b,a.cuts); result.push_cut(aa.cuts.front()); for (unsigned i=0; i cross(Piecewise > const &a, Piecewise > const &b){ Piecewise result; if (a.empty() || b.empty()) return result; Piecewise > aa = partition(a,b.cuts); Piecewise > bb = partition(b,a.cuts); result.push_cut(aa.cuts.front()); for (unsigned i=0; i > remove_short_cuts(Piecewise > const &f, double tol){ double min = tol; unsigned idx = f.size(); for(unsigned i=0; i f.cuts[i+1]-f.cuts[i]){ min = f.cuts[i+1]-f.cuts[i]; idx = int(i); } } if (idx==f.size()){ return f; } if (f.size()==1) { //removing this seg would result in an empty pw>... return f; } Piecewise > new_f=f; for (int dim=0; dim<2; dim++){ double v = Hat(f.segs.at(idx)[dim][0]); //TODO: what about closed curves? if (idx>0 && f.segs.at(idx-1).at1()==f.segs.at(idx).at0()) new_f.segs.at(idx-1)[dim][0][1] = v; if (idx0, only force continuity where the jump is smaller than tol. Piecewise > force_continuity(Piecewise > const &f, double tol, bool closed){ if (f.size()==0) return f; Piecewise > result=f; unsigned cur = (closed)? 0:1; unsigned prev = (closed)? f.size()-1:0; while(cur