PolyLinePlugin_BezierSpline.cc 5.89 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
/*===========================================================================*\
 *                                                                           *
 *                              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;
Jan Möbius's avatar
Jan Möbius committed
59
	if(!PluginFunctions::getObject(_SplineData->meshIndex(), mesh))
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		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();
Jan Möbius's avatar
Jan Möbius committed
80
	for(unsigned int i = 0; i < splineData->handles_.size(); i++) {
81
82
		ACG::SceneGraph::GlutPrimitiveNode* node = 0;
		if(_lineObject->getAdditionalNode(node, name(), "handle", i))
Jan Möbius's avatar
Jan Möbius committed
83
			node->set_position(splineData->handles_[i]);
84
		const PolyLineBezierSplineData::InterpolatePoint& control = splineData->getInterpolatePoint(i);
Jan Möbius's avatar
Jan Möbius committed
85
		_line->add_line(control.position, splineData->handles_[i]);
86
87
		_line->add_color(ACG::Vec4f(1,0,0,1));
	}
Jan Möbius's avatar
Jan Möbius committed
88
	for(unsigned int i = 0; i < splineData->points_.size(); i++) {
89
90
		ACG::SceneGraph::GlutPrimitiveNode* node = 0;
		if(_lineObject->getAdditionalNode(node, name(), "control", i))
Jan Möbius's avatar
Jan Möbius committed
91
			node->set_position(splineData->points_[i].position);
92
93
94
95
96
97
98
99
100
101
102
	}
}

void
PolyLinePlugin::
updatePolyBezierSpline(PolyLineObject* _lineObject, unsigned int _pointCount)
{
	PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(_lineObject->objectData(BEZSPLINE_DATA));
	if(!splineData)
		return;
	_lineObject->line()->clear();
Jan Möbius's avatar
Jan Möbius committed
103
	int segCount = (splineData->points_.size() + splineData->handles_.size() - 1) / 3, segment = 0;
104
	for(int s = 0; s < segCount; s++) {
Jan Möbius's avatar
Jan Möbius committed
105
106
		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];
107
108
109
110
111
112
113
114
115
116
		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);
}