Repository: godotengine/godot
File: scene/resources/curve.cpp (C++)

Code before:

	int idx = p_interval.idx;
	real_t frac = p_interval.frac;

	Vector3 forward_begin = baked_forward_vector_cache[idx];
	Vector3 forward_end = baked_forward_vector_cache[idx + 1];

	Vector3 up_begin;
	Vector3 up_end;
	if (up_vector_enabled) {
		up_begin = baked_up_vector_cache[idx];
		up_end = baked_up_vector_cache[idx + 1];
	} else {
		up_begin = Vector3(0.0, 1.0, 0.0);
		up_end = Vector3(0.0, 1.0, 0.0);
	}

	// Build frames at both ends of the interval, then interpolate.
	const Basis frame_begin = Basis::looking_at(forward_begin, up_begin);
	const Basis frame_end = Basis::looking_at(forward_end, up_end);
	const Basis frame = frame_begin.slerp(frame_end, frac).orthonormalized();

	if (!p_apply_tilt) {
		return frame;
	}

	// Applying tilt.
	const real_t tilt = _sample_baked_tilt(p_interval);
	Vector3 forward = frame.get_column(2);

	const Basis twist(forward, tilt);
	return twist * frame;
}

Vector3 Curve3D::sample_baked(real_t p_offset, bool p_cubic) const {
	if (baked_cache_dirty) {
		_bake();
	}

	// Validate: Curve may not have baked points.
	int pc = baked_point_cache.size();
	ERR_FAIL_COND_V_MSG(pc == 0, Vector3(), "No points in Curve3D.");

	if (pc == 1) {
		return baked_point_cache[0];
	}

	p_offset = CLAMP(p_offset, 0.0, get_baked_length()); // PathFollower implement wrapping logic.

	Curve3D::Interval interval = _find_interval(p_offset);
	return _sample_baked(interval, p_cubic);

Review comment (refactor):
This method looks like it could be implemented as a case by optional argument of `_sample_posture()`. For example, make change it to `Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt, bool p_simplify = false)` then if `p_simplify` is true, omit some steps to return this result.

Code after:
	ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_tilt_cache.size(), 0.0, "Invalid interval");

	int idx = p_interval.idx;
	real_t frac = p_interval.frac;

	const real_t *r = baked_tilt_cache.ptr();

	return Math::lerp(r[idx], r[idx + 1], frac);
}

// Internal method for getting posture at a baked point. Assuming caller
// make all sanity checks.
Basis Curve3D::_compose_posture(int p_index) const {
	Vector3 forward = baked_forward_vector_cache[p_index];

	Vector3 up;
	if (up_vector_enabled) {
		up = baked_up_vector_cache[p_index];
	} else {
		up = Vector3(0.0, 1.0, 0.0);
	}

	const Basis frame = Basis::looking_at(forward, up);
	return frame;
}

Basis Curve3D::_sample_posture(Interval p_interval, bool p_apply_tilt) const {
	// Assuming that p_interval is valid.
	ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_point_cache.size(), Basis(), "Invalid interval");
	if (up_vector_enabled) {
		ERR_FAIL_INDEX_V_MSG(p_interval.idx, baked_up_vector_cache.size(), Basis(), "Invalid interval");
	}

	int idx = p_interval.idx;
	real_t frac = p_interval.frac;

	// Get frames at both ends of the interval, then interpolate.
	const Basis frame_begin = _compose_posture(idx);
	const Basis frame_end = _compose_posture(idx + 1);
	const Basis frame = frame_begin.slerp(frame_end, frac).orthonormalized();

	if (!p_apply_tilt) {
		return frame;
	}

	// Applying tilt.
	const real_t tilt = _sample_baked_tilt(p_interval);
	Vector3 tangent = -frame.get_column(2);

	const Basis twist(tangent, tilt);
	return twist * frame;