PolyLinePlugin_BezierSpline.cc 5.88 KB
Newer Older
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 42 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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.openflipper.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenFlipper.                                        *
 *                                                                           *
 *  OpenFlipper is free software: you can redistribute it and/or modify      *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenFlipper is distributed in the hope that it will be useful,           *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenFlipper. If not,                                  *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $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;
	if(!PluginFunctions::getObject(_SplineData->MeshIndex_, mesh))
		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;
	OpenMeshTriangleBSPT<TriMesh>::RayCollision rayInt = bsp->raycollision(_point, nor);
	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();
	for(unsigned int i = 0; i < splineData->Handles_.size(); i++) {
		ACG::SceneGraph::GlutPrimitiveNode* node = 0;
		if(_lineObject->getAdditionalNode(node, name(), "handle", i))
			node->set_position(splineData->Handles_[i]);
		const PolyLineBezierSplineData::InterpolatePoint& control = splineData->getInterpolatePoint(i);
		_line->add_line(control.Pos_, splineData->Handles_[i]);
		_line->add_color(ACG::Vec4f(1,0,0,1));
	}
	for(unsigned int i = 0; i < splineData->Points_.size(); i++) {
		ACG::SceneGraph::GlutPrimitiveNode* node = 0;
		if(_lineObject->getAdditionalNode(node, name(), "control", i))
			node->set_position(splineData->Points_[i].Pos_);
	}
}

void
PolyLinePlugin::
updatePolyBezierSpline(PolyLineObject* _lineObject, unsigned int _pointCount)
{
	PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(_lineObject->objectData(BEZSPLINE_DATA));
	if(!splineData)
		return;
	_lineObject->line()->clear();
	int segCount = (splineData->Points_.size() + splineData->Handles_.size() - 1) / 3, segment = 0;
	for(int s = 0; s < segCount; s++) {
		const ACG::Vec3d a = splineData->Points_[s].Pos_, d = splineData->Points_[s + 1].Pos_,
						 b = splineData->Handles_[s * 2], c = splineData->Handles_[s * 2 + 1];
		for(unsigned int i = 0; i < _pointCount; i++) {
			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);
}