Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/painters/box-tracker-hit-simple.painter.ts is written in an unsupported language. File is not indexed.

0001 import {
0002   Object3D,
0003   Mesh,
0004   BoxGeometry,
0005   MeshBasicMaterial,
0006   Color,
0007 } from 'three';
0008 import { ComponentPainter } from './component-painter';
0009 import { BoxTrackerHitComponent } from '../model/box-tracker-hit.component';
0010 import { EntryComponent } from '../model/entry-component';
0011 
0012 /**
0013  * Alternative Painter class for rendering BoxTrackerHitComponent using individual Meshes.
0014  */
0015 export class BoxTrackerHitSimplePainter extends ComponentPainter {
0016   /** Array of Mesh objects representing hits */
0017   private hitMeshes: Mesh[] = [];
0018 
0019   private boxComponent: BoxTrackerHitComponent;
0020 
0021   /**
0022    * Constructs a new BoxTrackerHitAlternativePainter.
0023    *
0024    * @param parentNode - The Object3D node where the hit meshes will be added.
0025    * @param component - The BoxTrackerHitComponent containing the hit data.
0026    */
0027   constructor(parentNode: Object3D, component: EntryComponent) {
0028     super(parentNode, component);
0029 
0030     // Runtime type check
0031     if (component.type !== BoxTrackerHitComponent.type) {
0032       throw new Error('Invalid component type for BoxTrackerHitAlternativePainter');
0033     }
0034 
0035     this.boxComponent = component as BoxTrackerHitComponent;
0036 
0037     // Create a bright random color for this component collection
0038     const hue = Math.random();
0039     const randomColor = new Color().setHSL(hue, 1, 0.5); // Bright color
0040 
0041     // Create a material with the random color
0042     const material = new MeshBasicMaterial({ color: randomColor });
0043 
0044     // Create a mesh for each hit using the same material
0045     this.createHitMeshes(material);
0046   }
0047 
0048   /**
0049    * Creates Mesh instances for each hit and adds them to the parent node.
0050    *
0051    * @param material - The material to use for the hit meshes.
0052    */
0053   private createHitMeshes(material: MeshBasicMaterial): void {
0054     for (const hit of this.boxComponent.hits) {
0055       // Create geometry for the box
0056       const geometry = new BoxGeometry(10,10,10
0057         // hit.dimensions[0],
0058         // hit.dimensions[1],
0059         // hit.dimensions[2]
0060       );
0061 
0062       // Create the mesh
0063       const mesh = new Mesh(geometry, material);
0064 
0065       // Set position
0066       mesh.position.set(hit.position[0], hit.position[1], hit.position[2]);
0067 
0068       // Store the hit time
0069       mesh.userData['appearanceTime'] = hit.time[0];
0070 
0071       // Initially make the mesh invisible
0072       mesh.visible = false;
0073 
0074       // Add the mesh to the parent node and to the array
0075       this.parentNode.add(mesh);
0076       this.hitMeshes.push(mesh);
0077     }
0078   }
0079 
0080   /**
0081    * Paint method to update the visibility of the hits based on time.
0082    *
0083    * @param time - The current time in nanoseconds or null for static rendering.
0084    */
0085   public paint(time: number | null): void {
0086     for (const mesh of this.hitMeshes) {
0087       if (time !== null) {
0088         // Show the mesh if its appearance time is less than or equal to the current time
0089         mesh.visible = mesh.userData['appearanceTime'] <= time;
0090       } else {
0091         // In static mode, make all meshes visible
0092         mesh.visible = true;
0093       }
0094     }
0095   }
0096 
0097   /**
0098    * Dispose of resources used by the painter.
0099    */
0100   override dispose(): void {
0101     for (const mesh of this.hitMeshes) {
0102       // Dispose of geometry and material
0103       mesh.geometry.dispose();
0104 
0105       // Dispose of the material only if it's not shared with other meshes
0106       if (mesh.material instanceof MeshBasicMaterial) {
0107         mesh.material.dispose();
0108       }
0109 
0110       // Remove the mesh from the parent node
0111       this.parentNode.remove(mesh);
0112     }
0113 
0114     // Clear the array
0115     this.hitMeshes = [];
0116 
0117     super.dispose();
0118   }
0119 }