-
Notifications
You must be signed in to change notification settings - Fork 208
/
StandardView.ts
94 lines (83 loc) · 4.09 KB
/
StandardView.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Views
*/
import { Matrix3d } from "@itwin/core-geometry";
/** Describes a set of commonly-used view rotations.
* @public
* @extensions
*/
export enum StandardViewId {
/** Any rotation which does not match one of the standard rotations.
* Invalid as an argument to StandardView.getStandardRotation() - used as a return value only.
*/
NotStandard = -1,
Top = 0,
Bottom = 1,
Left = 2,
Right = 3,
Front = 4,
Back = 5,
Iso = 6,
RightIso = 7,
}
let standardViewMatrices: Matrix3d[] | undefined;
function getMatrices(): Matrix3d[] {
if (undefined !== standardViewMatrices)
return standardViewMatrices;
standardViewMatrices = [];
standardViewMatrices[StandardViewId.Top] = Matrix3d.identity;
standardViewMatrices[StandardViewId.Bottom] = Matrix3d.createRowValues(1, 0, 0, 0, -1, 0, 0, 0, -1);
standardViewMatrices[StandardViewId.Left] = Matrix3d.createRowValues(0, -1, 0, 0, 0, 1, -1, 0, 0);
standardViewMatrices[StandardViewId.Right] = Matrix3d.createRowValues(0, 1, 0, 0, 0, 1, 1, 0, 0);
standardViewMatrices[StandardViewId.Front] = Matrix3d.createRowValues(1, 0, 0, 0, 0, 1, 0, -1, 0);
standardViewMatrices[StandardViewId.Back] = Matrix3d.createRowValues(-1, 0, 0, 0, 0, 1, 0, 1, 0);
standardViewMatrices[StandardViewId.Iso] = Matrix3d.createRowValues(
0.707106781186548, -0.70710678118654757, 0.00000000000000000,
0.408248290463863, 0.40824829046386302, 0.81649658092772603,
-0.577350269189626, -0.57735026918962573, 0.57735026918962573);
standardViewMatrices[StandardViewId.RightIso] = Matrix3d.createRowValues(
0.707106781186548, 0.70710678118654757, 0.00000000000000000,
-0.408248290463863, 0.40824829046386302, 0.81649658092772603,
0.577350269189626, -0.57735026918962573, 0.57735026918962573);
standardViewMatrices.forEach((mat) => Object.freeze(mat));
return standardViewMatrices;
}
/** Supplies access to a set of commonly-used view rotations.
* @public
*/
export class StandardView {
public static get top(): Matrix3d { return this.getStandardRotation(StandardViewId.Top); }
public static get bottom(): Matrix3d { return this.getStandardRotation(StandardViewId.Bottom); }
public static get left(): Matrix3d { return this.getStandardRotation(StandardViewId.Left); }
public static get right(): Matrix3d { return this.getStandardRotation(StandardViewId.Right); }
public static get front(): Matrix3d { return this.getStandardRotation(StandardViewId.Front); }
public static get back(): Matrix3d { return this.getStandardRotation(StandardViewId.Back); }
public static get iso(): Matrix3d { return this.getStandardRotation(StandardViewId.Iso); }
public static get rightIso(): Matrix3d { return this.getStandardRotation(StandardViewId.RightIso); }
/** Obtain a [[Matrix3d]] corresponding to the specified [[StandardViewId]].
* @param id The ID of the desired rotation.
* @return A rotation matrix corresponding to the requested standard view ID, or a "top" view rotation if the input does not correspond to a standard rotation.
*/
public static getStandardRotation(id: StandardViewId): Matrix3d {
if (id < StandardViewId.Top || id > StandardViewId.RightIso)
id = StandardViewId.Top;
return getMatrices()[id];
}
/** Attempts to adjust the supplied rotation matrix to match the standard view rotation it most closely matches.
* If a matching standard rotation exists, the input matrix will be modified in-place to precisely match it.
* Otherwise, the input matrix will be unmodified.
* @param matrix The rotation matrix to adjust.
*/
public static adjustToStandardRotation(matrix: Matrix3d): void {
getMatrices().some((test) => {
if (test.maxDiff(matrix) > 1.0e-7)
return false;
matrix.setFrom(test);
return true;
});
}
}