Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
monman53 committed Jul 4, 2024
1 parent 95310ee commit 0dfd645
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 48 deletions.
12 changes: 11 additions & 1 deletion src/Controller.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { computed } from 'vue'
import { state, lensGroups, defaultConvexLens, defaultConcaveLens, sensor, appleProps, options, style, defaultDoubletLens } from './globals'
import { state, lensGroups, defaultConvexLens, defaultConcaveLens, sensor, appleProps, options, style, defaultDoubletLens, lights } from './globals'
import { humanReadable } from './utils';
import { Light } from "./type"
Expand Down Expand Up @@ -116,6 +116,16 @@ const createSensor = (d: number) => {
<br>
<button
@click="lensGroups.push({ lenses: [defaultDoubletLens(0)], selected: false })">Doublet</button>
<br>
<button @click="() => {
lensGroups = []
lensGroups.push({ lenses: [defaultConvexLens(40)], selected: false })
lensGroups.push({ lenses: [defaultConvexLens(20)], selected: false })
lensGroups.push({ lenses: [defaultDoubletLens(0)], selected: false })
lensGroups.push({ lenses: [defaultConvexLens(-20)], selected: false })
options.body = true
lights = []
}">Bench</button>
</td>
</tr>
<!-- <tr>
Expand Down
10 changes: 9 additions & 1 deletion src/globals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, computed, watch } from "vue"
import { Vec, calcLensFront, vec, calcLensR, calcLensBack, fGaussian, calcLensXCOG, calcLensMaxX } from "./math"
import { Vec, calcLensFront, vec, calcLensR, calcLensBack, fGaussian, calcLensXCOG, calcLensMaxX, calcLensPlaneEdge } from "./math"
import { type Lens, type LensGroup, Light } from "./type"

//================================
Expand Down Expand Up @@ -145,6 +145,14 @@ export const lensCOGs = computed(() => {
})
})

export const lensPlaneEdges = computed(() => {
return lensesSorted.value.map((lens) => {
return lens.planes.map(p=>{
return calcLensPlaneEdge(p)
})
})
})

