static public CartesianCoordinates Sphere2Kart(SphericalCoordinates spher) { double x = (spher.r * Math.Sin(spher.tetha * Math.PI / 180) * Math.Cos(spher.phi * Math.PI / 180)); double y = (spher.r * Math.Sin(spher.tetha * Math.PI / 180) * Math.Sin(spher.phi * Math.PI / 180)); double z = (spher.r * Math.Cos(spher.tetha * Math.PI / 180)); return(new CartesianCoordinates(x: x, y: y, z: z)); }
public GeometricalObject(CartesianCoordinates cartPoint, SphericalCoordinates spherPoint, double radius, double side) { this.x = cartPoint.x; this.y = cartPoint.y; this.z = cartPoint.z; this.r = spherPoint.r; this.tetha = spherPoint.tetha; this.phi = spherPoint.phi; this.radius = radius; this.side = side; }
public void GreatLoop(object param) { var _SimulationParameters = Cast(param, new { Ax = 0.0, Ay = 0.0, Az = 0.0, Ar = 0.0 }); double xcObst = _SimulationParameters.Ax; double ycObst = _SimulationParameters.Ay; double zcObst = _SimulationParameters.Az; double rcObst = _SimulationParameters.Ar; CartesianCoordinates ballC = new CartesianCoordinates(x: xcObst, y: ycObst, z: zcObst); ballStruct = new GeometricalObject(cartPoint: ballC, spherPoint: Elementary.Cart2Sphere(ballC), radius: rcObst, side: 0); Random localRandom = new Random(Form1.GLOBAL_RANDOM_VAR.Next() & DateTime.Now.Millisecond); double[,] pixelHITtemp = new double[NUM_PIXELS_SIDE, NUM_PIXELS_SIDE]; double[,] siatkaHITTemp = new double[NUM_PIXELS_SIDE, NUM_PIXELS_SIDE]; GeometricalObject tempLED = null; CartesianCoordinates posLED = new CartesianCoordinates(x: 0, y: 0, z: 0); double[] Lsa = new double[LEDStruct.Length]; for (int iled = 0; iled < LEDStruct.Length; iled++) { tempLED = LEDStruct[iled]; posLED = new CartesianCoordinates(x: LEDStruct[iled].x, y: LEDStruct[iled].y, z: LEDStruct[iled].z); Lsa[iled] = Elementary.DistanceFind(posLED, new CartesianCoordinates(x: ballStruct.x, y: ballStruct.y, z: ballStruct.z)); } // angle of the most wide beam from a LED double alfaSA = Math.Asin(ballStruct.radius / Lsa.Min()) * 180.0 / Math.PI; // angle of the most wide beam to the pinhole dynamic BallHitPointT = Elementary.DoesHitBall(new CartesianCoordinates(x: 0, y: 0, z: 0), new CartesianCoordinates(x: ballStruct.x, y: ballStruct.y, z: ballStruct.z), ballStruct); double Lphc = Elementary.DistanceFind(new CartesianCoordinates(x: BallHitPointT.x, y: BallHitPointT.y, z: BallHitPointT.z), new CartesianCoordinates(x: 0, y: 0, z: 0)); double alfaPH = Math.Atan(pinholeStuct.radius / Lphc) * 180.0 / Math.PI; for (int iPhoton = 1; iPhoton <= PHOTONS_PER_LED; iPhoton++) { for (int iled = 0; iled < LEDStruct.Length; iled++) { tempLED = LEDStruct[iled]; posLED = new CartesianCoordinates(x: LEDStruct[iled].x, y: LEDStruct[iled].y, z: LEDStruct[iled].z); double _tetha = localRandom.NextDouble() * alfaSA; double _phi = localRandom.NextDouble() * 360.0; double _r = 100.0; CartesianCoordinates photonEndPoint = Elementary.Sphere2Kart(new SphericalCoordinates(r: _r, tetha: _tetha, phi: _phi)); newOriginCart = new CartesianCoordinates(x: ballStruct.x - tempLED.x, y: ballStruct.y - tempLED.y, z: ballStruct.z - tempLED.z); newOriginSpher = Elementary.Cart2Sphere(newOriginCart); photonEndPoint = Elementary.PointRotation(photonEndPoint, newOriginSpher); photonEndPoint = Elementary.AddVectors(photonEndPoint, posLED); dynamic BallHitPoint = Elementary.DoesHitBall(posLED, photonEndPoint, ballStruct); double Llh = Elementary.DistanceFind(new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z), posLED); double alfa_p = Math.Acos(BallHitPoint.z / Llh) * 180.0 / Math.PI; angleEff[0] = Math.Cos(alfa_p * Math.PI / 180.0); if (BallHitPoint.z != -1) { L1 = Elementary.DistanceFind(new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z), posLED); double Lc = Elementary.DistanceFind(new CartesianCoordinates(x: ballStruct.x, y: ballStruct.y, z: ballStruct.z), posLED); alfa_p = Math.Acos((L1 * L1 + ballStruct.radius * ballStruct.radius - Lc * Lc) / (2 * ballStruct.radius * L1)) * 180 / Math.PI; angleEff[1] = Math.Cos((180.0 - alfa_p) * Math.PI / 180.0); if (Double.IsNaN(angleEff[1])) { //System.Console.Write("traingle error"); angleEff[1] = 0.0; // BUG : triangle sides a+b<c } for (int ipFot = 0; ipFot < SECONDARY_EMISSION_PHOTONS; ipFot++) { //sampling the end position of new emitting photon _tetha = 180.0 + localRandom.NextDouble() * alfaPH; _phi = localRandom.NextDouble() * 360.0; _r = 100.0; CartesianCoordinates photonEndPointFinal = Elementary.Sphere2Kart(new SphericalCoordinates(r: _r, tetha: _tetha, phi: _phi)); newOriginCart = new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z); newOriginSpher = Elementary.Cart2Sphere(newOriginCart); double[] wek = new double[] { 0, 0 }; // rotation with respect to hitBall point photonEndPointFinal = Elementary.PointRotation(photonEndPointFinal, newOriginSpher); // translation Elementary.AddVectors(photonEndPointFinal, new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z)); // secondary emission angle double Lh = Elementary.DistanceFind(new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z), photonEndPointFinal); Lc = Elementary.DistanceFind(photonEndPointFinal, new CartesianCoordinates(x: ballStruct.x, y: ballStruct.y, z: ballStruct.z)); alfa_p = Math.Acos((Lh * Lh + ballStruct.radius * ballStruct.radius - Lc * Lc) / (2 * ballStruct.radius * Lh)) * 180 / Math.PI; angleEff[2] = Math.Cos((180.0 - alfa_p) * Math.PI / 180.0); dynamic CircHitPoint = Elementary.DoesGoTrCirc(new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z), photonEndPointFinal, ballStructh); if (CircHitPoint.z != -1) //Photon goes trhough pinhole { double z0 = QPStruct.z; double x2 = photonEndPointFinal.x; double y2 = photonEndPointFinal.y; double z2 = photonEndPointFinal.z; double x1 = BallHitPoint.x; double y1 = BallHitPoint.y; double z1 = BallHitPoint.z; // parametric form solution double t = (z0 - z1) / (z2 - z1); double x = x1 + (x2 - x1) * t; double y = y1 + (y2 - y1) * t; double z = z1 + (z2 - z1) * t; L2 = Elementary.DistanceFind(new CartesianCoordinates(x: BallHitPoint.x, y: BallHitPoint.y, z: BallHitPoint.z), new CartesianCoordinates(x: x, y: y, z: z)); SphericalCoordinates hitParams = Elementary.Cart2Sphere(new CartesianCoordinates(x: x1 - x, y: y1 - y, z: z1 - z)); angleEff[3] = Math.Cos(hitParams.tetha * Math.PI / 180); double I0 = 1; double Ifin = (I0 * angleEff[0] / (L1 * L1)) * angleEff[1] * angleEff[2] * angleEff[3] / (L2 * L2); if (Double.IsNaN(Ifin)) { Ifin = 0; } int posx = 0, posy = 0; if (x > 0) { posx = Convert.ToInt16(Math.Floor(x / resolution) + sideLength / resolution / 2); } else { posx = Convert.ToInt16(Math.Ceiling(x / resolution) + sideLength / resolution / 2) - 1; } if (y > 0) { posy = Convert.ToInt16(Math.Floor(y / resolution) + sideLength / resolution / 2); } else { posy = Convert.ToInt16(Math.Ceiling(y / resolution) + sideLength / resolution / 2) - 1; } if (posx < Math.Floor(sideLength / resolution) && posy < Math.Floor(sideLength / resolution) && posx >= 0 && posy >= 0) { pixelHITtemp[posx, posy] = pixelHITtemp[posx, posy] + Ifin; siatkaHITTemp[posx, posy] = siatkaHITTemp[posx, posy] + 1; } } } } } if (iPhoton % REFRESH_STEP == 0 && iPhoton > 0) { refrcnt++; updateHitMesh(pixelHITtemp); if (refrcnt % NUM_THREADS == 0) { progressBar1.Invoke(new Action(delegate() { double temppr = (double)(1.0 / NUM_THREADS) * refrcnt * STEPPERCENT; temppr = temppr > 99.0 ? 100.0 : temppr; lProgress.Text = String.Format("Progress: {0:0}%", temppr); labelPerc.Text = String.Format("Progress: {0:0}%", temppr); progressBar1.Value = (int)temppr; })); lock (GLOBAL_RANDOM_VAR) { previewImage = CreateImage(pixelHIT); } //automatically copy the resulting Image to clipboard after the simulation is performed pictureBox1.BeginInvoke(new Action(delegate() { pictureBox1.Image = previewImage; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; Clipboard.SetImage(previewImage); try { //pictureBox1.Image.Save(imgPath + imc2 + ".png", ImageFormat.Png); using (MemoryStream memory = new MemoryStream()) { using (FileStream fs = new FileStream(imgPath + imc2 + ".png", FileMode.Create, FileAccess.ReadWrite)) { pictureBox1.Image.Save(memory, ImageFormat.Png); byte[] bytes = memory.ToArray(); fs.Write(bytes, 0, bytes.Length); } } BitmapSource bSource = new BitmapImage(new Uri(imgPath + imc2 + ".png")); encoder.Frames.Add(BitmapFrame.Create(bSource)); imc2++; } catch (Exception eb2) { System.Console.WriteLine(eb2.Message); } })); } //update the time elapsed label lTimeElapsed.BeginInvoke(new Action(delegate() { lTimeElapsed.Text = String.Format("Time: {0:00}:{1:00}:{2:00}.{3:00}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds / 10); })); } } updateHitMesh(pixelHITtemp); }
public static CartesianCoordinates PointRotation(CartesianCoordinates cartPoint, SphericalCoordinates spherPoint) { double tetha = spherPoint.tetha; double phi = spherPoint.phi; double[,] RotZ = new double[, ] { { Math.Cos(phi * Math.PI / 180), -Math.Sin(phi * Math.PI / 180), 0 }, { Math.Sin(phi * Math.PI / 180), Math.Cos(phi * Math.PI / 180), 0 }, { 0, 0, 1 } }; double[,] RotY = new double[, ] { { Math.Cos(tetha * Math.PI / 180), 0, Math.Sin(tetha * Math.PI / 180) }, { 0, 1, 0 }, { -Math.Sin(tetha * Math.PI / 180), 0, Math.Cos(tetha * Math.PI / 180) } }; double[] point2Rotate = new double[] { cartPoint.x, cartPoint.y, cartPoint.z }; point2Rotate = MatrixProduct(RotY, point2Rotate); point2Rotate = MatrixProduct(RotZ, point2Rotate); return(new CartesianCoordinates(x: point2Rotate[0], y: point2Rotate[1], z: point2Rotate[2])); }
public void InitializeEnvironment() { NUM_PIXELS_SIDE = Convert.ToInt16(sideLength / resolution); pixelHIT = new double[NUM_PIXELS_SIDE, NUM_PIXELS_SIDE]; siatkaHIT = new double[NUM_PIXELS_SIDE, NUM_PIXELS_SIDE]; _1Dprevimage = new double[NUM_PIXELS_SIDE * NUM_PIXELS_SIDE]; double ballRad = Convert.ToDouble(nudPSEbr.Value) * 0.1; //create ball CartesianCoordinates ballCoordinates = new CartesianCoordinates(x: 0, y: 0, z: 3); ballStruct = new GeometricalObject(ballCoordinates, Elementary.Cart2Sphere(ballCoordinates), radius: ballRad, side: 0); double phRad = Convert.ToDouble(nudPSEphr.Value) * 0.1; //create pinhole CartesianCoordinates pinholeCoordinates = new CartesianCoordinates(x: 0, y: 0, z: 0); pinholeStuct = new GeometricalObject(pinholeCoordinates, Elementary.Cart2Sphere(pinholeCoordinates), radius: phRad, side: 0); //create LEDs if (rbCircle.Checked) { int numLEDS = Convert.ToInt16(nudPCnl.Value); double r_dist = Convert.ToDouble(nudPCr.Value) * 0.1; LEDStruct = new GeometricalObject[numLEDS]; for (int ledsIterator = 0; ledsIterator < numLEDS; ledsIterator++) { SphericalCoordinates sphericalTemp = new SphericalCoordinates(r: r_dist, tetha: 90.0, phi: 360 * ledsIterator / numLEDS); LEDStruct[ledsIterator] = new GeometricalObject(Elementary.Sphere2Kart(sphericalTemp), sphericalTemp, 0, 0); } } else if (rbGrid.Checked) { int cols = Convert.ToInt16(nudPGc.Value); int rows = Convert.ToInt16(nudPGr.Value); double spacing = Convert.ToInt16(nudPGs.Value) * 0.1; int numLEDS = cols * rows; LEDStruct = new GeometricalObject[numLEDS]; int helpIter = 0; for (int colit = 0; colit < cols; colit++) { for (int rowit = 0; rowit < rows; rowit++) { CartesianCoordinates tempC = new CartesianCoordinates(x: spacing * ((cols + 1) / 2.0 - colit) - spacing, y: spacing * ((rows + 1) / 2.0 - rowit) - spacing, z: 0.0); LEDStruct[helpIter++] = new GeometricalObject(tempC, Elementary.Cart2Sphere(tempC), radius: 0.0, side: 0.0); } } } //create ball helper CartesianCoordinates ballCh = new CartesianCoordinates(x: 0, y: 0, z: 0); ballStructh = new GeometricalObject(ballCh, Elementary.Cart2Sphere(ballCh), radius: pinholeStuct.radius, side: 0); double imagePlaneDist = -Convert.ToDouble(nudPSEip.Value) * 0.1; double quadSide = Convert.ToDouble(nudPSEqps.Value) * 0.1; //create quad photodiode CartesianCoordinates qpC = new CartesianCoordinates(x: 0, y: 0, z: imagePlaneDist); QPStruct = new GeometricalObject(qpC, Elementary.Cart2Sphere(qpC), 0, quadSide); REFRESH_STEP = STEPPERCENT * PHOTONS_PER_LED / 100; }