Ejemplo n.º 1
0
        /// <summary>
        ///     Create keys at timestamps.
        /// </summary>
        /// <param name="curve"></param>
        private void UpdateCurveWithAddedKeys(
            AnimationCurve curve,
            List <float> timestamps)
        {
            var curveTimestamps = new float[curve.length];

            for (var i = 0; i < curve.length; i++)
            {
                curveTimestamps[i] = curve.keys[i].time;
            }

            // For each timestamp..
            for (int i = 0; i < timestamps.Count; i++)
            {
                var keyExists = curveTimestamps.Any(
                    t => Utilities.FloatsEqual(
                        timestamps[i],
                        t,
                        GlobalConstants.FloatPrecision));

                // Add missing key.
                if (keyExists)
                {
                    continue;
                }

                AddKeyToCurve(curve, timestamps[i]);
            }
        }
        public void IChannelAudioVolume_SetAllVolumes()
        {
            ExecuteServiceTest(service =>
            {
                UInt32 channelCount;
                service.GetChannelCount(out channelCount);

                float[] valOrig = new float[channelCount];
                service.GetAllVolumes(channelCount, valOrig);

                // Set to test values.
                var context = Guid.NewGuid();
                var result  = service.SetAllVolumes(channelCount, Enumerable.Repeat(0.5f, (int)channelCount).ToArray(), context);
                AssertCoreAudio.IsHResultOk(result);

                // Check new values.
                float[] valNew = new float[channelCount];
                service.GetAllVolumes(channelCount, valNew);
                Assert.IsFalse(valNew.Any(val => val != 0.5f), "One or more volumes were not set.");

                // Return to original value.
                result = service.SetAllVolumes(channelCount, valOrig, context);
                AssertCoreAudio.IsHResultOk(result);
            });
        }
Ejemplo n.º 3
0
        // Thanks to Saeed Amiri (https://stackoverflow.com/questions/4243042/c-sharp-point-in-polygon)
        /// <summary>
        /// Define polygon with points CW or CCW, works with convex polygons
        /// </summary>
        public static bool IsInPolygon(this Vector3 point, Vector3[] poly)
        {
            float[] coef = new float[poly.Length];
            for (int i = 0; i < poly.Length; i++)
            {
                Vector3 prev = poly[i == 0 ? poly.Length - 1 : i - 1];
                Vector3 cur  = poly[i];
                coef[i] = (point.y - cur.y) * (prev.x - cur.x)
                          - (point.x - cur.x) * (prev.y - cur.y);
            }

            if (coef.Any(p => Math.Abs(p) < Single.Epsilon))
            {
                return(true);
            }

            for (int i = 1; i < coef.Length; i++)
            {
                if (coef[i] * coef[i - 1] < 0)
                {
                    return(false);
                }
            }

            return(true);
        }
    public static IEnumerable <Vector2> plotLine(Vector2 start, Vector2 end)
    {
        var components = new float[] { start.X, start.Y, end.X, end.Y };
        var NaN        = components.Any(x => float.IsNaN(x));

        if (NaN)
        {
            return(new Vector2[0]);
        }

        var output     = new List <Vector2>();
        var DX         = end.X - start.X;
        var DY         = end.Y - start.Y;
        var diff       = 2 * DY - DX;
        var yIncrement = 1;

        if (DY < 0)
        {
            yIncrement = -1;
            DY         = -DY;
        }

        output.Add(new Vector2(start.X, start.Y));

        var j = start.Y;

        for (var i = start.X; i < end.X; i++)
        {
            if (diff < 0)
            {
                output.Add(new Vector2(i, j));
                diff = diff + 2 * DY;
            }
            //increment y
            else
            {
                j = j + yIncrement;
                output.Add(new Vector2(i, j));
                diff = diff + 2 * DY - 2 * DX;
            }
        }

        return(output);
    }
