Analysis of commit 846ffc9
Assignee: @copilot
Summary
Two distinct structural duplication patterns were found in the source files:
selectResolution and select2DResolution share nearly identical region-computation blocks in both their inner loop and fallback paths — 4 occurrences of the same ~14-line block.
roundPixelCoord / floorPixelCoord / ceilPixelCoord are structurally identical element-wise mapping functions differing only in the Math.* operation applied.
Pattern 1 — Duplicated region-computation block in ResolutionSelector.ts
- Severity: Medium
- Occurrences: 4 (2 inside each function's loop, 2 in fallback paths)
- Locations:
fidnii/src/ResolutionSelector.ts lines 47–59 (selectResolution loop body)
fidnii/src/ResolutionSelector.ts lines 74–88 (selectResolution fallback)
fidnii/src/ResolutionSelector.ts lines 282–296 (select2DResolution loop body)
fidnii/src/ResolutionSelector.ts lines 316–330 (select2DResolution fallback)
Duplicated block (~14 lines, repeated 4×):
const region = clipPlanesToPixelRegion(
clipPlanes,
volumeBounds,
image, // or lowestImage in fallback
viewportBounds,
)
const alignedRegion = alignRegionToChunks(region, image)
const dimensions: [number, number, number] = [
alignedRegion.end[0] - alignedRegion.start[0],
alignedRegion.end[1] - alignedRegion.start[1],
alignedRegion.end[2] - alignedRegion.start[2],
]
Root cause: select2DResolution is a near-copy of selectResolution with only the pixel-budget calculation differing (3D voxel count vs. in-plane × slab depth).
Refactoring Recommendation
Extract a shared helper that computes the aligned dimensions for a given resolution image:
function computeAlignedDimensions(
image: MultiscaleImage,
clipPlanes: ClipPlanes,
volumeBounds: VolumeBounds,
viewportBounds?: VolumeBounds,
): [number, number, number] {
const region = clipPlanesToPixelRegion(clipPlanes, volumeBounds, image, viewportBounds)
const aligned = alignRegionToChunks(region, image)
return [
aligned.end[0] - aligned.start[0],
aligned.end[1] - aligned.start[1],
aligned.end[2] - aligned.start[2],
]
}
Then accept a pixelCountFn: (dims: [number, number, number], image: MultiscaleImage) => number callback in a unified selectResolutionWithBudget helper, or simply call computeAlignedDimensions from both functions.
Pattern 2 — Structurally identical element-wise pixel-coordinate functions in coordinates.ts
- Severity: Low
- Occurrences: 3
- Locations:
fidnii/src/utils/coordinates.ts lines 232–240 (roundPixelCoord)
fidnii/src/utils/coordinates.ts lines 248–256 (floorPixelCoord)
fidnii/src/utils/coordinates.ts lines 264–272 (ceilPixelCoord)
Duplicated structure (9 lines each):
export function (name)PixelCoord(
pixelCoord: [number, number, number],
): [number, number, number] {
return [
Math.(op)(pixelCoord[0]),
Math.(op)(pixelCoord[1]),
Math.(op)(pixelCoord[2]),
]
}
Refactoring Recommendation
Introduce a private helper and re-implement:
function mapPixelCoord(
fn: (v: number) => number,
coord: [number, number, number],
): [number, number, number] {
return [fn(coord[0]), fn(coord[1]), fn(coord[2])]
}
export const roundPixelCoord = (c: [number, number, number]) => mapPixelCoord(Math.round, c)
export const floorPixelCoord = (c: [number, number, number]) => mapPixelCoord(Math.floor, c)
export const ceilPixelCoord = (c: [number, number, number]) => mapPixelCoord(Math.ceil, c)
This keeps the public API identical while eliminating the structural repetition.
Impact Analysis
- Maintainability: Any change to the region-computation logic in
selectResolution must be mirrored manually in select2DResolution, and any change to the pixel-budget block must be replicated across both fallback paths.
- Bug Risk: A bug fix applied to one copy will silently leave the other copies broken.
- Code Bloat: ~56 lines of duplicated region-computation code; ~18 lines of duplicated element-wise mapping boilerplate.
Implementation Checklist
Analysis Metadata
- Analyzed Files: 14 source files in
fidnii/src/ (excluding test files and workflow files)
- Detection Method: Serena semantic code analysis + symbol body inspection
- Commit: 846ffc9
- Analysis Date: 2026-04-15
Generated by Duplicate Code Detector
To install this agentic workflow, run
gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@33cd6c7f1fee588654ef19def2e6a4174be66197
Analysis of commit 846ffc9
Assignee:
@copilotSummary
Two distinct structural duplication patterns were found in the source files:
selectResolutionandselect2DResolutionshare nearly identical region-computation blocks in both their inner loop and fallback paths — 4 occurrences of the same ~14-line block.roundPixelCoord/floorPixelCoord/ceilPixelCoordare structurally identical element-wise mapping functions differing only in theMath.*operation applied.Pattern 1 — Duplicated region-computation block in
ResolutionSelector.tsfidnii/src/ResolutionSelector.tslines 47–59 (selectResolutionloop body)fidnii/src/ResolutionSelector.tslines 74–88 (selectResolutionfallback)fidnii/src/ResolutionSelector.tslines 282–296 (select2DResolutionloop body)fidnii/src/ResolutionSelector.tslines 316–330 (select2DResolutionfallback)Duplicated block (~14 lines, repeated 4×):
Root cause:
select2DResolutionis a near-copy ofselectResolutionwith only the pixel-budget calculation differing (3D voxel count vs. in-plane × slab depth).Refactoring Recommendation
Extract a shared helper that computes the aligned dimensions for a given resolution image:
Then accept a
pixelCountFn: (dims: [number, number, number], image: MultiscaleImage) => numbercallback in a unifiedselectResolutionWithBudgethelper, or simply callcomputeAlignedDimensionsfrom both functions.Pattern 2 — Structurally identical element-wise pixel-coordinate functions in
coordinates.tsfidnii/src/utils/coordinates.tslines 232–240 (roundPixelCoord)fidnii/src/utils/coordinates.tslines 248–256 (floorPixelCoord)fidnii/src/utils/coordinates.tslines 264–272 (ceilPixelCoord)Duplicated structure (9 lines each):
Refactoring Recommendation
Introduce a private helper and re-implement:
This keeps the public API identical while eliminating the structural repetition.
Impact Analysis
selectResolutionmust be mirrored manually inselect2DResolution, and any change to the pixel-budget block must be replicated across both fallback paths.Implementation Checklist
computeAlignedDimensionshelper inResolutionSelector.tsselectResolutionandselect2DResolutionto use itmapPixelCoordhelper incoordinates.tsand simplify the three public wrappersbunx tsc --noEmitto verify no type errorsbun run checkto verify lint/formatbun run testAnalysis Metadata
fidnii/src/(excluding test files and workflow files)