BSplineCurveT.hh 10.8 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42

Jan Möbius's avatar
Jan Möbius committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324


//=============================================================================
//
//  CLASS BSplineCurveT
//  Author:  Ellen Dekkers <dekkers@cs.rwth-aachen.de>
//
//=============================================================================


#ifndef BSPLINECURVET_HH
#define BSPLINECURVET_HH


//== INCLUDES =================================================================
#include <vector>
#include <iostream>

#include <ACG/Math/VectorT.hh>

#include <ObjectTypes/Knotvector/KnotvectorT.hh>

//== FORWARDDECLARATIONS ======================================================

//== NAMESPACES ===============================================================

namespace ACG {

//== CLASS DEFINITION =========================================================


/** \class BSplineCurveT BSplineCurveT.hh <ACG/.../BSplineCurveT.hh>

    Brief Description.

    A more elaborate description follows.
*/

template <class PointT>
class BSplineCurveT
{
public:

  // internal relevant Types
  typedef PointT                      Point;
  typedef typename Point::value_type  Scalar;

  /// Constructor
  BSplineCurveT(unsigned int _degree = 3);

  /// Copy Constructor
  BSplineCurveT(const BSplineCurveT& _curve);

  /// Destructor
  ~BSplineCurveT() {}

  /// get the knotvector of the bspline curve
  std::vector< Scalar >& get_knots() {return knotvector_.getKnotvector();};

  /// set the knotvector of the bspline curve
  void set_knots(std::vector< Scalar > _knots);

  /// get a reference to the knotvector
  Knotvector * get_knotvector_ref(){return &knotvector_;};

  /// set type of knotvector
  void set_knotvector_type(Knotvector::KnotvectorType _type) {knotvector_.setType(_type);};

  /// get knot i
  Scalar get_knot(int _i) {return knotvector_(_i);};

  /// get control point i
  Point& get_control_point(int _i) {return control_polygon_[_i];};

  /// add a control point
  void add_control_point(const Point& _cp);

  /// insert a control point at given index
  void insert_control_point(int _idx, const Point& _cp);

  /// delete control point at given index
  void delete_control_point(int _idx);

  /// reset a control point
  void set_control_point(int _idx, const Point& _cp);

  /// set whole control polygon
  void set_control_polygon(std::vector< Point> & _control_polygon);

  /// Clears the control polygon
  void reset_control_polygon();

  /// print information string
  void print() const;

  /**
   * Evaluates a spline curve at parameter _u
   * \param _u the parameter
   * \return the curve point
   */
  Point curvePoint( Scalar _u );

  /**
   * Returns the _derm'th derivative of a spline curve
   * \param _u the parameter
   * \param _der the _derm'th derivative
   * \return the derivative
   */
  Point derivativeCurvePoint(Scalar _u, unsigned int _der);

  /**
   * Evaluates the curve at parameter u using deBoor algorithm.
   * \param _u the parameter
   * \return triangular array of control points
   */
  std::vector<Point> deBoorAlgorithm(double _u);

  /// Inserts a new knot at parameter u
  void insertKnot(double _u);

  /// Returns the number of control points
  unsigned int n_control_points() const {return control_polygon_.size();};

  /// Returns the number of knots
  unsigned int n_knots() const { return knotvector_.size(); };

  /// Returns the spline degree
  unsigned int degree() const {return degree_;};

  /// Sets the spline degree
  void set_degree(unsigned int _degree) {degree_ = _degree;};

  /**
   * returns the basis functions which are unequal to zero at parameter t
   * \param _t the parameter
   */
  ACG::Vec2i span(double _t);

  /**
   * returns the index of the knots u and u+1 such that t in [u, u+1)
   * \param _t the parameter
   */
  ACG::Vec2i interval(double _t);
  
  void autocompute_knotvector(bool _auto) {autocompute_knotvector_ = _auto;};

