public void Run(Sphere shape) { var rayOrigin = new RtPoint(0, 0, -5); var wallZ = 10; var color = new RtColor(1.0, 0, 0); var wallSize = 7.0; var canvasSize = 800; var canvas = new Canvas(canvasSize, canvasSize); var pixelSize = wallSize / canvasSize; var half = wallSize / 2; Parallel.For(0, canvasSize, y => { for (int x = 0; x < canvasSize; x++) { var worldY = half - pixelSize * y; var worldX = -half + pixelSize * x; var position = new RtPoint(worldX, worldY, wallZ); var ray = new Ray(rayOrigin, (position - rayOrigin).Normalize()); if (shape.Intersect(ray).Count > 0) { canvas[x, y] = color; } } }); PpmOutput.WriteToFile("sphere.ppm", canvas.GetPPMContent()); }
public void Then_A_Multiplied_by_b_Equals_Tuple(double x, double y, double z) { var expectedPoint = new RtPoint(x, y, z); var actualPoint = _matrixContext.MatrixA * _pointsContext.Point; Assert.Equal(expectedPoint, actualPoint); }
public ViewTransform(RtPoint from, RtPoint to, RtVector up) { Up = up; To = to; From = from; Forward = (To - From).Normalize(); var upNormalized = Up.Normalize(); Left = Forward.Cross(upNormalized); var trueUp = Left.Cross(Forward); var orientation = RtMatrix.Identity; orientation.M11 = Left.X; orientation.M12 = Left.Y; orientation.M13 = Left.Z; orientation.M21 = trueUp.X; orientation.M22 = trueUp.Y; orientation.M23 = trueUp.Z; orientation.M31 = -Forward.X; orientation.M32 = -Forward.Y; orientation.M33 = -Forward.Z; var translation = RtMatrix.Identity; translation.M14 = -from.X; translation.M24 = -from.Y; translation.M34 = -from.Z; Matrix = orientation * translation; }
public void Then_point_Minus_vector_Should_Equal_Point(float x, float y, float z) { var expectedTuple = new RtPoint(x, y, z); RtPoint actualPoint = _pointsContext.Point - _vectorContext.Vector; Assert.Equal(expectedTuple, actualPoint); }
public void Then_Point_of_computations_Should_Equal_Point(float x, float y, float z) { var expectedPoint = new RtPoint(x, y, z); var actualPoint = _computationsContext.Computations.Position; Assert.Equal(expectedPoint, actualPoint); }
public void Then_Origin_Of_ray2_Should_Equal_Point(double x, double y, double z) { var expectedPoint = new RtPoint(x, y, z); var actualPoint = _rayContext.Ray2.Origin; Assert.Equal(expectedPoint, actualPoint); }
public void Then_transformT_Is_Multiplied_By_point_Equals_Point(string x, string y, string z) { var expectedPoint = new RtPoint(x.EvaluateExpression(), y.EvaluateExpression(), z.EvaluateExpression()); var actualPoint = _transformationsContext.TransformT * _pointsContext.Point; Assert.Equal(expectedPoint, actualPoint); }
public void Then_Origin_Of_ray_Should_Equal_Point(double x, double y, double z) { var expectedOrigin = new RtPoint(x, y, z); var actualOrigin = _rayContext.Ray.Origin; Assert.Equal(expectedOrigin, actualOrigin); }
public void Then_Origin_Of_SavedRay_Of_shape_Should_Equal_Point(double x, double y, double z) { var expectedOrigin = new RtPoint(x, y, z); var actualOrigin = _shapesContext.TestShape.SavedRay.Origin; Assert.Equal(expectedOrigin, actualOrigin); }
public void Then_Position_Of_p_Should_Equal_Point(double p, double x, double y, double z) { var expectedPosition = new RtPoint(x, y, z); RtPoint actualPosition = _rayContext.Ray.Position(p); Assert.Equal(expectedPosition, actualPosition); }
public void Then_VertexN_Of_Mesh_Of_objFile_Should_Equal(int indexOfVertex, double x, double y, double z) { var expectedPoint = new RtPoint(x, y, z); var actualPoint = _objFileContext.ObjFile.Mesh.GetVertex(indexOfVertex); Assert.Equal(expectedPoint, actualPoint); }
public void Then_Transform_Multiplied_By_point_Equals_A_Point(int x, int y, int z) { var expectedPoint = new RtPoint(x, y, z); var actualPoint = _transformationsContext.Transform * _pointsContext.Point; Assert.Equal(expectedPoint, actualPoint); }
public void WriteNumber(int number, RtPoint startingPoint) { var moveClock = new Transform() .RotateY(number * Math.PI / 6); var numberPoint = moveClock * startingPoint; WriteToCanvas(numberPoint.X, numberPoint.Z); }
private static void Draw(Canvas canvas, RtPoint position) { var x = (int)Math.Round(position.X); var y = canvas.Height - (int)Math.Round(position.Y) - 1; if (x >= 0 && x <= canvas.Width - 1 && y >= 0 && y <= canvas.Height - 1) { canvas[x, y] = new RtColor(1, 0, 0); } }
public RtPoint Perturb(RtPoint localPoint) { var newX = localPoint.X + PerlinNoise2.Perlin(localPoint.X, localPoint.Y, localPoint.Z) * _factor; var newY = localPoint.Y + PerlinNoise2.Perlin(localPoint.X, localPoint.Y, localPoint.Z + 1) * _factor; var newZ = localPoint.Z + PerlinNoise2.Perlin(localPoint.X, localPoint.Y, localPoint.Z + 2) * _factor; var newPoint = new RtPoint(newX, newY, newZ); return(newPoint); }
public void Run() { var startingPoint = new RtPoint(0, 0, 1); WriteToCanvas(startingPoint.X, startingPoint.Z); for (int i = 1; i < 12; i++) { WriteNumber(i, startingPoint); } PpmOutput.WriteToFile("file.ppm", _canvas.GetPPMContent()); }
private static Plane CreateWall(double yRotate, RtPoint location, RtColor color) { return(new Plane() { Transform = new Transform() .RotateX(Math.PI / 2) .RotateY(yRotate) .Translation(location), Material = new Material() { Color = color, Specular = 1 } }); }
public Canvas Run(Sphere shape) { var light = new PointLight(new RtPoint(-10, 10, -10), new RtColor(1, 1, 1)); var rayOrigin = new RtPoint(0, 0, -5); var wallZ = 10; var wallSize = 7.0; var canvasSize = 800; var canvas = new Canvas(canvasSize, canvasSize); var pixelSize = wallSize / canvasSize; var half = wallSize / 2; Parallel.For(0, canvasSize, y => { var worldY = half - pixelSize * y; for (int x = 0; x < canvasSize; x++) { var worldX = -half + pixelSize * x; var position = new RtPoint(worldX, worldY, wallZ); var direction = position - rayOrigin; direction = direction.Normalize(); var ray = new Ray(rayOrigin, direction); var intersections = shape.Intersect(ray); if (intersections.HasHit()) { var computations = new Computations(); var intersection = intersections.Hit(); computations.Shape = intersection.Shape; computations.Position = ray.Position(intersection.Time); computations.NormalVector = shape.NormalAt(computations.Position, null); computations.EyeVector = ray.Direction.Negate(); var color = shape.Material.Lighting(computations, light, 1); canvas[x, y] = color; } } }); return(canvas); }
//public RtMatrix Multiplication() //{ // var results = new RtMatrix(0, 0); // for (int c = 0; c < 1000; c++) // { // var matrix1 = CreateMatrix(256); // var matrix2 = CreateMatrix(256); // results = matrix1 * matrix2; // } // return results; //} //private static RtMatrix CreateMatrix(int size) //{ // var matrix = new RtMatrix(size, size); // var rnd = new Random(unchecked((int)DateTime.Now.Ticks)); // for (int x = 0; x < size; x++) // { // for (int y = 0; y < size; y++) // { // matrix[x, y] = rnd.NextDouble() * 10; // } // } // return matrix; //} public void BuildImageFromChapterFix() { var rayOrigin = new RtPoint(0, 0, -5); var wallZ = 10; var wallSize = 7.0; var canvasSize = 800; var canvas = new Canvas(canvasSize, canvasSize); var pixelSize = wallSize / canvasSize; var half = wallSize / 2; var color = new RtColor(1, 0, 0); var shape = new Sphere { Transform = new Transform() .Scaling(0.5, 1, 1) .Shearing(1, 0, 0, 0, 0, 0) }; for (int y = 0; y < canvasSize; y++) { var worldY = half - pixelSize * y; for (int x = 0; x < canvasSize; x++) { var worldX = -half + pixelSize * x; var position = new RtPoint(worldX, worldY, wallZ); var ray = new Ray(rayOrigin, (position - rayOrigin).Normalize()); var intersections = shape.Intersect(ray); if (intersections.HasHit()) { canvas[x, y] = color; } } } }
public void Then_point_Equals_Point(float x, float y, float z) { var expectedPoint = new RtPoint(x, y, z); Assert.Equal(expectedPoint, _pointsContext.Point); }
public override RtColor PatternAt(RtPoint point) { return(new RtColor(point.X, point.Y, point.Z)); }
public RtRaster <double> RunGpuInterpolation(YieldReportData dataPoints, IGeometry dataBoundary) { try { int cellSize = 1; var bounds = new RTBounds(dataBoundary).ToMeters(); var xMax = bounds.MaxX; var xMin = bounds.MinX; var yMax = bounds.MaxY; var yMin = bounds.MinY; var xDist = (int)(xMax - xMin + 1); var yDist = (int)(yMax - yMin + 1); //var cost = (xDist * yDist * dataPoints.Count) / (Environment.ProcessorCount * cellSize * cellSize); var nCols = xDist / cellSize; var nRows = yDist / cellSize; CollectionStats yld = dataPoints.YieldData.Yield; //create host side arrays for input data and boundary and output data double[] h_datax = yld.DataPoints.Select(pt => RtPoint <double> .LatLonToMeters(pt)).Select(ptz => ptz.X).ToArray(); double[] h_datay = yld.DataPoints.Select(pt => RtPoint <double> .LatLonToMeters(pt)).Select(ptz => ptz.Y).ToArray(); double[] h_dataz = yld.DataPoints.Select(pt => RtPoint <double> .LatLonToMeters(pt)).Select(ptz => ptz.Z).ToArray(); var dataSize = sizeof(double) * h_datax.Length; Console.WriteLine("Performing interpolation on {0} data points of {1} bytes", h_datax.Length, dataSize); var cols = Enumerable.Range(0, nCols - 1).ToArray(); var coords = Enumerable.Range(0, nRows).SelectMany(row => cols.Select(col => Tuple.Create(row, col))).ToArray(); double[] h_outputx = coords.Select(coord => xMin + (coord.Item2 * cellSize)).ToArray(); double[] h_outputy = coords.Select(coord => yMin + (coord.Item1 * cellSize)).ToArray(); double[] h_outputz = coords.Select(coord => 0.0).ToArray(); var outputSize = sizeof(double) * h_outputx.Length; Console.WriteLine("Output Raster has {0} raster points of {1} bytes", h_outputx.Length, outputSize); double2[] h_boundCoords = dataBoundary.Coordinates.Select(coord => new double2(coord.X, coord.Y)).ToArray(); using (CudaDeviceVariable <double> d_datax = h_datax) using (CudaDeviceVariable <double> d_datay = h_datay) using (CudaDeviceVariable <double> d_dataz = h_dataz) using (CudaDeviceVariable <double> d_outputx = h_outputx) using (CudaDeviceVariable <double> d_outputy = h_outputy) using (CudaDeviceVariable <double> d_outputz = h_outputz) { const int ThreadsPerBlock = 512; InterpolateKernel.BlockDimensions = ThreadsPerBlock; InterpolateKernel.GridDimensions = (h_outputx.Length + ThreadsPerBlock - 1) / ThreadsPerBlock; Console.WriteLine("Invoke Kernel with Grid Dimensions = {0}", InterpolateKernel.GridDimensions); Stopwatch timer = new Stopwatch(); timer.Start(); InterpolateKernel.Run(d_datax.DevicePointer, d_datay.DevicePointer, d_dataz.DevicePointer, d_outputx.DevicePointer, d_outputy.DevicePointer, d_outputz.DevicePointer, h_outputx.Length, h_datax.Length); timer.Stop(); var elapsed = timer.ElapsedTicks; Console.WriteLine("Gpu Interpolation took {0} ms", elapsed / (Stopwatch.Frequency / 1000)); h_outputz = d_outputz; } if (h_outputz.All(pt => pt != 0.0)) { Console.WriteLine("Kernel succeeded"); } else { Console.WriteLine("Kernel failed"); } var raster = new RtRaster <double>(nCols, nRows, xMin, yMin, cellSize * 2); var noDataValue = 0.0f; var rasterBand = RtRasterBand <double> .CreateRasterBand(typeof(double), nCols, nRows, noDataValue, h_outputz); raster.SetRasterBand(0, rasterBand); return(raster); } catch (Exception ex) { ctx.Dispose(); Console.WriteLine(ex.Message + "\nStackTrace: " + ex.StackTrace); //+ // ex.InnerException == null ? "" : // String.Format("\n\t{0}\n\tStackTrace: {1}", ex.InnerException.Message, // ex.InnerException.StackTrace) return(null); } }
public void Then_pointN_Equals_Point(int pointIndex, string x, string y, string z) { var expectedPoint = new RtPoint(x.EvaluateExpression(), y.EvaluateExpression(), z.EvaluateExpression()); Assert.Equal(expectedPoint, _pointsContext.Points[pointIndex]); }
public Projectile(RtPoint position, RtVector velocity) { Position = position; Velocity = velocity; }
public void Then_Position_Of_AreaLight_Should_Equal(float x, float y, float z) { var expectedPoint = new RtPoint(x, y, z); Assert.Equal(expectedPoint, _lightsContext.AreaLight.Position); }
public override RtVector LocalNormalAt(RtPoint point, Intersection hit) { return(new RtVector(point.X, point.Y, point.Z)); }