using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq; namespace com.rfilkov.components { [System.Serializable] public class Blob { [Tooltip("Number of pixels in the blob.")] public int pixels; [Tooltip("Blob limits in X (pixels), Y (pixels) & Z (mm).")] public int minx, miny, minz, maxx, maxy, maxz; //public long sumx, sumy, sumz; /// /// Blob constructor. /// /// /// /// public Blob(int x, int y, int z) { minx = maxx = x; //sumx = x; miny = maxy = y; //sumy = y; minz = maxz = z; //sumz = z; pixels++; } /// /// Checks if the current blob is inside the given one. /// /// Blob /// public bool IsInside(Blob b) { if (minx >= b.minx && maxx <= b.maxx && miny >= b.miny && maxy <= b.maxy && minz >= b.minz && maxz <= b.maxz) { return true; } return false; } /// /// Checks if a pixel is near or inside this blob. /// /// X /// Y /// Z /// Tolerance in X- & Y-directions (pixels) /// Tolerance in Z-direction (mm) /// public bool IsNearOrInside(int pixX, int pixY, int pixZ, int difXY, int difZ) { if (pixX >= (minx - difXY) && pixX <= (maxx + difXY) && pixY >= (miny - difXY) && pixY <= (maxy + difXY)) { if(pixZ >= (minz - difZ) && pixZ <= (maxz + difZ)) { return true; } } return false; } /// /// Adds a pixel to the blob. /// /// X /// Y /// Z public void AddDepthPixel(int pixX, int pixY, int pixZ) { pixels++; minx = Mathf.Min(minx, pixX); miny = Mathf.Min(miny, pixY); minz = Mathf.Min(minz, pixZ); maxx = Mathf.Max(maxx, pixX); maxy = Mathf.Max(maxy, pixY); maxz = Mathf.Max(maxz, pixZ); //sumx += pixX; //sumy += pixY; //sumz += pixZ; } /// /// Gets the blob width. /// /// Blob width public float GetWidth() { return maxx - minx; } /// /// Gets the blob height. /// /// Blob height public float GetHeight() { return maxy - miny; } /// /// Gets the blob center. /// /// Blob center public Vector3 GetBlobCenter() { return new Vector3((minx + maxx) / 2, (miny + maxy) / 2, (minz + maxz) / 2); } } }