Exemplo n.º 1
0
 public Scatter(LineSegment[] segs, SCATTER_SEG_DIRECTION direction=SCATTER_SEG_DIRECTION.CLOCKWISE, double dc=2, string name="")
 {
     if (!IsSegmentsLegal(segs))
         throw new Exception("线段集合不符合要求,请保证:\n" +
                             "1. 至少有3条线段\n" +
                             "2. 集合内的线段按序首尾相连\n" +
                             "3. 每条线段都不与除相临线段外的任何线段相交\n");
     this.border_segments = segs;
     this.seg_direction = direction;
     this.dielectric_constant = dc;
     int top = int.MaxValue, bottom = int.MinValue, left = int.MaxValue, right = int.MinValue;
     foreach(LineSegment seg in segs)
     {
         if (seg.Top < top)
             top = seg.Top;
         if (seg.Bottom > bottom)
             bottom = seg.Bottom;
         if (seg.Left < left)
             left = seg.Left;
         if (seg.Right > right)
             right = seg.Right;
     }
     this.border_rect.X = left;
     this.border_rect.Y = top;
     this.border_rect.Width = right - left;
     this.border_rect.Height = bottom - top;
     this.name = name == "" ? "Scatter" + id.ToString() : name;
     id++;
 }
Exemplo n.º 2
0
 /// <summary>
 /// 计算反射后的功率
 /// </summary>
 /// <param name="Pi">入射功率</param>
 /// <param name="ray">入射波束线段</param>
 /// <param name="barrier">碰撞散射体线段</param>
 /// <param name="dc">散射体介电常数</param>
 /// <returns></returns>
 public static double ReflectionLoss(double Pi, LineSegment ray, LineSegment barrier, double dc)
 {
     double angle = ray.Angle(barrier);
     double theta_i = angle > Math.PI / 2 ? angle - Math.PI / 2 : Math.PI / 2 - angle;
     double theta_t = Math.Asin(Math.Sin(theta_i) / Math.Sqrt(dc));
     double tau = (Math.Cos(theta_i) - Math.Sqrt(dc) * Math.Cos(theta_t)) / (Math.Cos(theta_i) + Math.Sqrt(dc) * Math.Cos(theta_t));
     return Pi * Math.Abs(tau);
 }
Exemplo n.º 3
0
        ///// <summary>
        ///// 自由空间路径损耗
        ///// </summary>
        ///// <param name="Pi"></param>
        ///// <param name="d"></param>
        ///// <param name="freq"></param>
        ///// <returns></returns>
        //public static double FreeSpacePathLoss(double Pi, double d, double freq)
        //{
        //    double dbi = 20 * Math.Log10(freq / 1e6) + 20 * Math.Log10(d / 1e3);
        //    double ratio = Math.Pow(10, (dbi / 10));
        //    return Pi / ratio;
        //}

        /// <summary>
        /// 计算镜面反射角
        /// </summary>
        /// <param name="ray">入射波束线段</param>
        /// <param name="barrier">产生碰撞的散射体线段</param>
        /// <param name="dir">散射体的向量旋转方向</param>
        /// /// <returns>出射角</returns>
        public static double SpecularReflection(LineSegment ray, LineSegment barrier, SCATTER_SEG_DIRECTION dir)
        {
            double result;
            double angle = ray.Angle(barrier);
            if(dir == SCATTER_SEG_DIRECTION.CLOCKWISE)
            {
                result = barrier.DirectionRadian + angle;
                result = result > Math.PI * 2 ? result - Math.PI * 2 : result;
            }
            else
            {
                result = barrier.DirectionRadian - angle;
                result = result < 0 ? result + Math.PI * 2 : result;
            }
            return result;
        }
Exemplo n.º 4
0
Arquivo: Map.cs Projeto: Yinzhe-Qi/RPS
        /// <summary>
        /// 运行单次仿真
        /// </summary>
        public Dictionary<Fingerprint_Name, Fingerprint> RunSingleSimulation(int num_rays, Transmitter.EMIT_OPTION emit_option, Signal_Type signal_type,
                                                 Noise_Type noise_type, double[] noise_params, int N)
        {
            foreach(Transmitter trans in this.Transmitters)
            {
                trans.Reset();
                trans.EmitRays(num_rays, emit_option);   //各发射机辐射波束
                foreach(Receiver rec in this.Receivers)
                {
                    rec.Reset();
                    if(this.IsLOS(trans.Location, rec.Location))    //若存在LOS情况
                    {
                        LineSegment segLOS = new LineSegment(trans.Location, rec.Location);
                        Ray rayLOS = new Ray(trans, new LineSegment[] { segLOS }, segLOS.DirectionRadian, trans.CenterFrequency, trans.EmitPower * trans.Gain, trans.EmitPower * trans.Gain, true);
                        rec.AcceptRay(rayLOS, this);
                    }
                }
                foreach(Ray ray in trans.Rays)  //更新每条ray, 若被接收,则更新接收它的接收机
                {
                    Receiver rec = null;
                    while (!ray.Update(this, out rec)) ;
                    if (rec != null)
                        rec.AcceptRay(ray, this);
                }
            }

            FingerprintGenerator fg = new FingerprintGenerator();
            Dictionary<Fingerprint_Name, Fingerprint> result = new Dictionary<Fingerprint_Name,Fingerprint>();
            List<double>[] ray_angles = new List<double>[this.receivers.Count];
            List<double>[] ray_strengths = new List<double>[this.receivers.Count];
            List<double>[] ray_taus = new List<double>[this.receivers.Count];
            int i = 0;
            foreach(Receiver rec in this.Receivers)
            {
                ray_angles[i] = new List<double>();
                ray_strengths[i] = new List<double>();
                ray_taus[i] = new List<double>();
                foreach(Ray ray in rec.Rays)
                {
                    ray_angles[i].Add(ray.CurrentDirection);
                    ray_strengths[i].Add(ray.FadedPower(this.MeterPixelRatio));
                    ray_taus[i].Add(ray.GetLifeSpan(this.MeterPixelRatio));
                }
                i++;
            }
            double[] args = null;
            switch (signal_type)
            {
                case Signal_Type.PSEUDO_DOPPLER:
                    if (this.receivers.First().Value.Antenna.AntType != AntennaType.PSEUDO_DOPPLER)
                        return null;
                    Antenna_PseudoDoppler ant = this.receivers.First().Value.Antenna as Antenna_PseudoDoppler;
                    args = fg.PackArgs_PseudoDoppler(this.receivers.First().Value.FrequencyLow, this.receivers.First().Value.CenterFrequency,
                                                     this.receivers.First().Value.SampleFrequency, ant.SwitchFrequency, ant.Radius, ant.AntennaNumber);
                    break;
            }
            for (int j = 0; j < Enum.GetNames(new Fingerprint_Name().GetType()).Length; j++ )
                result[(Fingerprint_Name)j] = fg.SingleFingerprintSim(signal_type, noise_type, noise_params, (Fingerprint_Name)j, ray_angles, ray_strengths, ray_taus,
                                                 N, args);
            return result;
        }
