@@ -554,16 +554,24 @@ def ndisty(x, y, y0):
554
554
555
555
return dists
556
556
557
- def d_ndisty_dx1 (x , y , y0 ):
557
+ def d_ndisty_dx1 (x1 , x2 , x3 , y1 , y2 , y3 , y0 ):
558
558
"""Calculate the derivative of ndisty with respect the the x coordinate of
559
559
the 'previous' point.
560
560
561
561
Parameters
562
562
---------
563
- x : list or np.array
564
- X coordinates of polygon
565
- y : list or np.array
566
- Y coordinates of polygon
563
+ x1 : float or list or np.array
564
+ X coordinate(s) of "previous" point
565
+ x2 : float or list or np.array
566
+ X coordinate(s) of "current" point
567
+ x3 : float or list or np.array
568
+ X coordinate(s) of "next" point
569
+ y1 : float or list or np.array
570
+ Y coordinate(s) of "previous" point
571
+ y2 : float or list or np.array
572
+ Y coordinate(s) of "current" point
573
+ y3 : float or list or np.array
574
+ Y coordinate(s) of "next" point
567
575
y0 : float
568
576
The y position to which the distance is calculated.
569
577
@@ -572,19 +580,27 @@ def d_ndisty_dx1(x, y, y0):
572
580
np.array
573
581
List of derivatives with length N-2 where N=len(x)
574
582
"""
575
- return - (y0 - y2 )** 2 * (y1 - y3 )** 2 / (sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
583
+ return - (y0 - y2 )** 2 * (y1 - y3 )** 2 / (np . sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
576
584
(y1 - y3 )** 2 )/ (x1 - x3 )** 2 )* (x1 - x3 )** 3 )
577
585
578
- def d_ndisty_dx2 (x , y , y0 ):
586
+ def d_ndisty_dx2 (x1 , x2 , x3 , y1 , y2 , y3 , y0 ):
579
587
"""Calculate the derivative of ndisty with respect the the x coordinate of
580
588
the 'current' point.
581
589
582
590
Parameters
583
591
---------
584
- x : list or np.array
585
- X coordinates of polygon
586
- y : list or np.array
587
- Y coordinates of polygon
592
+ x1 : float or list or np.array
593
+ X coordinate(s) of "previous" point
594
+ x2 : float or list or np.array
595
+ X coordinate(s) of "current" point
596
+ x3 : float or list or np.array
597
+ X coordinate(s) of "next" point
598
+ y1 : float or list or np.array
599
+ Y coordinate(s) of "previous" point
600
+ y2 : float or list or np.array
601
+ Y coordinate(s) of "current" point
602
+ y3 : float or list or np.array
603
+ Y coordinate(s) of "next" point
588
604
y0 : float
589
605
The y position to which the distance is calculated.
590
606
@@ -595,16 +611,24 @@ def d_ndisty_dx2(x, y, y0):
595
611
"""
596
612
return 0.0
597
613
598
- def d_ndisty_dx3 (x , y , y0 ):
614
+ def d_ndisty_dx3 (x1 , x2 , x3 , y1 , y2 , y3 , y0 ):
599
615
"""Calculate the derivative of ndisty with respect the the x coordinate of
600
616
the 'next' point.
601
617
602
618
Parameters
603
619
---------
604
- x : list or np.array
605
- X coordinates of polygon
606
- y : list or np.array
607
- Y coordinates of polygon
620
+ x1 : float or list or np.array
621
+ X coordinate(s) of "previous" point
622
+ x2 : float or list or np.array
623
+ X coordinate(s) of "current" point
624
+ x3 : float or list or np.array
625
+ X coordinate(s) of "next" point
626
+ y1 : float or list or np.array
627
+ Y coordinate(s) of "previous" point
628
+ y2 : float or list or np.array
629
+ Y coordinate(s) of "current" point
630
+ y3 : float or list or np.array
631
+ Y coordinate(s) of "next" point
608
632
y0 : float
609
633
The y position to which the distance is calculated.
610
634
@@ -613,19 +637,27 @@ def d_ndisty_dx3(x, y, y0):
613
637
np.array
614
638
List of derivatives with length N-2 where N=len(x)
615
639
"""
616
- return (y0 - y2 )** 2 * (y1 - y3 )** 2 / (sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
640
+ return (y0 - y2 )** 2 * (y1 - y3 )** 2 / (np . sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
617
641
(y1 - y3 )** 2 )/ (x1 - x3 )** 2 )* (x1 - x3 )** 3 )
618
642
619
- def d_ndisty_dy1 (x , y , y0 ):
643
+ def d_ndisty_dy1 (x1 , x2 , x3 , y1 , y2 , y3 , y0 ):
620
644
"""Calculate the derivative of ndisty with respect the the y coordinate of
621
645
the 'previous' point.
622
646
623
647
Parameters
624
648
---------
625
- x : list or np.array
626
- X coordinates of polygon
627
- y : list or np.array
628
- Y coordinates of polygon
649
+ x1 : float or list or np.array
650
+ X coordinate(s) of "previous" point
651
+ x2 : float or list or np.array
652
+ X coordinate(s) of "current" point
653
+ x3 : float or list or np.array
654
+ X coordinate(s) of "next" point
655
+ y1 : float or list or np.array
656
+ Y coordinate(s) of "previous" point
657
+ y2 : float or list or np.array
658
+ Y coordinate(s) of "current" point
659
+ y3 : float or list or np.array
660
+ Y coordinate(s) of "next" point
629
661
y0 : float
630
662
The y position to which the distance is calculated.
631
663
@@ -634,19 +666,27 @@ def d_ndisty_dy1(x, y, y0):
634
666
np.array
635
667
List of derivatives with length N-2 where N=len(x)
636
668
"""
637
- return (y0 - y2 )** 2 * (y1 - y3 )/ (sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
669
+ return (y0 - y2 )** 2 * (y1 - y3 )/ (np . sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
638
670
(y1 - y3 )** 2 )/ (x1 - x3 )** 2 )* (x1 - x3 )** 2 )
639
671
640
- def d_ndisty_dy2 (x , y , y0 ):
672
+ def d_ndisty_dy2 (x1 , x2 , x3 , y1 , y2 , y3 , y0 ):
641
673
"""Calculate the derivative of ndisty with respect the the y coordinate of
642
674
the 'current' point.
643
675
644
676
Parameters
645
677
---------
646
- x : list or np.array
647
- X coordinates of polygon
648
- y : list or np.array
649
- Y coordinates of polygon
678
+ x1 : float or list or np.array
679
+ X coordinate(s) of "previous" point
680
+ x2 : float or list or np.array
681
+ X coordinate(s) of "current" point
682
+ x3 : float or list or np.array
683
+ X coordinate(s) of "next" point
684
+ y1 : float or list or np.array
685
+ Y coordinate(s) of "previous" point
686
+ y2 : float or list or np.array
687
+ Y coordinate(s) of "current" point
688
+ y3 : float or list or np.array
689
+ Y coordinate(s) of "next" point
650
690
y0 : float
651
691
The y position to which the distance is calculated.
652
692
@@ -655,19 +695,27 @@ def d_ndisty_dy2(x, y, y0):
655
695
np.array
656
696
List of derivatives with length N-2 where N=len(x)
657
697
"""
658
- return (- y0 + y2 )* ((x1 - x3 )** 2 + (y1 - y3 )** 2 )/ (sqrt ((y0 - y2 )** 2 * \
698
+ return (- y0 + y2 )* ((x1 - x3 )** 2 + (y1 - y3 )** 2 )/ (np . sqrt ((y0 - y2 )** 2 * \
659
699
((x1 - x3 )** 2 + (y1 - y3 )** 2 )/ (x1 - x3 )** 2 )* (x1 - x3 )** 2 )
660
700
661
- def d_ndisty_dy3 (x , y , y0 ):
701
+ def d_ndisty_dy3 (x1 , x2 , x3 , y1 , y2 , y3 , y0 ):
662
702
"""Calculate the derivative of ndisty with respect the the y coordinate of
663
703
the 'next' point.
664
704
665
705
Parameters
666
706
---------
667
- x : list or np.array
668
- X coordinates of polygon
669
- y : list or np.array
670
- Y coordinates of polygon
707
+ x1 : float or list or np.array
708
+ X coordinate(s) of "previous" point
709
+ x2 : float or list or np.array
710
+ X coordinate(s) of "current" point
711
+ x3 : float or list or np.array
712
+ X coordinate(s) of "next" point
713
+ y1 : float or list or np.array
714
+ Y coordinate(s) of "previous" point
715
+ y2 : float or list or np.array
716
+ Y coordinate(s) of "current" point
717
+ y3 : float or list or np.array
718
+ Y coordinate(s) of "next" point
671
719
y0 : float
672
720
The y position to which the distance is calculated.
673
721
@@ -676,7 +724,7 @@ def d_ndisty_dy3(x, y, y0):
676
724
np.array
677
725
List of derivatives with length N-2 where N=len(x)
678
726
"""
679
- return (y0 - y2 )** 2 * (- y1 + y3 )/ (sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
727
+ return (y0 - y2 )** 2 * (- y1 + y3 )/ (np . sqrt ((y0 - y2 )** 2 * ((x1 - x3 )** 2 + \
680
728
(y1 - y3 )** 2 )/ (x1 - x3 )** 2 )* (x1 - x3 )** 2 )
681
729
682
730
def ndisty_penalty (x , y , y0 , dmin , delta_d , inds = None ):
@@ -694,9 +742,8 @@ def ndisty_penalty(x, y, y0, dmin, delta_d, inds=None):
694
742
695
743
Notes
696
744
-----
697
- This function assumes that the supplied indices are contiguous in
698
- increasing order. In other words, indices must have the form
699
- [i, i+1, i+2, i+3, i+4, ...].
745
+ The inds parameter may not contain 0 or N-1 where N is the length of x and
746
+ y.
700
747
701
748
Parameters
702
749
---------
@@ -720,17 +767,14 @@ def ndisty_penalty(x, y, y0, dmin, delta_d, inds=None):
720
767
float
721
768
The value of the penalty.
722
769
"""
723
- if (inds == None ):
770
+ if (inds is None ):
724
771
inds = np .arange (1 ,len (x )- 1 )
725
772
else :
726
773
inds = np .array (inds )
727
774
728
- indices = np .concatenate ([inds [0 :1 ]- 1 , inds , inds [- 1 :]+ 1 ])
729
- xs = x [indices ]
730
- ys = y [indices ]
731
- ndist = ndisty (xs , ys , y0 )
775
+ ndist = ndisty (x , y , y0 )
732
776
733
- penalties = rect (ndist , dmin * 2 , delta_d )
777
+ penalties = rect (ndist [ inds - 1 ] , dmin * 2 , delta_d )
734
778
return np .sum (penalties )
735
779
736
780
def ndisty_penalty_derivative (x , y , y0 , dmin , delta_d , inds = None ):
@@ -739,9 +783,8 @@ def ndisty_penalty_derivative(x, y, y0, dmin, delta_d, inds=None):
739
783
740
784
Notes
741
785
-----
742
- This function assumes that the supplied indices are contiguous in
743
- increasing order. In other words, indices must have the form
744
- [i, i+1, i+2, i+3, i+4, ...].
786
+ The inds parameter may not contain 0 or N-1 where N is the length of x and
787
+ y.
745
788
746
789
Parameters
747
790
---------
@@ -765,11 +808,39 @@ def ndisty_penalty_derivative(x, y, y0, dmin, delta_d, inds=None):
765
808
numpy.ndarray, numpy.ndarray
766
809
The derivatives with respect to the x and y coordinates, respectively.
767
810
"""
768
- if (inds == None ):
811
+ if (inds is None ):
769
812
inds = np .arange (1 ,len (x )- 1 )
770
813
else :
771
814
inds = np .array (inds )
772
815
816
+ Ni = len (inds )
817
+ dPdx = np .zeros (Ni )
818
+ dPdy = np .zeros (Ni )
819
+
820
+ # Currently using a for loop in order to allow indices to be supplied in
821
+ # any order. If points were supplied in increasing order, the we could
822
+ # vectorize it :S
823
+ for i in range (Ni ):
824
+ j = inds [i ]
825
+ x1 = x [j - 1 ]; x2 = x [j ]; x3 = x [j + 1 ]
826
+ y1 = y [j - 1 ]; y2 = y [j ]; y3 = y [j + 1 ]
827
+
828
+ ndist = ndisty ([x1 , x2 , x3 ], [y1 , y2 , y3 ], y0 )
829
+ rect_deriv = rect_derivative (ndist , 2 * dmin , delta_d )
830
+
831
+ dPdx [i ] += rect_deriv * d_ndisty_dx2 (x1 , x2 , x3 , y1 , y2 , y3 , y0 )
832
+ dPdy [i ] += rect_deriv * d_ndisty_dy2 (x1 , x2 , x3 , y1 , y2 , y3 , y0 )
833
+
834
+ if (i > 0 and j - 1 == inds [i - 1 ]):
835
+ dPdx [i - 1 ] += rect_deriv * d_ndisty_dx1 (x1 , x2 , x3 , y1 , y2 , y3 , y0 )
836
+ dPdy [i - 1 ] += rect_deriv * d_ndisty_dy1 (x1 , x2 , x3 , y1 , y2 , y3 , y0 )
837
+
838
+ if (i < Ni - 1 and j + 1 == inds [i + 1 ]):
839
+ dPdx [i + 1 ] += rect_deriv * d_ndisty_dx3 (x1 , x2 , x3 , y1 , y2 , y3 , y0 )
840
+ dPdy [i + 1 ] += rect_deriv * d_ndisty_dy3 (x1 , x2 , x3 , y1 , y2 , y3 , y0 )
841
+
842
+ return np .nan_to_num (dPdx ), np .nan_to_num (dPdy )
843
+
773
844
def dist_to_edges (x1 , x2 , x3 , y1 , y2 , y3 , xe , ye ):
774
845
"""Calculate the signed distance to a set of edges.
775
846
0 commit comments