  void fixNumberOfControlPoints(bool _fix) {fix_number_control_points_ = _fix;};

  /** \brief projected
   *
   * \todo This is a hack actually. review
   */
  bool projected() {return fix_number_control_points_;};

  /// Reverses the curve
  void reverse();


public:

  /**
   * A Spline Basis Function
   * \param _i the i'th basis function
   * \param _n the spline degree
   * \param _t the parameter
   * Returns N_i^p(_u)
   */
  Scalar basisFunction(int _i, int _n, Scalar _t);

  /**
   * Derivative of a Spline Basis Function
   * \param _i the i'th basis function
   * \param _n the spline degree
   * \param _t the parameter
   * \param _der the _der'th derivative
   */
  Scalar derivativeBasisFunction(int _i, int _n, Scalar _t, int _der);


public:

  /// Returns the lower parameter
  Scalar lower() const;

  /// Returns the upper parameter
  Scalar upper() const;


  // request properties
  void request_controlpoint_selections() { request_prop( ref_count_cpselections_, cpselections_);}
  void request_edge_selections()   { request_prop( ref_count_eselections_, eselections_);}

  // release properties
  void release_controlpoint_selections() { release_prop( ref_count_cpselections_, cpselections_);}
  void release_edge_selections()   { release_prop( ref_count_eselections_, eselections_);}

  // property availability
  bool controlpoint_selections_available() const {return bool(ref_count_cpselections_);}
  bool edge_selections_available()   const {return bool(ref_count_eselections_);}


  // property access ( no range or availability check! )
  unsigned char& controlpoint_selection(unsigned int _i){
    assert(_i < n_control_points());
    assert(controlpoint_selections_available());
    return cpselections_[_i];
  }
  const unsigned char& controlpoint_selection(unsigned int _i) const {
    assert(_i < n_control_points());
    assert(controlpoint_selections_available());
    return cpselections_[_i];
  }

  unsigned char& edge_selection(unsigned int _i) {
    assert(edge_selections_available());
    return eselections_[_i];
  }
  const unsigned char& edge_selection(unsigned int _i) const {
    assert(edge_selections_available());
    return eselections_[_i];
  }
  
  // Wrapper for selection functions
  void select_controlpoint(unsigned int _pIdx) { controlpoint_selection(_pIdx) = 1; };
  void deselect_controlpoint(unsigned int _pIdx) { controlpoint_selection(_pIdx) = 0; };
  
  bool controlpoint_selected(unsigned int _pIdx) const { return (controlpoint_selection(_pIdx) == 1); };
  
  void select_edge(unsigned int _pIdx) { edge_selection(_pIdx) = 1; };
  void deselect_edge(unsigned int _pIdx) { edge_selection(_pIdx) = 0; };
  
  bool edge_selected(unsigned int _pIdx) const { return (edge_selection(_pIdx) == 1); };

private:

  template <class PropT>
  void request_prop( unsigned int& _ref_count, PropT& _prop);

  template <class PropT>
  void release_prop( unsigned int& _ref_count, PropT& _prop);


private: // private objects

  std::vector<Point> control_polygon_;

  Knotvector knotvector_;

  unsigned int degree_;

  bool autocompute_knotvector_;

  bool fix_number_control_points_;

private: // private properties

  // ############################### Standard Property Handling #############################

  // list of vertex properties
  std::vector<unsigned char>   cpselections_;

  // list of edge properties
  std::vector<unsigned char>   eselections_;

  // property reference counter
  unsigned int ref_count_cpselections_;
  unsigned int ref_count_eselections_;

};


//=============================================================================
} // namespace ACG
//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(BSPLINECURVE_BSPLINECURVET_C)
#define BSPLINECURVE_BSPLINECURVET_TEMPLATES
#include "BSplineCurveT.cc"
#endif
//=============================================================================
#endif // ACG_BSPLINECURVET_HH defined
//=============================================================================