Exemplo n.º 5
0
 /// <summary>
 /// 判断线段集合是否符合构成散射体的要求
 /// 1. 至少有3条线段
 /// 2. 集合内的线段按序首尾相连
 /// 3. 每条线段都不与除相临线段外的任何线段相交
 /// </summary>
 /// <param name="segs"></param>
 /// <returns></returns>
 public bool IsSegmentsLegal(LineSegment[] segs)
 {
     if (segs == null || segs.Length < 3)
         return false;
     List<LineSegment> tmp = new List<LineSegment>();
     for (int i = 1; i < segs.Length; i++)
     {
         if (segs[i].GetIntersectionPoint(segs[i - 1]) != segs[i].TailPosition || segs[i - 1].HeadPosition != segs[i].TailPosition)
             return false;
         if (i < segs.Length - 1)
         {
             foreach (LineSegment prev_seg in tmp)
                 if (segs[i].JudgeIntersection(prev_seg))
                     return false;
             tmp.Add(segs[i - 1]);
         }
     }
     if (segs[0].GetIntersectionPoint(segs[segs.Length - 1]) != segs[0].TailPosition || segs[segs.Length - 1].HeadPosition != segs[0].TailPosition)
         return false;
     return true;
 }
Exemplo n.º 6
0
Arquivo: Map.cs Projeto: Yinzhe-Qi/RPS
 /// <summary>
 /// 判断地图上两点是否为视距
 /// </summary>
 /// <param name="p1"></param>
 /// <param name="p2"></param>
 /// <returns></returns>
 public bool IsLOS(Point p1, Point p2)
 {
     LineSegment seg = new LineSegment(p1, p2);
     LineSegment tmp = new LineSegment();
     foreach (Scatter s in this.scatters.Values)
         if (s.GetClosestReflectionPoint(seg, out tmp) != null)
             return false;
     return true;
 }
Exemplo n.º 7
0
Arquivo: Map.cs Projeto: Yinzhe-Qi/RPS
 /// <summary>
 /// 获取反射点坐标及碰撞线段和碰撞散射体的旋转方向
 /// </summary>
 /// <param name="seg"></param>
 /// <param name="collision_seg"></param>
 /// <param name="dir"></param>
 /// <returns></returns>
 public Point? GetClosestReflectionPoint(LineSegment seg, out LineSegment collision_seg, out SCATTER_SEG_DIRECTION dir, out double dc)
 {
     Dictionary<Point?, object[]> pt_seg_dir_dict = new Dictionary<Point?, object[]>();
     foreach (Scatter scatter in this.Scatters)
     {
         Point? tmp_pt = null;
         LineSegment tmp_seg = null;
         SCATTER_SEG_DIRECTION tmp_dir = scatter.SegDirection;
         double tmp_dc = scatter.DielectricConstant;
         tmp_pt = scatter.GetClosestReflectionPoint(seg, out tmp_seg);
         if (tmp_pt != null)
             pt_seg_dir_dict.Add(tmp_pt, new object[] { tmp_seg, tmp_dir, tmp_dc });
     }
     if(pt_seg_dir_dict.Count == 0)  //没有交点
     {
         collision_seg = null;
         dir = SCATTER_SEG_DIRECTION.CLOCKWISE;
         dc = 0;
         return null;
     }
     KeyValuePair<Point?, object[]> tmp_kvp = pt_seg_dir_dict.First();
     double min_d2 = double.PositiveInfinity;
     foreach (KeyValuePair<Point?, object[]> kvp in pt_seg_dir_dict)
     {
         double dist2 = Math.Pow(kvp.Key.Value.X - seg.TailPosition.X, 2) + Math.Pow(kvp.Key.Value.Y - seg.TailPosition.Y, 2);
         if (dist2 > 0 && dist2 < min_d2)
         {
             min_d2 = dist2;
             tmp_kvp = kvp;
         }
     }
     collision_seg = tmp_kvp.Value[0] as LineSegment;
     dir = (SCATTER_SEG_DIRECTION)tmp_kvp.Value[1];
     dc = (double)tmp_kvp.Value[2];
     return tmp_kvp.Key;
 }
Exemplo n.º 8
0
 /// <summary>
 /// 计算与另一线段的夹角(rad)
 /// </summary>
 /// <param name="that"></param>
 /// <returns></returns>
 public double Angle(LineSegment that)
 {
     if (this.Length == 0 || that.Length == 0)
         return 0;
     double angle = Math.Acos(this.DotProduct(that));
     return angle;
 }
Exemplo n.º 9
0
Arquivo: Ray.cs Projeto: Yinzhe-Qi/RPS
 /// <summary>
 /// 判断线段集合是否符合构成波束的要求
 /// 1. 至少有1条线段
 /// 2. 集合内的线段按序首尾相连
 /// </summary>
 /// <param name="segs"></param>
 /// <returns></returns>
 public bool IsSegmentsLegal(LineSegment[] segs)
 {
     if (segs == null || segs.Length < 1)
         return false;
     for (int i = 1; i < segs.Length; i++)
     {
         if (segs[i - 1].HeadPosition != segs[i].TailPosition)
             return false;
     }
     return true;
 }
