-
Notifications
You must be signed in to change notification settings - Fork 25
Measures support (with toggle button) #819
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
asmith26
wants to merge
24
commits into
jupytercad:main
Choose a base branch
from
asmith26:measures_support_ruler
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 11 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e08bc30
Add measure support
asmith26 87aa35b
Measure -> Measurement
asmith26 1a5b1c6
Merge branch 'jupytercad:main' into measures_support
asmith26 f08452f
Add comment
asmith26 7c23ef9
Add toggle measurement button
asmith26 1570e31
Increase size of ruler image
asmith26 e14281f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 79cd9fc
Merge branch 'jupytercad:main' into measures_support_ruler
asmith26 bc5f431
Fix test
asmith26 dcac414
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 35fa2ee
Update failing snapshots
asmith26 e8fcd72
Update snapshots
asmith26 3194256
Update snapshots
asmith26 58269ed
Update snapshots
asmith26 73ed175
React -> Class
asmith26 cae7af3
Remove redundant code, add comment
asmith26 a4e7535
Fix measurements not refreshing on delete/move
asmith26 4abf570
Fix: align bounding box to the object only when measuring a single ob…
asmith26 5707339
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d2cd181
Update comment
asmith26 56fdfca
Hide 0 axes
asmith26 9329bbe
Improved dashed line
asmith26 c4377ea
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6e6f135
Update snapshot
asmith26 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 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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /** | ||
| * This file defines a React component for rendering measurements of a 3D object. | ||
| * It uses `three.js` to create dimension lines and labels for a given bounding box. | ||
| */ | ||
| import * as React from 'react'; | ||
| import * as THREE from 'three'; | ||
| import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js'; | ||
|
|
||
| /** | ||
| * Props for the Measurement component. | ||
| */ | ||
| interface IMeasurementProps { | ||
| /** | ||
| * The bounding box to measure. | ||
| */ | ||
| box: THREE.Box3; | ||
| } | ||
|
|
||
| /** | ||
| * A React component that displays the dimensions of a THREE.Box3. | ||
| * It creates visual annotations (lines and labels) for the X, Y, and Z dimensions. | ||
| */ | ||
| export class Measurement extends React.Component<IMeasurementProps> { | ||
| private _group: THREE.Group; | ||
|
|
||
| /** | ||
| * Constructor for the Measurement component. | ||
| * @param props The component props. | ||
| */ | ||
| constructor(props: IMeasurementProps) { | ||
| super(props); | ||
| this._group = new THREE.Group(); | ||
| this.createAnnotations(); | ||
| } | ||
|
|
||
| /** | ||
| * Called when the component updates. | ||
| * If the bounding box has changed, it clears the old annotations and creates new ones. | ||
| * @param prevProps The previous component props. | ||
| */ | ||
| componentDidUpdate(prevProps: IMeasurementProps) { | ||
| if (this.props.box !== prevProps.box) { | ||
| this.clearAnnotations(); | ||
| this.createAnnotations(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Called when the component is about to be unmounted. | ||
| * It clears any existing annotations. | ||
| */ | ||
| componentWillUnmount() { | ||
| this.clearAnnotations(); | ||
| } | ||
|
|
||
| /** | ||
| * Removes all annotations from the scene. | ||
| */ | ||
| clearAnnotations() { | ||
| this._group.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates the dimension lines and labels for the bounding box. | ||
| */ | ||
| createAnnotations() { | ||
| const { box } = this.props; | ||
| if (!box) { | ||
| return; | ||
| } | ||
|
|
||
| const size = new THREE.Vector3(); | ||
| box.getSize(size); | ||
|
|
||
| const min = box.min; | ||
| const max = box.max; | ||
|
|
||
| // Create dimension lines for X, Y, and Z axes | ||
| this.createDimensionLine( | ||
| new THREE.Vector3(min.x, min.y, min.z), | ||
| new THREE.Vector3(max.x, min.y, min.z), | ||
| 'X', | ||
| size.x | ||
| ); | ||
| this.createDimensionLine( | ||
| new THREE.Vector3(max.x, min.y, min.z), | ||
| new THREE.Vector3(max.x, max.y, min.z), | ||
| 'Y', | ||
| size.y | ||
| ); | ||
| this.createDimensionLine( | ||
| new THREE.Vector3(max.x, max.y, min.z), | ||
| new THREE.Vector3(max.x, max.y, max.z), | ||
| 'Z', | ||
| size.z | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a single dimension line with a label. | ||
| * @param start The start point of the line. | ||
| * @param end The end point of the line. | ||
| * @param axis The axis name ('X', 'Y', or 'Z'). | ||
| * @param value The length of the dimension. | ||
| */ | ||
| createDimensionLine( | ||
| start: THREE.Vector3, | ||
| end: THREE.Vector3, | ||
| axis: string, | ||
| value: number | ||
| ) { | ||
| // Create the dashed line | ||
| const material = new THREE.LineDashedMaterial({ | ||
| color: 0x000000, | ||
| linewidth: 1, | ||
| scale: 1, | ||
| dashSize: 0.1, | ||
| gapSize: 0.1 | ||
| }); | ||
| const geometry = new THREE.BufferGeometry().setFromPoints([start, end]); | ||
| const line = new THREE.Line(geometry, material); | ||
| line.computeLineDistances(); | ||
| this._group.add(line); | ||
|
|
||
| // Create the label | ||
| const labelDiv = document.createElement('div'); | ||
| labelDiv.className = 'measurement-label'; | ||
| labelDiv.textContent = `${axis}: ${value.toFixed(2)}`; | ||
| labelDiv.style.color = 'black'; | ||
| labelDiv.style.fontSize = '12px'; | ||
| labelDiv.style.backgroundColor = 'rgba(255, 255, 255, 0.7)'; | ||
| labelDiv.style.padding = '2px 5px'; | ||
| labelDiv.style.borderRadius = '3px'; | ||
|
|
||
| const label = new CSS2DObject(labelDiv); | ||
|
|
||
| // Position the label at the midpoint of the line | ||
| const midPoint = new THREE.Vector3() | ||
| .addVectors(start, end) | ||
| .multiplyScalar(0.5); | ||
| label.position.copy(midPoint); | ||
|
|
||
| this._group.add(label); | ||
| } | ||
|
|
||
| /** | ||
| * This component does not render any DOM elements itself. | ||
| * The measurements are rendered in the 3D scene. | ||
| */ | ||
| render(): null { | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Getter for the THREE.Group containing the measurement annotations. | ||
| * This group can be added to a THREE.Scene to be rendered. | ||
| */ | ||
| public get group(): THREE.Group { | ||
| return this._group; | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.