-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbasis.js
121 lines (84 loc) · 2.82 KB
/
basis.js
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
import { Vector3 } from 'https://app.webaverse.com/three.module.js';
const basesRegex = /^([+-][xyz])([+-][xyz])([+-][xyz])$/i;
const nameToIndex = { x: 0, y: 1, z: 2 };
const orderedVectors = [ new Vector3(), new Vector3(), new Vector3() ];
function stringToAxes( axesString ) {
if ( ! basesRegex.test( axesString ) ) {
return null;
}
axesString = axesString.toLowerCase();
return axesString
.match( basesRegex )
.splice( 1, 3 )
.map( str => {
const negative = str[ 0 ] === '-';
const name = str[ 1 ];
return { negative, name };
} );
}
function getBasisTransform( from, to, targetMatrix ) {
if ( ! basesRegex.test( from ) ) {
return null;
}
if ( ! basesRegex.test( to ) ) {
return null;
}
const fromAxes = stringToAxes( from );
const toAxes = stringToAxes( to );
for ( let i = 0; i < 3; i ++ ) {
const fromAxis = fromAxes[ i ];
const toAxis = toAxes[ i ];
const fromIndex = nameToIndex[ fromAxis.name ];
const equalNegative = fromAxis.negative === toAxis.negative;
const vector = orderedVectors[ fromIndex ];
vector.set( 0, 0, 0 );
vector[ toAxis.name ] = equalNegative ? 1 : - 1;
}
targetMatrix.makeBasis( orderedVectors[ 0 ], orderedVectors[ 1 ], orderedVectors[ 2 ] );
}
function axesToAsciiImage( str ) {
const axes = stringToAxes( str );
const axis0 = axes[ 0 ];
const r = axis0.negative ? ' ' : '-';
const R = axis0.negative ? ' ' : axis0.name.toUpperCase();
const l = axis0.negative ? '-' : ' ';
const L = axis0.negative ? axis0.name.toUpperCase() : ' ';
const axis1 = axes[ 1 ];
const u = axis1.negative ? ' ' : '|';
const U = axis1.negative ? ' ' : axis1.name.toUpperCase();
const d = axis1.negative ? '|' : ' ';
const D = axis1.negative ? axis1.name.toUpperCase() : ' ';
const axis2 = axes[ 2 ];
const f = axis2.negative ? ' ' : '/';
const F = axis2.negative ? ' ' : axis2.name.toUpperCase();
const b = axis2.negative ? '/' : ' ';
const B = axis2.negative ? axis2.name.toUpperCase() : ' ';
const template =
' U \n' +
' u B\n' +
' u b \n' +
'Llllll.rrrrr R\n' +
' fd \n' +
' F d \n' +
' D ';
return template
// right
.replace( /R/g, R )
.replace( /r/g, r )
// left
.replace( /L/g, L )
.replace( /l/g, l )
// up
.replace( /U/g, U )
.replace( /u/g, u )
// down
.replace( /D/g, D )
.replace( /d/g, d )
// forward
.replace( /F/g, F )
.replace( /f/g, f )
// back
.replace( /B/g, B )
.replace( /b/g, b );
}
export { getBasisTransform, axesToAsciiImage, stringToAxes };