Exemplo n.º 10
0
        /// <summary>
        /// 线段类的单元测试
        /// </summary>
        /// <param name="epsilon"></param>
        /// <returns></returns>
        public static string RunTest(double epsilon=1e-3)
        {
            LineSegment This = new LineSegment();
            LineSegment That = new LineSegment();
            DateTime time = DateTime.Now;
            StringBuilder sb = new StringBuilder("LineSegment Class Test Begin at " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
            test_log.Add(time, "");

            //test Length
            sb.AppendLine("----------------------------");
            sb.AppendLine("length test 1");
            Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 0)).Length == 0, "length should be 0");
            sb.AppendLine("length test 2");
            Debug.Assert(new LineSegment(new Point(0, 1), new Point(1, 0)).Length == Math.Sqrt(2), "length should be sqrt(2)");
            sb.AppendLine("length test 3");
            Debug.Assert(new LineSegment(new Point(2, 0), new Point(0, 0)).Length == 2, "length should be 2");
            sb.AppendLine("length test 4");
            Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 2)).Length == 2, "length should be 2");

            //test DirectionRadius
            sb.AppendLine("----------------------------");
            sb.AppendLine("direction test 1");
            Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 0)).DirectionRadian == 0, "direction should be 0");
            sb.AppendLine("direction test 2");
            Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 0)).DirectionRadian == 0, "direction should be 0");
            sb.AppendLine("direction test 3");
            Debug.Assert(new LineSegment(new Point(0, 0), new Point(0, 1)).DirectionRadian == Math.PI / 2, "direction should be pi/2");
            sb.AppendLine("direction test 4");
            Debug.Assert(new LineSegment(new Point(0, 0), new Point(1, 0)).DirectionRadian == Math.PI, "direction should be pi");
            sb.AppendLine("direction test 5");
            Debug.Assert(new LineSegment(new Point(0, 1), new Point(0, 0)).DirectionRadian == Math.PI / 2 * 3, "direction should be pi/2*3");
            sb.AppendLine("direction test 6");
            Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 1)).DirectionRadian == Math.PI / 4, "direction should be pi/4");
            sb.AppendLine("direction test 7");
            Debug.Assert(new LineSegment(new Point(0, 0), new Point(1, 1)).DirectionRadian == Math.PI / 4 * 3, "direction should be pi/4*3");
            sb.AppendLine("direction test 8");
            Debug.Assert(new LineSegment(new Point(0, 1), new Point(1, 0)).DirectionRadian == Math.PI / 4 * 5, "direction should be pi/4*5");
            sb.AppendLine("direction test 9");
            Debug.Assert(new LineSegment(new Point(1, 1), new Point(0, 0)).DirectionRadian == Math.PI / 4 * 7, "direction should be pi/4*7");

            //test Angle
            sb.AppendLine("----------------------------");
            This = new LineSegment(new Point(1, 0), new Point(0, 0));
            sb.AppendLine("angle test 1");
            That = new LineSegment(new Point(1, 0), new Point(0, 0));
            Debug.Assert(This.Angle(That) == 0, "angle should be 0");
            sb.AppendLine("angle test 2");
            That = new LineSegment(new Point(0, 0), new Point(0, 1));
            Debug.Assert(This.Angle(That) == Math.PI / 2, "angle should be pi/2");
            sb.AppendLine("angle test 3");
            That = new LineSegment(new Point(0, 0), new Point(1, 0));
            Debug.Assert(This.Angle(That) == Math.PI, "angle should be pi");
            sb.AppendLine("angle test 4");
            That = new LineSegment(new Point(0, 1), new Point(0, 0));
            Debug.Assert(This.Angle(That) == Math.PI / 2, "angle should be pi/2");
            sb.AppendLine("angle test 5");
            That = new LineSegment(new Point(1, 0), new Point(0, 1));
            Debug.Assert(This.Angle(That) > Math.PI / 4 - epsilon && This.Angle(That) < Math.PI / 4 + epsilon, "angle should be pi/4");
            sb.AppendLine("angle test 6");
            Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 0)).Angle(new LineSegment(new Point(0, 0), new Point(1, 1))) == Math.PI / 4 * 3, "angle should be pi/4*3");
            sb.AppendLine("angle test 7");
            Debug.Assert(new LineSegment(new Point(1, 0), new Point(0, 0)).Angle(new LineSegment(new Point(0, 1), new Point(1, 0))) == Math.PI / 4 * 3, "angle should be pi/4*3");
            sb.AppendLine("angle test 8");
            That = new LineSegment(new Point(1, 1), new Point(0, 0));
            Debug.Assert(This.Angle(That) > Math.PI / 4 - epsilon && This.Angle(That) < Math.PI / 4 + epsilon, "angle should be pi/4");
            sb.AppendLine("angle test 9");
            That = new LineSegment(new Point(0, 0), new Point(0, 0));
            Debug.Assert(This.Angle(That) == 0, "angle should be 0");

            //test JudgeIntersection
            sb.AppendLine("----------------------------");
            This = new LineSegment(new Point(2, 2), new Point(0, 2));
            sb.AppendLine("intersection test 1");
            That = new LineSegment(new Point(1, 2), new Point(0, 2));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");
            sb.AppendLine("intersection test 2");
            That = new LineSegment(new Point(1, 1), new Point(0, 1));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");
            sb.AppendLine("intersection test 3");
            That = new LineSegment(new Point(0, 1), new Point(1, 1));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");
            sb.AppendLine("intersection test 4");
            That = new LineSegment(new Point(1, 0), new Point(1, 1));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");
            sb.AppendLine("intersection test 5");
            That = new LineSegment(new Point(1, 1), new Point(1, 0));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");
            sb.AppendLine("intersection test 6");
            That = new LineSegment(new Point(1, 1), new Point(0, 2));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 7");
            That = new LineSegment(new Point(1, 1), new Point(2, 2));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 8");
            That = new LineSegment(new Point(3, 1), new Point(2, 2));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 9");
            That = new LineSegment(new Point(2, 1), new Point(1, 2));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 10");
            That = new LineSegment(new Point(1, 2), new Point(2, 3));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 11");
            That = new LineSegment(new Point(0, 0), new Point(2, 3));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 12");
            That = new LineSegment(new Point(1, 0), new Point(0, 3));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 13");
            That = new LineSegment(new Point(1, 3), new Point(1, 1));
            Debug.Assert(This.JudgeIntersection(That) == true, "intersection should be true");
            sb.AppendLine("intersection test 14");
            That = new LineSegment(new Point(3, 2), new Point(2, 2));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");
            sb.AppendLine("intersection test 15");
            That = new LineSegment(new Point(1, 2), new Point(1, 2));
            Debug.Assert(This.JudgeIntersection(That) == false, "intersection should be false");


            //test GetIntersectionPoint
            sb.AppendLine("----------------------------");
            This = new LineSegment(new Point(3, 2), new Point(1, 2));
            sb.AppendLine("intersection point test 1");
            That = new LineSegment(new Point(3, 1), new Point(1, 1));
            Debug.Assert(This.GetIntersectionPoint(That) == null, "intersection point should be null");
            sb.AppendLine("intersection point test 2");
            That = new LineSegment(new Point(4, 1), new Point(3, 2));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)");
            sb.AppendLine("intersection point test 3");
            That = new LineSegment(new Point(3, 1), new Point(3, 2));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)");
            sb.AppendLine("intersection point test 4");
            That = new LineSegment(new Point(2, 1), new Point(3, 2));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)");
            sb.AppendLine("intersection point test 5");
            That = new LineSegment(new Point(3, 2), new Point(2, 3));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)");
            sb.AppendLine("intersection point test 6");
            That = new LineSegment(new Point(3, 2), new Point(4, 3));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(3, 2), "intersection point should be (3, 2)");
            sb.AppendLine("intersection point test 7");
            That = new LineSegment(new Point(2, 1), new Point(2, 3));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)");
            sb.AppendLine("intersection point test 8");
            That = new LineSegment(new Point(1, 1), new Point(3, 3));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)");
            sb.AppendLine("intersection point test 9");
            That = new LineSegment(new Point(3, 3), new Point(1, 1));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)");
            sb.AppendLine("intersection point test 10");
            That = new LineSegment(new Point(3, 1), new Point(1, 3));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)");
            sb.AppendLine("intersection point test 11");
            That = new LineSegment(new Point(1, 3), new Point(3, 1));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(2, 2), "intersection point should be (2, 2)");
            sb.AppendLine("intersection point test 12");
            That = new LineSegment(new Point(2, 3), new Point(0, 1));
            Debug.Assert(This.GetIntersectionPoint(That) == new Point(1, 2), "intersection point should be (1, 2)");
            sb.AppendLine("intersection point test 13");
            That = new LineSegment(new Point(2, 2), new Point(2, 2));
            Debug.Assert(This.GetIntersectionPoint(That) == null, "intersection point should be null");

            test_log[time] = sb.ToString();
            return test_log[time];
        }
