/
PointCloud.ts
1623 lines (1328 loc) · 46 KB
/
PointCloud.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import GLEnv from "./GLEnv";
import Scene from "./Scene";
import Mesh from "./Mesh";
import RenderStage from "./RenderStage";
import GeoMath, { Vector3, Vector4 } from "./GeoMath";
import GeoPoint from "./GeoPoint";
import PointCloudMaterial, { PointCloudDebugWireMaterial, PointCloudDebugFaceMaterial, PointCloudPickMaterial } from "./PointCloudMaterial";
import PointCloudProvider from "./PointCloudProvider";
import PointCloudBoxCollector from "./PointCloudBoxCollector";
/**
* 点群データを表現するクラス
*
* ```typescript
* const provider = new mapray.RawPointCloudProvider({
* resource: {
* prefix: "https://..."
* }
* });
* const point_cloud = viewer.point_cloud_collection.add( provider );
* point_cloud.setPointShape( mapray.PointCloud.PointShapeType.GRADIENT_CIRCLE );
* ```
*
* @see [[PointCloudProvider]]
*/
class PointCloud {
private _glenv: GLEnv;
private _scene: Scene;
private _provider: PointCloudProvider;
private _root: PointCloud.Box;
private _points_per_pixel: number;
private _point_shape: PointCloud.PointShapeType;
private _point_size_type: PointCloud.PointSizeType;
private _point_size: number;
private _point_size_limit: number;
private _dispersion: boolean;
private _rid?: number;
// for debug
private _debug_shader: boolean;
private _debug_render_box: boolean;
private _debug_render_ellipsoid: boolean;
private _debug_render_axis: boolean;
private _debug_render_section: boolean;
/**
* @param scene 所属するシーン
* @param provider プロバイダ
*/
constructor( scene: Scene, provider: PointCloudProvider )
{
this._glenv = scene.glenv;
this._scene = scene;
this._provider = provider;
this._root = PointCloud.Box.createRoot( this );
// properties
this._points_per_pixel = 0.7;
this._point_shape = PointCloud.PointShapeType.CIRCLE;
this._point_size_type = PointCloud.PointSizeType.FLEXIBLE;
this._point_size = 1;
this._point_size_limit = 10;
// hidden properties
this._dispersion = true;
this._debug_shader = false;
// for debug
this._debug_render_box = false;
this._debug_render_ellipsoid = false;
this._debug_render_axis = false;
this._debug_render_section = false;
this._checkMaterials();
PointCloud._instances.push(this);
}
/**
* 初期化
* [[PointCloudBoxCollector]]へ追加時に自動的に呼ばれる。
* @internal
*/
async init() {
await this._provider.init();
}
/**
* 破棄
* [[PointCloudBoxCollector]]から削除時に自動的に呼ばれる。
* @internal
*/
async destroy() {
if (this._provider) {
await this._provider.destroy();
}
if (this._root) {
await this._root.dispose();
}
const index = PointCloud._instances.indexOf( this );
if ( index !== -1 ) {
PointCloud._instances.splice( index, 1 );
}
}
/**
* @internal
*/
get glenv() { return this._glenv }
/**
* @internal
*/
get scene() { return this._scene }
/**
* プロバイダ
* @internal
*/
get provider() { return this._provider; }
/**
* ルートBox
* @internal
*/
get root() { return this._root };
// Properties
/**
* 点群Box読み込みを行う際の解像度[points/pixel]
*/
getPointsPerPixel(): number { return this._points_per_pixel; }
/**
* 点群Box読み込みを行う際の解像度[points/pixel]を設定
* @param val 設定する値
*/
setPointsPerPixel( val: number ) {
console.assert( val <= 1 );
this._points_per_pixel = val;
}
/**
* 点を描画する際の形状
* @return [[PointCloud.PointShapeType]]
*/
getPointShape() { return this._point_shape; }
/**
* 点を描画する際の形状を設定
* @param {PointCloud.PointShapeType} val 設定する値
*/
setPointShape( val: PointCloud.PointShapeType ) {
this._point_shape = val;
}
/**
* 点を描画する際のサイズの指定方法
* @return {PointCloud.PointSizeType}
*/
getPointSizeType() { return this._point_size_type; }
/**
* 点を描画する際のサイズの指定方法を設定
* @param val 設定する値
*/
setPointSizeType( val: PointCloud.PointSizeType ) {
this._point_size_type = val;
}
/**
* 点を描画する際のサイズ
* point_size_typeにより単位が異なる
* @see [[PointCloud.getPointSizeType]]
*/
getPointSize(): number { return this._point_size; }
/**
* 点を描画する際のサイズを設定。
* [[PointCloud.setPointSizeType]]により指定された値によって解釈される単位が異なる。
* @param val 設定する値
*/
setPointSize( val: number ) {
console.assert( val > 0 );
this._point_size = val;
}
/**
* 点を描画する際の最大ピクセルサイズ
*/
getPointSizeLimit(): number { return this._point_size_limit; }
/**
* 点を描画する際の最大ピクセルサイズを設定
* @param val 設定する値
*/
setPointSizeLimit( val: number ) {
console.assert( val > 0 );
this._point_size_limit = val;
}
/**
* Render ID をセットする
* @param id render id
* @internal
*/
setRenderId( id: number ): void {
this._rid = id;
}
/**
* Render IDを取得する
* @returns render ID
* @internal
*/
getRenderId(): number | undefined {
return this._rid;
}
// hidden properties
/** @internal */
getDispersion() { return this._dispersion }
/** @internal */
setDispersion( val: boolean ) { this._dispersion = val; }
// for debug
/** @internal */
getDebugShader() { return this._debug_shader; }
/** @internal */
setDebugShader( val: boolean ) { this._debug_shader = val; }
/** @internal */
getDebugRenderBox() { return this._debug_render_box; }
/** @internal */
setDebugRenderBox( val: boolean ) { this._debug_render_box = val; this._updateDebugMesh(); }
/** @internal */
getDebugRenderEllipsoid() { return this._debug_render_ellipsoid }
/** @internal */
setDebugRenderEllipsoid( val: boolean ) { this._debug_render_ellipsoid = val; this._updateDebugMesh(); }
/** @internal */
getDebugRenderAxis() { return this._debug_render_axis; }
/** @internal */
setDebugRenderAxis( val: boolean ) { this._debug_render_axis = val; this._updateDebugMesh(); }
/** @internal */
getDebugRenderSection() { return this._debug_render_section; }
/** @internal */
setDebugRenderSection( val: boolean ) { this._debug_render_section = val; this._updateDebugMesh(); }
/** @internal */
_updateDebugMesh() {
if ( this._root ) {
this._root._updateDebugMeshes();
}
}
/**
* Traverse結果の統計情報を取得。
* リクエストキューに登録し、[[RenderStage]]が処理を完了するのを待つ。
* @return {Promise<PointCloud.Statistics>}
* @internal
*/
private static async requestTraverseSummary(): Promise<PointCloud.Statistics> {
return new Promise(onSuccess => {
const notifier = (statistics: PointCloud.Statistics) => {
onSuccess(statistics);
const index = PointCloud.getTraverseDataRequestQueue().indexOf(notifier);
if (index !== -1) PointCloud.getTraverseDataRequestQueue().splice(index, 1);
};
PointCloud.getTraverseDataRequestQueue().push( notifier );
});
}
/**
* Traverse結果取得用のリクエストキューを取得
* @internal
*/
private static getTraverseDataRequestQueue() {
return PointCloud._traverseDataRequestQueue || (PointCloud._traverseDataRequestQueue=[]);
}
/* *
* 指定された level, x, y, z のURLを生成します
* @param level
* @param x
* @param y
* @param z
* /
private getURL( level: number, x: number, y: number, z: number ): string
{
return this._urlGenerator( level, x, y, z );
}
*/
/**
*/
private _checkMaterials() {
const viewer = this._scene.viewer;
const render_cache: RenderCacheType = viewer._render_cache as RenderCacheType || ( viewer._render_cache = {} );
if ( !render_cache.point_cloud_materials ) {
const map = new Map<PointCloud.PointShapeType, PointCloudMaterial>();
PointCloud.ListOfPointShapeTypes.forEach( point_shape_type => {
map.set( point_shape_type, new PointCloudMaterial( viewer, { point_shape_type } ) );
} );
render_cache.point_cloud_materials = map;
}
if ( !render_cache.point_cloud_pick_materials ) {
const map = new Map<PointCloud.PointShapeType, PointCloudPickMaterial>();
PointCloud.ListOfPointShapeTypes.forEach( point_shape_type => {
map.set( point_shape_type, new PointCloudPickMaterial( viewer, { point_shape_type } ) );
} );
render_cache.point_cloud_pick_materials = map;
}
if ( !render_cache.point_cloud_debug_wire_material ) {
render_cache.point_cloud_debug_wire_material = new PointCloudDebugWireMaterial( viewer );
}
if ( !render_cache.point_cloud_debug_face_material ) {
render_cache.point_cloud_debug_face_material = new PointCloudDebugFaceMaterial( viewer );
}
}
/**
* @internal
*/
getMaterial( point_shape: PointCloud.PointShapeType, renderTarget: RenderStage.RenderTarget ): PointCloudMaterial {
const viewer = this._scene.viewer;
const render_cache: RenderCacheType = viewer._render_cache as RenderCacheType || ( viewer._render_cache = {} );
const materials = (
renderTarget === RenderStage.RenderTarget.SCENE ? render_cache.point_cloud_materials :
render_cache.point_cloud_pick_materials
);
const material = materials.get( point_shape );
if ( !material ) {
throw new Error( 'pointcloud material not initialized' );
}
return material;
}
/**
* @internal
*/
static setStatisticsHandler( statistics_handler: PointCloud.StatisticsHandler ) {
if (statistics_handler) {
PointCloud._statistics = {
statistics_obj: new PointCloud.Statistics(),
statistics_handler: statistics_handler,
};
}
}
/** @internal */
private static getStatistics() { return PointCloud._statistics; }
/** @internal */
private static getStatisticsHandler() { return PointCloud._statistics.statistics_handler; }
/** @internal */
private static _statistics: {
statistics_obj: PointCloud.Statistics,
statistics_handler: PointCloud.StatisticsHandler,
};
/** @internal */
private static _instances: PointCloud[] = [];
/** @internal */
private static _traverseDataRequestQueue: any[] = [];
}
type RenderCacheType = {
point_cloud_materials: Map<PointCloud.PointShapeType, PointCloudMaterial>;
point_cloud_debug_wire_material: PointCloudDebugWireMaterial;
point_cloud_debug_face_material: PointCloudDebugFaceMaterial;
point_cloud_pick_materials: Map<PointCloud.PointShapeType, PointCloudPickMaterial>;
}
namespace PointCloud {
/**
* @internal
*/
export class Statistics {
render_point_count: number;
total_point_count: number;
render_boxes: number;
total_boxes: number;
loading_boxes: number;
created_boxes: number;
disposed_boxes: number;
total_time: number;
traverse_time: number;
render_time: number;
private _start_time: number;
private _done_time: number;
private _done_traverse_time: number;
_now: () => number;
constructor() {
this._now = performance ? () => performance.now() : () => Date.now();
this.clear();
this.render_point_count = 0;
this.total_point_count = 0;
this.render_boxes = 0;
this.total_boxes = 0;
this.loading_boxes = 0;
this.created_boxes = 0;
this.disposed_boxes = 0;
this.total_time = 0.0;
this.traverse_time = 0.0;
this.render_time = 0.0;
this._start_time = -1;
this._done_time = -1;
this._done_traverse_time = -1;
};
start() {
this._start_time = this._now();
}
doneTraverse() {
this._done_traverse_time = this._now();
this.traverse_time += this._done_traverse_time - this._start_time;
}
done() {
this._done_time = this._now();
this.render_time += this._done_time - this._done_traverse_time;
this.total_time += this._done_time - this._start_time;
}
clear() {
this.render_point_count = 0;
this.total_point_count = 0;
this.render_boxes = 0;
this.total_boxes = 0;
this.loading_boxes = 0;
this.created_boxes = 0;
this.disposed_boxes = 0;
this.total_time = 0.0;
this.traverse_time = 0.0;
this.render_time = 0.0;
this._start_time = -1;
this._done_time = -1;
this._done_traverse_time = -1;
}
}
export type StatisticsHandler = (statistics_obj: PointCloud.Statistics) => void;
/**
* 点描画の種類
*/
export enum PointShapeType {
/**
* 矩形
*/
RECTANGLE,
/**
* 円
*/
CIRCLE,
/**
* 境界線付きの円
*/
CIRCLE_WITH_BORDER,
/**
* グラデーションで塗り潰した円
*/
GRADIENT_CIRCLE,
};
export const ListOfPointShapeTypes = [
PointShapeType.RECTANGLE,
PointShapeType.CIRCLE,
PointShapeType.CIRCLE_WITH_BORDER,
PointShapeType.GRADIENT_CIRCLE,
];
/**
* 点描画のサイズ指定方法の種類
*/
export enum PointSizeType {
/**
* setPointSize()により指定された値をピクセルとして解釈する
*/
PIXEL,
/**
* setPointSize()により指定された値をmmとして解釈する
*/
MILLIMETERS,
/**
* setPointSize()により指定された値を参照せず、表示位置に応じて適切なサイズを自動的に指定する。
*/
FLEXIBLE,
};
/**
* 点群ツリーを構成するノード。
* ルート要素(level === 0) は、Box.createRoot()を用いて作成する。
* @internal
*/
export class Box {
/**
* 親Box
*/
private _parent?: Box;
/**
* 所属するPointCloud。
* ルート要素の場合は [[Box.createRoot]] で設定される。
*/
private _owner!: PointCloud;
/** レベル */
level: number;
/** x */
x: number;
/** y */
y: number;
/** z */
z: number;
/**
* Box一辺の半分の長さ
* @internal
*/
size: number;
/**
* 軸方向に投影した際の面積
* @internal
*/
proj_area: number;
/**
* GOCS座標系でのBoxの中心位置
* @internal
*/
gocs_center: Vector3;
/**
* GOCS座標系でのBoxの最小位置
* @internal
*/
gocs_min: Vector3;
/**
* GOCS座標系でのBoxの最大位置
* @internal
*/
gocs_max: Vector3;
/**
*
* @internal
*/
private _status: PointCloud.Status;
/**
* 子Box、セルに関する情報
*/
private _metaInfo!: PointCloud.BoxInfo;
/**
* 子Box。
* `(u, v, w)` のインデックスは `(u | v << 1 | w << 2)` によって算出される。
* @internal
*/
private _children: [ Box?, Box?, Box?, Box?, Box?, Box?, Box?, Box? ];
/**
* @internal
*/
average!: Vector3;
/**
* @internal
*/
eigenVector!: [ Vector3, Vector3, Vector3 ];
/**
* @internal
*/
eigenVectorLength!: [ number, number, number ];
/**
* @internal
*/
private _vertex_buffer!: WebGLBuffer;
/**
* @internal
*/
private _vertex_length!: number;
/**
* @internal
*/
private _vertex_attribs!: object;
/**
* @internal
*/
private debug1!: number;
/**
* @internal
*/
private _loadId?: number;
private _abort_controller!: AbortController; // @ToDo: 必要性を確認
private _debugMesh?: Mesh[];
/**
* @param parent 親Box(level === 0の場合はnull)
* @param level レベル
* @param x x
* @param y y
* @param z z
*/
constructor( parent: Box | undefined, level: number, x: number, y: number, z: number )
{
this._parent = parent;
if (parent) {
this._owner = parent._owner;
}
this.level = level;
this.x = x;
this.y = y;
this.z = z;
/*
2次元(X,Y)までを下記に図示する。Z軸についても同様。
^ Y
|
+-------------+-------------M
| cell (0, 1) | cell (1, 1) |
| | | c: gocs_center [GOCS]
| | | m: gocs_min [GOCS]
| | | M: gocs_max [GOCS]
| | |
+-------------c-------------+
| cell (0, 0) | cell (1, 0) |
| | |
| | |
| | |
| | |
m-------------+-------------+ --> X
|<--size[m]-->|
*/
const size = this.size = (
level === 0 ? 2147483648: // 2^31
level < 31 ? 1 << (31-level):
Math.pow(0.5, level-31)
);
this.proj_area = 4.0 * size * size;
this.gocs_center = GeoMath.createVector3([
MIN_INT + (2 * x + 1) * size,
MIN_INT + (2 * y + 1) * size,
MIN_INT + (2 * z + 1) * size
]);
this.gocs_min = GeoMath.createVector3([
this.gocs_center[0] - size,
this.gocs_center[1] - size,
this.gocs_center[2] - size
]);
this.gocs_max = GeoMath.createVector3([
this.gocs_center[0] + size,
this.gocs_center[1] + size,
this.gocs_center[2] + size
]);
this._status = Box.Status.NOT_LOADED;
this._children = [
undefined, undefined, undefined, undefined,
undefined, undefined, undefined, undefined,
];
if ( this._owner ) {
this._updateDebugMesh();
}
}
/**
* 読み込み済みの `Box` を引数として、指定されたコールバック関数を呼び出します。
* 読み込み済みとは、`Box.status == PointCloud.Status.LOADED` を満たす `Box` を指します。
* @param callback コールバック関数
*/
forEach( callback: ( box: PointCloud.Box ) => void ) {
for (let i=0; i<this._children.length; i++) {
const child = this._children[i];
if ( child && child.status === Box.Status.LOADED ) {
child.forEach( callback );
}
}
callback( this );
}
/**
* デバッグ表示用のメッシュを生成します。
* @internal
*/
_updateDebugMeshes() {
this._updateDebugMesh();
for (let i=0; i<this._children.length; i++) {
const child = this._children[i];
if ( child ) {
child._updateDebugMeshes();
}
}
}
/**
* デバッグ表示用のメッシュを生成します。
* @internal
*/
_updateDebugMesh() {
const vertices = [];
const indices = [];
const tindices = [];
if ( this._owner.getDebugRenderBox() ) {
/*
* 4----------5
* .´: .´|
* .´ : .´ |
* 0----------1 |
* | 6 - - |----7
* | .´ | .´
* |.´ |.´
* 2----------3
*/
for ( let i=0; i<PointCloud.CHILDREN_INDICES.length; i++) {
vertices.push(
this.size * (2 * PointCloud.CHILDREN_INDICES[i][2] - 1),
this.size * (2 * PointCloud.CHILDREN_INDICES[i][1] - 1),
this.size * (2 * PointCloud.CHILDREN_INDICES[i][0] - 1)
);
}
indices.push(
0, 1, 1, 3, 3, 2, 2, 0,
4, 5, 5, 7, 7, 6, 6, 4,
0, 4, 1, 5, 3, 7, 2, 6,
);
//*
tindices.push(
0, 2, 1, 1, 2, 3,
4, 5, 6, 7, 6, 5,
0, 1, 4, 1, 5, 4,
1, 3, 5, 3, 7, 5,
3, 6, 7, 3, 2, 6,
6, 2, 4, 2, 0, 4,
);
//*/
}
if ( this.average && !isNaN( this.eigenVector[0][0] ) ) {
if ( this._owner.getDebugRenderAxis() ) { // Render Normal
let offset = vertices.length / 3;
for ( let i=0; i<3; i++) {
const len = Math.max(0.2, this.eigenVectorLength[i]);
const ev = this.eigenVector[i];
for ( let j=0; j<3; j++ ) vertices.push( this.average[j] - len * ev[j] );
for ( let j=0; j<3; j++ ) vertices.push( this.average[j] + len * ev[j] );
indices.push( offset++, offset++ );
}
}
if ( this._owner.getDebugRenderSection() ) {
if ( this.level > 20 && this.vertex_length > 5000 && this.eigenVectorLength[0] < this.size * 0.2 ) { // = 10% = (2 * s) / 10
this._putSectionShapePoints(vertices, indices, tindices); // Render Cross Section
}
}
if ( this._owner.getDebugRenderEllipsoid() ) {
this._putVariancePoints(vertices, indices, tindices); // Render Normal Ring
}
}
const meshes = [];
if (indices.length > 0) {
const mesh_data = {
vtype: [
{ name: "a_position", size: 3 }
],
ptype: "lines",
vertices: vertices,
indices: indices
};
meshes.push( new Mesh( this._owner.glenv, mesh_data ) );
}
if (tindices.length > 0) {
const mesh_data = {
vtype: [
{ name: "a_position", size: 3 }
],
ptype: "triangles",
vertices: vertices,
indices: tindices
};
meshes.push( new Mesh( this._owner.glenv, mesh_data ) );
}
this._debugMesh = meshes;
}
/**
* @internal
*/
private _putVariancePoints( vertices: number[], indices: number[], tindices: number[] ) {
const offset = vertices.length / 3;
const [ e1, e2, e3 ] = this.eigenVector;
const [ e1l, e2l, e3l ] = this.eigenVectorLength;
const cache = PointCloud._variance_points_cache;
const G = cache.G;
const N = cache.N;
const putPoint = (ro: number, th: number, vs: number[]) => {
const cos_ro = cache.cos_ro[ro];
const sin_ro = cache.sin_ro[ro];
const cos_th = cache.cos_th[th];
const sin_th = cache.sin_th[th];
vs.push(
this.average[0] + sin_ro * (e2l * cos_th * e2[0] + e3l * sin_th * e3[0]) + e1l * cos_ro * e1[0],
this.average[1] + sin_ro * (e2l * cos_th * e2[1] + e3l * sin_th * e3[1]) + e1l * cos_ro * e1[1],
this.average[2] + sin_ro * (e2l * cos_th * e2[2] + e3l * sin_th * e3[2]) + e1l * cos_ro * e1[2]
);
}
for ( let j=0; j<=G; ++j ) {
for ( let i=0; i<=N; ++i ) {
putPoint(j, i, vertices);
}
}
for ( let j=0; j<G; ++j ) {
for ( let i=0; i<N; ++i ) {
const p = offset + j * (N+1) + i;
indices.push( p, p+1, p, p+N+1 );
tindices.push( p, p+1, p+N+1, p+N+1, p+1, p+N+2 );
}
}
}
/**
* @internal
*/
private _putSectionShapePoints( vertices: number[], indices: number[], tindices: number[] ) {
const offset = vertices.length / 3;
const a = this.average;
const e = this.eigenVector[0];
const s = this.size;
const l = Math.sqrt( e[0] * e[0] + e[1] * e[1] + e[2] * e[2] );
const ue = [ e[0] / l, e[1] / l, e[2] / l ];
const ps = [];
/*
Compute Intersection(c) of Plane(a, ue) and Line(p, v)
\ a: average point
\ ue ue: eigenVector (normal vector)
a-¯¯ p: point
\ v: vector
p---> . . c c = p + alpha v
v \
\ (a - p) ue
|-------->| \ alpha = ------------
alpha v \ v ue
*/
const q = []
for ( let i=0; i<2; ++i ) {
for ( let j=0; j<2; ++j ) {
q.push(
{ p: [i>0?s:-s, j>0?s:-s, 0], v: [0, 0, s] },
{ p: [j>0?s:-s, 0, i>0?s:-s], v: [0, s, 0] },
{ p: [ 0, i>0?s:-s, j>0?s:-s], v: [s, 0, 0] }
)
}
}
let isFirst = true;
const n = [0, 0, 0];
const t = [0, 0, 0];
for ( let i=0; i<q.length; i++ ) {
const p = q[i].p;
const v = q[i].v;
const alpha = ((a[0]-p[0])*ue[0] + (a[1]-p[1])*ue[1] + (a[2]-p[2])*ue[2]) / (v[0]*ue[0] + v[1]*ue[1] + v[2]*ue[2]);
if (Math.abs(alpha) <= 1.0) {
const c = [p[0]+alpha*v[0], p[1]+alpha*v[1], p[2]+alpha*v[2]];
const lp = [c[0]-a[0], c[1]-a[1], c[2]-a[2]];
let angle;
if ( isFirst ) {
angle = 0;
t[0] = ue[1]*lp[2]-ue[2]*lp[1];
t[1] = ue[2]*lp[0]-ue[0]*lp[2];
t[2] = ue[0]*lp[1]-ue[1]*lp[0];
n[0] = t[1]*ue[2]-t[2]*ue[1];
n[1] = t[2]*ue[0]-t[0]*ue[2];