Skip to content

Commit

Permalink
straighten call from macro
Browse files Browse the repository at this point in the history
  • Loading branch information
ekatrukha committed Jun 25, 2024
1 parent 8f0f0b0 commit eb1eb6d
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 56 deletions.
70 changes: 20 additions & 50 deletions src/main/java/bigtrace/BigTrace.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,15 @@ public class BigTrace < T extends RealType< T > & NativeType< T > > implements P
/** ROI manager + list tab **/
public RoiManager3D<T> roiManager;

/** BigTrace interface panel **/
BigTraceMacro<T> btMacro;

/**macro extensions **/
private ExtensionDescriptor[] extensions = {

ExtensionDescriptor.newDescriptor("btLoadROIs", this, ARG_STRING, ARG_STRING),
ExtensionDescriptor.newDescriptor("btLoadROIs", this, ARG_STRING, ARG_STRING),
ExtensionDescriptor.newDescriptor("btStraighten", this, ARG_NUMBER, ARG_STRING),
ExtensionDescriptor.newDescriptor("btTest", this),
ExtensionDescriptor.newDescriptor("btClose", this),
ExtensionDescriptor.newDescriptor("btTest", this),

Expand All @@ -157,13 +162,17 @@ public class BigTrace < T extends RealType< T > & NativeType< T > > implements P
@Override
public void run(String arg)
{
//lock interface for initialization
bInputLock = true;

btMacro = new BigTraceMacro<>(this);
//register IJ macro extensions
if (IJ.macroRunning())
{
Functions.registerExtensions(this);
}
bInputLock = true;



//switch to FlatLaf theme
try {
Expand All @@ -176,6 +185,8 @@ public void run(String arg)

btData = new BigTraceData<>(this);
btLoad = new BigTraceLoad<>(this);


if(arg.equals(""))
{
btData.sFileNameFullImg = IJ.getFilePath("Open TIF/BDV/Bioformats file (3D, composite, time)...");
Expand Down Expand Up @@ -1531,17 +1542,19 @@ public String handleExtension(String name, Object[] args) {
{
if (name.equals("btLoadROIs"))
{
macroLoadROIs((String)args[0],(String)args[1]);
btMacro.macroLoadROIs( (String)args[0],(String)args[1]);
}
if (name.equals("btStraighten"))
{
btMacro.macroStraighten((int)Math.round(((Double)args[0]).doubleValue()),(String)args[1]);
}
if (name.equals("btClose"))
{
macroCloseBT();
IJ.log("BigTrace closed.");
btMacro.macroCloseBT();
}
if (name.equals("btTest"))
{
IJ.log("test ok right away");
macroTest();
btMacro.macroTest();
}
}
catch ( InterruptedException exc )
Expand All @@ -1550,49 +1563,6 @@ public String handleExtension(String name, Object[] args) {
}
return null;
}
void macroLoadROIs(String sFileName, String input) throws InterruptedException
{
while(bInputLock)
{
Thread.sleep(1000);
}
if(input == null)
return;
int nLoadMode =0;
switch (input)
{
case "Clean":
nLoadMode = 0;
break;
case "Append":
nLoadMode = 1;
break;
default:
IJ.log( "Error! ROIs loading mode should be either Clean or Append. Loading failed." );
return;
}
roiManager.loadROIs( sFileName, nLoadMode );
}

void macroCloseBT() throws InterruptedException
{
while(bInputLock)
{
Thread.sleep(100);
}
closeWindows();
}
void macroTest() throws InterruptedException
{
while(bInputLock)
{
IJ.log( "not unlocked" );
Thread.sleep(100);
}
IJ.log( "unlocked" );
resetViewXY();

}

@SuppressWarnings("rawtypes")
public static void main(String... args) throws Exception
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/bigtrace/BigTraceMacro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package bigtrace;

import java.util.ArrayList;

import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;

import bigtrace.rois.AbstractCurve3D;
import bigtrace.rois.Roi3D;
import bigtrace.volume.StraightenCurve;
import ij.IJ;


public class BigTraceMacro < T extends RealType< T > & NativeType< T > >
{
/** plugin instance **/
BigTrace<T> bt;

public BigTraceMacro(final BigTrace<T> bt_)
{
bt = bt_;
}

void macroLoadROIs(String sFileName, String input) throws InterruptedException
{
while(bt.bInputLock)
{
Thread.sleep(1000);
}
if(input == null)
return;
int nLoadMode = 0;
switch (input)
{
case "Clean":
nLoadMode = 0;
break;
case "Append":
nLoadMode = 1;
break;
default:
IJ.log( "Error! ROIs loading mode should be either Clean or Append. Loading failed." );
return;
}
bt.roiManager.loadROIs( sFileName, nLoadMode );
IJ.log( "BigTrace ROIs loaded from " + sFileName);
}

void macroStraighten(final int nStraightenAxis, String sSaveDir) throws InterruptedException
{
while(bt.bInputLock)
{
Thread.sleep(100);
}
//build list of ROIs
final ArrayList<AbstractCurve3D> curvesOut = new ArrayList<>();

for (int nRoi = 0; nRoi<bt.roiManager.rois.size(); nRoi++)
{
Roi3D roi = bt.roiManager.rois.get(nRoi);
if(bt.roiManager.groups.get(roi.getGroupInd()).bVisible)
{
if((roi.getType() == Roi3D.LINE_TRACE) || (roi.getType() == Roi3D.POLYLINE))
{
curvesOut.add((AbstractCurve3D) roi);
}
}
}
int nAxis = nStraightenAxis;
if(nStraightenAxis<0 || nStraightenAxis>2)
{
nAxis = 0;
IJ.log( "First axis parameter should be in the range of 0-2, wher 0 = X axis, 1 = Y axis, 2 = Z axis" );
IJ.log( "Setting the value to 0, X axis." );
}
if(curvesOut.size()>0)
{
StraightenCurve<T> straightBG = new StraightenCurve<>(curvesOut, bt, -1.0f, nAxis, 0, 1, sSaveDir);
straightBG.addPropertyChangeListener(bt.btPanel);
straightBG.execute();
}
else
{
IJ.log("Cannot find proper curve ROIs to straighten.");
bt.btPanel.progressBar.setString("curve straightening aborted.");
}
}

void macroCloseBT() throws InterruptedException
{
while(bt.bInputLock)
{
Thread.sleep(100);
}
bt.closeWindows();
IJ.log("BigTrace closed.");
}

void macroTest() throws InterruptedException
{
while(bt.bInputLock)
{
IJ.log( "not unlocked" );
Thread.sleep(100);
}
IJ.log( "unlocked" );
bt.resetViewXY();
IJ.log("test ok right away");

}

void waitUntilUnlock()
{

}
}
19 changes: 13 additions & 6 deletions src/main/java/bigtrace/volume/StraightenCurve.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class StraightenCurve < T extends RealType< T > & NativeType< T > > exten
final int nOutput;
final String sSaveFolderPath;
final Calibration cal;

public boolean bMacroMessage = false;

public StraightenCurve(final ArrayList<AbstractCurve3D> curveROIArr_, final BigTrace<T> bt_, final float fRadius_, final int nStraightenAxis_, final int nTimePoint_, final int nOutput_, final String sSaveFolderPath_)
{
Expand Down Expand Up @@ -113,7 +115,11 @@ protected Void doInBackground() throws Exception {
setProgressState("extracting ROI ("+Integer.toString(nRoi+1)+"/"+Integer.toString(nTotROIs)+") "+ sRoiName);
IntervalView<T> extractedRAI = extractCurveRAI(curveROIArr.get(nRoi), full_RAI, false);
setProgressState("storing ROI ("+Integer.toString(nRoi+1)+"/"+Integer.toString(nTotROIs)+") "+ sRoiName);
outputImagePlus(VolumeMisc.wrapImgImagePlusCal(extractedRAI, sRoiName + "_straight",cal));
if(!outputImagePlus(VolumeMisc.wrapImgImagePlusCal(extractedRAI, sRoiName + "_straight",cal)))
{
IJ.log("Error saving straighten ROIs to "+sSaveFolderPath);
break;
}
}
setProgressState("ROI straightening finished.");
setProgress(100);
Expand All @@ -123,16 +129,15 @@ protected Void doInBackground() throws Exception {
return null;
}

void outputImagePlus(ImagePlus ip)
boolean outputImagePlus(ImagePlus ip)
{
if(nOutput == 0)
{
ip.show();
return true;
}
else
{
IJ.saveAsTiff(ip, sSaveFolderPath+ip.getTitle());
}

return IJ.saveAsTiff(ip, sSaveFolderPath+ip.getTitle());
}


Expand Down Expand Up @@ -316,6 +321,8 @@ public void done()
//unlock user interaction
bt.bInputLock = false;
bt.roiManager.setLockMode(false);
if(bMacroMessage)
IJ.log( "BigTrace: done saving straightened volumes to " + sSaveFolderPath);

}
}

0 comments on commit eb1eb6d

Please sign in to comment.