Skip to content
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

VV: Add VolumetricViewer to lib-subject-viewers #6349

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/lib-subject-viewers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@
"storybook": "storybook dev -p 6008",
"build-storybook": "storybook build",
"test": "mocha --config ./test/.mocharc.json ./.storybook/specConfig.js \"./src/**/*.spec.js\"",
"test:ci": "mocha --config ./test/.mocharc.json ./.storybook/specConfig.js --reporter=min \"./src/**/*.spec.js\""
"test:ci": "mocha --config ./test/.mocharc.json ./.storybook/specConfig.js --reporter=min \"./src/**/*.spec.js\"",
"watch": "watch 'yarn build' ./src",
"watch:test": "watch 'yarn test' ./src"
},
"dependencies": {
"buffer": "^6.0.3",
"three": "^0.162.0"
},
"dependencies": {},
"peerDependencies": {
"@zooniverse/grommet-theme": "3.x.x",
"grommet": "2.x.x",
Expand All @@ -45,13 +50,15 @@
"@storybook/addon-a11y": "~7.6.11",
"@storybook/addon-essentials": "~7.6.11",
"@storybook/react": "~7.6.11",
"canvas": "^2.11.2",
"chai": "~4.5.0",
"chai-dom": "~1.12.0",
"dirty-chai": "~2.0.1",
"mocha": "~10.7.3",
"sinon": "~17.0.0",
"sinon-chai": "~3.7.0",
"storybook": "~7.6.11"
"storybook": "~7.6.11",
"watch": "^1.0.2"
},
"engines": {
"node": ">=20.5"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# VolumetricViewer

This directory holds all the relevant code for rendering the VolumetricViewer. There are two primary exports:

- `VolumetricViewerComponent` - a React component for the VolumetricViewer
- `VolumetricViewerData` - a function that returns the data with instantiated models along with the React Component
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { object, string } from 'prop-types'
import { useEffect, useState } from 'react'
import { Buffer } from 'buffer'
import { ComponentViewer } from './components/ComponentViewer.js'
import { ModelViewer } from './models/ModelViewer.js'
import { ModelAnnotations } from './models/ModelAnnotations.js'
import { ModelTool } from './models/ModelTool.js'

export default function VolumetricViewerComponent ({
config = {},
subjectData = '',
subjectUrl = '',
models
}) {
const [data, setData] = useState(null)
if (!models) {
const [modelState] = useState({
annotations: ModelAnnotations(),
tool: ModelTool(),
viewer: ModelViewer()
})
models = modelState
}

// Figure out subject data
useEffect(() => {
if (subjectData !== '') {
setData(Buffer.from(subjectData, 'base64'))
} else if (subjectUrl !== '') {
fetch(subjectUrl)
.then((res) => res.json())
.then((data) => {
setData(Buffer.from(data, 'base64'))
})
} else {
console.log('No data to display')
}
}, [])

// Loading screen will always display if we have no subject data
if (!data || !models) return <div>Loading...</div>

return (
<ComponentViewer
config={config}
data={data}
models={models}
/>
)
}

export const VolumetricViewerData = ({ subjectData = '', subjectUrl = '' }) => {
return {
data: {
config: {},
subjectData,
subjectUrl,
models: {
annotations: ModelAnnotations(),
tool: ModelTool(),
viewer: ModelViewer()
}
},
component: VolumetricViewerComponent
}
}

VolumetricViewerComponent.propTypes = {
config: object,
subjectData: string,
subjectUrl: string,
models: object
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { array, number, object } from 'prop-types'

export const AnnotationView = ({ annotation, annotations, index }) => {
function annotationActive () {
annotations.actions.annotation.active({ index })
}

function annotationDelete (e) {
e.stopPropagation()
annotations.actions.annotation.remove({ index })
}

const color = annotations.config.activeAnnotation === index ? '#555' : '#222'

return (
<li
style={{ padding: '20px', backgroundColor: color }}
onClick={annotationActive}
>
<p>Label: {annotation.label}</p>
<p>Threshold: {annotation.threshold}</p>
<p>Points: {annotation.points.active.length}</p>
<p onClick={annotationDelete}>Delete Annotation</p>
</li>
)
}

AnnotationView.propTypes = {
annotation: object,
annotations: array,
index: number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { object } from 'prop-types'
import { AlgorithmAStar } from './../helpers/AlgorithmAStar.js'
import { Cube } from './Cube.js'
import { Plane } from './Plane.js'
import { Box } from 'grommet'

export const ComponentViewer = ({
data,
models
}) => {
// Initialize Annotations
if (models.annotations) {
models.annotations.initialize({
algorithm: AlgorithmAStar,
data: [], // will come from Caesar if they exist
viewer: models.viewer
})
}

// Initialize Tool
if (models.tool) {
models.tool.initialize({
annotations: models.annotations
})
}

// Initialize Viewer
if (models.viewer) {
models.viewer.initialize({
annotations: models.annotations,
data,
tool: models.tool
})
}

return (
<Box direction='row' style={{ maxWidth: '800px', padding: '20px' }}>
<Box flex>
{models.viewer.dimensions.map((dimensionName, dimension) => {
return (
<Plane
annotations={models.annotations}
dimension={dimension}
key={`dimension-${dimensionName}`}
tool={models.tool}
viewer={models.viewer}
/>
)
})}
</Box>
<Box flex>
<Cube
annotations={models.annotations}
tool={models.tool}
viewer={models.viewer}
/>
</Box>
</Box>
)
}

ComponentViewer.propTypes = {
data: object,
models: object
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { object } from 'prop-types'
import { useEffect, useState } from 'react'
import { AnnotationView } from './AnnotationView.js'
import { InputRangeDual } from './InputRangeDual.js'

export const Config = ({
annotations,
viewer
}) => {
const [_annotations, setAnnotations] = useState(annotations.annotations)

function annotationsChange ({ annotations }) {
setAnnotations([...annotations])
}

// State Change Management through useEffect()
useEffect(() => {
// State Listeners to bypass React rerenders
annotations.on('active:annotation', annotationsChange)
annotations.on('add:annotation', annotationsChange)
annotations.on('update:annotation', annotationsChange)
annotations.on('remove:annotation', annotationsChange)

return () => {
annotations.off('active:annotation', annotationsChange)
annotations.off('add:annotation', annotationsChange)
annotations.off('update:annotation', annotationsChange)
annotations.off('remove:annotation', annotationsChange)
}
}, [])

function downloadPoints () {
const rows = annotations.annotations.map((annotation) => {
return [
annotation.label,
annotation.threshold,
annotation.points.active.join('|'),
annotation.points.all.data.join('|')
]
})

rows.unshift([
'annotation name',
'annotation threshold',
'control points',
'connected points'
])
const csvContent =
'data:text/csv;charset=utf-8,' + rows.map((r) => r.join(',')).join('\n')
const encodedUri = encodeURI(csvContent)
const link = document.createElement('a')
link.setAttribute('href', encodedUri)
link.setAttribute('download', 'brainsweeper.csv')
document.body.appendChild(link)
link.click()
}

function saveScreenshot () {
viewer.saveScreenshot()
}

return (
<>
<h3 style={{ paddingBottom: '10px' }}>Volumetric File</h3>
<br />

<h3>Brightness Range</h3>
<InputRangeDual
valueMax={255}
valueMin={0}
valueMaxCurrent={viewer.threshold.max}
valueMinCurrent={viewer.threshold.min}
onChange={(min, max) => {
viewer.setThreshold({ min, max })
}}
/>
<br />
<br />

<button onClick={downloadPoints} style={{ marginBottom: '20px' }}>
Download Active Points
</button>

<button onClick={saveScreenshot} style={{ marginBottom: '20px' }}>
Save Screenshot
</button>

<ul>
{_annotations.map((annotation, index) => {
return (
<AnnotationView
annotation={annotation}
annotations={annotations}
index={index}
key={`annotation-${index}`}
/>
)
})}
</ul>
</>
)
}

Config.propTypes = {
annotations: object,
viewer: object
}
Loading