U
    Gvfs                  T   @   sh  d dl Z d dlZd dlZd dlmZmZmZmZm	Z	 d dl
Z
d dl
mZ d dlm  mZ d dlmZ d dlmZ d dlZdd Zdd	 Zefd
dZejd ddddddgZeddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddgddggQZeddgdd gddgd dgd d gd dgddeej j! gdd gddgg	Z"ej#dded d gd dgdd gddgggZ$e$ej#ddd ed d gd dgdd gddgg g Z%e&eej#d!dej#d!d"ej#d!d#ej#d!d$ej#dd%ej#dd&ej#dd'ee"d(
Z'e$dfe%dfd)Z(d*d+ Z)e'D ]Z*d,D ]Z+e)e*e+ qqG d-d. d.Z,G d/d0 d0Z-G d1d2 d2Z.G d3d4 d4Z/d5d6 Z0G d7d8 d8Z1G d9d: d:Z2G d;d< d<Z3dS )=    N)assert_equalassert_almost_equalassert_assert_allcloseassert_array_equal)raises)cKDTree)Voronoic                 C   s   t t| S Ntuplesortedx r   B/tmp/pip-unpacked-wheel-96ln3f52/scipy/spatial/tests/test_qhull.pysorted_tuple   s    r   c                 C   s   t t| S r
   )r   npuniquer   r   r   r   sorted_unique_tuple   s    r   c                 C   sb   t | tjr|  } t |tjr(| }tt|| } |   tt||}|  t| | d S r
   )
isinstancer   Zndarraytolistlistmapsortr   )abtplr   r   r   !assert_unordered_tuple_list_equal   s    r     r   r   r      r"   r   r"   r"         ?r&   )r&   g      ?gQ	gzGgQgHzG        gHzG?gQ?gzG@gQ	@r"   
            ?                     )
some-pointsz	random-2dz	random-3dz	random-4dz	random-5dz	random-6dz	random-7dz	random-8dpathological-1pathological-2)zbug-2850z
bug-2850-2c           	      C   s   t |  }|jd }d}|d }| dkr,d}n| dkr8d}|d| g}t|t||D ]}|||||   qVd| |f }|tkst||ft|< dS )	z<
    Generate incremental datasets from basic data sets
    r"   Nr*   r3   zQJ Ppr4      z%s-chunk-%d)DATASETSshaperangelenappendINCREMENTAL_DATASETSAssertionError)	name	chunksizepointsndimoptsZnminchunksjnew_namer   r   r   _add_inc_dataa   s    
rF   )r"   r.      c                   @   s   e Zd Zdd Zdd ZdS )
Test_Qhullc                 C   s  t dtddgddgddgddgddggd}t| }t dtddgddgddgddggd}t| }t| }|  t| }|  tt	|j tt	|j t
|d |d  t|d |d td t|d	 |d	 td t|d
 |d
 td t|d |d  t
|d |d  t|d |d td t|d	 |d	 td t|d
 |d
 td t|d |d  |  tt	|j |  tt	|j d S )N   vr   r"         ?r&   s   Qz       @r   r*   r-   r.   )qhullZ_Qhullr   arraycopydeepcopyZget_voronoi_diagramcloseassert_raisesRuntimeErrorr   r   r   r   )selfr   ZxdyZydZxd2Zyd2r   r   r   test_swapping   s<    & zTest_Qhull.test_swappingc                 C   sL   t ddgddgddgddgddgddgddgddgddgg	}t| d S )Nr   r"   r*   )r   rN   r	   )rT   r@   r   r   r   test_issue_8051   s    @zTest_Qhull.test_issue_8051N)__name__
__module____qualname__rV   rW   r   r   r   r   rH      s   &rH   c                   @   sb   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dddZ
dd Zejjdd ZdS )TestUtilitiesz-
    Check that utility functions work.

    c                 C   s   t jddddgt jd}t|}t|jdddgddd	gg d
