Skip to content

Commit

Permalink
feat: add phase correction trace to both directions
Browse files Browse the repository at this point in the history
close #2936
  • Loading branch information
hamed-musallam committed Mar 4, 2024
1 parent 1a21386 commit 7e20bfd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export function useActivePhaseTraces() {
const {
toolOptions: {
data: {
twoDimensionPhaseCorrection: { traces, activeTraceDirection },
twoDimensionPhaseCorrection: {
traces,
activeTraceDirection,
addTracesToBothDirections,
},
},
},
} = useChartData();
Expand All @@ -14,5 +18,6 @@ export function useActivePhaseTraces() {
...traces[activeTraceDirection],
activeTraceDirection,
color,
addTracesToBothDirections,
};
}
18 changes: 17 additions & 1 deletion src/component/header/PhaseCorrectionTwoDimensionsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Spectrum2D } from 'nmr-load-save';
import { Filters } from 'nmr-processing';
import { useCallback, useEffect, useRef, useState } from 'react';
import { FaRulerHorizontal, FaRulerVertical } from 'react-icons/fa';
import { MdLooksTwo } from 'react-icons/md';
import { Toolbar } from 'react-science/ui';

import { useActivePhaseTraces } from '../2d/1d-tracer/phase-correction-traces/useActivePhaseTraces';
Expand Down Expand Up @@ -34,7 +35,8 @@ const inputStyle: InputStyle = {
const emptyData = { datum: {}, filter: null };

export default function PhaseCorrectionTwoDimensionsPanel() {
const { activeTraceDirection, pivot } = useActivePhaseTraces();
const { activeTraceDirection, pivot, addTracesToBothDirections } =
useActivePhaseTraces();

const { data } = useSpectrum(emptyData) as Spectrum2D;
const filter = useFilter(Filters.phaseCorrectionTwoDimensions.id);
Expand Down Expand Up @@ -154,6 +156,10 @@ export default function PhaseCorrectionTwoDimensionsPanel() {
dispatch({ type: 'APPLY_MANUAL_PHASE_CORRECTION_TOW_DIMENSION_FILTER' });
}

function handleToggleAddTraceToBothDirections() {
dispatch({ type: 'TOGGLE_ADD_PHASE_CORRECTION_TRACE_TO_BOTH_DIRECTIONS' });
}

return (
<HeaderContainer style={{ padding: '0 5px' }}>
<Label title="Direction :" style={headerLabelStyle}>
Expand All @@ -178,6 +184,16 @@ export default function PhaseCorrectionTwoDimensionsPanel() {
/>
</Toolbar>
</Label>
<div style={{ paddingRight: '5px' }}>
<Toolbar>
<Toolbar.Item
title="Add the trace in both directions"
icon={<MdLooksTwo />}
active={addTracesToBothDirections}
onClick={handleToggleAddTraceToBothDirections}
/>
</Toolbar>
</div>

<Label title="PH0 :" style={headerLabelStyle}>
<Input
Expand Down
4 changes: 4 additions & 0 deletions src/component/reducer/Reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface PhaseCorrectionTraceData {
export interface TwoDimensionPhaseCorrection {
traces: Record<TraceDirection, PhaseCorrectionTraceData>;
activeTraceDirection: TraceDirection;
addTracesToBothDirections: boolean;
}

export const getDefaultTwoDimensionsPhaseCorrectionTraceOptions =
Expand Down Expand Up @@ -160,6 +161,7 @@ export const getInitialState = (): State => ({
apodizationOptions: {} as ApodizationOptions,
twoDimensionPhaseCorrection: {
activeTraceDirection: 'horizontal',
addTracesToBothDirections: true,
traces: getDefaultTwoDimensionsPhaseCorrectionTraceOptions(),
},
pivot: { value: 0, index: 0 },
Expand Down Expand Up @@ -497,6 +499,8 @@ function innerSpectrumReducer(draft: Draft<State>, action: Action) {
draft,
action,
);
case 'TOGGLE_ADD_PHASE_CORRECTION_TRACE_TO_BOTH_DIRECTIONS':
return FiltersActions.handleToggleAddTracesToBothDirections(draft);
case 'DELETE_PHASE_CORRECTION_TRACE':
return FiltersActions.handleDeletePhaseCorrectionTrace(draft, action);
case 'SET_ONE_DIMENSION_PIVOT_POINT':
Expand Down
32 changes: 25 additions & 7 deletions src/component/reducer/actions/FiltersActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export type FiltersActions =
| 'APPLY_AUTO_PHASE_CORRECTION_FILTER'
| 'APPLY_ABSOLUTE_FILTER'
| 'APPLY_MANUAL_PHASE_CORRECTION_TOW_DIMENSION_FILTER'
| 'TOGGLE_ADD_PHASE_CORRECTION_TRACE_TO_BOTH_DIRECTIONS'
>;

function getFilterUpdateDomainRules(
Expand Down Expand Up @@ -843,7 +844,8 @@ function handleAddPhaseCorrectionTrace(
data: spectra,
} = draft;

const { activeTraces } = getTwoDimensionPhaseCorrectionOptions(draft);
const { activeTraces, traces, addTracesToBothDirections } =
getTwoDimensionPhaseCorrectionOptions(draft);

if (activeSpectrum?.id) {
const spectrum = spectra[activeSpectrum.index] as Spectrum2D;
Expand All @@ -853,15 +855,30 @@ function handleAddPhaseCorrectionTrace(
const scale2dY = get2DYScale({ margin, height, yDomain });
const xPPM = scale2dX.invert(x);
const yPPM = scale2dY.invert(y);

activeTraces.spectra.push({
id: v4(),
x: xPPM,
y: yPPM,
});
if (addTracesToBothDirections) {
for (const direction of Object.keys(traces)) {
traces[direction].spectra.push({
id: v4(),
x: xPPM,
y: yPPM,
});
}
} else {
activeTraces.spectra.push({
id: v4(),
x: xPPM,
y: yPPM,
});
}
}
}
}
//action
function handleToggleAddTracesToBothDirections(draft: Draft<State>) {
const options = draft.toolOptions.data.twoDimensionPhaseCorrection;
options.addTracesToBothDirections = !options.addTracesToBothDirections;
}

//action
function handleChangePhaseCorrectionDirection(
draft: Draft<State>,
Expand Down Expand Up @@ -1428,4 +1445,5 @@ export {
handleSetOneDimensionPhaseCorrectionPivotPoint,
handleSetTwoDimensionPhaseCorrectionPivotPoint,
handleCalculateManualTwoDimensionPhaseCorrection,
handleToggleAddTracesToBothDirections,
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ export function getTwoDimensionPhaseCorrectionOptions(
const {
toolOptions: {
data: {
twoDimensionPhaseCorrection: { traces, activeTraceDirection },
twoDimensionPhaseCorrection: {
traces,
activeTraceDirection,
addTracesToBothDirections,
},
},
},
} = state;
return {
activeTraces: traces[activeTraceDirection],
traces,
activeTraceDirection,
addTracesToBothDirections,
};
}

Expand Down

0 comments on commit 7e20bfd

Please sign in to comment.