Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 3x 3x 3x 3x 3x 3x 3x | import { HttpClient, HttpResponse } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import {
Calibration,
CalibrationPoint,
CalibrationTransform,
CompleteInteractionData,
FrameSizeDefinition,
Interaction,
Point3
} from '@reflex/shared-types';
import { fromEventPattern, Observable, using } from 'rxjs';
import { concatMap, map } from 'rxjs/operators';
import { LogService } from 'src/app/log/log.service';
import { BASE_URL } from 'src/shared/tokens/base-url.token';
import { SignalRBaseService } from './signalR.base.service';
@Injectable({
providedIn: 'root'
})
export class CalibrationService extends SignalRBaseService<string> {
private readonly http = inject(HttpClient);
private readonly baseUrl: string;
private readonly calibrationRoute = 'api/Calibration/';
private readonly getFrameSizeRoute = `${this.calibrationRoute}FrameSize`;
private readonly getSourceValuesRoute = `${this.calibrationRoute}SourceValues`;
private readonly getTargetValuesRoute = `${this.calibrationRoute}TargetValues`;
private readonly getCalibrationMatrixRoute = `${this.calibrationRoute}GetCalibrationMatrix`;
private readonly applyCalibrationRoute = `${this.calibrationRoute}ApplyCalibration`;
private readonly restartCalibrationRoute = `${this.calibrationRoute}Restart`;
private readonly saveCalibrationRoute = `${this.calibrationRoute}SaveCalibration`;
private readonly calibrateInteractionsRoute = `${this.calibrationRoute}CalibratedInteractions`;
private readonly updateFrameSizeRoute = `${this.calibrationRoute}UpdateFrameSize`;
private readonly updateCalibrationPointRoute = `${this.calibrationRoute}UpdateCalibrationPoint/`;
public constructor() {
const baseUrl = inject(BASE_URL);
const logService = inject(LogService);
super(`${baseUrl}calibhub`, 'calibrationState', logService);
this.baseUrl = baseUrl;
}
public getFrameSize(): Observable<FrameSizeDefinition> {
return this.http.get<FrameSizeDefinition>(
this.baseUrl + this.getFrameSizeRoute
);
}
public getCalibrationSourcePoints(): Observable<Array<CalibrationPoint>> {
return this.http.get<Array<CalibrationPoint>>(
this.baseUrl + this.getSourceValuesRoute
);
}
public getCurrentCalibrationTargetPoints(): Observable<
Array<CalibrationPoint>
> {
return this.http.get<Array<CalibrationPoint>>(
this.baseUrl + this.getTargetValuesRoute
);
}
public getCalibrationMatrix(): Observable<CalibrationTransform> {
return this.http.get<CalibrationTransform>(
this.baseUrl + this.getCalibrationMatrixRoute
);
}
public applyCalibration(): Observable<CalibrationTransform> {
return this.http.get<CalibrationTransform>(
this.baseUrl + this.applyCalibrationRoute
);
}
public restartCalibration(): Observable<CalibrationTransform> {
return this.http.get<CalibrationTransform>(
this.baseUrl + this.restartCalibrationRoute
);
}
public saveCalibration(): Observable<CalibrationTransform> {
return this.http.get<CalibrationTransform>(
this.baseUrl + this.saveCalibrationRoute
);
}
public updateFrameSize(
newSize: FrameSizeDefinition
): Observable<HttpResponse<FrameSizeDefinition>> {
return this.http.post<FrameSizeDefinition>(
this.baseUrl + this.updateFrameSizeRoute,
newSize,
{ observe: 'response' }
);
}
public updateCalibrationPoint(
idx: number,
target: CalibrationPoint
): Observable<HttpResponse<CalibrationTransform>> {
return this.http.post<CalibrationTransform>(
`${this.baseUrl}${this.updateCalibrationPointRoute}${idx}`,
target,
{ observe: 'response' }
);
}
public computeCalibratedPosition(
rawInteractions: Array<Interaction>
): Observable<HttpResponse<Array<Interaction>>> {
return this.http.post<Array<Interaction>>(
`${this.baseUrl}${this.calibrateInteractionsRoute}`,
rawInteractions,
{ observe: 'response' }
);
}
public computeCalibratedAbsolutePosition(
rawInteractions: Array<Interaction>
): Observable<CompleteInteractionData> {
return this.computeCalibratedPosition(rawInteractions).pipe(
concatMap((result) => {
let normalized: Array<Interaction> = [];
if (result.body && result.ok) {
normalized = result.body;
}
return this.getFrameSize().pipe(
map((frame) => ({
raw: rawInteractions,
normalized: normalized,
absolute: this.transform(frame, normalized)
}))
);
})
);
}
public getCalibration(): Observable<Calibration> {
const calibration$ = fromEventPattern<Calibration>(
(handler) => this.connection.on('Calibration', handler),
(handler) => this.connection.off('Calibration', handler)
);
return using(
() => {
this.connection.send('StartCalibrationSubscription').catch((error) => {
console.error(error);
this.logService.sendErrorLog(`${error}`);
});
return {
// @todo: check if this is correct
// eslint-disable-next-line @typescript-eslint/no-misused-promises
unsubscribe: async () =>
this.connection
.send('StopCalibrationSubscription')
.catch((error) => {
console.error(error);
this.logService.sendErrorLog(`${error}`);
})
};
},
() => calibration$
);
}
private transform(
borders: FrameSizeDefinition,
normalized: Array<Interaction>
): Array<Interaction> {
const calibrated: Array<Interaction> = [];
normalized.forEach((norm) => {
const x = (norm.position.x * borders.width) + borders.left;
const y = (norm.position.y * borders.height) + borders.top;
const absolutePos: Point3 = {
x: x,
y: y,
z: norm.position.z,
isValid: norm.position.isValid,
isFiltered: norm.position.isFiltered
};
calibrated.push({
touchId: norm.touchId,
position: absolutePos,
confidence: norm.confidence,
time: norm.time,
type: norm.type,
extremumDescription: norm.extremumDescription
});
});
return calibrated;
}
}
|