
    +j^                        d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
mZ ddlmZ ddlmZmZ ddlmZmZ ddlmZ d	d
lmZ dag adadag adadadad Z  G d dej!                  Z"d Z#d Z$d Z%d Z&d#dZ'd#dZ(d Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0d Z1e2dk    r G d  d!e"          Z3 e-e3d"fg           dS dS )$a	   This module attempts to make it easy to create VTK-Python
unittests.  The module uses unittest for the test interface.  For more
documentation on what unittests are and how to use them, please read
these:

   http://www.python.org/doc/current/lib/module-unittest.html

   http://www.diveintopython.org/roman_divein.html


This VTK-Python test module supports image based tests with multiple
images per test suite and multiple images per individual test as well.
It also prints information appropriate for CDash
(http://open.kitware.com/).

This module defines several useful classes and functions to make
writing tests easy.  The most important of these are:

class vtkTest:
   Subclass this for your tests.  It also has a few useful internal
   functions that can be used to do some simple blackbox testing.

compareImage(renwin, img_fname, threshold=0.15):
   Compares renwin with image and generates image if it does not
   exist.  The threshold determines how closely the images must match.
   The function also handles multiple images and finds the best
   matching image.

compareImageWithSavedImage(src_img, img_fname, threshold=0.15):
   Compares given source image (in the form of a vtkImageData) with
   saved image and generates the image if it does not exist.  The
   threshold determines how closely the images must match.  The
   function also handles multiple images and finds the best matching
   image.

getAbsImagePath(img_basename):
   Returns the full path to the image given the basic image name.

main(cases):
   Does the testing given a list of tuples containing test classes and
   the starting string of the functions used for testing.

interact():
    Interacts with the user if necessary.  The behavior of this is
    rather trivial and works best when using Tkinter.  It does not do
    anything by default and stops to interact with the user when given
    the appropriate command line arguments.

isInteractive():
    If interact() is not good enough, use this to find if the mode is
    interactive or not and do whatever is necessary to generate an
    interactive view.

Examples:

  The best way to learn on how to use this module is to look at a few
  examples.  The end of this file contains a trivial example.  Please
  also look at the following examples:

    Rendering/Testing/Python/TestTkRenderWidget.py,
    Rendering/Testing/Python/TestTkRenderWindowInteractor.py

Created: September, 2002

Prabhu Ramachandran <prabhu@aero.iitb.ac.in>
    )absolute_importN)
vtkCommandvtkDebugLeaks)vtkTimerLog)vtkPNGReadervtkPNGWriter)vtkImageDifferencevtkImageShiftScale)vtkWindowToImageFilter   )BlackBox c                  .    t          j        d           dS )z>Cause the test to be skipped due to insufficient requirements.}   N)sysexit     Q/DATA/AppData/hermes/venv/lib/python3.11/site-packages/vtkmodules/test/Testing.pyskipr   l   s    HSMMMMMr   c                       e Zd ZdZ ej        d          Z e            Ze	                    d           dZd Z
g fdZg fdZd Zd	 Zd
 Zd ZdS )vtkTesta  A simple default VTK test class that defines a few useful
    blackbox tests that can be readily used.  Derive your test cases
    from this class and use the following if you'd like to.

    Note: Unittest instantiates this class (or your subclass) each
    time it tests a method.  So if you do not want that to happen when
    generating VTK pipelines you should create the pipeline in the
    class definition as done below for _blackbox.
    r   )debugNc                 :    | j                             |           dS )a  Does a blackbox test by attempting to parse the class for
        its various methods using vtkMethodParser.  This is a useful
        test because it gets all the methods of the vtkObject, parses
        them and sorts them into different classes of objects.N)	_blackbox	testParse)selfobjs     r   
_testParsezvtkTest._testParse   s     
 	  %%%%%r   c                 <    | j                             ||           dS )zChecks the Get/Set method pairs by setting the value using
        the current state and making sure that it equals the value it
        was originally.  This effectively calls _testParse
        internally. N)r   
