13
13
import numpy as np
14
14
15
15
from phyjax2d .impl import (
16
+ _ALL_POLYGON_KEYS ,
16
17
Capsule ,
17
18
Circle ,
18
19
Polygon ,
@@ -529,7 +530,7 @@ def _circle_polygon_overlap(
529
530
# Suppose that pstate.p.xy.shape == (N, 2) and xy.shape == (2,)
530
531
cxy = pstate .p .inv_transform (jnp .expand_dims (xy , axis = 0 ))
531
532
p2cxy = jnp .expand_dims (cxy , axis = 1 ) - polygon .points
532
- separation = _vmap_dot (polygon .normals , ( p2cxy - polygon . points ) ) # (N, NP)
533
+ separation = _vmap_dot (polygon .normals , p2cxy ) # (N, NP)
533
534
max_sep = jnp .max (separation , axis = 1 )
534
535
i1 = jnp .argmax (separation , axis = 1 )
535
536
i2 = (i1 + 1 ) % n_vertices
@@ -553,7 +554,7 @@ def circle_overlap(
553
554
) -> jax .Array :
554
555
# Circle overlap
555
556
overlap = jnp .array (False )
556
- if stated . circle is not None and shaped .circle is not None :
557
+ if not stated .circle . is_empty () :
557
558
cpos = stated .circle .p .xy
558
559
# Suppose that cpos.shape == (N, 2) and xy.shape == (2,)
559
560
dist = jnp .linalg .norm (cpos - jnp .expand_dims (xy , axis = 0 ), axis = - 1 )
@@ -562,7 +563,7 @@ def circle_overlap(
562
563
overlap = jnp .any (has_overlap )
563
564
564
565
# Static_circle overlap
565
- if stated . static_circle is not None and shaped .static_circle is not None :
566
+ if not stated .static_circle . is_empty () :
566
567
cpos = stated .static_circle .p .xy
567
568
# Suppose that cpos.shape == (N, 2) and xy.shape == (2,)
568
569
dist = jnp .linalg .norm (cpos - jnp .expand_dims (xy , axis = 0 ), axis = - 1 )
@@ -571,7 +572,7 @@ def circle_overlap(
571
572
overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
572
573
573
574
# Circle-segment overlap
574
- if stated . segment is not None and shaped .segment is not None :
575
+ if not stated .segment . is_empty () :
575
576
spos = stated .segment .p
576
577
# Suppose that spos.shape == (N, 2) and xy.shape == (2,)
577
578
pb = spos .inv_transform (jnp .expand_dims (xy , axis = 0 ))
@@ -587,49 +588,16 @@ def circle_overlap(
587
588
has_overlap = jnp .logical_and (stated .segment .is_active , penetration >= 0 )
588
589
overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
589
590
590
- # Circle-segment overlap
591
- if stated .segment is not None and shaped .segment is not None :
592
- spos = stated .segment .p
593
- # Suppose that cpos.shape == (N, 2) and xy.shape == (2,)
594
- pb = spos .inv_transform (jnp .expand_dims (xy , axis = 0 ))
595
- p1 , p2 = shaped .segment .point1 , shaped .segment .point2
596
- edge = p2 - p1
597
- s1 = jnp .expand_dims (_vmap_dot (pb - p1 , edge ), axis = 1 )
598
- s2 = jnp .expand_dims (_vmap_dot (p2 - pb , edge ), axis = 1 )
599
- in_segment = jnp .logical_and (s1 >= 0.0 , s2 >= 0.0 )
600
- ee = jnp .sum (jnp .square (edge ), axis = - 1 , keepdims = True )
601
- pa = jnp .where (in_segment , p1 + edge * s1 / ee , jnp .where (s1 < 0.0 , p1 , p2 ))
602
- dist = jnp .linalg .norm (pb - pa , axis = - 1 )
603
- penetration = radius - dist
604
- has_overlap = jnp .logical_and (stated .segment .is_active , penetration >= 0 )
605
- overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
606
-
607
591
# Circle-polygon overlap
608
- if stated .triangle is not None and shaped .triangle is not None :
609
- has_overlap = _circle_polygon_overlap (
610
- shaped .triangle ,
611
- stated .triangle ,
612
- xy ,
613
- radius ,
614
- )
615
- overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
616
-
617
- if stated .quadrangle is not None and shaped .quadrangle is not None :
618
- has_overlap = _circle_polygon_overlap (
619
- shaped .quadrangle ,
620
- stated .quadrangle ,
621
- xy ,
622
- radius ,
623
- )
624
- overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
625
-
626
- if stated .pentagon is not None and shaped .pentagon is not None :
627
- has_overlap = _circle_polygon_overlap (
628
- shaped .pentagon ,
629
- stated .pentagon ,
630
- xy ,
631
- radius ,
632
- )
633
- overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
592
+ print ("before" , overlap )
593
+ for key in _ALL_POLYGON_KEYS :
594
+ if not stated [key ].is_empty (): # type: ignore
595
+ has_overlap = _circle_polygon_overlap (
596
+ shaped [key ], # type: ignore
597
+ stated [key ], # type: ignore
598
+ xy ,
599
+ radius ,
600
+ )
601
+ overlap = jnp .logical_or (jnp .any (has_overlap ), overlap )
634
602
635
603
return overlap
0 commit comments