Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tests #3

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "pro-3d-video",
"version": "1.0.0",
"description": "Procedural 3D Video: A Procedural Approach For Describing Motion",
"main": "dist/index.js",
"type": "module",
"scripts": {
"generate:parser": "nearleyc grammar.ne -o src/parser/parser.ts",
"build": "tsc -p build.tsconfig.json",
"fix": "run-s fix:prettier fix:eslint",
"fix:prettier": "prettier --write 'src/**/*.{ts,tsx}'",
"fix:eslint": "eslint src --fix",
"test": "nyc mocha ./tests/*.spec.ts --timeout 15000"
"name": "pro-3d-video",
"version": "1.0.0",
"description": "Procedural 3D Video: A Procedural Approach For Describing Motion",
"main": "dist/index.js",
"type": "module",
"scripts": {
"generate:parser": "nearleyc grammar.ne -o src/parser/parser.ts",
"build": "tsc -p build.tsconfig.json",
"fix": "run-s fix:prettier fix:eslint",
"fix:prettier": "prettier --write 'src/**/*.{ts,tsx}'",
"fix:eslint": "eslint src --fix",
"test": "nyc mocha ./tests/*.spec.ts --timeout 15000",
"test:interpreter": "nyc mocha ./tests/interpreter.spec.ts --timeout 15000"
},
"exports": {
".": "./dist/index.js",
Expand Down
29 changes: 29 additions & 0 deletions src/domains/arithmetic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { InterpreterOptions, Operations, Value } from "../../interpreter/index.js"

const operations: Operations = {}

export const arithmeticInterpreterOptions: InterpreterOptions = {
cloneValue(value) {
return value
},
serialize(values: Value[], prevProgress: any, currentProgress: any) {
return values
},
comparePriority(e1, e2, e1Trans, e2Trans) {
return (e1 as number) - (e2 as number)
},
computeDurationMS: 1000,
createValue() {
return 0
},
getComputeProgress() {
return 0
},
operations,
shouldInterrrupt(startProgress, currentProgress) {
return currentProgress - startProgress > 1000 //interrupt every second
},
shouldWait(p) {
return p > 10000 //only computes the first 10 second
},
}
4 changes: 4 additions & 0 deletions src/domains/arithmetic/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { initializeWorker } from "../../interpreter/initialize-worker.js"
import { arithmeticInterpreterOptions } from "./index.js"

initializeWorker(arithmeticInterpreterOptions)
33 changes: 29 additions & 4 deletions src/domains/motion/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NestedPrecomputedOperation } from "../../index.js"
import { InterpreterOptions, OperationNextCallback, Operations } from "../../interpreter/index.js"

const TIME_STEP = 100 //ms
Expand Down Expand Up @@ -115,10 +116,25 @@ export const operations: Operations = {
},
}