testGetSetr   r   excluded_methodss      r   _testGetSetzvtkTest._testGetSet   s#    
 	!!#'788888r   c                 <    | j                             ||           dS )zChecks the Boolean methods by setting the value on and off
        and making sure that the GetMethod returns the set value.
        This effectively calls _testParse internally. N)r   testBooleanr"   s      r   _testBooleanzvtkTest._testBoolean   s#     	""3(899999r   c                     |st           S t           D ]/}|t          j                            |          d         k    r|c S 0|S )aq  Given a filename with no path (i.e., no leading directories
        prepended), return the full path to a file as specified on the
        command line with a '-D' option.

        As an example, if a test is run with "-D /path/to/grid.vtu"
        then calling

            self.pathToData('grid.vtu')

        in your test will return "/path/to/grid.vtu". This is
        useful in combination with ExternalData, where data may be
        staged by CTest to a user-configured directory at build time.

        In order for this method to work, you must specify
        the JUST_VALID option for your test in CMake.
        )VTK_DATA_PATHSospathsplitr   filenamer,   s      r   
pathToDatazvtkTest.pathToData   sR    $  	"!!" 	 	D27==..r222 3r   c                     |st           S t           D ]/}|t          j                            |          d         k    r|c S 0|S )a  Given a filename with no path (i.e., no leading directories
        prepended), return the full path to a file as specified on the
        command line with a '-V' option.

        As an example, if a test is run with
        "-V /path/to/validImage.png" then calling

            self.pathToData('validImage.png')

        in your test will return "/path/to/validImage.png". This is
        useful in combination with ExternalData, where data may be
        staged by CTest to a user-configured directory at build time.

        In order for this method to work, you must specify
        the JUST_VALID option for your test in CMake.
        r)   )VTK_BASELINE_PATHSr+   r,   r-   r.   s      r   pathToValidatedOutputzvtkTest.pathToValidatedOutput   sR    $  	&%%& 	 	D27==..r222 3r   c           	         dd l  j                     g fd}fd}fd}|                    t          j        |           |                    t          j        |           |                    t          j        |           |                    t          j        |           |                                 |                                }d}d|v rt                      }|
                                 |                    |           |                                 |d         }	t          j                            |	          d         }t!                      }
|
                    |	           |
                    |                                           	 |
                                 n8# t*          $ r+ |                                 |
                                 Y nw xY w|                                }|                                fdt3                                                    D             }g d	}g d
}g d}t7          |          dk    rV|d                                         }|                                }|                                }|                                }tA          d!                    |||||                     S )Nr   c                     t          d|                                 z   dz                                   j                    z
  ||                                 f           d S )Nzkey is "")print	GetKeySymappendtimecallereventIdevents	startTimer:   s     r   
onKeyPressz,vtkTest.prepareTestImage.<locals>.onKeyPress   s_    *v//111C7888MM949;;2GV=M=M=O=OPQQQQQr   c                 V                          j                    z
  |f           d S N)r9   r:   r;   s     r   onButtonz*vtkTest.prepareTestImage.<locals>.onButton   s-    MM949;;2G<=====r   c                 |                          j                    z
  ||                                 f           d S rB   )r9   r:   GetEventPositionr;   s     r   
