Esempio n. 1
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(Float) && value != null && parameter != null)
            {
                Float input, multiplier;
                if (Float.TryParse(value.ToString(), out input) &&
                    Float.TryParse(parameter.ToString(), out multiplier))
                {
                    return(input * multiplier);
                }
            }

            return(value);
        }
Esempio n. 2
0
        public static FnLabType[,] LoadFromTextFile(string name, int size)
        {
            FnLabType[,] result = new FnLabType[size, size];

            if (File.Exists(name))
            {
                using (var file = File.Open(name, FileMode.Open))
                {
                    using (StreamReader reader = new StreamReader(file))
                    {
                        string s = reader.ReadLine();
                        Console.WriteLine(s);

                        int i = 0;
                        while (!reader.EndOfStream)
                        {
                            int j = 0;
                            s = reader.ReadLine();
                            s = s + " ";
                            s = s.TrimStart(' ');
                            while (s.Length != 0)
                            {
                                FnLabType number;
                                string    snumber = s.Substring(0, s.IndexOf(" "));
                                //snumber = snumber.Replace('.', ',');
                                //Console.WriteLine(snumber);
                                FnLabType.TryParse(snumber, out number);
                                //Console.WriteLine(number);
                                s = s.Remove(0, s.IndexOf(" ") + 1);
                                s = s.TrimStart(' ');

                                result[i, j] = number;
                                j            = j + 1;
                            }
                            i = i + 1;
                        }
                    }
                }

                return(result);
            }
            else
            {
                Console.WriteLine("Что-то там ...");
                return(result);
            }
        }
Esempio n. 3
0
        public static bool TryParse(string s, out Real result)
        {
            bool b;

#if !(XBOX || XBOX360)
            Numeric retval;
            b      = Numeric.TryParse(s, out retval);
            result = (Real)retval;
#else
            try
            {
                result = Parse(s);
                b      = true;
            }
            catch
            {
                result = (Numeric)0;
                b      = false;
            }
#endif
            return(b);
        }
