public static double Asin(double value) { if (XMath.IsNaN(value) || value < -1.0 || value > 1.0) { return(double.NaN); } if (value == 1.0) { return(XMath.PIHalfD); } else if (value == -1.0) { return(-XMath.PIHalfD); } double arg = value * Rsqrt(1.0 - value * value); return(Atan(arg)); }
public static float Asin(float value) { if (XMath.IsNaN(value) || value < -1.0f || value > 1.0f) { return(float.NaN); } if (value == 1.0f) { return(XMath.PIHalf); } else if (value == -1.0f) { return(-XMath.PIHalf); } float arg = value * Rsqrt(1.0f - value * value); return(Atan(arg)); }
public void Body_Moving(MovableBody body) { if (IsDead) { return; } var dt = Time.fixedDeltaTime; var velocity = body.Velocity; var vyMax = Data.GetMoveVerMax(_diver.IsActive); var ay = -GetGravity(); var targetVx = GetTargetVelocityX(); var hacc = GetHorizontalAcceleration(); velocity.x = XMath.AddByMod(velocity.x, targetVx, hacc * dt); velocity.y = GetVelocityY(dt, ay, velocity.y); if (velocity.y > vyMax) { velocity.y = vyMax; } body.Velocity = velocity; }
public Ray GetRaySimple(float x, float y) { x = (x / (ScreenWidth - 1f) - 0.5f) * (ScreenWidth / ScreenHeight); y = (1f - y / (ScreenHeight - 1f)) - 0.5f; Vector3 direction = Vector3.Normalize(new Vector3(x * 36f, y * 36f, FocalLength)); float angle = -sphericalPosition.Y + XMath.PIHalf; float newY = direction.Y * XMath.Cos(angle) - direction.Z * XMath.Sin(angle); float newZ = direction.Z * XMath.Cos(angle) - direction.Y * XMath.Sin(angle); direction.Y = newY; direction.Z = newZ; angle = sphericalPosition.Z + XMath.PIHalf; float newX = direction.X * XMath.Cos(angle) - direction.Z * XMath.Sin(angle); newZ = direction.Z * XMath.Cos(angle) + direction.X * XMath.Sin(angle); direction.X = newX; direction.Z = newZ; return(new Ray(Position, direction)); }
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 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); }
private static void ShrinkKernel(Index2 index, ArrayView2D <float> xImage, ArrayView2D <float> bMap, ArrayView2D <float> aMap, ArrayView <float> lambdaAlpha, ArrayView <Pixel> output) { if (index.X == 0 & index.Y == 0) { output[0].AbsDiff = 0; } if (index.InBounds(xImage.Extent)) { var xOld = xImage[index]; var gradient = bMap[index]; var lipschitz = aMap[index]; var lambda = lambdaAlpha[0]; var alpha = lambdaAlpha[1]; var xNew = GPUProximalOperator(xOld * lipschitz + gradient, lipschitz, lambda, alpha); var xAbsDiff = XMath.Abs(xNew - xOld); var xIndex = index.X; var yIndex = index.Y; var sign = XMath.Sign(xNew - xOld); var pix = new Pixel() { AbsDiff = xAbsDiff, X = xIndex, Y = yIndex, Sign = sign }; Atomic.MakeAtomic(ref output[0], pix, new MaxPixelOperation(), new PixelCompareExchange()); } }
public float getDiractionToAchieveAngle(float inAngleToAchieve) { float theCurrentAngle = getAngle(); float theAngleToAchieve = XMath.getNormalizedAngle(inAngleToAchieve); if (Mathf.Approximately(theCurrentAngle, theAngleToAchieve)) return 0.0f; if (isAngleAchievable(theAngleToAchieve)) { float theNearestDiraction = XMath.getNormalizedAngle(theAngleToAchieve - theCurrentAngle); if (isUnlimited()) return theNearestDiraction > 0.0f ? 1.0f : -1.0f; if (theNearestDiraction > 0.0f) { float theTestAngle = XMath.getNormalizedAnglesClockwiseDelta(getLimitFrom(), theCurrentAngle); theTestAngle += XMath.getNormalizedAnglesClockwiseDelta(theCurrentAngle, theAngleToAchieve); return theTestAngle < getLimitingAngle() || Mathf.Approximately(theTestAngle, getLimitingAngle()) ? 1.0f : -1.0f; } else { float theTestAngle = XMath.getNormalizedAnglesClockwiseDelta(getLimitFrom(), theCurrentAngle); theTestAngle -= XMath.getNormalizedAnglesClockwiseDelta(getLimitFrom(), theAngleToAchieve); return theTestAngle > 0.0f || Mathf.Approximately(theTestAngle, 0.0f) ? -1.0f : 1.0f; } } else { return getDiractionToAchieveAngle(getNearestLimitForAngle(inAngleToAchieve)); } }
// Methods (non-static) public float Magnitude() { return(XMath.Sqrt(x * x + y * y + z * z)); }
//-Implementation //--Utils private void normalizeState() { _state.LimitFrom = XMath.getNormalizedAngle(_state.LimitFrom); _state.LimitTo = XMath.getNormalizedAngle(_state.LimitTo); setAngle(_state.Angle); }
//-Utils public bool isAngleAchievable(float inAngle) { float theNormalizedAngle = XMath.getNormalizedAngle(inAngle); float theDelta = XMath.getNormalizedAnglesClockwiseDelta(getLimitFrom(), theNormalizedAngle); return theDelta < getLimitingAngle() || Mathf.Approximately(theDelta, getLimitingAngle()); }
void FixedUpdate() { if (!_targetObject) { return; } float theTargetAngle = XMath.getNearestAngleBetweenPoints( transform.position, _targetObject.transform.position ); float theCurrentAngle = transform.rotation.eulerAngles.z; float theNearestDelta = XMath.getNormalizedAngle(theTargetAngle - theCurrentAngle); if (XMath.equalsWithPrecision(theNearestDelta, 0.0f, 30.0f)) { _carPhysics.applyGas(); } else { float theDistanceSquare = _euristicDistanceToMakeSpeedLessIfFar * _euristicDistanceToMakeSpeedLessIfFar; Vector2 theDelta = _targetObject.transform.position - transform.position; if (theDelta.sqrMagnitude < theDistanceSquare) { if (_carPhysics.getGasValue().getValue() > _minimumGas) { _carPhysics.applyRevers(); } else { _carPhysics.applyGas(); } } else { _carPhysics.applyGas(); } } bool theIsNeedMoveByClockwiseToAchieveTargetAngle = (theNearestDelta > 0.0f); bool theItsTimeToCorrectWheels = false; _simulationManager.simulate(gameObject, 50, (ISimulatableLogic inLogic) => { var theLogic = inLogic as CarPhysicsLogic; if (theIsNeedMoveByClockwiseToAchieveTargetAngle) { theLogic.rotateSteeringWheelCounterClockwise(); if (!theLogic.isRotatingByClockwice()) { return(false); } } else { theLogic.rotateSteeringWheelClockwise(); if (theLogic.isRotatingByClockwice()) { return(false); } } float theSimulationCurrentAngle = theLogic.transform.rotation.eulerAngles.z; float theSimulationNearestDelta = XMath.getNormalizedAngle(theTargetAngle - theSimulationCurrentAngle); if (!XMath.hasSameSigns(theNearestDelta, theSimulationNearestDelta)) { theItsTimeToCorrectWheels = true; return(false); } //theLogic.debugDraw(); return(true); }); if (theIsNeedMoveByClockwiseToAchieveTargetAngle) { if (theItsTimeToCorrectWheels) { _carPhysics.rotateSteeringWheelCounterClockwise(); } else { _carPhysics.rotateSteeringWheelClockwise(); } } else { if (theItsTimeToCorrectWheels) { _carPhysics.rotateSteeringWheelClockwise(); } else { _carPhysics.rotateSteeringWheelCounterClockwise(); } } }
private static bool IsOddInteger(float value) { var remainder = XMath.Abs(value) % 2.0f; return(remainder == 1.0f); }
public float length() { 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; }
static double Evaluate(int individualIndex, int independentsRowIndex, ArrayView2D <double> independents, ArrayView <NodeGPU> nodes, ArrayView <int> nodeArrayStarts) { for (int nodeIndex = 0; nodeIndex < nodeArrayStarts.Length; nodeIndex++) { Index1 currentNodeIndex = new Index1(nodeArrayStarts[individualIndex] + nodeIndex); //NodeGPU currentNode = nodes[currentNodeIndex]; if (nodes[currentNodeIndex].IndependentIndex >= 0) { int independentIndex = nodes[currentNodeIndex].IndependentIndex; nodes[currentNodeIndex].Number = independents[independentsRowIndex, independentIndex]; } else if (nodes[currentNodeIndex].OperatorIndex >= 0) { Index1 branchIndex1 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch1); Index1 branchIndex2 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch2); if (nodes[currentNodeIndex].OperatorIndex < 6) { if (nodes[currentNodeIndex].OperatorIndex < 4) { if (nodes[currentNodeIndex].OperatorIndex == 2) { nodes[currentNodeIndex].Number = nodes[branchIndex1].Number + nodes[branchIndex2].Number; } else if (nodes[currentNodeIndex].OperatorIndex == 3) { nodes[currentNodeIndex].Number = nodes[branchIndex1].Number - nodes[branchIndex2].Number; } } else { if (nodes[currentNodeIndex].OperatorIndex == 4) { nodes[currentNodeIndex].Number = nodes[branchIndex1].Number * nodes[branchIndex2].Number; } else if (nodes[currentNodeIndex].OperatorIndex == 5) { nodes[currentNodeIndex].Number = nodes[branchIndex1].Number / nodes[branchIndex2].Number; } } } else if (nodes[currentNodeIndex].OperatorIndex >= 6 && nodes[currentNodeIndex].OperatorIndex <= 15) { if (nodes[currentNodeIndex].OperatorIndex == 6) { nodes[currentNodeIndex].Number = -nodes[branchIndex1].Number; } else if (nodes[currentNodeIndex].OperatorIndex == 8) { nodes[currentNodeIndex].Number = XMath.Sin(nodes[branchIndex1].Number); } else if (nodes[currentNodeIndex].OperatorIndex == 9) { nodes[currentNodeIndex].Number = XMath.Cos(nodes[branchIndex1].Number); } else if (nodes[currentNodeIndex].OperatorIndex == 14) { nodes[currentNodeIndex].Number = XMath.Pow(nodes[branchIndex1].Number, nodes[branchIndex2].Number); } else if (nodes[currentNodeIndex].OperatorIndex == 15) { nodes[currentNodeIndex].Number = XMath.Sign(nodes[branchIndex1].Number); } } else { Index1 branchIndex3 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch3); Index1 branchIndex4 = new Index1(nodeArrayStarts[individualIndex] + nodes[currentNodeIndex].Branch4); if (nodes[currentNodeIndex].OperatorIndex == 18) { if (nodes[branchIndex1].Number == nodes[branchIndex2].Number) { nodes[currentNodeIndex].Number = nodes[branchIndex3].Number; } else { nodes[currentNodeIndex].Number = nodes[branchIndex4].Number; } } else if (nodes[currentNodeIndex].OperatorIndex == 19) { if (nodes[branchIndex1].Number < nodes[branchIndex2].Number) { nodes[currentNodeIndex].Number = nodes[branchIndex3].Number; } else { nodes[currentNodeIndex].Number = nodes[branchIndex4].Number; } } else if (nodes[currentNodeIndex].OperatorIndex == 20) { if (nodes[branchIndex1].Number <= nodes[branchIndex2].Number) { nodes[currentNodeIndex].Number = nodes[branchIndex3].Number; } else { nodes[currentNodeIndex].Number = nodes[branchIndex4].Number; } } else if (nodes[currentNodeIndex].OperatorIndex == 21) { if (nodes[branchIndex1].Number == 0) { nodes[currentNodeIndex].Number = nodes[branchIndex2].Number; } else { nodes[currentNodeIndex].Number = nodes[branchIndex3].Number; } } else if (nodes[currentNodeIndex].OperatorIndex == 22) { if (nodes[branchIndex1].Number == 1) { nodes[currentNodeIndex].Number = nodes[branchIndex2].Number; } else { nodes[currentNodeIndex].Number = nodes[branchIndex3].Number; } } } if (nodes[currentNodeIndex].Number == double.NaN) { return(double.NaN); } } if (nodes[currentNodeIndex].IsRoot == 1) { return(nodes[currentNodeIndex].Number); } } return(double.NaN); }
/// <summary> /// A custom kernel using <see cref="XMath"/> functions. /// </summary> public static void KernelWithXMath(Index1D index, ArrayView <float> data, float c) { data[index] = XMath.Sinh(c + index) + XMath.Atan(c); }
public static float Log10(float value) => XMath.Log(value) * XMath.OneOverLn10;
public static float Log(float value) => XMath.Log2(value) * XMath.OneOverLog2E;
public static float Exp(float value) => XMath.Exp2(value * XMath.OneOverLn2);
private Vector2 calculateVelocityDiraction() { float theRotation = transform.rotation.eulerAngles.z; return(XMath.getDiractionVectorForAngle(theRotation)); }
public static float Pow(float @base, float exp) { if (exp == 0.0f) { // Rule #2 // Ignoring Rule #1 return(1.0f); } else if (@base == 1.0f) { // Rule #14 but ignoring second part about y = NaN // Ignoring Rule #1 return(1.0f); } else if (XMath.IsNaN(@base) || XMath.IsNaN(exp)) { // Rule #1 return(float.NaN); } else if (@base == float.NegativeInfinity) { if (exp < 0.0f) { // Rule #3 return(0.0f); } else if (IsOddInteger(exp)) { // Rule #4 return(float.NegativeInfinity); } else { // Rule #5 return(float.PositiveInfinity); } } else if (@base < 0.0f && !IsInteger(exp) && !XMath.IsInfinity(exp)) { // Rule #6 return(float.NaN); } else if (@base == -1.0f && XMath.IsInfinity(exp)) { // Rule #7 return(1.0f); } else if (-1.0f < @base && @base < 1.0f && exp == float.NegativeInfinity) { // Rule #8 return(float.PositiveInfinity); } else if (-1.0f < @base && @base < 1.0f && exp == float.PositiveInfinity) { // Rule #9 return(0.0f); } else if ((@base < -1.0f || @base > 1.0f) && exp == float.NegativeInfinity) { // Rule #10 return(0.0f); } else if ((@base < -1.0f || @base > 1.0f) && exp == float.PositiveInfinity) { // Rule #11 return(float.PositiveInfinity); } else if (@base == 0.0f) { if (exp < 0.0f) { // Rule #12 return(float.PositiveInfinity); } else { // Rule #13 // NB: exp == 0.0 already handled by Rule #2 return(0.0f); } } else if (@base == float.PositiveInfinity) { if (exp < 0.0f) { // Rule #15 return(0.0f); } else { // Rule #16 // NB: exp == 0.0 already handled by Rule #2 return(float.PositiveInfinity); } } if (@base < 0.0) { // 'exp' is an integer, due to Rule #6. var sign = IsOddInteger(exp) ? -1.0f : 1.0f; return(sign * Exp(exp * Log(XMath.Abs(@base)))); } return(Exp(exp * Log(@base))); }
public float Std() { return(XMath.Sqrt(this.Var())); }
public float getLimitingAngle() { return XMath.getNormalizedAnglesClockwiseDelta(getLimitFrom(), getLimitTo()); }
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 float setAngle(float inAngle) { float theNormalizedAngle = XMath.getNormalizedAngle(inAngle); return _state.Angle = isAngleAchievable(theNormalizedAngle) ? theNormalizedAngle : getNearestLimitForAngle(theNormalizedAngle); }
public static AABB surrounding_box(AABB box0, AABB box1) { Vec3 small = new Vec3(XMath.Min(box0.min.x, box1.min.x), XMath.Min(box0.min.y, box1.min.y), XMath.Min(box0.min.z, box1.min.z)); Vec3 big = new Vec3(XMath.Max(box0.max.x, box1.max.x), XMath.Max(box0.max.y, box1.max.y), XMath.Max(box0.max.z, box1.max.z)); return(new AABB(small, big)); }
private static bool IsOddInteger(double value) { var remainder = XMath.Abs(value) % 2.0; return(remainder == 1.0); }
public static Vec3 unitVector(Vec3 v) { return(v / XMath.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z)); }
public float getValuePercenFromMaximum() { return XMath.getValueRatioInRange( getMinimum(), getMinimum(), getValueFromMaximum() ); }