All files / src/datasources/cognitesdk/cad buildSectorMetadata.ts

89.19% Statements 33/37
80% Branches 8/10
100% Functions 4/4
89.19% Lines 33/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84          3x   3x     3x       4x 6x 6x     6x 6x 6x                       6x 6x 6x   6x 6x 6x                 6x 6x     4x     4x 6x   6x 6x 2x   4x 4x 3x     3x 3x 1x   2x             6x 2x   4x    
/*!
 * Copyright 2020 Cognite AS
 */
 
import { RevealSector3D } from '@cognite/sdk';
import { vec3 } from 'gl-matrix';
import { SectorMetadata, SectorScene } from '../../../models/cad/types';
import { Box3 } from '../../../utils/Box3';
import { LocalSimpleCadMetadataResponse } from '../../local/cad/loadLocalSimpleSectorMetadata';
 
export function buildSectorMetadata(
  sectors: RevealSector3D[],
  simpleSectors: Map<number, LocalSimpleCadMetadataResponse>
): SectorScene {
  const sectorsMetadata = sectors.reduce((map, x) => {
    const simpleSector = simpleSectors.get(x.id);
    Iif (!simpleSector) {
      throw new Error(`Could not find corresponding simple sector for sector with ID ${x.id}`);
    }
    const simple = (() => {
      Eif (!simpleSector.sector_contents) {
        return undefined;
      }
      const size = simpleSector.sector_contents.grid_size;
      const origin = simpleSector.sector_contents.grid_size;
      return {
        gridSize: vec3.fromValues(size[0], size[1], size[2]),
        gridOrigin: vec3.fromValues(origin[0], origin[1], origin[2]),
        gridIncrement: simpleSector.sector_contents.grid_increment,
        nodeCount: simpleSector.sector_contents.node_count
      };
    })();
 
    const bbox = x.boundingBox;
    const boundsMin = vec3.fromValues(bbox.min[0], bbox.min[1], bbox.min[2]);
    const boundsMax = vec3.fromValues(bbox.max[0], bbox.max[1], bbox.max[2]);
 
    const bounds = new Box3([boundsMin, boundsMax]);
    const id: number = x.id;
    const metadata: SectorMetadata = {
      id,
      path: x.path,
      bounds,
      parent: undefined,
      children: [],
      simple
    };
 
    map.set(x.path, metadata);
    return map;
  }, new Map<string, SectorMetadata>());
 
  const sectorMap = new Map<number, SectorMetadata>();
 
  // Initialize relationships
  for (const [path, data] of sectorsMetadata) {
    sectorMap.set(data.id, data);
 
    const parentPath = parentOf(path);
    if (parentPath === '') {
      continue;
    }
    const parent = sectorsMetadata.get(parentPath);
    parent!.children.push(data);
    data.parent = parent;
  }
 
  const root = sectorsMetadata.get('0/');
  if (!root) {
    throw new Error('No root sector');
  }
  return {
    root,
    sectors: sectorMap
  };
}
 
function parentOf(path: string): string {
  if (path.length <= 2) {
    return '';
  }
  return path.slice(0, path.length - 2);
}