D ]L}||d d }t||d d|f d t||d d }t|| qBd S )Nr    r!   r$   r#   Zdtyper"   r-   r*   r   ))r+   r+   r"   )      ?r]   r   )333333?皙?r"   z%rerr_msg)	r   rN   doublerM   Delaunayr   	simplicesfind_simplexZtsearch)rT   r@   tripirD   r   r   r   test_find_simplex   s    
zTestUtilities.test_find_simplexc                 C   s   t jdddddgt jd}t jddgt jd}t|}||}||}||}t|jD ]\}}||d	  }	||d
  }
||d  }t 	|	| |
| }|t 
t || }|t |d  9 }t ||| }t|| | q`d S )Nr    r$   r#   )g]2ʐ?g>mj?)g~T!?g^Rz?r\   gB?g?r   r"   r*   )r   rN   rb   rM   rc   Zlift_pointsZplane_distance	enumeraterd   Zcrosssqrtdotsignr   )rT   r   rg   rf   zZpzdistrD   vx1Zx2Zx3ndr   r   r   test_plane_distance   s&    



z!TestUtilities.test_plane_distancec                 C   sJ   t jddddgt jd}t|}t|jddgddgdd	gdd	gg d S )
Nr    r!   r$   r#   r\   r-   r*   r"   r   )r   rN   rb   rM   rc   r   convex_hullrT   r@   rf   r   r   r   test_convex_hull   s    
zTestUtilities.test_convex_hullc              
   C   sL   t ddddddddg}t|}t|jd	d
dd t|jdd
dd d S )Nr   r   r   r   r"   r   r"   r   r   r"   r"   r   r   r   r"   r   r"   r"   r"   r   r"   r"   r"   r"   rJ   +=zVolume of cube is incorrectrtolra         @zArea of cube is incorrectr   rN   rM   
ConvexHullr   volumearearT   r@   hullr   r   r   test_volume_area   s       


zTestUtilities.test_volume_areac                 C   sP   t ddddddddd	d
g
}t|}t|jdddd t|jdddd d S )N)gQ4?gΜR@?gX&lL5?)gfNz?gjM?g{D흚j?)ga?8\?g?gw5K?)g,lnq?g6=?gU?)gw컝?g`?ga?)g+q?g\hϱ?gLg?)gGe?gE9;?gd:L?)gȊKB?gBYM?g?)g~EHy?gUdk?gmVq"?)gqp?ge?g\*L?gY/?Hz>z(Volume of random polyhedron is incorrectr   g\4?z&Area of random polyhedron is incorrectr   r   r   r   r   test_random_volume_area   s$    


z%TestUtilities.test_random_volume_areac                 C   s  d}d}t j||f}tj|d|d ddf dd}tj|d|d ddf dd}t|d |D ]}t|d|d ddf }||||d ddf  |j|||d ddf dd t|j|jdd	 t|j|jdd	 t|j|jdd	 t|j|jdd	 qjdS )
zwTest that incremental mode gives the same volume/area as
        non-incremental mode and incremental mode with restart   r-   Nr"   TincrementalZrestartr   r   )	r   randomrM   r   r9   
