Skip to content

Commit

Permalink
okkkkkkkkk lets go
Browse files Browse the repository at this point in the history
  • Loading branch information
tizayi committed Dec 5, 2023
1 parent 89b23c4 commit 2161791
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 157 deletions.
4 changes: 0 additions & 4 deletions src/calculations/numericRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default class NumericRange {
return other.min >= this.min && other.max <= this.max;
}


toString(): string {
return `(min:${this.min}, max:${this.max})`;
}
Expand All @@ -38,17 +37,14 @@ export default class NumericRange {
);
}


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


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


inPlaceApply(func: (value: number) => number): NumericRange {
this.min = func(this.min);
this.max = func(this.max);
Expand Down
18 changes: 14 additions & 4 deletions src/calculations/qvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ export const getPointForQ = (
beamstopCentre: UnitVector,
): UnitVector => {
const ray = new Ray(
new Vector2(Math.cos(angle.toSI().toNumber()), Math.sin(angle.toSI().toNumber())),
new Vector2(beamstopCentre.x.toSI().toNumber(), beamstopCentre.y.toSI().toNumber()),
new Vector2(
Math.cos(angle.toSI().toNumber()),
Math.sin(angle.toSI().toNumber()),
),
new Vector2(
beamstopCentre.x.toSI().toNumber(),
beamstopCentre.y.toSI().toNumber(),
),
);
const result = ray.getPointAtDistance(
(calculateDistanceFromQValue(qValue.toSI().toNumber(), cameralength.toSI().toNumber(), wavelength.toSI().toNumber())) ?? 0,
calculateDistanceFromQValue(
qValue.toSI().toNumber(),
cameralength.toSI().toNumber(),
wavelength.toSI().toNumber(),
) ?? 0,
);
return { x: mathjs.unit(result.x, "m"), y: mathjs.unit(result.y, "m") }
return { x: mathjs.unit(result.x, "m"), y: mathjs.unit(result.y, "m") };
};
108 changes: 58 additions & 50 deletions src/calculations/unitRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,75 @@ import * as mathjs from "mathjs";
import NumericRange from "./numericRange";

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

constructor(min: mathjs.Unit, max: mathjs.Unit) {
this.min = min;
this.max = max;
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;
}
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))
}
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);
}
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,);
}
containsRange(other: UnitRange): boolean {
return (
mathjs.smallerEq(this.min, other.min) &&
mathjs.largerEq(this.max, other.max)
);
}

toString(): string {
return `(min:${this.min.toString()}, max:${this.max.toString()})`;
}
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;

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),
);
}

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)
);
}

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;
}

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),
)
}
static fromNumericRange(
range: NumericRange | null,
units: string,
): UnitRange {
return new UnitRange(
mathjs.unit(range?.min ?? NaN, units),
mathjs.unit(range?.max ?? NaN, units),
);
}
}
83 changes: 46 additions & 37 deletions src/plot/centrePlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import { Vector3 } from "three";
import { useBeamstopStore } from "../data-entry/beamstopStore";
import { useDetectorStore } from "../data-entry/detectorStore";
import { useCameraTubeStore } from "../data-entry/cameraTubeStore";
import {
UnitVector,
Plotter,
getDomains,
} from "./plotUtils";
import { UnitVector, Plotter, getDomains } from "./plotUtils";
import { usePlotStore } from "./plotStore";
import {
BeamlineConfig,
Expand Down Expand Up @@ -71,32 +67,32 @@ export default function CentrePlot(): JSX.Element {
return { centre: state.centre, diameter: state.diameter };
});


