File: te_bezier_curve.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (230 lines) | stat: -rw-r--r-- 6,898 bytes parent folder | download
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
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "tetraedge/te/te_bezier_curve.h"
#include "tetraedge/te/te_mesh.h"
#include "tetraedge/te/te_renderer.h"
#include "tetraedge/tetraedge.h"


namespace Tetraedge {

TeBezierCurve::TeBezierCurve() : _length(0.0), _rawLength(0.0), _lengthNeedsUpdate(true),
_rawLengthNeedsUpdate(true), _numIterations(1000) {
}

//long TeBezierCurve::bounds(int start);

void TeBezierCurve::clear() {
	_lengthNeedsUpdate = true;
	_rawLengthNeedsUpdate = true;
	_length = 0.0;
	_controlPoints.clear();
}

void TeBezierCurve::draw() {
	if (!worldVisible() || _controlPoints.empty())
		return;

	Common::SharedPtr<TeMesh> mesh1(TeMesh::makeInstance());
	Common::SharedPtr<TeMesh> mesh2(TeMesh::makeInstance());
	uint npoints = _controlPoints.size();

	mesh1->setConf(npoints, npoints, TeMesh::MeshMode_Points, 0, 0);
	for (uint i = 0; i < npoints; i++) {
		mesh1->setVertex(i, _controlPoints[i]);
		mesh1->setIndex(i, i);
	}

	mesh2->setConf(npoints, npoints, TeMesh::MeshMode_LineStrip, 0, 0);
	for (uint i = 0; i < npoints; i++) {
		mesh2->setVertex(i, _controlPoints[i]);
		mesh2->setNormal(i, TeVector3f32(0.0f, 1.0f, 0.0));
		mesh2->setIndex(i, i);
	}

	TeRenderer *renderer = g_engine->getRenderer();
	const TeColor prevColor = renderer->currentColor();
	renderer->pushMatrix();
	renderer->multiplyMatrix(worldTransformationMatrix());
	renderer->setCurrentColor(TeColor(0, 0xff, 0xff, 0xff));
	mesh2->draw();
	renderer->setCurrentColor(TeColor(0xff, 0, 0xff, 0xff));
	mesh1->draw();
	renderer->popMatrix();
	renderer->setCurrentColor(prevColor);
}

float TeBezierCurve::length() {
	if (_lengthNeedsUpdate) {
		_length = 0.0;
		_lengthNeedsUpdate = false;
		_lengths.clear();

		TeVector3f32 lastpt = _controlPoints[0];
		lastpt.y() = 0;
		for (uint i = 0; i < _numIterations; i++) {
			float amount = (float)i / _numIterations;
			TeVector3f32 pt = retrievePoint(amount);
			pt.y() = 0;
			float len = (lastpt - pt).length();
			_length += len;
			_lengths.push_back(_length);
			lastpt = pt;
		}
	}
	return _length;
}

void TeBezierCurve::pseudoTangent(float offset, TeVector3f32 &v1, TeVector3f32 &v2) {
	const float step = 1.0f / _numIterations;
	if (step + offset <= 1.0f) {
		v1 = retrievePoint(offset);
		v2 = retrievePoint(offset + step);
	} else {
		v1 = retrievePoint(offset - step);
		v2 = retrievePoint(offset);
	}
}

float TeBezierCurve::rawLength() {
	if (_rawLengthNeedsUpdate) {
		_rawLengthNeedsUpdate = false;
		_rawLength = 0.0;
		_rawLengths.clear();
		_rawLengths.push_back(0.0);
		for (uint i = 1; i < _controlPoints.size(); i++) {
			const TeVector3f32 diff = _controlPoints[i] - _controlPoints[i - 1];
			_rawLength += diff.length();
			_rawLengths.push_back(_rawLength);
		}
	}
	return _rawLength;
}

TeVector3f32 TeBezierCurve::retrievePoint(float offset) {
	const int npoints = _controlPoints.size();

	// Simple cases for small numbers of points.
	if (npoints == 0)
		return TeVector3f32();
	else if (npoints == 1)
		return _controlPoints[0];
	else if (npoints == 2)
		return _controlPoints[0] + (_controlPoints[1] - _controlPoints[0]) * offset;

	// else, there are at least 3 points so need to actually interpolate.
	const float rawlen = rawLength();

	float proportion = 0.0f;
	int startpt = 0;
	while (startpt < npoints) {
		proportion = _rawLengths[startpt] / rawlen;
		if (proportion >= offset)
			break;
		startpt++;
	}

	float t;
	if (proportion == offset) {
		// Exactly on a point
		t = 0.0f;
	} else {
		// Proportion between two points
		float p1 = _rawLengths[startpt - 1];
		float p2 = _rawLengths[startpt];
		t = (rawlen * offset - p1) / (p2 - p1);
		startpt--;
	}

	// Collect 4 points around the startpt (1 before, 2 after)
	TeVector3f32 points[4];
	const int maxPt = _controlPoints.size() - 1;
	for (int p = 0; p < 4; p++) {
		int ptno = CLIP(startpt + p - 1, 0, maxPt);
		points[p] = _controlPoints[ptno];
	}

	// If we hit the end, linearly extend the last gradient.
	if (startpt <= 0)
		points[0] += (points[1] - points[2]);

	if (startpt + 1 >= maxPt)
		points[3] += (points[2] - points[1]);

	return hermiteInterpolate(t, points, 0.0, 0.0);
}

void TeBezierCurve::setControlPoints(const Common::Array<TeVector3f32> &points) {
	_lengthNeedsUpdate = true;
	_rawLengthNeedsUpdate = true;
	_controlPoints = points;
}

void TeBezierCurve::setNbIterations(unsigned long iterations) {
	_lengthNeedsUpdate = true;
	_rawLengthNeedsUpdate = true;
	_numIterations = iterations;
}

/*static*/
void TeBezierCurve::serialize(Common::WriteStream &stream, const TeBezierCurve &curve) {
	error("TODO: Implement TeBezierCurve::serialize");
}

/*static*/
void TeBezierCurve::deserialize(Common::ReadStream &stream, TeBezierCurve &curve) {
	Te3DObject2::deserialize(stream, curve);

	curve._lengthNeedsUpdate = false;
	curve._length = stream.readFloatLE();
	uint32 npoints = stream.readUint32LE();
	if (npoints > 1000000)
		error("TeBezierCurve::deserialize improbable number of control ponts %d", npoints);

	for (uint i = 0; i < npoints; i++) {
		TeVector3f32 vec;
		TeVector3f32::deserialize(stream, vec);
		curve._controlPoints.push_back(vec);
	}
}

/*static*/
TeVector3f32 TeBezierCurve::hermiteInterpolate(float t, const TeVector3f32 *points, float param_4, float param_5) {
	assert(points);
	const TeVector3f32 delta1 =  ((points[1] - points[0]) * (param_5 + 1.0) * (1.0 - param_4)) / 2.0;
	const TeVector3f32 delta2a = ((points[2] - points[1]) * (1.0 - param_5) * (1.0 - param_4)) / 2.0;
	const TeVector3f32 delta2b = ((points[2] - points[1]) * (param_5 + 1.0) * (1.0 - param_4)) / 2.0;
	const TeVector3f32 delta3  = ((points[3] - points[2]) * (1.0 - param_5) * (1.0 - param_4)) / 2.0;

	const TeVector3f32 x1 = delta1 + delta2a;
	const TeVector3f32 x2 = delta2b + delta3;

	const float t2 = t * t;
	const float t3 = t * t * t;
	const TeVector3f32 h1a = points[1] * ((t3 + t3) - t2 * 3.0 + 1.0);
	const TeVector3f32 h1b = x1 * ((t3 - (t2 + t2)) + t);
	const TeVector3f32 h1 = (h1a + h1b) + (x2 * (t3 - t2));
	return h1 + (points[2] * (t3 * -2.0 + t2 * 3.0));
}


} // end namespace Tetraedge