Skip to content

Commit

Permalink
feat: scale 2D phase correction traces
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Oct 20, 2023
1 parent 0eef276 commit 9cb5a1f
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/component/2d/1d-tracer/HorizontalSliceChart.tsx
Expand Up @@ -25,7 +25,7 @@ function usePath(data: NmrData1D, options: UsePathOptions) {

const { x, re: y } = data;

const scaleY = getSliceYScale(y, height, mode, vericalMargin);
const scaleY = getSliceYScale(y, height, mode, { margin: vericalMargin });

const pathBuilder = new PathBuilder();
pathBuilder.moveTo(scaleX(x[0]), scaleY(y[0]));
Expand Down
2 changes: 1 addition & 1 deletion src/component/2d/1d-tracer/VerticalSliceChart.tsx
Expand Up @@ -24,7 +24,7 @@ function usePath(data, props: usePathOptions) {

const { x, re: y } = data;

const scaleY = getSliceYScale(y, width, mode, horizontalMargin);
const scaleY = getSliceYScale(y, width, mode, { margin: horizontalMargin });

const pathBuilder = new PathBuilder();

Expand Down
Expand Up @@ -8,6 +8,7 @@ import {
get2DYScale,
getSliceYScale,
} from '../../utilities/scale';
import { useActivePhaseTraces } from './useActivePhaseTraces';

interface SpectrumPhaseTraceProps extends React.SVGAttributes<SVGGElement> {
data: { x: Float64Array; re: Float64Array };
Expand All @@ -19,10 +20,14 @@ interface SpectrumPhaseTraceProps extends React.SVGAttributes<SVGGElement> {

function usePath(x: Float64Array, y: Float64Array, direction: TraceDirection) {
const { width, margin, height, xDomain, yDomain, mode } = useChartData();
const { scaleRatio } = useActivePhaseTraces();

if (direction === 'horizontal') {
const scaleX = get2DXScale({ margin, width, xDomain, mode });
const scaleY = getSliceYScale(y, height, 'RTL', margin.top + margin.bottom);
const scaleY = getSliceYScale(y, height, 'RTL', {
margin: margin.top + margin.bottom,
scaleRatio,
});

const pathBuilder = new PathBuilder();
pathBuilder.moveTo(scaleX(x[0]), scaleY(y[0]));
Expand All @@ -34,7 +39,10 @@ function usePath(x: Float64Array, y: Float64Array, direction: TraceDirection) {
}

const scaleX = get2DYScale({ margin, height, yDomain });
const scaleY = getSliceYScale(y, width, 'RTL', margin.left + margin.right);
const scaleY = getSliceYScale(y, width, 'RTL', {
margin: margin.left + margin.right,
scaleRatio,
});

const pathBuilder = new PathBuilder();

Expand Down
11 changes: 7 additions & 4 deletions src/component/2d/Viewer2D.tsx
Expand Up @@ -140,14 +140,17 @@ function Viewer2D({ emptyText = undefined }: Viewer2DProps) {
);

const handleZoom: OnZoom = (event) => {
const { x: startX, y: startY } = event;
const { x: startX, y: startY, shiftKey } = event;
const trackID = getLayoutID(DIMENSION, { startX, startY });

if (trackID) {
if (trackID === 'CENTER_2D') {
dispatch({ type: 'SET_2D_LEVEL', payload: event });
} else {
if (
trackID !== 'CENTER_2D' ||
(selectedTool === 'phaseCorrectionTwoDimension' && !shiftKey)
) {
dispatch({ type: 'SET_ZOOM', payload: { event, trackID } });
} else {
dispatch({ type: 'SET_2D_LEVEL', payload: event });
}
}
};
Expand Down
13 changes: 11 additions & 2 deletions src/component/2d/utilities/scale.ts
Expand Up @@ -94,15 +94,24 @@ function use1DTraceYScale(
return get1DYScale(yDomains[SpectrumId], height, verticalMargin);
}

interface SliceYScaleOptions {
margin?: number;
scaleRatio?: number;
}

function getSliceYScale(
data: Float64Array,
size: number,
mode: SpectraDirection,
margin = 10,
options: SliceYScaleOptions = {},
) {
const { margin = 10, scaleRatio = 1 } = options;
const max = xMaxValue(data);
size = mode === 'RTL' ? size : size / 2;
return scaleLinear([0, max] as number[], [size - margin, margin]);
return scaleLinear([0, max * scaleRatio] as number[], [
size - margin,
margin,
]);
}

export {
Expand Down
10 changes: 9 additions & 1 deletion src/component/reducer/Reducer.ts
Expand Up @@ -62,7 +62,13 @@ export interface SpectrumTrace {

export type PhaseCorrrectionTraces = Record<
TraceDirection,
{ spectra: SpectrumTrace[]; ph0: number; ph1: number; pivot: Pivot | null }
{
spectra: SpectrumTrace[];
ph0: number;
ph1: number;
pivot: Pivot | null;
scaleRatio: number;
}
>;
export interface TwoDimensionPhaseCorrrection {
traces: PhaseCorrrectionTraces;
Expand Down Expand Up @@ -149,12 +155,14 @@ export const getInitialState = (): State => ({
ph1: 0,
pivot: null,
spectra: [],
scaleRatio: 1,
},
vertical: {
ph0: 0,
ph1: 0,
pivot: null,
spectra: [],
scaleRatio: 1,
},
},
},
Expand Down
2 changes: 0 additions & 2 deletions src/component/reducer/actions/FiltersActions.ts
Expand Up @@ -681,7 +681,6 @@ function handleAddPhaseCorrectionTrace(
},
},
data: spectra,
mode,
} = draft;

const tracesSpectra = traces[activeTraceDirection].spectra;
Expand Down Expand Up @@ -1134,7 +1133,6 @@ function handleSetTwoDimensionPhaseCorrectionPivotPoint(
twoDimensionPhaseCorrection: { activeTraceDirection, traces },
},
},
mode,
} = draft;
const { x, y } = action.payload;
const traceDirection = traces[activeTraceDirection];
Expand Down
29 changes: 19 additions & 10 deletions src/component/reducer/actions/ToolsActions.ts
Expand Up @@ -25,7 +25,9 @@ import zoomHistoryManager, {
} from '../helper/ZoomHistoryManager';
import { getActiveSpectra } from '../helper/getActiveSpectra';
import { getActiveSpectrum } from '../helper/getActiveSpectrum';
import { getTwoDimensionPhaseCorrectionOptions } from '../helper/getTwoDimensionPhaseCorrectionOptions';
import { getVerticalAlign } from '../helper/getVerticalAlign';
import { setIntegralsViewProperty } from '../helper/setIntegralsViewProperty';
import { setRangesViewProperty } from '../helper/setRangesViewProperty';
import { ActionType } from '../types/ActionType';

Expand All @@ -36,7 +38,6 @@ import {
rollbackSpectrum,
} from './FiltersActions';
import { changeSpectrumVerticalAlignment } from './PreferencesActions';
import { setIntegralsViewProperty } from '../helper/setIntegralsViewProperty';

interface BrushBoundary {
startX: number;
Expand Down Expand Up @@ -309,17 +310,25 @@ function handleZoom(draft: Draft<State>, action: ZoomAction) {
toolOptions: { selectedTool },
} = draft;
const scaleRatio = toScaleRatio(event);

switch (displayerMode) {
case '2D': {
// change the vertical scale of 1D traces
const index =
trackID === LAYOUT.TOP_1D ? 0 : trackID === LAYOUT.LEFT_1D ? 1 : null;
if (index !== null) {
const id = getSpectrumID(draft, index);
if (id) {
const domain = yDomains[id];
yDomains[id] = wheelZoom(event, domain);
// change the vertical scale for traces in 2D phase correction
if (
selectedTool === 'phaseCorrectionTwoDimensions' &&
trackID === 'CENTER_2D'
) {
const { activeTraces } = getTwoDimensionPhaseCorrectionOptions(draft);
activeTraces.scaleRatio *= scaleRatio;
} else {
// change the vertical scale of 1D traces
const index =
trackID === LAYOUT.TOP_1D ? 0 : trackID === LAYOUT.LEFT_1D ? 1 : null;
if (index !== null) {
const id = getSpectrumID(draft, index);
if (id) {
const domain = yDomains[id];
yDomains[id] = wheelZoom(event, domain);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/component/reducer/helper/Zoom1DManager.ts
Expand Up @@ -14,7 +14,7 @@ export const ZOOM_TYPES = {

export type ZoomType = keyof typeof ZOOM_TYPES;

export function toScaleRatio(event: WheelEvent, zoomOptions: ZoomOptions = {}) {
function toScaleRatio(event: WheelEvent, zoomOptions: ZoomOptions = {}) {
const { factor = 1, invert = false } = zoomOptions;

const deltaY =
Expand Down Expand Up @@ -96,4 +96,4 @@ function setZoom(
}
}

export { setZoom, wheelZoom };
export { setZoom, wheelZoom, toScaleRatio };
@@ -0,0 +1,20 @@
import { Draft } from 'immer';

import { State } from '../Reducer';

export function getTwoDimensionPhaseCorrectionOptions(
state: Draft<State> | State,
) {
const {
toolOptions: {
data: {
twoDimensionPhaseCorrection: { traces, activeTraceDirection },
},
},
} = state;
return {
activeTraces: traces[activeTraceDirection],
traces,
activeTraceDirection,
};
}

0 comments on commit 9cb5a1f

Please sign in to comment.