11/**
2- * This file defines a React component for rendering measurements of a 3D object.
2+ * This file defines a class for rendering measurements of a 3D object.
33 * It uses `three.js` to create dimension lines and labels for a given bounding box.
44 */
5- import * as React from 'react' ;
65import * as THREE from 'three' ;
76import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js' ;
87
98/**
10- * Props for the Measurement component.
11- */
12- interface IMeasurementProps {
13- /**
14- * The bounding box to measure.
15- */
16- box : THREE . Box3 ;
17- }
18-
19- /**
20- * A React component that displays the dimensions of a THREE.Box3.
9+ * A class that displays the dimensions of a THREE.Box3.
2110 * It creates visual annotations (lines and labels) for the X, Y, and Z dimensions.
2211 */
23- export class Measurement extends React . Component < IMeasurementProps > {
12+ export class Measurement {
2413 private _group : THREE . Group ;
14+ private _box : THREE . Box3 ;
2515
2616 /**
27- * Constructor for the Measurement component .
28- * @param props The component props .
17+ * Constructor for the Measurement class .
18+ * @param box The bounding box to measure .
2919 */
30- constructor ( props : IMeasurementProps ) {
31- super ( props ) ;
20+ constructor ( box : THREE . Box3 ) {
21+ this . _box = box ;
3222 this . _group = new THREE . Group ( ) ;
3323 this . createAnnotations ( ) ;
3424 }
3525
36- /**
37- * Called when the component updates.
38- * If the bounding box has changed, it clears the old annotations and creates new ones.
39- * @param prevProps The previous component props.
40- */
41- componentDidUpdate ( prevProps : IMeasurementProps ) {
42- if ( this . props . box !== prevProps . box ) {
43- this . clearAnnotations ( ) ;
44- this . createAnnotations ( ) ;
45- }
46- }
47-
48- /**
49- * Called when the component is about to be unmounted.
50- * It clears any existing annotations.
51- */
52- componentWillUnmount ( ) {
53- this . clearAnnotations ( ) ;
54- }
55-
5626 /**
5727 * Removes all annotations from the scene.
5828 */
@@ -64,16 +34,15 @@ export class Measurement extends React.Component<IMeasurementProps> {
6434 * Creates the dimension lines and labels for the bounding box.
6535 */
6636 createAnnotations ( ) {
67- const { box } = this . props ;
68- if ( ! box ) {
37+ if ( ! this . _box ) {
6938 return ;
7039 }
7140
7241 const size = new THREE . Vector3 ( ) ;
73- box . getSize ( size ) ;
42+ this . _box . getSize ( size ) ;
7443
75- const min = box . min ;
76- const max = box . max ;
44+ const min = this . _box . min ;
45+ const max = this . _box . max ;
7746
7847 // Create dimension lines for X, Y, and Z axes
7948 this . createDimensionLine (
@@ -143,14 +112,6 @@ export class Measurement extends React.Component<IMeasurementProps> {
143112 this . _group . add ( label ) ;
144113 }
145114
146- /**
147- * This component does not render any DOM elements itself.
148- * The measurements are rendered in the 3D scene.
149- */
150- render ( ) : null {
151- return null ;
152- }
153-
154115 /**
155116 * Getter for the THREE.Group containing the measurement annotations.
156117 * This group can be added to a THREE.Scene to be rendered.
0 commit comments