Exemplo n.º 11
0
        /// <summary>
        /// 线段类的单元测试
        /// </summary>
        /// <param name="epsilon"></param>
        /// <returns></returns>
        public static string RunTest(double epsilon = 1e-3)
        {
            DateTime time = DateTime.Now;
            StringBuilder sb = new StringBuilder("Scatter Class Test Begin at " + time.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
            test_log.Add(time, "");

            //test IsSegmentsLegal
            sb.AppendLine("----------------------------");
            sb.AppendLine("IsSegmentsLegal test 1");
            LineSegment[] segs = null;
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == false, "result should be false");
            sb.AppendLine("IsSegmentsLegal test 2");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(1, 1)) };
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == false, "result should be false");
            sb.AppendLine("IsSegmentsLegal test 3");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(1, 1)), 
                                       new LineSegment(new Point(1, 1), new Point(1, 0)), 
                                       new LineSegment(new Point(1, 0), new Point(0, 0)) };
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == true, "result should be true");
            sb.AppendLine("IsSegmentsLegal test 4");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 1)),
                                       new LineSegment(new Point(0, 1), new Point(1, 1)),
                                       new LineSegment(new Point(1, 1), new Point(1, 0)), 
                                       new LineSegment(new Point(1, 0), new Point(0, 0)) };
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == true, "result should be true");
            sb.AppendLine("IsSegmentsLegal test 5");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 1)),
                                       new LineSegment(new Point(1, 1), new Point(1, 0)), 
                                       new LineSegment(new Point(1, 0), new Point(0, 0)) };
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == false, "result should be false");
            sb.AppendLine("IsSegmentsLegal test 6");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 1)),
                                       new LineSegment(new Point(0, 1), new Point(1, 1)),
                                       new LineSegment(new Point(1, 1), new Point(1, 0)), 
                                       new LineSegment(new Point(1, 0), new Point(0, 0)),
                                       new LineSegment(new Point(0, 0), new Point(0, 1)) };
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == false, "result should be false");
            sb.AppendLine("IsSegmentsLegal test 7");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 1)),
                                       new LineSegment(new Point(0, 1), new Point(1, 1)),
                                       new LineSegment(new Point(1, 1), new Point(1, 0)), 
                                       new LineSegment(new Point(1, 0), new Point(0, 0)),
                                       new LineSegment(new Point(0, 0), new Point(2, 1)) };
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == false, "result should be false");
            sb.AppendLine("IsSegmentsLegal test 8");
            segs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 2)),
                                       new LineSegment(new Point(0, 2), new Point(1, 2)),
                                       new LineSegment(new Point(1, 2), new Point(1, 4)), 
                                       new LineSegment(new Point(1, 4), new Point(0, 4)),
                                       new LineSegment(new Point(0, 4), new Point(0, 6)),
                                       new LineSegment(new Point(0, 6), new Point(2, 6)),
                                       new LineSegment(new Point(2, 6), new Point(2, 4)),
                                       new LineSegment(new Point(2, 4), new Point(4, 4)),
                                       new LineSegment(new Point(4, 4), new Point(4, 2)),
                                       new LineSegment(new Point(4, 2), new Point(2, 2)), 
                                       new LineSegment(new Point(2, 2), new Point(2, 0)), 
                                       new LineSegment(new Point(2, 0), new Point(0, 0))};
            Debug.Assert(new Scatter().IsSegmentsLegal(segs) == true, "result should be true");
            sb.AppendLine("IsSegmentsLegal test 9");
            Point[] pts = new Point[] { new Point(0, 0), new Point(0, 2),
                                        new Point(1, 2),
                                        new Point(1, 4), 
                                        new Point(0, 4),
                                        new Point(0, 6),
                                        new Point(2, 6),
                                        new Point(2, 4),
                                        new Point(4, 4),
                                        new Point(4, 2),
                                        new Point(2, 2), 
                                        new Point(2, 0) };
            Scatter s = new Scatter(pts);

            //test BorderRect
            sb.AppendLine("----------------------------");
            LineSegment[] sgs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 2)),
                                                   new LineSegment(new Point(0, 2), new Point(1, 2)),
                                                   new LineSegment(new Point(1, 2), new Point(1, 4)), 
                                                   new LineSegment(new Point(1, 4), new Point(0, 4)),
                                                   new LineSegment(new Point(0, 4), new Point(0, 6)),
                                                   new LineSegment(new Point(0, 6), new Point(2, 6)),
                                                   new LineSegment(new Point(2, 6), new Point(2, 4)),
                                                   new LineSegment(new Point(2, 4), new Point(4, 4)),
                                                   new LineSegment(new Point(4, 4), new Point(4, 2)),
                                                   new LineSegment(new Point(4, 2), new Point(2, 2)), 
                                                   new LineSegment(new Point(2, 2), new Point(2, 0)), 
                                                   new LineSegment(new Point(2, 0), new Point(0, 0))};
            s = new Scatter(sgs, SCATTER_SEG_DIRECTION.ANTI_CLOCKWISE);
            sb.AppendLine("BorderRect test 1");
            Debug.Assert(s.BorderRect == new Rectangle(0, 0, 4, 6), "rect should be (0, 0, 4, 6)");
            sb.AppendLine("BorderRect test 2");
            sgs = new LineSegment[] { new LineSegment(new Point(2, 0), new Point(0, 2)),
                                    new LineSegment(new Point(0, 2), new Point(2, 4)),
                                    new LineSegment(new Point(2, 4), new Point(4, 2)), 
                                    new LineSegment(new Point(4, 2), new Point(2, 0)) };
            s = new Scatter(sgs, SCATTER_SEG_DIRECTION.ANTI_CLOCKWISE);
            Debug.Assert(s.BorderRect == new Rectangle(0, 0, 4, 4), "rect should be (0, 0, 4, 4)");

            //test GetClosestReflectionPoint
            sb.AppendLine("----------------------------");
            sgs = new LineSegment[] { new LineSegment(new Point(0, 0), new Point(0, 2)),
                                    new LineSegment(new Point(0, 2), new Point(1, 2)),
                                    new LineSegment(new Point(1, 2), new Point(1, 4)), 
                                    new LineSegment(new Point(1, 4), new Point(0, 4)),
                                    new LineSegment(new Point(0, 4), new Point(0, 6)),
                                    new LineSegment(new Point(0, 6), new Point(2, 6)),
                                    new LineSegment(new Point(2, 6), new Point(2, 4)),
                                    new LineSegment(new Point(2, 4), new Point(4, 4)),
                                    new LineSegment(new Point(4, 4), new Point(4, 2)),
                                    new LineSegment(new Point(4, 2), new Point(2, 2)), 
                                    new LineSegment(new Point(2, 2), new Point(2, 0)), 
                                    new LineSegment(new Point(2, 0), new Point(0, 0))};
            s = new Scatter(sgs, SCATTER_SEG_DIRECTION.ANTI_CLOCKWISE);
            LineSegment ss;
            sb.AppendLine("GetClosestReflectionPoint test 1");
            LineSegment seg = new LineSegment(new Point(0, 10), new Point(10, 10));
            Debug.Assert(s.GetClosestReflectionPoint(seg, out ss) == null && ss == null, "point should be null");
            sb.AppendLine("GetClosestReflectionPoint test 2");
            seg = new LineSegment(new Point(0, 2), new Point(8, 2));
            //Point? aaa = s.GetClosestReflectionPoint(seg, out ss);
            //Debug.Assert(aaa.Value == new Point(0, 2));
            Debug.Assert(s.GetClosestReflectionPoint(seg, out ss) == new Point(0, 2), "point should be (0, 2)");

            test_log[time] = sb.ToString();
            return test_log[time];
        }