const scaleFactor: mathjs.Unit | null = null;
if (
beamlineConfig.cameraLength &&
beamlineConfig.wavelength
) {
// const scaleFactor = mathjs.divide(2e-12 * Math.PI, mathjs.multiply(mathjs.unit(beamlineConfig.cameraLength, "m"), beamlineConfig.wavelength));
let scaleFactor: mathjs.Unit | null = null;
if (beamlineConfig.cameraLength && beamlineConfig.wavelength) {
scaleFactor = mathjs.divide(
2 * Math.PI,
mathjs.multiply(
mathjs.unit(beamlineConfig.cameraLength, "m"),
beamlineConfig.wavelength.to("m"),
),
);
}

// evil :( :( :( :()
/* eslint-disable */
if (mathjs.Unit.UNITS.xpixel) {
delete mathjs.Unit.UNITS.xpixel
delete mathjs.Unit.UNITS.xpixel;
}

if (mathjs.Unit.UNITS.ypixel) {
delete mathjs.Unit.UNITS.ypixel
delete mathjs.Unit.UNITS.ypixel;
}
/* eslint-enable */
// evil :( :( :( :()


mathjs.createUnit("xpixel", detector.pixelSize.width.toString());
mathjs.createUnit("ypixel", detector.pixelSize.height.toString());


const { ptMin, ptMax, visibleQRange, fullQRange } = computeQrange(
detector,
beamstop,
Expand All @@ -105,54 +101,64 @@ export default function CentrePlot(): JSX.Element {
);

// I am about here
const visibleQRangeUnits = UnitRange.fromNumericRange(visibleQRange, "m^-1").to("nm^-1");
const fullQRangeUnits = UnitRange.fromNumericRange(fullQRange, "m^-1").to("nm^-1")
const visibleQRangeUnits = UnitRange.fromNumericRange(
visibleQRange,
"m^-1",
).to("nm^-1");
const fullQRangeUnits = UnitRange.fromNumericRange(fullQRange, "m^-1").to(
"nm^-1",
);

const minPoint: UnitVector = {
x: mathjs.unit(ptMin.x, "m"),
y: mathjs.unit(ptMin.y, "m")
}
y: mathjs.unit(ptMin.y, "m"),
};

const maxPoint: UnitVector = {
x: mathjs.unit(ptMax.x, "m"),
y: mathjs.unit(ptMax.y, "m")
}
y: mathjs.unit(ptMax.y, "m"),
};

const beamstopCentre: UnitVector = {
x: mathjs.unit(beamstop.centre.x ?? NaN, "xpixel"),
y: mathjs.unit(beamstop.centre.y ?? NaN, "ypixel")
}
y: mathjs.unit(beamstop.centre.y ?? NaN, "ypixel"),
};

const cameraTubeCentre: UnitVector = {
x: mathjs.unit(cameraTube.centre.x ?? NaN, "xpixel"),
y: mathjs.unit(cameraTube.centre.y ?? NaN, "ypixel")
}
y: mathjs.unit(cameraTube.centre.y ?? NaN, "ypixel"),
};

const plotter = new Plotter(plotConfig.plotAxes, scaleFactor)
const plotter = new Plotter(plotConfig.plotAxes, scaleFactor);

const plotBeamstop = plotter.createPlotEllipse(
beamstopCentre,
beamstop.diameter
beamstop.diameter,
beamstopCentre,
);

const plotCameraTube = plotter.createPlotEllipse(
cameraTubeCentre,
cameraTube.diameter
cameraTube.diameter,
beamstopCentre,
);

const plotClearance = plotter.createPlotEllipseClearance(
beamstopCentre,
beamstop.diameter,
beamstop.clearance ?? 0
beamstop.clearance ?? 0,
beamstopCentre,
);

const plotDetector = plotter.createPlotRectangle(
detector.resolution,
beamstopCentre,
);

const plotVisibleRange = plotter.createPlotRange(
minPoint,
maxPoint,
beamstopCentre,
);

// I am up to here
Expand All @@ -175,15 +181,14 @@ export default function CentrePlot(): JSX.Element {
result = mathjs.unit(value, state.qUnits);
}
return result;
}
};

return new UnitRange(
getUnit(state.requestedMin),
getUnit(state.requestedMax)
)
getUnit(state.requestedMax),
);
});


let plotRequestedRange = {
start: new Vector3(0, 0),
end: new Vector3(0, 0),
Expand All @@ -209,11 +214,12 @@ export default function CentrePlot(): JSX.Element {
);
plotRequestedRange = plotter.createPlotRange(
requestedMinPt,
requestedMaxPt
requestedMaxPt,
beamstopCentre,
);
}


console.log(plotDetector);

const domains = getDomains(plotDetector, plotConfig.plotAxes);

Expand Down Expand Up @@ -359,7 +365,10 @@ export default function CentrePlot(): JSX.Element {
<LegendBar />
</Box>
</Stack>
<ResultsBar visableQRange={visibleQRangeUnits} fullQrange={fullQRangeUnits} />
<ResultsBar
visableQRange={visibleQRangeUnits}
fullQrange={fullQRangeUnits}
/>
</Stack>
</Box>
);
Expand Down
2 changes: 1 addition & 1 deletion src/plot/plotStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { create } from "zustand";
export enum PlotAxes {
milimeter = "mm",
pixel = "pixel",
reciprocal = "mm^-1",
reciprocal = "nm^-1",
}

export interface PlotConfig {
Expand Down
Loading

0 comments on commit 2161791

Please sign in to comment.