Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit d113946

Browse files
committed
Use numeric.dotVV() for performance.
1 parent 3fafb48 commit d113946

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

javascripts/lambert.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lambert.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ brentsMethod = (a, b, relativeAccuracy, f) ->
9090
n = r1 + r2 - c
9191

9292
# Assume we want a prograde orbit counter-clockwise around the +z axis
93-
transferAngle = Math.acos(numeric.dot(pos1, pos2) / (r1 * r2))
93+
transferAngle = Math.acos(numeric.dotVV(pos1, pos2) / (r1 * r2))
9494
transferAngle = TWO_PI - transferAngle if (pos1[0] * pos2[1] - pos1[1] * pos2[0]) * prograde < 0 # (pos1 x pos2).z
9595

9696
angleParameter = Math.sqrt(n / m)

src/orbit.coffee

+21-21
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ crossProduct = (a, b) ->
3232

3333
normalize = (v) -> numeric.divVS(v, numeric.norm2(v))
3434

35-
projectToPlane = (p, n) -> numeric.subVV(p, numeric.mulSV(numeric.dot(p, n), n))
35+
projectToPlane = (p, n) -> numeric.subVV(p, numeric.mulSV(numeric.dotVV(p, n), n))
3636

3737
angleInPlane = (from, to, normal) ->
3838
from = normalize(projectToPlane(from, normal))
@@ -125,11 +125,11 @@ newtonsMethod = (x0, f, df) ->
125125
n = @normalVector()
126126
p1 = @positionAtTrueAnomaly(@trueAnomalyAt(t))
127127
p2 = orbit.positionAtTrueAnomaly(orbit.trueAnomalyAt(t))
128-
p2 = numeric.subVV(p2, numeric.mulVS(n, numeric.dot(p2, n))) # Project p2 onto our orbital plane
128+
p2 = numeric.subVV(p2, numeric.mulVS(n, numeric.dotVV(p2, n))) # Project p2 onto our orbital plane
129129
r1 = numeric.norm2(p1)
130130
r2 = numeric.norm2(p2)
131-
phaseAngle = Math.acos(numeric.dot(p1, p2) / (r1 * r2))
132-
phaseAngle = TWO_PI - phaseAngle if numeric.dot(crossProduct(p1, p2), n) < 0
131+
phaseAngle = Math.acos(numeric.dotVV(p1, p2) / (r1 * r2))
132+
phaseAngle = TWO_PI - phaseAngle if numeric.dotVV(crossProduct(p1, p2), n) < 0
133133
phaseAngle = phaseAngle - TWO_PI if orbit.semiMajorAxis < @semiMajorAxis
134134
phaseAngle
135135

@@ -262,7 +262,7 @@ Orbit.fromPositionAndVelocity = (referenceBody, position, velocity, t) ->
262262
nodeVector = normalize([-specificAngularMomentum[1], specificAngularMomentum[0], 0]) # Eq. 5.22
263263
else
264264
nodeVector = [1, 0, 0]
265-
eccentricityVector = numeric.mulSV(1 / mu, numeric.subVV(numeric.mulSV(v*v - mu / r, position), numeric.mulSV(numeric.dot(position, velocity), velocity))) # Eq. 5.23
265+
eccentricityVector = numeric.mulSV(1 / mu, numeric.subVV(numeric.mulSV(v*v - mu / r, position), numeric.mulSV(numeric.dotVV(position, velocity), velocity))) # Eq. 5.23
266266

