Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

polyline.H

Go to the documentation of this file.
00001 #ifndef POLYLINE_H_IS_INCLUDED
00002 #define POLYLINE_H_IS_INCLUDED
00003 
00004 #include "mlib/points.H"
00005 
00006 template <class L>
00007 inline L
00008 refine_polyline_interp(const L& poly)
00009 {
00010    // Use the 4-point interpolating subdivision rule to generate a
00011    // finer polyline that interpolates the original points. This
00012    // function applies 1 level of subdivision.
00013 
00014    int n = poly.num();
00015    if (n < 2)
00016       return poly;
00017 
00018    // first generate a refined curve using linear subdivision:
00019    L ret(2*n - 1);
00020    for (int i=0; i<n-1; i++) {
00021       ret += poly[i];
00022       ret += interp(poly[i], poly[i+1], 0.5);
00023    }
00024    ret += poly.last();
00025 
00026    if (n == 2)
00027       return ret;
00028 
00029    // correct internal midpoints:
00030    for (int j = 1; j < n-2; j++) {
00031       ret[2*j + 1] = interp(interp(poly[j-1], poly[j+2], 0.5),
00032                             ret[2*j + 1], 9.0/8);
00033    }
00034    // correct first and last midpoints:
00035    ret[1] += (-1.0/8)*(poly[2] - ret[1]).orthogonalized(poly.vec(0));
00036    int m = ret.num()-2;
00037    ret[m] += (-1.0/8)*(poly[n-3] - ret[m]).orthogonalized(poly.vec(n-2));
00038    return ret;
00039 }
00040 
00041 template <class L>
00042 inline L
00043 refine_polyline_interp(const L& poly, int levels)
00044 {
00045    // do repeated 4-point interpolating subdivision
00046 
00047    L ret = poly;
00048    for (int k = 0; k<levels; k++) {
00049       ret = refine_polyline_interp(ret);
00050    }
00051    return ret;
00052 }
00053 
00054 template <class L>
00055 inline L
00056 resample_polyline(const L& poly, int num_samples)
00057 {
00058    // resample the polyline regularly with the given number of samples
00059 
00060    assert(num_samples > 1);
00061    L ret(num_samples);
00062    double dt = 1.0/(num_samples - 1);
00063    for (int i=0; i<num_samples - 1; i++) {
00064       ret += poly.interpolate(i*dt);
00065    }
00066    ret += poly.last();
00067    return ret;
00068 }
00069 
00070 #endif // POLYLINE_H_IS_INCLUDED
00071 
00072 // end of file polyline.H

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4