Beispiel #1
0
        public void IsInRange(object value, string dtoName, ValidationIssues issues)
        {
            if (!HasRange)
            {
                return;
            }
            RangeResult result = Range.IsInRange(value);

            if (result != RangeResult.ValueInRange)
            {
                string insert = (result == RangeResult.ValueBelowMinimum)
                    ? "below the minimum allowed value"
                    : "above the maximum allowed value";
                string message = string.Format("The {0} has {1} which is {2}.", dtoName, ColumnName, insert);
                issues.Add(ValidationIssue.Code.ValueOutOfRange, message);
            }
        }
Beispiel #2
0
        public RangeResult IsInRange(object value)
        {
            DateTime?   dateValue   = (DateTime?)value;
            RangeResult result      = RangeResult.ValueInRange;
            DateTime?   dateMinimum = (DateTime?)Minimum;

            if (dateValue < dateMinimum)
            {
                result = RangeResult.ValueBelowMinimum;
            }
            DateTime?dateMaximum = (DateTime?)Maximum;

            if (dateValue > dateMaximum)
            {
                result = RangeResult.ValueAboveMaximum;
            }
            return(result);
        }
Beispiel #3
0
        public RangeResult IsInRange(object value)
        {
            uint?       uintValue   = (uint?)value;
            RangeResult result      = RangeResult.ValueInRange;
            uint?       uintMinimum = (uint?)Minimum;

            if (uintValue < uintMinimum)
            {
                result = RangeResult.ValueBelowMinimum;
            }
            uint?uintMaximum = (uint?)Maximum;

            if (uintValue > uintMaximum)
            {
                result = RangeResult.ValueAboveMaximum;
            }
            return(result);
        }
Beispiel #4
0
        public RangeResult IsInRange(object value)
        {
            double?     doubleValue   = (double?)value;
            RangeResult result        = RangeResult.ValueInRange;
            double?     doubleMinimum = (double?)Minimum;

            if (doubleValue < doubleMinimum)
            {
                result = RangeResult.ValueBelowMinimum;
            }
            double?doubleMaximum = (double?)Maximum;

            if (doubleValue > doubleMaximum)
            {
                result = RangeResult.ValueAboveMaximum;
            }
            return(result);
        }
Beispiel #5
0
        private static RangeResult GetRangeBlock(LdapConnection conn, string entryDn, string attrName, int start, int? end, bool extendedDns)
        {
            SearchRequest req = new SearchRequest();
            req.DistinguishedName = entryDn;
            req.Scope = SearchScope.Base;
            req.Filter = "(&(objectClass=*))";
            req.Attributes.Add(attrName + ";range=" + start + "-" + (end == null ? "*" : end.ToString()));

            if (extendedDns)
                req.Controls.Add(new ExtendedDNControl(ExtendedDNFlag.StandardString));

            SearchResponse resp = (SearchResponse)conn.SendRequest(req);
            if (resp.Entries.Count == 0)
                return null;

            SearchResultEntry e = resp.Entries[0];

            foreach (string s in e.Attributes.AttributeNames)
                if (s.StartsWith(attrName, StringComparison.InvariantCultureIgnoreCase))
                {
                    RangeResult res = new RangeResult();
                    DirectoryAttribute attr = e.Attributes[s];

                    res.Values = (string[])attr.GetValues(typeof(string));

                    if (s.EndsWith("*"))
                        res.IsFinal = true;

                    int pos = s.IndexOf('=');
                    int hyp = s.IndexOf('-', pos + 1);

                    res.Start = int.Parse(s.Substring(pos + 1, hyp - pos - 1));

                    if (!res.IsFinal)
                        res.End = int.Parse(s.Substring(hyp + 1));

                    return res;
                }

            return null;
        }
