-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add patients datastructure (not complete)
- Loading branch information
1 parent
0d8594e
commit fa61a42
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { IsDefined, IsUUID } from 'class-validator'; | ||
import { Quadtree, quadtree } from 'd3-quadtree'; | ||
import type { ExerciseState } from '../state'; | ||
import { UUID, uuid, uuidValidationOptions } from '../utils'; | ||
import type { Position } from './utils'; | ||
import { getCreate } from './utils'; | ||
import type { Patient } from '.'; | ||
|
||
/** | ||
* | ||
* @param id PatientId | ||
* @param x x-coordinate of patient | ||
* @param y y-coordinate of patient | ||
*/ | ||
interface PatientData { | ||
id: UUID; | ||
x: number; | ||
y: number; | ||
} | ||
|
||
export class PatientsDataStructure { | ||
@IsUUID(4, uuidValidationOptions) | ||
public readonly id: UUID = uuid(); | ||
|
||
@IsDefined() | ||
public readonly dataStructure: Quadtree<PatientData>; | ||
|
||
static readonly create = getCreate(this); | ||
|
||
/** | ||
* @deprecated Use {@link create} instead | ||
*/ | ||
constructor() { | ||
// initialize and setting accessors to correct value in PatientData | ||
this.dataStructure = quadtree<PatientData>() | ||
.x((d) => d.x) | ||
.y((d) => d.y); | ||
} | ||
|
||
fillDataStructure(state: ExerciseState) { | ||
const patients = Object.values(state.patients).filter( | ||
(patient) => patient.position !== undefined | ||
); | ||
const data: PatientData[] = []; | ||
for (const patient of patients) { | ||
if (patient.position?.x && patient.position.y) { | ||
data.push({ | ||
id: patient.id, | ||
x: patient.position?.x, | ||
y: patient.position.y, | ||
}); | ||
} | ||
} | ||
// addAll calculates the extend before adding the data resulting in a more compact quadtree | ||
// see https://github.com/d3/d3-quadtree#quadtree_addAll | ||
this.dataStructure.addAll(data); | ||
} | ||
|
||
/** | ||
* @param patient needs a patient with valid position | ||
*/ | ||
addPatient(patient: Patient) { | ||
// TODO: check if patient got added sucessfully, | ||
// maybe lazy via length checking (something got added) | ||
if (patient.position?.x && patient.position.y) { | ||
const x = patient.position.x; | ||
const y = patient.position.y; | ||
this.dataStructure.add({ id: patient.id, x, y }); | ||
} else throw new TypeError('Expected patient to have a valid position'); | ||
} | ||
|
||
/** | ||
* @param patient needs a patient with valid position | ||
*/ | ||
removePatient(patient: Patient) { | ||
// TODO: check if patient got deleted sucessfully, | ||
// as function remove does nothing when datum does not exist in quadtree | ||
// maybe lazy via length checking (something got deleted) | ||
if (patient.position?.x && patient.position.y) { | ||
const x = patient.position.x; | ||
const y = patient.position.y; | ||
this.dataStructure.remove({ id: patient.id, x, y }); | ||
} else throw new TypeError('Expected patient to have a valid position'); | ||
} | ||
|
||
movePatient(patient: Patient) { | ||
// TODO: check if patient got moved sucessfully | ||
if (patient.position?.x && patient.position.y) { | ||
const x = patient.position.x; | ||
const y = patient.position.y; | ||
// first add then remove, to not have maybe an unnecessary extend | ||
this.dataStructure.add({ id: patient.id, x, y }); | ||
this.dataStructure.remove({ id: patient.id, x, y }); | ||
} else throw new TypeError('Expected patient to have a valid position'); | ||
} | ||
|
||
findAllPatientsInCircle(position: Position, radius: number) { | ||
// TODO: implement search | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters