GetClosestIndices MEL

From scripting
Revision as of 05:41, 22 April 2017 by Nickpisca (talk | contribs) (Created page with " global proc int[] GetClosestIndices(string $RGBMat[], int $CurInt[], string $Skip[], int $All0NeighborNum, string $PlaneName, int $XDivs, int $YDivs) { //0001D LLC 2...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
global proc int[] GetClosestIndices(string $RGBMat[], int $CurInt[], string $Skip[], 
					int $All0NeighborNum, string $PlaneName, int $XDivs, int $YDivs) {
	//0001D LLC 2015 Nick Pisca
	//string $RGBMat[] = $WZ; int $CurInt[] = {1,9}; string $Skip[] = {"1,9","0,10"};
	
		int $XEndCt; int $xbeg;
	int $indexNums[1] = {0,0};
	float $SmallestRGB = 1000000.0;
	float $SmallestDist = 1000000.0;

	vector $MainVec = `pointPosition($PlaneName + ".uv[" + (float($CurInt[0])/float($XDivs)) + "][" + (float($CurInt[1])/float($YDivs)) + "]")`;
	string $indexNumStrArr[];
	if ($All0NeighborNum > 0) {
		$XEndCt = $CurInt[0] + $All0NeighborNum;
		if ($XEndCt > size($RGBMat)) {
			$XEndCt = size($RGBMat);
		}
		$xbeg = $CurInt[0] - $All0NeighborNum;
		if ($xbeg < 0) { $xbeg = 0; }
	} else {
		$XEndCt = size($RGBMat);
		$xbeg = 0;
	}
	for ($x=$xbeg;$x<=$XEndCt;$x++) {
		int $yend;
		int $ybeg;
		string $YSub[] = RealTokenize($RGBMat[$x], ",");

		if ($All0NeighborNum > 0) {
			$ybeg = $CurInt[1] - $All0NeighborNum;
			if ($ybeg < 0) { $ybeg = 0; }
			$yend = $CurInt[1] + $All0NeighborNum;
			if ($yend > size($YSub)-1) {
				$yend = size($YSub) - 1;
			}
		} else {
			$ybeg = 0;
			$yend = size($YSub) - 1;
		}		
		for ($y=$ybeg;$y<=$yend;$y++) {
			string $CurIndStr = $x + "," + $y;
			
			string $ObjLoc = $PlaneName + ".uv[" + (float($x)/float($XDivs)) + "][" + (float($y)/float($YDivs)) + "]";
			vector $ObjVec = `pointPosition($ObjLoc)`;
			vector $Diff = $ObjVec - $MainVec;
			float $DMag = mag($Diff);
			
			if(IsInStrArr($Skip,$CurIndStr) == 0) {
				if ($DMag <= $SmallestDist) {
					if ($DMag == $SmallestDist) {
						$indexNumStrArr[(size($indexNumStrArr))] = $x+","+$y;  //print($indexNumStrArr);
					} else {
						clear($indexNumStrArr);
					}
					$SmallestDist = $DMag;
					$indexNums = {$x,$y};
				}
			}
		}
	}
	if(size($indexNumStrArr) == 0) {
		return $indexNums; 
	} else {
			int $RI = rand(0,size($indexNumStrArr));
			//string $RISolve[] = RealTokenize($indexNumStrArr[$RI], ",");   //print($indexNumStrArr[$RI]);
			string $BB[];
			$NT = `tokenize $indexNumStrArr[$RI] "," $BB`;
			int $RIFinal[] = {int($BB[0]), int($BB[1])};
			return $RIFinal;
	}
}


More information on vectors and arrays, read pages 12-16 in YSYT.