Beispiel #6
0
        public RangeResult CalculateParameterRangeResult(double parameterValue)
        {
            var result = new RangeResult
            {
                Parameter = _parameter,
                Status    = ParameterStatus.Normal,
                Level     = BreachLevel.Safe
            };

            for (var i = 0; i < _map.Length; i++)
            {
                if (!IsParameterInRange(_map[i].LowerLimit, _map[i].UpperLimit, parameterValue))
                {
                    continue;
                }

                result.Status = _map[i].Status;
                result.Level  = _map[i].Level;
                OnParameterRangeBreach(result);
                break;
            }

            return(result);
        }
Beispiel #7
0
        /// <summary>
        /// Try to parse an IRange<T> from a string.
        /// Returns RangeResult<T> to avoid throwing an exception.
        public static RangeResult <T> GetResultFromString <T>(string value)
            where T : struct
        {
            var range = new Range <T>();

            /**
             * This expression is very cumbersome due to its requirement of handling
             * several formats. Please take note of the lookbehind assertion and its preceeding
             * word-boundary. The former lets us define a max-value using simple-syntax while
             * the latter accounts for the min-value (due to the assertion).
             * Valid formats (not exhaustive):
             * [123,456]
             * [123,
             * [123,456
             * 123
             * 123,456
             * 123,
             */
            var parsedValue = Regex.Match(
                value + ",",
                @"(?<startSyntax>[\[\(])?(?<minValue>[^,[(]+?)?(?:[\W])(?<=,)(?<maxValue>[^,\]\)]+)?(?<endSyntax>[\]\)])?");

            if (!parsedValue.Success)
            {
                return(RangeResult <T> .Error("value does not match expected format."));
            }
            else
            {
                var groups         = parsedValue.Groups;
                var parsedMinValue = groups["minValue"].Value;

                /**
                 * If we using short-range syntax, then we
                 * want to set the max-value if not provided.
                 */
                var parsedMaxValue =
                    string.IsNullOrEmpty(
                        groups["maxValue"].Value + groups["startSyntax"].Value + groups["endSyntax"].Value)
                    ? parsedMinValue
                    : groups["maxValue"].Value;

                var isMinInclusive =
                    (groups["startSyntax"].Value == "[" || string.IsNullOrEmpty(groups["startSyntax"].Value))
                    ? true
                    : false;
                var isMaxInclusive =
                    (groups["endSyntax"].Value == "]" || string.IsNullOrEmpty(groups["endSyntax"].Value))
                    ? true
                    : false;
                var isMinInfinite = parsedMinValue == "-∞" ? true : false;
                var isMaxInfinite = parsedMaxValue == "+∞" ? true : false;

                if (string.IsNullOrWhiteSpace(parsedMinValue) &&
                    string.IsNullOrWhiteSpace(parsedMaxValue))
                {
                    return(RangeResult <T> .Error("value cannot be open-ended for both min and max-values."));
                }

                if (isMinInclusive && isMinInfinite)
                {
                    return(RangeResult <T> .Error("value cannot have inclusive infinite lower-bound."));
                }

                if (isMaxInclusive && isMaxInfinite)
                {
                    return(RangeResult <T> .Error("value cannot have inclusive infinite upper-bound."));
                }

                T?minValue = default(T?);
                T?maxValue = default(T?);

                if (!string.IsNullOrWhiteSpace(parsedMinValue) && !isMinInfinite)
                {
                    try
                    {
                        minValue = SmartConverter.Convert <T>(
                            parsedMinValue,
                            isMinInclusive ? ConvertingKind.MinInclusive : ConvertingKind.MinExclusive
                            );
                    }
                    catch (Exception)
                    {
                        return(RangeResult <T> .Error(
                                   string.Format("parsed minimum value `{0}` does not match expected type of `{1}`.",
                                                 groups["minValue"].Value,
                                                 typeof(T).Name)));
                    }
                }

                if (!string.IsNullOrWhiteSpace(parsedMaxValue) && !isMaxInfinite)
                {
                    try
                    {
                        maxValue = SmartConverter.Convert <T>(parsedMaxValue,
                                                              isMaxInclusive ? ConvertingKind.MaxInclusive : ConvertingKind.MaxExclusive
                                                              );
                    }
                    catch (Exception)
                    {
                        return(RangeResult <T> .Error(
                                   string.Format("parsed maximum value `{0}` does not match expected type of `{1}`.",
                                                 groups["maxValue"].Value,
                                                 typeof(T).Name)));
                    }
                }

                range.MinValue       = minValue;
                range.MaxValue       = maxValue;
                range.IsMinInclusive = isMinInclusive;
                range.IsMaxInclusive = isMaxInclusive;
            }

            if (range.MinValue.HasValue && range.MaxValue.HasValue)
            {
                var anyInclusiveRanges = range.IsMinInclusive || range.IsMaxInclusive;
                var compareResult      = Comparer <T> .Default.Compare(range.MinValue.Value, range.MaxValue.Value);

                if (anyInclusiveRanges && compareResult > 0)
                {
                    return(RangeResult <T> .Error(
                               "Minimum value of range must be less than or equal to maximum value."));
                }

                if (!anyInclusiveRanges && compareResult >= 0)
                {
                    return(RangeResult <T> .Error(
                               "Minimum value of range must be less than maximum value when range is non-inclusive."));
                }
            }

            return(RangeResult <T> .Success(range));
        }