Exemplo n.º 12
0
 public LineSegmentCollection(LineSegment[] l)
 {
     this.segs = l;
 }
Exemplo n.º 13
0
 public LineSegEnumerator(LineSegment[] seg)
 {
     this.segs = seg.Clone() as LineSegment[];
 }
Exemplo n.º 14
0
 /// <summary>
 /// 获取两线段交点,若无唯一交点则返回null
 /// </summary>
 /// <param name="that"></param>
 /// <returns></returns>
 public Point? GetIntersectionPoint(LineSegment that)
 {
     if (!this.JudgeIntersection(that))
         return null;
     if(this.Vector[0] == 0) //本线段是垂直的
     {
         double k = that.Vector[1] / that.Vector[0];
         double b = -that.HeadPosition.Y - k * that.HeadPosition.X;
         double a = this.HeadPosition.X;
         return new Point((int)Math.Round(a), -(int)Math.Round(k * a + b));
     }
     if (that.Vector[0] == 0) //另一线段是垂直的
     {
         double k = this.Vector[1] / this.Vector[0];
         double b = -this.HeadPosition.Y - k * this.HeadPosition.X;
         double a = that.HeadPosition.X;
         return new Point((int)Math.Round(a), -(int)Math.Round(k * a + b));
     }
     double k1 = this.Vector[1] / this.Vector[0];
     double b1 = -this.HeadPosition.Y - k1 * this.HeadPosition.X;
     double k2 = that.Vector[1] / that.Vector[0];
     double b2 = -that.HeadPosition.Y - k2 * that.HeadPosition.X;
     double x = (b2 - b1) / (k1 - k2);
     double y = k1 * x + b1;
     return new Point((int)Math.Round(x), -(int)Math.Round(y));
 }
Exemplo n.º 15
0
 /// <summary>
 /// 判断两线段是否相交(只当有且仅有一个交点时才返回true)
 /// </summary>
 /// <param name="that"></param>
 /// <returns></returns>
 public bool JudgeIntersection(LineSegment that)
 {
     if (this.CrossProduct(that) == 0)    //两线段同向或反向
         return false;
     if (!this.JudgeRectIntersection(that))  //快速排斥实验
         return false;
     //跨立实验
     LineSegment q1p1 = new LineSegment(that.HeadPosition, this.TailPosition);
     LineSegment q1p2 = new LineSegment(that.TailPosition, this.TailPosition);
     if (q1p1.CrossProduct(this) * q1p2.CrossProduct(this) > 0)
         return false;
     LineSegment p2q2 = new LineSegment(this.HeadPosition, that.TailPosition);
     LineSegment p2q1 = new LineSegment(this.TailPosition, that.TailPosition);
     if (p2q2.CrossProduct(that) * p2q1.CrossProduct(that) > 0)
         return false;
     return true;
 }
Exemplo n.º 16
0
 /// <summary>
 /// 判断以两线段作为对角线的两个矩形是否相交
 /// </summary>
 /// <param name="that"></param>
 public bool JudgeRectIntersection(LineSegment that)
 {
     return (this.Top <= that.Bottom) && (that.Top <= this.Bottom) &&
            (this.Right >= that.Left) && (that.Right >= this.Left);
 }
