/
ArrowFH.ts
200 lines (193 loc) · 5.74 KB
/
ArrowFH.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
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
import { Facet } from '../core/Facet';
import { Color } from '../core/Color';
import { ContextManager } from '../core/ContextManager';
import { Renderable } from '../core/Renderable';
import { Geometric3 } from '../math/Geometric3';
import { normVectorE3 } from '../math/normVectorE3';
import { VectorE3 } from '../math/VectorE3';
import { ArrowHead } from './ArrowHead';
import { ArrowOptions } from './ArrowOptions';
import { ArrowTail } from './ArrowTail';
/**
* An arrow with a fixed head and variable length.
*/
export class ArrowFH implements Renderable {
private readonly head: ArrowHead;
private readonly tail: ArrowTail;
private readonly $vector: Geometric3 = Geometric3.zero(false);
private $vectorLock: number;
private readonly $position: Geometric3 = Geometric3.zero(false);
private $positionLock: number;
private readonly $attitude: Geometric3 = Geometric3.zero(false);
private $attitudeLock: number;
private readonly $color: Color = Color.fromRGB(1, 1, 1);
private $colorLock: number;
private $isHeadVisible = true;
/**
* @param contextManager This will usually be provided by the `Engine`.
* @param options
*/
constructor(contextManager: ContextManager, options: Partial<ArrowOptions> = {}) {
this.head = new ArrowHead(contextManager, options);
this.tail = new ArrowTail(contextManager, options);
this.$vector.copyVector(this.head.vector).addVector(this.tail.vector);
this.$vectorLock = this.$vector.lock();
this.$position.copyVector(this.tail.position);
this.$positionLock = this.$position.lock();
this.$attitude.copySpinor(this.tail.attitude);
this.$attitudeLock = this.$attitude.lock();
this.$color.copy(this.tail.color);
this.$colorLock = this.$color.lock();
this.updateHeadAttitude();
this.updateHeadPosition();
}
name: string;
transparent: boolean;
render(ambients: Facet[]): void {
if (this.$isHeadVisible) {
this.head.render(ambients);
}
this.tail.render(ambients);
}
/**
* @hidden
*/
contextFree(): void {
this.head.contextFree();
this.tail.contextFree();
}
/**
* @hidden
*/
contextGain(): void {
this.head.contextGain();
this.tail.contextGain();
}
/**
* @hidden
*/
contextLost(): void {
this.head.contextLost();
this.tail.contextLost();
}
addRef(): number {
this.head.addRef();
return this.tail.addRef();
}
release(): number {
this.head.release();
return this.tail.release();
}
/**
* The vector from the tail of the arrow to the head of the arrow.
*/
get vector(): Geometric3 {
return this.$vector;
}
set vector(vector: Geometric3) {
this.$vector.unlock(this.$vectorLock);
this.$vector.copyVector(vector);
this.$vectorLock = this.$vector.lock();
const magnitude = normVectorE3(vector);
const heightShaft = magnitude - this.head.heightCone;
if (heightShaft >= 0) {
this.$isHeadVisible = true;
this.tail.heightShaft = heightShaft;
this.updateHeadPosition();
} else {
this.$isHeadVisible = false;
this.tail.heightShaft = magnitude;
}
// Don't try to set the direction for the zero vector.
if (magnitude > 0) {
this.head.axis = vector;
this.tail.axis = vector;
}
this.updateHeadPosition();
}
/**
* @hidden
*/
isZombie(): boolean {
if (this.head.isZombie()) {
if (this.tail.isZombie()) {
return true;
} else {
throw new Error();
}
} else {
if (this.tail.isZombie()) {
throw new Error();
} else {
return false;
}
}
}
/**
* Alias for `position`.
*/
get X(): Geometric3 {
return this.$position;
}
set X(X: Geometric3) {
this.setPosition(X);
}
/**
* The position (vector).
*/
get position(): Geometric3 {
return this.$position;
}
set position(position: Geometric3) {
this.setPosition(position);
}
/**
* Alias for `attitude`.
*/
get R(): Geometric3 {
return this.$attitude;
}
set R(R: Geometric3) {
this.setAttitude(R);
}
/**
* The attitude (spinor).
*/
get attitude(): Geometric3 {
return this.$attitude;
}
set attitude(attitude: Geometric3) {
this.setAttitude(attitude);
}
get color(): Color {
return this.$color;
}
set color(color: Color) {
this.$color.unlock(this.$colorLock);
this.$color.copy(color);
this.$colorLock = this.$color.lock();
this.head.color = color;
this.tail.color = color;
}
private setPosition(position: Geometric3): void {
this.$position.unlock(this.$positionLock);
this.$position.copyVector(position);
this.$positionLock = this.$position.lock();
this.tail.position = position;
this.updateHeadPosition();
}
private setAttitude(attitude: Geometric3): void {
this.$attitude.unlock(this.$attitudeLock);
this.$attitude.copySpinor(attitude);
this.$attitudeLock = this.$attitude.lock();
this.tail.attitude = attitude;
this.updateHeadPosition();
this.updateHeadAttitude();
}
private updateHeadPosition(): void {
this.head.position.copyVector(this.tail.position).addVector(this.tail.vector);
}
private updateHeadAttitude(): void {
this.head.attitude = this.tail.attitude;
}
}