Skip to content

Commit

Permalink
Merge pull request #96 from FMS-Cat/smaller-bezier-nodes
Browse files Browse the repository at this point in the history
BREAKING: smaller bezier nodes
  • Loading branch information
Yutaka "FMS_Cat" Obuchi authored Nov 18, 2020
2 parents b767562 + ab84c5f commit 7b921d5
Show file tree
Hide file tree
Showing 23 changed files with 202 additions and 169 deletions.
2 changes: 1 addition & 1 deletion packages/automaton-fxs/src/tests/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { SerializedAutomaton } from '@fms-cat/automaton';
const defaultData: SerializedAutomaton = {
resolution: 100.0,
curves: [
{ nodes: [ { time: 0.0, value: 0.0 }, { time: 1.0, value: 1.0 } ] },
{ nodes: [ [ 0.0, 0.0 ], [ 1.0, 1.0 ] ] },
],
channels: []
};
Expand Down
2 changes: 1 addition & 1 deletion packages/automaton-fxs/src/tests/repeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { SerializedAutomaton } from '@fms-cat/automaton';
const defaultData: SerializedAutomaton = {
resolution: 100.0,
curves: [
{ nodes: [ { time: 0.0, value: 0.0 }, { time: 1.0, value: 1.0 } ] },
{ nodes: [ [ 0.0, 0.0 ], [ 1.0, 1.0 ] ] },
],
channels: []
};
Expand Down
69 changes: 28 additions & 41 deletions packages/automaton-with-gui/src/CurveWithGUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,8 @@ export class CurveWithGUI extends Curve {
*/
public static readonly DEFAULT_DATA: SerializedCurve = {
nodes: [
{
time: 0.0,
value: 0.0,
out: { time: CURVE_DEFAULT_HANDLE_LENGTH, value: 0.0 }
},
{
time: 1.0,
value: 0.0,
in: { time: -CURVE_DEFAULT_HANDLE_LENGTH, value: 0.0 }
}
[ 0.0, 0.0, 0.0, 0.0, CURVE_DEFAULT_HANDLE_LENGTH, 0.0 ],
[ 1.0, 0.0, -CURVE_DEFAULT_HANDLE_LENGTH, 0.0, 0.0, 0.0 ],
],
fxs: []
};
Expand Down Expand Up @@ -117,12 +109,6 @@ export class CurveWithGUI extends Curve {
* @param data Data of curve
*/
public deserialize( data: SerializedCurve ): void {
data.nodes.forEach( ( node ) => {
// fill missing handles
node.in = node.in ?? { time: 0.0, value: 0.0 };
node.out = node.out ?? { time: 0.0, value: 0.0 };
} );

data.fxs?.forEach( ( fx ) => {
// fill missing params
const defParams = this.__automaton.getFxDefinitionParams( fx.def );
Expand Down Expand Up @@ -274,13 +260,15 @@ export class CurveWithGUI extends Curve {
*/
public createNode( time: number, value: number ): BezierNode & WithID {
const id = genID();
const data = {
$id: id,
const data: any = {
time,
value,
in: { time: -CURVE_DEFAULT_HANDLE_LENGTH, value: 0.0 },
out: { time: CURVE_DEFAULT_HANDLE_LENGTH, value: 0.0 }
inTime: -CURVE_DEFAULT_HANDLE_LENGTH,
inValue: 0.0,
outTime: CURVE_DEFAULT_HANDLE_LENGTH,
outValue: 0.0,
};
data.$id = id;
this.__nodes.push( data );
this.__sortNodes();

Expand Down Expand Up @@ -435,8 +423,7 @@ export class CurveWithGUI extends Curve {

const newTime = ( dir === 'in' ) ? Math.min( 0.0, time ) : Math.max( 0.0, time );

const handle = node[ dir ];
handle.time = newTime;
node[ dir === 'in' ? 'inTime' : 'outTime' ] = newTime;

this.precalc();

Expand All @@ -458,8 +445,7 @@ export class CurveWithGUI extends Curve {

const node = this.__nodes[ index ];

const handle = node[ dir ];
handle.value = value;
node[ dir === 'in' ? 'inValue' : 'outValue' ] = value;

this.precalc();

Expand All @@ -479,10 +465,8 @@ export class CurveWithGUI extends Curve {
if ( index === 0 && dir === 'in' ) { return; }

const node = this.__nodes[ index ];
node[ dir ] = {
time: ( ( dir === 'in' ) ? -1.0 : 1.0 ) * CURVE_DEFAULT_HANDLE_LENGTH,
value: 0.0
};
node[ dir === 'in' ? 'inTime' : 'outTime' ] = ( ( dir === 'in' ) ? -1.0 : 1.0 ) * CURVE_DEFAULT_HANDLE_LENGTH;
node[ dir === 'in' ? 'inValue' : 'outValue' ] = 0.0;

this.precalc();

Expand Down Expand Up @@ -814,20 +798,23 @@ export class CurveWithGUI extends Curve {
*/
private __serializeNodes(): SerializedBezierNode[] {
return this.__nodes.map( ( node ) => {
const data: SerializedBezierNode = {};
if ( node.time !== 0.0 ) {
data.time = node.time;
}
if ( node.value !== 0.0 ) {
data.value = node.value;
}
if ( node.in.time !== 0.0 || node.in.value !== 0.0 ) {
data.in = jsonCopy( node.in );
}
if ( node.out.time !== 0.0 || node.out.value !== 0.0 ) {
data.out = jsonCopy( node.out );
const { time, value, inTime, inValue, outTime, outValue } = node;

if ( outValue !== 0.0 ) {
return [ time, value, inTime, inValue, outTime, outValue ];
} else if ( outTime !== 0.0 ) {
return [ time, value, inTime, inValue, outTime ];
} else if ( inValue !== 0.0 ) {
return [ time, value, inTime, inValue ];
} else if ( inTime !== 0.0 ) {
return [ time, value, inTime ];
} else if ( value !== 0.0 ) {
return [ time, value ];
} else if ( time !== 0.0 ) {
return [ time ];
} else {
return [];
}
return data;
} );
}

Expand Down
7 changes: 2 additions & 5 deletions packages/automaton-with-gui/src/compat/compat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SerializedAutomatonWithGUI, defaultDataWithGUI } from '../types/SerializedAutomatonWithGUI';
import { jsonCopy } from '../utils/jsonCopy';
import { preversionCompat } from './preversionCompat';
import { v1Compat } from './v1Compat';
import { v2Compat } from './v2Compat';
import { v3Compat } from './v3Compat';
Expand All @@ -21,10 +20,8 @@ export function compat( data?: any ): SerializedAutomatonWithGUI {
let version = parseFloat( newData.version ) || parseFloat( newData.v );

if ( !version && !newData.rev ) {
newData = preversionCompat( newData );
if ( newData === null ) {
return jsonCopy( defaultDataWithGUI );
}
// assuming it's latest
return newData;
}

if ( newData.rev ) { // fuck
Expand Down
9 changes: 0 additions & 9 deletions packages/automaton-with-gui/src/compat/preversionCompat.ts

This file was deleted.

7 changes: 4 additions & 3 deletions packages/automaton-with-gui/src/compat/v2Compat.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { defaultGUISettings } from '../types/GUISettings';
import { jsonCopy } from '../utils/jsonCopy';
import type { FxSection, SerializedCurve } from '@fms-cat/automaton';
import type { FxSection } from '@fms-cat/automaton';
import type { V2FxSection } from './v2types/V2FxSection';
import type { V2SerializedData } from './v2types/V2SerializedData';
import type { V3SerializedAutomatonWithGUI } from './v3types/V3SerializedAutomatonWithGUI';
import type { V3SerializedChannel } from './v3types/V3SerializedChannel';
import type { V3SerializedCurve } from './v3types/V3SerializedCurve';

export function v2Compat( data: V2SerializedData ): V3SerializedAutomatonWithGUI {
const curves: SerializedCurve[] = [];
const curves: V3SerializedCurve[] = [];
const channels: { [ name: string ]: V3SerializedChannel } = {};

Object.entries( data.params ).forEach( ( [ name, param ] ) => {
const curve: SerializedCurve = {
const curve: V3SerializedCurve = {
nodes: param.nodes,
fxs: param.fxs.map( ( fx: V2FxSection ) => {
const newFx: FxSection = {
Expand Down
20 changes: 18 additions & 2 deletions packages/automaton-with-gui/src/compat/v3Compat.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import { SerializedChannel } from '@fms-cat/automaton';
import { defaultGUISettings } from '../types/GUISettings';
import type { SerializedAutomatonWithGUI } from '../types/SerializedAutomatonWithGUI';
import type { SerializedBezierNode, SerializedChannel, SerializedCurve } from '@fms-cat/automaton';
import type { V3SerializedAutomatonWithGUI } from './v3types/V3SerializedAutomatonWithGUI';

export function v3Compat( data: V3SerializedAutomatonWithGUI ): SerializedAutomatonWithGUI {
const channels: [ name: string, channel: SerializedChannel ][] = [];

const curves: SerializedCurve[] = data.curves.map( ( curve ) => {
const nodes = curve.nodes.map( ( node ) => [
node.time,
node.value,
node.in?.time,
node.in?.value,
node.out?.time,
node.out?.value,
] as SerializedBezierNode );

return {
nodes,
fxs: curve.fxs
};
} );

Object.entries( data.channels ).forEach( ( [ name, channel ] ) => {
channels.push( [ name, channel ] );
} );

const newData: SerializedAutomatonWithGUI = {
version: data.version,
resolution: data.resolution,
curves: data.curves,
curves,
channels,
labels: data.labels,
guiSettings: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SerializedCurve } from '@fms-cat/automaton';
import type { V3SerializedChannel } from './V3SerializedChannel';
import type { V3SerializedCurve } from './V3SerializedCurve';

/**
* Interface of serialized automaton data.
Expand All @@ -13,7 +13,7 @@ export interface V3SerializedAutomaton {
/**
* Curves of the automaton.
*/
curves: SerializedCurve[];
curves: V3SerializedCurve[];

/**
* Channels of the automaton.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Serialized variant of {@link BezierControlPoint}.
* Some values are optional.
*/
export interface SerializedBezierControlPoint {
export interface V3SerializedBezierControlPoint {
/**
* Time of the control point.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { V3SerializedBezierControlPoint } from './V3SerializedBezierControlPoint';

/**
* Serialized variant of {@link BezierNode}.
* Some values are optional.
*/
export interface V3SerializedBezierNode {
/**
* Time of the node.
* `0.0` by default.
*/
time?: number;

/**
* Value of the node.
* `0.0` by default.
*/
value?: number;

/**
* Bezier control point of inlet.
* `{ time: 0.0, value: 0.0 }` by default.
*/
in?: V3SerializedBezierControlPoint;

/**
* Bezier control point of outlet.
* `{ time: 0.0, value: 0.0 }` by default.
*/
out?: V3SerializedBezierControlPoint;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { SerializedFxSection } from '@fms-cat/automaton';
import type { V3SerializedBezierNode } from './V3SerializedBezierNode';

/**
* Interface of a serialized curve.
*/
export interface V3SerializedCurve {
/**
* Bezier nodes of the curve.
*/
nodes: V3SerializedBezierNode[];

/**
* Fx sections of the curve.
*/
fxs?: SerializedFxSection[];
}
27 changes: 10 additions & 17 deletions packages/automaton-with-gui/src/minimizeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,16 @@ function minimizeNode(
data: SerializedBezierNode,
options: MinimizeOptions
): SerializedBezierNode {
const time = precOrUndefined( data.time, options.precisionTime );
const value = precOrUndefined( data.value, options.precisionValue );
const inPoint = data.in ? {
time: prec( data.in.time, options.precisionTime ),
value: prec( data.in.value, options.precisionValue )
} : undefined;
const outPoint = data.out ? {
time: prec( data.out.time, options.precisionTime ),
value: prec( data.out.value, options.precisionValue )
} : undefined;

return {
time,
value,
in: inPoint,
out: outPoint
};
return [
precOrUndefined( data[ 0 ], options.precisionTime ),
precOrUndefined( data[ 1 ], options.precisionValue ),
precOrUndefined( data[ 2 ], options.precisionTime ),
precOrUndefined( data[ 3 ], options.precisionValue ),
precOrUndefined( data[ 4 ], options.precisionTime ),
precOrUndefined( data[ 5 ], options.precisionValue ),
].filter( ( element ) => {
return element != null;
} ) as SerializedBezierNode;
}

function minimizeFx(
Expand Down
Loading

0 comments on commit 7b921d5

Please sign in to comment.