Exemplo n.º 17
0
 /// <summary>
 /// 判断给定点是否在散射体内部
 /// 采用射线法:从给定点作一条射线,若与多边形有偶数个交点
 /// 则在多边形外,若有奇数个交点则在内部
 /// </summary>
 /// <param name="pt"></param>
 /// <returns></returns>
 public bool IsPointInside(Point pt)
 {
     if(this.BorderRect.IntersectsWith(new Rectangle(pt, new Size(1, 1))))   //  先判断点是否在边界矩形内
     {
         int l = (int)(Math.Pow(this.BorderRect.Width, 2) + Math.Pow(this.BorderRect.Height, 2)) + 2;
         LineSegment seg_l = new LineSegment(pt, new Point(pt.X + l, pt.Y));
         int pt_num = 0;
         foreach (LineSegment seg in this.BorderSegments)
             if (seg_l.JudgeIntersection(seg))
                 pt_num++;
         return pt_num % 2 == 1;
     }
     return false;
 }
Exemplo n.º 18
0
 private MWNumericArray[][] wrap_func(int num, int ray_num, Transmitter.EMIT_OPTION emit_opt, Signal_Type signal_type, int sample_cnt)
 {
     MWNumericArray[][] signals = new MWNumericArray[num][];
     while (num-- > 0)
     {
         foreach (Transmitter trans in this.viz_rps.Map.Transmitters)
         {
             trans.Reset();
             trans.EmitRays(ray_num, emit_opt);   //各发射机辐射波束
             foreach (Receiver rec in this.viz_rps.Map.Receivers)
             {
                 rec.Reset();
                 if (this.viz_rps.Map.IsLOS(trans.Location, rec.Location))    //若存在LOS情况
                 {
                     LineSegment segLOS = new LineSegment(trans.Location, rec.Location);
                     Ray rayLOS = new Ray(trans, new LineSegment[] { segLOS }, segLOS.DirectionRadian, trans.CenterFrequency, trans.EmitPower * trans.Gain, trans.EmitPower * trans.Gain, true);
                     rec.AcceptRay(rayLOS, this.viz_rps.Map);
                 }
             }
             foreach (Ray ray in trans.Rays)  //更新每条ray, 若被接收,则更新接收它的接收机
             {
                 Receiver rec = null;
                 while (!ray.Update(this.viz_rps.Map, out rec)) ;
                 if (rec != null)
                     rec.AcceptRay(ray, this.viz_rps.Map);
             }
         }
         FingerprintGenerator fg = new FingerprintGenerator();
         Dictionary<Fingerprint_Name, Fingerprint> result = new Dictionary<Fingerprint_Name, Fingerprint>();
         List<double>[] ray_angles = new List<double>[this.viz_rps.Map.ReceiverCount];
         List<double>[] ray_strengths = new List<double>[this.viz_rps.Map.ReceiverCount];
         List<double>[] ray_taus = new List<double>[this.viz_rps.Map.ReceiverCount];
         int kk = 0;
         foreach (Receiver rec in this.viz_rps.Map.Receivers)
         {
             ray_angles[kk] = new List<double>();
             ray_strengths[kk] = new List<double>();
             ray_taus[kk] = new List<double>();
             foreach (Ray ray in rec.Rays)
             {
                 ray_angles[kk].Add(ray.CurrentDirection);
                 ray_strengths[kk].Add(ray.FadedPower(this.viz_rps.Map.MeterPixelRatio));
                 ray_taus[kk].Add(ray.GetLifeSpan(this.viz_rps.Map.MeterPixelRatio));
             }
             kk++;
         }
         double[] args = null;
         switch (signal_type)
         {
             case Signal_Type.PSEUDO_DOPPLER:
                 //if (this.receivers.First().Value.Antenna.AntType != AntennaType.PSEUDO_DOPPLER)
                 //    return null;
                 Antenna_PseudoDoppler ant = this.viz_rps.Map.receivers.First().Value.Antenna as Antenna_PseudoDoppler;
                 args = fg.PackArgs_PseudoDoppler(this.viz_rps.Map.receivers.First().Value.FrequencyLow, this.viz_rps.Map.receivers.First().Value.CenterFrequency,
                                                  this.viz_rps.Map.receivers.First().Value.SampleFrequency, ant.SwitchFrequency, ant.Radius, ant.AntennaNumber);
                 break;
         }
         signals[num - 1] = new MWNumericArray[ray_angles.GetLength(0)];
         for (int i = 0; i < signals.Length; i++)
         {
             signals[num - 1][i] = new FingerprintGenerator().GenerateSignal_MW(signal_type, ray_angles[i].ToArray(), ray_strengths[i].ToArray(), ray_taus[i].ToArray(), sample_cnt, args);
         }
     }
     return signals;
 }
Exemplo n.º 19
0
 /// <summary>
 /// 获取反射点坐标及碰撞线段
 /// </summary>
 /// <param name="seg"></param>
 /// <returns></returns>
 public Point? GetClosestReflectionPoint(LineSegment seg, out LineSegment collision_seg)
 {
     if (!this.BorderRect.IntersectsWith(new Rectangle(seg.Left, seg.Top, seg.Right - seg.Left, seg.Bottom - seg.Top)))  //先用矩形进行快速判断
     {
         collision_seg = null;
         return null;
     }
     Dictionary<Point?, int> pt_idx_dict = new Dictionary<Point?, int>();
     for (int i = 0; i < this.border_segments.Length; i++)
     {
         Point? pt = seg.GetIntersectionPoint(this.border_segments[i]);
         if (pt != null && !pt_idx_dict.Keys.Contains(pt))
             pt_idx_dict.Add(pt, i);
     }
     if(pt_idx_dict.Count == 0)  //没有交点
     {
         collision_seg = null;
         return null;
     }
     KeyValuePair<Point?, int> pt_idx = new KeyValuePair<Point?, int>();
     double min_d2 = double.PositiveInfinity;
     foreach(KeyValuePair<Point?, int> kvp in pt_idx_dict)
     {
         double dist2 = Math.Pow(kvp.Key.Value.X - seg.TailPosition.X, 2) + Math.Pow(kvp.Key.Value.Y - seg.TailPosition.Y, 2);
         if(dist2 > 0 && dist2 < min_d2)
         {
             min_d2 = dist2;
             pt_idx = kvp;
         }
     }
     collision_seg = this.border_segments[pt_idx.Value];
     return pt_idx.Key;
 }
Exemplo n.º 20
0
 /// <summary>
 /// 计算与另一向量的点积
 /// </summary>
 /// <param name="that"></param>
 /// <returns></returns>
 public double DotProduct(LineSegment that)
 {
     double sum = 0;
     for (int i = 0; i < this.Vector.Length; i++)
         sum += this.Vector[i] * that.Vector[i];
     return sum;
 }
