Commit 971dd284 authored by Andrey Filippov's avatar Andrey Filippov
Browse files

refactoring

parent 38475568
Loading
Loading
Loading
Loading
+117 −1
Original line number Diff line number Diff line
@@ -2058,7 +2058,7 @@ public class SuperTiles{
		return planes_selections;
	}

	public TilePlanes.PlaneData [][] createPlanesFromSelections(
	public TilePlanes.PlaneData [][] createPlanesFromSelectionsOld(
			final boolean [][][][] plane_selections, //  = new boolean [nStiles][][][]; // num_tiles
			final double  [][][][] disp_strength,			
			final double     plDispNorm,
@@ -2104,10 +2104,16 @@ public class SuperTiles{
							System.out.println("createPlanesFromSelections(): nsTile="+nsTile);
						}
						if (plane_selections[nsTile] != null) {
							
							
							
							int stileY = nsTile / stilesX;  
							int stileX = nsTile % stilesX;
							int [] sTiles = {stileX, stileY};
							int dl =  (nsTile == debug_stile) ? 3 : 0;
							
							
							
							result_planes[nsTile] = null;
							// first make a plane from all tiles
							ArrayList<TilePlanes.PlaneData> st_planes = new ArrayList<TilePlanes.PlaneData>();
@@ -2118,6 +2124,8 @@ public class SuperTiles{
									correct_distortions,
									measuredLayers,     // MeasuredLayers measuredLayers,
									plPreferDisparity);   // boolean preferDisparity)

							
							// iterate through all plane selections
							for (int ps = 0; ps < plane_selections[nsTile].length; ps++) {
								TilePlanes.PlaneData pd = pd0.clone(); 
@@ -2234,6 +2242,9 @@ public class SuperTiles{
								dbg_img =  showSupertileSeparation(true, disp_strength[nsTile], plane_selections[nsTile], result_planes[nsTile]);
								sdfa_instance.showArrays(dbg_img, 2 * superTileSize, 2* superTileSize, true, "create_planes_world-"+nsTile+"-"+debugLevel,dbg_titles);
							}
							
							
							
						}
					}
				}
@@ -2243,6 +2254,111 @@ public class SuperTiles{
		return result_planes;		
	}

	public TilePlanes.PlaneData [][] createPlanesFromSelections(
			final boolean [][][][] plane_selections, //  = new boolean [nStiles][][][]; // num_tiles
			final double  [][][][] disp_strength,			
			final double     plDispNorm,
			final int        plMinPoints, //          =     5;  // Minimal number of points for plane detection
			final double     plTargetEigen, //        =   0.1;  // Remove outliers until main axis eigenvalue (possibly scaled by plDispNorm) gets below
			final double     plFractOutliers, //      =   0.3;  // Maximal fraction of outliers to remove
			final int        plMaxOutliers, //        =    20;  // Maximal number of outliers to remove
//			final double     plVertWors,    //        =    1.5  // if rotating plane vertical does not increase 'eigenvalue' more, use vertical  
			final boolean    plPreferDisparity, // Always start with disparity-most axis (false - lowest eigenvalue)
			final GeometryCorrection geometryCorrection,
			final boolean    correct_distortions,

			final boolean    smplMode, //        = true;   // Use sample mode (false - regular tile mode)
			final int        smplSide, //        = 2;      // Sample size (side of a square)
			final int        smplNum, //         = 3;      // Number after removing worst
			final double     smplRms, //         = 0.1;    // Maximal RMS of the remaining tiles in a sample

			final int        debugLevel,
			final int        dbg_X,
			final int        dbg_Y)
	{
		final int tilesX =        tileProcessor.getTilesX();
		final int tilesY =        tileProcessor.getTilesY();
		final int superTileSize = tileProcessor.getSuperTileSize();
		final int tileSize =      tileProcessor.getTileSize();

		final int stilesX = (tilesX + superTileSize -1)/superTileSize;  
		final int stilesY = (tilesY + superTileSize -1)/superTileSize;
		final int nStiles = stilesX * stilesY; 
		final Thread[] threads = ImageDtt.newThreadArray(tileProcessor.threadsMax);
		final AtomicInteger ai = new AtomicInteger(0);
		final TilePlanes.PlaneData [][] result_planes = new TilePlanes.PlaneData[nStiles][];
		//		this.planes = new TilePlanes.PlaneData[nStiles][];
		final int debug_stile = (debugLevel > -1)? (dbg_Y * stilesX + dbg_X):-1;
		// TODO: Remove when promoting PlaneData
		final TilePlanes tpl = new TilePlanes(tileSize,superTileSize, geometryCorrection);

		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nsTile = ai.getAndIncrement(); nsTile < nStiles; nsTile = ai.getAndIncrement()) {
						if (nsTile == debug_stile){
							System.out.println("createPlanesFromSelections(): nsTile="+nsTile);
						}
						if (plane_selections[nsTile] != null) {
							
							
							
							int stileY = nsTile / stilesX;  
							int stileX = nsTile % stilesX;
							int [] sTiles = {stileX, stileY};
							int dl =  (nsTile == debug_stile) ? 3 : 0;
							
							result_planes[nsTile] = null;
							// first make a plane from all tiles
							TilePlanes.PlaneData pd0 = tpl.new  PlaneData (
									sTiles, // int [] sTileXY, 
									tileSize, // int tileSize,
									geometryCorrection, // GeometryCorrection   geometryCorrection,
									correct_distortions,
									measuredLayers,     // MeasuredLayers measuredLayers,
									plPreferDisparity);   // boolean preferDisparity)
							ArrayList<TilePlanes.PlaneData> st_planes = pd0.createTilePlanesFromSelections(
											"" + nsTile, // String        suffix,
											plane_selections[nsTile], // boolean [][][] plane_selections, //  = new boolean [nStiles][][][]; // num_tiles
											disp_strength[nsTile],    // double  [][][] disp_strength,			
											plDispNorm,               // double       dispNorm,   //  Normalize disparities to the average if above
											plMinPoints,              // int          min_tiles,
											plTargetEigen,            // double       plTargetEigen, //        =   0.1;  // Remove outliers until main axis eigenvalue (possibly scaled by plDispNorm) gets below
											plFractOutliers,          // double       plFractOutliers, //      =   0.3;  // Maximal fraction of outliers to remove
											plMaxOutliers,            // int          plMaxOutliers, //        =    20;  // Maximal number of outliers to remove
											strength_floor,           // double       strength_floor,
											strength_pow,             // double       strength_pow,
											correct_distortions,      // boolean      correct_distortions,
											smplMode,                 // boolean      smplMode, //        = true;   // Use sample mode (false - regular tile mode)
											smplSide,                 // int          smplSide, //        = 2;      // Sample size (side of a square)
											smplNum,                  //  int          smplNum, //         = 3;      // Number after removing worst
											smplRms,                  //  double       smplRms, //         = 0.1;    // Maximal RMS of the remaining tiles in a sample
											dl);                       // int          debugLevel);

							if ((st_planes != null) && (!st_planes.isEmpty())){
								if (LOWEST_PLANE(2) > 0) st_planes.add(0, st_planes.get(0)); // insert dummy at pos 0;
								result_planes[nsTile] = st_planes.toArray(new TilePlanes.PlaneData[0] );
								if (LOWEST_PLANE(2) > 0) result_planes[nsTile][0] = null; // remove dummy
								if (dl >0){
									System.out.println("createPlanesFromSelections(): nsTile="+nsTile);
								}
								if (dl > 2) {
									String [] dbg_titles = showSupertileSeparationTitles( disp_strength[nsTile], plane_selections[nsTile], result_planes[nsTile]);
									double [][] dbg_img =  showSupertileSeparation(false, disp_strength[nsTile], plane_selections[nsTile], result_planes[nsTile]);
									showDoubleFloatArrays sdfa_instance = new showDoubleFloatArrays();
									sdfa_instance.showArrays(dbg_img, 2 * superTileSize, 2* superTileSize, true, "create_planes_disp-"+nsTile+"-"+debugLevel,dbg_titles);
									dbg_img =  showSupertileSeparation(true, disp_strength[nsTile], plane_selections[nsTile], result_planes[nsTile]);
									sdfa_instance.showArrays(dbg_img, 2 * superTileSize, 2* superTileSize, true, "create_planes_world-"+nsTile+"-"+debugLevel,dbg_titles);
								}
							}
						}
					}
				}
			};
		}		      
		ImageDtt.startAndJoin(threads);
		return result_planes;		
	}
	
	public void processPlanes5(
			final int        growSelection,                     // grow initial selection before processing 
+139 −0
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@
 ** -----------------------------------------------------------------------------**
 **
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import Jama.EigenvalueDecomposition;
import Jama.Matrix;

@@ -3326,7 +3330,142 @@ public class TilePlanes {
			world_xyz = norm_xyz.times((xyz.transpose().times(norm_xyz).get(0,0))).getColumnPackedCopy();
			return world_xyz;
		}
		
		public ArrayList<TilePlanes.PlaneData> createTilePlanesFromSelections(
				String        suffix,
				boolean [][][] plane_selections, //  = new boolean [nStiles][][][]; // num_tiles
				double  [][][] disp_strength,			
				// double       disp_far, // minimal disparity to select (or NaN)
				// double       disp_near, // maximal disparity to select (or NaN)
				double       dispNorm,   //  Normalize disparities to the average if above
//				double       min_weight,
				int          min_tiles,
				double       plTargetEigen, //        =   0.1;  // Remove outliers until main axis eigenvalue (possibly scaled by plDispNorm) gets below
				double       plFractOutliers, //      =   0.3;  // Maximal fraction of outliers to remove
				int          plMaxOutliers, //        =    20;  // Maximal number of outliers to remove
				double       strength_floor,
				double       strength_pow,
				boolean      correct_distortions,
				boolean      smplMode, //        = true;   // Use sample mode (false - regular tile mode)
				int          smplSide, //        = 2;      // Sample size (side of a square)
				int          smplNum, //         = 3;      // Number after removing worst
				double       smplRms, //         = 0.1;    // Maximal RMS of the remaining tiles in a sample
				int          debugLevel)
		{
			
			// first make a plane from all tiles
			ArrayList<TilePlanes.PlaneData> st_planes = new ArrayList<TilePlanes.PlaneData>();
			
			// iterate through all plane selections
			for (int ps = 0; ps < plane_selections.length; ps++) {
				TilePlanes.PlaneData pd = this.clone(); 
				boolean OK = (pd.getPlaneFromMeas(
						plane_selections[ps], // tile_sel,       // boolean [][] tile_sel, // null - do not use, {} use all (will be modified)
						disp_strength,
						Double.NaN,    // double       disp_far, // minimal disparity to select (or NaN)
						Double.NaN,    // double       disp_near, // maximal disparity to select (or NaN)
						dispNorm,    // 0.0,            // plDispNorm,  // double       dispNorm,   //  Normalize disparities to the average if above
						0.0,            // double       min_weight,
						min_tiles,    // int          min_tiles,
						
						strength_floor, // 
						strength_pow,   // double       strength_pow,
						
						// update !
						smplMode,
						smplSide,
						smplNum,
						smplRms,
						debugLevel) != null);            // int          debugLevel)
				if (OK) {
					if (debugLevel > 0) {
						if (pd.getWeight() > 1.0) {
							System.out.println("Processing subplane "+ suffix+
									", numPoints="+ pd.getNumPoints()+
									", swc = "+pd.getWeight()+
									", center=["+pd.getZxy()[0]+","+pd.getZxy()[1]+","+pd.getZxy()[2]+"]"+
									", eig_val = {"+pd.getValues()[0]+","+pd.getValues()[1]+","+pd.getValues()[2]+"}"+
									", eig_vect[0] = {"+pd.getVector()[0]+","+pd.getVector()[1]+","+pd.getVector()[2]+"}");
						}
					}
					// now try to remove outliers
					int max_outliers = (int) Math.round(pd.getNumPoints() * plFractOutliers);
					if (max_outliers > plMaxOutliers) max_outliers = plMaxOutliers;
					double targetV = plTargetEigen;
					double z0 = pd.getZxy()[0];
					if ((dispNorm > 0.0) && (z0 > dispNorm)) {
						double dd = (dispNorm + z0)/ dispNorm; // > 1
						targetV *= dd * dd; // > original
					}
					if (pd.getValues()[0] > targetV) {
						OK = pd.removeOutliers( // getPlaneFromMeas should already have run
								disp_strength, 
								targetV,      // double     targetEigen, // target eigenvalue for primary axis (is disparity-dependent, so is non-constant)
								max_outliers, // int        maxRemoved,  // maximal number of tiles to remove (not a constant)
								debugLevel); // int        debugLevel)
						if (!OK) {
							continue;
						}
						if (debugLevel > 0) {
							if (pd.getWeight() > 1.0) {
								System.out.println("Removed outliers "+ suffix +
										", numPoints="+ pd.getNumPoints()+
										", swc = "+pd.getWeight()+
										", center=["+pd.getZxy()[0]+","+pd.getZxy()[1]+","+pd.getZxy()[2]+"]"+
										", eig_val = {"+pd.getValues()[0]+","+pd.getValues()[1]+","+pd.getValues()[2]+"}"+
										", eig_vect[0] = {"+pd.getVector()[0]+","+pd.getVector()[1]+","+pd.getVector()[2]+"}");
							}
						}
					}
					double [] norm_xyz = pd.getWorldXYZ(
							correct_distortions);
					st_planes.add(pd);
					if (debugLevel > 0) {
						System.out.println("World normal " + suffix + " = {"+
								norm_xyz[0]+", "+norm_xyz[1]+", "+norm_xyz[2]+"}");

					}
					// calculate the world planes too									
//					if (debugLevel > -1){
						pd.getWorldPlaneFromMeas(
								plane_selections[ps], // tile_sel,       // boolean [][] tile_sel, // null - do not use, {} use all (will be modified)
								disp_strength,
								Double.NaN,    // double       disp_far, // minimal disparity to select (or NaN)
								Double.NaN,    // double       disp_near, // maximal disparity to select (or NaN)
								dispNorm,      // 0.0,            // plDispNorm,  // double       dispNorm,   //  Normalize disparities to the average if above
								0.0,            // double       min_weight,
								min_tiles,      // int          min_tiles,
								strength_floor, // 
								strength_pow,   // double       strength_pow,
								// update !
								smplMode,
								smplSide,
								smplNum,
								smplRms,
								debugLevel);
				}
			}
			if (st_planes.size() > 0){
				// sort planes by increasing disparity (tile center or plane center ? ) Using plane center
				Collections.sort(st_planes, new Comparator<TilePlanes.PlaneData>() {
					@Override
					public int compare(TilePlanes.PlaneData lhs, TilePlanes.PlaneData rhs) {
						// -1 - less than, 1 - greater than, 0 - equal
						return (rhs.getZxy()[0] > lhs.getZxy()[0]) ? -1 : (rhs.getZxy()[0] < lhs.getZxy()[0] ) ? 1 : 0;
					}
				});
				return st_planes;
			}
			return null;		
		}
		
		
		
		
	}
	
	
	
	
	//TODO: Remove below methods and promote PlaneData (no TilePlanes) after tested