export const releaseAllLenses = () => {
for (const lensGroup of lensGroups.value) {
lensGroup.selected = false
Expand Down
43 changes: 21 additions & 22 deletions src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class Vec {
}

inplaceNormalize() {
return this.div(this.length())
return this.inplaceDiv(this.length())
}

add(v: Vec) {
Expand Down Expand Up @@ -153,30 +153,29 @@ export const crossAngle = (p: Vec, q: Vec) => {
export const eps = 1e-9

// ccw
const ccw = (a: Vec, b: Vec, c: Vec) => {
b = b.sub(a)
c = c.sub(a)
if (cross(b, c) > eps) return +1 // counter clockwise
if (cross(b, c) < -eps) return -1 // clockwise
if (dot(b, c) < 0) return +2 // cab (back)
if (b.length() < c.length()) return -2 // abc (front)
return 0 // acb (on segment)
}

const isIntersectedSS = (a1: Vec, a2: Vec, b1: Vec, b2: Vec) => {
return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0
}

const intersectionLL = (a1: Vec, a2: Vec, b1: Vec, b2: Vec) => {
// const ccw = (a: Vec, b: Vec, c: Vec) => {
// b = b.sub(a)
// c = c.sub(a)
// if (cross(b, c) > eps) return +1 // counter clockwise
// if (cross(b, c) < -eps) return -1 // clockwise
// if (dot(b, c) < 0) return +2 // cab (back)
// if (b.length() < c.length()) return -2 // abc (front)
// return 0 // acb (on segment)
// }

// const isIntersectedSS = (a1: Vec, a2: Vec, b1: Vec, b2: Vec) => {
// return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
// ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0
// }

const intersectionLL = (s: Vec, v: Vec, a1: Vec, a2: Vec) => {
const a = a2.sub(a1)
const b = b2.sub(b1)
return a1.add(a.mul(cross(b, b1.sub(a1))).inplaceDiv(cross(b, a)))
return s.add(v.mul(cross(a, a1.sub(s))).inplaceDiv(cross(a, v)))
}

export const intersectionSS = (a1: Vec, a2: Vec, b1: Vec, b2: Vec) => {
if (isIntersectedSS(a1, a2, b1, b2)) {
return intersectionLL(a1, a2, b1, b2)
export const intersectionLS = (s: Vec, v: Vec, a1: Vec, a2: Vec) => {
if (cross(v, a1.sub(s)) * cross(v, a2.sub(s)) < 0) {
return intersectionLL(s, v, a1, a2)
} else {
return null
}
Expand Down
31 changes: 7 additions & 24 deletions src/rayTrace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { aperture, body, infR, lensBacks, lensCOGs, lensFronts, lensFs, lensRs, lensesSorted, options, sensor } from "./globals";
import { calcLensPlaneEdge, crossAngle, dot, eps, fGaussian, intersectionCL, intersectionSS, intersectionX, intersectionY, vec, vecRad, type Vec } from "./math";
import { aperture, body, infR, lensBacks, lensCOGs, lensFronts, lensFs, lensPlaneEdges, lensRs, lensesSorted, options, sensor } from "./globals";
import { crossAngle, dot, eps, fGaussian, intersectionCL, intersectionLS, intersectionX, intersectionY, vec, vecRad, type Vec } from "./math";

const collisionLens = (s: Vec, v: Vec, cx: number, r: number, h: number, ni: number, no: number) => {
v = v.normalize()
Expand Down Expand Up @@ -76,8 +76,8 @@ const collisionAperture = (s: Vec, v: Vec, x: number, rMin: number, rMax: number
}

const collisionSensor = (s: Vec, v: Vec) => {
const ps = intersectionSS(s, s.add(v.mul(infR.value)), sensor.value.s, sensor.value.t)
if (ps !== null) {
const ps = intersectionLS(s, v, sensor.value.s, sensor.value.t)
if (ps !== null && dot(ps.sub(s), v) > 0) {
return {
p: ps,
d: ps.sub(s).length(),
Expand Down Expand Up @@ -120,6 +120,7 @@ const collisionAll = (s: Vec, v: Vec): ({ p: Vec, d: number, isSensor?: boolean,
return
}
if (p.d <= eps) {
// Ray-tracing technique
return
}
if (pMin === null) {
Expand Down Expand Up @@ -153,14 +154,14 @@ const collisionAll = (s: Vec, v: Vec): ({ p: Vec, d: number, isSensor?: boolean,
if (options.value.lensIdeal) {
updateMin(collisionIdealLens(s, v, xm, h * lens.aperture, f))
} else {
lens.planes.forEach(p => {
lens.planes.forEach((p, j) => {
const ni = p.r > 0 ? p.nb : p.na
const no = p.r > 0 ? p.na : p.nb
// Plane
updateMin(collisionLens(s, v, p.x + p.r, p.r, p.h, ni, no))

// Plane outside
updateMin(collisionAperture(s, v, calcLensPlaneEdge(p), p.h, h))
updateMin(collisionAperture(s, v, lensPlaneEdges.value[i][j], p.h, h))
})
}

Expand Down Expand Up @@ -189,24 +190,6 @@ const collisionAll = (s: Vec, v: Vec): ({ p: Vec, d: number, isSensor?: boolean,
}

return pMin

//--------------------------------
// Find nearest collision
//--------------------------------
// ps = ps.filter((p) => p !== null && p.d > eps)
// ps.sort((a, b) => {
// if (a !== null && b !== null) {
// return a.d - b.d
// } else {
// return 0
// }
// })

// if (ps.length > 0) {
// return ps[0]
// } else {
// return null
// }
}

export const rayTrace = (s: Vec, v: Vec) => {
Expand Down

0 comments on commit 0dfd645

Please sign in to comment.