Ejemplo n.º 5
0
        public static Color ParseColor(string stringColor, bool errorMessages = true)
        {
            string[] strComponents = stringColor.Split(',');

            Color color = Color.White;

            if (strComponents.Length < 3)
            {
                if (errorMessages)
                {
                    DebugConsole.ThrowError("Failed to parse the string \"" + stringColor + "\" to Color");
                }
                return(Color.White);
            }

            float[] components = new float[4] {
                1.0f, 1.0f, 1.0f, 1.0f
            };

            for (int i = 0; i < 4 && i < strComponents.Length; i++)
            {
                float.TryParse(strComponents[i], NumberStyles.Float, CultureInfo.InvariantCulture, out components[i]);
            }

            if (components.Any(c => c > 1.0f))
            {
                for (int i = 0; i < 4; i++)
                {
                    components[i] = components[i] / 255.0f;
                }
                //alpha defaults to 255 if not given
                if (strComponents.Length < 4)
                {
                    components[3] = 255;
                }
            }

            return(new Color(components[0], components[1], components[2], components[3]));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Find the centers of stars in an image
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        static List <StarInfo> FindStarCenters(ushort[] image, int width, int height, double peakMin)
        {
            var results = new List <StarInfo>();

            for (var y = 0; y < height - 3; y++)
            {
                for (var x = 0; x < width - 3; x++)
                {
                    var center = image[(y + 1) * width + x + 1];

                    if (center < peakMin)
                    {
                        continue;
                    }

                    var surrounds = new float[]
                    {
                        image[(y + 0) * width + x + 0],
                        image[(y + 0) * width + x + 1],
                        image[(y + 0) * width + x + 2],
                        image[(y + 1) * width + x + 0],
                        image[(y + 1) * width + x + 2],
                        image[(y + 2) * width + x + 0],
                        image[(y + 2) * width + x + 1],
                        image[(y + 2) * width + x + 2]
                    };

                    if (!surrounds.Any(v => v >= center || v == 0))
                    {
                        results.Add(new StarInfo {
                            X = x + 1, Y = y + 1, Peak = center
                        });
                    }
                }
            }

            return(results);
        }
Ejemplo n.º 7
0
        public static bool TryDispenseCustomFood(ref Thing __result, Building_NutrientPasteDispenser __instance, ref List <IntVec3> ___cachedAdjCellsCardinal)
        {
            if (__instance.def.HasModExtension <NutrientPasteCustom>())
            {
                if (!__instance.CanDispenseNow)
                {
                    __result = null;
                    return(false);
                }
                List <ThingDef> list = new List <ThingDef>();
                List <IngredientAndCostClass> ingredientList = __instance.def.GetModExtension <NutrientPasteCustom>().ingredientList;
                bool empty = !__instance.def.GetModExtension <NutrientPasteCustom>().ingredientList.Any();
                Log.Message("-" + empty);
                if (!empty)
                {
                    float[] nutritionLeft = new float[ingredientList.Count];
                    for (int i = 0; i < nutritionLeft.Length; i++)
                    {
                        nutritionLeft[i] = ingredientList[i].nutritionCost;
                    }
                    for (; ;)
                    {
                        Thing thing = __instance.def.GetModExtension <NutrientPasteCustom>().FindNextIngredientInHopper(___cachedAdjCellsCardinal, __instance, nutritionLeft);
                        if (thing is null)
                        {
                            break;
                        }
                        int index = ingredientList.FindIndex(x => x.thingDef == thing.def);
                        int num2  = Mathf.Min(thing.stackCount, Mathf.CeilToInt(nutritionLeft[index] / thing.GetStatValue(StatDefOf.Nutrition, true)));
                        nutritionLeft[index] -= (float)num2 * thing.GetStatValue(StatDefOf.Nutrition, true);
                        list.Add(thing.def);
                        thing.SplitOff(num2);
                        if (!nutritionLeft.Any(x => x > 0f))
                        {
                            goto Block_3;
                        }
                    }
                }
                else
                {
                    Log.Message("2");
                    float num = __instance.def.building.nutritionCostPerDispense - 0.0001f;
                    for (; ;)
                    {
                        Thing thing = __instance.FindFeedInAnyHopper();
                        if (thing is null)
                        {
                            break;
                        }
                        int num2 = Mathf.Min(thing.stackCount, Mathf.CeilToInt(num / thing.GetStatValue(StatDefOf.Nutrition, true)));
                        num -= (float)num2 * thing.GetStatValue(StatDefOf.Nutrition, true);
                        list.Add(thing.def);
                        thing.SplitOff(num2);
                        if (num <= 0f)
                        {
                            goto Block_3;
                        }
                    }
                }

                Log.Error("Did not find enough food in hoppers while trying to dispense.", false);
                __result = null;
                return(false);

Block_3:
                __instance.def.building.soundDispense.PlayOneShot(new TargetInfo(__instance.Position, __instance.Map, false));
                Thing           thing2          = ThingMaker.MakeThing(__instance.def.GetModExtension <NutrientPasteCustom>().customMeal, null);
                CompIngredients compIngredients = thing2.TryGetComp <CompIngredients>();
                foreach (ThingDef ingredient in list)
                {
                    if (!__instance.def.GetModExtension <NutrientPasteCustom>().mysteryIngredients)
                    {
                        compIngredients.RegisterIngredient(ingredient);
                    }
                }
                __result = thing2;
                return(false);
            }
            return(true);
        }
Ejemplo n.º 8
0
        public static Color ParseColor(string stringColor, bool errorMessages = true)
        {
            if (stringColor.StartsWith("gui.", StringComparison.OrdinalIgnoreCase))
            {
#if CLIENT
                if (GUI.Style != null)
                {
                    string colorName = stringColor.Substring(4);
                    var    property  = GUI.Style.GetType().GetProperties().FirstOrDefault(
                        p => p.PropertyType == typeof(Color) &&
                        p.Name.Equals(colorName, StringComparison.OrdinalIgnoreCase));
                    if (property != null)
                    {
                        return((Color)property?.GetValue(GUI.Style));
                    }
                }
#endif
                return(Color.White);
            }


            string[] strComponents = stringColor.Split(',');

            Color color = Color.White;

            float[] components = new float[4] {
                1.0f, 1.0f, 1.0f, 1.0f
            };

            if (strComponents.Length == 1)
            {
                bool hexFailed = true;
                stringColor = stringColor.Trim();
                if (stringColor.Length > 0 && stringColor[0] == '#')
                {
                    stringColor = stringColor.Substring(1);

                    int colorInt = 0;
                    if (int.TryParse(stringColor, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out colorInt))
                    {
                        if (stringColor.Length == 6)
                        {
                            colorInt = (colorInt << 8) | 0xff;
                        }
                        components[0] = ((float)((colorInt & 0xff000000) >> 24)) / 255.0f;
                        components[1] = ((float)((colorInt & 0x00ff0000) >> 16)) / 255.0f;
                        components[2] = ((float)((colorInt & 0x0000ff00) >> 8)) / 255.0f;
                        components[3] = ((float)(colorInt & 0x000000ff)) / 255.0f;

                        hexFailed = false;
                    }
                }

                if (hexFailed)
                {
                    if (errorMessages)
                    {
                        DebugConsole.ThrowError("Failed to parse the string \"" + stringColor + "\" to Color");
                    }
                    return(Color.White);
                }
            }
            else
            {
                for (int i = 0; i < 4 && i < strComponents.Length; i++)
                {
                    float.TryParse(strComponents[i], NumberStyles.Float, CultureInfo.InvariantCulture, out components[i]);
                }

                if (components.Any(c => c > 1.0f))
                {
                    for (int i = 0; i < 4; i++)
                    {
                        components[i] = components[i] / 255.0f;
                    }
                    //alpha defaults to 1.0 if not given
                    if (strComponents.Length < 4)
                    {
                        components[3] = 1.0f;
                    }
                }
            }

            return(new Color(components[0], components[1], components[2], components[3]));
        }
Ejemplo n.º 9
0
        private List <float[]> ReadDataSetToSingle(AbstractWarpDataset srcbandpro, int[] bands)
        {
            List <float[]> datas    = new List <float[]>();
            var            prjBands = PrjBandTable.GetPrjBands(srcbandpro);
            H5ID           h5FileId = H5F.open(srcbandpro.fileName, H5F.ACC_RDONLY);

            foreach (int index in bands)
            {
                //Single[] data = new Single[srcSize.Width * srcSize.Height];
                var    bandIndex = prjBands[index - 1].DataSetIndex;
                string dsName    = "CALChannel" + bandIndex.ToString("00");


                H5ID datasetId = H5D.open(h5FileId, dsName);
                if (datasetId <= 0)
                {
                    throw new ArgumentNullException(string.Format("FY4辐射定标,未找到名称为{0}的数据.",
                                                                  "CALChannel" + index.ToString("00")));
                }
                H5ID typeId  = H5D.get_type(datasetId);
                H5ID spaceId = H5D.get_space(datasetId);
                if (H5T.get_class(typeId) == H5T.class_t.FLOAT)
                {
                    int     rank    = H5S.get_simple_extent_ndims(spaceId);
                    ulong[] dims    = new ulong[rank];
                    ulong[] maxDims = new ulong[rank];
                    H5S.get_simple_extent_dims(spaceId, dims, maxDims);

                    float[]  buffer = new float[dims[0]];
                    GCHandle hnd    = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    H5D.read(datasetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, hnd.AddrOfPinnedObject());
                    hnd.Free();
                    if (buffer.Any(t => t > Math.Pow(10, 10) || t < -Math.Pow(10, 10)))
                    {
                        for (int i = 0; i < buffer.Length; i++)
                        {
                            var t = BitConverter.GetBytes(buffer[i]);
                            Array.Reverse(t);
                            buffer[i] = BitConverter.ToSingle(t, 0);
                        }
                    }

                    datas.Add(buffer);
                }

                if (spaceId != 0)
                {
                    H5S.close(spaceId);
                }
                if (typeId != 0)
                {
                    H5T.close(typeId);
                }
                if (datasetId != 0)
                {
                    H5D.close(datasetId);
                }
            }

            if (h5FileId != 0)
            {
                H5F.close(h5FileId);
            }

            return(datas);
        }
Ejemplo n.º 10
0
        public static Color ParseColor(string stringColor, bool errorMessages = true)
        {
            string[] strComponents = stringColor.Split(',');

            Color color = Color.White;

            float[] components = new float[4] {
                1.0f, 1.0f, 1.0f, 1.0f
            };

            if (strComponents.Length == 1)
            {
                bool hexFailed = true;
                stringColor = stringColor.Trim();
                if (stringColor.Length > 0 && stringColor[0] == '#')
                {
                    stringColor = stringColor.Substring(1);

                    int colorInt = 0;
                    if (int.TryParse(stringColor, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out colorInt))
                    {
                        if (stringColor.Length == 6)
                        {
                            colorInt = (colorInt << 8) | 0xff;
                        }
                        components[0] = ((float)((colorInt & 0xff000000) >> 24)) / 255.0f;
                        components[1] = ((float)((colorInt & 0x00ff0000) >> 16)) / 255.0f;
                        components[2] = ((float)((colorInt & 0x0000ff00) >> 8)) / 255.0f;
                        components[3] = ((float)(colorInt & 0x000000ff)) / 255.0f;

                        hexFailed = false;
                    }
                }

                if (hexFailed)
                {
                    if (errorMessages)
                    {
                        DebugConsole.ThrowError("Failed to parse the string \"" + stringColor + "\" to Color");
                    }
                    return(Color.White);
                }
            }
            else
            {
                for (int i = 0; i < 4 && i < strComponents.Length; i++)
                {
                    float.TryParse(strComponents[i], NumberStyles.Float, CultureInfo.InvariantCulture, out components[i]);
                }

                if (components.Any(c => c > 1.0f))
                {
                    for (int i = 0; i < 4; i++)
                    {
                        components[i] = components[i] / 255.0f;
                    }
                    //alpha defaults to 1.0 if not given
                    if (strComponents.Length < 4)
                    {
                        components[3] = 1.0f;
                    }
                }
            }

            return(new Color(components[0], components[1], components[2], components[3]));
        }