Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jpiv3;
- import javax.media.jai.PlanarImage;
- import javax.media.jai.JAI;
- import java.awt.image.renderable.ParameterBlock;
- import java.awt.image.BufferedImage;
- import java.util.Set;
- import jpiv2.FileHandling;
- import jpiv2.FlexFileChooser;
- import jpiv2.PivData;
- import jpiv2.PivImg;
- import jpiv2.PivUtil;
- import jpiv2.Settings;
- /** PIV Evaluation.
- *
- */
- public class PivEvaluation extends Thread {
- private jpiv2.JPiv jpiv;
- private static jpiv3.Settings settings;
- // correlation fields
- public volatile static BufferedImage[] corr;
- // A BufferedImage is used instead of a PlanarImage to force the JAI
- // operation chain to be executed. Otherwise all operations are stored in
- // memory until the data is requested (jai pull model). This would lead to
- // higher computation speed, but also to excessive memory usage.
- private float[] pixels;
- // settings
- private int passes;
- public volatile static int p=0;
- public volatile static int[] intWinW;
- public volatile static int[] intWinH;
- private int[] searchAreaW;
- private int[] searchAreaH;
- private int[] horVecSpacing;
- private int[] verVecSpacing;
- private int horPreShift;
- private int verPreShift;
- // initial vector field
- private int x0, y0, x1, y1, nx, ny;
- public volatile static double[][] shift;
- // first derivative for window deformation
- public volatile static double[][][] deriv;
- public volatile static int[] dim;
- // file handling
- private String[] files;
- private String destFileName;
- private java.text.DecimalFormat fileNumFormat;
- private String[] filenames;
- public volatile static int shiftLength;
- public volatile static int thrdCount;
- public volatile static String[] filesInUse = new String[2];
- /**
- * Creates a new instance of PivEvaluation
- * @param jpiv The jpiv2.JPiv mother component.
- */
- public PivEvaluation() {
- this.jpiv = jpiv;
- }
- /** Overrides run() in java.lang.Thread. Never call run() directly.
- * Use jpiv2.PivEvaluation.start(), inherited from java.lang.Thread to
- * start an instance of PivEvaluation as a new thread. When the PIV
- * evaluation is finished, jpiv.getListFrame().notify() is called. Thus, you can
- * use jpiv.getListFrame().wait() to stop your thread as long as this thread
- * is busy. In this way, you can automatically start a couple of PivEvaluation
- * threads and modify some parameters in between.
- */
- @Override
- public void run() {
- filenames = JPivMain.getFilenames();
- if(filenames == null) {
- System.out.println("No files selected.");
- }
- else {
- files=filenames;
- doPivEvaluation();
- }
- }
- /** Does the actual PIV evaluation.
- * This function does the looping over the passes and the images and writes the output into files.
- * If you want to run the PIV evaluation in a separate thread, call the method start() instead.
- * @return The last used filename.
- */
- public String doPivEvaluation() {
- initVariables();
- String lastDestFileName = null;
- if(settings.pivUseImageBaseName || settings.pivUseDefaultDestFileName || chooseDestFileName()) {
- System.out.println("JPiv PIV evaluation started.");
- jpiv2.PivData theData;
- jpiv2.PivImg pivImg;
- for (p=0; p<passes; p++) {
- for(int f=0; f<files.length; f++) {
- //Most of this can be ignored, I included it for completeness' sake
- switch (settings.pivSequence) {
- case jpiv2.Settings.PIV_CONSECUTIVE:
- {
- if (f==0) f = 1;
- pivImg = new PivImg(jpiv, files[f-1], files[f]);
- System.out.println( "Processing: " +
- jpiv2.FileHandling.stripFilename(files[f-1]) +
- " and " +
- jpiv2.FileHandling.stripFilename(files[f]));
- filesInUse[0] = files[f-1];
- filesInUse[1] = files[f];
- break;
- }
- case jpiv2.Settings.PIV_SKIP:
- {
- int skip = settings.pivSkip;
- if (f==0) f = skip + 1;
- pivImg = new PivImg(jpiv, files[f-skip-1], files[f]);
- System.out.println( "Processing: " +
- jpiv2.FileHandling.stripFilename(files[f-skip-1]) +
- " and " +
- jpiv2.FileHandling.stripFilename(files[f]));
- filesInUse[0] = files[f-skip-1];
- filesInUse[1] = files[f];
- break;
- }
- case jpiv2.Settings.PIV_CASCADE:
- {
- if (f==0) f = 1;
- pivImg = new PivImg(jpiv, files[0], files[f]);
- System.out.println( "Processing: " +
- jpiv2.FileHandling.stripFilename(files[0]) +
- " and " +
- jpiv2.FileHandling.stripFilename(files[f]));
- filesInUse[0]=files[0];
- filesInUse[1]=files[f];
- break;
- }
- case jpiv2.Settings.PIV_PAIRS:
- {
- f++;
- pivImg = new PivImg(jpiv, files[f-1], files[f]);
- System.out.println( "Processing: " +
- jpiv2.FileHandling.stripFilename(files[f-1]) +
- " and " +
- jpiv2.FileHandling.stripFilename(files[f]));
- filesInUse[0]=files[f-1];
- filesInUse[1]=files[f];
- break;
- }
- default: {
- pivImg = new PivImg(jpiv, files[f]);
- System.out.println( "Processing: " +
- jpiv2.FileHandling.stripFilename(files[f]) );
- }
- }
- /*Important method call here*/
- addCorrelation(pivImg);
- /*That's where the error occurs, I think*/
- if(!settings.exportCorrFunctionOnlySumOfCorr || f==files.length-1) {
- checkExportCorrFunction( f );
- }
- if (!settings.pivSumOfCorr) {
- theData = calcDisplacements();
- p++;
- while(p<passes) {
- initCorrMap();
- System.out.println( (p+1) + " pass");
- theData = doPostprocessing(theData);
- theData.resample(x0, y0, x1, y1, horVecSpacing[p], verVecSpacing[p]);
- shift = theData.getPivData();
- if(settings.pivShearIntWindows) deriv = theData.getShearField();
- dim = theData.getDimension();
- addCorrelation(pivImg);
- if(!settings.exportCorrFunctionOnlySumOfCorr || f==files.length-1) {
- checkExportCorrFunction( f );
- }
- theData = calcDisplacements();
- p++;
- }
- lastDestFileName = destFileName +
- fileNumFormat.format(f) +
- ".jvc";
- theData.writeDataToFile(lastDestFileName,
- settings.loadSaveTecplotHeader);
- //jpiv.getListFrame().appendElement(lastDestFileName);
- // clean up for next evaluation
- p = 0;
- initCorrMap();
- initPreShift();
- }
- }
- if (!settings.pivSumOfCorr) {
- System.out.println("JPiv PIV evaluation finished.");
- return(lastDestFileName);
- }
- if (p < passes-1) {
- theData = calcDisplacements();
- System.out.println( (p+2) + " pass");
- theData = doPostprocessing(theData);
- p++;
- theData.resample(x0, y0, x1, y1, horVecSpacing[p], verVecSpacing[p]);
- shift = theData.getPivData();
- deriv = theData.getShearField();
- dim = theData.getDimension();
- initCorrMap();
- p--;
- }
- }
- p--;
- theData = calcDisplacements();
- lastDestFileName = destFileName + ".jvc";
- theData.writeDataToFile(lastDestFileName,
- settings.loadSaveTecplotHeader);
- //jpiv.getListFrame().appendElement(lastDestFileName);
- System.out.println("PIV evaluation finished.");
- }
- return(lastDestFileName);
- }
- public void addCorrelation(jpiv2.PivImg pivImg) {
- //ParameterBlock pb = new ParameterBlock();
- //PlanarImage tmpCorr;
- //int x,y;
- shiftLength = shift.length;
- jpiv3.corrThread thrd1 = new jpiv3.corrThread(1);
- jpiv3.corrThread thrd2 = new jpiv3.corrThread(2);
- jpiv3.corrThread thrd3 = new jpiv3.corrThread(3);
- jpiv3.corrThread thrd4 = new jpiv3.corrThread(4);
- thrd1.start();
- thrd2.start();
- thrd3.start();
- thrd4.start();
- while (!thrd1.stopThrd1 && !thrd2.stopThrd2 && !thrd3.stopThrd3 && !thrd4.stopThrd4){
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- System.out.println("All threads finished");
- }
- //and some other methods that I know work,
- }
- // corrThread class:
- package jpiv3;
- import javax.media.jai.JAI;
- import javax.media.jai.PlanarImage;
- import java.awt.image.renderable.ParameterBlock;
- import jpiv2.PivUtil;
- public class corrThread extends Thread{
- private int thrdNum;
- private jpiv2.JPiv jpiv;
- public boolean stopThrd1 = false;
- public boolean stopThrd2 = false;
- public boolean stopThrd3 = false;
- public boolean stopThrd4 = false;
- jpiv2.Settings settings = new jpiv2.Settings();
- private String[] filesInUse = PivEvaluation.filesInUse;
- jpiv2.PivImg pivImg = new jpiv2.PivImg(jpiv, filesInUse[0], filesInUse[1]);
- public corrThread(int thrdNum){
- this.thrdNum = thrdNum;
- }
- public void run(){
- while (!stopThrd1&&!stopThrd2&&!stopThrd3&&!stopThrd4){
- doThings();
- switch (thrdNum){
- case (1):
- stopThrd1 = true;
- break;
- case(2):
- stopThrd2 = true;
- break;
- case(3):
- stopThrd3 = true;
- break;
- case(4):
- stopThrd4 = true;
- break;
- }
- }
- System.out.println("Thread "+thrdNum+" finished");
- }
- public void doThings(){
- PlanarImage tmpCorr;
- double[][] shift = PivEvaluation.shift;
- int[] intWinW = PivEvaluation.intWinW;
- int[] intWinH = PivEvaluation.intWinH;
- int p = PivEvaluation.p;
- int x,y;
- double[][][] deriv = PivEvaluation.deriv;
- int[] dim = PivEvaluation.dim;
- ParameterBlock pb = new ParameterBlock();
- int start = (int) Math.floor(((thrdNum-1)*(PivEvaluation.shiftLength))/4);
- int end = (int) (Math.floor((thrdNum*(PivEvaluation.shiftLength))/4)-1);
- for (int c = start; c < end; c++) {
- // without window deformation
- if (!settings.pivShearIntWindows || p == 0) {
- try {
- // central difference
- //PivUtil.correlate gets the correlation function of the
- //Interrogation window defined by pivImg.getSubImage.
- //
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]+shift[c][2])/2 ),
- (float)( shift[c][1]-(intWinH[p]+shift[c][3])/2 ),
- intWinW[p], intWinH[p], 0),
- pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]-shift[c][2])/2 ),
- (float)( shift[c][1]-(intWinH[p]-shift[c][3])/2 ),
- intWinW[p], intWinH[p], 1)
- );
- } catch (java.lang.IllegalArgumentException tryBackwardDifference) {
- try {
- // backward difference
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2-shift[c][2] ),
- (float)(shift[c][1]-intWinH[p]/2-shift[c][3] ),
- intWinW[p], intWinH[p], 0),
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p], 1)
- );
- } catch (java.lang.IllegalArgumentException tryForwardDifference) {
- // forward difference
- try {
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p], 0),
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2+shift[c][2] ),
- (float)(shift[c][1]-intWinH[p]/2+shift[c][3] ),
- intWinW[p], intWinH[p], 1)
- );
- } catch (java.lang.IllegalArgumentException fuckingHellNothingHelps) {
- // no preshift, this might happen in the corners of an image, when
- // the window size is not reduced from pass to pass.
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p], 0),
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p], 1)
- );
- PivEvaluation.shift[c][2] = 0;
- PivEvaluation.shift[c][3] = 0;
- }
- }
- }
- }
- else {
- y = (int)(c/dim[0]);
- x = c-y*dim[0];
- try {
- // central difference
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]+shift[c][2])/2 ),
- (float)( shift[c][1]-(intWinH[p]+shift[c][3])/2 ),
- intWinW[p], intWinH[p],
- (float)deriv[y][x][0]/2,
- (float)deriv[y][x][1]/2,
- 0),
- pivImg.getSubImage((float)( shift[c][0]-(intWinW[p]-shift[c][2])/2 ),
- (float)( shift[c][1]-(intWinH[p]-shift[c][3])/2 ),
- intWinW[p], intWinH[p],
- (float)-deriv[y][x][0]/2,
- (float)-deriv[y][x][1]/2,
- 1)
- );
- } catch (java.lang.IllegalArgumentException tryBackwardDifference) {
- try {
- // backward difference
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2-shift[c][2] ),
- (float)(shift[c][1]-intWinH[p]/2-shift[c][3] ),
- intWinW[p], intWinH[p],
- (float)deriv[y][x][0]/2,
- (float)deriv[y][x][1]/2,
- 0),
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p],
- (float)-deriv[y][x][0]/2,
- (float)-deriv[y][x][1]/2,
- 1)
- );
- } catch (java.lang.IllegalArgumentException tryForwardDifference) {
- // forward difference
- try {
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p],
- (float)deriv[y][x][0]/2,
- (float)deriv[y][x][1]/2,
- 0),
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2+shift[c][2] ),
- (float)(shift[c][1]-intWinH[p]/2+shift[c][3] ),
- intWinW[p], intWinH[p],
- (float)-deriv[y][x][0]/2,
- (float)-deriv[y][x][1]/2,
- 1)
- );
- } catch (java.lang.IllegalArgumentException fuckingHellNothingHelps) {
- // no preshift, this might happen in the corners of an image, when
- // the window size is not reduced from pass to pass.
- tmpCorr = PivUtil.correlate(
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p],
- (float)deriv[y][x][0]/2,
- (float)deriv[y][x][1]/2,
- 0),
- pivImg.getSubImage((float)(shift[c][0]-intWinW[p]/2 ),
- (float)(shift[c][1]-intWinH[p]/2 ),
- intWinW[p], intWinH[p],
- (float)-deriv[y][x][0]/2,
- (float)-deriv[y][x][1]/2,
- 1)
- );
- shift[c][2] = 0;
- shift[c][3] = 0;
- }
- }
- }
- }
- // with window deformation (experimental!!!)
- /* I'm fairly sure it's this bit that breaks it*/
- // add correlation matrix
- pb.removeSources();
- pb.removeParameters();
- pb.addSource(PivEvaluation.corr[c]);
- pb.addSource(tmpCorr);
- PivEvaluation.corr[c] = JAI.create("add",pb,null).getAsBufferedImage();
- /*But I don't know what it does or why*/
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement