PolyLinePlugin_BezierSpline.cc 6.68 KB
Newer Older
1 2 3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
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
 *                                                                           *
 *---------------------------------------------------------------------------*
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision: 17392 $                                                       *
 *   $Author: moebius $                                                      *
 *   $Date: 2013-08-26 15:03:53 +0200 (Mon, 26 Aug 2013) $                   *
 *                                                                           *
\*===========================================================================*/

//=============================================================================
//
//  CLASS PolyLinePlugin - IMPLEMENTATION
//
//=============================================================================


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

#include "PolyLinePlugin.hh"

ACG::Vec3d
PolyLinePlugin::
getPointOnMesh(PolyLineBezierSplineData* _SplineData, ACG::Vec3d _point, ACG::Vec3d* _nor)
{
	TriMeshObject* mesh;
66
	if(!PluginFunctions::getObject(_SplineData->meshIndex_, mesh))
67 68 69 70 71 72
		return _point;
	OpenMeshTriangleBSPT<TriMesh>* bsp = mesh->requestTriangleBsp();
	OpenMeshTriangleBSPT<TriMesh>::NearestNeighbor neigh = bsp->nearest(_point);
	ACG::Vec3d nor = mesh->mesh()->normal(neigh.handle);
	if(_nor)
		*_nor = nor;
Martin Schultz's avatar
Martin Schultz committed
73 74
    OpenMeshTriangleBSPT<TriMesh>::RayCollision rayInt;
	rayInt = bsp->raycollision(_point, nor);
75 76 77 78 79 80 81 82 83 84 85 86 87
	if(rayInt.size())
		return _point + nor * rayInt[0].second;
	return _point + nor.normalize() * neigh.dist;
}

void
PolyLinePlugin::
updatePolyBezierHandles(PolyLineObject* _lineObject, ACG::SceneGraph::LineNode* _line)
{
	PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(_lineObject->objectData(BEZSPLINE_DATA));
	if(!splineData)
		return;
	_line->clear();
Jan Möbius's avatar
Jan Möbius committed
88
	for(unsigned int i = 0; i < splineData->handles_.size(); i++) {
89 90
		ACG::SceneGraph::GlutPrimitiveNode* node = 0;
		if(_lineObject->getAdditionalNode(node, name(), "handle", i))
Jan Möbius's avatar
Jan Möbius committed
91
			node->set_position(splineData->handles_[i]);
92
		const PolyLineBezierSplineData::InterpolatePoint& control = splineData->getInterpolatePoint(i);
Jan Möbius's avatar
Jan Möbius committed
93
		_line->add_line(control.position, splineData->handles_[i]);
94 95
		_line->add_color(ACG::Vec4f(1,0,0,1));
	}
Jan Möbius's avatar
Jan Möbius committed
96
	for(unsigned int i = 0; i < splineData->points_.size(); i++) {
97 98
		ACG::SceneGraph::GlutPrimitiveNode* node = 0;
		if(_lineObject->getAdditionalNode(node, name(), "control", i))
Jan Möbius's avatar
Jan Möbius committed
99
			node->set_position(splineData->points_[i].position);
100 101 102 103 104 105 106 107
	}
}

void
PolyLinePlugin::
updatePolyBezierSpline(PolyLineObject* _lineObject, unsigned int _pointCount)
{
	PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(_lineObject->objectData(BEZSPLINE_DATA));
108
	TriMeshObject* mesh;
109
	if(!splineData || !PluginFunctions::getObject(splineData->meshIndex_, mesh)) {
110
		return;//no mesh -> do nothing
111
	}
112
	_lineObject->line()->clear();
113 114
	unsigned int segCount = (splineData->points_.size() + splineData->handles_.size() - 1) / 3;
	for(unsigned int s = 0; s < segCount; s++) {
Jan Möbius's avatar
Jan Möbius committed
115 116
		const ACG::Vec3d a = splineData->points_[s].position, d = splineData->points_[s + 1].position,
						 b = splineData->handles_[s * 2], c = splineData->handles_[s * 2 + 1];
117 118 119
		//the last segment will get one more point to close the spline
		unsigned int n = (s != segCount - 1) ? _pointCount : (_pointCount + 1);
		for(unsigned int i = 0; i < n; i++) {
120 121 122 123 124 125 126 127 128
			float alpha = float(i) / float(_pointCount);
			const ACG::Vec3d e = a + (b - a) * alpha, f = c + (d - c) * alpha;
			ACG::Vec3d g = e + (f - e) * alpha;
			g = getPointOnMesh(splineData, g);
			_lineObject->line()->add_point(g);
		}
	}
	emit updatedObject(_lineObject->id(), UPDATE_GEOMETRY | UPDATE_TOPOLOGY);
}