public static double PearsonSobelCorrelation(ArrayView3D <byte> bufIn, Index2 pixel1, Index2 pixel2) { var minLength = bufIn.Depth; int sum1 = 0, sum2 = 0; float mean1 = 0, mean2 = 0; for (int i = 0; i < minLength; i++) { short x = bufIn[pixel1.X, pixel1.Y, i]; short y = bufIn[pixel2.X, pixel2.Y, i]; sum1 += x; sum2 += y; } mean1 = sum1 / minLength; mean2 = sum2 / minLength; float covariation = 0; float xDerSqrSum = 0; float yDerSqrSum = 0; for (int i = 0; i < minLength; i++) { short x = bufIn[pixel1.X, pixel1.Y, i]; short y = bufIn[pixel2.X, pixel2.Y, i]; float xDer = x - mean1; float yDer = y - mean2; covariation += xDer * yDer; xDerSqrSum += xDer * xDer; yDerSqrSum += yDer * yDer; } return(covariation / XMath.Sqrt(xDerSqrSum * yDerSqrSum)); }
public Vector3 GetDirectReflection(XorShift64Star random, Scene scene, Vector3 position, Vector3 direction, Vector3 normal) { float roughness = 0.01f; Vector3 lightAccumulator = new Vector3(); for (int i = 0; i < scene.SceneLights.Length; i++) { random.NextFloat(); Light l = scene.SceneLights[i]; Vector3 ldir = l.GetPoint(random) - position; ldir /= XMath.Sqrt(Vector3.Dot(ldir, ldir)); Ray r = new Ray(position + ldir * 0.002f, ldir); Intersection intersection = scene.TraceScene(r); if (!intersection.HitLight) { continue; } float probability = Helper.ggxNormalDistribution(Vector3.Dot(Vector3.Reflect(-ldir, normal), -direction), roughness); Vector3 reflectionColor = intersection.HitLightObject.GetColor(ldir); lightAccumulator += reflectionColor * probability; } return(lightAccumulator); }
public static void CalculateSignatureLengthDerivative(Index2 index, ArrayView3D <float> output, ArrayView3D <byte> input) { var x = index.X; var y = index.Y; if (x == 0) { x = 1; } if (x == input.Width - 1) { x = input.Width - 2; } if (y == 0) { y = 1; } if (y == input.Height - 1) { y = input.Height - 2; } var depth = input.Depth; float xDiffComponentSum = 0; float yDiffComponentSum = 0; for (int i = 0; i < depth; i++) { var val1 = input[x - 1, y, i]; if (val1 < 0) { val1 = 0; } var val2 = input[x + 1, y, i]; if (val2 < 0) { val1 = 0; } var xDiff = val2 - val1; xDiffComponentSum += xDiff * xDiff; val1 = input[x, y - 1, i]; if (val1 < 0) { val1 = 0; } val2 = input[x, y + 1, i]; if (val2 < 0) { val1 = 0; } var yDiff = val2 - val1; yDiffComponentSum += yDiff * yDiff; } var xDiffLength = XMath.Sqrt(xDiffComponentSum); var yDiffLength = XMath.Sqrt(yDiffComponentSum); output[index.X, index.Y, 0] = xDiffLength; output[index.X, index.Y, 1] = yDiffLength; output[index.X, index.Y, 2] = XMath.Max(xDiffLength, yDiffLength); }
public static float Distance(Vertex vertA, Vertex vertB) { float dx = vertA.x - vertB.x; float dy = vertA.y - vertB.y; float dz = vertA.z - vertB.z; return(XMath.Sqrt(dx * dx + dy * dy + dz * dz)); }
public float Distance(Vertex vert) { float dx = this.x - vert.x; float dy = this.y - vert.y; float dz = this.z - vert.z; return(XMath.Sqrt(dx * dx + dy * dy + dz * dz)); }
public static float dist(Vec3 v1, Vec3 v2) { float dx = v1.x - v2.x; float dy = v1.y - v2.y; float dz = v1.z - v2.z; return(XMath.Sqrt(dx * dx + dy * dy + dz * dz)); }
private static Vec3 RandomUnitVector(XorShift64Star rng) { float a = 2f * XMath.PI * rng.NextFloat(); float z = (rng.NextFloat() * 2f) - 1f; float r = XMath.Sqrt(1f - z * z); return(new Vec3(r * XMath.Cos(a), r * XMath.Sin(a), z)); }
static void GetLatLon(float line, float sample, out float latitude, out float longitude) { var x = (sample - S0) * Scale; var y = (L0 - line) * Scale; var P = XMath.Sqrt(x * x + y * y); var C = 2f * GPUHorizons.Atan2(P, 2f * MoonRadius); latitude = GPUHorizons.Asin(XMath.Cos(C) * XMath.Sin(LatP) + (y == 0 ? 0 : y * XMath.Sin(C) * XMath.Cos(LatP) / P)); longitude = LonP + GPUHorizons.Atan2(x, y * LonFactor); }
public static short Magnitude(ArrayView <short> a) { var extent = a.Extent.X; int sum = 0; for (int i = 0; i < extent; i++) { sum = (int)XMath.Pow(a[i], 2); } return((short)XMath.Sqrt(sum)); }
/// <summary> /// A simple 1D kernel using math functions. /// The GPUMath class contains intrinsic math functions that - /// in contrast to the default .Net Math class - /// work on both floats and doubles. /// Note that both classes are supported on all /// accelerators. The CompileUnitFlags.FastMath flag can be used during the creation of the /// compile unit to enable fast math intrinsics. /// </summary> /// <param name="index">The current thread index.</param> /// <param name="dataView">The view pointing to our memory buffer.</param> /// <param name="constant">A uniform constant.</param> static void MathKernel( Index index, // The global thread index (1D in this case) ArrayView <float> singleView, // A view of floats to store float results from GPUMath ArrayView <double> doubleView, // A view of doubles to store double results from GPUMath ArrayView <double> doubleView2) // A view of doubles to store double results from .Net Math { // Note the different returns type of GPUMath.Sqrt and Math.Sqrt. singleView[index] = XMath.Sqrt(index); doubleView[index] = XMath.Sqrt((double)(int)index); doubleView2[index] = Math.Sqrt(index); }
public static short Magnitude(ArrayView3D <short> a, Index2 index) { var extent = a.Depth; int sum = 0; for (int i = 0; i < extent; i++) { sum = (int)XMath.Pow(a[index.X, index.Y, i], 2); } return((short)XMath.Sqrt(sum)); }
public static Vec3 refract(Vec3 v, Vec3 n, float niOverNt) { Vec3 uv = unitVector(v); float dt = dot(uv, n); float discriminant = 1.0f - niOverNt * niOverNt * (1f - dt * dt); if (discriminant > 0) { return(niOverNt * (uv - (n * dt)) - n * XMath.Sqrt(discriminant)); } return(v); }
public static Vertex Refract(Vertex v, Vertex n, float niOverNt) { v.UnitVector_IP(); float dt = Dot(v, n); float discriminant = 1f - niOverNt * niOverNt * (1f - dt * dt); if (discriminant <= 0) { return(v); } //return niOverNt * (v - (n * dt)) - n * XMath.Sqrt(discriminant); return(niOverNt * v + n * (-(niOverNt * dt + XMath.Sqrt(discriminant)))); }
private static HitRecord GetSphereHit(Ray r, ArrayView <Sphere> spheres, float minT) { float closestT = 10000; int sphereIndex = -1; Sphere s; Vec3 oc; for (int i = 0; i < spheres.Length; i++) { s = spheres[i]; oc = r.a - s.center; float b = Vec3.dot(oc, r.b); float c = Vec3.dot(oc, oc) - s.radiusSquared; float discr = (b * b) - (c); if (discr > 0.1f) { float sqrtdisc = XMath.Sqrt(discr); float temp = (-b - sqrtdisc); if (temp < closestT && temp > minT) { closestT = temp; sphereIndex = i; } else { temp = (-b + sqrtdisc); if (temp < closestT && temp > minT) { closestT = temp; sphereIndex = i; } } } } if (sphereIndex != -1) { oc = r.pointAtParameter(closestT); s = spheres[sphereIndex]; return(new HitRecord(closestT, oc, (oc - s.center) / s.radius, r.b, s.materialIndex, sphereIndex)); } else { return(new HitRecord(float.MaxValue, new Vec3(), new Vec3(), false, -1, -1)); } }
public static Vector3 PointOnHemisphere(this XorShift64Star random, Vector3 n) { float azimuthal = random.NextFloat() * XMath.PI * 2f; float z = random.NextFloat(); float xyproj = XMath.Sqrt(1 - z * z); Vector3 v = new Vector3(xyproj * XMath.Cos(azimuthal), xyproj * XMath.Sin(azimuthal), z); if (Vector3.Dot(v, n) < 0) { return(v * -1); } else { return(v); } }
// https://developer.download.nvidia.com/cg/asin.html public static float Asin(float x) { float negate = x < 0 ? 1f : 0f; // float(x < 0); x = XMath.Abs(x); float ret = -0.0187293f; ret *= x; ret += 0.0742610f; ret *= x; ret -= 0.2121144f; ret *= x; ret += 1.5707288f; ret = 3.14159265358979f * 0.5f - XMath.Sqrt(1.0f - x) * ret; return(ret - 2 * negate * ret); }
public static float NormalReflectance(Vec3 normal, Vec3 incomming, float iorFrom, float iorTo) { float iorRatio = iorFrom / iorTo; float cosThetaI = -dot(normal, incomming); float sinThetaTSquared = iorRatio * iorRatio * (1 - cosThetaI * cosThetaI); if (sinThetaTSquared > 1) { return(1f); } float cosThetaT = XMath.Sqrt(1 - sinThetaTSquared); float rPerpendicular = (iorFrom * cosThetaI - iorTo * cosThetaT) / (iorFrom * cosThetaI + iorTo * cosThetaT); float rParallel = (iorFrom * cosThetaI - iorTo * cosThetaT) / (iorFrom * cosThetaI + iorTo * cosThetaT); return((rPerpendicular * rPerpendicular + rParallel * rParallel) / 2f); }
public static Vector3 GetGGXMicrofacet(XorShift64Star random, float roughness, Vector3 normal) { float rand1 = random.NextFloat(); float rand2 = random.NextFloat(); Vector3 B = GetPerpendicularVector(normal); Vector3 T = Vector3.Cross(B, normal); float a2 = roughness * roughness; float cosThetaH = XMath.Sqrt(XMath.Max(0f, (1f - rand1) / ((a2 - 1f) * rand1 + 1f))); float sinThetaH = XMath.Sqrt(XMath.Max(0f, 1f - cosThetaH * cosThetaH)); float phiH = rand2 * XMath.PI * 2f; return(T * (sinThetaH * XMath.Cos(phiH)) + B * (sinThetaH * XMath.Sin(phiH)) + normal * cosThetaH); }
public static void CalculateBrightnessStats(Index index, ArrayView3D <short> input, ArrayView <double> meanBrightness, ArrayView <short> maxBrightness, ArrayView <double> standartDeviation) { long sum = 0; short max = 0; var band = input.GetSliceView(index).AsLinearView(); for (int i = 0; i < band.Length; i++) { var val = band[i]; if (val < 0) { val = 0; } //for deviation and mean sum sum += val; if (val > max) { max = val; } } double mean = sum / (double)band.Length; meanBrightness[index] = mean; maxBrightness[index] = max; double dividend = 0; for (int i = 0; i < band.Length; i++) { var val = band[i]; if (val < 0) { val = 0; } dividend += XMath.Pow(XMath.Abs(val - mean), 2); } standartDeviation[index] = XMath.Sqrt(dividend / band.Length); }
public static void CorrelationMap(Index2 index, ArrayView3D <float> result, ArrayView3D <byte> bufIn) { if (index.X == bufIn.Width - 1 || index.X == 0 || index.Y == bufIn.Height - 1 || index.Y == 0) { result[index.X, index.Y, 0] = 1.0f; result[index.X, index.Y, 1] = 1.0f; result[index.X, index.Y, 2] = 1.0f; return; } var corrX = (float)XMath.Abs(Kernels.PearsonCorrelation(bufIn, new Index2(index.X - 1, index.Y), new Index2(index.X + 1, index.Y))); var corrY = (float)XMath.Abs(Kernels.PearsonCorrelation(bufIn, new Index2(index.X, index.Y - 1), new Index2(index.X, index.Y + 1))); result[index.X, index.Y, 0] = ((1.0f - corrX)); result[index.X, index.Y, 1] = (XMath.Sqrt(XMath.Pow(1.0f - corrX, 2) + XMath.Pow(1.0f - corrY, 2))); result[index.X, index.Y, 2] = ((1.0f - XMath.Min(corrX, corrY))); }
public static void StandardDeviation(Index index, ArrayView <double> result, ArrayView3D <short> input) { long sum = 0; var band = input.GetSliceView(index).AsLinearView(); for (int i = 0; i < band.Length; i++) { sum += band[i]; } double mean = sum / band.Length; double dividend = 0; for (int i = 0; i < band.Length; i++) { dividend += XMath.Pow(band[i] - mean, 2); } result[index] = XMath.Sqrt(dividend / band.Length); }
public static float Schlick(Vector3 nrm, Vector3 dir, float n1, float n2) { float r0 = (n1 - n2) / (n1 + n2); r0 *= r0; float cosI = XMath.Max(Vector3.Dot(-nrm, dir), 0f); if (n1 > n2) { float n = n1 / n2; float sinT2 = n * n * (1f - cosI * cosI); if (sinT2 > 1.0) { return(1f); } cosI = XMath.Sqrt(1f - sinT2); } float x = 1f - cosI; return(r0 + (1f - r0) * x * x * x * x * x); }
public Vector3 GetIndirectDiffuse2(XorShift64Star random, Scene scene, Vector3 position, Vector3 normal) { Vector3 lightAccumulator = new Vector3(); int samples = 4; for (int i = 0; i < samples; i++) { float azimuthal = random.NextFloat() * XMath.PI * 2f; float z = random.NextFloat(); float xyproj = XMath.Sqrt(1 - z * z); Vector3 dir = new Vector3(xyproj * XMath.Cos(azimuthal), xyproj * XMath.Sin(azimuthal), z); if (Vector3.Dot(dir, normal) < 0) { dir *= -1; } Ray ray = new Ray(position + dir * 0.002f, dir); Intersection intersection = scene.TraceScene(ray); if (!intersection.Hit && !intersection.HitLight) { lightAccumulator += Vector3.Multiply(scene.SkylightColor * scene.SkylightBrightness * Vector3.Dot(dir, normal), DiffuseColor) * Diffuse; continue; } else if (!intersection.Hit || intersection.HitLight) { continue; } Material hitMaterial = intersection.HitObject.Material; Vector3 lightC = hitMaterial.GetShaded3(random, scene, intersection.HitPosition, dir, intersection.HitNormal); lightC *= Vector3.Dot(dir, normal); lightAccumulator += Vector3.Multiply(lightC, DiffuseColor) * Diffuse; } lightAccumulator /= (float)samples; return(lightAccumulator); }
public static IntersectionResult[] Intersect(ref Ray ray, ref Sphere sphere) { var sphereToRay = ray.Origin - sphere.Origin; // normalize direction before passing it to intersect? var a = Vector3.Dot(ray.Direction, ray.Direction); var b = Vector3.Dot(ray.Direction, sphereToRay) * 2; var c = Vector3.Dot(sphereToRay, sphereToRay) - 1; var discriminant = b * b - 4 * a * c; if (discriminant < 0) { return(new IntersectionResult[0]); } else { return(new IntersectionResult[2] { new IntersectionResult((-b - XMath.Sqrt(discriminant)) / (2 * a), ref sphere), new IntersectionResult((-b + XMath.Sqrt(discriminant)) / (2 * a), ref sphere) }); } }
public static Vec3 unitVector(Vec3 v) { return(v / XMath.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z)); }
public static float Rsqrt(float value) => XMath.Rcp(XMath.Sqrt(value));
public float length() { return(XMath.Sqrt(x * x + y * y + z * z)); }
// Methods (non-static) public float Magnitude() { return(XMath.Sqrt(x * x + y * y + z * z)); }
static void NearFieldKernel1( Index2 index, // Contains (sample, line) First dimension changes fastest int target_line, // row within the 128 x 128 target patch int target_sample, // column int slope_idx_start, int slope_idx_stop, ArrayView <short> gpu_terrain, ArrayView <float> gpu_range, ArrayView <float> gpu_slope, ArrayView <float> gpu_rise, ArrayView <int> gpu_range_limit) { var flip = -LonFactor; // From ILGPU source code: public int ComputeLinearIndex(Index2 dimension) => Y * dimension.X + X; var rise_idx = index.ComputeLinearIndex(Dimension); var rise = gpu_rise[rise_idx]; var center_line = target_line + index.Y; var center_sample = target_sample + index.X; var center_idx = center_line * TerrainSize + center_sample; var center_height = 0.5f * gpu_terrain[center_idx]; GetVector3d(center_line, center_sample, center_height, out float center_x, out float center_y, out float center_z); center_z *= flip; for (var slope_idx = slope_idx_start; slope_idx < slope_idx_stop; slope_idx++) { var slope = gpu_slope[slope_idx]; // Work out the direction vector var ray_rad = XMath.PI * 2f * slope_idx / cpu_slope_size; // 0 deg in ME frame points toward the earth var ray_cos = XMath.Cos(ray_rad); // varies the line var ray_sin = XMath.Sin(ray_rad); // varies the sample // iterate over the ranges var range_limit = gpu_range_limit[slope_idx]; for (var range_idx = 0; range_idx < range_limit; range_idx++) { var range = gpu_range[range_idx]; var caster_line = center_line + ray_sin * range; var caster_sample = center_sample + ray_cos * range; var x1 = (int)caster_sample; // Calculate the caster point by interpolating between four points from the points array var y1 = (int)caster_line; int x2 = x1 + 1; int y2 = y1 + 1; var q11_idx = y1 * TerrainSize + x1; var q11 = gpu_terrain[q11_idx]; var q12_idx = q11_idx + TerrainSize; var q12 = gpu_terrain[q12_idx]; // First interpolation across rows (line) var q1_line = q11 + (caster_line - y1) * (q12 - q11); var q21_idx = q11_idx + 1; var q21 = gpu_terrain[q21_idx]; var q22_idx = q11_idx + TerrainSize + 1; var q22 = gpu_terrain[q22_idx]; // Second interpolation across rows var q2_line = q21 + (caster_line - y1) * (q22 - q21); // Interpolate across samples and convert to meters var caster_height = q1_line + (caster_sample - x1) * (q2_line - q1_line); caster_height *= 0.5f; GetVector3d(caster_line, caster_sample, caster_height, out float caster_x, out float caster_y, out float caster_z); caster_z *= flip; var dx = caster_x - center_x; var dy = caster_y - center_y; var d = XMath.Sqrt(dx * dx + dy * dy); // horizontal distance in moon radius units var light_ray_height = caster_z - slope * d; // negative slope gets higher as light ray goes toward the center var ray_rise_height = light_ray_height - center_z; // moon radius units var ray_rise_meters = ray_rise_height * 1000f; // Alternative //var dInMeters = d * 1000f; //var deltaHeightInMeters = (caster_z - center_z) * 1000f; //var rise2 = deltaHeightInMeters - dInMeters * slope; rise = XMath.Max(rise, ray_rise_meters); } } gpu_rise[rise_idx] = rise; }
public float Std() { return(XMath.Sqrt(this.Var())); }