fit.h

Go to the documentation of this file.
00001 /*
00002  * fit.h
00003  *
00004  * Copyright (C) 2007   Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Functions to fit beziers to a series of points.
00032  */
00033 
00034 #ifndef WAVEPACKET_BEZIER_FIT_H__
00035 #define WAVEPACKET_BEZIER_FIT_H__
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "common/common.h"
00039 
00040 #include "bezier/bezier.h"
00041 #include "bezier/quad.h"
00042 
00043 
00044 /// \ingroup bezier
00045 /*@{*/
00046 
00047 
00048 namespace bezier {
00049 
00050 
00051 ////////////////////////////////////////////////////////////////////////////////
00052 //
00053 //      Quadratic curve fitting
00054 //
00055 ////////////////////////////////////////////////////////////////////////////////
00056 
00057 typedef std::vector<quad_bezier_t> quad_path_t;
00058 
00059 /// given a set of points, return the best fit path of quadratic beziers
00060 void getQuadPathFromRawPoints(IN const point_t * p,
00061                                 IN int nPoints,
00062                                 IN float ds,
00063                                 OUT quad_path_t& path);
00064 
00065 
00066 
00067 ////////////////////////////////////////////////////////////////////////////////
00068 //
00069 //      Cubic curve fitting.
00070 //
00071 ////////////////////////////////////////////////////////////////////////////////
00072 
00073 typedef std::vector<curve_t> path_t;
00074 
00075 typedef path_t cubic_path_t;            // I want to deprecate path_t
00076 
00077 void getCubicPathFromRawPoints(IN const point_t * p,
00078                                 IN int nPoints,
00079                                 IN float ds,
00080                                 OUT cubic_path_t& path);
00081 
00082 
00083 float getCoefficient(IN const float * x, IN int nPoints, IN int degree) throw();
00084 void removeCoefficient(IO float * x, IN int nPoints,
00085                                 IN float coeff, IN int degree) throw();
00086 
00087 struct fit_t {
00088         fit_t(void) throw() { this->clear(); }
00089         void clear(void) throw() {
00090                         q0 = a = b = c = 0.0;
00091                 }
00092 
00093         void dump(const char * title) const throw() {
00094                         DPRINTF("  %s  q0=%5.2f  a=%5.2f  b=%5.2f  c=%5.2f",
00095                             title, q0, a, b, c);
00096                 }
00097 
00098         float getq(IN float t) const throw() {
00099                         float q = q0;
00100                         q += c * t;
00101                         q += b * t * t;
00102                         q += a * t * t * t;
00103                         return q;
00104                 }
00105 
00106         // data fields
00107         float           q0;
00108         float           a;
00109         float           b;
00110         float           c;
00111 };
00112 
00113 
00114 void fitPoints(IN const float * x, IN int nPoints,
00115                                 IN float slope,
00116                                 OUT fit_t& fit) throw();
00117 
00118 int getCuspIndices(IN const point_t * p, IN int nPoints,
00119                                 OUT int ** cusps);
00120 
00121 float getFitQuality(IN const point_t * p, IN int nPoints,
00122                                 IN const curve_t& curve) throw();
00123 
00124 void fitPath(IN const point_t * p, IN int nPoints,
00125                                 IN float tolerance,
00126                                 OUT path_t& path);
00127 
00128 void respacePath(IN const point_t * p, IN int nPoints,
00129                                 IN float ds,            // path step increment
00130                                 OUT point_t ** out_p,   // points to static memory!
00131                                 OUT int& newPoints);
00132 
00133 };      // bezier namespace
00134 
00135 #endif  // WAVEPACKET_BEZIER_FIT_H__
00136