Skip to content

Commit

Permalink
Merge pull request #41 from tizayi/units
Browse files Browse the repository at this point in the history
Units
  • Loading branch information
tizayi authored Dec 4, 2023
2 parents d85d8e1 + 3eed616 commit 85c378b
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 329 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 5 additions & 44 deletions src/calculations/numericRange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* An object that stores a numeric range
*/
export default class NumericRange {
min: number;
max: number;
Expand All @@ -15,37 +12,20 @@ export default class NumericRange {
this.min = temp;
}
}
/**
* Returns true if the input value falls inside the numeric range
* @param value a number
* @returns
*/

containsValue(value: number): boolean {
return value >= this.min && value <= this.max;
}

/**
* Returns true if another range falls inside the numeric range
* @param other another range
* @returns
*/
containsRange(other: NumericRange): boolean {
return other.min >= this.min && other.max <= this.max;
}

/**
* Returns string represtaion of the numeric range
* @returns
*/

toString(): string {
return `(min:${this.min}, max:${this.max})`;
}

/**
* Returns the intersection of the numric range and another numeric range
* @param other another numeric range
* @returns the intersection of the two ranges
*/
intersect(other: NumericRange | null): NumericRange | null {
if (other === null) {
return null;
Expand All @@ -58,42 +38,23 @@ export default class NumericRange {
);
}

/**
* Returns true if the neumeric ranges are equal
* @param other another numeric range
* @returns
*/

equals(other: NumericRange): boolean {
return this.min === other.min && this.max === other.max;
}

/**
* Returns a new numeric range with the function applied elementwise
* @param func A function to apply to both the min and max of the range
* @returns A new Numeric range with the call back applied
*/

apply(func: (value: number) => number): NumericRange {
return new NumericRange(func(this.min), func(this.max));
}

/**
* Applys a function to the range in place
* @param func
* @returns
*/

inPlaceApply(func: (value: number) => number): NumericRange {
this.min = func(this.min);
this.max = func(this.max);
return this;
}

/**
* Create a new numeric range from a num max and a funtion
* @param min
* @param max
* @param func
* @returns
*/
static createWithFunc(
min: number,
max: number,
Expand Down
51 changes: 0 additions & 51 deletions src/calculations/qrange.test.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/calculations/qrange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export function computeQrange(
beamcentreY,
);

if (typeof initialPositionX === "number" || !("units" in initialPositionX)) {
return defaultReturn;
}
if (typeof initialPositionY === "number" || !("units" in initialPositionY)) {
return defaultReturn;
}
const initialPosition = new Vector2(
initialPositionX.toSI().toNumber(),
initialPositionY.toSI().toNumber(),
Expand Down
24 changes: 13 additions & 11 deletions src/calculations/qvalue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { UnitVector } from "../plot/plotUtils";
import { Ray } from "./ray";
import { Vector2 } from "three";
import * as mathjs from "mathjs";

export const calculateQValue = (
distance: number,
Expand Down Expand Up @@ -34,18 +36,18 @@ export const calculateDistanceFromQValue = (
};

export const getPointForQ = (
qValue: number,
angle: number,
cameralength: number,
wavelength: number,
beamstopCentre: Vector2,
): Vector2 => {
qValue: math.Unit,
angle: math.Unit,
cameralength: math.Unit,
wavelength: math.Unit,
beamstopCentre: UnitVector,
): UnitVector => {
const ray = new Ray(
new Vector2(Math.cos(angle), Math.sin(angle)),
beamstopCentre,
new Vector2(Math.cos(angle.toSI().toNumber()), Math.sin(angle.toSI().toNumber())),
new Vector2(beamstopCentre.x.toSI().toNumber(), beamstopCentre.y.toSI().toNumber()),
);
return ray.getPointAtDistance(
1.0e3 *
(calculateDistanceFromQValue(qValue, cameralength, wavelength) ?? 0),
const result = ray.getPointAtDistance(
(calculateDistanceFromQValue(qValue.toSI().toNumber(), cameralength.toSI().toNumber(), wavelength.toSI().toNumber())) ?? 0,
);
return { x: mathjs.unit(result.x, "m"), y: mathjs.unit(result.y, "m") }
};
68 changes: 68 additions & 0 deletions src/calculations/unitRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as mathjs from "mathjs";
import NumericRange from "./numericRange";

export default class UnitRange {
min: mathjs.Unit;
max: mathjs.Unit;

constructor(min: mathjs.Unit, max: mathjs.Unit) {
this.min = min;
this.max = max;

if (mathjs.larger(min, max)) {
const temp = max.clone();
this.max = min;
this.min = temp;
}
}

to(units: string): UnitRange {
return new UnitRange(this.min.to(units), this.max.to(units))
}

containsValue(value: mathjs.Unit): boolean {
return mathjs.largerEq(value, this.min) && mathjs.largerEq(value, this.max);
}

containsRange(other: UnitRange): boolean {
console.log(`${other.min.formatUnits()} ${this.min.formatUnits()}`)
console.log(`${other.min.formatUnits()} ${this.min.formatUnits()}`)
return mathjs.smallerEq(this.min, other.min) && mathjs.largerEq(this.max, other.max,);
}

toString(): string {
return `(min:${this.min.toString()}, max:${this.max.toString()})`;
}


intersect(other: UnitRange): UnitRange | null {
if (mathjs.larger(other.min, this.max) || mathjs.larger(this.min, other.max)) return null;

return new UnitRange(
mathjs.max(other.min, this.min),
mathjs.min(other.max, this.max),
);
}

equals(other: UnitRange): boolean {
return mathjs.equal(this.min, other.min) && mathjs.equal(this.max, other.max);
}


apply(func: (value: mathjs.Unit) => mathjs.Unit): UnitRange {
return new UnitRange(func(this.min), func(this.max));
}

inPlaceApply(func: (value: mathjs.Unit) => mathjs.Unit): UnitRange {
this.min = func(this.min);
this.max = func(this.max);
return this;
}

static fromNumericRange(range: NumericRange | null, units: string): UnitRange {
return new UnitRange(
mathjs.unit(range?.min ?? NaN, units),
mathjs.unit(range?.max ?? NaN, units),
)
}
}
2 changes: 1 addition & 1 deletion src/data-entry/beamlineconfigStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useBeamlineConfigStore = create<BeamlineConfigStore>((set) => ({
energy: wavelength2EnergyConverter(defaultConfig.wavelength).to("keV"),
updateAngle: (newAngle: number | null, newUnits: string) =>
set({
angle: unit(newAngle ?? NaN, newUnits as string),
angle: unit(newAngle ?? NaN, newUnits),
userAngle: newAngle,
}),
updateAngleUnits: (newUnits: AngleUnits) =>
Expand Down
Loading

0 comments on commit 85c378b

Please sign in to comment.