Beispiel #8
0
 private void OnParameterRangeBreach(RangeResult result)
 {
     _parameterRangeBreached?.Invoke(_parameter, result.Status, result.Level);
 }
Beispiel #9
0
    public Frame MakeFrame()
    {
        Frame result         = new Frame();
        float deltaFrameTime = Time.time - lastFrameTime;

        lastFrameTime = Time.time;
        seq++;

        Vector3 thisPosition = transform.position;
        Vector3 thisRotation = transform.eulerAngles;

        result.posX            = thisPosition.x;
        result.posY            = thisPosition.y;
        result.posZ            = thisPosition.z;
        result.rotX            = thisRotation.x;
        result.rotY            = thisRotation.y;
        result.rotZ            = thisRotation.z;
        result.accX            = thisPosition.x - lastPosition.x;
        result.accY            = thisPosition.y - lastPosition.y;
        result.accZ            = thisPosition.z - lastPosition.z;
        result.gyrX            = thisRotation.x - lastRotation.x;
        result.gyrY            = thisRotation.y - lastRotation.y;
        result.gyrZ            = thisRotation.z - lastRotation.z;
        result.angle_increment = readingInterval;
        result.angle_min       = Mathf.Min(readingRange.x, readingRange.y) * Mathf.Deg2Rad;
        result.angle_max       = Mathf.Max(readingRange.x, readingRange.y) * Mathf.Deg2Rad;
        result.range_min       = min_range;
        result.range_max       = max_range;
        RangeResult rangeResult = GenerateRanges();

        result.ranges      = (rangeResult == null) ? new float[0] : rangeResult.ranges;
        result.intensities = (rangeResult == null) ? new float[0] : rangeResult.intensities;
        result.seq         = seq;
        result.timestamp   = Time.time;
        result.img         = GenerateImage();
        result.imgfmt      = (result.img == null) ? "none" : "png";
        result.frameid     = "unity";

        return(result);

        RangeResult GenerateRanges()
        {
            if (readingInterval <= 0.0001f)
            {
                if (!errorAlreadyShown)
                {
                    errorAlreadyShown = true;
                    Debug.LogError("Cannot record with a reading interval of less than 0.0001 degrees as this will take forever to compute.");
                }

                return(null);
            }

            List <Vector3> baseVectors = new List <Vector3>();

            for (float deg = Mathf.Min(readingRange.x, readingRange.y); deg <= Mathf.Max(readingRange.x, readingRange.y); deg += readingInterval)
            {
                baseVectors.Add(new Vector3(Mathf.Cos(Mathf.Deg2Rad * deg), 0f, Mathf.Sin(Mathf.Deg2Rad * deg)));
            }

            List <Vector3> rotatedVectors = new List <Vector3>();

            foreach (Vector3 baseVector in baseVectors)
            {
                float alpha = thisRotation.x * Mathf.Deg2Rad;
                float beta  = thisRotation.y * Mathf.Deg2Rad;
                float gamma = thisRotation.z * Mathf.Deg2Rad;

                float cosa = Mathf.Cos(alpha);
                float sina = Mathf.Sin(alpha);
                float cosb = Mathf.Cos(beta);
                float sinb = Mathf.Sin(beta);
                float cosc = Mathf.Cos(gamma);
                float sinc = Mathf.Sin(gamma);

                float axx = cosa * cosb;
                float axy = (cosa * sinb * sinc) - (sina * cosc);
                float axz = (cosa * sinb * cosc) + (sina * sinc);
                float ayx = sina * cosb;
                float ayy = (sina * sinb * sinc) + (cosa * cosc);
                float ayz = (sina * sinb * cosc) - (cosa * sinc);
                float azx = -sinb;
                float azy = cosb * sinc;
                float azz = cosb * cosc;

                float mx = (baseVector.x * axx) + (baseVector.y * axy) + (baseVector.z * axz);
                float my = (baseVector.x * ayx) + (baseVector.y * ayy) + (baseVector.z * ayz);
                float mz = (baseVector.x * azx) + (baseVector.y * azy) + (baseVector.z * azz);
                rotatedVectors.Add(new Vector3(mx, my, mz));
            }

            List <float> rangeReadings     = new List <float>();
            List <float> intensityReadings = new List <float>();

            foreach (Vector3 direction in rotatedVectors)
            {
                Ray          ray         = new Ray(transform.position, direction);
                RaycastHit[] hits        = Physics.RaycastAll(ray, max_range);
                float        distance    = max_range;
                float        intensity   = 0f;
                RaycastHit   closestHit  = new RaycastHit();
                float        minDistance = float.MaxValue;
                foreach (RaycastHit hit in hits)
                {
                    if (minDistance > hit.distance)
                    {
                        minDistance = hit.distance;
                        closestHit  = hit;
                    }
                    if (showRays)
                    {
                        Debug.DrawLine(transform.position, hit.point, Color.green);
                    }

                    if (hit.distance < min_range)
                    {
                        continue;
                    }
                    distance = hit.distance;
                    switch (intensityMetric)
                    {
                    case IntensityMetric.None:
                        intensity = 0f;
                        break;

                    case IntensityMetric.Success:
                        intensity = 1f;
                        break;

                    case IntensityMetric.Noise:
                        intensity = 1f - (distance / max_range);
                        break;
                    }
                    break;
                }
                rangeReadings.Add(distance);
                intensityReadings.Add(intensity);
            }

            return(new RangeResult()
            {
                intensities = intensityReadings.ToArray(),
                ranges = rangeReadings.ToArray()
            });
        }

        byte[] GenerateImage()
        {
            if (camera == null)
            {
                camera = GetComponent <Camera>();
            }
            if (camera == null)
            {
                return(null);
            }
            byte[] ba = new byte[0];

            if (camera.targetTexture == null)
            {
                camera.targetTexture = new RenderTexture(Screen.width, Screen.height, 24);
            }
            RenderTexture active = RenderTexture.active;

            RenderTexture.active = camera.targetTexture;
            camera.Render();
            Texture2D texture = new Texture2D(camera.targetTexture.width, camera.targetTexture.height, TextureFormat.RGB24, false);

            texture.ReadPixels(new Rect(0, 0, camera.targetTexture.width, camera.targetTexture.height), 0, 0);
            texture.Apply();
            RenderTexture.active = active;
            ba = texture.EncodeToPNG();
            Destroy(texture);
            return(ba);
        }
    }
 public RegisterCompareResult(int dummy = 0)
 {
     RangeResult = RangeResult.NotAvailable;
     EqualityResult = EqualityResult.Incompatible;
 }