Skip to content

Commit

Permalink
find neighbors using kdtree
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyangXYZ committed Dec 28, 2023
1 parent 85a4961 commit 7c6196d
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 145 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useDrawTopology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ export function useDrawTopology(dom: HTMLElement) {

let drawnLinks: { [uid: number]: any } = {}
const drawLinks = () => {
for (const l of Network.Links.value) {
for (const l of Object.values(Network.Links.value)) {
if (drawnLinks[l.uid] == undefined) {
drawLink(l.uid, l.v1, l.v2)
}
Expand Down
4 changes: 2 additions & 2 deletions src/networks/5G/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref } from 'vue'
import { Network, NETWORK_TYPE, NODE_TYPE, type LinkMeta } from '../common'
import { Network, NETWORK_TYPE, NODE_TYPE } from '../common'
import type { ScheduleConfig, FiveGNodeMeta } from './typedefs'
import { SeededRandom } from '@/hooks/useSeed'

Expand Down Expand Up @@ -54,7 +54,7 @@ export class FiveGNetwork extends Network {
w: new Worker(new URL('@/networks/5G/node.ts', import.meta.url), { type: 'module' })
}
// add links
this.Links.value.push(<LinkMeta>{ uid: i * 3, v1: 0, v2: i })
super.addLink(0, i)

// send init msg
// n.w!.postMessage(<Message>{
Expand Down
8 changes: 2 additions & 6 deletions src/networks/TSCH/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import { ADDR, MSG_TYPES, PKT_TYPES, CELL_TYPES } from './typedefs'
import { SeededRandom } from '@/hooks/useSeed'
import { Network, NETWORK_TYPE, NODE_TYPE } from '../common'
import type { Packet, Message, MsgHandler, LinkMeta } from '../common'
import type { Packet, Message, MsgHandler } from '../common'

export class TSCHNetwork extends Network {
doneCnt = 0
Expand Down Expand Up @@ -80,11 +80,7 @@ export class TSCHNetwork extends Network {
this.Nodes.value[node].parent = payload.parent
this.Nodes.value[node].neighbors.push(payload.parent)
this.Nodes.value[payload.parent].neighbors.push(node)
this.Links.value.push(<LinkMeta>{
uid: node * 2 + payload.parent * 3,
v1: node,
v2: payload.parent
})
super.addLink(node, payload.parent)
}

this.Nodes.value[node].queueLen = payload.queue.length
Expand Down
83 changes: 83 additions & 0 deletions src/networks/TSN/kdtree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
export class KDNode {
id: number
pos: [number, number]
left: KDNode | undefined
right: KDNode | undefined
constructor(id: number, pos: [number, number]) {
this.id = id
this.pos = pos
this.left = undefined
this.right = undefined
}
}

export class KDTree {
root: KDNode | undefined
constructor() {
this.root = undefined
}

Insert(node: KDNode): void {
this.root = this._insertRecur(this.root, node, 0)
}
private _insertRecur(current: KDNode | undefined, node: KDNode, depth: number): KDNode {
if (current == undefined) {
return node
}
const cd = depth % 2 // two dimension only
if (node.pos[cd] < current.pos[cd]) {
current.left = this._insertRecur(current.left, node, depth + 1)
} else {
current.right = this._insertRecur(current.right, node, depth + 1)
}
return current
}

FindKNearest(pos: [number, number], k: number, range: number): number[] {
const nearestNodes: KDNode[] = []
this._findKNearestRecur(this.root, pos, 0, k, range * range, nearestNodes)
return nearestNodes.map(({ id }) => id)
}
private _findKNearestRecur(
current: KDNode | undefined,
pos: [number, number],
depth: number,
k: number,
range: number,
nearestNodes: KDNode[]
): void {
if (current == undefined) return

const d = this._distanceSquared(current.pos, pos)
if (current.pos != pos && d <= range) {
if (nearestNodes.length < k) {
nearestNodes.push(current)
} else if (d < this._distanceSquared(pos, nearestNodes[k - 1].pos)) {
nearestNodes[k - 1] = current
}
nearestNodes.sort(
(a: KDNode, b: KDNode) =>
this._distanceSquared(pos, a.pos) - this._distanceSquared(pos, b.pos)
)
}

const cd = depth % 2
const diff = pos[cd] - current.pos[cd]
const closer = diff < 0 ? current.left : current.right
const farther = diff < 0 ? current.right : current.left
this._findKNearestRecur(closer, pos, depth + 1, k, range, nearestNodes)

if (
diff * diff <= range &&
(nearestNodes.length < k || diff * diff < this._distanceSquared(pos, nearestNodes[k - 1].pos))
) {
this._findKNearestRecur(farther, pos, depth + 1, k, range, nearestNodes)
}
}

private _distanceSquared(v1: [number, number], v2: [number, number]): number {
const dx = v2[0] - v1[0]
const dy = v2[1] - v1[1]
return dx * dx + dy * dy
}
}
68 changes: 16 additions & 52 deletions src/networks/TSN/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ref, toRaw } from 'vue'
import { Network, NETWORK_TYPE, NODE_TYPE, type Message } from '../common'
import { MSG_TYPES, type INIT_MSG_PAYLOAD, type ScheduleConfig, type TSNNodeMeta } from './typedefs'
import { SeededRandom } from '@/hooks/useSeed'
import { KDNode, KDTree } from './kdtree'

export class TSNNetwork extends Network {
InPorts: any
Expand Down Expand Up @@ -41,6 +42,9 @@ export class TSNNetwork extends Network {
}
] // placeholder

// to find neighbors
const tree = new KDTree()

for (let i = 1; i <= this.TopoConfig.value.num_nodes; i++) {
const n = <TSNNodeMeta>{
id: i,
Expand All @@ -66,60 +70,20 @@ export class TSNNetwork extends Network {
sch_config: toRaw(this.SchConfig.value)
}
})
// handle msg/pkt from nodes
// n.w!.onmessage = (e: any) => {
// if ('ch' in e.data == false) {
// const msg: Message = e.data
// if (this.msgHandlers[msg.type] != undefined) {
// this.msgHandlers[msg.type](msg)
// } else {
// console.log('!! undefined message type:', msg.type)
// }
// } else {
// const pkt: Packet = e.data
// // check channel interference, only one packet can be transmitted on each channel in a slot
// if (
// this.PacketsCurrent.value.filter((p) => p.ch == pkt.ch).length == 0 ||
// pkt.type == PKT_TYPES.ACK
// ) {
// // must use this format for the detailedView function of el-table-v2
// pkt.id = this.Packets.value.length
// pkt.children = [
// {
// id: `${this.Packets.value.length}-detail-content`,
// detail: JSON.stringify(pkt.payload).replace(/"/g, '')
// }
// ]

// this.Packets.value.push(pkt)
// this.PacketsCurrent.value.push(pkt)

// if (pkt.dst == ADDR.BROADCAST) {
// for (const nn of this.Nodes.value) {
// // check if in tx_range
// const distance = Math.sqrt(
// Math.pow(n.pos[0] - nn.pos[0], 2) + Math.pow(n.pos[1] - nn.pos[1], 2)
// )
// if (nn.id > 0 && nn.id != n.id && distance <= this.TopoConfig.value.tx_range) {
// nn.w!.postMessage(pkt)
// }
// }
// } else {
// const nn = this.Nodes.value[pkt.dst]
// if (nn != undefined) {
// // check if in tx_range
// const distance = Math.sqrt(
// Math.pow(n.pos[0] - nn.pos[0], 2) + Math.pow(n.pos[1] - nn.pos[1], 2)
// )
// if (distance <= this.TopoConfig.value.tx_range) {
// nn.w!.postMessage(pkt)
// }
// }
// }
// }
// }
// }
this.Nodes.value.push(n)
tree.Insert(new KDNode(i, this.Nodes.value[i].pos))
}

for (let i = 1; i <= this.TopoConfig.value.num_nodes; i++) {
this.Nodes.value[i].neighbors = tree.FindKNearest(
this.Nodes.value[i].pos,
5,
this.TopoConfig.value.tx_range
)
this.Nodes.value[i].neighbors.forEach((n: number) => {
super.addLink(i, n)
})
}
}
}
4 changes: 2 additions & 2 deletions src/networks/TSN/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class TSNNode {
if (this.msgHandlers[msg.type] != undefined) {
this.msgHandlers[msg.type](msg)
} else {
console.log('!! undefined message type:', msg.type)
// console.log('!! undefined message type:', msg.type)
}
} else {
const pkt: Packet = e.data

if (this.pktHandlers[pkt.type] != undefined) {
this.pktHandlers[pkt.type](pkt)
} else {
console.log('!! undefined packet type:', pkt.type)
// console.log('!! undefined packet type:', pkt.type)
}
}
}
Expand Down
Loading

0 comments on commit 7c6196d

Please sign in to comment.