Exemplo n.º 21
0
Arquivo: Ray.cs Projeto: Yinzhe-Qi/RPS
 /// <summary>
 /// 构造已包含一段路径的波束
 /// </summary>
 /// <param name="transm"></param>
 /// <param name="segs"></param>
 /// <param name="current_direction"></param>
 /// <param name="freq"></param>
 /// <param name="init_power"></param>
 /// <param name="current_power"></param>
 /// <param name="beam_width"></param>
 public Ray(Transmitter transm, LineSegment[] segs, double current_direction, double freq, double init_power, double current_power, bool is_direct=false, double beam_width = Math.PI / 3)
 {
     this.transmitter = transm;
     foreach(LineSegment seg in segs)
         this.path_segs.Add(seg);
     this.current_direction = current_direction;
     this.center_frequency = freq;
     this.init_power = init_power;
     this.power = current_power;
     this.beam_width = beam_width;
 }
Exemplo n.º 22
0
 /// <summary>
 /// 计算与另一向量的叉积
 /// </summary>
 /// <param name="that"></param>
 /// <returns></returns>
 public double CrossProduct(LineSegment that)
 {
     return this.Vector[0] * that.Vector[1] - that.Vector[0] * this.Vector[1];
 }
Exemplo n.º 23
0
        private void AutoTest(object sender, DoWorkEventArgs e)
        {
            object[] param = (object[])e.Argument;
            int avg_cnt = (int)param[0];
            int KNN_K = (int)param[1];
            int KNNHC_K = (int)param[2];
            string db_file_name = (string)param[3];
            string data_file_name = (string)param[4];
            double start_snr = (double)param[5];
            double stop_snr = (double)param[6];
            double step_snr = (double)param[7];
            int ray_num = (int)param[8];
            Transmitter.EMIT_OPTION emit_opt = (Transmitter.EMIT_OPTION)param[9];
            Noise_Type noise_type = (Noise_Type)param[10];
            double alpha = (double)param[11];
            int sample_cnt = (int)param[12];
            Signal_Type signal_type = (Signal_Type)param[13];

            System.IO.FileStream data_fs = new FileStream(data_file_name, FileMode.Create, FileAccess.Write, FileShare.Read);

            System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            FileStream fs = new FileStream(db_file_name, FileMode.Open, FileAccess.Read, FileShare.Read);
            FingerprintDataBase fdb = (FingerprintDataBase)formatter.Deserialize(fs);
            fs.Close();

            Point pt_real = new Point();
            foreach (Transmitter trans in this.viz_rps.Map.Transmitters)
                pt_real = trans.Location;

            double[] snrs = new double[(int)((stop_snr - start_snr) / step_snr) + 1];
            for(int i = 0; i < snrs.Length; i++)
                snrs[i] = start_snr + i * step_snr;

            foreach (Transmitter trans in this.viz_rps.Map.Transmitters)
            {
                trans.Reset();
                trans.EmitRays(ray_num, emit_opt);   //各发射机辐射波束
                foreach (Receiver rec in this.viz_rps.Map.Receivers)
                {
                    rec.Reset();
                    if (this.viz_rps.Map.IsLOS(trans.Location, rec.Location))    //若存在LOS情况
                    {
                        LineSegment segLOS = new LineSegment(trans.Location, rec.Location);
                        Ray rayLOS = new Ray(trans, new LineSegment[] { segLOS }, segLOS.DirectionRadian, trans.CenterFrequency, trans.EmitPower * trans.Gain, trans.EmitPower * trans.Gain, true);
                        rec.AcceptRay(rayLOS, this.viz_rps.Map);
                    }
                }
                foreach (Ray ray in trans.Rays)  //更新每条ray, 若被接收,则更新接收它的接收机
                {
                    Receiver rec = null;
                    while (!ray.Update(this.viz_rps.Map, out rec)) ;
                    if (rec != null)
                        rec.AcceptRay(ray, this.viz_rps.Map);
                }
            }
            FingerprintGenerator fg = new FingerprintGenerator();
            Dictionary<Fingerprint_Name, Fingerprint> result = new Dictionary<Fingerprint_Name, Fingerprint>();
            List<double>[] ray_angles = new List<double>[this.viz_rps.Map.ReceiverCount];
            List<double>[] ray_strengths = new List<double>[this.viz_rps.Map.ReceiverCount];
            List<double>[] ray_taus = new List<double>[this.viz_rps.Map.ReceiverCount];
            int kk = 0;
            foreach (Receiver rec in this.viz_rps.Map.Receivers)
            {
                ray_angles[kk] = new List<double>();
                ray_strengths[kk] = new List<double>();
                ray_taus[kk] = new List<double>();
                foreach (Ray ray in rec.Rays)
                {
                    ray_angles[kk].Add(ray.CurrentDirection);
                    ray_strengths[kk].Add(ray.FadedPower(this.viz_rps.Map.MeterPixelRatio));
                    ray_taus[kk].Add(ray.GetLifeSpan(this.viz_rps.Map.MeterPixelRatio));
                }
                kk++;
            }
            double[] args = null;
            switch (signal_type)
            {
                case Signal_Type.PSEUDO_DOPPLER:
                    //if (this.receivers.First().Value.Antenna.AntType != AntennaType.PSEUDO_DOPPLER)
                    //    return null;
                    Antenna_PseudoDoppler ant = this.viz_rps.Map.receivers.First().Value.Antenna as Antenna_PseudoDoppler;
                    args = fg.PackArgs_PseudoDoppler(this.viz_rps.Map.receivers.First().Value.FrequencyLow, this.viz_rps.Map.receivers.First().Value.CenterFrequency,
                                                     this.viz_rps.Map.receivers.First().Value.SampleFrequency, ant.SwitchFrequency, ant.Radius, ant.AntennaNumber);
                    break;
            }
            MWNumericArray[] signals = new MWNumericArray[ray_angles.GetLength(0)];
            for (int i = 0; i < signals.Length; i++)
            {
                signals[i] = new FingerprintGenerator().GenerateSignal_MW(signal_type, ray_angles[i].ToArray(), ray_strengths[i].ToArray(), ray_taus[i].ToArray(), sample_cnt, args);                
            }

            for (int n = 0; n < snrs.Length; n++)
            {
                double[] sum_errs = new double[6];//SA_single, SA_KNN, SA_KNNHC, S_single, S_KNN, S_KNNHC
                for(int i = 0; i < avg_cnt; i++)
                {
                    MWNumericArray[] n_signals = new MWNumericArray[signals.Length];
                    for (int l = 0; l < signals.Length; l++)
                    {
                        n_signals[l] = new FingerprintGenerator().AddNoise_MW(noise_type, signals[l], new double[] { snrs[n] });
                    }
                    Dictionary<Fingerprint_Name, Fingerprint> res = new Dictionary<Fingerprint_Name,Fingerprint>();
                    for (int j = 0; j < Enum.GetNames(new Fingerprint_Name().GetType()).Length; j++ )
                        res[(Fingerprint_Name)j] = new FingerprintGenerator().GenerateFingerprint(signal_type, (Fingerprint_Name)j, n_signals, args);
                    this.current_fingerprint = res;

                    //SA_single
                    PointF loc_SA_single = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.SA], FingerprintDataBase.Match_Strategy.Single, null);
                    sum_errs[0] += new MathUtils.MathUtility().point_dist(pt_real, loc_SA_single);

                    //SA_KNN
                    PointF loc_SA_KNN = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.SA], FingerprintDataBase.Match_Strategy.KNN, new double[] { KNN_K });
                    sum_errs[1] += new MathUtils.MathUtility().point_dist(pt_real, loc_SA_KNN);

                    //SA_KNNHC
                    PointF loc_SA_KNNHC = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.SA], FingerprintDataBase.Match_Strategy.KNN_HC, new double[] { KNNHC_K });
                    sum_errs[2] += new MathUtils.MathUtility().point_dist(pt_real, loc_SA_KNNHC);

                    //S_single
                    PointF loc_S_single = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.S], FingerprintDataBase.Match_Strategy.Single, null);
                    sum_errs[3] += new MathUtils.MathUtility().point_dist(pt_real, loc_S_single);

                    //S_KNN
                    PointF loc_S_KNN = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.S], FingerprintDataBase.Match_Strategy.KNN, new double[] { KNN_K });
                    sum_errs[4] += new MathUtils.MathUtility().point_dist(pt_real, loc_S_KNN);

                    //S_KNNHC
                    PointF loc_S_KNNHC = fdb.MatchLocation(this.current_fingerprint[Fingerprint_Name.S], FingerprintDataBase.Match_Strategy.KNN_HC, new double[] { KNNHC_K });
                    sum_errs[5] += new MathUtils.MathUtility().point_dist(pt_real, loc_S_KNNHC);

                    bgAutoTest.ReportProgress((int)((n * avg_cnt + i + 1) / (double)(avg_cnt * snrs.Length) * 100));
                    System.Threading.Thread.Sleep(200);
                }

                for (int m = 0; m < sum_errs.Length; m++ )
                {
                    double err = sum_errs[m] / avg_cnt;
                    byte[] tmp = Encoding.ASCII.GetBytes(err.ToString("0.00") + "\t");
                    data_fs.Write(tmp, 0, tmp.Length);
                }
                byte[] rn = Encoding.ASCII.GetBytes("\r\n");
                data_fs.Write(rn, 0, rn.Length);
            }

            data_fs.Close();
            e.Result = true;
        }