Esempio n. 4
0
        /// <summary>
        /// Generic parameter parser. Currently hand-hacked to auto-detect type.
        ///
        /// Generic form:   Name:Values
        /// for example,    lr:0.05-0.4
        ///          lambda:0.1-1000@log10
        ///          nl:2-64@log2
        ///          norm:-,+
        /// </summary>
        /// REVIEW: allow overriding auto-detection to specify type
        /// and delegate to parameter type for actual parsing
        /// REVIEW: specifying ordinal discrete parameters
        public bool TryParseParameter(string paramValue, Type paramType, string paramName, out IValueGenerator sweepValues, out string error)
        {
            sweepValues = null;
            error       = null;

            if (paramValue.Contains(','))
            {
                var generatorArgs = new DiscreteParamArguments();
                generatorArgs.Name   = paramName;
                generatorArgs.Values = paramValue.Split(',');
                sweepValues          = new DiscreteValueGenerator(generatorArgs);
                return(true);
            }

            // numeric parameter
            if (!CmdParser.IsNumericType(paramType))
            {
                return(false);
            }

            // REVIEW:  deal with negative bounds
            string scaleStr = null;
            int    atIdx    = paramValue.IndexOf('@');

            if (atIdx < 0)
            {
                atIdx = paramValue.IndexOf(';');
            }
            if (atIdx >= 0)
            {
                scaleStr   = paramValue.Substring(atIdx + 1);
                paramValue = paramValue.Substring(0, atIdx);
                if (scaleStr.Length < 1)
                {
                    error = $"Could not parse sweep range for parameter: {paramName}";
                    return(false);
                }
            }

            // Extract the minimum, and the maximum value of the list of suggested sweeps.
            // Positive lookahead splitting at the '-' character.
            // It is used for the Float and Long param types.
            // Example format: "0.02-0.1;steps:5".
            string[] minMaxRegex = Regex.Split(paramValue, "(?<=[^eE])-");
            if (minMaxRegex.Length != 2)
            {
                if (minMaxRegex.Length > 2)
                {
                    error = $"Could not parse sweep range for parameter: {paramName}";
                }

                return(false);
            }
            string minStr = minMaxRegex[0];
            string maxStr = minMaxRegex[1];

            int    numSteps = 100;
            Double stepSize = -1;
            bool   logBase  = false;

            if (scaleStr != null)
            {
                try
                {
                    string[] options          = scaleStr.Split(';');
                    bool[]   optionsSpecified = new bool[3];
                    foreach (string option in options)
                    {
                        if (option.StartsWith("log") && !option.StartsWith("log-") && !option.StartsWith("log:-"))
                        {
                            logBase             = true;
                            optionsSpecified[0] = true;
                        }
                        if (option.StartsWith("steps"))
                        {
                            numSteps            = int.Parse(option.Substring(option.IndexOf(':') + 1));
                            optionsSpecified[1] = true;
                        }
                        if (option.StartsWith("inc"))
                        {
                            stepSize            = Double.Parse(option.Substring(option.IndexOf(':') + 1), CultureInfo.InvariantCulture);
                            optionsSpecified[2] = true;
                        }
                    }
                    if (options.Length != optionsSpecified.Count(b => b))
                    {
                        error = $"Could not parse sweep range for parameter: {paramName}";
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    error = $"Error creating sweep generator for parameter '{paramName}': {e.Message}";
                    return(false);
                }
            }

            if (paramType == typeof(UInt16) ||
                paramType == typeof(UInt32) ||
                paramType == typeof(UInt64) ||
                paramType == typeof(short) ||
                paramType == typeof(int) ||
                paramType == typeof(long))
            {
                long min;
                long max;
                if (!long.TryParse(minStr, out min) || !long.TryParse(maxStr, out max))
                {
                    return(false);
                }
                var generatorArgs = new Microsoft.ML.Sweeper.LongParamArguments();
                generatorArgs.Name     = paramName;
                generatorArgs.Min      = min;
                generatorArgs.Max      = max;
                generatorArgs.NumSteps = numSteps;
                generatorArgs.StepSize = (stepSize > 0 ? stepSize : new Nullable <Double>());
                generatorArgs.LogBase  = logBase;

                try
                {
                    sweepValues = new LongValueGenerator(generatorArgs);
                }
                catch (Exception e)
                {
                    error = $"Error creating sweep generator for parameter '{paramName}': {e.Message}";
                    return(false);
                }
            }
            else
            {
                Float minF;
                Float maxF;
                if (!Float.TryParse(minStr, out minF) || !Float.TryParse(maxStr, out maxF))
                {
                    return(false);
                }
                var floatArgs = new FloatParamArguments();
                floatArgs.Name     = paramName;
                floatArgs.Min      = minF;
                floatArgs.Max      = maxF;
                floatArgs.NumSteps = numSteps;
                floatArgs.StepSize = (stepSize > 0 ? stepSize : new Nullable <Double>());
                floatArgs.LogBase  = logBase;

                try
                {
                    sweepValues = new FloatValueGenerator(floatArgs);
                }
                catch (Exception e)
                {
                    error = $"Error creating sweep generator for parameter '{paramName}': {e.Message}";
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 5
0
        public static MashTrigle[] LoadModel(string filePath, bool negativeZ = false)
        {
            string[] lines = File.ReadAllLines(filePath);

            List <MashTrigle> mashes = new List <MashTrigle>();
            List <Vector3f>   v      = new List <Vector3f>()
            {
                new Vector3f()
            };
            Dictionary <int, List <PW_Trigle> > faceList = new Dictionary <int, List <PW_Trigle> >();
            List <Vector2f> vt = new List <Vector2f>()
            {
                new Vector2f()
            };
            List <Vector3f> vn = new List <Vector3f>()
            {
                new Vector3f()
            };

            MashTrigle       nobj    = null;
            List <PW_Trigle> trigles = new List <PW_Trigle>();

            for (int linecount = 0; linecount < lines.Length; linecount++)
            {
                string line = lines[linecount];
                if (string.IsNullOrWhiteSpace(line) || line.StartsWith('#'))
                {
                    continue;
                }

                float    p0 = 0.0f, p1 = 0.0f, p2 = 0.0f;
                string[] parts = line.Split(LineSplitChars, StringSplitOptions.RemoveEmptyEntries);
                switch (parts[0].ToLower())
                {
                case "v":
                    #region v
                    if (parts.Length != 4)
                    {
                        throw new Exception($"line {linecount} : 缺少参数的行,请求3个,实际为{parts.Length - 1}个");
                    }
                    if (!Float.TryParse(parts[1], out p0))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[1]}");
                    }
                    if (!Float.TryParse(parts[2], out p1))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[2]}");
                    }
                    if (!Float.TryParse(parts[3], out p2))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[3]}");
                    }
                    if (negativeZ)
                    {
                        v.Add(new Vector3f(p0, p1, -p2));
                    }
                    else
                    {
                        v.Add(new Vector3f(p0, p1, p2));
                    }
                    #endregion
                    break;

                case "vt":
                    #region vt
                    if (parts.Length != 3 && parts.Length != 4)
                    {
                        throw new Exception($"line {linecount} : 缺少参数的行,请求2或3个,实际为{parts.Length - 1}个");
                    }
                    if (!Float.TryParse(parts[1], out p0))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[1]}");
                    }
                    if (!Float.TryParse(parts[2], out p1))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[2]}");
                    }
                    vt.Add(new Vector2f(p0, p1));
                    #endregion
                    break;

                case "vn":
                    #region vn
                    if (parts.Length != 4)
                    {
                        throw new Exception($"line {linecount} : 缺少参数的行,请求3个,实际为{parts.Length - 1}个");
                    }
                    if (!Float.TryParse(parts[1], out p0))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[1]}");
                    }
                    if (!Float.TryParse(parts[2], out p1))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[2]}");
                    }
                    if (!Float.TryParse(parts[3], out p2))
                    {
                        throw new Exception($"line {linecount} : 不能转换的数据 {parts[3]}");
                    }
                    if (negativeZ)
                    {
                        vn.Add(Vector3f.Normalize(new Vector3f(p0, p1, -p2)));
                    }
                    else
                    {
                        vn.Add(Vector3f.Normalize(new Vector3f(p0, p1, p2)));
                    }
                    #endregion
                    break;

                case "vp":

                    break;

                case "g":
                    #region g
                    if (trigles != null && trigles.Count > 0)
                    {
                        if (nobj == null)
                        {
                            nobj = new MashTrigle();
                        }
                        nobj.SetTrigles(Smooth(trigles, v, vt, faceList));
                        mashes.Add(nobj);
                    }
                    nobj    = new MashTrigle();
                    trigles = new List <PW_Trigle>();
                    #endregion
                    break;

                case "f":
                    #region f
                    if (parts.Length < 4)
                    {
                        throw new Exception($"line {linecount} : 缺少参数的行,请求至少为3个,实际为{parts.Length - 1}个");
                    }
                    int   pointcount = parts.Length - 1;
                    int[] vidx       = new int[pointcount], vtidx = new int[pointcount], vnidx = new int[pointcount];

                    // 分割顶点信息
                    for (int i = 0; i < pointcount; i++)
                    {
                        string   pinfo = parts[i + 1];
                        string[] infos = pinfo.Split(PInfoSplitChars);
                        vidx[i] = int.Parse(infos[0]);
                        int tmp;
                        if (infos.Length > 1 && int.TryParse(infos[1], out tmp))
                        {
                            vtidx[i] = tmp;
                        }
                        else
                        {
                            vtidx[i] = 0;                                    //-1;
                        }
                        if (infos.Length > 2 && int.TryParse(infos[1], out tmp))
                        {
                            vnidx[i] = tmp;
                        }
                        else
                        {
                            vnidx[i] = 0;                                    //-1;
                        }
                    }

                    // 生成三角形面
                    for (int i = 2; i < pointcount; i++)
                    {
                        PW_Trigle face = new PW_Trigle();
                        face.v0 = vidx[0];
                        face.v1 = vidx[i - 1];
                        face.v2 = vidx[i];

                        {                                 //normal
                            Vector3f e1 = v[face.v1] - v[face.v0], e2 = v[face.v2] - v[face.v0];
                            face.Normal = Vector3f.Normalize(Vector3f.Cross(e1, e2));
                        }
                        {                                 //sp
                                                          //if (vtidx[0] != -1) {
                            face.vt0 = vtidx[0];
                            //}
                            //if (vtidx[i - 1] != -1) {
                            face.vt1 = vtidx[i - 1];
                            //}
                            //if (vtidx[i] != -1) {
                            face.vt2 = vtidx[i];
                            //}
                        }

                        trigles.Add(face);
                        if (!faceList.ContainsKey(face.v0))
                        {
                            faceList[face.v0] = new List <PW_Trigle>();
                        }
                        faceList[face.v0].Add(face);
                        if (!faceList.ContainsKey(face.v1))
                        {
                            faceList[face.v1] = new List <PW_Trigle>();
                        }
                        faceList[face.v1].Add(face);
                        if (!faceList.ContainsKey(face.v2))
                        {
                            faceList[face.v2] = new List <PW_Trigle>();
                        }
                        faceList[face.v2].Add(face);
                    }

                    #endregion
                    break;
                }
            }

            if (trigles != null && trigles.Count > 0)
            {
                if (nobj == null)
                {
                    nobj = new MashTrigle();
                }
                nobj.SetTrigles(Smooth(trigles, v, vt, faceList));
                mashes.Add(nobj);
            }

            return(mashes.ToArray());
        }