267267
semiMajorAxis = 1 / (2 / r - v * v / mu) # Eq. 5.24
268268
eccentricity = numeric.norm2(eccentricityVector) # Eq. 5.25
@@ -275,11 +275,11 @@ Orbit.fromPositionAndVelocity = (referenceBody, position, velocity, t) ->
275275
else
276276
orbit.longitudeOfAscendingNode = Math.acos(nodeVector[0]) # Eq. 5.27
277277
orbit.longitudeOfAscendingNode = TWO_PI - orbit.longitudeOfAscendingNode if nodeVector[1] < 0
278-
orbit.argumentOfPeriapsis = Math.acos(numeric.dot(nodeVector, eccentricityVector) / eccentricity) # Eq. 5.28
278+
orbit.argumentOfPeriapsis = Math.acos(numeric.dotVV(nodeVector, eccentricityVector) / eccentricity) # Eq. 5.28
279279
orbit.argumentOfPeriapsis = TWO_PI - orbit.argumentOfPeriapsis if eccentricityVector[2] < 0
280280

281-
trueAnomaly = Math.acos(numeric.dot(eccentricityVector, position) / (eccentricity * r)) # Eq. 5.29
282-
trueAnomaly = -trueAnomaly if numeric.dot(position, velocity) < 0
281+
trueAnomaly = Math.acos(numeric.dotVV(eccentricityVector, position) / (eccentricity * r)) # Eq. 5.29
282+
trueAnomaly = -trueAnomaly if numeric.dotVV(position, velocity) < 0
283283

284284
meanAnomaly = orbit.meanAnomalyAtTrueAnomaly(trueAnomaly)
285285
orbit.timeOfPeriapsisPassage = t - meanAnomaly / orbit.meanMotion()
@@ -344,9 +344,9 @@ ejectionAngle = (vsoi, theta, prograde) ->
344344

345345
prograde = [prograde[0], prograde[1], 0] # Project the prograde vector onto the XY plane
346346
if crossProduct([vx, vy, 0], prograde)[2] < 0
347-
TWO_PI - Math.acos(numeric.dot([vx, vy, 0], prograde))
347+
TWO_PI - Math.acos(numeric.dotVV([vx, vy, 0], prograde))
348348
else
349-
Math.acos(numeric.dot([vx, vy, 0], prograde))
349+
Math.acos(numeric.dotVV([vx, vy, 0], prograde))
350350