add_pointsr   r   r   )rT   Z	nr_pointsZdimr@   Zinc_hullZinc_restart_hullrh   r   r   r   r   )test_incremental_volume_area_random_input  s    """z7TestUtilities.test_incremental_volume_area_random_input Fr   c              	      s  |j |j }d|jd  }|jdd| }dd }ttj}	||j|}
tj	dd4 t
|
jddt|
| | dk jddB }W 5 Q R X t| d	|t| f  |d
d
d
dd
d
f |d
d
dd
d
d
f   t fddt|jD }t|jd
d
ddf |t|	k B }t| d	|t| f  ||}|dkt
|jd
d
ddf B }t| d	|t| f  |r||kjdd}||d| kjddO }|dk|B }t| d	|t| f  d
S )z@Check that a triangulation has reasonable barycentric transformsr"   rJ   Zaxisc                 S   sB   | d d dd d f }| d d d dd d f }t d||| S )Nr(   z
ijk,ik->ij)r   Zeinsum)trr   rZTinvr   r   r   barycentric_transform*  s    zJTestUtilities._check_barycentric_transforms.<locals>.barycentric_transformignore)invalidg?z%s %sNr(   c              	      s*   g | ]"}t j |d d d d f qS r
   )r   linalgZdet).0kqr   r   
<listcomp>9  s   z?TestUtilities._check_barycentric_transforms.<locals>.<listcomp>r   )r@   rd   rA   sumr   finfofloateps	transformZerrstateisnanallabsr   nonzerorN   r9   Znsimplexisfiniterk   re   any)rT   rf   ra   	unit_cubeunit_cube_tolverticesZscZ	centroidsr   r   cokr   rD   Zat_boundaryr   r   r   _check_barycentric_transforms  s.    82(
"z+TestUtilities._check_barycentric_transformsc                 C   sr   t tjtjtdd}|d }|  t	|}t 
|jd d ddf  }t|dk | | | d S )Ndatazdegenerate_pointset.npzr   r      )r   loadospathjoindirname__file__rQ   rM   rc   r   r   r   r   r   )rT   r   r@   rf   Z	bad_countr   r   r   &test_degenerate_barycentric_transformsL  s    
z4TestUtilities.test_degenerate_barycentric_transformsc           	   
   C   s4  t tj}ddddd}tddD ]}t dd	|| }t jttt j	t j
t j|g|    j}d
| }t|}| j||dd t jd t j|jd dk }||d d f  d| t jj||d d f j d  7  < t|}| j||dd| d tt j||f }| j||dd| d q$d S )NF      r/   r-   )r*   r-   r.   r/   r*   r0   r   r"   zndim=%dT)ra   r   r   r_   r&   )ra   r   r   )r   r   r   r   r9   Zlinspacec_r   r   ravelZbroadcast_arraysZix_TrM   rc   r   r   seedrandr8   r_)	rT   r   ZnpointsrA   r   Zgridra   rf   mr   r   r    test_more_barycentric_transforms]  s.    *
:
z.TestUtilities.test_more_barycentric_transformsN)r   Fr   )rX   rY   rZ   __doc__ri   rt   rw   r   r   r   r   r   pytestmarkZslowr   r   r   r   r   r[      s      
-r[   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestVertexNeighborVerticesc                    s   dd t |jjd D }|jD ].}|D ]$}|D ]}||kr0|| | q0q(q |j\  fddt |jjd D }t||d||f d d S )Nc                 S   s   g | ]
}t  qS r   )setr   rD   r   r   r   r     s     z5TestVertexNeighborVertices._check.<locals>.<listcomp>r   c              
      s.   g | ]&}t tt | |d    qS r"   )r   r   intr   indicesZindptrr   r   r     s   z%r != %rr`   )r9   r@   r8   rd   addZvertex_neighbor_verticesr   )rT   rf   expectedsr   r   gotr   r   r   _check  s    

z!TestVertexNeighborVertices._checkc                 C   s.   t jdddgt jd}t|}| | d S )Nr    r!   r#   r\   r   rN   rb   rM   rc   r   rv   r   r   r   test_triangle  s    
z(TestVertexNeighborVertices.test_trianglec                 C   s0   t jddddgt jd}t|}| | d S )Nr    r!   r$   r#   r\   r   rv   r   r   r   test_rectangle  s    
z)TestVertexNeighborVertices.test_rectanglec                 C   s4   t jddddddgt jd}t|}| | d S )Nr    r!   r$   r#   r%   )g?r&   r\   r   rv   r   r   r   test_complicated  s     
z+TestVertexNeighborVertices.test_complicatedN)rX   rY   rZ   r   r   r   r   r   r   r   r   r     s   r   c                   @   sz   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zejdeedd Zdd ZdS )TestDelaunayz*
    Check that triangulation works.

    c                 C   s   t jd}tttj| d S Nr"   )r   ma
masked_allrR   
ValueErrorrM   rc   rT   Zmasked_arrayr   r   r   test_masked_array_fails  s    z$TestDelaunay.test_masked_array_failsc                 C   s0   t jddddt jfgt jd}tttj| d S )Nr    r!   r$   r"   r\   )r   rN   nanrb   rR   r   rM   rc   rT   Zpoints_with_nanr   r   r   test_array_with_nans_fails  s    z'TestDelaunay.test_array_with_nans_failsc                 C   s   t ddD ]}t|d |f}t |D ]}d|||f< q(d|dd d f< t|}|j  t|jtj|d t	dd d d f  t|j
dtj|d t	dd d d f   q
d S )Nr*   r2   r"   rJ   r(   r\   )r9   r   zerosrM   rc   rd   r   r   aranger   	neighbors)rT   Zndr@   rD   rf   r   r   r   test_nd_simplex  s    

&zTestDelaunay.test_nd_simplexc                 C   s^   t jddddgt jd}t|}t|jdddgddd	gg t|jd
d
dgd
d
d	gg d S )Nr    r!   r$   r#   r\   r"   r-   r*   r   r(   )r   rN   rb   rM   rc   r   rd   r   rv   r   r   r   test_2d_square  s    
zTestDelaunay.test_2d_squarec                 C   sx   t jddddgt jd}t jddddgt jd}t j||f }t j||f }tt j||f  tt j||f  d S )Nr   r"   r\   )r   rN   float64r   rM   rc   r   )rT   r   rU   ZxpZypr   r   r   test_duplicate_points  s    z"TestDelaunay.test_duplicate_pointsc                 C   s   t d }t|}t|j|j  |  t|j|j  |  t d }t|}t|j|j  |  t|j|j  |  d S )Nr4   r5   )r7   rM   rc   r   r@   rd   maxminrv   r   r   r   test_pathological  s    

zTestDelaunay.test_pathologicalc                 C   sN   t jdd}t j||f }tj|dd}tt |j	 t 
t| d S )Nr)   r*   z	QJ Qbb Ppqhull_options)r   r   r   r   rM   rc   r   r   rd   r   r   r:   rv   r   r   r   test_joggle  s    zTestDelaunay.test_jogglec                 C   s   t jdd}t j||f }t|}ttt |j	
 t|d k tt|jt|d k ttt |jd d df t|d k tt |jdk d S )Nr)   r*   r   )r   r   r   r   rM   rc   r   r:   r   rd   r   Zcoplanarr   Zvertex_to_simplexrv   r   r   r   test_coplanar  s    
$,zTestDelaunay.test_coplanarc                 C   s:   dddddg}t j|dd}tdd	g}t|j| d S )
Nr    r!   r#   r%   皙?r   Tfurthest_site)r"   r.   r   )r.   r*   r   )rM   rc   r   rN   r   rd   )rT   r@   rf   r   r   r   r   test_furthest_site  s    zTestDelaunay.test_furthest_siter>   c           	      C   s  t | \}}tj|dd}tj|d d|d}|dd  D ]}|| q:t|}tj|d d|d}t|dkr|jtj|dd  dddd |drtt	|j
 t|jd  tt	|j
 t|jd  nt|j
|j
td t|j
|j
td d S )	Nr   r   Tr   r   r"   r   ZpathologicalrL   )r<   r   concatenaterM   rc   r   r:   
startswithr   r   rd   r   r   r8   r   r   )	rT   r>   rC   rB   r@   objchunkobj2obj3r   r   r   test_incremental  s8    



zTestDelaunay.test_incrementalc              	   C   s8   t dddg}d}tjt|d |j W 5 Q R X d S )Nr    r!   r#   ziDelaunay attribute 'vertices' is deprecated in favour of 'simplices' and will be removed in Scipy 1.11.0.)match)rM   rc   r   ZwarnsDeprecationWarningr   )rT   rf   msgr   r   r   test_vertices_deprecation"  s    z&TestDelaunay.test_vertices_deprecationN)rX   rY   rZ   r   r   r   r   r   r   r   r   r   r   r   r   parametrizer   r<   r   r  r   r   r   r   r     s   
#r   c                 C   s  t tt|}t tt|}||kr| jd dkrdttj }|D ]4\}}|D ]\}}| | | |  }|tj	| }t
|d  |d g}	t|	| | | |  }
t|	| | | |  }tt|
|	dsqXtt||	dsqXt|| | | |  }
t|| | | |  }t|| | | |  }|
| k sX|
|| krXqX|| k sX||| krrqX qJqXtdqJd S t|| d S )Nr"   r*   i  r   zcomparison fails)r   r   r   r8   r   r   r   r   r   ZnormrN   rl   allcloser=   r   )r@   Zfacets_1Zfacets_2r   r   r   ZapZbptrr   Zc1c2c3r   r   r   assert_hulls_equal*  s4    
r  c                   @   s  e Zd Zdd Zdd Zejdee	dd Z
ejdeedd	 Zd
d Zdd Zejdddgdd Zejdddgejdeddggejdddddgedfeddggejdddddgedfeddggejdddddgedfeddgddggejdddddgedfeddggejdddddgedfgdd  Zejdddgd!d" Zejdddgd#d$ Zejdddgd%d& Zd'S )(TestConvexHullc                 C   s   t jd}tttj| d S r   )r   r   r   rR   r   rM   r   r   r   r   r   r   b  s    z&TestConvexHull.test_masked_array_failsc                 C   s.   t jdddt jfgt jd}tttj| d S )Nr    r$   r*   r\   )r   rN   r   rb   rR   r   rM   r   r   r   r   r   r   f  s    z)TestConvexHull.test_array_with_nans_failsr>   c                 C   sn   t | }t|}t|}t||j|j |jd dkrVtt	
|jt	|j ntt	
|j|j d S )Nr"   r*   )r7   rM   rc   r   r  ru   rd   r8   r   r   r   r   r   )rT   r>   r@   rf   r   r   r   r   test_hull_consistency_trij  s    

z(TestConvexHull.test_hull_consistency_tric           	      C   s   t | \}}tj|dd}tj|d dd}|dd  D ]}|| q8t|}tj|d dd}t|dkr|jtj|dd  dddd t||j|j t||j|j d S )Nr   r   Tr   r"   r   )	r<   r   r   rM   r   r   r:   r  rd   )	rT   r>   rC   _r@   r   r   r   r   r   r   r   r   {  s    
zTestConvexHull.test_incrementalc                 C   s   t jd t jdd}t|}tt |jt 	|j
 |j|j
 j\}}t ||  ||  }tt t t |dk d S )Nr   r,   r*   r   )r   r   r   r   rM   r   r   r   rd   r   r   r@   r   Zarctan2meanr   r   Zdiffunwrap)rT   r@   r   r   rU   Zangler   r   r   test_vertices_2d  s    
zTestConvexHull.test_vertices_2dc              
   C   sH   t ddddddddg}t|}t|jd	d
d t|jdd
d d S )Nrx   ry   rz   r{   r|   r}   r~   r   rJ   r   r   r   r   rv   r   r   r   r     s       
zTestConvexHull.test_volume_arear   FTc                 C   sb   t ddgddgddgddgddgg}tj||dd}t jddddgtd	}|j}t|| d S )
Nr_   皙?r^   333333?QG4r@   r   r   FTr\   r   rN   rM   r   boolgoodr   rT   r   r@   r   r   actualr   r   r   test_good2d  s    
zTestConvexHull.test_good2d
visibilityr  zQG-4znew_gen, expectedr^   gffffff?r\   gffffffg=
ףp=?r&   r  g433333?c                 C   sh   t ddgddgddgddgddgg}tj|d|d}|| |j}d|krZt |}t|| d S )Nr_   r  r^   r  Tr  -)r   rN   rM   r   r   r  invertr   )rT   Znew_genr   r  r@   r   r  r   r   r   test_good2d_incremental_changes  s    '


z.TestConvexHull.test_good2d_incremental_changesc                 C   sr   t ddgddgddgddgddgg}tj||d}|j}|d ksHt|rn|t d |j}|d ksntd S )Nr_   r  r^   r  )r@   r   r"   r*   )r   rN   rM   r   r  r=   r   r   )rT   r   r@   r   r  r   r   r   test_good2d_no_option  s    
z$TestConvexHull.test_good2d_no_optionc                 C   sb   t ddgddgddgddgddgg}tj||dd}t jddddgtd}|j}t|| d S )Nr_   r  r^   r  r  Fr\   r  r  r   r   r   test_good2d_inside  s    
z!TestConvexHull.test_good2d_insidec              	   C   sh   t dddgdddgdddgdd	d
gdddgg}tj||dd}t jddddgtd}t|j| d S )Nr'   gN7?g+xٿgg>@?gFE%'?gWۧ=g/J?g6r?gtĭ4gŷgw6y?gQgb?ZQG0r  TFr\   )r   rN   rM   r   r  r   r  )rT   r   r@   r   r   r   r   r   test_good3d  s    zTestConvexHull.test_good3dN)rX   rY   rZ   r   r   r   r   r  r   r7   r	  r<   r   r  r   r  r   rN   r  r  r  r  r  r   r   r   r   r  a  sL   

	


r  c                   @   s   e Zd Zejdddgejdddgejddd	gd
d Zdd Zdd Zdd Z	ejde
edd Zdd Zdd Zejde
edd ZdS )TestVoronoizqhull_opts, extra_pts)z	Qbb Qc Qzr"   )zQbb Qcr   n_pts2   d   rA   r*   r-   c           
      C   s   t jd}|||f}t||d}|j}| |d | ksDt|jt|j	| ks\tt|j	|| ksrt|j
jd |kst|rdd |j	D }	|	ddkst|	d|kstd S )Nin  r   r"   r   c                 S   s   g | ]}t |qS r   r:   r   r   r   r   r   r   ;  s     z;TestVoronoi.test_point_region_structure.<locals>.<listcomp>)r   r   Zdefault_rngr	   point_regionr   r=   sizer:   regionsr@   r8   countindex)
rT   Z
qhull_optsr!  Z	extra_ptsrA   rngr@   vorZ	pt_regionZsublensr   r   r   test_point_region_structure  s    z'TestVoronoi.test_point_region_structurec                 C   s   t jd}tttj| d S r   )r   r   r   rR   r   rM   r	   r   r   r   r   r   A  s    z#TestVoronoi.test_masked_array_failsc              	   C   s*   ddddddddd	g	}d
}|  || d S )Nr    r!   )r   r*   r#   r$   r  )r*   r   )r*   r"   )r*   r*   a	  
        2
        5 10 1
        -10.101 -10.101
           0.5    0.5
           0.5    1.5
           1.5    0.5
           1.5    1.5
        2 0 1
        3 2 0 1
        2 0 2
        3 3 0 1
        4 1 2 4 3
        3 4 0 2
        2 0 3
        3 4 0 3
        2 0 4
        0
        12
        4 0 3 0 1
        4 0 1 0 1
        4 1 4 1 2
        4 1 2 0 2
        4 2 5 0 2
        4 3 4 1 3
        4 3 6 0 3
        4 4 5 2 4
        4 4 7 3 4
        4 5 8 0 4
        4 6 7 0 3
        4 7 8 0 4
        _compare_qvoronoirT   r@   outputr   r   r   test_simpleE  s         zTestVoronoi.test_simplec                 K   sZ  dd |   D }t|d d }ttt|dd|  }t|d d }dd |d| d| |  D }dd |d| | d	 D }d
d |d| | d	 D }	tj|f|}
dd }t|
j	| t
ttt|
jttt| tttt||tt||	}tttt||
j tt||
j}|  |  t
|| d	S )z:Compare to output from 'qvoronoi o Fv < data' to Voronoi()c                 S   s   g | ]}t tt| qS r   )r   r   r   splitr%  r   r   r   r   r  s     z1TestVoronoi._compare_qvoronoi.<locals>.<listcomp>r"   r   r-   r*   c                 S   s"   g | ]}d d |dd D qS )c                 S   s   g | ]}t |d  qS r   r   r   rU   r   r   r   r   v  s     <TestVoronoi._compare_qvoronoi.<locals>.<listcomp>.<listcomp>r"   Nr   r%  r   r   r   r   v  s   c                 S   s"   g | ]}d d |dd D qS )c                 S   s   g | ]}t |qS r   r4  r5  r   r   r   r   x  s     r6  r"   r-   r   r%  r   r   r   r   x  s   Nc                 S   s"   g | ]}d d |dd D qS )c                 S   s   g | ]}t |d  qS r   r4  r5  r   r   r   r   z  s     r6  r-   Nr   r%  r   r   r   r   z  s   c                 S   s   t t| S r
   r   r   r   r   r   	sorttuple  s    z0TestVoronoi._compare_qvoronoi.<locals>.sorttuple)strip
splitlinesr   r   r   r   rM   r	   r   r   r   r   r(  zipridge_pointsr   ridge_verticesr   )rT   r@   r1  kwZnvertexr   Znregionr(  r;  r<  r,  r7  p1p2r   r   r   r/  n  s4    "zTestVoronoi._compare_qvoronoir>   c                 C   s   t | }t|}t|}|j D ]\}}tt|dksBq$|j	| j
dd}d||d  |  }|j|| dd\}	}
t|
|d  |j|| dd\}	}
t|
|d  q$d S )Nr   r   gư>r"   )r   )r7   KDTreerM   r	   Z
ridge_dictitemsr   r   asarrayr   r  queryr   )rT   r>   r@   treer,  rg   rp   Zridge_midpointrs   ro   r   r   r   r   test_ridges  s    
zTestVoronoi.test_ridgesc                 C   s&   dddddg}d}| j ||dd d S )	Nr    r!   r#   r%   r   a'  
        2
        3 5 1
        -10.101 -10.101
        0.6000000000000001    0.5
           0.5 0.6000000000000001
        3 0 2 1
        2 0 1
        2 0 2
        0
        3 0 2 1
        5
        4 0 2 0 2
        4 0 4 1 2
        4 0 1 0 1
        4 1 4 0 1
        4 2 4 0 2
        Tr   r.  r0  r   r   r   r     s    zTestVoronoi.test_furthest_sitec                 C   s>   dddddg}t |}t|jd t |dd}t|jd d S )	Nr    r!   r#   r%   r   FTr   )r	   r   r   )rT   r@   r,  r   r   r   test_furthest_site_flag  s
    z#TestVoronoi.test_furthest_site_flagc                    s  t | d d jd dkrd S t | \}}tj|dd}tj|d d|d}|dd  D ]}|| qXt|}tj|d d|d}t|dkr|jtj|dd  dddd tt|j	t|j	 tt|j	t|j	 ||fD ] ddit
 jD ]4\}	}
t
|jD ] \}}t|
|r||	< qq fd	d
dd }t| j||j t| j||j qd S )Nr   r"   r-   r   Tr   r   r(   c              
      sl   t | dr$ttfdd| D S z
|  W S  tk
rf } ztd j|  f |W 5 d }~X Y nX d S )N__len__c                    s   g | ]} |qS r   r   r5  )remapr   r   r     s     z?TestVoronoi.test_incremental.<locals>.remap.<locals>.<listcomp>z,incremental result has spurious vertex at %r)hasattrr   r   KeyErrorr=   r   )r   eZobjxrH  Z
vertex_mapr   r   rH    s    


z+TestVoronoi.test_incremental.<locals>.remapc                 S   s:   t tt| }d|kr |d dd |D }|  |S )Nr   c                 S   s   g | ]}t |d kr|qS r   r$  r%  r   r   r   r     s      zDTestVoronoi.test_incremental.<locals>.simplified.<locals>.<listcomp>)r   r   r   remover   )r   rA  r   r   r   
simplified  s    
z0TestVoronoi.test_incremental.<locals>.simplified)r<   r8   r   r   rM   r	   r   r:   r   r&  rj   r   r  r(  r<  )rT   r>   rC   rB   r@   r   r   r   r   rh   rp   rD   Zv2rN  r   rL  r   r     sF    
	zTestVoronoi.test_incrementalN)rX   rY   rZ   r   r   r  r-  r   r2  r/  r   r7   rE  r   rF  r<   r   r   r   r   r   r     s    )!
r   c                   @   sR   e Zd ZdddZejdeje	gdd Z
dd Zd	d
 Zdd Zdd ZdS )Test_HalfspaceIntersectionr   c                 C   sx   t |j|j tj|jd ftd}|D ]>}tt|| |k jddd }t |jd d||d < q(t|  dS )z2Check that every line in arr1 is only once in arr2r   r\   r"   r   r   TN)	r   r8   r   r   r  r   r   r   r   )rT   Zarr1Zarr2r   Ztruthsl1Zindexesr   r   r   assert_unordered_allclose  s    "z4Test_HalfspaceIntersection.assert_unordered_allclosedtc                 C   s~   t jdddgdddgdddgdddgg|d}t jddg|d}t ddgddgddgddgg}t||}t|j| d S )Nr(   r   r"   r\   r'   rK   )r   rN   rM   HalfspaceIntersectionr   intersections)rT   rR  
halfspacesfeasible_pointr@   r   r   r   r    test_cube_halfspace_intersection  s    "z;Test_HalfspaceIntersection.test_cube_halfspace_intersectionc           
      C   sD  t jt jtdd}t|}t|d d dd f |d d d df f }tddddg}t	
||}t|jjd t|jd tt|jd |jD ]}tt|d q|d d d	f |d d d d	f | }| |d d d d	f j| j|j tddd
dg}|D ]$}	tt|j|	kjddd qd S )Nr   zselfdual-4d-polytope.txtr"   r'   )   r.   g      @@rY  r0   r(   r&         r   )r   r   r   r   r   r   Z
genfromtxthstackrN   rM   rT  r   rU  r8   r   Zdual_volumer:   dual_facetsrl   rQ  r   dual_points	itertoolspermutationsr   r   )
rT   fnameZineqsrV  
feas_pointhsZfacetdistsr@   pointr   r   r   $test_self_dual_polytope_intersection$  s"    
0
*&z?Test_HalfspaceIntersection.test_self_dual_polytope_intersectionc                 C   s   t dddgdddgdddgdddgg}t dddg}tttj|| t dgdgg}tttj|| t ddgg}tttj|| t ddg}ttjtj|| d S )N      r'   rJ   r&   rZ  )r   rN   rR   r   rM   rT  Z
QhullError)rT   rV  rW  r   r   r   test_wrong_feasible_point;  s    z4Test_HalfspaceIntersection.test_wrong_feasible_pointc                 C   s  t ddddgddddgddddgddddgddddgddddgg}t dddgdddgdddgdddgg}t dggd }t t || f|f}t dddg}tj||dd}tj||dd}t|D ]\}}	||	t jd d f  |j|	t jd d f dd	 t ||d |d d d f f}
t|
|}t	|j
|j
 t	|j
|j
 t	|j|j | |j|j q|  d S )
Nr'   rf  rZ  rJ   r"   r2   Tr   r   )r   rN   r[  ZvstackrM   rT  rj   Zadd_halfspacesZnewaxisr   rV  rU  rQ  rQ   )rT   rV  Zextra_normalsoffsetsZextra_halfspacesra  Zinc_hsZ
inc_res_hsrh   Zehstotalrb  r   r   r   r   N  s:    




"z+Test_HalfspaceIntersection.test_incrementalc                 C   s4  t ddddgddddgddddgddddgddddgddddgg}t dddg}t||}t dddgdddgdddgdddgdddgdddgg}dddgddd	gd
ddgdd
d	gddd	gdddgd
dd	gdd
dgg}t|t|jkstt||jD ] \}}t|t|kstqt	|j
| d S )Nrf  r'   rJ   r&   rS  r   r*   r.   r"   r/   r-   )r   rN   rM   rT  r:   r\  r=   r:  r   r   r]  )rT   rV  rd  rb  Zqhalf_pointsZqhalf_facetsr   r   r   r   r   	test_cubex  s<    





z$Test_HalfspaceIntersection.test_cubeN)r   )rX   rY   rZ   rQ  r   r   r  r   r   r   rX  re  rg  r   rj  r   r   r   r   rO  
  s   

*rO  )4r   rO   Znumpyr   Znumpy.testingr   r   r   r   r   r   r   rR   Zscipy.spatial._qhullZspatialZ_qhullrM   Zscipy.spatialr   r@  r	   r^  r   r   r   r   r   r   r@   rN   Zpathological_data_1r   Zfloat_r   Zpathological_data_2r   Zbug_2850_chunksZbug_2850_chunks_2rB  r7   r<   rF   r>   r?   rH   r[   r   r   r  r  r   rO  r   r   r   r   <module>   s$                                                                      $, W! 7 = n