public Color CalcPixelColor(ref long seed, ref int maxDepth, int x, int y, PixelDebugData debug) { if (debug != null) { debug.Points.Add(Origin); } Color accumulateColor = Color.black; int c = debug == null ? AntiAliasRayCount : 1; for (int i = 0; i < c; ++i) { var offsetx = (debug == null ? SimpleRandom.RandNorm(ref seed) : 0.5f) * StepX; var offsety = (debug == null ? SimpleRandom.RandNorm(ref seed) : 0.5f) * StepY; var dir = new Vector4(StartX + x * StepX + offsetx, StartY + y * StepY + offsety, 1, 0); var dir2 = CameraLocal2World * dir; Ray ray = new Ray(Origin, dir2.normalized); accumulateColor += TraceScene(ref seed, ref maxDepth, ray, 0, debug); } Color col = accumulateColor / c; if (GammarCorrection) { col.r = Mathf.Sqrt(col.r); col.g = Mathf.Sqrt(col.g); col.b = Mathf.Sqrt(col.b); } col.a = 1; return(col); }
public void Random() { var boundingRectangle = new Rectangle(new Point(0, 0), new Point(10, 10)); var randomCount = 20; var initialRectangles = new List <Rectangle>(randomCount); var seed = (uint)Environment.TickCount; var random = new SimpleRandom { Seed = seed }; for (var i = 0; i < randomCount; i++) { initialRectangles.Add(Rectangle.CreateRandom(random, boundingRectangle)); } var targetRectangle = Rectangle.CreateRandom(random, boundingRectangle); try { Test(initialRectangles, boundingRectangle, targetRectangle); } catch (Exception e) { throw new InvalidOperationException("Seed: " + seed, e); } }
public static Rectangle CreateRandom(SimpleRandom random, Rectangle boundingRectangle, Dimensions minSize) { var width = (byte)random.Next(minSize.Width, boundingRectangle.Width + 1); var x1 = (byte)random.Next(boundingRectangle.TopLeft.X, boundingRectangle.BottomRight.X + 2 - width); var height = (byte)random.Next(minSize.Height, boundingRectangle.Height + 1); var y1 = (byte)random.Next(boundingRectangle.TopLeft.Y, boundingRectangle.BottomRight.Y + 2 - height); return(new Rectangle(new Point(x1, y1), width, height)); }
public Point?PlaceInside(Rectangle rectangleToPlace, SimpleRandom random) { if (rectangleToPlace.Width > Width || rectangleToPlace.Height > Height) { return(null); } var xOffset = random.Next(Width - rectangleToPlace.Width + 1); var yOffset = random.Next(Height - rectangleToPlace.Height + 1); return(new Point((byte)(TopLeft.X + xOffset), (byte)(TopLeft.Y + yOffset))); }
public void T21_RandomWriteRead() { BlockStream blocks = new BlockStream(); blocks.CreateMemoryStream(); SimpleRandom random = new SimpleRandom(102854); int TestIterations = random.RandomInteger(100, 10000); Console.Out.WriteLine("Performing " + TestIterations.ToString() + " test iterations."); for (int TestNumber = 1; TestNumber <= TestIterations; TestNumber++) { int action = random.RandomInteger(0, 100); if (action < 50) // Insert { Hashtable tags = this.NewTestTags(TestNumber); byte[] bytes = this.NewTestBytes(TestNumber); blocks.WriteBlock(Guid.NewGuid(), tags, bytes); } else { List <Guid> ids = blocks.GetBlockIDs(); int ndx = random.RandomInteger(1, ids.Count); Guid id = ids[ndx - 1]; if (action < 75) // Update { Hashtable tags = this.NewTestTags(TestNumber); byte[] bytes = this.NewTestBytes(TestNumber); blocks.WriteBlock(id, tags, bytes); } else if (action <= 100) // Delete { blocks.DestroyBlock(id); } } } this.AssertIsValid(blocks); blocks.Compact(); this.AssertIsValid(blocks); return; }
protected override Rectangle?SelectNextLot(RectangleIntervalTree placedFragments, SimpleRandom random) { var boundingRectangle = placedFragments.BoundingRectangle; while (_lotSize.Width > boundingRectangle.Width || _lotSize.Height > boundingRectangle.Height) { _lotSize = new Dimensions((byte)(_lotSize.Width - 1), (byte)(_lotSize.Height - 1)); } while (_lotSize.Contains(MinLotSize)) { // TODO: Optimize using // "Polygon Decomposition". Handbook of Computational Geometry. p. 491 or // "Graph-Theoretic Solutions to Computational Geometry Problems" // https://stackoverflow.com/questions/5919298/algorithm-for-finding-the-fewest-rectangles-to-cover-a-set-of-rectangles-without for (var attempt = 0; attempt < LotPlacementAttempts; attempt++) { var x1 = (byte)random.Next(boundingRectangle.TopLeft.X, boundingRectangle.BottomRight.X - _lotSize.Width + 1); var y1 = (byte)random.Next(boundingRectangle.TopLeft.Y, boundingRectangle.BottomRight.Y - _lotSize.Height + 1); var potentialLot = new Rectangle(new Point(x1, y1), (byte)(_lotSize.Width - 1), (byte)(_lotSize.Height - 1)); if (!placedFragments.GetOverlapping(potentialLot).Any()) { return(potentialLot); } } _lotSize = new Dimensions((byte)(_lotSize.Width - 1), (byte)(_lotSize.Height - 1)); } return(null); }
public byte RandomPoint(SimpleRandom random) => (byte)random.Next(Beginning, End + 1);
protected abstract Rectangle?SelectNextLot(RectangleIntervalTree placedFragments, SimpleRandom random);
public bool Scatter(PixelDebugData debug, ref long seed, Ray rayIn, ref HitRecord hit, out Color atten, out Ray scattered) { switch (MatType) { case RayTracingMaterialType.Lambert: { Vector3 dir = hit.Normal * 1.0001f; if (debug == null) { dir += SimpleRandom.RandomInsideUnitSphere(ref seed); } Ray rayOut = new Ray(); rayOut.origin = hit.Point; rayOut.direction = Vector3.Normalize(dir); scattered = rayOut; atten = Albedo; return(true); } case RayTracingMaterialType.Metal: { Vector3 dir = Reflect(rayIn.direction, hit.Normal); Ray rayOut = new Ray(); rayOut.origin = hit.Point; rayOut.direction = Vector3.Normalize(dir); scattered = rayOut; atten = Albedo; return(Vector3.Dot(rayOut.direction, hit.Normal) > 0); } case RayTracingMaterialType.Dielectric: { Vector3 N = hit.Normal; Vector3 R = Reflect(rayIn.direction, N); atten = Color.white; float ni_over_nt = Mathf.Max(0.1f, RefractionIndex); if (Vector3.Dot(rayIn.direction, N) > 0) { // Ray travel through inside N = -N; } else { // Ray comming from outside ni_over_nt = 1.0f / ni_over_nt; } Vector3 refracted; float reflect_prob = 1.0f; if (Refract(rayIn.direction, N, ni_over_nt, out refracted)) { reflect_prob = Schlick(Vector3.Dot(N, -rayIn.direction), RefractionIndex); } if (SimpleRandom.RandNorm(ref seed) < reflect_prob) { scattered = new Ray(hit.Point, R); } else { scattered = new Ray(hit.Point, refracted); } return(true); } } atten = Color.black; scattered = new Ray(); return(false); }
public static Rectangle CreateRandom(SimpleRandom random, Rectangle boundingRectangle) => CreateRandom(random, boundingRectangle, new Dimensions(1, 1));
protected override Rectangle?SelectNextLot(RectangleIntervalTree placedFragments, SimpleRandom random) => null;