private static int[] GetFieldSphere(Point3D point, double radius, FluidField3D field) { // Get the box that contains the return circle var center = GetFieldPoint_XYZ(point, field); var min = GetFieldPoint_XYZ(new Point3D(point.X - radius, point.Y - radius, point.Z - radius), field); var max = GetFieldPoint_XYZ(new Point3D(point.X + radius, point.Y + radius, point.Z + radius), field); // Get points that are inside the circle List <int> retVal = new List <int>(); double maxDistance = radius * field.Size; double maxDistanceSquared = maxDistance * maxDistance; for (int x = min.Item1; x <= max.Item1; x++) { for (int y = min.Item2; y <= max.Item2; y++) { for (int z = min.Item3; z <= max.Item3; z++) { double dx = x - center.Item1; double dy = y - center.Item2; double dz = z - center.Item3; double distanceSquared = (dx * dx) + (dy * dy) + (dz * dz); if (distanceSquared <= maxDistanceSquared) { retVal.Add(field.Get1DIndex(x, y, z)); } } } } // Exit Function return(retVal.ToArray()); }
private void DrawLines_Grid(int numSamples, double half, double lineThickness) { double[] velX = _field.VelocityX; double[] velY = _field.VelocityY; double[] velZ = _field.VelocityZ; // Always include 0 and size - 1. The rest should be evenly distributed double increment = Convert.ToDouble(_field.Size - 1) / (numSamples - 1); // subtracting 1 to guarantee the edges get one _velocityLines.BeginAddingLines(); // Try all of them for (double x = 0; x < _field.Size; x += increment) { for (double y = 0; y < _field.Size; y += increment) { for (double z = 0; z < _field.Size; z += increment) { int ix = Convert.ToInt32(Math.Round(x)); if (ix > _field.Size - 1) { ix = _field.Size - 1; } int iy = Convert.ToInt32(Math.Round(y)); if (iy > _field.Size - 1) { iy = _field.Size - 1; } int iz = Convert.ToInt32(Math.Round(z)); if (iz > _field.Size - 1) { iz = _field.Size - 1; } int index = _field.Get1DIndex(ix, iy, iz); DrawLinesSprtAddLine(ix, iy, iz, index, half, lineThickness, velX, velY, velZ); } } } _velocityLines.EndAddingLines(); }
private void Reset1_Click(object sender, RoutedEventArgs e) { try { ResetField(); for (int x = 1; x < _field.Size - 1; x++) // if I go to the edges, they just get blanked out { for (int y = 1; y < _field.Size - 1; y++) { _field.SetInk(_field.Get1DIndex(x, y, 1 + StaticRandom.Next(_field.Size - 2)), 1); } } //_field.Update(); //DrawField(_bitmap, _field); } catch (Exception ex) { MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error); } }
private static void DrawFieldSprtDoIt(byte[] pixels, double[] ink, bool[] blocked, int size, byte[] colorZFront, byte[] colorZBack, AxisFor pixelX, AxisFor pixelY, AxisFor pixelZ) { List <Mapping_2D_1D> flattened = new List <Mapping_2D_1D>(); // Setup the pixel array //for (int y2D = pixelY.Start; pixelY.IsPos ? y2D <= pixelY.Stop : y2D >= pixelY.Stop; y2D += pixelY.Increment) foreach (int y2D in pixelY.Iterate()) { int offsetY = pixelY.GetValueForOffset(y2D) * size; //for (int x2D = pixelX.Start; pixelX.IsPos ? x2D <= pixelX.Stop : x2D >= pixelX.Stop; x2D += pixelX.Increment) foreach (int x2D in pixelX.Iterate()) { int offset = offsetY + pixelX.GetValueForOffset(x2D); flattened.Add(new Mapping_2D_1D(x2D, y2D, offset)); } } // Each pixel of the output bitmap can be added up independently, so employ some threading flattened.AsParallel().ForAll(o => { //TODO: Color is 6.5 times slower than byte array List <byte[]> colorColumn = new List <byte[]>(); for (int z2D = pixelZ.Start; pixelZ.IsPos?z2D <= pixelZ.Stop : z2D >= pixelZ.Stop; z2D += pixelZ.Increment) { int x = -1, y = -1, z = -1; pixelX.Set3DIndex(ref x, ref y, ref z, o.X); pixelY.Set3DIndex(ref x, ref y, ref z, o.Y); pixelZ.Set3DIndex(ref x, ref y, ref z, z2D); int index = FluidField3D.Get1DIndex(x, y, z, size); if (blocked[index]) { // Blocked cells are all white, so save the overlay method a bit of work, and throw out everything behind this colorColumn.Clear(); colorColumn.Add(new byte[] { 255, 255, 255, 255 }); continue; } double inkCell = ink[index]; if (Math1D.IsNearZero(inkCell)) { continue; } byte[] depthColor = UtilityWPF.AlphaBlend(colorZBack, colorZFront, UtilityCore.GetScaledValue_Capped(0, 1, 0, size - 1, z)); int alpha = Convert.ToInt32(Math.Round(inkCell * 255)); if (alpha < 0) { alpha = 0; } else if (alpha > 255) { alpha = 255; } colorColumn.Add(new byte[] { Convert.ToByte(alpha), depthColor[1], depthColor[2], depthColor[3] }); } byte[] color = colorColumn.Count > 0 ? UtilityWPF.OverlayColors(colorColumn) : new byte[] { 0, 0, 0, 0 }; pixels[o.Offset1D * 4 + 0] = color[3]; // Blue pixels[o.Offset1D * 4 + 1] = color[2]; // Green pixels[o.Offset1D * 4 + 2] = color[1]; // Red pixels[o.Offset1D * 4 + 3] = color[0]; // Alpha }); }