export function compareMotionEntityPriority(e1: MotionEntity, e2: MotionEntity) {
const e1Time = e1.keyframes[e1.keyframes.length - 1].t
const e2Time = e2.keyframes[e2.keyframes.length - 1].t
return e2Time - e1Time
export function comparePriority(e1: unknown, e2: unknown, e1Trans: unknown, e2Trans: unknown) {
if (!isMotionEntity(e1) || !isMotionEntity(e2)) {
return 0
}
let addedCurrTime = 0
let addedListItemTime = 0
if (isNestedPrecomputedOperation(e1Trans)) {
const oper = e1Trans as NestedPrecomputedOperation
addedCurrTime = oper.parameters[3]
}
if (isNestedPrecomputedOperation(e2Trans)) {
const oper = e2Trans as NestedPrecomputedOperation
addedListItemTime = oper.parameters[3]
}
return (
e1.keyframes[e1.keyframes.length - 1]!.t +
addedCurrTime -
(e2.keyframes[e2.keyframes.length - 1]!.t + addedListItemTime)
)
}

export function createMotionEntitiy({ type, x, y, z, time }: any, astId: string): MotionEntity {
Expand Down Expand Up @@ -161,5 +177,14 @@ export function isMotionEntity(value: unknown): value is MotionEntity {
return typeof value === "object" && value != null && "keyframes" in value
}

function isNestedPrecomputedOperation(value: unknown): value is NestedPrecomputedOperation {
return (
typeof value === "object" &&
!Array.isArray(value) &&
value !== null &&
"type" in value &&
value.type == "precomputedOperation"
)
}
export * from "./helper.js"
export * from "./exporter.js"
8 changes: 3 additions & 5 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type InterpreterOptions = Readonly<{
/**
* compares the priority between two entries; higher priority results in an faster execution. Example function: (v1, v2) => v1.prio - v2.prio (returns negative value if the order is wrong)
*/
comparePriority: (v1: unknown, v2: unknown) => number
comparePriority: (v1: unknown, v2: unknown, v1Tr: unknown, v2Tr: unknown) => number
createValue: (initialVariables: NestedDescription["initialVariables"], astId: string) => any
serialize: (values: Array<Value>, prevProgress: any, currentProgress: any | undefined) => any
cloneValue: (value: unknown) => unknown
Expand Down Expand Up @@ -125,9 +125,7 @@ function nextQueued(
}
return
}

currentEntry.stack.unshift(...newTransformations)

if (newRaw !== undefined) {
//we need to reinsert the entry since the value changed which can change the change the priority and this the order in the queue
queue.pop()
Expand Down Expand Up @@ -196,7 +194,7 @@ export function interpreteQueueRecursive(

if (
nextEntry == null ||
options.shouldWait(references.requestedProgress, options.getComputeProgress(nextEntry.value))
options.shouldWait(references.requestedProgress, options.getComputeProgress(nextEntry.value.raw))
) {
return
}
Expand Down Expand Up @@ -308,7 +306,7 @@ function interpreteStochasticSwitch<R>(
options: InterpreterOptions,
next: NextCallback<R>
): R {
const rand = Math.random()// murmurhash.v3(value.variables.index ?? "", options.seed) / _32bit_max_int
const rand = Math.random() // murmurhash.v3(value.variables.index ?? "", options.seed) / _32bit_max_int

let sum = 0
let i = -1
Expand Down
10 changes: 5 additions & 5 deletions src/interpreter/initialize-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ function publishResult(
options: InterpreterOptions,
values: Array<Value>,
prevProgress: any,
currentProgress: any | undefined,
currentProgress: any | undefined
) {
postMessage({
type: WorkerMessageType.Results,
result: options.serialize(values, prevProgress, currentProgress),
isFinal: currentProgress === undefined
isFinal: currentProgress === undefined,
})
}

Expand All @@ -38,9 +38,9 @@ export function initializeWorker(options: InterpreterOptions): void {
throw new Error(`unable to update requested progress when interpretation has not yet been started`)
}
references.requestedProgress = e.data.requestedProgress
if (references.timeoutRef != null) {
interpreteQueueRecursive(queue, descriptions, options, references, publish)
}
//if (references.timeoutRef != null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Den Bug hier habe ich nach wie vor nicht fixen können. Ohne das "if" Statement laufen die Tests durch.

interpreteQueueRecursive(queue, descriptions, options, references, publish)
//}
return
}
}
Expand Down
21 changes: 16 additions & 5 deletions src/interpreter/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class Queue {
*
* @param compare should compare the priority between two entries; higher priority results in an faster execution. Example function: (v1, v2) => v1.prio - v2.prio (returns negative value if the order is wrong)
*/
constructor(private compare: (v1: unknown, v2: unknown) => number) {}
constructor(private compare: (v1: unknown, v2: unknown, v1Tr: unknown, v2Tr: unknown) => number) {}

/**
* remove the stack entry with the highest priority
Expand All @@ -45,10 +45,21 @@ export class Queue {
this.results.push(entry.value)
return
}
let i = 0
while (i < this.list.length && this.compare(entry.value.raw, this.list[i].value.raw) < 0) {
i++
let index = this.list.length
const newEntryTrans = entry.stack[0]
for (let i = 0; i < this.list.length; i++) {
if (
this.compare(
entry,
this.list[i],
newEntryTrans,
this.list[i].stack.length > 0 ? this.list[i].stack[0] : undefined
) < 0
) {
index = i
break
}
}
this.list.splice(i, 0, entry)
this.list.splice(index, 0, entry)
}
}
Loading