/** * Set up the data object with empty UltrasoundPoint%s representing the points * that need to be scanned. * * This is analagous to setting up the view frustum in traditional 3D graphics. */ private void EstablishScanningPlane(ref UltrasoundScanData data) { OnionLogger.globalLog.PushInfoLayer("Pre-populating data"); UltrasoundProbeConfiguration config = data.GetProbeConfig(); // nearZ and farZ represent near and far clipping "planes" (they're really arcs) float nearZ = config.GetMinScanDistance(); float farZ = config.GetMaxScanDistance(); float arcSizeDegrees = config.GetArcSizeInDegrees(); int scanlines = config.GetNumberOfScanlines(); int pointsPerScanline = config.GetPointsPerScanline(); for (int i = 0; i < scanlines; ++i) { UltrasoundScanline scanline = new UltrasoundScanline(config.GetPosition()); float angleInDegrees = -(arcSizeDegrees / 2) + i * arcSizeDegrees / (scanlines - 1); float angleInRadians = Mathf.Deg2Rad * angleInDegrees; Vector2 trajectory = new Vector2(Mathf.Sin(angleInRadians), Mathf.Cos(angleInRadians)); for (int j = 0; j < pointsPerScanline; ++j) { float d = nearZ + j * (farZ - nearZ) / (pointsPerScanline - 1); Vector2 positionOnPlane = d * trajectory; Vector3 positionInWorldSpace = WorldSpaceFromProjectedPosition(positionOnPlane, config); UltrasoundPoint point = new UltrasoundPoint(positionInWorldSpace, positionOnPlane); scanline.AddUltrasoundPoint(point); } data.AddScanline(scanline); } OnionLogger.globalLog.PopInfoLayer(); }
/** * UltrasoundPoints are considered equal if they have the same ProjectedLocation and WorldSpaceLocation. * Brightness is not included in the equality comparison. * * @param obj The object to compare to this UltrasoundPoint. */ public override bool Equals(object obj) { if (this == obj) { return(true); } if (!(obj is UltrasoundPoint)) { return(false); } UltrasoundPoint other = (UltrasoundPoint)obj; return(other.GetProjectedLocation().Equals(this.GetProjectedLocation()) && other.GetWorldSpaceLocation().Equals(this.GetWorldSpaceLocation())); }
/** * Comparison is solely calculated based on distance in the projection plane. * * @param obj Another Ultrasound point to compare to. * @return A factor proportional in distance between the origin of the two UltrasoundPoints. */ public int CompareTo(object obj) { if (this.Equals(obj)) { return(0); } if (!(obj is UltrasoundPoint)) { throw new ArgumentException("Can only compare UltrasoundPoints to other UltrasoundPoints!"); } UltrasoundPoint other = (UltrasoundPoint)obj; float difference = this.GetProjectedLocation().magnitude - other.GetProjectedLocation().magnitude; /* CompareTo must return an integer. If we just rounded difference directly, we would lose too much * precision. Therefore, we multiply it by a large scalar before casting to an integer. */ return((int)(10000f * difference)); }
/** * Draw a single UltrasoundPoint into a color buffer. * @param point The UltrasoundPoint data to draw. * @param index The index in the buffer at which to place the pixel. * @param bitmap The bitmap into which to draw the pixel. */ protected virtual void DrawPoint(UltrasoundPoint point, int index, ref ColorBitmap bitmap) { long bufferSize = bitmap.colors.Length; #if UNITY_EDITOR UltrasoundDebug.Assert(index < bufferSize && index >= 0, string.Format("{0} should be in the interval[0, {1})", index, bufferSize), this); UltrasoundDebug.Assert(point.GetBrightness() >= 0f && point.GetBrightness() <= 1f, string.Format("Pixel brightness {0} should be in the interval [0,1]", point.GetBrightness()), this); #endif if (index >= bufferSize || index < 0) { return; } Color pointColor = drawColor; // minimum pixel brightness of 0.1f pointColor *= Mathf.Max(0.1f, point.GetBrightness()); bitmap.colors[index] = pointColor; }
public UltrasoundScanData SendScanData() { UltrasoundProbeConfiguration config = new UltrasoundProbeConfiguration(); config.SetMaxScanDistance(MAX_Y); config.SetMinScanDistance(MIN_Y); UltrasoundScanData data = new UltrasoundScanData(config); for (float i = MIN_X; i <= MAX_X; i += STEPSIZE) { UltrasoundScanline scanline = new UltrasoundScanline(config.GetPosition()); for (float j = MIN_Y; j <= MAX_Y; j += STEPSIZE) { UltrasoundPoint p = new UltrasoundPoint(Vector3.zero, new Vector2(i * (j / MAX_Y), j)); p.SetBrightness(Random.Range(0f, 1f)); // Generate noise. scanline.AddUltrasoundPoint(p); } data.AddScanline(scanline); } return(data); }