Refractoring the magnitude sum object cache calculation which should (hopefully) reduce bugs and improve maintainability.

This commit is contained in:
Adsooi 2024-09-17 00:44:14 +02:00
parent 601efc6122
commit a16f02fd5f
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
2 changed files with 60 additions and 47 deletions

View file

@ -133,10 +133,19 @@ export default class GainBode extends ExecutableObject {
update() { update() {
super.update() super.update()
if(Objects.currentObjects['Somme gains Bode'] !== undefined && Objects.currentObjects['Somme gains Bode'].length > 0) { let sumObjs = Objects.currentObjects['Somme gains Bode']
Objects.currentObjects['Somme gains Bode'][0].recalculateCache() if(sumObjs !== undefined && sumObjs.length > 0) {
sumObjs[0].recalculateCache()
} else { } else {
Objects.createNewRegisteredObject('Somme gains Bode') Objects.createNewRegisteredObject('Somme gains Bode')
} }
} }
delete() {
super.delete()
let sumObjs = Objects.currentObjects['Somme gains Bode']
if(sumObjs !== undefined && sumObjs.length > 0) {
sumObjs[0].recalculateCache()
}
}
} }

View file

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { parseDomain, Expression, Domain } from "../mathlib.mjs" import { Range, Expression, Domain } from "../mathlib.mjs"
import * as P from "../parameters.mjs" import * as P from "../parameters.mjs"
import Objects from "../objects.mjs" import Objects from "../objects.mjs"
import Latex from "../math/latex.mjs" import Latex from "../math/latex.mjs"
@ -53,9 +53,9 @@ export default class SommeGainsBode extends ExecutableObject {
} }
execute(x = 0) { execute(x = 0) {
for(let [dbfn, inDrawDom] of this.cachedParts) { for(let [limitedDrawFunction, inDrawDom] of this.cachedParts) {
if(inDrawDom.includes(x)) { if(inDrawDom.includes(x)) {
return dbfn.execute(x) return limitedDrawFunction.execute(x)
} }
} }
return null return null
@ -66,9 +66,9 @@ export default class SommeGainsBode extends ExecutableObject {
} }
simplify(x = 1) { simplify(x = 1) {
for(let [dbfn, inDrawDom] of this.cachedParts) { for(let [limitedDrawFunction, inDrawDom] of this.cachedParts) {
if(inDrawDom.includes(x)) { if(inDrawDom.includes(x)) {
return dbfn.simplify(x) return limitedDrawFunction.simplify(x)
} }
} }
return '' return ''
@ -77,61 +77,65 @@ export default class SommeGainsBode extends ExecutableObject {
recalculateCache() { recalculateCache() {
this.cachedParts = [] this.cachedParts = []
// Calculating this is fairly resource expansive so it's cached. // Calculating this is fairly resource expansive so it's cached.
if(Objects.currentObjects['Gain Bode'] !== undefined) { let magnitudeObjects = Objects.currentObjects['Gain Bode']
if(magnitudeObjects === undefined || magnitudeObjects.length < 1) {
Objects.deleteObject(this.name)
} else {
console.log('Recalculating cache gain') console.log('Recalculating cache gain')
// Minimum to draw (can be expended if needed, just not infinite or it'll cause issues. // Minimum to draw (can be expended if needed, just not infinite or it'll cause issues.
let drawMin = 0.001 const MIN_DRAW = 1e-20
// Format: [[x value of where the filter transitions, magnitude, high-pass (bool)]]
const magnitudes = []
const XVALUE = 0
const MAGNITUDE = 1
const PASS = 2
magnitudes.push([Number.MAX_VALUE, 0, true]) // Draw the ending section
// Collect data from current magnitude (or gain in French) objects.
let baseY = 0 let baseY = 0
let om0xGains = {1000000000: 0} // To draw the last part for(/** @type {GainBode} */ let magnitudeObj of magnitudeObjects) { // Sorting by their om_0 position.
let om0xPass = {1000000000: 'high'} // To draw the last part const om0x = magnitudeObj.om_0.x.execute()
for(/** @type {GainBode} */ let gainObj of Objects.currentObjects['Gain Bode']) { // Sorting by their om_0 position. magnitudes.push([om0x, magnitudeObj.gain.execute(), magnitudeObj.pass === 'high'])
let om0x = gainObj.om_0.x.execute() baseY += magnitudeObj.execute(MIN_DRAW)
if(om0xGains[om0x] === undefined) {
om0xGains[om0x] = gainObj.gain.execute()
om0xPass[om0x] = gainObj.pass === 'high'
} else {
om0xGains[om0x+0.001] = gainObj.gain.execute()
om0xPass[om0x+0.001] = gainObj.pass === 'high'
}
baseY += gainObj.execute(drawMin)
} }
// Sorting the om_0x // Sorting the data by their x transitions value
let om0xList = Object.keys(om0xGains).map(x => parseFloat(x)) // THEY WERE CONVERTED TO STRINGS... magnitudes.sort((a,b) => a[XVALUE] - b[XVALUE])
om0xList.sort((a,b) => a - b)
// Adding the total gains. // Adding the total gains.
let gainsBeforeP = [] let magnitudesBeforeTransition = []
let gainsAfterP = [] let magnitudesAfterTransition = []
let gainTotal = 0 let totalMagnitudeAtStart = 0 // Magnitude at the lowest x value (sum of all high-pass magnitudes)
for(let om0x of om0xList){ for(let [om0x, magnitude, highpass] of magnitudes){
if(om0xPass[om0x]) { // High-pass if(highpass) {
gainsBeforeP.push(om0xGains[om0x]) magnitudesBeforeTransition.push(magnitude)
gainsAfterP.push(0) magnitudesAfterTransition.push(0)
gainTotal += om0xGains[om0x] // Gain at first totalMagnitudeAtStart += magnitude
} else { } else {
gainsBeforeP.push(0) magnitudesBeforeTransition.push(0)
gainsAfterP.push(om0xGains[om0x]) magnitudesAfterTransition.push(magnitude)
} }
} }
// Calculating parts // Calculating parts
let previousPallier = drawMin let previousTransitionX = MIN_DRAW
for(let pallier = 0; pallier < om0xList.length; pallier++) { let currentMagnitude = totalMagnitudeAtStart
let dbfn = new Expression(`${gainTotal}*(ln(x)-ln(${previousPallier}))/ln(10)+${baseY}`) for(let transitionID = 0; transitionID < magnitudes.length; transitionID++) {
let inDrawDom = parseDomain(`]${previousPallier};${om0xList[pallier]}]`) const transitionX = magnitudes[transitionID][XVALUE]
this.cachedParts.push([dbfn, inDrawDom]) // Create draw function that will be used during drawing.
previousPallier = om0xList[pallier] const limitedDrawFunction = new Expression(`${currentMagnitude}*(ln(x)-ln(${previousTransitionX}))/ln(10)+${baseY}`)
baseY = dbfn.execute(om0xList[pallier]) const drawDomain = new Range(previousTransitionX, transitionX, true, false)
gainTotal += gainsAfterP[pallier] - gainsBeforeP[pallier] this.cachedParts.push([limitedDrawFunction, drawDomain])
// Prepare default values for next function.
previousTransitionX = transitionX
baseY = limitedDrawFunction.execute(transitionX)
currentMagnitude += magnitudesAfterTransition[transitionID] - magnitudesBeforeTransition[transitionID]
} }
} }
} }
draw(canvas) { draw(canvas) {
if(this.cachedParts.length > 0) { if(this.cachedParts.length > 0) {
for(let [dbfn, inDrawDom] of this.cachedParts) { for(let [limitedDrawFunction, drawDomain] of this.cachedParts) {
Function.drawFunction(canvas, dbfn, inDrawDom, Domain.R) Function.drawFunction(canvas, limitedDrawFunction, drawDomain, Domain.R)
if(inDrawDom.includes(this.labelX)) { // Check if necessary to draw label
// Label if(drawDomain.includes(this.labelX)) {
this.drawLabel(canvas, this.labelPosition, canvas.x2px(this.labelX), canvas.y2px(this.execute(this.labelX))) this.drawLabel(canvas, this.labelPosition, canvas.x2px(this.labelX), canvas.y2px(this.execute(this.labelX)))
} }
} }