|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Bentley Systems, Incorporated. All rights reserved. |
| 3 | + * See LICENSE.md in the project root for license terms and full copyright notice. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import * as path from "path"; |
| 6 | +import { |
| 7 | + DefinitionModel, |
| 8 | + GeometricElement3d, |
| 9 | + IModelDb, |
| 10 | + IModelJsFs, |
| 11 | + PhysicalModel, |
| 12 | + PhysicalObject, |
| 13 | + SnapshotDb, |
| 14 | + SpatialCategory, |
| 15 | + Subject, |
| 16 | +} from "@itwin/core-backend"; |
| 17 | +import { |
| 18 | + Cartographic, |
| 19 | + Code, |
| 20 | + ColorDef, |
| 21 | + EcefLocation, |
| 22 | + GeometryStreamBuilder, |
| 23 | + PhysicalElementProps, |
| 24 | +} from "@itwin/core-common"; |
| 25 | +import { Point3d, Sphere, YawPitchRollAngles } from "@itwin/core-geometry"; |
| 26 | +import { assert } from "console"; |
| 27 | +import { |
| 28 | + IModelTransformer, |
| 29 | + IModelTransformOptions, |
| 30 | +} from "../../IModelTransformer"; |
| 31 | +import { expect } from "chai"; |
| 32 | + |
| 33 | +describe("Linear Geolocation Transformations", () => { |
| 34 | + function initOutputFile(fileBaseName: string) { |
| 35 | + const outputDirName = path.join(__dirname, "output"); |
| 36 | + if (!IModelJsFs.existsSync(outputDirName)) { |
| 37 | + IModelJsFs.mkdirSync(outputDirName); |
| 38 | + } |
| 39 | + const outputFileName = path.join(outputDirName, fileBaseName); |
| 40 | + if (IModelJsFs.existsSync(outputFileName)) { |
| 41 | + IModelJsFs.removeSync(outputFileName); |
| 42 | + } |
| 43 | + return outputFileName; |
| 44 | + } |
| 45 | + |
| 46 | + function convertLatLongToEcef(lat: number, long: number): EcefLocation { |
| 47 | + const cartographic = Cartographic.fromDegrees({ |
| 48 | + longitude: long, |
| 49 | + latitude: lat, |
| 50 | + height: 0, |
| 51 | + }); |
| 52 | + const ecef = EcefLocation.createFromCartographicOrigin(cartographic); |
| 53 | + |
| 54 | + return ecef; |
| 55 | + } |
| 56 | + |
| 57 | + // Create a test iModel with a specified ECEF location and number of spherical elements |
| 58 | + // Elements are of radius 1, placed in 2 by 2 by x grids 5 meters apart, and first eleement is inserted at origin |
| 59 | + // which is the specified ECEF location |
| 60 | + function createTestSnapshotDb( |
| 61 | + ecef: EcefLocation, |
| 62 | + dbName: string, |
| 63 | + numElements: number = 1, |
| 64 | + color: string = "red" |
| 65 | + ): SnapshotDb { |
| 66 | + const dbFileName = initOutputFile(`${dbName}.bim`); |
| 67 | + const imodelDb = SnapshotDb.createEmpty(dbFileName, { |
| 68 | + rootSubject: { name: dbName }, |
| 69 | + ecefLocation: ecef, |
| 70 | + }); |
| 71 | + // bug in SnapshotEb.createEmpty does not properly set ecefLocation, this was corrected in itwinjs-core 5.1, and will be fixed when transformer repo is updated |
| 72 | + imodelDb.setEcefLocation(ecef); |
| 73 | + |
| 74 | + const subjectId = Subject.insert( |
| 75 | + imodelDb, |
| 76 | + IModelDb.rootSubjectId, |
| 77 | + "Test Subject" |
| 78 | + ); |
| 79 | + const defintionModelId = DefinitionModel.insert( |
| 80 | + imodelDb, |
| 81 | + subjectId, |
| 82 | + "DefinitionModel" |
| 83 | + ); |
| 84 | + |
| 85 | + const categoryId = SpatialCategory.insert( |
| 86 | + imodelDb, |
| 87 | + defintionModelId, |
| 88 | + `${color} Category`, |
| 89 | + { color: ColorDef.fromString(color).toJSON() } |
| 90 | + ); |
| 91 | + |
| 92 | + const modelId = PhysicalModel.insert(imodelDb, subjectId, "Test Model"); |
| 93 | + |
| 94 | + const builder = new GeometryStreamBuilder(); |
| 95 | + builder.appendGeometry(Sphere.createCenterRadius(Point3d.createZero(), 1)); |
| 96 | + for (let i = 0; i < numElements; i++) { |
| 97 | + // Arrange elements in a 2x2 grid, incrementing z every 4 elements |
| 98 | + const x = (i % 2) * 5; |
| 99 | + const y = (Math.floor(i / 2) % 2) * 5; |
| 100 | + const z = Math.floor(i / 4) * 5; |
| 101 | + |
| 102 | + const elementProps: PhysicalElementProps = { |
| 103 | + classFullName: PhysicalObject.classFullName, |
| 104 | + model: modelId, |
| 105 | + category: categoryId, |
| 106 | + code: Code.createEmpty(), |
| 107 | + geom: builder.geometryStream, |
| 108 | + placement: { |
| 109 | + origin: Point3d.create(x, y, z), |
| 110 | + angles: YawPitchRollAngles.createDegrees(0, 0, 0), |
| 111 | + }, |
| 112 | + }; |
| 113 | + imodelDb.elements.insertElement(elementProps); |
| 114 | + } |
| 115 | + imodelDb.saveChanges("Created test elements"); |
| 116 | + |
| 117 | + return imodelDb; |
| 118 | + } |
| 119 | + |
| 120 | + // Get all GeometricElement3d elements from the iModel |
| 121 | + // Used to find and compare placement of elements before and after transform |
| 122 | + async function getGeometric3dElements( |
| 123 | + iModelDb: IModelDb |
| 124 | + ): Promise<GeometricElement3d[]> { |
| 125 | + const elements: GeometricElement3d[] = []; |
| 126 | + const query = "SELECT ECInstanceId FROM bis.GeometricElement3d"; |
| 127 | + for await (const row of iModelDb.createQueryReader(query)) { |
| 128 | + const element = iModelDb.elements.getElement<GeometricElement3d>(row.id); |
| 129 | + elements.push(element); |
| 130 | + } |
| 131 | + return elements; |
| 132 | + } |
| 133 | + |
| 134 | + it("should transform placement of src elements using core transfromer", async function () { |
| 135 | + const srcEcef = convertLatLongToEcef( |
| 136 | + 39.952959446468206, |
| 137 | + -75.16349515933572 |
| 138 | + ); // City Hall |
| 139 | + const targetEcef = convertLatLongToEcef( |
| 140 | + 39.95595450339434, |
| 141 | + -75.16697176954752 |
| 142 | + ); // Bentley Cherry Street |
| 143 | + |
| 144 | + // generate imodels with ecef locations specified above, and number of spherical elements inserted |
| 145 | + const sourceDb = createTestSnapshotDb( |
| 146 | + srcEcef, |
| 147 | + "Source-ECEF-core-Transform", |
| 148 | + 12, |
| 149 | + "red" |
| 150 | + ); |
| 151 | + const targetDb = createTestSnapshotDb( |
| 152 | + targetEcef, |
| 153 | + "Target-ECEF-core-Transform", |
| 154 | + 12, |
| 155 | + "blue" |
| 156 | + ); |
| 157 | + assert(sourceDb.geographicCoordinateSystem === undefined); |
| 158 | + assert(targetDb.geographicCoordinateSystem === undefined); |
| 159 | + assert(sourceDb.ecefLocation !== undefined); |
| 160 | + assert(targetDb.ecefLocation !== undefined); |
| 161 | + |
| 162 | + // get Fed Guid of one geomentric element in srcDb so we can compare the transfromed element in targetDb |
| 163 | + const srcElements = await getGeometric3dElements(sourceDb); |
| 164 | + const srcElemFedGuid = srcElements[0].federationGuid; |
| 165 | + |
| 166 | + const transformerOptions: IModelTransformOptions = { |
| 167 | + alignECEFLocations: true, |
| 168 | + }; |
| 169 | + const transfrom = new IModelTransformer( |
| 170 | + sourceDb, |
| 171 | + targetDb, |
| 172 | + transformerOptions |
| 173 | + ); |
| 174 | + |
| 175 | + await transfrom.process(); |
| 176 | + targetDb.saveChanges("clone contents from source"); |
| 177 | + |
| 178 | + const srcElemPositionPostTransform = |
| 179 | + targetDb.elements.getElement<GeometricElement3d>( |
| 180 | + srcElemFedGuid! |
| 181 | + ).placement; |
| 182 | + srcElemPositionPostTransform.multiplyTransform(targetEcef.getTransform()); |
| 183 | + // assert that the element at the origin of sourceDb still has the same ecef location when transformed to targetDb |
| 184 | + expect( |
| 185 | + srcEcef.origin.isAlmostEqual(srcElemPositionPostTransform.origin), |
| 186 | + "Source element position's ecef location does not match target element position's ecef location after transform" |
| 187 | + ).to.be.true; |
| 188 | + |
| 189 | + targetDb.close(); |
| 190 | + sourceDb.close(); |
| 191 | + transfrom.dispose(); |
| 192 | + }); |
| 193 | +}); |
0 commit comments