onMovementz,vtkTest.prepareTestImage.<locals>.onMovement   s;    MM949;;2GV=T=T=V=VWXXXXXr   baselineFilenamer/   r)   c                 :    g | ]}                     |          S r   )GetItemAsObject).0irrcs     r   
<listcomp>z,vtkTest.prepareTestImage.<locals>.<listcomp>   s'    MMM!s""1%%MMMr   )r   r   r   )r   r   r   )r   r   r   a%  
        Replace prepareTestImage() in your script with the following to make a test:

            camera.SetPosition({eye[0]}, {eye[1]}, {eye[2]})
            camera.SetFocalPoint({aim[0]}, {aim[1]}, {aim[2]})
            camera.SetViewUp({up[0]}, {up[1]}, {up[2]})
            renwin.SetSize({rsz[0]}, {rsz[1]})
            self.assertImageMatch(renwin, '{baseline}')

        Be sure that "renwin" and "camera" are valid variables (or rename them in the
        snippet above) referencing the vtkRenderWindow and vtkCamera, respectively.
        )eyeaimuprszbaseline)"r:   AddObserverr   KeyPressEventLeftButtonPressEventLeftButtonReleaseEventMouseMoveEventStartGetRenderWindowr   ReadFrontBufferOffSetInputUpdater+   r,   r-   r   SetFileNameSetInputConnectionGetOutputPortWriteRuntimeErrorReadFrontBufferOnGetSizeGetRenderersrangeGetNumberOfItemslenGetActiveCameraGetPositionGetFocalPoint	GetViewUpr7   format)r   
