/
verbEval.js
3161 lines (2518 loc) · 97.8 KB
/
verbEval.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
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
if ( typeof exports != 'object' || exports === undefined ) // browser context
{
importScripts('labor.js');
importScripts('binomial.js');
importScripts('numeric-1.2.6.min.js');
}
else // node.js context
{
var labor = require('labor');
}
var verb = verb || {};
verb.eval = verb.eval || {};
verb.eval.nurbs = verb.eval.nurbs || {};
verb.eval.mesh = verb.eval.mesh || {};
verb.eval.geom = verb.eval.geom || {};
verb.geom = verb.geom || {};
verb.EPSILON = 1e-8;
verb.TOLERANCE = 1e-3;
var router = new labor.Router(verb.eval.nurbs);
numeric.normalized = function(arr){
return numeric.div( arr, numeric.norm2(arr) );
}
numeric.cross = function(u, v){
return [u[1]*v[2]-u[2]*v[1],u[2]*v[0]-u[0]*v[2],u[0]*v[1]-u[1]*v[0]];
}
//
// ####left(arr)
//
// Get the first half of an array including the pivot
//
// **params**
// + *Array*, array of stuff
//
// **returns**
// + *Array*, the right half
//
verb.left = function(arr){
if (arr.length === 0) return [];
var len = Math.ceil( arr.length / 2 );
return arr.slice( 0, len );
}
//
// ####right(arr)
//
// Get the second half of an array, not including the pivot
//
// **params**
// + *Array*, array of stuff
//
// **returns**
// + *Array*, the right half
//
verb.right = function(arr){
if (arr.length === 0) return [];
var len = Math.ceil( arr.length / 2 );
return arr.slice( len );
}
//
// ####rightWithPivot(arr)
//
// Get the second half of an array including the pivot
//
// **params**
// + *Array*, array of stuff
//
// **returns**
// + *Array*, the right half
//
verb.rightWithPivot = function(arr){
if (arr.length === 0) return [];
var len = Math.ceil( arr.length / 2 );
return arr.slice( len-1 );
}
//
// ####unique(arr, comparator)
//
// Obtain the unique set of elements in an array
//
// **params**
// + *Array*, array of stuff
// + *Function*, a function that receives two arguments (two objects from the array). Returning true indicates
// the objects are equal.
//
// **returns**
// + *Array*, array of unique elements
//
verb.unique = function( arr, comparator ){
if (arr.length === 0) return [];
var uniques = [ arr.pop() ];
for (var i = 0; i < arr.length; i++ ){
var ele = arr.pop();
var isUnique = true;
for (var j = 0; j < uniques.length; j++ ){
if ( comparator( ele, uniques[i] ) ){
isUnique = false;
break;
}
}
if ( isUnique ){
uniques.push( ele );
}
}
return uniques;
}
//
// ####intersect_rational_curve_surface_by_aabb( degree_u, knots_u, degree_v, knots_v, homo_control_points, degree_crv, knots_crv, homo_control_points_crv, sample_tol, tol )
//
// Get the intersection of a NURBS curve and a NURBS surface by axis-aligned bounding box intersection and refinement
//
// **params**
// + *Number*, integer degree of surface in u direction
// + *Array*, array of nondecreasing knot values in u direction
// + *Number*, integer degree of surface in v direction
// + *Array*, array of nondecreasing knot values in v direction
// + *Array*, 3d array of homogeneous control points, top to bottom is increasing u direction, left to right is increasing v direction,
// and where each control point is an array of length (dim+1)
// + *Number*, integer degree of curve
// + *Array*, array of nondecreasing knot values
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1)
// and form (wi*pi, wi)
// + *Number*, the sample tolerance of the curve
// + *Number*, tolerance for the curve intersection
// + *Number*, integer number of divisions of the surface in the U direction for initial approximation (placeholder until adaptive tess of surfaces)
// + *Number*, integer number of divisions of the surface in the V direction for initial approximation (placeholder until adaptive tess of surfaces)
//
// **returns**
// + *Array*, array of intersection objects, each holding:
// - a "point" property where intersections took place
// - a "p" the parameter on the curve
// - a "uv" the parameter on the surface
// - a "face" the index of the face where the intersection took place
//
verb.eval.nurbs.intersect_rational_curve_surface_by_aabb_refine = function( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, degree_crv, knots_crv, homo_control_points_crv, sample_tol, tol, divs_u, divs_v ) {
// get the approximate intersections
var ints = verb.eval.nurbs.intersect_rational_curve_surface_by_aabb( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, degree_crv, knots_crv, homo_control_points_crv, sample_tol, tol, divs_u, divs_v );
// refine them
return ints.map(function( inter ){
// get intersection params
var start_params = [inter.p, inter.uv[0], inter.uv[1] ]
// refine the parameters
, refined_params = verb.eval.nurbs.refine_rational_curve_surface_intersection( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, degree_crv, knots_crv, homo_control_points_crv, start_params );
// update the inter object
inter.p = refined_params[0];
inter.uv[0] = refined_params[1];
inter.uv[1] = refined_params[2];
inter.distance = refined_params[3];
delete inter.face;
return inter;
});
}
//
// ####refine_rational_curve_surface_intersection( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, degree_crv, knots_crv, homo_control_points_crv, start_params )
//
// Refine an intersection pair for a surface and curve given an initial guess. This is an unconstrained minimization,
// so the caller is responsible for providing a very good initial guess.
//
// **params**
// + *Number*, integer degree of surface in u direction
// + *Array*, array of nondecreasing knot values in u direction
// + *Number*, integer degree of surface in v direction
// + *Array*, array of nondecreasing knot values in v direction
// + *Array*, 3d array of homogeneous control points, top to bottom is increasing u direction, left to right is increasing v direction,
// and where each control point is an array of length (dim+1)
// + *Number*, integer degree of curve
// + *Array*, array of nondecreasing knot values
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1)
// and form (wi*pi, wi)
// + *Array*, array of initial parameter values [ u_crv, u_srf, v_srf ]
//
// **returns**
// + *Array*, a length 3 array containing the [ u_crv, u_srf, v_srf, final_distance ]
//
verb.eval.nurbs.refine_rational_curve_surface_intersection = function( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, degree_crv, knots_crv, homo_control_points_crv, start_params ) {
var objective = function(x) {
var p1 = verb.eval.nurbs.rational_curve_point(degree_crv, knots_crv, homo_control_points_crv, x[0])
, p2 = verb.eval.nurbs.rational_surface_point( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, x[1], x[2] )
, p1_p2 = numeric.sub(p1, p2);
return numeric.dot(p1_p2, p1_p2);
}
var sol_obj = numeric.uncmin( objective, start_params);
return sol_obj.solution.concat( sol_obj.f );
}
//
// ####intersect_rational_curve_surface_by_aabb( degree_u, knots_u, degree_v, knots_v, homo_control_points, degree_crv, knots_crv, homo_control_points_crv, sample_tol, tol )
//
// Approximate the intersection of two nurbs surface by axis-aligned bounding box intersection.
//
// **params**
// + *Number*, integer degree of surface in u direction
// + *Array*, array of nondecreasing knot values in u direction
// + *Number*, integer degree of surface in v direction
// + *Array*, array of nondecreasing knot values in v direction
// + *Array*, 3d array of homogeneous control points, top to bottom is increasing u direction, left to right is increasing v direction,
// and where each control point is an array of length (dim+1)
// + *Number*, integer degree of curve
// + *Array*, array of nondecreasing knot values
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1)
// and form (wi*pi, wi)
// + *Array*, array of initial parameter values [ u_crv, u_srf, v_srf ]
// + *Number*, the sample tolerance of the curve
// + *Number*, tolerance for the curve intersection
// + *Number*, integer number of divisions of the surface in the U direction
// + *Number*, integer number of divisions of the surface in the V direction
//
// **returns**
// + *Array*, array of intersection objects, each holding:
// - a "point" property where intersections took place
// - a "p" the parameter on the polyline
// - a "uv" the parameter on the mesh
// - a "face" the index of the face where the intersection took place
//
verb.eval.nurbs.intersect_rational_curve_surface_by_aabb = function( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, degree_crv, knots_crv, homo_control_points_crv, sample_tol, tol, divs_u, divs_v ) {
// tesselate the curve
var crv = verb.eval.nurbs.rational_curve_adaptive_sample( degree_crv, knots_crv, homo_control_points_crv, sample_tol, true)
// tesselate the surface
, mesh = verb.eval.nurbs.tesselate_rational_surface_naive( degree_u, knots_u, degree_v, knots_v, homo_control_points_srf, divs_u, divs_v )
// separate parameters from points in the polyline (params are the first index in the array)
, u1 = crv.map( function(el) { return el[0]; })
, p1 = crv.map( function(el) { return el.slice(1) })
// perform intersection
, res = verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( p1, u1, mesh, verb.range(mesh.faces.length), tol );
// eliminate duplicate intersections
return verb.unique( res, function(a, b){
return numeric.norm2( numeric.sub( a.point, b.point ) ) < tol && Math.abs( a.p - b.p ) < tol && numeric.norm2( numeric.sub( a.uv, b.uv ) ) < tol
});
}
//
// ####intersect_parametric_polyline_mesh_by_aabb( crv_points, crv_param_points, mesh, included_faces, tol )
//
// Approximate the intersection of a polyline and mesh while maintaining parameter information
//
// **params**
// + *Array*, array of 3d points on the curve
// + *Array*, array of parameters corresponding to the parameters on the curve
// + *Object*, a triangular mesh with a "faces" attribute and "points" attribute
// + *Array*, an array of indices, representing the faces to include in the intersection operation
// + *Number*, tolerance for the intersection
//
// **returns**
// + *Array*, array of intersection objects (with potential duplicates ) each holding:
// - a "point" property where intersections took place
// - a "p" the parameter on the polyline
// - a "uv" the parameter on the mesh
// - a "face" the index of the face where the intersection took place
//
verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb = function( crv_points, crv_param_points, mesh, included_faces, tol ) {
// check if two bounding boxes intersect
var pl_bb = new verb.geom.BoundingBox( crv_points )
, mesh_bb = verb.eval.mesh.make_mesh_aabb( mesh.points, mesh.faces, included_faces );
// if bounding boxes do not intersect, return empty array
if ( !pl_bb.intersects( mesh_bb, tol ) ) {
return [];
}
if ( crv_points.length === 2 && included_faces.length === 1 ){
// intersect segment and triangle
var inter = verb.eval.geom.intersect_segment_with_tri( crv_points[0], crv_points[1], mesh.points, mesh.faces[ included_faces[0] ] );
if ( inter != null ){
// map the parameters of the segment to the parametric space of the entire polyline
var p = inter.p * ( crv_param_points[1]-crv_param_points[0] ) + crv_param_points[0];
// map the parameters of the single triangle to the entire parametric space of the triangle
var index_v0 = mesh.faces[ included_faces ][0]
, index_v1 = mesh.faces[ included_faces ][1]
, index_v2 = mesh.faces[ included_faces ][2]
, uv_v0 = mesh.uvs[ index_v0 ]
, uv_v1 = mesh.uvs[ index_v1 ]
, uv_v2 = mesh.uvs[ index_v2 ]
, uv_s_diff = numeric.sub( uv_v1, uv_v0 )
, uv_t_diff = numeric.sub( uv_v2, uv_v0 )
, uv = numeric.add( uv_v0, numeric.mul( inter.s, uv_s_diff ), numeric.mul( inter.t, uv_t_diff ) );
// a pair representing the param on the polyline and the param on the mesh
return [ { point: inter.point, p: p, uv: uv, face: included_faces[0] } ];
}
} else if ( included_faces.length === 1 ) {
// intersect triangle and polyline
// divide polyline in half, rightside includes the pivot
var crv_points_a = verb.left( crv_points )
, crv_points_b = verb.rightWithPivot( crv_points )
, crv_param_points_a = verb.left( crv_param_points )
, crv_param_points_b = verb.rightWithPivot( crv_param_points );
return verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points_a, crv_param_points_a, mesh, included_faces, tol )
.concat( verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points_b, crv_param_points_b, mesh, included_faces, tol ) );
} else if ( crv_points.length === 2 ) {
// intersect mesh >2 faces and line
// divide mesh in "half" by first sorting then dividing array in half
var sorted_included_faces = verb.eval.mesh.sort_tris_on_longest_axis( mesh_bb, mesh.points, mesh.faces, included_faces )
, included_faces_a = verb.left( sorted_included_faces )
, included_faces_b = verb.right( sorted_included_faces );
return verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points, crv_param_points, mesh, included_faces_a, tol )
.concat( verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points, crv_param_points, mesh, included_faces_b, tol ));
} else {
// intersect mesh with >2 faces and polyline
// divide mesh in "half"
var sorted_included_faces = verb.eval.mesh.sort_tris_on_longest_axis( mesh_bb, mesh.points, mesh.faces, included_faces )
, included_faces_a = verb.left( sorted_included_faces )
, included_faces_b = verb.right( sorted_included_faces );
// divide polyline in half, rightside includes the pivot
var crv_points_a = verb.left( crv_points )
, crv_points_b = verb.rightWithPivot( crv_points )
, crv_param_points_a = verb.left( crv_param_points )
, crv_param_points_b = verb.rightWithPivot( crv_param_points );
return verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points_a, crv_param_points_a, mesh, included_faces_a, tol )
.concat( verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points_a, crv_param_points_a, mesh, included_faces_b, tol ) )
.concat( verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points_b, crv_param_points_b, mesh, included_faces_a, tol ) )
.concat( verb.eval.nurbs.intersect_parametric_polyline_mesh_by_aabb( crv_points_b, crv_param_points_b, mesh, included_faces_b, tol ) );
}
return [];
}
//
// ####intersect_segment_with_tri( p1, p0, points, tri )
//
// Intersect segment with triangle (from http://geomalgorithms.com/a06-_intersect-2.html)
//
// **params**
// + *Array*, array of length 3 representing first point of the segment
// + *Array*, array of length 3 representing second point of the segment
// + *Array*, array of length 3 arrays representing the points of the triangle
// + *Array*, array of length 3 containing int indices in the array of points, this allows passing a full mesh
//
// **returns**
// + *Object*, an object with an "intersects" property that is true or false and if true, a
// "s" property giving the param on u, and "t" is the property on v, where u is the
// axis from v0 to v1, and v is v0 to v1, a "point" property
// where the intersection took place, and "p" property representing the parameter along the segment
//
verb.eval.geom.intersect_segment_with_tri = function( p0, p1, points, tri ) {
var v0 = points[ tri[0] ]
, v1 = points[ tri[1] ]
, v2 = points[ tri[2] ]
, u = numeric.sub( v1, v0 )
, v = numeric.sub( v2, v0 )
, n = numeric.cross( u, v );
var dir = numeric.sub( p1, p0 )
, w0 = numeric.sub( p0, v0 )
, a = -numeric.dot( n, w0 )
, b = numeric.dot( n, dir )
// is ray is parallel to triangle plane?
if ( Math.abs( b ) < verb.EPSILON ){
return null;
}
var r = a / b;
// segment goes away from triangle or is beyond segment
if ( r < 0 || r > 1 ){
return null;
}
// get proposed intersection
var pt = numeric.add( p0, numeric.mul( r, dir ) );
// is I inside T?
var uv = numeric.dot(u,v)
, uu = numeric.dot(u,u)
, vv = numeric.dot(v,v)
, w = numeric.sub( pt, v0 )
, wu = numeric.dot( w, u )
, wv = numeric.dot( w, v )
, denom = uv * uv - uu * vv
, s = ( uv * wv - vv * wu ) / denom
, t = ( uv * wu - uu * wv ) / denom;
if (s > 1.0 + verb.EPSILON || t > 1.0 + verb.EPSILON || t < -verb.EPSILON || s < -verb.EPSILON || s + t > 1.0 + verb.EPSILON){
return null;
}
return { point: pt, s: s, t: t, p: r };
}
//
// ####intersect_segment_with_plane( p0, p1, v0, n )
//
// Intersect ray/segment with plane (from http://geomalgorithms.com/a06-_intersect-2.html)
//
// If intersecting a ray, the param needs to be between 0 and 1 and the caller is responsible
// for making that check
//
// **params**
// + *Array*, array of length 3 representing first point of the segment
// + *Array*, array of length 3 representing second point of the segment
// + *Array*, array of length 3 representing an origin point on the plane
// + *Array*, array of length 3 representing the normal of the plane
//
// **returns**
// null or an object with a p property representing the param on the segment
//
verb.eval.geom.intersect_segment_with_plane = function( p0, p1, v0, n ) {
var denom = numeric.dot( n, numeric.sub(p0,p1) );
// parallel case
if ( abs( denom ) < EPSILON ) {
return null;
}
var numer = numeric.dot( n, numeric.sub(v0,p0) );
return { p: numer / denom };
}
//
// ####intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1, aabb_tree2 )
//
// Intersect two aabb trees - a recursive function
//
// **params**
// + *Array*, array of length 3 arrays of numbers representing the points of mesh1
// + *Array*, array of length 3 arrays of number representing the triangles of mesh1
// + *Array*, array of length 3 arrays of numbers representing the points of mesh2
// + *Array*, array of length 3 arrays of number representing the triangles of mesh2
// + *Object*, nested object representing the aabb tree of the first mesh
// + *Object*, nested object representing the aabb tree of the second mesh
//
// **returns**
// + *Array*, a list of pairs of triangle indices for mesh1 and mesh2 that are intersecting
//
verb.eval.geom.intersect_aabb_trees = function( points1, tris1, points2, tris2, aabb_tree1, aabb_tree2 ) {
var intersects = aabb_tree1.bounding_box.intersects( aabb_tree2.bounding_box );
if (!intersects){
return [];
}
if (aabb_tree1.children.length === 0 && aabb_tree2.children.length === 0){
return [ [aabb_tree1.triangle, aabb_tree2.triangle ] ];
} else if (aabb_tree1.children.length === 0 && aabb_tree2.children.length != 0){
return verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1, aabb_tree2.children[0] )
.concat( verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1, aabb_tree2.children[1] ) );
} else if (aabb_tree1.children.length != 0 && aabb_tree2.children.length === 0){
return verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1.children[0], aabb_tree2 )
.concat( verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1.children[1], aabb_tree2 ) );
} else if (aabb_tree1.children.length != 0 && aabb_tree2.children.length != 0){
return verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1.children[0], aabb_tree2.children[0] )
.concat( verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1.children[0], aabb_tree2.children[1] ) )
.concat( verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1.children[1], aabb_tree2.children[0] ) )
.concat( verb.eval.geom.intersect_aabb_trees( points1, tris1, points2, tris2, aabb_tree1.children[1], aabb_tree2.children[1] ) );
}
}
//
// ####make_mesh_aabb_tree( points, tris, tri_indices )
//
// Make tree of axis aligned bounding boxes
//
// **params**
// + *Array*, array of length 3 arrays of numbers representing the points
// + *Array*, array of length 3 arrays of number representing the triangles
// + *Array*, array of numbers representing the relevant triangles to use to form aabb
//
// **returns**
// + *Array*, a point represented by an array of length (dim)
//
verb.eval.mesh.make_mesh_aabb_tree = function( points, tris, tri_indices ) {
// build bb
var aabb = { bounding_box: verb.eval.mesh.make_mesh_aabb( points, tris, tri_indices ),
children: [] };
// if only one ele, terminate recursion and store the triangles
if (tri_indices.length === 1){
aabb.triangle = tri_indices[0];
return aabb;
}
// sort triangles in sub mesh
var sorted_tri_indices = verb.eval.mesh.sort_tris_on_longest_axis( aabb.bounding_box, points, tris, tri_indices )
, tri_indices_a = sorted_tri_indices.slice( 0, Math.floor( sorted_tri_indices.length / 2 ) )
, tri_indices_b = sorted_tri_indices.slice( Math.floor( sorted_tri_indices.length / 2 ), sorted_tri_indices.length );
// recurse
aabb.children = [ verb.eval.mesh.make_mesh_aabb_tree(points, tris, tri_indices_a),
verb.eval.mesh.make_mesh_aabb_tree(points, tris, tri_indices_b) ];
// return result
return aabb;
}
//
// ####make_mesh_aabb( points, tris, tri_indices )
//
// Form axis-aligned bounding box from triangles of mesh
//
// **params**
// + *Array*, array of length 3 arrays of numbers representing the points
// + *Array*, array of length 3 arrays of number representing the triangles
// + *Array*, array of numbers representing the relevant triangles
//
// **returns**
// + *Array*, a point represented by an array of length (dim)
//
verb.eval.mesh.make_mesh_aabb = function( points, tris, tri_indices ) {
var bb = new verb.geom.BoundingBox();
tri_indices.forEach(function(x){
bb.add( points[ tris[ x ][0] ] );
bb.add( points[ tris[ x ][1] ] );
bb.add( points[ tris[ x ][2] ] );
});
return bb;
}
//
// ####sort_tris_on_longest_axis( container_bb, points, tris, tri_indices )
//
// Sort triangles on longest axis
//
// **params**
// + *Number*, integer degree of surface in u direction
// + *Array*, array of nondecreasing knot values in u direction
// + *Number*, integer degree of surface in v direction
// + *Array*, array of nondecreasing knot values in v direction
//
// **returns**
// + *Array*, a point represented by an array of length (dim)
//
verb.eval.mesh.sort_tris_on_longest_axis = function( container_bb, points, tris, tri_indices ) {
var long_axis = container_bb.get_longest_axis();
var axis_position_map = [];
for (var i = tri_indices.length - 1; i >= 0; i--) {
var tri_i = tri_indices[i],
tri_min = verb.eval.mesh.get_min_coordinate_on_axis( points, tris[ tri_i ], long_axis );
axis_position_map.push( [ tri_min, tri_i ] );
}
axis_position_map.sort(function(a,b) { return a[0] > b[0] } );
var sorted_tri_indices = [];
for (var i = 0, l = axis_position_map.length; i < l; i++) {
sorted_tri_indices.push( axis_position_map[i][1] );
}
return sorted_tri_indices;
}
//
// ####get_min_coordinate_on_axis( points, tri, axis )
//
// Get min coordinate on axis
//
// **params**
// + *Array*, array of length 3 arrays of numbers representing the points
// + *Array*, length 3 array of point indices for the triangle
//
// **returns**
// + *Number*, a point represented by an array of length 3
//
verb.eval.mesh.get_min_coordinate_on_axis = function( points, tri, axis ) {
var axis_coords = [];
for (var i = 0; i < 3; i++){
axis_coords.push( points[ tri[i] ][ axis ] );
}
return Math.min.apply(Math, axis_coords);
};
//
// ####get_tri_centroid( points, tri )
//
// Get triangle centroid
//
// **params**
// + *Array*, array of length 3 arrays of numbers representing the points
// + *Array*, length 3 array of point indices for the triangle
//
// **returns**
// + *Array*, a point represented by an array of length 3
//
verb.eval.geom.get_tri_centroid = function( points, tri ) {
var centroid = [0,0,0];
for (var i = 0; i < 3; i++){
for (var j = 0; j < 3; j++){
centroid[j] += points[ tri[i] ][j];
}
}
for (var i = 0; i < 3; i++){
centroid[i] /= 3;
}
return centroid;
};
//
// ####get_tri_norm( points, tri )
//
// Get triangle normal
//
// **params**
// + *Array*, array of length 3 arrays of numbers representing the points
// + *Array*, length 3 array of point indices for the triangle
//
// **returns**
// + *Array*, a normal vector represented by an array of length 3
//
verb.eval.geom.get_tri_norm = function( points, tri ) {
var v0 = points[ tri[0] ]
, v1 = points[ tri[1] ]
, v2 = points[ tri[2] ]
, u = numeric.sub( v1, v0 )
, v = numeric.sub( v2, v0 )
, n = numeric.cross( u, v );
return numeric.mul( 1 / numeric.norm2( n ), n );
};
//
// ####intersect_rational_curves_by_aabb_refine( degree1, knots1, homo_control_points1, degree2, knots2, homo_control_points2, sample_tol, tol )
//
// Approximate the intersection of two nurbs surface by axis-aligned bounding box intersection and then refine all solutions.
//
// **params**
// + *Number*, integer degree of curve1
// + *Array*, array of nondecreasing knot values for curve 1
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1) and form (wi*pi, wi) for curve 1
// + *Number*, integer degree of curve2
// + *Array*, array of nondecreasing knot values for curve 2
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1) and form (wi*pi, wi) for curve 2
// + *Number*, tolerance for the intersection
//
// **returns**
// + *Array*, a 2d array specifying the intersections on u params of intersections on curve 1 and cruve 2
//
verb.eval.nurbs.intersect_rational_curves_by_aabb_refine = function( degree1, knots1, homo_control_points1, degree2, knots2, homo_control_points2, sample_tol, tol ) {
var ints = verb.eval.nurbs.intersect_rational_curves_by_aabb( degree1, knots1, homo_control_points1, degree2, knots2, homo_control_points2, sample_tol, tol );
return ints.map(function(start_params){
return verb.eval.nurbs.refine_rational_curve_intersection( degree1, knots1, homo_control_points1, degree2, knots2, homo_control_points2, start_params )
});
}
//
// ####rational_curve_curve_bb_intersect_refine( degree1, knots1, control_points1, degree2, knots2, control_points2, start_params )
//
// Refine an intersection pair for two curves given an initial guess. This is an unconstrained minimization,
// so the caller is responsible for providing a very good initial guess.
//
// **params**
// + *Number*, integer degree of curve1
// + *Array*, array of nondecreasing knot values for curve 1
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1)
// and form (wi*pi, wi) for curve 1
// + *Number*, integer degree of curve2
// + *Array*, array of nondecreasing knot values for curve 2
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1)
// and form (wi*pi, wi) for curve 2
// + *Array*, length 2 array with first param guess in first position and second param guess in second position
//
// **returns**
// + *Array*, a length 3 array containing the [ distance// distance, u1, u2 ]
//
verb.eval.nurbs.refine_rational_curve_intersection = function( degree1, knots1, control_points1, degree2, knots2, control_points2, start_params ) {
var objective = function(x) {
var p1 = verb.eval.nurbs.rational_curve_point(degree1, knots1, control_points1, x[0])
, p2 = verb.eval.nurbs.rational_curve_point(degree2, knots2, control_points2, x[1])
, p1_p2 = numeric.sub(p1, p2);
return numeric.dot(p1_p2, p1_p2);
}
var sol_obj = numeric.uncmin( objective, start_params);
return sol_obj.solution.concat( sol_obj.f );
}
//
// ####intersect_rational_curves_by_aabb( degree1, knots1, homo_control_points1, degree2, knots2, homo_control_points2, sample_tol, tol )
//
// Approximate the intersection of two nurbs surface by axis-aligned bounding box intersection.
//
// **params**
// + *Number*, integer degree of curve1
// + *Array*, array of nondecreasing knot values for curve 1
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1) and form (wi*pi, wi) for curve 1
// + *Number*, integer degree of curve2
// + *Array*, array of nondecreasing knot values for curve 2
// + *Array*, 2d array of homogeneous control points, where each control point is an array of length (dim+1) and form (wi*pi, wi) for curve 2
// + *Number*, tolerance for the intersection
//
// **returns**
// + *Array*, array of parameter pairs representing the intersection of the two parameteric polylines
//
verb.eval.nurbs.intersect_rational_curves_by_aabb = function( degree1, knots1, homo_control_points1, degree2, knots2, homo_control_points2, sample_tol, tol ) {
var up1 = verb.eval.nurbs.rational_curve_adaptive_sample( degree1, knots1, homo_control_points1, sample_tol, true)
, up2 = verb.eval.nurbs.rational_curve_adaptive_sample( degree2, knots2, homo_control_points2, sample_tol, true)
, u1 = up1.map( function(el) { return el[0]; })
, u2 = up2.map( function(el) { return el[0]; })
, p1 = up1.map( function(el) { return el.slice(1) })
, p2 = up2.map( function(el) { return el.slice(1) });
return verb.eval.nurbs.intersect_parametric_polylines_by_aabb( p1, p2, u1, u2, tol );
}
//
// ####intersect_parametric_polylines_by_aabb( p1, p2, u1, u2, tol )
//
// Intersect two polyline curves, keeping track of parameterization on each
//
// **params**
// + *Array*, array of point values for curve 1
// + *Array*, array of parameter values for curve 1, same length as first arg
// + *Array*, array of point values for curve 2
// + *Array*, array of parameter values for curve 2, same length as third arg
// + *Number*, tolerance for the intersection
//
// **returns**
// + *Array*, array of parameter pairs representing the intersection of the two parameteric polylines
//
verb.eval.nurbs.intersect_parametric_polylines_by_aabb = function( p1, p2, u1, u2, tol ) {
var bb1 = new verb.geom.BoundingBox(p1)
, bb2 = new verb.geom.BoundingBox(p2);
if ( !bb1.intersects(bb2, tol) ) {
return [];
}
if (p1.length === 2 && p2.length === 2 ){
var inter = verb.eval.geom.intersect_segments(p1[0],p1[1], p2[0], p2[1], tol);
if ( inter != null ){
// map the parameters of the segment to the parametric space of the entire polyline
inter[0][0] = inter[0][0] * ( u1[1]-u1[0] ) + u1[0];
inter[1][0] = inter[1][0] * ( u2[1]-u2[0] ) + u2[0];
return [ [ inter[0][0], inter[1][0] ] ];
}
} else if (p1.length === 2) {
var p2_mid = Math.ceil( p2.length / 2 ),
p2_a = p2.slice( 0, p2_mid ),
p2_b = p2.slice( p2_mid-1 ),
u2_a = u2.slice( 0, p2_mid ),
u2_b = u2.slice( p2_mid-1 );
return verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1, p2_a, u1, u2_a, tol)
.concat( verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1, p2_b, u1, u2_b, tol) );
} else if (p2.length === 2) {
var p1_mid = Math.ceil( p1.length / 2 ),
p1_a = p1.slice( 0, p1_mid ),
p1_b = p1.slice( p1_mid-1 ),
u1_a = u1.slice( 0, p1_mid ),
u1_b = u1.slice( p1_mid-1 );
return verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1_a, p2, u1_a, u2, tol)
.concat( verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1_b, p2, u1_b, u2, tol) );
} else {
var p1_mid = Math.ceil( p1.length / 2 ),
p1_a = p1.slice( 0, p1_mid ),
p1_b = p1.slice( p1_mid-1 ),
u1_a = u1.slice( 0, p1_mid ),
u1_b = u1.slice( p1_mid-1 ),
p2_mid = Math.ceil( p2.length / 2 ),
p2_a = p2.slice( 0, p2_mid ),
p2_b = p2.slice( p2_mid-1 ),
u2_a = u2.slice( 0, p2_mid ),
u2_b = u2.slice( p2_mid-1 );
return verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1_a, p2_a, u1_a, u2_a, tol)
.concat( verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1_a, p2_b, u1_a, u2_b, tol) )
.concat( verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1_b, p2_a, u1_b, u2_a, tol) )
.concat( verb.eval.nurbs.intersect_parametric_polylines_by_aabb(p1_b, p2_b, u1_b, u2_b, tol) );
}
return [];
}
//
// ####intersect_segments( a0, a1, b0, b1, tol )
//
// Find the closest parameter on two rays, see http://geomalgorithms.com/a07-_distance.html
//
// **params**
// + *Array*, first point on a
// + *Array*, second point on a
// + *Array*, first point on b
// + *Array*, second point on b
// + *Number*, tolerance for the intersection
//
// **returns**
// + *Array*, a 2d array specifying the intersections on u params of intersections on curve 1 and cruve 2
//
verb.eval.geom.intersect_segments = function( a0, a1, b0, b1, tol ) {
// get axis and length of segments
var a1ma0 = numeric.sub(a1, a0),
aN = Math.sqrt( numeric.dot(a1ma0, a1ma0) ),
a = numeric.mul( 1/ aN, a1ma0 ),
b1mb0 = numeric.sub(b1, b0),
bN = Math.sqrt( numeric.dot(b1mb0, b1mb0) ),
b = numeric.mul( 1 / bN, b1mb0 ),
int_params = verb.eval.geom.intersect_rays(a0, a, b0, b);
if ( int_params != null ) {
var u1 = Math.min( Math.max( 0, int_params[0] / aN ), 1.0),
u2 = Math.min( Math.max( 0, int_params[1] / bN ), 1.0),
int_a = numeric.add( numeric.mul( u1, a1ma0 ), a0 ),
int_b = numeric.add( numeric.mul( u2, b1mb0 ), b0 ),
dist = numeric.norm2Squared( numeric.sub(int_a, int_b) );
if ( dist < tol*tol ) {
return [ [u1].concat(int_a), [u2].concat(int_b) ] ;
}
}
return null;
}
//
// ####closest_point_on_ray( pt, o, r )
//
// Find the closest point on a ray
//
// **params**
// + *Array*, point to project
// + *Array*, origin for ray
// + *Array*, direction of ray 1, assumed normalized
//
// **returns**
// + *Array*, [param, pt]
//
verb.eval.geom.closest_point_on_ray = function( pt, o, r ) {
var o2pt = numeric.sub(pt,o)
, do2ptr = numeric.dot(o2pt, r)
, proj = numeric.add(o, numeric.mul(do2ptr, r));
return proj;
}
//
// ####intersect_rays( a0, a, b0, b )
//
// Find the closest parameter on two rays, see http://geomalgorithms.com/a07-_distance.html
//
// **params**
// + *Array*, origin for ray 1
// + *Array*, direction of ray 1, assumed normalized
// + *Array*, origin for ray 1
// + *Array*, direction of ray 1, assumed normalized
//
// **returns**
// + *Array*, a 2d array specifying the intersections on u params of intersections on curve 1 and curve 2
//
verb.eval.geom.intersect_rays = function( a0, a, b0, b ) {
var dab = numeric.dot( a, b ),
dab0 = numeric.dot( a, b0 ),
daa0 = numeric.dot( a, a0 ),
dbb0 = numeric.dot( b, b0 ),
dba0 = numeric.dot( b, a0 ),
daa = numeric.dot( a, a ),