public override bool IsIntersect(Ray ray, Matrices transform) { //if (a == 0) //{ // ray.IntersectDistance = -c / b; // return true; //} //else if (D < 0) // return false; //else //{ Vector L = ray.Position - this.center; double a = Vector.DotProduct(ray.Direction, ray.Direction); double b = 2 * Vector.DotProduct(ray.Direction, L); double c = Vector.DotProduct(L, L) - this.radius * this.radius; double D = b * b - 4 * a * c; double q = (b > 0) ? -0.5 * (b + Math.Sqrt(D)) : -0.5 * (b - Math.Sqrt(D)); double[] t = new double[2]; t[0] = q / a; t[1] = c/q; //if (t[0] > 0 && t[1] > 0) //{ Vector temp1 = ray.Direction * Math.Min(t[0], t[1]); Matrices temp2 = new Matrices(temp1.x, temp1.y, temp1.z, 1); temp2 = transform.Matrix * temp2; temp1.x = temp2.Matrix[0, 0]; temp1.y = temp2.Matrix[1, 0]; temp1.z = temp2.Matrix[2, 0]; ray.IntersectDistance = temp1.Distance(); //ray.IntersectDistance = Math.Min(t[0], t[1]); return true; //} //else if (t[0] < 0 && t[1] > 0) //{ // ray.IntersectDistance = t[1]; // return true; //} //else if (t[0] > 0 && t[1] < 0) //{ // ray.IntersectDistance = t[0]; // return true; //} //else return false; //} }
public override bool IsIntersect(Ray ray, Matrices transform) { Vector temp = Vector.CrossProduct((this.pointC - this.pointA),(this.pointB - this.pointA)); Vector n = temp / temp.Distance(); double a = Vector.DotProduct(this.pointA, n); double b = Vector.DotProduct(ray.GetPosition(), n); double c = Vector.DotProduct(ray.GetDirection(), n); double t = (a - b) / c; Vector pointT = ray.GetPosition() + (ray.GetDirection() * t); if (PointInTriangle(pointT)) { Vector temp1 = ray.Direction * t; Matrices temp2 = new Matrices(temp1.x, temp1.y, temp1.z, 1); temp2 = transform.Matrix * temp2; temp1.x = temp2.Matrix[0, 0]; temp1.y = temp2.Matrix[1, 0]; temp1.z = temp2.Matrix[2, 0]; ray.IntersectDistance = temp1.Distance() ; //ray.IntersectDistance = t ; //ray.IntersectWith = this; return true; } else return false; }
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; }
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; }
/// <summary> /// 接收一个波束,并更新接收机参数,只有与接收频率相同的波束才会产生影响 /// </summary> /// <param name="ray"></param> public void AcceptRay(Ray ray, Map map) { if(ray.CenterFrequency == this.CenterFrequency) { this.rays.Add(ray); this.total_power += ray.FadedPower(map.MeterPixelRatio) * this.Gain; } }
public static Ray Intersection(Ray ray, Scene scene) { double mindist = double.MaxValue; foreach(Object obj in scene.ObjectList) { //for (int i = 0; i < obj.Transform.Matrix.GetLength(0); i++) //{ // for (int j = 0; j < obj.Transform.Matrix.GetLength(1); j++) // Console.Write("\t" + obj.Transform.Matrix[i, j]); // Console.WriteLine(); //} // //Console.WriteLine(); // //for(int i = 0 ; i < obj.Transform.Invers.GetLength(0) ; i++) //{ // for(int j = 0 ; j < obj.Transform.Invers.GetLength(1) ; j++) // Console.Write("\t" + obj.Transform.Invers[i,j]); // Console.WriteLine(); //} //Console.WriteLine(); //Console.WriteLine("---------------------------------"); //Console.WriteLine(); //Console.ReadKey(); Matrices pos = new Matrices(ray.Position.x, ray.Position.y, ray.Position.z, 1); //Console.WriteLine(pos.Matrix[0, 0] + " " + pos.Matrix[1, 0] + " " + pos.Matrix[2, 0] + " " + pos.Matrix[3, 0]); pos = obj.Transform.Invers * pos; //Console.WriteLine(pos.Matrix[0, 0] + " " + pos.Matrix[1, 0] + " " + pos.Matrix[2, 0] + " " + pos.Matrix[3, 0]); //Console.ReadKey(); Matrices dir = new Matrices(ray.Direction.x, ray.Direction.y, ray.Direction.z, 0); dir = obj.Transform.Invers * dir; //Debug-------------------------------------------------- //Console.WriteLine(ray.Position.x + " " + ray.Position.y + " " + ray.Position.z); //Console.WriteLine(pos.Matrix[0, 0] + " " + pos.Matrix[1, 0] + " " + pos.Matrix[2, 0]); //Console.ReadKey(); //------------------------------------------------------- ray.Position.x = pos.Matrix[0,0]; ray.Position.y = pos.Matrix[1,0]; ray.Position.z = pos.Matrix[2,0]; ray.Direction.x = dir.Matrix[0, 0]; ray.Direction.y = dir.Matrix[1, 0]; ray.Direction.z = dir.Matrix[2, 0]; if (obj.IsIntersect(ray,obj.Transform) && ray.IntersectDistance < mindist) { mindist = ray.IntersectDistance; ray.IntersectWith = obj; } // kali transform biasa ray direction(1) + position(0) pos = obj.Transform.Matrix * pos; dir = obj.Transform.Matrix * dir; ray.Position.x = pos.Matrix[0, 0]; ray.Position.y = pos.Matrix[1, 0]; ray.Position.z = pos.Matrix[2, 0]; ray.Direction.x = dir.Matrix[0, 0]; ray.Direction.y = dir.Matrix[1, 0]; ray.Direction.z = dir.Matrix[2, 0]; } ray.IntersectDistance = mindist; return ray; }
public abstract bool IsIntersect(Ray ray, Matrices transform);
/// <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; }
public override bool Hit(Ray r, float tMin, float tMax, out HitRecord rec) { //Plane plane = Plane.CreateFromVertices(points[0], points[1], points[2]); var normal = Vector3.Normalize(Vector3.Cross((points[0] - points[1]), (points[1] - points[2]))); r.direction = Vector3.Normalize(r.direction); rec = new HitRecord(); //面片背面剔除 var cos = Vector3.Dot(r.direction, normal); if (cos == 0) { return(false); } if (cullBack && cos < 0) { return(false); } //计算t(距离) rec.t = Vector3.Dot(points[0] - r.from, normal) / Vector3.Dot(r.direction, normal); //rec.t = (Vector3.Dot(normal,points[0]) - Vector3.Dot(normal, r.from) )/ Vector3.Dot(normal,r.direction ); if (float.IsNaN(rec.t) || rec.t < tMin || rec.t > tMax) { return(false); } //计算p(平面交点) var p = r.from + rec.t * r.direction; //光栅剔除 points.Add(points[0]); Vector3 firstTowards = Vector3.Zero; for (int i = 0; i < points.Count - 1; i++) { var oa = Vector3.Normalize(points[i] - points[i + 1]); var op = Vector3.Normalize(points[i] - p); var towards = Vector3.Normalize(Vector3.Cross(oa, op)); if (firstTowards == Vector3.Zero) { firstTowards = towards; } else if (Vector3.Dot(firstTowards, towards) < 0.999999) { points.RemoveAt(points.Count - 1); return(false); } } points.RemoveAt(points.Count - 1); rec.position = p; //是否有normal记录 if (normals.Count == points.Count) { if (uvs.Count == 3) { rec.normal = GetAvgNormalInTriangle(p); } else { rec.normal = GetAvgNormal(p); } } else { rec.normal = normal; } //是否有uv记录 if (uvs.Count == points.Count) { if (uvs.Count == 3) { rec.uv = GetAvgUVInTriangle(p); } else { rec.uv = GetAvgUV(p); } } rec.material = material; return(true); }
public RayEnumerator(Ray[] ray) { this.rays = ray.Clone() as Ray[]; }
public RayCollection(Ray[] r) { this.rays = r; }