Working aobrtable perspective search
parent
8c4fd2d4cc
commit
04a4fa2953
@ -0,0 +1,96 @@
|
||||
import { deferedSpawn } from "async-spawner";
|
||||
import Emittery from "emittery";
|
||||
import { promisify } from "util";
|
||||
|
||||
//needed to pass information on wether or not the promise has been aborted witout 'this'
|
||||
class AbortableEmittery extends Emittery {
|
||||
public aborted = false;
|
||||
|
||||
abort() {
|
||||
this.aborted = true;
|
||||
this.emit("abort");
|
||||
}
|
||||
}
|
||||
|
||||
class AbortablePromise<T> extends Promise<T | null> {
|
||||
private emitter: AbortableEmittery;
|
||||
|
||||
constructor(
|
||||
make_generator: (
|
||||
await_or_abort: (
|
||||
promise: Promise<any>,
|
||||
on_abort: () => void
|
||||
) => Promise<void>
|
||||
) => AsyncGenerator
|
||||
) {
|
||||
const emitter = new AbortableEmittery();
|
||||
const await_or_abort = (
|
||||
promise: Promise<any>,
|
||||
on_abort: () => void
|
||||
) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
let resolved = false;
|
||||
emitter.on("abort", () => {
|
||||
if (!resolved) {
|
||||
on_abort();
|
||||
resolve(null);
|
||||
resolved = true;
|
||||
}
|
||||
});
|
||||
promise
|
||||
.then((result) => {
|
||||
if (!resolved) {
|
||||
resolve(result);
|
||||
resolved = true;
|
||||
}
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
||||
super(async (resolve, reject) => {
|
||||
let step_value;
|
||||
const generator = make_generator(await_or_abort);
|
||||
do {
|
||||
if (emitter.aborted) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
step_value = await generator.next();
|
||||
} while (!step_value.done);
|
||||
});
|
||||
this.emitter = emitter;
|
||||
}
|
||||
|
||||
abort() {
|
||||
this.emitter.abort();
|
||||
}
|
||||
}
|
||||
|
||||
// const sleep = promisify((timeout: number, callback: (...args: any[]) => void) =>
|
||||
// setTimeout(callback, timeout)
|
||||
// );
|
||||
|
||||
// const a = new AbortablePromise(async function* () {
|
||||
// yield await sleep(1000);
|
||||
// console.log("awaited 100");
|
||||
// yield await sleep(2000);
|
||||
// console.log("awaited 200");
|
||||
// yield await sleep(3000);
|
||||
// console.log("awaited 300");
|
||||
// });
|
||||
|
||||
// setTimeout(() => a.abort(), 1500);
|
||||
|
||||
const b = new AbortablePromise(async function* (await_or_abort) {
|
||||
const ping = await deferedSpawn("ping", ["8.8.8.8"]);
|
||||
while (true) {
|
||||
console.log(
|
||||
await await_or_abort(ping.waitForNextData(), () => {
|
||||
ping.kill();
|
||||
})
|
||||
);
|
||||
yield;
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(() => b.abort(), 5000);
|
@ -0,0 +1,139 @@
|
||||
export function newpos(
|
||||
x: number,
|
||||
y: number,
|
||||
t: transformCoefs
|
||||
): { x: number; y: number } {
|
||||
return {
|
||||
x: (x * t[0] + y * t[1] + t[2]) / (t[6] * x + t[7] * y + 1),
|
||||
y: (x * t[3] + y * t[4] + t[5]) / (t[6] * x + t[7] * y + 1),
|
||||
};
|
||||
}
|
||||
|
||||
export type transformCoefs = [
|
||||
number,
|
||||
number,
|
||||
number,
|
||||
number,
|
||||
number,
|
||||
number,
|
||||
number,
|
||||
number
|
||||
];
|
||||
export type transformDesc = {
|
||||
forward: transformCoefs;
|
||||
backward: transformCoefs;
|
||||
};
|
||||
|
||||
// See perspective.md to learn how coefficients are created for calls below:
|
||||
export const transforms: transformDesc[] = [
|
||||
{
|
||||
forward: [
|
||||
0.418385,
|
||||
-0.064912,
|
||||
1.11204e-12,
|
||||
-1.07285e-16,
|
||||
0.407118,
|
||||
1.85136e-13,
|
||||
-0.000143964,
|
||||
-0.0000155811,
|
||||
],
|
||||
backward: [
|
||||
2.39014,
|
||||
0.381091,
|
||||
-5.07071e-13,
|
||||
8.09745e-16,
|
||||
2.45629,
|
||||
-1.95474e-12,
|
||||
0.000344095,
|
||||
0.0000931351,
|
||||
],
|
||||
},
|
||||
{
|
||||
forward: [
|
||||
1.52043,
|
||||
3.31591e-16,
|
||||
-9.50693e-13,
|
||||
4.91024e-16,
|
||||
1.29283,
|
||||
-4.86523e-28,
|
||||
0.00012882,
|
||||
0.000072315,
|
||||
],
|
||||
backward: [
|
||||
0.657707,
|
||||
2.87435e-16,
|
||||
-6.72954e-13,
|
||||
7.80686e-16,
|
||||
0.773495,
|
||||
-1.67079e-12,
|
||||
-0.0000847259,
|
||||
-0.0000559353,
|
||||
],
|
||||
},
|
||||
{
|
||||
forward: [
|
||||
0.894876,
|
||||
-0.285098,
|
||||
8.16135e-13,
|
||||
-4.48986e-17,
|
||||
1.06368,
|
||||
1.8139e-13,
|
||||
-0.0000260208,
|
||||
-0.0000110713,
|
||||
],
|
||||
backward: [
|
||||
1.11747,
|
||||
0.299515,
|
||||
-4.53809e-13,
|
||||
4.55732e-16,
|
||||
0.940129,
|
||||
-4.27521e-13,
|
||||
0.0000290775,
|
||||
0.0000182021,
|
||||
],
|
||||
},
|
||||
{
|
||||
forward: [
|
||||
1.96875,
|
||||
8.8124e-16,
|
||||
-1.34293e-12,
|
||||
0.725124,
|
||||
1,
|
||||
-8.35683e-13,
|
||||
0.00023979,
|
||||
7.05268e-19,
|
||||
],
|
||||
backward: [
|
||||
0.507937,
|
||||
-2.73557e-16,
|
||||
4.90838e-13,
|
||||
-0.368317,
|
||||
1,
|
||||
-1.03804e-12,
|
||||
-0.000121798,
|
||||
-1.49027e-19,
|
||||
],
|
||||
},
|
||||
{
|
||||
forward: [
|
||||
3.88517,
|
||||
0.106038,
|
||||
-3.10281e-12,
|
||||
1.43097,
|
||||
0.722206,
|
||||
-5.02081e-13,
|
||||
0.00071415,
|
||||
-0.0000918632,
|
||||
],
|
||||
backward: [
|
||||
0.272104,
|
||||
-0.0399518,
|
||||
-6.63745e-13,
|
||||
-0.539145,
|
||||
1.46381,
|
||||
-8.91691e-12,
|
||||
-0.000243851,
|
||||
0.000163002,
|
||||
],
|
||||
},
|
||||
];
|
Loading…
Reference in New Issue