-
Notifications
You must be signed in to change notification settings - Fork 2
/
PointTypeConverter.ts
169 lines (145 loc) · 4.89 KB
/
PointTypeConverter.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module TypeConverters
*/
import type { Primitives } from "@itwin/appui-abstract";
import { StandardTypeNames } from "@itwin/appui-abstract";
import { isPromiseLike } from "@itwin/core-react";
import { TypeConverter } from "./TypeConverter";
import { TypeConverterManager } from "./TypeConverterManager";
import type { ConvertedPrimitives } from "./valuetypes/ConvertedTypes";
// cSpell:ignore valuetypes
/**
* Point type converter.
* @public
*/
export abstract class BasePointTypeConverter extends TypeConverter {
public componentConverterName: string;
public constructor(
componentConverterName: string = StandardTypeNames.Double
) {
super();
this.componentConverterName = componentConverterName;
}
private formatValue(value: number | string): string | Promise<string> {
if (typeof value === "string") value = parseFloat(value);
return TypeConverterManager.getConverter(
this.componentConverterName
).convertToString(value);
}
public override convertToString(
value?: Primitives.Point
): string | Promise<string> {
if (!value) return "";
let components = new Array<string | Promise<string>>();
if (Array.isArray(value)) {
if (value.length === 0) return "";
// eslint-disable-next-line @typescript-eslint/no-floating-promises
components = (value as Array<string | number>).map(
(c): string | Promise<string> => this.formatValue(c)
);
} else {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
components = [this.formatValue(value.x), this.formatValue(value.y)];
if (undefined !== (value as any).z)
components.push(this.formatValue((value as any).z));
}
const hasAsyncComponents = components.some(isPromiseLike);
if (hasAsyncComponents) {
return Promise.all(
components.map(async (c) => (isPromiseLike(c) ? c : Promise.resolve(c)))
).then((c) => c.join(", "));
}
return components.join(", ");
}
public override convertFromString(value: string) {
return this.constructPoint(value.split(","));
}
protected abstract constructPoint(
_values: Primitives.Point
): ConvertedPrimitives.Point | undefined;
protected abstract getVectorLength(
point: Primitives.Point
): number | undefined;
public sortCompare(
a: Primitives.Point,
b: Primitives.Point,
_ignoreCase?: boolean
): number {
const aLength = this.getVectorLength(a);
const bLength = this.getVectorLength(b);
if (aLength === bLength) return 0;
if (aLength === undefined) return -1;
if (bLength === undefined) return 1;
return aLength - bLength;
}
}
/**
* Point2d type converter.
* @public
*/
export class Point2dTypeConverter extends BasePointTypeConverter {
public constructor(componentConverterName?: string) {
super(componentConverterName);
}
protected getVectorLength(point: Primitives.Point): number | undefined {
const derivedPoint = this.constructPoint(point);
if (derivedPoint === undefined) return undefined;
return Math.sqrt(Math.pow(derivedPoint.x, 2) + Math.pow(derivedPoint.y, 2));
}
protected constructPoint(
values: Primitives.Point
): ConvertedPrimitives.Point2d | undefined {
if (Array.isArray(values)) {
if (values.length !== 2 || isNaN(+values[0]) || isNaN(+values[1]))
return undefined;
return { x: +values[0], y: +values[1] };
}
return values;
}
}
TypeConverterManager.registerConverter(
StandardTypeNames.Point2d,
Point2dTypeConverter
);
/**
* Point3d type converter.
* @public
*/
export class Point3dTypeConverter extends BasePointTypeConverter {
public constructor(componentConverterName?: string) {
super(componentConverterName);
}
protected getVectorLength(point: Primitives.Point): number | undefined {
const derivedPoint = this.constructPoint(point);
if (derivedPoint === undefined) return undefined;
return Math.sqrt(
Math.pow(derivedPoint.x, 2) +
Math.pow(derivedPoint.y, 2) +
Math.pow(derivedPoint.z, 2)
);
}
protected constructPoint(
values: Primitives.Point
): ConvertedPrimitives.Point3d | undefined {
if (Array.isArray(values)) {
if (
values.length !== 3 ||
isNaN(+values[0]) ||
isNaN(+values[1]) ||
isNaN(+values[2])
)
return undefined;
return { x: +values[0], y: +values[1], z: +values[2] };
}
const z = (values as any).z;
return { ...values, z: z ? z : 0 };
}
}
TypeConverterManager.registerConverter(
StandardTypeNames.Point3d,
Point3dTypeConverter
);