2017-08-26 16:47:18 -04:00
|
|
|
// pathfinder/client/src/utils.ts
|
|
|
|
//
|
|
|
|
// Copyright © 2017 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-09-11 19:07:11 -04:00
|
|
|
import * as glmatrix from 'gl-matrix';
|
|
|
|
|
2017-09-29 14:58:16 -04:00
|
|
|
export const FLOAT32_SIZE: number = 4;
|
|
|
|
|
|
|
|
export const UINT16_SIZE: number = 2;
|
|
|
|
|
2017-09-02 01:29:05 -04:00
|
|
|
export const UINT32_MAX: number = 0xffffffff;
|
2017-08-26 16:47:18 -04:00
|
|
|
export const UINT32_SIZE: number = 4;
|
|
|
|
|
2017-08-27 15:43:17 -04:00
|
|
|
export function panic(message: string): never {
|
|
|
|
throw new PathfinderError(message);
|
|
|
|
}
|
|
|
|
|
2017-08-26 16:47:18 -04:00
|
|
|
export function assert(value: boolean, message: string) {
|
|
|
|
if (!value)
|
2017-08-27 15:43:17 -04:00
|
|
|
panic(message);
|
2017-08-26 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function expectNotNull<T>(value: T | null, message: string): T {
|
|
|
|
if (value === null)
|
|
|
|
throw new PathfinderError(message);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-08-29 17:31:45 -04:00
|
|
|
function expectNotUndef<T>(value: T | undefined, message: string): T {
|
|
|
|
if (value === undefined)
|
|
|
|
throw new PathfinderError(message);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-08-26 16:47:18 -04:00
|
|
|
export function unwrapNull<T>(value: T | null): T {
|
|
|
|
return expectNotNull(value, "Unexpected null!");
|
|
|
|
}
|
|
|
|
|
2017-08-29 17:31:45 -04:00
|
|
|
export function unwrapUndef<T>(value: T | undefined): T {
|
|
|
|
return expectNotUndef(value, "Unexpected `undefined`!");
|
|
|
|
}
|
|
|
|
|
2017-09-11 19:07:11 -04:00
|
|
|
export function scaleRect(rect: glmatrix.vec4, scale: number): glmatrix.vec4 {
|
|
|
|
const upperLeft = glmatrix.vec2.clone([rect[0], rect[1]]);
|
|
|
|
const lowerRight = glmatrix.vec2.clone([rect[2], rect[3]]);
|
|
|
|
glmatrix.vec2.scale(upperLeft, upperLeft, scale);
|
|
|
|
glmatrix.vec2.scale(lowerRight, lowerRight, scale);
|
|
|
|
return glmatrix.vec4.clone([upperLeft[0], upperLeft[1], lowerRight[0], lowerRight[1]]);
|
|
|
|
}
|
|
|
|
|
2017-08-26 16:47:18 -04:00
|
|
|
export class PathfinderError extends Error {
|
|
|
|
constructor(message?: string | undefined) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|