PolyLineBezierSplineData.cc 5.47 KB
Newer Older
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Martin Schultz's avatar
Martin Schultz committed
4 5 6 7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
8 9
 *                                                                           *
 *---------------------------------------------------------------------------*
Martin Schultz's avatar
Martin Schultz committed
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
 * 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.              *
39 40 41 42 43
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
44 45 46
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *                                                                           *
\*===========================================================================*/

//=============================================================================
//
//  CLASS PolyLineCircleData - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

#include "PolyLineBezierSplineData.hh"

61
PolyLineBezierSplineData::PolyLineBezierSplineData(unsigned int _meshIndex)
Jan Möbius's avatar
Jan Möbius committed
62 63 64 65 66
: meshIndex_(_meshIndex)
{
}

void PolyLineBezierSplineData::addInterpolatePoint(ACG::Vec3d _position, ACG::Vec3d _normal)
67 68
{
	InterpolatePoint p;
Jan Möbius's avatar
Jan Möbius committed
69 70 71 72
	p.position = _position;
	p.normal   = _normal;

	points_.push_back(p);
73 74 75 76
}

bool PolyLineBezierSplineData::finishSpline()
{
77 78
  if(points_.size() < 2)
      return false;
79

80
  handles_.clear();
Jan Möbius's avatar
Jan Möbius committed
81

82
  for(unsigned int i = 0; i + 1 < points_.size(); i++) {
Jan Möbius's avatar
Jan Möbius committed
83

84 85 86 87 88
    const ACG::Vec3d firstPoint = points_[i].position, sndPoint = points_[i + 1].position;
    double r = (firstPoint - sndPoint).norm() / 4.0;
    const ACG::Vec3d dir = sndPoint - firstPoint;
    const ACG::Vec3d ort0 = dir % points_[i].normal, ort1 = dir % points_[i + 1].normal;
    ACG::Vec3d f0 = ort0 % points_[i].normal, f1 = ort1 % points_[i + 1].normal;
Jan Möbius's avatar
Jan Möbius committed
89

90 91
    ACG::Vec3d near = firstPoint - f0.normalize() * r,
   	       far  = sndPoint   + f1.normalize() * r;
Jan Möbius's avatar
Jan Möbius committed
92

93 94
    handles_.push_back(near);
    handles_.push_back(far);
Jan Möbius's avatar
Jan Möbius committed
95

96
  }
Jan Möbius's avatar
Jan Möbius committed
97

98 99
  //handles will be degenerate up to now
  for(unsigned int i = 1; i + 1 < handles_.size(); i += 2) {
Jan Möbius's avatar
Jan Möbius committed
100

101
    const ACG::Vec3d dir = (handles_[i + 1] - handles_[i]) / 2.0;
Jan Möbius's avatar
Jan Möbius committed
102

103 104 105 106
    InterpolatePoint& p = getInterpolatePoint(i);
    handles_[i + 1]     = p.position + dir;
    handles_[i]         = p.position - dir;
  }
Jan Möbius's avatar
Jan Möbius committed
107

108
  return true;
109 110 111 112 113
}

PolyLineBezierSplineData::InterpolatePoint& PolyLineBezierSplineData::getInterpolatePoint(unsigned int _handleIndex)
{
	if(!_handleIndex)
Jan Möbius's avatar
Jan Möbius committed
114 115 116 117
		return points_[0];
	else if(_handleIndex == handles_.size())
		return points_.back();
	else return points_[(_handleIndex - 1) / 2 + 1];
118
}