Exemplo n.º 24
0
Arquivo: Ray.cs Projeto: Yinzhe-Qi/RPS
 /// <summary>
 /// 根据所在地图更新波束(完成一次碰撞的结算)
 /// </summary>
 /// <param name="map">所在地图</param>
 /// <param name="sink">返回被接收的接收机,若没被接收则返回null</param>
 /// <returns>若生命周期结束(被接收 || 强度过低 || 射出地图边界)则返回true,否则返回false</returns>
 public bool Update(Map map, out Receiver sink)
 {
     double len2 = Math.Pow(map.Size.Width, 2) + Math.Pow(map.Size.Height, 2);
     double[] vector = new double[] { Math.Cos(this.CurrentDirection), Math.Sin(this.CurrentDirection) };
     vector[0] *= len2;
     vector[1] *= len2;
     Point dest_pt = new Point((int)Math.Round(this.CurrentLocation.X + vector[0]), 
                               (int)Math.Round(this.CurrentLocation.Y - vector[1]));
     LineSegment shoot_seg = new LineSegment(this.CurrentLocation, dest_pt);
     if (this.path_segs.Count > 0)   //至少要反射一次才能被接收机接收
     {
         List<Receiver> tmp_rec = new List<Receiver>();
         foreach (Receiver rec in map.Receivers)
         {
             if (map.IsLOS(this.CurrentLocation, rec.Location))  //若接收机与当前点为视距且在波束宽度内
                 if(new LineSegment(this.CurrentLocation, rec.Location).Angle(shoot_seg) < this.BeamWidth / 2)
                     tmp_rec.Add(rec);
         }
         if(tmp_rec.Count > 0)
         {
             sink = tmp_rec[new Random().Next(tmp_rec.Count)];   //随机被一满足条件的接收机接收
             LineSegment l = new LineSegment(this.CurrentLocation, sink.Location);
             this.path_segs.Add(l);
             this.current_direction = this.path_segs.Last().DirectionRadian;
             return true;
         }
     }
     Point? tmp_pt = null;
     LineSegment tmp_seg = null;
     SCATTER_SEG_DIRECTION tmp_dir = SCATTER_SEG_DIRECTION.CLOCKWISE;
     double tmp_dc = 0;
     tmp_pt = map.GetClosestReflectionPoint(shoot_seg, out tmp_seg, out tmp_dir, out tmp_dc);
     if(tmp_pt == null)  //无碰撞,射出地图边界
     {
         sink = null;
         this.path_segs.Add(shoot_seg);
         this.current_direction = shoot_seg.DirectionRadian;
         return true;
     }
     else    //发生碰撞
     {
         LineSegment l = new LineSegment(this.CurrentLocation, tmp_pt.Value);
         this.path_segs.Add(l);
         double reflect_angle = RadioPropagationModel.SpecularReflection(shoot_seg, tmp_seg, tmp_dir);
         if(tmp_pt == tmp_seg.HeadPosition || tmp_pt == tmp_seg.TailPosition)
         {
             reflect_angle = shoot_seg.DirectionRadian + Math.PI;
             if(reflect_angle > Math.PI * 2)
                 reflect_angle -= Math.PI * 2;
         }
         this.current_direction = RadioPropagationModel.DiffuseReflection(reflect_angle);
         this.power = RadioPropagationModel.ReflectionLoss(this.UnfadedPower, shoot_seg, tmp_seg, tmp_dc);
         sink = null;
         return this.FadedPower(map.MeterPixelRatio) < map.RayPowerThreshold * this.InitPower;    //若碰撞后强度过低则返回true结束,否则返回false,可以继续Update
     }
 }