Skip to content

Commit

Permalink
Optimize (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrinktofit committed Oct 8, 2021
1 parent 397a0f5 commit dbc8934
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
11 changes: 7 additions & 4 deletions cocos/core/animation/kinematics/ccd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export function ccdIK(

const iEndFactor = links.length - 1;
const endFactor = links[iEndFactor];
for (let iteration = 0; iteration < maxIterations; ++iteration) {
// Won't run in infinite loop since we have `nLinks >= 2`
if (forward) {
if (forward) {
for (let iteration = 0; iteration < maxIterations; ++iteration) {
// Won't run in infinite loop since we have `nLinks >= 2`
for (let iLink = 0; iLink < iEndFactor; ++iLink) {
const result = correct(iLink);
if (result === IterationResult.INTERRUPTED) {
Expand All @@ -54,7 +54,10 @@ export function ccdIK(
return;
}
}
} else {
}
} else {
for (let iteration = 0; iteration < maxIterations; ++iteration) {
// Won't run in infinite loop since we have `nLinks >= 2`
for (let iLink = iEndFactor - 1; iLink >= 0; --iLink) {
const result = correct(iLink);
if (result === IterationResult.INTERRUPTED) {
Expand Down
47 changes: 26 additions & 21 deletions cocos/core/animation/marionette/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class BinaryCondition implements Condition {
evaluation.setLhs,
evaluation,
);
const rhsValue = bindOr(
const rhsValue = bindNumericOr(
context,
rhs,
VariableType.NUMBER,
Expand All @@ -81,27 +81,30 @@ export declare namespace BinaryCondition {

class BinaryConditionEval implements ConditionEval {
private declare _operator: BinaryOperator;
private declare _operands: [Value, Value | undefined];
private declare _lhs: number;
private declare _rhs: number;
private declare _result: boolean;

constructor (operator: BinaryOperator, lhs: Value, rhs?: Value) {
constructor (operator: BinaryOperator, lhs: number, rhs: number) {
this._operator = operator;
this._operands = [lhs, rhs];
this._lhs = lhs;
this._rhs = rhs;
this._eval();
}

public reset (lhs: Value, rhs?: Value) {
this._operands = [lhs, rhs];
public reset (lhs: number, rhs: number) {
this._lhs = lhs;
this._rhs = rhs;
this._eval();
}

public setLhs (value: Value) {
this._operands[0] = value;
public setLhs (value: number) {
this._lhs = value;
this._eval();
}

public setRhs (value: Value) {
this._operands[1] = value;
public setRhs (value: number) {
this._rhs = value;
this._eval();
}

Expand All @@ -113,8 +116,10 @@ class BinaryConditionEval implements ConditionEval {
}

private _eval () {
// TODO: rhs assertion?
const [lhs, rhs] = this._operands;
const {
_lhs: lhs,
_rhs: rhs,
} = this;
switch (this._operator) {
default:
case BinaryOperator.EQUAL_TO:
Expand All @@ -124,16 +129,16 @@ class BinaryConditionEval implements ConditionEval {
this._result = lhs !== rhs;
break;
case BinaryOperator.LESS_THAN:
this._result = lhs < rhs!;
this._result = lhs < rhs;
break;
case BinaryOperator.LESS_THAN_OR_EQUAL_TO:
this._result = lhs <= rhs!;
this._result = lhs <= rhs;
break;
case BinaryOperator.GREATER_THAN:
this._result = lhs > rhs!;
this._result = lhs > rhs;
break;
case BinaryOperator.GREATER_THAN_OR_EQUAL_TO:
this._result = lhs >= rhs!;
this._result = lhs >= rhs;
break;
}
}
Expand Down Expand Up @@ -163,7 +168,7 @@ export class UnaryCondition implements Condition {

public [createEval] (context: ConditionEvalContext) {
const { operator, operand } = this;
const evaluation = new UnaryConditionEval(operator, 0.0);
const evaluation = new UnaryConditionEval(operator, false);
const value = bindOr(
context,
operand,
Expand All @@ -182,20 +187,20 @@ export declare namespace UnaryCondition {

class UnaryConditionEval implements ConditionEval {
private declare _operator: UnaryOperator;
private declare _operand: Value;
private declare _operand: boolean;
private declare _result: boolean;

constructor (operator: UnaryOperator, operand: Value) {
constructor (operator: UnaryOperator, operand: boolean) {
this._operator = operator;
this._operand = operand;
this._eval();
}

public reset (value: Value) {
public reset (value: boolean) {
this.setOperand(value);
}

public setOperand (value: Value) {
public setOperand (value: boolean) {
this._operand = value;
this._eval();
}
Expand Down
2 changes: 1 addition & 1 deletion cocos/core/animation/marionette/parametric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function bindNumericOr<TValue, TThis, TArgs extends any[]> (
...args,
);

return initialValue;
return initialValue as unknown as number;
}

export function validateVariableExistence (varInstance: VarInstance | undefined, name: string): varInstance is VarInstance {
Expand Down

0 comments on commit dbc8934

Please sign in to comment.