Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonnet-Songbird committed May 13, 2024
1 parent 00fb34d commit 6632775
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 92 deletions.
8 changes: 4 additions & 4 deletions data/2Dpathfinding.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// const canvas = document.getElementById("canvasView");
// const ctx = canvas.getContext("2d"); 필요.
// const canvasPostprocessing
// 클로저 어느 쪽 방식으로 할까
// 캔버스에 대해 전처리 알고리즘이 필요하면 const canvasPreprocessing = () => {}


const clearCanvas = function (canvas) {
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (typeof canvasPostprocessing !== 'undefined') {
canvasPostprocessing(canvas);
if (typeof canvasPreprocessing !== 'undefined') {
canvasPreprocessing(canvas);
}
}

Expand Down
198 changes: 112 additions & 86 deletions data/repository/navRepo.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,100 @@
const navRepo3D = {
levelHeight: 3.9
, posRepo: []
, markerRepo: []
, pathRepo: []

//함수부
, findPosByMarkerId: function (markerId) {
const foundMarker = navRepo2D.markerRepo.find(marker => marker.id === markerId);
if (!foundMarker) return null; // 마커가 없으면 null 반환
return navRepo2D.posRepo.find(pos => pos.id === foundMarker.posId);
}
, findPosById: function (Id) {
return navRepo2D.posRepo.find(pos => pos.id === Id);
}
, pathfinding: function (startPosId, endPosId) {
}
, empty: function () {
this.posRepo = []
this.markerRepo = []
this.pathRepo = []
}
}

class repoInserter {
constructor() {
this.poiDict = {}
this.relayDict = {}
this.pathDict = []
}

addPoi(key, name, x, y, z = 0) {
this.poiDict[key] = new Pos(navRepo3D.posRepo + 1, name, x, y, z, Pos.type.POI);
return this.poiDict[key]
}

addRelay(key, name, x, y, z = 0) {
this.relayDict[key] = new Pos(navRepo3D.posRepo + 1, name, x, y, z, Pos.type.RELAY);
return this.poiDict[key]
}

addPath(aKey, bKey, restrict = Path.restrict.BOTH) {
this.pathDict.push({aKey: aKey, bKey: bKey, restrict: restrict})
}

pathFromDict(path) {
const aPos = this.poiDict[path.aKey]
const bPos = this.poiDict[path.bKey]
const name = `${aPos.name}:${bPos.name}${path.restrict}`
return new Path(navRepo3D.posRepo + 1, name, aPos, bPos, path.restrict)
}

update() {
this.poiDict.forEach(pos => {
navRepo3D.posRepo.push(pos);
})
this.relayDict.forEach(pos => {
navRepo3D.posRepo.push(pos);
})
this.pathDict.forEach(path => {
navRepo3D.path.push(this.pathFromDict(path));
})
}
}

class Coordinate {
constructor(x, y, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
}

const relativeCoordinate = {
origin: new Coordinate(0, 0, 0)
, getRelativeCoordinate(targetCoordinate) {
const convertedX = Math.abs(this.origin.x - targetCoordinate.x)
const convertedY = Math.abs(this.origin.y - targetCoordinate.y)
const convertedZ = Math.abs(this.origin.z - targetCoordinate.z)
}
, setOrigin(coordinate) {
static origin = new Coordinate(0, 0, 0)

static setOrigin(coordinate) {
this.origin = coordinate
}

getDistance(axis) {
return Math.abs(Coordinate.origin[axis] - this[axis]);
}
}

class pos {
constructor(id, name, coordinate, type) {
class Pos {
constructor(id, name, type, x, y, z) {
this.id = id;
this.name = name;
this.coordinate = coordinate;
this.type = type
this.coordinate = new Coordinate(x, y, z);
}
static type = {
POI: "PoI" // 현실에 유의미한 관심지점

static type = Object.freeze({
POI: "PoI" // 현실에서 유의미한 관심지점
, RELAY: "Relay" // 내비게이션을 위한 정적 가상 지점
, DYNAMIC: "DYNAMIC" // 내비게이션을 위한 정적 관심 지점
}
, DYNAMIC: "Dynamic" // 내비게이션을 위한 정적 관심 지점.
})

get x() {
return this.coordinate.x;
}
Expand All @@ -42,6 +107,18 @@ class pos {
return this.coordinate.z;
}

get dx() {
return this.coordinate.getDistance('x');
}

get dy() {
return this.coordinate.getDistance('y');
}

get dz() {
return this.coordinate.getDistance('z');
}

equals(target) {
return this.id === target.id;
}
Expand All @@ -57,17 +134,27 @@ class Path {
this.posA = posA;
this.posB = posB;
this.restrict = restrict;
return this;
}

static restrict = {
static makePath(id, name, posA, posB, restrict) {
const newPath = new Path(id, name, posA, posB, restrict);
const existingPath = navRepo3D.pathRepo.find(path => path.equals(newPath));
if (existingPath) {
return existingPath;
} else return newPath
}

static restrict = Object.freeze({
AB: "AtoB"
, BA: "BtoA"
, BOTH: "Both"
, NONE: "None"
}
static trait = { // 요구에 따라 사용될 수도 있는 경로 특성. 현재는 미사용
})
static trait = Object.freeze({ // 요구에 따라 사용될 수도 있는 경로 특성. 현재는 미사용
STAIR: "stair" // 계단을 통과 하는 경로는 피할 수 있음.
}
})

equals(target) {
//정순이나 역순으로 동일한 pos일 경우
return this.posA.equals(target.posA) && this.posB.equals(target.posB) || this.posB.equals(target.posA) && this.posA.equals(target.posB);
Expand All @@ -79,74 +166,12 @@ class Path {
}
return Math.hypot(this.posA.x - this.posB.x, this.posA.y - this.posB.y);
}
}

const navRepo3D = {
levelHeight: 3.9
, posRepo: [
{"id": 1, "name": "기준점", "x": 0, "y": 0, "z": 0}
, {"id": 2, "name": "x100", "x": 100, "y": 0, "z": 0}
, {"id": 3, "name": "x-100", "x": -100, "y": 0, "z": 0}
, {"id": 4, "name": "y100", "x": 0, "y": 100, "z": 0}
, {"id": 5, "name": "y-100", "x": 0, "y": -100, "z": 0}
, {"id": 6, "name": "z2", "x": 0, "y": 0, "z": 2}
]
, markerRepo: [
{"id": 1, "name": "바코드1", "posId": 1},
{"id": 2, "name": "바코드2", "posId": 2},
{"id": 3, "name": "바코드3", "posId": 3},
{"id": 4, "name": "바코드4", "posId": 4}
]
, pathRepo: [
new Path(1, "test", 2, 3, 0),
new Path(2, "test", 3, 4, 0),
new Path(3, "test", 4, 5, 0),
new Path(4, "test", 5, 6, 0),
new Path(5, "test", 3, 6, 0),
new Path(6, "test", 2, 5, 0)
]

//함수부
, findPosByMarkerId: function (markerId) {
const foundMarker = navRepo2D.markerRepo.find(marker => marker.id === markerId);
if (!foundMarker) return null; // 마커가 없으면 null 반환
return navRepo2D.posRepo.find(pos => pos.id === foundMarker.posId);
}
, findPosById: function (Id) {
return navRepo2D.posRepo.find(pos => pos.id === Id);
}
, pathfinding: function (startPosId, endPosId) {
const graph = new DijkstraGraph();
this.posRepo.forEach(pos => {
graph.addVertex(pos.id);
});
this.pathRepo.forEach(path => {
graph.addEdge(path.posA, path.posB, path.pathLength(), path.restrict);
});
return graph.pathfinding(startPosId, endPosId)
}
, empty: function () {
this.posRepo = []
this.markerRepo = []
this.pathRepo = []
}
, pushPos: function (name, x, y, z) {
const id = this.posRepo.length + 1
const pos = {
"id": id
, "name": `${name}`
, "x": x
, "y": y
, "z": z
dPathLength() {
if (this.restrict === Path.restrict.NONE) {
return Infinity;
}
this.posRepo.push(pos)
return id;
}
, pushPath: function (name, posA, posB) {
const id = this.pathRepo.length + 1
const path = new Path(id, `${name}`, posA, posB, 0);
this.pathRepo.push(path);
return path;
return Math.hypot(this.posA.dx - this.posB.dx, this.posA.dy - this.posB.dy);
}
}

Expand All @@ -170,6 +195,7 @@ class DijkstraGraph {
}
}


pathfinding(start, finish) {
const nodes = new PriorityQueue();
nodes.enqueue(start, 0);
Expand Down
4 changes: 2 additions & 2 deletions demo/cruise2D.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@
<script>
const canvas = document.getElementById("canvasView");
const ctx = canvas.getContext("2d");
const canvasPostprocessing = function (canvas) {
const canvasPreprocessing = function (canvas) {
canvas.getContext("2d").drawImage(canvasBackground, 0, 0, canvas.width, canvas.height);
}
const canvasBackground = new Image();
canvasBackground.onload = function () {
canvasPostprocessing(canvas, canvasBackground); // 이미지가 로드된 후 canvasPostprocessing 함수 호출
canvasPreprocessing(canvas, canvasBackground); // 이미지가 로드된 후 canvasPreprocessing 함수 호출
};
canvasBackground.src = "../data/images/cruise-deck.jpg"; // 이미지 경로 할당

Expand Down

0 comments on commit 6632775

Please sign in to comment.