interactorkwargsr@   rC   rF   rwrR   w2ifbaselineWithPathpngwrQ   rrsrN   rO   rP   camr>   rL   r?   r:   s                    @@@@r   prepareTestImagezvtkTest.prepareTestImage   s   DIKK		R 	R 	R 	R 	R 	R 	R	> 	> 	> 	> 	> 	> 	>	Y 	Y 	Y 	Y 	Y 	Y 	Y 	z7DDDz>IIIz@(KKKz8*EEE''))%)++D##%%%MM"KKMMM%j1w}}%566r:H>>D-...##D$6$6$8$8999

   &&(((

 jjllooMMMMuS5I5I5K5K/L/LMMMggggggs88a<<a&((**C//##C##%%C--//B  FshFGG	I 	I 	I s   !F6 62G+*G+c                 v    |}	 t          |d           n#  t          |          }Y nxY wt          ||fi | dS )a  Throw an error if a rendering in the render window does not match the baseline image.

        This method accepts a threshold keyword argument (with a default of 0.15)
        that specifies how different a baseline may be before causing a failure.
        rN)opengetAbsImagePathcompareImage)r   renwinrR   rn   absoluteBaselines        r   assertImageMatchzvtkTest.assertImageMatch	  s_     $	9!3''''	9.x88V-8888888s    ()__name__
__module____qualname____doc__r   Testerr   r   dlSetExitErrorr   r$   r'   r0   r3   ru   r}   r   r   r   r   r   q   s           a(((I 
BOOA	B& & & 13 9 9 9 9 24 : : : :  2  2= = =~9 9 9 9 9r   r   c                  6    t           rt          d           dS dS )z&Interacts with the user if necessary. z6
Press Enter/Return to continue with the testing. --> N)	_INTERACT	raw_inputr   r   r   interactr     s.      MKLLLLLM Mr   c                      t           S )z]Returns if the currently chosen mode is interactive or not
    based on command line options.)r   r   r   r   isInteractiver     s
     r   c                     t           D ])}t          j                            |          | k    r|c S *t          j                            t
          |           S )zBReturns the full path to the image given the basic image
    name.)r2   r+   r,   basenamejoinVTK_BASELINE_ROOT)img_basenamer,   s     r   ry   ry   !  sS     #  7D!!\11KKK 27<<)<888r   c                     t           j                            t          t           j                            |           d                   }t           j                            |          S )Nr   )r+   r,   r   VTK_TEMP_DIRr-   abspath)	img_fnamexs     r   _getTempImagePathr   )  s>    
\27==#;#;A#>??A7??1r   333333?c                    t           rdS t          j                            |          \  }}t          j                            |          st                      }|                    t          |                     |                    | 	                                           |
                                 t          |           d|z   dz   t          |          z   }dt          _        t          |          t                      }|                    |           |                                 t#                      }|                    | 	                                           |                    |	                                           |                                 |                                }	|	}
d}d}|	|k    rd}d}d}	 |d|z  z   }t          j                            |          snr|                    |           |                                 |                                 |                                }||k     r	|}d}|}	|}
n||	k     r|}|}	|}
|dz   }|rTt+          |||           t-          |
||           d|                                z  }dt          _        t          |          t/          |
|           dS )	ah  Compares a source image (src_img, which is a vtkImageData) with
    the saved image file whose name is given in the second argument.
    If the image file does not exist the image is generated and
    stored.  If not the source image is compared to that of the
    figure.  This function also handles multiple images and finds the
    best matching image.
    NzMissing baseline image: z
Test image created: r   r   r)   z_%d.pngzFailed image test: %f
)	_NO_IMAGEr+   r,   splitextisfiler   r]   r   r^   r_   r`   _printCDashImageNotFoundErrorr   tracebacklimitra   r   r\   r	   SetImageConnectionGetThresholdedErrorexists_handleFailedImage_printCDashImageError_printCDashImageSuccess)src_imgr   	thresholdf_basef_extrr   msgpngridiffmin_errimg_err	err_indexcounttest_failed	new_fnamealt_errs                   r   compareImageWithSavedImager   -  s     G$$Y//MFE7>>)$$ 	 ~~*955666 5 5 7 7888

%i000(947OORcdmRnRnn3>>DYKKMMM  E	W2244555	T//11222	LLNNN''))GGIE		50I7>>),, Y'''KKMMMLLNNN//11G""!	!!W$$ %I%G%GAIE1	6  	$udI666!'9f===+E,E,E,G,GGC!"Cs###GY/////r   c                 >   t           rdS t                      }|                                 |                    |            |                                 	 t          |||           n5# t          $ r( |                                 t          |||           Y nw xY wdS )aa  Compares renwin's (a vtkRenderWindow) contents with the image
    file whose name is given in the second argument.  If the image
    file does not exist the image is generated and stored.  If not the
    image in the render window is compared to that of the figure.
    This function also handles multiple images and finds the best
    matching image.  N)r   r   rZ   r[   r\   r   ra   rb   )r{   r   r   rp   s       r   rz   rz   ~  s      !##DMM&KKMMM?"4I>>>> ? ? ?   "4I>>>>>? Fs   A( (/BBc                 :   t          |          }t          d| z             t          d| z             |dk    rt          d           nt          d|z             t          d|dz   z             t          d|d	z   z             t          d
|dz   z             dS )z(Prints the XML data necessary for CDash.z Failed image test with error: %fO<DartMeasurement name="ImageError" type="numeric/double"> %f </DartMeasurement>r   S<DartMeasurement name="BaselineImage" type="text/string">Standard</DartMeasurement>S<DartMeasurement name="BaselineImage" type="numeric/integer"> %d </DartMeasurement>zQ<DartMeasurementFile name="TestImage" type="image/png"> %s </DartMeasurementFile>.pngzW<DartMeasurementFile name="DifferenceImage" type="image/png"> %s </DartMeasurementFile>	.diff.pngzR<DartMeasurementFile name="ValidImage" type="image/png"> %s </DartMeasurementFile>
.valid.pngN)r   r7   )r   r   img_bases      r   r   r     s     **H	
,W
4555	 "")* + + +A~~ghhhh &&/0 	1 	1 	1 
 &'/&'8: ; ; ; 
 &'/+'=? @ @ @	 &'/,'>@ A A A A Ar   c                 0    t          d| z   dz              dS )zLPrints the XML data necessary for Dart when the baseline image is not found.z9<DartMeasurement name="ImageNotFound" type="text/string">z</DartMeasurement>Nr7   )r   s    r   r   r     s"    	
