1
1
"""
2
- A simple, fast visualizer based on moderngl.
2
+ A simple and fast visualizer based on moderngl.
3
3
Currently, only supports circles and lines.
4
4
"""
5
5
@@ -402,72 +402,76 @@ def __init__(
402
402
vertex_shader = _CIRCLE_VERTEX_SHADER ,
403
403
fragment_shader = _CIRCLE_FRAGMENT_SHADER ,
404
404
)
405
- points , scales , colors = _collect_circles (
406
- space .shaped .circle ,
407
- stated .circle ,
408
- self ._circle_scaling ,
409
- )
410
- if len (points ) > 0 :
405
+ if space .shaped .circle .is_empty ():
406
+ self ._circles = None
407
+ else :
408
+ points , scales , colors = _collect_circles (
409
+ space .shaped .circle ,
410
+ stated .circle ,
411
+ self ._circle_scaling ,
412
+ )
411
413
self ._circles = CircleVA (
412
414
ctx = context ,
413
415
program = circle_program ,
414
416
points = points ,
415
417
scales = scales ,
416
418
colors = colors ,
417
419
)
420
+ if space .shaped .static_circle .is_empty ():
421
+ self ._static_circles = None
418
422
else :
419
- self ._circles = None
420
- points , scales , _ = _collect_circles (
421
- space .shaped .static_circle ,
422
- stated .static_circle ,
423
- self ._circle_scaling ,
424
- )
425
- if len (points ) > 0 :
423
+ points , scales , _ = _collect_circles (
424
+ space .shaped .static_circle ,
425
+ stated .static_circle ,
426
+ self ._circle_scaling ,
427
+ )
426
428
self ._static_circles = CircleVA (
427
429
ctx = context ,
428
430
program = circle_program ,
429
431
points = points ,
430
432
scales = scales ,
431
433
colors = _get_sc_color (self ._sc_color , stated .static_circle ),
432
434
)
435
+ if space .shaped .segment .is_empty ():
436
+ self ._static_lines = None
433
437
else :
434
- self ._static_circles = None
435
- static_segment_program = self ._make_gl_program (
436
- vertex_shader = _LINE_VERTEX_SHADER ,
437
- geometry_shader = _LINE_GEOMETRY_SHADER ,
438
- fragment_shader = _LINE_FRAGMENT_SHADER ,
439
- color = np .array ([0.0 , 0.0 , 0.0 , 0.4 ], dtype = np .float32 ),
440
- width = np .array ([0.004 ], dtype = np .float32 ),
441
- w_rad = np .array ([0.001 ], dtype = np .float32 ),
442
- l_rad = np .array ([0.001 ], dtype = np .float32 ),
443
- )
444
- points = _collect_static_lines (space .shaped .segment , stated .segment )
445
- if len (points ) > 0 :
438
+ static_segment_program = self ._make_gl_program (
439
+ vertex_shader = _LINE_VERTEX_SHADER ,
440
+ geometry_shader = _LINE_GEOMETRY_SHADER ,
441
+ fragment_shader = _LINE_FRAGMENT_SHADER ,
442
+ color = np .array ([0.0 , 0.0 , 0.0 , 0.4 ], dtype = np .float32 ),
443
+ width = np .array ([0.004 ], dtype = np .float32 ),
444
+ w_rad = np .array ([0.001 ], dtype = np .float32 ),
445
+ l_rad = np .array ([0.001 ], dtype = np .float32 ),
446
+ )
447
+ self ._static_line_points = _collect_static_lines (
448
+ space .shaped .segment ,
449
+ stated .segment ,
450
+ )
446
451
self ._static_lines = SegmentVA (
447
452
ctx = context ,
448
453
program = static_segment_program ,
449
454
segments = points ,
450
455
)
456
+
457
+ if space .shaped .static_triangle .is_empty ():
458
+ self ._triangles = None
451
459
else :
452
- self ._static_lines = None
453
- points , colors = _collect_triangles (
454
- space .shaped .static_triangle ,
455
- stated .static_triangle ,
456
- )
457
- triangle_program = self ._make_gl_program (
458
- vertex_shader = _TRIANGLE_VERTEX_SHADER ,
459
- geometry_shader = _TRIANGLE_GEOMETRY_SHADER ,
460
- fragment_shader = _TRIANGLE_FRAGMENT_SHADER ,
461
- )
462
- if len (points ) > 0 :
460
+ points , colors = _collect_triangles (
461
+ space .shaped .static_triangle ,
462
+ stated .static_triangle ,
463
+ )
464
+ triangle_program = self ._make_gl_program (
465
+ vertex_shader = _TRIANGLE_VERTEX_SHADER ,
466
+ geometry_shader = _TRIANGLE_GEOMETRY_SHADER ,
467
+ fragment_shader = _TRIANGLE_FRAGMENT_SHADER ,
468
+ )
463
469
self ._triangles = TriangleVA (
464
470
ctx = context ,
465
471
program = triangle_program ,
466
472
vertices = points ,
467
473
colors = colors ,
468
474
)
469
- else :
470
- self ._triangles = None
471
475
472
476
if sensor_fn is not None :
473
477
segment_program = self ._make_gl_program (
@@ -568,15 +572,20 @@ def render(
568
572
stated : StateDict ,
569
573
circle_colors : NDArray | None = None ,
570
574
sc_colors : NDArray | None = None ,
575
+ point_offset : NDArray | None = None ,
571
576
) -> None :
577
+ if point_offset is None :
578
+ po = np .array ([[0.0 , 0.0 ]], dtype = np .float32 )
579
+ else :
580
+ po = point_offset .astype (np .float32 ).reshape (1 , 2 )
572
581
circle_points , circle_scale , circle_colors_default = _collect_circles (
573
582
self ._space .shaped .circle ,
574
583
stated .circle ,
575
584
self ._circle_scaling ,
576
585
)
577
586
if self ._circles is not None :
578
587
circle_colors = self ._get_colors (circle_colors_default , circle_colors )
579
- if self ._circles .update (circle_points , circle_scale , circle_colors ):
588
+ if self ._circles .update (circle_points + po , circle_scale , circle_colors ):
580
589
self ._circles .render ()
581
590
if self ._static_circles is not None :
582
591
sc_points , sc_scale , _ = _collect_circles (
@@ -588,22 +597,25 @@ def render(
588
597
_get_sc_color (self ._sc_color , stated .static_circle ),
589
598
sc_colors ,
590
599
)
591
- if self ._static_circles .update (sc_points , sc_scale , sc_colors ):
600
+ if self ._static_circles .update (sc_points + po , sc_scale , sc_colors ):
592
601
self ._static_circles .render ()
593
602
if self ._triangles is not None :
594
603
points , _ = _collect_triangles (
595
604
self ._space .shaped .static_triangle ,
596
605
stated .static_triangle ,
597
606
)
598
- if self ._triangles .update (points ):
607
+ if self ._triangles .update (points + po ):
599
608
self ._triangles .render ()
600
609
if self ._sensors is not None and self ._collect_sensors is not None :
601
- if self ._sensors .update (self ._collect_sensors (stated )):
610
+ if self ._sensors .update (self ._collect_sensors (stated ) + po ):
602
611
self ._sensors .render ()
603
- if self ._heads .update (_collect_heads (self ._space .shaped .circle , stated .circle )):
612
+ if self ._heads .update (
613
+ _collect_heads (self ._space .shaped .circle , stated .circle ) + po
614
+ ):
604
615
self ._heads .render ()
605
616
if self ._static_lines is not None :
606
- self ._static_lines .render ()
617
+ if self ._static_lines .update (self ._static_line_points + po ):
618
+ self ._static_lines .render ()
607
619
608
620
609
621
class MglVisualizer :
0 commit comments