Conflicts.java 21.2 KB
Newer Older
1
package com.elphel.imagej.tileprocessor;
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 **
 ** Conflicts - Represent "conflicts" (instances of Conflict) between connected supertiles
 **
 ** Copyright (C) 2017 Elphel, Inc.
 **
 ** -----------------------------------------------------------------------------**
 **  
 **  Conflicts.java is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** -----------------------------------------------------------------------------**
 **
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class Conflicts {
	private int [] num_ortho_diag_ortho = new int [8];
	private int [] num_ortho_ortho_diag = new int [16];
	private  int [] num_all_conflicts =        new int [24];
	private  int    num_ortho_incompat = 0;
	private  int    num_ortho_dual = 0;
36
	private  int    num_conflicts = 0;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	private  SuperTiles st = null;
	
	public Conflicts(
			SuperTiles st){
		this.st = st;
	}
	
	public Conflicts(
			int [][][] conflicts,
			SuperTiles st,
			int        debugLevel)
	{
		this.st = st;
		addConflicts( conflicts, debugLevel);
	}
	public Conflicts(
			int [][][] conflicts,
			SuperTiles st)
	{
		this.st = st;
		addConflicts( conflicts, -1);
	}
	
	public Conflicts(
			Conflict [][] conflicts,
			SuperTiles st,
			int        debugLevel)
	{
		this.st = st;
		addConflicts( conflicts, debugLevel);
	}
	public Conflicts(
			Conflict [][] conflicts,
			SuperTiles st)
	{
		this.st = st;
		addConflicts( conflicts, -1);
	}
	public void addConflicts(Conflicts conflicts){
		addsubConflicts(conflicts, false);
	}
	public void subConflicts(Conflicts conflicts){
		addsubConflicts(conflicts, true);
	}
	
	public void addsubConflicts(Conflicts conflicts, boolean sub)
	{
		int s = sub? -1: 1;
		for (int i = 0; i < num_ortho_diag_ortho.length; i++) num_ortho_diag_ortho[i] += s * conflicts.num_ortho_diag_ortho[i];  
		for (int i = 0; i < num_ortho_ortho_diag.length; i++) num_ortho_ortho_diag[i] += s * conflicts.num_ortho_ortho_diag[i];  
		for (int i = 0; i < num_all_conflicts.length; i++)    num_all_conflicts[i] += s * conflicts.num_all_conflicts[i];  
		num_ortho_incompat += s * conflicts.num_ortho_incompat;
		num_ortho_dual     += s * conflicts.num_ortho_dual;
		num_conflicts      += s * conflicts.num_conflicts;
		
	}
93 94 95 96 97 98 99 100 101 102 103 104

	public void resetConflicts()
	{
		num_ortho_diag_ortho = new int [8];
		num_ortho_ortho_diag = new int [16];
		num_all_conflicts =    new int [24];
		num_ortho_incompat =   0;
		num_ortho_dual =       0;
		num_conflicts =        0;
	}
	
	
105 106 107 108 109 110 111
	
	public int addConflicts(
			int [][][] conflicts,
			int        debugLevel)
	{
		for (int nsTile = 0; nsTile < conflicts.length; nsTile++) if (conflicts[nsTile] != null){
			for (int nc = 0; nc < conflicts[nsTile].length; nc++){
112
				Conflict conf = new Conflict(nsTile, conflicts[nsTile][nc]);
113 114 115 116 117 118 119
				if (conf.getNumOrthoDiagOrthoConflicts() > 0)  num_ortho_diag_ortho[conf.getNumOrthoDiagOrthoConflicts() - 1]++;
				if (conf.getNumOrthoOrthoDiagConflicts() > 0)  num_ortho_ortho_diag[conf.getNumOrthoOrthoDiagConflicts() - 1]++;
				if (conf.getNumConflicts() > 0)                num_all_conflicts[conf.getNumConflicts() - 1]++;
				num_ortho_incompat += conf.getIncompatibleOrthoDiagOrthoConflicts();
				num_ortho_dual += conf.getDualTriOrthoDiagOrthoConflicts();
				num_conflicts += conf.getNumConflicts();
				if (debugLevel > 0){
Andrey Filippov's avatar
Andrey Filippov committed
120 121 122 123 124 125
					int tilesX =        st.tileProcessor.getTilesX();
					int superTileSize = st.tileProcessor.getSuperTileSize();
					int stilesX = (tilesX + superTileSize -1)/superTileSize;  
					int ty = nsTile / stilesX;
					int tx = nsTile % stilesX;
					printConflict("addConflicts() nsTile = "+nsTile+" ["+tx+":"+ty+"] ", conf);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
				}
			}
		}
		return num_conflicts;
	}

	public int addConflicts(
			Conflict [][] conflicts,
			int           debugLevel)
	{
		for (int nsTile = 0; nsTile < conflicts.length; nsTile++) if (conflicts[nsTile] != null){
			for (int nc = 0; nc < conflicts[nsTile].length; nc++){
				Conflict conf = conflicts[nsTile][nc];
				if (conf.getNumOrthoDiagOrthoConflicts() > 0)  num_ortho_diag_ortho[conf.getNumOrthoDiagOrthoConflicts() - 1]++;
				if (conf.getNumOrthoOrthoDiagConflicts() > 0)  num_ortho_ortho_diag[conf.getNumOrthoOrthoDiagConflicts() - 1]++;
				if (conf.getNumConflicts() > 0)                num_all_conflicts[conf.getNumConflicts() - 1]++;
				num_ortho_incompat += conf.getIncompatibleOrthoDiagOrthoConflicts();
				num_ortho_dual += conf.getDualTriOrthoDiagOrthoConflicts();
				num_conflicts += conf.getNumConflicts();
				if (debugLevel > 0){
Andrey Filippov's avatar
Andrey Filippov committed
146 147 148 149 150 151
					int tilesX =        st.tileProcessor.getTilesX();
					int superTileSize = st.tileProcessor.getSuperTileSize();
					int stilesX = (tilesX + superTileSize -1)/superTileSize;  
					int ty = nsTile / stilesX;
					int tx = nsTile % stilesX;
					printConflict("addConflicts() nsTile = "+nsTile+" ["+tx+":"+ty+"] ", conf);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
				}
			}
		}
		return num_conflicts;
	}
	
	public int [] getNumOrthoDiagOrtho(){
		return num_ortho_diag_ortho;
	}
	public int [] getNumOrthoOrthoDiag(){
		return num_ortho_ortho_diag;
	}
	public int [] getNumAllConflicts(){
		return num_all_conflicts;
	}
	public int getNumOrthoIncompat(){
		return num_ortho_incompat;
	}
	public int getNumOrthoDual(){
		return num_ortho_dual;
	}
	public int getNumConflicts(){
		return num_conflicts;
	}
	
	public void printConflict(String prefix, Conflict conf)
	{
179
		System.out.println(prefix+conf.toString());
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	}
	
	public int numBetterWorse(
			boolean better,
			boolean use_all,
			boolean use_odo,
			boolean use_ood)
	{
		int num = 0;
		if (use_all) {
			for (int i = 0; i < num_all_conflicts.length; i++){
				if (better?(num_all_conflicts[i] < 0):(num_all_conflicts[i] > 0)) num++;
			}
		}
		if (use_odo) {
			for (int i = 0; i < num_ortho_diag_ortho.length; i++){
				if (better?(num_ortho_diag_ortho[i] < 0):(num_ortho_diag_ortho[i] > 0)) num++;
			}
			if (better? (num_ortho_incompat < 0) : (num_ortho_incompat > 0)) num++;
			if (better? (num_ortho_dual < 0) : (num_ortho_dual > 0)) num++;

		}
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		if (use_ood) {
			for (int i = 0; i < num_ortho_ortho_diag.length; i++){
				if (better?(num_ortho_ortho_diag[i] < 0):(num_ortho_ortho_diag[i] > 0)) num++;
			}
		}
		return num;
	}
	public int sumConflicts(
			boolean use_all,
			boolean use_odo,
			boolean use_ood)
	{
		int num = 0;
		if (use_all) {
			for (int i = 0; i < num_all_conflicts.length; i++){
				num += num_all_conflicts[i];
			}
		}
		if (use_odo) {
			for (int i = 0; i < num_ortho_diag_ortho.length; i++){
				num +=num_ortho_diag_ortho[i];
			}
		}
		if (use_ood) {
			for (int i = 0; i < num_ortho_ortho_diag.length; i++){
				num +=num_ortho_ortho_diag[i];
			}
		}
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
		return num;
	}
	
	
	public void printConflictSummary(
			String prefix,
			boolean use_all,
			boolean use_odo,
			boolean use_ood)
	{
		System.out.print(prefix);
		if (use_all) {
			for (int i = 0; i < num_all_conflicts.length; i++){
				if (num_all_conflicts[i] != 0) System.out.print(" all_"+(i + 1)+": "+num_all_conflicts[i]);
			}
		}

		if (use_odo) {
			for (int i = 0; i < num_ortho_diag_ortho.length; i++){
				if (num_ortho_diag_ortho[i] != 0) System.out.print(" odo_"+(i + 1)+": "+num_ortho_diag_ortho[i]);
			}
			if (num_ortho_incompat != 0) {
				System.out.print(" number of incompatible odo triangles = " + num_ortho_incompat);
			}
			if (num_ortho_dual != 0) {
				System.out.print(" number of dual odo triangles = " + num_ortho_dual);
			}
		}
		if (use_ood) {
			for (int i = 0; i < num_ortho_ortho_diag.length; i++){
				if (num_ortho_ortho_diag[i] != 0) System.out.print(" ood_"+(i + 1)+": "+num_ortho_ortho_diag[i]);
			}
		}		
		System.out.println();		
	}
//	TilePlanes.PlaneData [][] planes =  null;

	/**
	 * Find "triangular" conflicts after running selectNeighborPlanesMutual.
	 * Such conflicts happen is when starting N (or other ortho direction) from some node,
	 * then turning 135 degrees right, and then 135 right again it will get to the different
	 * layer than started (and all 3 connections exist. 
	 * @param debugLevel
	 * @return 3-d array, first index - tile number, 2-nd - tile conflict number. Each conflict
	 * is stored as {start layer, end layer, bitmask of start directions} (+1 - N, +2 - E, +4 - S, +8 - W)
	 */

	
	public int [][][] detectTriangularConflicts(
			final int debugLevel)
	{
		final int tilesX =        st.getTileProcessor().getTilesX();
		final int tilesY =        st.getTileProcessor().getTilesY();
		final int superTileSize = st.getTileProcessor().getSuperTileSize();
		final int stilesX =       (tilesX + superTileSize -1)/superTileSize;  
		final int stilesY =       (tilesY + superTileSize -1)/superTileSize;
		final int nStiles =       stilesX * stilesY;
		final int [][][] conflicts = new int [st.getPlanes().length][][];
288
		final TileNeibs tnSurface = new TileNeibs(stilesX, stilesY);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		final Thread[] threads = ImageDtt.newThreadArray(st.getTileProcessor().threadsMax);
		final AtomicInteger ai = new AtomicInteger(0);
		for (int ithread = 0; ithread < threads.length; ithread++) {
			threads[ithread] = new Thread() {
				public void run() {
					for (int nsTile0 = ai.getAndIncrement(); nsTile0 < nStiles; nsTile0 = ai.getAndIncrement()) {
						if ( st.getPlanes()[nsTile0] != null) {
							conflicts[nsTile0] =  detectTriangularTileConflicts(
									nsTile0,
									null,    // HashMap<Integer,Integer> replacement_tiles, //
									null,    // int [][][] replacement_neibs,
									tnSurface);
						}
					}
				}
			};
		}
		ImageDtt.startAndJoin(threads);
		if (debugLevel > -1){
			addConflicts(
					conflicts,
					debugLevel);
			printConflictSummary("Detected conflicts:",true,false,false);
			printConflictSummary("Detected ortho-diagonal-ortho conflicts:",false, true, false);
			printConflictSummary("Detected ortho-ortho-diagonal conflicts:",false, false, true);
		}
		return conflicts;
	}
	
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	/**
	 * Calculate cost of all conflicts around supertile nsTile0 by adding "star" weight of each tile involved.
	 * Tile weight can be either from the planes[][] array or from the replacement values in replacement_val_weights
	 * (when the tiles configuration is not yet committed)
	 * @param nsTile0 supertile index to process
	 * @param scaleStartEnd for each conflict triangle add start and end tiles (at the center nsTile0) scaled by this value
	 * @param conflicts array of calculated conflicts (each is {start_layer, end_layer, direction/type bitmask} or null,
	 * in that case it will be calculated
	 * @param replacement_tiles a map of supertile indices to replacement indices (for replacement_neibs and
	 *        replacement_val_weights) or null. If not null, and entry for the supertile full index exists, 
	 *        replacement_neibs and replacement_val_weights will be used, otherwise planes[][] data.
	 *        replacement_tiles may be null, in that case planes[][] data will be used unconditionally.
	 * @param replacement_neibs array of neighbors to use instead of the planes data. First index is an index
	 *        of the replacement supertile (values in the  replacement_tiles map), second - layer number and
	 *        the 3-rd one - direction index for 8 connections: N, NE, E...NW. Value is the destination layer
	 *        number. Can be null if not used (when replacement_tiles is null)
	 * @param replacement_val_weights similar array for tile value/weight data (per-tile index, per layer).
	 *  The innermost data is a tuple {value, weight}
	 * @param tnSurface TileNeibs instance to navigate through the 2-d array encoded in linescan order
	 * @return sum ao the weights of all conflicts fro this tile
	 */
	
	public double getConflictsCost(
			int           nsTile0,
			double        scaleStartEnd, // include start and and layer tiles in the center in overall cost for each triangle (1.0)
			int [][]      conflicts, //
			HashMap<Integer,Integer> replacement_tiles, // null is OK
			int [][][]    replacement_neibs,               // null OK if  replacement_tiles == null
			double [][][] replacement_val_weights,
347
			TileNeibs tnSurface)
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	{
		TilePlanes.PlaneData [][] planes = st.getPlanes();
		// generate conflicts if not provided
		if (conflicts == null) {
			conflicts = detectTriangularTileConflicts(
					nsTile0,
					replacement_tiles, //
					replacement_neibs,
					tnSurface);
		}
		double cost = 0.0;
		if (conflicts != null){
			Integer isTile = (replacement_tiles != null) ? replacement_tiles.get(nsTile0) : null;
			for (int nConfl = 0; nConfl < conflicts.length; nConfl++){
				Conflict conflict = new Conflict(nsTile0, conflicts[nConfl]);
				int start_layer = conflict.getStartLayer();
				int end_layer =   conflict.getEndLayer();
				int [] neibs1 = ((isTile == null) ?
						planes[nsTile0][start_layer].getNeibBest() :
							replacement_neibs[isTile][start_layer]);
				int [] neibs2 = ((isTile == null) ?
						planes[nsTile0][end_layer].getNeibBest() :
							replacement_neibs[isTile][end_layer]);

				int [][] involved = conflict.getInvolvedTiles();
				for (int nTri = 0; nTri < involved.length; nTri++){
					int [] nsTiles = {
							tnSurface.getNeibIndex(nsTile0, involved[nTri][0]),
							tnSurface.getNeibIndex(nsTile0, involved[nTri][1])};
					int [] layers = {neibs1[involved[nTri][0]], neibs2[involved[nTri][1]]};
					for (int it = 0; it < nsTiles.length; it++){
						Integer isTile1 = (replacement_tiles != null) ? replacement_tiles.get(nsTiles[it]) : null;
						if (isTile1 != null){
							cost += replacement_val_weights[isTile1][layers[it]][1];
						} else {
							cost += planes[nsTiles[it]][layers[it]].getStarValueWeight()[1];
						}
					}
				}
				if (scaleStartEnd != 0.0) {
					if (isTile != null){
						cost += scaleStartEnd * (replacement_val_weights[isTile][start_layer][1]+
								replacement_val_weights[isTile][end_layer][1]);
					} else {
						cost += scaleStartEnd * (planes[nsTile0][start_layer].getStarValueWeight()[1]+
								planes[nsTile0][end_layer].getStarValueWeight()[1]);
					}
				}
			}
		}
		return cost;
	}


	public double getConflictsCost(
			int           nsTile0,
			double        scaleStartEnd, // include start and and layer tiles in the center in overall cost for each triangle (1.0)
			int [][]      conflicts, //
			HashMap<Integer,Integer> replacement_tiles, // null is OK
			int [][][]    replacement_neibs,               // null OK if  replacement_tiles == null
			ConnectionCosts connectionCosts,
409
			TileNeibs tnSurface)
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	{
		TilePlanes.PlaneData [][] planes = st.getPlanes();
		// generate conflicts if not provided
		if (conflicts == null) {
			conflicts = detectTriangularTileConflicts(
					nsTile0,
					replacement_tiles, //
					replacement_neibs,
					tnSurface);
		}
		double cost = 0.0;
		if (conflicts != null){
			Integer isTile = (replacement_tiles != null) ? replacement_tiles.get(nsTile0) : null;
			for (int nConfl = 0; nConfl < conflicts.length; nConfl++){
				Conflict conflict = new Conflict(nsTile0, conflicts[nConfl]);
				int start_layer = conflict.getStartLayer();
				int end_layer =   conflict.getEndLayer();
				int [] neibs1 = ((isTile == null) ?
						planes[nsTile0][start_layer].getNeibBest() :
							replacement_neibs[isTile][start_layer]);
				int [] neibs2 = ((isTile == null) ?
						planes[nsTile0][end_layer].getNeibBest() :
							replacement_neibs[isTile][end_layer]);

				int [][] involved = conflict.getInvolvedTiles();
				for (int nTri = 0; nTri < involved.length; nTri++){
					int [] nsTiles = {
							tnSurface.getNeibIndex(nsTile0, involved[nTri][0]),
							tnSurface.getNeibIndex(nsTile0, involved[nTri][1])};
					int [] layers = {neibs1[involved[nTri][0]], neibs2[involved[nTri][1]]};
					for (int it = 0; it < nsTiles.length; it++){
						cost += connectionCosts. getValWeightLast(
								nsTiles[it], // int nsTile,
								layers[it], // int nl,
								false)[1]; // boolean initialValue)
					}
				}
				if (scaleStartEnd != 0.0) {
					cost += scaleStartEnd * connectionCosts. getValWeightLast(
							nsTile0,     // int nsTile,
							start_layer, // int nl,
							false)[1];   // boolean initialValue)
					cost += scaleStartEnd * connectionCosts. getValWeightLast(
							nsTile0,     // int nsTile,
							end_layer, // int nl,
							false)[1];   // boolean initialValue)
				}
			}
		}
		return cost;
	}


	
464 465 466
	
	public int [][] detectTriangularTileConflicts(
			int nsTile0,
467 468
			HashMap<Integer,Integer> replacement_tiles, // null is OK - will use only planes data
			int [][][] replacement_neibs,               // null OK if  replacement_tiles == null
469
			TileNeibs tnSurface)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	{
		TilePlanes.PlaneData [][] planes = st.getPlanes();
		ArrayList<Conflict> conflicts_list= new ArrayList<Conflict>();
		if ( planes[nsTile0] != null) {
			Integer repl_indx = (replacement_tiles != null) ? replacement_tiles.get(new Integer(nsTile0)): null;
			for (int np0 = 0; np0 < planes[nsTile0].length; np0++){
				if (planes[nsTile0][np0] != null) {
					int [] neibs0 = ((repl_indx == null) || (replacement_neibs[repl_indx][np0] == null)) ?
							planes[nsTile0][np0].getNeibBest() :
								replacement_neibs[repl_indx][np0];
					for (int dir = 0; dir < 8; dir +=2){
						int np1;
						np1= neibs0[dir]; // planes[nsTile0][np0].getNeibBest(dir);
						if (np1 >=0) {
							int nsTile1 = tnSurface.getNeibIndex(nsTile0, dir);
							if (nsTile1 >= 0){
								Integer repl_indx1 = (replacement_tiles != null) ? replacement_tiles.get(new Integer(nsTile1)): null;
								int [] neibs1 = (((repl_indx1 == null) || (replacement_neibs[repl_indx1][np1] == null) )?
										planes[nsTile1][np1].getNeibBest() :
											replacement_neibs[repl_indx1][np1]);
								int dir1 = (dir + 3) % 8;
								int np2 = neibs1[dir1]; // planes[nsTile1][np1].getNeibBest(dir1);
								if (np2 >= 0){
									int nsTile2 = tnSurface.getNeibIndex(nsTile1, dir1);
									if (nsTile2 >= 0){
										Integer repl_indx2 = (replacement_tiles != null) ? replacement_tiles.get(new Integer(nsTile2)): null;
										int [] neibs2 = (((repl_indx2 == null) || (replacement_neibs[repl_indx2][np2] == null)) ?
												planes[nsTile2][np2].getNeibBest() :
													replacement_neibs[repl_indx2][np2]);
										int dir2 = (dir1 + 3) % 8;
										int np3 = neibs2[dir2]; // planes[nsTile2][np2].getNeibBest(dir2);
										if ((np3 >= 0) && (np3 != np0)){
502
											Conflict conflict = new Conflict(nsTile0, np0, np3, dir/2);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
											label_apply:
											{
												for (Conflict conf_old:conflicts_list){
													if (conf_old.combine(conflict)){
														break label_apply;
													}
												}
												conflicts_list.add(conflict);
											}
										}
									}
								}
								// ortho-ortho-diagonal, right hand from np0
								dir1 = (dir + 2) % 8;
								np2 = neibs1[dir1]; // planes[nsTile1][np1].getNeibBest(dir1);
								if (np2 >= 0){
									int nsTile2 = tnSurface.getNeibIndex(nsTile1, dir1);
									if (nsTile2 >= 0){
										Integer repl_indx2 = (replacement_tiles != null) ? replacement_tiles.get(new Integer(nsTile2)): null;
										int [] neibs2 = (((repl_indx2 == null) || (replacement_neibs[repl_indx2][np2] == null)) ?
												planes[nsTile2][np2].getNeibBest() :
													replacement_neibs[repl_indx2][np2]);
										int dir2 = (dir1 + 3) % 8;
										int np3 = neibs2[dir2]; // planes[nsTile2][np2].getNeibBest(dir2);
										if ((np3 >= 0) && (np3 != np0)){
528
											Conflict conflict = new Conflict(nsTile0, np0, np3, dir/2, true); // ood, right
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
											label_apply:
											{
												for (Conflict conf_old:conflicts_list){
													if (conf_old.combine(conflict)){
														break label_apply;
													}
												}
												conflicts_list.add(conflict);
											}
										}
									}
								}
								// ortho-ortho-diagonal, left hand from np0
								dir1 = (dir + 6) % 8;
								np2 = neibs1[dir1]; // planes[nsTile1][np1].getNeibBest(dir1);
								if (np2 >= 0){
									int nsTile2 = tnSurface.getNeibIndex(nsTile1, dir1);
									if (nsTile2 >= 0){
										Integer repl_indx2 = (replacement_tiles != null) ? replacement_tiles.get(new Integer(nsTile2)): null;
										int [] neibs2 = (((repl_indx2 == null) || (replacement_neibs[repl_indx2][np2] == null)) ?
												planes[nsTile2][np2].getNeibBest() :
													replacement_neibs[repl_indx2][np2]);
										int dir2 = (dir1 + 5) % 8;
										int np3 = neibs2[dir2]; // planes[nsTile2][np2].getNeibBest(dir2);
										if ((np3 >= 0) && (np3 != np0)){
554
											Conflict conflict = new Conflict(nsTile0, np0, np3, dir/2, false); // ood, left
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
											label_apply:
											{
												for (Conflict conf_old:conflicts_list){
													if (conf_old.combine(conflict)){
														break label_apply;
													}
												}
												conflicts_list.add(conflict);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		if(conflicts_list.isEmpty()){
			return null;
		}
		int [][] conflicts = new int [conflicts_list.size()][];
		int indx=0;
		for (Conflict conflict:conflicts_list){
			conflicts[indx++] = conflict.toArray();
		}
		return conflicts;
	}
	
}