问题
NB: I'll present this question in degrees purely for simplicity, radians, degrees, different zero-bearing, the problem is essentially the same.
Does anyone have any ideas on the code behind rotational interpolation? Given a linear interpolation function: Lerp(from, to, amount), where amount is 0...1 which returns a value between from and to, by amount. How could I apply this same function to a rotational interpolation between 0 and 360 degrees? Given that degrees should not be returned outside 0 and 360.
Given this unit circle for degrees:
where from = 45 and to = 315, the algorithm should take the shortest path to the angle, i.e. it should go through zero, to 360 and then to 315 - and not all the way round 90, 180, 270 to 315.
Is there a nice way to achieve this? Or is it going to just be a horrid mess of if() blocks? Am I missing some well understood standard way of doing this? Any help would be appreciated.
回答1:
I know this is 2 years old, but I've recently been looking around for the same problem and I don't see an elegant solution without ifs posted in here, so here it goes:
shortest_angle=((((end - start) % 360) + 540) % 360) - 180;
return start + (shortest_angle * amount) % 360;
that's it
ps: of course, % is meaning modulo and shortest_angle is the variable that holds the whole interpolation angle
回答2:
Sorry, that was a bit convoluted, here's a more concise version:
public static float LerpDegrees(float start, float end, float amount)
{
float difference = Math.Abs(end - start);
if (difference > 180)
{
// We need to add on to one of the values.
if (end > start)
{
// We'll add it on to start...
start += 360;
}
else
{
// Add it on to end.
end += 360;
}
}
// Interpolate it.
float value = (start + ((end - start) * amount));
// Wrap it..
float rangeZero = 360;
if (value >= 0 && value <= 360)
return value;
return (value % rangeZero);
}
Anyone got a more optimised version?
回答3:
I think a better approach is to interpolate sin and cos since they don't suffer form being multiply defined. Let w = "amount" so that w = 0 is angle A and w = 1 is angle B. Then
CS = (1-w)*cos(A) + w*cos(B);
SN = (1-w)*sin(A) + w*sin(B);
C = atan2(SN,CS);
One has to convert to radians and degrees as needed. One also has to adjust the branch. For atan2 C comes back in the range -pi to pi. If you want 0 to 2pi then just add pi to C.
回答4:
NB: using C# code
After some crazy rummaging around in my brain, here's what I've come up with. Basically the premise is to perform the 0-360 wrapping at the last minute. Deal internally with values outside 0-360 and then wrap them inside 0-360 at the point a value is requested from the function.
At the point where you pick a start an an end point, you perform the following:
float difference = Math.Abs(end - start);
if (difference > 180)
{
// We need to add on to one of the values.
if (end > start)
{
// We'll add it on to start...
start += 360;
}
else
{
// Add it on to end.
end += 360;
}
}
This gives you the actual start and end values, which may be outside 0-360...
We have a wrap function to ensure a value is between 0 and 360...
public static float Wrap(float value, float lower, float upper)
{
float rangeZero = upper - lower;
if (value >= lower && value <= upper)
return value;
return (value % rangeZero) + lower;
}
Then at the point you request the current value from the function:
return Wrap(Lerp(start, end, amount), 0, 360);
This is almost certainly not the most optimal solution to the problem, however it does appear to work consistently. If anyone has any more optimal way to do this that would be great.
回答5:
I wanted to rewrite my answer to better explain answer the question. I'm using EXCEL for my formulas, and degrees for my units.
For simplicity, B is the larger of the two values, and A is the smaller of the two values. You can use MAX() and MIN() respectively in your solution later.
PART 1 - WHICH WAY TO GO?
What we want to do first is work out in which direction we want to perform the calculation, clockwise or anticlockwise. We use an IF() Statement for that:
IF( (B-A)<=180, (Clockwise_Formula), (AntiClockwise_Formula) )
The above formula checks if going anticlockwise from B to A (which is the same as going clockwise from A to B) is less than or equal to 180 degrees. If not, it's going to be shorter to go the other direction.
To check this works: 90 - 45 = 45 (which is less than or equal to 180) makes the IF statement TRUE, so the clockwise direction is shorter, but 315 - 45 = 270 (which is larger than 180) makes the if statement FALSE, so the anticlockwise formula would be shorter.
PART 2 - CLOCKWISE FORMULA
Now you want to interpolate N times between A and B, either clockwise or anticlockwise. The clockwise formula is relatively simple.
Clockwise_Formula: ((B-A)/N*S)+A
Where S is a count of the number of interpolations, starting at 1 and finishing at N-1 (If S = N, your answer will be B)
Example: A = 90, B = 270, N = 4
S=1: ((270-90)/4*1)+90 = 135
S=2: ((270-90)/4*2)+90 = 180
S=3: ((270-90)/4*3)+90 = 225
PART 3 - ANITCLOCKWISE FORMULA
The anitclockwise formula is going to be a little more complex, because we'll need to cross anticlockwise over the 360 degree angle. The easiest method I can think of is to add 360 to A, then Modulate the answer by 360 using the MOD(FORMULA,VALUE) function.
You'll also have to swap A and B around in the formula because B is now the smallest number. (That might sound a bit confusing, but it works!)
(Unmodulated) AntiClockwise_Formula: (((A+360)-B)/N*S)+B
Example: A = 60, B = 300, N = 4
S=1: (((60+360)-300)/4*1)+300 = 330
S=2: (((60+360)-300)/4*2)+300 = 360
S=3: (((60+360)-300)/4*3)+300 = 390
PART 4 - RESTRICTING ANSWERS TO BETWEEN 0 AND 360
See how sometimes (but not always) the answers are going to be greater than 360? This is where the wrapping your Anticlockwise_formula in a MOD() function comes in:
AntiClockwise_Formula: MOD((((A+360)-B)/N*S)+B,360)
Modulating the example used in Part 3 will give you:
S=1: 330
S=2: 0
S=3: 30
PART 5 - PUTTING IT ALL TOGETHER
Combining all of the elements from Parts 1-4 together, the answer is:
IF((B-A)<=180,((B-A)/N*S)+A,MOD((((A+360)-B)/N*S)+B,360))
Where:
A = The smaller of the two values (you can replace A with MIN())
B = The larger of the two values (you can replace B with MAX())
N = The number of interpolations you want to do (e.g. 2 is a half, 3 is into thirds etc)
S = An incrimental count to a max of N-1 (see Part 2 for explanation)
回答6:
My preferred way to deal with angle is to use units that are a power of 2 per revolution. For exanple, it you use 16 bit signed integers to represent -180 to +180 degrees, you can simply take (from-to)/num_steps to do your interpolation. Adding and subtracting angles always works, as the binary values overflow right at the point where you go from 360 to 0.
What you probably want to do in your case is math modulo 360. So angle differences are computed as (from-to)%360. There are still some sign issues with that which have been addressed in other SO questions.
回答7:
My solution to slerp of degrees. In my VarTracker class
@classmethod
def shortest_angle(cls, start: float, end: float, amount: float):
""" Find shortest angle change around circle from start to end, the return
fractional part by amount.
VarTracker.shortest_angle(10, 30, 0.1) --> 2.0
VarTracker.shortest_angle(30, 10, 0.1) --> -2.0
VarTracker.shortest_angle(350, 30, 0.1) --> 4.0
VarTracker.shortest_angle(350, 30, 0.8) --> 32.0
VarTracker.shortest_angle(30, 350, 0.5) --> -20.0
VarTracker.shortest_angle(170, 190, 0.1) --> 2.0
VarTracker.shortest_angle(10, 310, 0.5) --> -30.0
"""
sa = ((((end - start) % 360) + 540) % 360) - 180;
return sa * amount;
@classmethod
def slerp(cls, current: float, target: float, amount: float):
""" Return the new value if spherical linear interpolation from current toward target, by amount, all in degrees.
This method uses abs(amount) so sign of amount is ignored.
current and target determine the direction of the lerp.
Wraps around 360 to 0 correctly.
Lerp from 10 degrees toward 30 degrees by 3 degrees
VarTracker.slerp(10, 30, 3.0) --> 13.0
Ignores sign of amount
VarTracker.slerp(10, 30, -3.0) --> 13.0
VarTracker.slerp(30, 10, 3.0) --> 27.0
Wraps around 360 correctly
VarTracker.slerp(350, 30, 6) --> 356.0
VarTracker.slerp(350, 30, 12) --> 2.0
VarTracker.slerp(30, 350, -35) --> 355.0
a = VarTracker.slerp(30, 3140, -35) --> 355.0
VarTracker.slerp(170, 190, 2) --> 172.0
VarTracker.slerp(10, 310, 12) --> 358.0
Wraps over 0 degrees correctly
VarTracker.slerp(-10, 10, 3) --> 353.0
VarTracker.slerp(10, -10, 12) --> 358
"""
a = VarTracker.shortest_angle(current, target, 1.0)
diff = target - current
if np.abs(amount) > np.abs(diff):
amount = diff
if a < 0:
amount = -np.abs(amount)
else:
amount = np.abs(amount)
ret = current + amount
while ret < 0:
ret = ret + 360
ret = ret % 360
return ret
回答8:
Modification of user151496 's answer (the original was in degrees and also giving me a wrong output):
def interp_angle(theta_1, theta_2, ratio):
shortest_angle = ((((theta_2 - theta_1) % (np.pi*2)) + np.pi) % (np.pi*2)) - np.pi
return (theta_1 + shortest_angle * ratio) % (np.pi*2)
Tests: Running with
theta1, theta2 = 0, 0.5
print('Average of {:.4g}pi rad and {:.4g}pi rad = {:.4g}pi rad'.format(theta1, theta2, interp_angle(theta1*np.pi, theta2*np.pi, 0.5)/np.pi))
theta1, theta2 = 0, 0.99
print('Average of {:.4g}pi rad and {:.4g}pi rad = {:.4g}pi rad'.format(theta1, theta2, interp_angle(theta1*np.pi, theta2*np.pi, 0.5)/np.pi))
theta1, theta2 = 0, 1.01
print('Average of {:.4g}pi rad and {:.4g}pi rad = {:.4g}pi rad'.format(theta1, theta2, interp_angle(theta1*np.pi, theta2*np.pi, 0.5)/np.pi))
theta1, theta2 = 0.1, -0.1
print('Average of {:.4g}pi rad and {:.4g}pi rad = {:.4g}pi rad'.format(theta1, theta2, interp_angle(theta1*np.pi, theta2*np.pi, 0.5)/np.pi))
theta1, theta2 = 0.1, 2-0.1
print('Average of {:.4g}pi rad and {:.4g}pi rad = {:.4g}pi rad'.format(theta1, theta2, interp_angle(theta1*np.pi, theta2*np.pi, 0.5)/np.pi))
Gives me:
Average of 0pi rad and 0.5pi rad = 0.25pi rad
Average of 0pi rad and 0.99pi rad = 0.495pi rad
Average of 0pi rad and 1.01pi rad = 1.505pi rad
Average of 0.1pi rad and -0.1pi rad = 0pi rad
Average of 0.1pi rad and 1.9pi rad = 0pi rad
来源:https://stackoverflow.com/questions/2708476/rotation-interpolation