-
Notifications
You must be signed in to change notification settings - Fork 7
NextJsWithCornerstone3D4Version #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
radhe65gupta
wants to merge
2
commits into
cornerstonejs:main
Choose a base branch
from
radhe65gupta:NextJsWithCornerstone3D4Version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| "use client" | ||
|
|
||
| import { useEffect, useRef } from "react" | ||
| import createImageIdsAndCacheMetaData from "../lib/createImageIdsAndCacheMetaData" | ||
| import { | ||
| RenderingEngine, | ||
| Enums, | ||
| type Types, | ||
| volumeLoader, | ||
| } from "@cornerstonejs/core" | ||
| import { init as csRenderInit } from "@cornerstonejs/core" | ||
| import { init as csToolsInit } from "@cornerstonejs/tools" | ||
| import { init as dicomImageLoaderInit } from "@cornerstonejs/dicom-image-loader" | ||
|
|
||
|
|
||
| function Viewer() { | ||
| const elementRef = useRef<HTMLDivElement>(null) | ||
| const running = useRef(false) | ||
|
|
||
| useEffect(() => { | ||
| const setup = async () => { | ||
| if (running.current) { | ||
| return | ||
| } | ||
| running.current = true | ||
|
|
||
| await csRenderInit() | ||
| await csToolsInit() | ||
| dicomImageLoaderInit({ maxWebWorkers: 1 }) | ||
|
|
||
| // Get Cornerstone imageIds and fetch metadata into RAM | ||
| const imageIds = await createImageIdsAndCacheMetaData({ | ||
| StudyInstanceUID: | ||
| "1.3.6.1.4.1.14519.5.2.1.7009.2403.334240657131972136850343327463", | ||
| SeriesInstanceUID: | ||
| "1.3.6.1.4.1.14519.5.2.1.7009.2403.226151125820845824875394858561", | ||
| wadoRsRoot: "https://d14fa38qiwhyfd.cloudfront.net/dicomweb", | ||
| }) | ||
|
|
||
| // Instantiate a rendering engine | ||
| const renderingEngineId = "myRenderingEngine" | ||
| const renderingEngine = new RenderingEngine(renderingEngineId) | ||
| const viewportId = "CT" | ||
|
|
||
| const viewportInput = { | ||
| viewportId, | ||
| type: Enums.ViewportType.ORTHOGRAPHIC, | ||
| element: elementRef.current, | ||
| defaultOptions: { | ||
| orientation: Enums.OrientationAxis.SAGITTAL, | ||
| }, | ||
| } | ||
|
|
||
| renderingEngine.enableElement(viewportInput) | ||
|
|
||
| // Get the stack viewport that was created | ||
| const viewport = renderingEngine.getViewport( | ||
| viewportId | ||
| ) as Types.IVolumeViewport | ||
|
|
||
| // Define a volume in memory | ||
| const volumeId = "streamingImageVolume" | ||
| const volume = await volumeLoader.createAndCacheVolume(volumeId, { | ||
| imageIds, | ||
| }) | ||
|
|
||
| // Set the volume to load | ||
| // @ts-ignore | ||
| volume.load() | ||
|
|
||
| // Set the volume on the viewport and it's default properties | ||
| await viewport.setVolumes([{ volumeId }]) | ||
|
|
||
| // Render the image | ||
| viewport.render() | ||
| } | ||
|
|
||
| setup() | ||
|
|
||
| // Create a stack viewport | ||
| }, [elementRef, running]) | ||
|
|
||
| return ( | ||
| <div | ||
| ref={elementRef} | ||
| style={{ | ||
| width: "512px", | ||
| height: "512px", | ||
| backgroundColor: "#000", | ||
| }} | ||
| ></div> | ||
| ) | ||
| } | ||
|
|
||
| export default Viewer | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,9 @@ | ||
| "use client" | ||
|
|
||
| import { useEffect, useRef } from "react" | ||
| import createImageIdsAndCacheMetaData from "../lib/createImageIdsAndCacheMetaData" | ||
| import { | ||
| RenderingEngine, | ||
| Enums, | ||
| type Types, | ||
| volumeLoader, | ||
| } from "@cornerstonejs/core" | ||
| import { init as csRenderInit } from "@cornerstonejs/core" | ||
| import { init as csToolsInit } from "@cornerstonejs/tools" | ||
| import { init as dicomImageLoaderInit } from "@cornerstonejs/dicom-image-loader" | ||
| import dynamic from "next/dynamic" | ||
|
|
||
| const Viewer = dynamic(() => import("./Viewer"), { ssr: false }) | ||
|
|
||
| function App() { | ||
| const elementRef = useRef<HTMLDivElement>(null) | ||
| const running = useRef(false) | ||
|
|
||
| useEffect(() => { | ||
| const setup = async () => { | ||
| if (running.current) { | ||
| return | ||
| } | ||
| running.current = true | ||
|
|
||
| await csRenderInit() | ||
| await csToolsInit() | ||
| dicomImageLoaderInit({ maxWebWorkers: 1 }) | ||
|
|
||
| // Get Cornerstone imageIds and fetch metadata into RAM | ||
| const imageIds = await createImageIdsAndCacheMetaData({ | ||
| StudyInstanceUID: | ||
| "1.3.6.1.4.1.14519.5.2.1.7009.2403.334240657131972136850343327463", | ||
| SeriesInstanceUID: | ||
| "1.3.6.1.4.1.14519.5.2.1.7009.2403.226151125820845824875394858561", | ||
| wadoRsRoot: "https://d3t6nz73ql33tx.cloudfront.net/dicomweb", | ||
| }) | ||
|
|
||
| // Instantiate a rendering engine | ||
| const renderingEngineId = "myRenderingEngine" | ||
| const renderingEngine = new RenderingEngine(renderingEngineId) | ||
| const viewportId = "CT" | ||
|
|
||
| const viewportInput = { | ||
| viewportId, | ||
| type: Enums.ViewportType.ORTHOGRAPHIC, | ||
| element: elementRef.current, | ||
| defaultOptions: { | ||
| orientation: Enums.OrientationAxis.SAGITTAL, | ||
| }, | ||
| } | ||
|
|
||
| renderingEngine.enableElement(viewportInput) | ||
|
|
||
| // Get the stack viewport that was created | ||
| const viewport = renderingEngine.getViewport( | ||
| viewportId | ||
| ) as Types.IVolumeViewport | ||
|
|
||
| // Define a volume in memory | ||
| const volumeId = "streamingImageVolume" | ||
| const volume = await volumeLoader.createAndCacheVolume(volumeId, { | ||
| imageIds, | ||
| }) | ||
|
|
||
| // Set the volume to load | ||
| // @ts-ignore | ||
| volume.load() | ||
|
|
||
| // Set the volume on the viewport and it's default properties | ||
| viewport.setVolumes([{ volumeId }]) | ||
|
|
||
| // Render the image | ||
| viewport.render() | ||
| } | ||
|
|
||
| setup() | ||
|
|
||
| // Create a stack viewport | ||
| }, [elementRef, running]) | ||
|
|
||
| return ( | ||
| <div | ||
| ref={elementRef} | ||
| style={{ | ||
| width: "512px", | ||
| height: "512px", | ||
| backgroundColor: "#000", | ||
| }} | ||
| ></div> | ||
| ) | ||
| export default function Page() { | ||
| return <Viewer /> | ||
| } | ||
|
|
||
| export default App |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: cornerstonejs/nextjs-cornerstone3d
Length of output: 3937
🏁 Script executed:
Repository: cornerstonejs/nextjs-cornerstone3d
Length of output: 551
🏁 Script executed:
Repository: cornerstonejs/nextjs-cornerstone3d
Length of output: 6148
🏁 Script executed:
Repository: cornerstonejs/nextjs-cornerstone3d
Length of output: 297
🏁 Script executed:
Repository: cornerstonejs/nextjs-cornerstone3d
Length of output: 226
🏁 Script executed:
Repository: cornerstonejs/nextjs-cornerstone3d
Length of output: 2532
Return cleanup from the effect + reset
running.currenton init failure to avoid leaks and “stuck” init.In
app/Viewer.tsx,running.currentis set totrueand never cleared if any awaited init step throws (no try/catch), so the viewer can get stuck in the “already running” state. Also, the effect doesn’t return a cleanup, so the createdRenderingEngineis never torn down on unmount—yetRenderingEnginein@cornerstonejs/coreprovides adestroy()API and the repo currently has nodestroy()calls.♻️ Proposed fix
useEffect(() => { + let cancelled = false + let renderingEngine: RenderingEngine | undefined + const setup = async () => { - if (running.current) { + if (running.current || !elementRef.current) { return } + running.current = true - - await csRenderInit() - await csToolsInit() - dicomImageLoaderInit({ maxWebWorkers: 1 }) - - // Get Cornerstone imageIds and fetch metadata into RAM - const imageIds = await createImageIdsAndCacheMetaData({ - StudyInstanceUID: - "1.3.6.1.4.1.14519.5.2.1.7009.2403.334240657131972136850343327463", - SeriesInstanceUID: - "1.3.6.1.4.1.14519.5.2.1.7009.2403.226151125820845824875394858561", - wadoRsRoot: "https://d14fa38qiwhyfd.cloudfront.net/dicomweb", - }) - - // Instantiate a rendering engine - const renderingEngineId = "myRenderingEngine" - const renderingEngine = new RenderingEngine(renderingEngineId) - const viewportId = "CT" - - const viewportInput = { - viewportId, - type: Enums.ViewportType.ORTHOGRAPHIC, - element: elementRef.current, - defaultOptions: { - orientation: Enums.OrientationAxis.SAGITTAL, - }, - } - - renderingEngine.enableElement(viewportInput) - - // Get the stack viewport that was created - const viewport = renderingEngine.getViewport( - viewportId - ) as Types.IVolumeViewport - - // Define a volume in memory - const volumeId = "streamingImageVolume" - const volume = await volumeLoader.createAndCacheVolume(volumeId, { - imageIds, - }) - - // Set the volume to load - // `@ts-ignore` - volume.load() - - // Set the volume on the viewport and it's default properties - await viewport.setVolumes([{ volumeId }]) - - // Render the image - viewport.render() + + try { + await csRenderInit() + await csToolsInit() + dicomImageLoaderInit({ maxWebWorkers: 1 }) + + const imageIds = await createImageIdsAndCacheMetaData({ + StudyInstanceUID: + "1.3.6.1.4.1.14519.5.2.1.7009.2403.334240657131972136850343327463", + SeriesInstanceUID: + "1.3.6.1.4.1.14519.5.2.1.7009.2403.226151125820845824875394858561", + wadoRsRoot: "https://d14fa38qiwhyfd.cloudfront.net/dicomweb", + }) + + const renderingEngineId = "myRenderingEngine" + renderingEngine = new RenderingEngine(renderingEngineId) + const viewportId = "CT" + + renderingEngine.enableElement({ + viewportId, + type: Enums.ViewportType.ORTHOGRAPHIC, + element: elementRef.current, + defaultOptions: { + orientation: Enums.OrientationAxis.SAGITTAL, + }, + }) + + const viewport = renderingEngine.getViewport( + viewportId + ) as Types.IVolumeViewport + + const volumeId = "streamingImageVolume" + const volume = await volumeLoader.createAndCacheVolume(volumeId, { + imageIds, + }) + + // `@ts-ignore` + volume.load() + + if (cancelled) { + return + } + + await viewport.setVolumes([{ volumeId }]) + + if (!cancelled) { + viewport.render() + } + } catch (error) { + running.current = false + throw error + } } - setup() + void setup() - // Create a stack viewport - }, [elementRef, running]) + return () => { + cancelled = true + running.current = false + renderingEngine?.destroy() + } + }, [])🤖 Prompt for AI Agents