All files / datasources/local/cad createLocalCadModel.ts

14.29% Statements 8/56
0% Branches 0/16
0% Functions 0/6
14.29% Lines 8/56

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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100        1x   1x 1x 1x 1x 1x 1x       1x                                                                                                                                                                        
/*!
 * Copyright 2020 Cognite AS
 */
 
import { createParser, createQuadsParser } from '../../../models/cad/parseSectorData';
import { FetchCtmDelegate, FetchSectorDelegate, FetchSectorMetadataDelegate } from '../../../models/cad/delegates';
import { loadLocalCadMetadata } from './loadLocalCadMetadata';
import { loadLocalSimpleCadMetadata } from './loadLocalSimpleSectorMetadata';
import { DefaultSectorRotationMatrix, DefaultInverseSectorRotationMatrix } from '../../constructMatrixFromRotation';
import { loadLocalFileMap } from './loadLocalFileMap';
import { buildSectorMetadata } from '../../cognitesdk/cad/buildSectorMetadata';
import { getNewestVersionedFile } from '../../cognitesdk/utilities';
import { CadModel } from '../../../models/cad/CadModel';
 
// TODO rename file from sector to cad
export async function createLocalCadModel(baseUrl: string): Promise<CadModel> {
  const loadMetadata = loadLocalCadMetadata(baseUrl + '/uploaded_sectors.txt');
  const loadSimpleMetadata = loadLocalSimpleCadMetadata(baseUrl + '/uploaded_sectors_simple.txt');
  const loadSectorIdToFileId = loadMetadata.then(metadata => {
    const sectorIdToFileId = new Map<number, number>();
    for (const sector of metadata) {
      const bestFile = getNewestVersionedFile(sector.threedFiles);
      sectorIdToFileId.set(sector.id, bestFile.fileId);
    }
    return sectorIdToFileId;
  });
  const loadFilemap = loadLocalFileMap(baseUrl + '/uploaded_files.txt');
 
  const fetchSectorMetadata: FetchSectorMetadataDelegate = async () => {
    return [
      buildSectorMetadata(await loadMetadata, await loadSimpleMetadata),
      {
        modelMatrix: DefaultSectorRotationMatrix,
        inverseModelMatrix: DefaultInverseSectorRotationMatrix
      }
    ];
  };
  const fetchSectorDetailed: FetchSectorDelegate = async (sectorId: number) => {
    const sectorIdToFileId = await loadSectorIdToFileId;
    const fileId = sectorIdToFileId.get(sectorId);
    if (!fileId || fileId === -1) {
      throw new Error(`${sectorId} is not a valid sector ID`);
    }
    return fetchCtm(fileId);
  };
  // TODO this function is a big hack because we do not have the f3d fileId
  const fetchSectorSimple: FetchSectorDelegate = async (sectorId: number) => {
    const sectorIdToFileId = await loadSectorIdToFileId;
    const fileId = sectorIdToFileId.get(sectorId);
    if (!fileId || fileId === -1) {
      throw new Error(`${sectorId} is not a valid sector ID`);
    }
    const filemap = await loadFilemap;
    const filename = filemap.get(fileId);
    if (!filename) {
      throw new Error(`Could not find filename mapping for file ${fileId})`);
    }
    const filenameQuads = filename.replace('.i3d', '.f3d');
    const url = baseUrl + '/' + filenameQuads;
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`Got error ${response.status} while fetching '${url}' (${response.statusText})`);
    }
 
    const buffer = await response.arrayBuffer();
 
    return new Uint8Array(buffer);
  };
  const fetchCtm: FetchCtmDelegate = async (fileId: number) => {
    const filemap = await loadFilemap;
    const filename = filemap.get(fileId);
    if (!filename) {
      throw new Error(`Could not find filename mapping for file ${fileId})`);
    }
 
    const url = baseUrl + '/' + filename;
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`Got error ${response.status} while fetching '${url}' (${response.statusText})`);
    }
 
    const buffer = await response.arrayBuffer();
    return new Uint8Array(buffer);
  };
  // Fetch metadata
  const [scene, modelTransformation] = await fetchSectorMetadata();
  const parseDetailed = createParser(fetchCtm);
  const parseSimple = await createQuadsParser();
  return {
    fetchSectorMetadata,
    fetchSectorDetailed,
    fetchSectorSimple,
    fetchCtm,
    parseDetailed,
    parseSimple,
    scene,
    modelTransformation
  };
}