II
UXl
lmmmmmr   c                 |    t          d| z             |dk    rt          d           dS t          d|z             dS )z3Prints XML data for Dart when image test succeeded.r   r   r   r   Nr   )r   r   s     r   r   r     sh    	 "")* + + +A~~ghhhhh %%./ 0 0 0 0 0r   c                 l   t           j                            |          \  }}t                      }|                    |                                            |                    d           |                    d           t                      }|	                    t          |dz                        |                    |                                           |                                 |                    |                     dd                     |	                    t          |dz                        |                                 |                    |                     dd                     |	                    t          |dz                        |                                 dS )zDWrites all the necessary images when an image comparison
    failed.r   
   r   r   r   r   N)r+   r,   r   r
   r^   r_   SetShiftSetScaler   r]   r   r`   GetInputConnection)r   r   r   r   r   gammarr   s          r   r   r     ss    G$$Y//MFE   E	U0022333	NN1	NN2>>D&v';<<===E//11222JJLLL 	E44Qq99:::&v77888JJLLL 	E44Qq99:::&v'<==>>>JJLLLLLr   c                    t                       t                      }|                                }t          j                    }t	          |           }|                                |z
  }t          t          j                    |z
            }t          d|z             t          d|z             ~ ~|                                rt          j	        d           dS t          j	        d           dS )z Pass a list of tuples containing test classes and the starting
    string of the functions used for testing.

    Example:

    main ([(vtkTestClass, 'test'), (vtkTestClass1, 'test')])
    zN<DartMeasurement name="WallTime" type="numeric/double">  %f </DartMeasurement>zM<DartMeasurement name="CPUTime" type="numeric/double">  %f </DartMeasurement>r   r   N)
processCmdLiner   
GetCPUTimer:   testfloatr7   wasSuccessfulr   r   )casestimers_times_wall_timeresulttot_timetot_wall_times          r   mainr     s     MMEF)++K %[[F!!F*H$)++344M 
 ##01 2 2 2	 ##+, - - - 	u r   c                    g }| D ]6}|                     t          j        |d         |d                              7t          j        |          }t          j        t
                    }|                    |          }|S )a    Pass a list of tuples containing test classes and the
    functions used for testing.

    It returns a unittest._TextTestResult object.

    Example:

      test = test_suite([(vtkTestClass, 'test'),
                        (vtkTestClass1, 'test')])
    r   r   )	verbosity)r9   unittest	makeSuite	TestSuiteTextTestRunner_VERBOSErun)r   suitescase
test_suiterunnerr   s         r   r   r     s}     F < <h(a$q'::;;;;#F++J $x888FZZ
##FMr   c                  
    d} | S )Na   Usage:
TestScript.py [options]
Where options are:


    -D /path/to/VTKData
    --data-dir /path/to/VTKData

          Directory containing VTK Data use for tests.  If this option
          is not set via the command line the environment variable
          VTK_DATA_ROOT is used.  If the environment variable is not
          set the value defaults to '../../../../../VTKData'.

    -B /path/to/valid/image_dir/
    --baseline-root /path/to/valid/image_dir/

          This is a path to the directory containing the valid images
          for comparison.  If this option is not set via the command
          line the environment variable VTK_BASELINE_ROOT is used.  If
          the environment variable is not set the value defaults to
          the same value set for -D (--data-dir).

    -T /path/to/valid/temporary_dir/
    --temp-dir /path/to/valid/temporary_dir/

          This is a path to the directory where the image differences
          are written.  If this option is not set via the command line
          the environment variable VTK_TEMP_DIR is used.  If the
          environment variable is not set the value defaults to
          '../../../../Testing/Temporary'.

    -V /path/to/validated/output.png
    --validated-output /path/to/valid/output.png

          This is a path to a file (usually but not always an image)
          which is compared to data generated by the test.

    -v level
    --verbose level

          Sets the verbosity of the test runner.  Valid values are 0,
          1, and 2 in increasing order of verbosity.

    -I
    --interact

          Interacts with the user when chosen.  If this is not chosen
          the test will run and exit as soon as it is finished.  When
          enabled, the behavior of this is rather trivial and works
          best when the test uses Tkinter.

    -n
    --no-image

          Does not do any image comparisons.  This is useful if you
          want to run the test and not worry about test images or
          image failures etc.

    -h
    --help

                 Prints this message.

r   )r   s    r   usager     s    <	Cz Jr   c                     t           j        dd          } d}g d}	 d}t          |           D ]\  }}|                    d          r|} nt	          j        | |d          ||          \  }}ni# t          j        $ rW}t          t                                 t          d           t          |           t          j        d           Y d }~nd }~ww xY w||fS )Nr   zB:D:T:V:v:hnI)zbaseline-root=z	data-dir=z	temp-dir=zvalidated-output=zverbose=helpzno-imager   r   -zF----------------------------------------------------------------------)	r   argv	enumerate
startswithgetopterrorr7   r   r   )		argumentsoptionslong_optionsfirstrK   argoptsargsr   s	            r   parseCmdLiner   R  s    IG, , ,L	** 	 	FAs~~c""  ]9UVV#4g|LL
dd<   eggfc


!	 :s   AA. .C=ACCc                  n   t                      \  } }	 t          j        d         an/# t          $ r" t          j                            d          aY nw xY w	 t          j        d         an# t          $ r Y nw xY w	 t          j        d         an/# t          $ r" t          j                            d          aY nw xY w| D ]e\  }}|dv r[t          j        	                    |          }t          j        
                    |          rt                              |           n|a|dv rt          j        	                    |          a|dv rt          j        	                    |          a|d	v r7t                              t          j        	                    |                     |d
v rda|dv rda|dv r=	 t!          |          an,#  d}t%          |           t'          j        d           Y nxY w|dv r.t%          t+                                 t'          j                     gt          s	t          ad S d S )NVTK_DATA_ROOTz../../../../../VTKDatar   r   z../../../../Testing/Temporary)z-Dz
--data-dir)z-Bz--baseline-root)z-Tz
--temp-dir)z-Vz--validated-output)z-nz
--no-imager   )z-Iz
--interact)z-vz	--verbosez3Verbosity should be an integer.  0, 1, 2 are valid.)z-hz--help)r   r+   environr   KeyErrorr,   normpathr   r   r   r   r*   r9   r2   r   r   intr   r7   r   r   r   )r   r   oaoar   s         r   r   r   k  sa   JD$C
?3 C C C(()ABBCJ':;   Iz.1 I I Iw''(GHHI   1$$$##Bw~~b!! #%%b)))) "))) " 2 2$$$7??1--L,,,%%bgooa&8&8999$$$I$$$I###q66Ic


   %''NNNHJJJ *)* *s?   & )AAA) )
A65A6:B )B98B95G'G.__main__c                   @    e Zd ZddlmZ  e            Zd Zd Zd ZdS )
SampleTestr   )vtkActorc                 :    |                      | j                   dS )zTest if class is parseableN)r   r   r   s    r   r   zSampleTest.testParse  s    OODH%%%%%r   c                 :    |                      | j                   dS )zTesting Get/Set methodsN)r$   r   r   s    r   r!   zSampleTest.testGetSet  s    TX&&&&&r   c                 :    |                      | j                   dS )zTesting Boolean methodsN)r'   r   r   s    r   r&   zSampleTest.testBoolean  s    dh'''''r   N)	r~   r   r   vtkmodules.vtkRenderingCorer   r   r   r!   r&   r   r   r   r   r     s^        888888hjj	& 	& 	&	' 	' 	'	( 	( 	( 	( 	(r   r   r   )r   )4r   
__future__r   r   r+   r:   os.pathr   r   vtkmodules.vtkCommonCorer   r   vtkmodules.vtkCommonSystemr   vtkmodules.vtkIOImager   r   vtkmodules.vtkImagingCorer	   r
   r  r   r   r   r   r*   r   r   r2   r   r   r   r   TestCaser   r   r   ry   r   r   rz   r   r   r   r   r   r   r   r   r   r~   r   r   r   r   <module>r	     s  A AD ' & & & & &                      > > > > > > > > 2 2 2 2 2 2 < < < < < < < < L L L L L L L L > > > > > >               	
 	  
c9 c9 c9 c9 c9h c9 c9 c9JM M M  
9 9 9  N0 N0 N0 N0b   0A A A(n n n0 0 0  8! ! !H  0> > >B  23* 3* 3*n z( ( ( ( (W ( ( (  	DZ 
"$$$$$' r   