351351
Orbit.transfer = (transferType, originBody, destinationBody, t0, dt, initialOrbitalVelocity, finalOrbitalVelocity, p0, v0, n0, p1, v1, planeChangeAngleToIntercept) ->
352352
# Fill in missing values
@@ -382,7 +382,7 @@ Orbit.transfer = (transferType, originBody, destinationBody, t0, dt, initialOrbi
382382
# in the target position rotated into the origin plane as the plane change axis changes.
383383
# This approximation should be valid so long as the transfer orbit's semi-major axis and eccentricity
384384
# does not change significantly with the change in the plane change axis.
385-
relativeInclination = Math.asin(numeric.dot(p1, n0) / numeric.norm2(p1))
385+
relativeInclination = Math.asin(numeric.dotVV(p1, n0) / numeric.norm2(p1))
386386
planeChangeRotation = quaternion.fromAngleAxis(-relativeInclination, crossProduct(p1, n0))
387387
p1InOriginPlane = quaternion.rotate(planeChangeRotation, p1)
388388
v1InOriginPlane = quaternion.rotate(planeChangeRotation, v1)
@@ -409,15 +409,15 @@ Orbit.transfer = (transferType, originBody, destinationBody, t0, dt, initialOrbi
409409
return Orbit.transfer("planeChange", originBody, destinationBody, t0, dt, initialOrbitalVelocity, finalOrbitalVelocity, p0, v0, n0, p1, v1, x)
410410
else if transferType == "planeChange"
411411
planeChangeAngleToIntercept ?= HALF_PI
412-
relativeInclination = Math.asin(numeric.dot(p1, n0) / numeric.norm2(p1))
412+
relativeInclination = Math.asin(numeric.dotVV(p1, n0) / numeric.norm2(p1))
413413
planeChangeAngle = Math.atan2(Math.tan(relativeInclination), Math.sin(planeChangeAngleToIntercept))
414414
if planeChangeAngle != 0
415415
planeChangeAxis = quaternion.rotate(quaternion.fromAngleAxis(-planeChangeAngleToIntercept, n0), projectToPlane(p1, n0))
416416
planeChangeRotation = quaternion.fromAngleAxis(planeChangeAngle, planeChangeAxis)
417417
p1InOriginPlane = quaternion.rotate(quaternion.conjugate(planeChangeRotation), p1)
418418

419419
# Assume a counter-clockwise transfer around the +z axis
420-
transferAngle = Math.acos(numeric.dot(p0, p1) / (numeric.norm2(p0) * numeric.norm2(p1)))
420+
transferAngle = Math.acos(numeric.dotVV(p0, p1) / (numeric.norm2(p0) * numeric.norm2(p1)))
421421
transferAngle = TWO_PI - transferAngle if p0[0] * p1[1] - p0[1] * p1[0] < 0 # (p0 x p1).z
422422

423423
if !planeChangeAngle or transferAngle <= HALF_PI
@@ -507,13 +507,13 @@ Orbit.transferDetails = (transfer, originBody, t0, initialOrbitalVelocity) ->
507507
n0 = originBody.orbit.normalVector()
508508
burnDirection = numeric.divVS(ejectionDeltaVector, ejectionDeltaV)
509509

510-
transfer.ejectionPitch = Math.asin(numeric.dot(burnDirection, positionDirection))
510+
transfer.ejectionPitch = Math.asin(numeric.dotVV(burnDirection, positionDirection))
511511
transfer.ejectionHeading = angleInPlane([0,0,1], burnDirection, positionDirection)
512512

513-
progradeDeltaV = numeric.dot(ejectionDeltaVector, progradeDirection)
514-
normalDeltaV = numeric.dot(ejectionDeltaVector, n0)
513+
progradeDeltaV = numeric.dotVV(ejectionDeltaVector, progradeDirection)
514+
normalDeltaV = numeric.dotVV(ejectionDeltaVector, n0)
515515
radialDeltaV = Math.sqrt(ejectionDeltaV*ejectionDeltaV - progradeDeltaV*progradeDeltaV - normalDeltaV*normalDeltaV)
516-
radialDeltaV = -radialDeltaV if numeric.dot(crossProduct(burnDirection, progradeDirection), n0) < 0
516+
radialDeltaV = -radialDeltaV if numeric.dotVV(crossProduct(burnDirection, progradeDirection), n0) < 0
517517

518518
transfer.ejectionProgradeDeltaV = progradeDeltaV
519519
transfer.ejectionNormalDeltaV = normalDeltaV
@@ -608,14 +608,14 @@ Orbit.courseCorrection = (transferOrbit, destinationOrbit, burnTime, eta) ->
608608
burnDirection = numeric.divVS(deltaVector, deltaV)
609609
positionDirection = numeric.divVS(p0, numeric.norm2(p0))
610610

611-
pitch = Math.asin(numeric.dot(burnDirection, positionDirection))
611+
pitch = Math.asin(numeric.dotVV(burnDirection, positionDirection))
612612
heading = angleInPlane([0,0,1], burnDirection, positionDirection)
613613

614614
progradeDirection = numeric.divVS(v0, numeric.norm2(v0))
615-
progradeDeltaV = numeric.dot(deltaVector, progradeDirection)
616-
normalDeltaV = numeric.dot(deltaVector, n0)
615+
progradeDeltaV = numeric.dotVV(deltaVector, progradeDirection)
616+
normalDeltaV = numeric.dotVV(deltaVector, n0)
617617
radialDeltaV = Math.sqrt(deltaV*deltaV - progradeDeltaV*progradeDeltaV - normalDeltaV*normalDeltaV)
618-
radialDeltaV = -radialDeltaV if numeric.dot(crossProduct(burnDirection, progradeDirection), n0) < 0
618+
radialDeltaV = -radialDeltaV if numeric.dotVV(crossProduct(burnDirection, progradeDirection), n0) < 0
619619

620620
return {
621621
correctedVelocity: correctedVelocity

0 commit comments

Comments
 (0)