Advertisement
Guest User

JPiv JAI threading problem

a guest
Jul 9th, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.83 KB | None | 0 0
  1. package jpiv3;
  2.  
  3. import javax.media.jai.PlanarImage;
  4. import javax.media.jai.JAI;
  5. import java.awt.image.renderable.ParameterBlock;
  6. import java.awt.image.BufferedImage;
  7. import java.util.Set;
  8.  
  9. import jpiv2.FileHandling;
  10. import jpiv2.FlexFileChooser;
  11. import jpiv2.PivData;
  12. import jpiv2.PivImg;
  13. import jpiv2.PivUtil;
  14. import jpiv2.Settings;
  15.  
  16. /** PIV Evaluation.
  17.  *
  18.  */
  19. public class PivEvaluation extends Thread {
  20.    
  21.     private jpiv2.JPiv jpiv;
  22.     private static jpiv3.Settings settings;
  23.     // correlation fields
  24.     public volatile static BufferedImage[] corr;
  25.     // A BufferedImage is used instead of a PlanarImage to force the JAI
  26.     // operation chain to be executed. Otherwise all operations are stored in
  27.     // memory until the data is requested (jai pull model). This would lead to
  28.     // higher computation speed, but also to excessive memory usage.
  29.     private float[] pixels;
  30.     // settings
  31.     private int passes;
  32.     public volatile static int p=0;
  33.     public volatile static int[] intWinW;
  34.     public volatile static int[] intWinH;
  35.     private int[] searchAreaW;
  36.     private int[] searchAreaH;
  37.     private int[] horVecSpacing;
  38.     private int[] verVecSpacing;
  39.     private int horPreShift;
  40.     private int verPreShift;
  41.     // initial vector field
  42.     private int x0, y0, x1, y1, nx, ny;
  43.     public volatile static double[][] shift;
  44.     // first derivative for window deformation
  45.     public volatile static double[][][] deriv;
  46.     public volatile static int[] dim;
  47.     // file handling
  48.     private String[] files;
  49.     private String destFileName;
  50.     private java.text.DecimalFormat fileNumFormat;
  51.     private String[] filenames;
  52.     public volatile static int shiftLength;
  53.     public volatile static int thrdCount;
  54.     public volatile static String[] filesInUse = new String[2];
  55.  
  56.    
  57.     /**
  58.      * Creates a new instance of PivEvaluation
  59.      * @param jpiv The jpiv2.JPiv mother component.
  60.      */
  61.     public PivEvaluation() {
  62.         this.jpiv = jpiv;
  63.     }
  64.  
  65.     /** Overrides run() in java.lang.Thread. Never call run() directly.
  66.      * Use jpiv2.PivEvaluation.start(), inherited from java.lang.Thread to  
  67.      * start an instance of PivEvaluation as a new thread. When the PIV
  68.      * evaluation is finished, jpiv.getListFrame().notify() is called. Thus, you can
  69.      * use jpiv.getListFrame().wait() to stop your thread as long as this thread
  70.      * is busy. In this way, you can automatically start a couple of PivEvaluation
  71.      * threads and modify some parameters in between.
  72.      */
  73.     @Override
  74.     public void run() {
  75.         filenames = JPivMain.getFilenames();
  76.         if(filenames == null) {
  77.             System.out.println("No files selected.");
  78.         }
  79.         else {
  80.             files=filenames;
  81.             doPivEvaluation();
  82.         }
  83.      }
  84.    
  85.    
  86.     /** Does the actual PIV evaluation.
  87.      * This function does the looping over the passes and the images and writes the output into files.
  88.      * If you want to run the PIV evaluation in a separate thread, call the method start() instead.
  89.      * @return The last used filename.
  90.      */
  91.     public String doPivEvaluation() {
  92.         initVariables();
  93.         String lastDestFileName = null;
  94.         if(settings.pivUseImageBaseName || settings.pivUseDefaultDestFileName || chooseDestFileName()) {
  95.             System.out.println("JPiv PIV evaluation started.");
  96.             jpiv2.PivData theData;
  97.             jpiv2.PivImg pivImg;
  98.             for (p=0; p<passes; p++) {
  99.                 for(int f=0; f<files.length; f++) {
  100.  
  101.             //Most of this can be ignored, I included it for completeness' sake
  102.  
  103.                     switch (settings.pivSequence) {
  104.                         case jpiv2.Settings.PIV_CONSECUTIVE:
  105.                         {
  106.                             if (f==0) f = 1;
  107.                             pivImg = new PivImg(jpiv, files[f-1], files[f]);
  108.                             System.out.println( "Processing: " +
  109.                                 jpiv2.FileHandling.stripFilename(files[f-1]) +
  110.                                 " and " +
  111.                                 jpiv2.FileHandling.stripFilename(files[f]));
  112.                             filesInUse[0] = files[f-1];
  113.                             filesInUse[1] = files[f];
  114.                             break;
  115.                         }
  116.                         case jpiv2.Settings.PIV_SKIP:
  117.                         {
  118.                             int skip = settings.pivSkip;
  119.                             if (f==0) f = skip + 1;
  120.                             pivImg = new PivImg(jpiv, files[f-skip-1], files[f]);
  121.                             System.out.println( "Processing: " +
  122.                                 jpiv2.FileHandling.stripFilename(files[f-skip-1]) +
  123.                                 " and " +
  124.                                 jpiv2.FileHandling.stripFilename(files[f]));
  125.                             filesInUse[0] = files[f-skip-1];
  126.                             filesInUse[1] = files[f];
  127.                             break;
  128.                         }
  129.                         case jpiv2.Settings.PIV_CASCADE:
  130.                         {
  131.                             if (f==0) f = 1;
  132.                             pivImg = new PivImg(jpiv, files[0], files[f]);
  133.                             System.out.println( "Processing: " +
  134.                                 jpiv2.FileHandling.stripFilename(files[0]) +
  135.                                 " and " +
  136.                                 jpiv2.FileHandling.stripFilename(files[f]));
  137.                             filesInUse[0]=files[0];
  138.                             filesInUse[1]=files[f];
  139.                             break;
  140.                         }
  141.                         case jpiv2.Settings.PIV_PAIRS:
  142.                         {
  143.                             f++;
  144.                             pivImg = new PivImg(jpiv, files[f-1], files[f]);
  145.                             System.out.println( "Processing: " +
  146.                                 jpiv2.FileHandling.stripFilename(files[f-1]) +
  147.                                 " and " +
  148.                                 jpiv2.FileHandling.stripFilename(files[f]));
  149.                             filesInUse[0]=files[f-1];
  150.                             filesInUse[1]=files[f];
  151.                             break;
  152.                         }
  153.                         default: {
  154.                             pivImg = new PivImg(jpiv, files[f]);
  155.                             System.out.println( "Processing: " +
  156.                                 jpiv2.FileHandling.stripFilename(files[f]) );
  157.                         }
  158.                     }
  159.  
  160.  
  161.             /*Important method call here*/
  162.                     addCorrelation(pivImg);
  163.             /*That's where the error occurs, I think*/
  164.  
  165.  
  166.  
  167.                     if(!settings.exportCorrFunctionOnlySumOfCorr || f==files.length-1) {
  168.                         checkExportCorrFunction( f );
  169.                     }
  170.                     if (!settings.pivSumOfCorr) {
  171.                         theData = calcDisplacements();
  172.                         p++;
  173.                         while(p<passes) {
  174.                             initCorrMap();
  175.                             System.out.println( (p+1) + " pass");
  176.                             theData = doPostprocessing(theData);
  177.                             theData.resample(x0, y0, x1, y1, horVecSpacing[p], verVecSpacing[p]);
  178.                             shift = theData.getPivData();
  179.                             if(settings.pivShearIntWindows) deriv = theData.getShearField();
  180.                             dim = theData.getDimension();
  181.                             addCorrelation(pivImg);
  182.                             if(!settings.exportCorrFunctionOnlySumOfCorr || f==files.length-1) {
  183.                                 checkExportCorrFunction( f );
  184.                             }
  185.                             theData = calcDisplacements();
  186.                             p++;
  187.                         }
  188.                         lastDestFileName = destFileName +
  189.                                            fileNumFormat.format(f) +
  190.                                            ".jvc";
  191.                         theData.writeDataToFile(lastDestFileName,
  192.                                                 settings.loadSaveTecplotHeader);
  193.                         //jpiv.getListFrame().appendElement(lastDestFileName);
  194.                         // clean up for next evaluation
  195.                         p = 0;
  196.                         initCorrMap();
  197.                         initPreShift();
  198.                     }
  199.                 }
  200.                 if (!settings.pivSumOfCorr) {
  201.                     System.out.println("JPiv PIV evaluation finished.");
  202.                     return(lastDestFileName);
  203.                 }
  204.                 if (p < passes-1) {      
  205.                     theData = calcDisplacements();
  206.                     System.out.println( (p+2) + " pass");
  207.                     theData = doPostprocessing(theData);
  208.                     p++;
  209.                     theData.resample(x0, y0, x1, y1, horVecSpacing[p], verVecSpacing[p]);
  210.                     shift = theData.getPivData();
  211.                     deriv = theData.getShearField();
  212.                     dim = theData.getDimension();
  213.                     initCorrMap();
  214.                     p--;
  215.                 }
  216.             }
  217.             p--;
  218.             theData = calcDisplacements();
  219.             lastDestFileName = destFileName + ".jvc";
  220.             theData.writeDataToFile(lastDestFileName,
  221.                                     settings.loadSaveTecplotHeader);
  222.             //jpiv.getListFrame().appendElement(lastDestFileName);
  223.             System.out.println("PIV evaluation finished.");
  224.         }
  225.         return(lastDestFileName);
  226.     }
  227.    
  228.     public void addCorrelation(jpiv2.PivImg pivImg) {
  229.         //ParameterBlock pb = new ParameterBlock();
  230.         //PlanarImage tmpCorr;
  231.         //int x,y;
  232.         shiftLength = shift.length;
  233.         jpiv3.corrThread thrd1 = new jpiv3.corrThread(1);
  234.         jpiv3.corrThread thrd2 = new jpiv3.corrThread(2);
  235.         jpiv3.corrThread thrd3 = new jpiv3.corrThread(3);
  236.         jpiv3.corrThread thrd4 = new jpiv3.corrThread(4);
  237.         thrd1.start();
  238.         thrd2.start();
  239.         thrd3.start();
  240.         thrd4.start();
  241.         while (!thrd1.stopThrd1 && !thrd2.stopThrd2 && !thrd3.stopThrd3 && !thrd4.stopThrd4){
  242.             try {
  243.                 Thread.sleep(10);
  244.             } catch (InterruptedException e) {
  245.                 // TODO Auto-generated catch block
  246.                 e.printStackTrace();
  247.             }
  248.         }
  249.         System.out.println("All threads finished");
  250.     }
  251.  
  252. //and some other methods that I know work,
  253. }
  254.  
  255.  
  256. // corrThread class:
  257.  
  258.  
  259. package jpiv3;
  260.  
  261. import javax.media.jai.JAI;
  262. import javax.media.jai.PlanarImage;
  263. import java.awt.image.renderable.ParameterBlock;
  264. import jpiv2.PivUtil;
  265.  
  266. public class corrThread extends Thread{
  267.    
  268.     private int thrdNum;
  269.     private jpiv2.JPiv jpiv;
  270.     public boolean stopThrd1 = false;
  271.     public boolean stopThrd2 = false;
  272.     public boolean stopThrd3 = false;
  273.     public boolean stopThrd4 = false;
  274.     jpiv2.Settings settings = new jpiv2.Settings();
  275.     private String[] filesInUse = PivEvaluation.filesInUse;
  276.     jpiv2.PivImg pivImg = new jpiv2.PivImg(jpiv, filesInUse[0], filesInUse[1]);
  277.    
  278.     public corrThread(int thrdNum){
  279.         this.thrdNum = thrdNum;
  280.     }
  281.    
  282.     public void run(){
  283.         while (!stopThrd1&&!stopThrd2&&!stopThrd3&&!stopThrd4){
  284.             doThings();
  285.             switch (thrdNum){
  286.                 case (1):
  287.                     stopThrd1 = true;
  288.                     break;
  289.                 case(2):
  290.                     stopThrd2 = true;
  291.                     break;
  292.                 case(3):
  293.                     stopThrd3 = true;
  294.                     break;
  295.                 case(4):
  296.                     stopThrd4 = true;
  297.                     break;
  298.             }
  299.            
  300.         }
  301.         System.out.println("Thread "+thrdNum+" finished");
  302.     }
  303.    
  304.     public void doThings(){
  305.         PlanarImage tmpCorr;
  306.         double[][] shift = PivEvaluation.shift;
  307.         int[] intWinW = PivEvaluation.intWinW;
  308.         int[] intWinH = PivEvaluation.intWinH;
  309.         int p = PivEvaluation.p;
  310.         int x,y;
  311.         double[][][] deriv = PivEvaluation.deriv;
  312.         int[] dim = PivEvaluation.dim;
  313.         ParameterBlock pb = new ParameterBlock();
  314.         int start =  (int) Math.floor(((thrdNum-1)*(PivEvaluation.shiftLength))/4);
  315.         int end = (int) (Math.floor((thrdNum*(PivEvaluation.shiftLength))/4)-1);
  316.         for (int c = start; c < end; c++) {
  317.             // without window deformation
  318.             if (!settings.pivShearIntWindows || p == 0) {
  319.                 try {
  320.                     // central difference
  321.  
  322.             //PivUtil.correlate gets the correlation function of the
  323.             //Interrogation window defined by pivImg.getSubImage.
  324.             //
  325.                     tmpCorr = PivUtil.correlate(
  326.                         pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]+shift[c][2])/2 ),
  327.                                            (float)( shift[c][1]-(intWinH[p]+shift[c][3])/2 ),
  328.                                            intWinW[p], intWinH[p], 0),
  329.                         pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]-shift[c][2])/2 ),
  330.                                            (float)( shift[c][1]-(intWinH[p]-shift[c][3])/2 ),
  331.                                            intWinW[p], intWinH[p], 1)
  332.                     );
  333.                 } catch (java.lang.IllegalArgumentException tryBackwardDifference) {
  334.                     try {
  335.                         // backward difference
  336.                         tmpCorr = PivUtil.correlate(
  337.                             pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2-shift[c][2] ),
  338.                                                (float)(shift[c][1]-intWinH[p]/2-shift[c][3] ),
  339.                                                intWinW[p], intWinH[p], 0),
  340.                             pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  341.                                                (float)(shift[c][1]-intWinH[p]/2 ),
  342.                                               intWinW[p], intWinH[p], 1)
  343.                         );
  344.                     } catch (java.lang.IllegalArgumentException tryForwardDifference) {
  345.                         // forward difference
  346.                         try {
  347.                             tmpCorr = PivUtil.correlate(
  348.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  349.                                                    (float)(shift[c][1]-intWinH[p]/2 ),
  350.                                                    intWinW[p], intWinH[p], 0),
  351.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2+shift[c][2] ),
  352.                                                    (float)(shift[c][1]-intWinH[p]/2+shift[c][3] ),
  353.                                                   intWinW[p], intWinH[p], 1)
  354.                             );
  355.                         } catch (java.lang.IllegalArgumentException fuckingHellNothingHelps) {
  356.                             // no preshift, this might happen in the corners of an image, when
  357.                             // the window size is not reduced from pass to pass.
  358.                             tmpCorr = PivUtil.correlate(
  359.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  360.                                                    (float)(shift[c][1]-intWinH[p]/2 ),
  361.                                                    intWinW[p], intWinH[p], 0),
  362.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  363.                                                    (float)(shift[c][1]-intWinH[p]/2 ),
  364.                                                   intWinW[p], intWinH[p], 1)
  365.                             );
  366.                             PivEvaluation.shift[c][2] = 0;
  367.                             PivEvaluation.shift[c][3] = 0;
  368.                         }
  369.                     }
  370.                 }
  371.             }
  372.              else {
  373.                     y = (int)(c/dim[0]);
  374.                     x = c-y*dim[0];
  375.                     try {
  376.                         // central difference
  377.                         tmpCorr = PivUtil.correlate(
  378.                             pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]+shift[c][2])/2 ),
  379.                                                (float)( shift[c][1]-(intWinH[p]+shift[c][3])/2 ),
  380.                                                intWinW[p], intWinH[p],
  381.                                                (float)deriv[y][x][0]/2,
  382.                                                (float)deriv[y][x][1]/2,
  383.                                                0),
  384.                             pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]-shift[c][2])/2 ),
  385.                                                (float)( shift[c][1]-(intWinH[p]-shift[c][3])/2 ),
  386.                                                intWinW[p], intWinH[p],
  387.                                                (float)-deriv[y][x][0]/2,
  388.                                                (float)-deriv[y][x][1]/2,
  389.                                                1)
  390.                         );
  391.                     } catch (java.lang.IllegalArgumentException tryBackwardDifference) {
  392.                         try {
  393.                             // backward difference
  394.                             tmpCorr = PivUtil.correlate(
  395.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2-shift[c][2] ),
  396.                                                    (float)(shift[c][1]-intWinH[p]/2-shift[c][3] ),
  397.                                                    intWinW[p], intWinH[p],
  398.                                                    (float)deriv[y][x][0]/2,
  399.                                                    (float)deriv[y][x][1]/2,
  400.                                                    0),
  401.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  402.                                                    (float)(shift[c][1]-intWinH[p]/2 ),
  403.                                                    intWinW[p], intWinH[p],
  404.                                                    (float)-deriv[y][x][0]/2,
  405.                                                    (float)-deriv[y][x][1]/2,
  406.                                                    1)
  407.                             );
  408.                         } catch (java.lang.IllegalArgumentException tryForwardDifference) {
  409.                             // forward difference
  410.                             try {
  411.                                 tmpCorr = PivUtil.correlate(
  412.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  413.                                                    (float)(shift[c][1]-intWinH[p]/2 ),
  414.                                                    intWinW[p], intWinH[p],
  415.                                                    (float)deriv[y][x][0]/2,
  416.                                                    (float)deriv[y][x][1]/2,
  417.                                                    0),
  418.                                 pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2+shift[c][2] ),
  419.                                                    (float)(shift[c][1]-intWinH[p]/2+shift[c][3] ),
  420.                                                    intWinW[p], intWinH[p],
  421.                                                    (float)-deriv[y][x][0]/2,
  422.                                                    (float)-deriv[y][x][1]/2,
  423.                                                    1)
  424.                                 );
  425.                             } catch (java.lang.IllegalArgumentException fuckingHellNothingHelps) {
  426.                                 // no preshift, this might happen in the corners of an image, when
  427.                                 // the window size is not reduced from pass to pass.
  428.                                 tmpCorr = PivUtil.correlate(
  429.                                     pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  430.                                                        (float)(shift[c][1]-intWinH[p]/2 ),
  431.                                                        intWinW[p], intWinH[p],
  432.                                                        (float)deriv[y][x][0]/2,
  433.                                                        (float)deriv[y][x][1]/2,
  434.                                                        0),
  435.                                     pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
  436.                                                        (float)(shift[c][1]-intWinH[p]/2 ),
  437.                                                        intWinW[p], intWinH[p],
  438.                                                        (float)-deriv[y][x][0]/2,
  439.                                                        (float)-deriv[y][x][1]/2,
  440.                                                        1)
  441.                                 );
  442.                                 shift[c][2] = 0;
  443.                                 shift[c][3] = 0;
  444.                             }
  445.                         }
  446.                     }
  447.                 }
  448.             // with window deformation (experimental!!!)
  449.  
  450.            
  451.                    /* I'm fairly sure it's this bit that breaks it*/
  452.             // add correlation matrix
  453.             pb.removeSources();
  454.             pb.removeParameters();
  455.             pb.addSource(PivEvaluation.corr[c]);
  456.             pb.addSource(tmpCorr);
  457.             PivEvaluation.corr[c] = JAI.create("add",pb,null).getAsBufferedImage();
  458.         /*But I don't know what it does or why*/
  459.  
  460.         }
  461.        
  462.     }
  463.  
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement