Skip to content

Commit

Permalink
Implement transforms cache to reduce number of transform calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowm committed Jul 3, 2023
1 parent 534acea commit bb0f830
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/modules/SatelliteProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "./util/CesiumSampledPositionRawValueAccess";

import satvisIcon from "../assets/android-chrome-192x192.png";
import { CesiumCallbackHelper } from "./util/CesiumCallbackHelper";
// import { CesiumTransformsCache } from "./util/CesiumTransformsCache";

export class SatelliteProperties {
constructor(tle, tags = []) {
Expand Down Expand Up @@ -177,12 +178,14 @@ export class SatelliteProperties {
const positionInertialTEME = this.computePositionInertialTEME(timestamp);

const temeToFixed = Cesium.Transforms.computeTemeToPseudoFixedMatrix(timestamp);
// const temeToFixed = CesiumTransformsCache.getTemeToPseudoFixedMatrix(timestamp);
if (!Cesium.defined(temeToFixed)) {
console.error("Reference frame transformation data failed to load");
}
const positionFixed = Cesium.Matrix3.multiplyByVector(temeToFixed, positionInertialTEME, new Cesium.Cartesian3());

const fixedToIcrf = Cesium.Transforms.computeFixedToIcrfMatrix(timestamp);
// const fixedToIcrf = CesiumTransformsCache.getFixedToIcrfMatrix(timestamp);
if (!Cesium.defined(fixedToIcrf)) {
console.error("Reference frame transformation data failed to load");
}
Expand Down
62 changes: 62 additions & 0 deletions src/modules/util/CesiumTransformsCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as Cesium from "cesium";

export class CesiumTransformsCache {
static cache = {
temeToPseudoFixed: {
stats: {
calls: 0,
miss: 0,
},
},
fixedToIcrfMatrix: {
stats: {
calls: 0,
miss: 0,
},
},
};

/**
* Computes and caches a rotation matrix to transform a point or vector from the Earth-Fixed frame axes (ITRF)
* to the International Celestial Reference Frame (GCRF/ICRF) inertial frame axes
* at a given time
* @param {JulianDate} time The time at which to compute the rotation matrix.
* @returns {Matrix3} The rotation matrix
*/
static getTemeToPseudoFixedMatrix(time) {
const cache = CesiumTransformsCache.cache.temeToPseudoFixed;
const key = `${time.dayNumber}-${Math.floor(time.secondsOfDay)}`;
// const key = Cesium.JulianDate.toIso8601(time, 0);
if (!cache[key]) {
cache[key] = Cesium.Transforms.computeTemeToPseudoFixedMatrix(time);
cache.stats.miss += 1;
}
cache.stats.calls += 1;
if (cache.stats.calls % 10000 === 0) {
console.log(`getTemeToPseudoFixedMatrix: ${cache.stats.calls} calls, ${cache.stats.miss} misses Ratio: ${cache.stats.miss / cache.stats.calls}`);
}
return cache[key];
}

/**
* Computes and caches a rotation matrix to transform a point or vector from the Earth-Fixed frame axes (ITRF)
* to the International Celestial Reference Frame (GCRF/ICRF) inertial frame axes
* at a given time
* @param {JulianDate} time The time at which to compute the rotation matrix.
* @returns {Matrix3} The rotation matrix
*/
static getFixedToIcrfMatrix(time) {
const cache = CesiumTransformsCache.cache.fixedToIcrfMatrix;
const key = `${time.dayNumber}-${Math.floor(time.secondsOfDay)}`;
// const key = Cesium.JulianDate.toIso8601(time, 0);
if (!cache[key]) {
cache[key] = Cesium.Transforms.computeFixedToIcrfMatrix(time);
cache.stats.miss += 1;
}
cache.stats.calls += 1;
if (cache.stats.calls % 10000 === 0) {
console.log(`getFixedToIcrfMatrix: ${cache.stats.calls} calls, ${cache.stats.miss} misses Ratio: ${cache.stats.miss / cache.stats.calls}`);
}
return cache[key];
}
}

0 comments on commit bb0f830

Please sign in to comment.