BorderSearchPlastic.cs 5.16 KB
Newer Older
1 2
using FLY.Thick.Base.IService;
using Misc;
潘栩锋's avatar
潘栩锋 committed
3 4
using System;
using System.Collections.Generic;
5
using System.ComponentModel;
潘栩锋's avatar
潘栩锋 committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
using System.Linq;
using System.Text;

namespace FLY.Thick.Base.Server
{
    public class BorderSearchPlastic:BorderSearch
    {
        public BorderSearchPlastic() : base()
        {

        }
        public BorderSearchPlastic(string param_path) : base(param_path)
        {

        }
21

潘栩锋's avatar
潘栩锋 committed
22
        /// <summary>
23
        /// 找空气与膜均值的 中间位置
潘栩锋's avatar
潘栩锋 committed
24 25
        /// </summary>
        /// <returns></returns>
26
        void FindFilm_Mean(int[] dat, int sensor_grid, int n2_grid, ref int borderbegin_grid, ref int borderend_grid) 
潘栩锋's avatar
潘栩锋 committed
27 28 29
        {
            Range r1_grid = new Range();
            //算膜中间的平均值
30 31
            r1_grid.Begin = borderbegin_grid + sensor_grid + n2_grid;
            r1_grid.End = borderend_grid - (sensor_grid + n2_grid);
潘栩锋's avatar
潘栩锋 committed
32 33 34 35
            int avg = MyMath.Avg(dat, r1_grid.Begin, r1_grid.End);

            //算空气的平均值
            Range r2_grid = new Range();
36 37
            r2_grid.Begin = borderbegin_grid - sensor_grid/3;
            r2_grid.End = borderend_grid + sensor_grid/3;
潘栩锋's avatar
潘栩锋 committed
38 39 40 41 42
            if (r2_grid.Begin < 0)
                r2_grid.Begin = 0;
            if (r2_grid.End > dat.Length - 1)
                r2_grid.End = dat.Length - 1;

43 44
            int avg1 = MyMath.Avg(dat, 0, r2_grid.Begin);
            int avg2 = MyMath.Avg(dat, r2_grid.End, dat.Length - 1);
潘栩锋's avatar
潘栩锋 committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
            int threshold;
            if ((Misc.MyBase.ISVALIDATA(avg1)) && (Misc.MyBase.ISVALIDATA(avg2)))
            {
                threshold = (avg + (avg1 + avg2) / 2) / 2;
            }
            else if (Misc.MyBase.ISVALIDATA(avg1))
            {
                threshold = (avg + avg1) / 2;
            }
            else if (Misc.MyBase.ISVALIDATA(avg2))
            {
                threshold = (avg + avg2) / 2;
            }
            else
            {
60
                threshold = (TempAD+avg) / 2;
潘栩锋's avatar
潘栩锋 committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            }

            //左边界
            for (int i = r1_grid.Begin; i >= r2_grid.Begin; i--)
            {
                if (Misc.MyBase.ISVALIDATA(dat[i]))
                {
                    if (dat[i] < threshold)
                    {
                        borderbegin_grid = i;
                    }
                }
            }
            //右边界
            for (int i = r1_grid.End; i < r2_grid.End; i++)
            {
                if (Misc.MyBase.ISVALIDATA(dat[i]))
                {
                    if (dat[i] < threshold)
                    {
                        borderend_grid = i;
                    }
                }
            }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        }
        /// <summary>
        /// 放入grid数据,分析边界
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="posOfGrid"></param>
        /// <param name="gridBegin"></param>
        /// <param name="dat"></param>
        /// <returns></returns>
        public override bool FindFilm(DIRECTION direction, int posOfGrid, int gridBegin, int[] dat)
        {
            //把数据记录下来
            getViewReponse = new BorderSearchGetViewReponse()
            {
                direction = direction,
                posOfGrid = posOfGrid,
                gridBegin = gridBegin,
                dat = dat
            };
            UpdateTime = DateTime.Now;

            //这些是AD值
            //转为 dat 的序号
            int validbegin_grid = Valid.Begin / posOfGrid - gridBegin;
            int validend_grid = Valid.End / posOfGrid - gridBegin;


            if (!FindFilm_Threshold(dat,
                validbegin_grid, validend_grid,
                out int borderbegin_grid, out int borderend_grid,
                out bool isBorderBeginOk, out bool isBorderEndOk))
            {
                return false;
            }

            //N2 只是用来判断一下膜够不够宽而已,没用
            int N2_grid = (N2 * 3 + SensorWidth) / posOfGrid;

            if ((borderend_grid - borderbegin_grid) < N2_grid)//膜太短
                return false;

            FindFilm_Mean(dat, SensorWidth / posOfGrid, N2 / posOfGrid, ref borderbegin_grid, ref borderend_grid);

            int idx = (direction == DIRECTION.FORWARD) ? 0 : 1;
            var border = borders[idx];

131 132 133
            //borderbegin_grid, borderend_grid 是实际边界位置, 
            //border.value 是空气与膜的拐点
            //必须外扩
潘栩锋's avatar
潘栩锋 committed
134 135

            //转为 脉冲
136 137
            border.value.Begin = (borderbegin_grid + gridBegin) * posOfGrid - SensorWidth / 2;
            border.value.End = (borderend_grid + gridBegin) * posOfGrid + SensorWidth / 2;
138 139 140
            border.isBeginOk = true;
            border.isEndOk = true;
            border.UpdateReal(SensorWidth);
潘栩锋's avatar
潘栩锋 committed
141

142
            if (borders.All(b => b.value.IsValid))
潘栩锋's avatar
潘栩锋 committed
143
            {
144
                Border = new RangeStruct((int)borders.Average(b => b.real.Begin), (int)borders.Average(b => b.real.End));
潘栩锋's avatar
潘栩锋 committed
145
            }
146
            else if (border.value.IsValid)
潘栩锋's avatar
潘栩锋 committed
147
            {
148
                Border = border.real.ToStruct();
潘栩锋's avatar
潘栩锋 committed
149
            }
150 151 152

            getViewReponse.border = border.real;

153 154
            Width = Border.Width;
            Mid = Border.Mid;
潘栩锋's avatar
潘栩锋 committed
155 156 157 158
            return true;
        }
    }
}