Skip to content

Commit

Permalink
calculations working as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
tizayi committed Sep 26, 2023
1 parent 548b764 commit 24a445f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/calculations/qrange.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from "vitest";
import { assert, expect, test } from "vitest";
import { computeQrange } from "./qrange";
import {
BeamlineConfig,
Expand All @@ -11,7 +11,7 @@ test("Test computing q ranges", () => {
const detector: Detector = {
resolution: {
height: 1679,
width: 1465,
width: 1475,
},
pixelSize: {
height: 0.172,
Expand All @@ -28,15 +28,15 @@ test("Test computing q ranges", () => {
cameraLength: 1.9,
minWavelength: 6.2e-2,
maxWavelength: 0.335,
minCameraLength: 0,
maxCameraLength: 4,
wavelength: 9e-2,
minCameraLength: 1.9,
maxCameraLength: 9.9,
wavelength: 0.09,
};
const cameraTube: CircularDevice = {
centre: { x: 738, y: 840 },
diameter: 310,
};

const result = computeQrange(detector, beamstop, cameraTube, beamConfig);
console.log(result);
expect(result).toBeTruthy()
});
32 changes: 14 additions & 18 deletions src/calculations/qrange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export function computeQrange(
fullQRange: NumericRange;
} | null {
// convert pixel values to mm
const clearanceWidthMM = (beamstop.clearance ?? 0) * detector.pixelSize.width;
const clearanceWidthMM = (beamstop.clearance ?? 0) * detector.pixelSize.width + (beamstop.diameter / 2)
const clearaceHeightMM =
(beamstop.clearance ?? 0) * detector.pixelSize.height;
(beamstop.clearance ?? 0) * detector.pixelSize.height + (beamstop.diameter / 2);

const beamcentreXMM = (beamstop.centre.x ?? 0) * detector.pixelSize.width;
const beamcentreYMM = (beamstop.centre.y ?? 0) * detector.pixelSize.height;
Expand Down Expand Up @@ -85,58 +85,54 @@ export function computeQrange(
const ptMin = ray.getPoint(t1.min);
const ptMax = ray.getPoint(t1.max);

// works as intended up to here

const detProps: DetectorProperties = {
...detector,
origin: new Vector3(
beamcentreXMM,
beamcentreYMM,
beamProperties.cameraLength * 1e-3,
beamProperties.cameraLength * 1e3,
),
beamVector: new Vector3(0, 0, 1),
};

const diffCrystEnv: DiffractionCrystalEnvironment = {
wavelength: beamProperties.wavelength * 1e10,
referenceNormal: new Vector3(0, 1, 0),
strokesVector: new Vector4(1, 1, 0, 0),
};

const qspace = new QSpace(detProps, diffCrystEnv, 2 * Math.PI);


// get visible range
const visibleQMin = qspace.qFromPixelPosition(
ptMin.x / detector.pixelSize.width,
ptMin.y / detector.pixelSize.height,
ptMin,
);
const visibleQMax = qspace.qFromPixelPosition(
ptMax.x / detector.pixelSize.width,
ptMax.y / detector.pixelSize.height,
ptMax
);

detProps.origin.z = beamProperties.minCameraLength * 1e3;
qspace.setDiffractionCrystalEnviroment({
...diffCrystEnv,
wavelength: beamProperties.minCameraLength * 1e10,
wavelength: beamProperties.minWavelength * 1e10,
});
const fullQMin = qspace.qFromPixelPosition(
ptMin.x / detector.pixelSize.width,
ptMin.y / detector.pixelSize.height,
ptMax
);

detProps.origin.z = beamProperties.maxCameraLength * 1e3;
qspace.setDiffractionCrystalEnviroment({
...diffCrystEnv,
wavelength: beamProperties.maxCameraLength * 1e10,
wavelength: beamProperties.maxWavelength * 1e10,
});
const fullQMax = qspace.qFromPixelPosition(
ptMax.x / detector.pixelSize.width,
ptMax.y / detector.pixelSize.height,
ptMin
);

return {
ptMin: ptMin,
ptMax: ptMax,
visibleQRange: new NumericRange(visibleQMin.x, visibleQMax.x),
fullQRange: new NumericRange(fullQMin.x, fullQMax.x),
visibleQRange: new NumericRange(visibleQMin.length() * 1e10, visibleQMax.length() * 1e10),
fullQRange: new NumericRange(fullQMin.length() * 1e10, fullQMax.length() * 1e10),
};
}
30 changes: 15 additions & 15 deletions src/calculations/qspace.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Vector3, Vector4 } from "three";
import { Vector3, Vector2 } from "three";
import { Detector } from "../utils/types";

export interface DiffractionCrystalEnvironment {
wavelength: number;
referenceNormal: Vector3;
strokesVector: Vector4;
}

export interface DetectorProperties extends Detector {
Expand All @@ -15,8 +13,6 @@ export interface DetectorProperties extends Detector {
export default class QSpace {
detProps: DetectorProperties;
kmod: number;
reference: Vector3;
strokes: Vector4;
qScale: number;
mki: Vector3;

Expand All @@ -28,10 +24,13 @@ export default class QSpace {
this.detProps = detProps;
detProps.beamVector.normalize();
this.qScale = qScale;
this.mki = new Vector3().copy(detProps.beamVector).negate();


this.mki = detProps.beamVector.clone().negate();
this.kmod = this.qScale / diffexp.wavelength;
this.reference = diffexp.referenceNormal;
this.strokes = diffexp.strokesVector;
const ki = this.detProps.beamVector.clone()
ki.multiplyScalar(this.kmod)
this.mki = ki.negate()
}

convertToQ(q: Vector3) {
Expand All @@ -42,23 +41,24 @@ export default class QSpace {
} else {
q.add(this.mki);
}
return q
}

qFromPixelPosition(x: number, y: number) {
qFromPixelPosition(vector: Vector2) {
const q = new Vector3();
q.set(
-this.detProps.pixelSize.height * x,
-this.detProps.pixelSize.width * y,
-vector.x,
-vector.y,
0,
);
q.add(this.detProps.origin);
this.convertToQ(q);
return q;
return this.convertToQ(q);
}

setDiffractionCrystalEnviroment(diffexp: DiffractionCrystalEnvironment) {
this.kmod = this.qScale / diffexp.wavelength;
this.reference = diffexp.referenceNormal;
this.strokes = diffexp.strokesVector;
const ki = this.detProps.beamVector.clone()
ki.multiplyScalar(this.kmod)
this.mki = ki.negate()
}
}

0 comments on commit 24a445f

Please sign in to comment.