Skip to content

Commit

Permalink
add slerp
Browse files Browse the repository at this point in the history
  • Loading branch information
Aixile committed Jun 19, 2018
1 parent bd76b17 commit ec399cf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/_reducers/generator.reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const initialGeneratorConfigState =
webglDisabled: false,
remoteComputing: false,
transitionCount: 9,
noiseInterpolation: 'POLAR',
noiseInterpolation: 'SLERP',
};

export function generatorConfig(state = initialGeneratorConfigState, action) {
Expand Down
51 changes: 44 additions & 7 deletions src/components/pages/Transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Transition extends Component {
<div style={{width: '100%'}}>
<h5><FormattedMessage id="NoiseInterpolationMethod"/></h5>
<Dropdown
options={['POLAR', 'LINEAR']}
options={['SLERP', 'POLAR', 'LINEAR']}
value={this.props.noiseInterpolation}
onChange={(value) => this.props.dispatch(generatorConfigAction.changeNoiseInterpolation(value))}
/>
Expand Down Expand Up @@ -122,7 +122,7 @@ class Transition extends Component {
}

static interpolateNoise(a, b, value, method) {
if (method==='POLAR'){
if (method === 'POLAR'){
return Math.sqrt(1 - value) * a + Math.sqrt(value) * b;
}
else{
Expand All @@ -131,6 +131,46 @@ class Transition extends Component {

}

static calcVectorNorm(vec) {
let ans = 1e-8;
for (let i = 0; i < vec.length; i++) {
ans += vec[i] * vec[i];
}
return Math.sqrt(ans);
}

static interpolateNoiseVector(startInput, endInput, value, method) {
if (method === 'SLERP') {
let normStart = Transition.calcVectorNorm(startInput.noise);
let normEnd = Transition.calcVectorNorm(endInput.noise);
let length = startInput.noise.length;
let dotsum = 0;
for (let i = 0; i < length; i++) {
dotsum += (startInput.noise[i] / normStart) * (endInput.noise[i] / normEnd);
}
dotsum = dotsum > 1 ? 1 : (dotsum < -1 ? -1 : dotsum);
let omega = Math.acos(dotsum);
let so = Math.sin(omega);
if (Math.abs(so) < 1e-6) {
return Utils.range(length).map(index => Transition.interpolateNoise(
startInput.noise[index],
endInput.noise[index],
value,
'LINEAR'));
} else {
return Utils.range(length).map(index =>
Math.sin((1 - value) * omega) / so * startInput.noise[index] + Math.sin(value * omega) / so * endInput.noise[index]
)
}
} else {
return Utils.range(startInput.noise.length).map(index => Transition.interpolateNoise(
startInput.noise[index],
endInput.noise[index],
value,
method));
}
}

static interpolateLabel(a, b, value) {
return (1 - value) * a + value * b;
}
Expand All @@ -143,11 +183,8 @@ class Transition extends Component {
this.props.setGanState({isRunning: true});
for (var i = 1; i <= this.props.transitionCount; i++) {
var value = i / (this.props.transitionCount + 1);
var noise = Utils.range(this.getModelConfig().gan.noiseLength).map(index => Transition.interpolateNoise(
startInput.noise[index],
endInput.noise[index],
value,
this.props.noiseInterpolation));
var noise = Transition.interpolateNoiseVector(startInput, endInput, value, this.props.noiseInterpolation);

var label = Utils.range(this.getModelConfig().gan.labelLength).map(index => Transition.interpolateLabel(
startInput.label[index],
endInput.label[index],
Expand Down

0 comments on commit ec399cf

Please sign in to comment.