public void AssociatedLegendreLowOrders() { foreach (double x in TestUtilities.GenerateRealValues(0.01, 1.0, 10)) { Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(0, 0, x), 1.0)); Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(1, 0, x), x)); Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(1, 1, x), -Math.Sqrt(1.0 - x * x))); Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(2, 0, x), (3.0 * x * x - 1.0) / 2.0)); Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(2, 1, x), -3.0 * x * Math.Sqrt(1.0 - x * x))); Assert.IsTrue(TestUtilities.IsNearlyEqual(OrthogonalPolynomials.LegendreP(2, 2, x), 3.0 * (1.0 - x * x))); } }
public void SphericalHarmonicLowOrders() { foreach (double theta in GenerateRandomAngles(-Math.PI, Math.PI, 4)) { foreach (double phi in GenerateRandomAngles(0, 2.0 * Math.PI, 4)) { Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.SphericalHarmonic(0, 0, theta, phi), 1.0 / Math.Sqrt(4.0 * Math.PI))); //Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.SphericalHarmonic(1, -1, theta, phi), Math.Sqrt(3.0 / 8.0 / Math.PI) * Math.Sin(theta) * ComplexMath.Exp(-ComplexMath.I * phi))); //Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.SphericalHarmonic(1, 1, theta, phi), -Math.Sqrt(3.0 / 8.0 / Math.PI) * Math.Sin(theta) * ComplexMath.Exp(ComplexMath.I * phi))); Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.SphericalHarmonic(1, 0, theta, phi), Math.Sqrt(3.0 / 4.0 / Math.PI) * Math.Cos(theta))); } } }
static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); XrayLib xl = XrayLib.Instance; // If something goes wrong, the test will end with EXIT_FAILURE // xl.SetHardExit(1); xl.SetErrorMessages(0); Console.Title = String.Format("XrayLib.NET v{0}.{1}", XrayLib.VERSION_MAJOR, XrayLib.VERSION_MINOR); Console.WriteLine("Example C# program using XrayLib.NET\n"); Console.WriteLine("Density of pure Al: {0} g/cm3", xl.ElementDensity(13)); Console.WriteLine("Ca K-alpha Fluorescence Line Energy: {0}", xl.LineEnergy(20, XrayLib.KA_LINE)); Console.WriteLine("Fe partial photoionization cs of L3 at 6.0 keV: {0}", xl.CS_Photo_Partial(26, XrayLib.L3_SHELL, 6.0)); Console.WriteLine("Zr L1 edge energy: {0}", xl.EdgeEnergy(40, XrayLib.L1_SHELL)); Console.WriteLine("Pb Lalpha XRF production cs at 20.0 keV (jump approx): {0}", xl.CS_FluorLine(82, XrayLib.LA_LINE, 20.0)); Console.WriteLine("Pb Lalpha XRF production cs at 20.0 keV (Kissel): {0}", xl.CS_FluorLine_Kissel(82, XrayLib.LA_LINE, 20.0)); Console.WriteLine("Bi M1N2 radiative rate: {0}", xl.RadRate(83, XrayLib.M1N2_LINE)); Console.WriteLine("U M3O3 Fluorescence Line Energy: {0}", xl.LineEnergy(92, XrayLib.M3O3_LINE)); Console.WriteLine("Pb information: {0}", xl.GetElementData(82).ToString()); // Parser test for Ca(HCO3)2 (calcium bicarbonate) CompoundData cd = new CompoundData("Ca(HCO3)2"); Console.WriteLine("Ca(HCO3)2 contains:"); Console.Write(cd.ToString()); // Parser test for SiO2 (quartz) cd.Parse("SiO2"); Console.WriteLine("SiO2 contains:"); Console.Write(cd.ToString()); Console.WriteLine("Ca(HCO3)2 Rayleigh cs at 10.0 keV: {0}", xl.CS_Rayl_CP("Ca(HCO3)2", 10.0)); Console.WriteLine("CS2 Refractive Index at 10.0 keV : {0} - {1} i", xl.Refractive_Index_Re("CS2", 10.0, 1.261), xl.Refractive_Index_Im("CS2", 10.0, 1.261)); Console.WriteLine("C16H14O3 Refractive Index at 1 keV : {0} - {1} i", xl.Refractive_Index_Re("C16H14O3", 1.0, 1.2), xl.Refractive_Index_Im("C16H14O3", 1.0, 1.2)); Console.WriteLine("SiO2 Refractive Index at 5 keV : {0} - {1} i", xl.Refractive_Index_Re("SiO2", 5.0, 2.65), xl.Refractive_Index_Im("SiO2", 5.0, 2.65)); Complex n = xl.Refractive_Index("SiO2", 5.0, 2.65); Console.WriteLine("SiO2 Refractive Index at 5 keV : {0} - {1} i", n.Real, n.Imaginary); Console.WriteLine("Compton profile for Fe at pz = 1.1 : {0}", xl.ComptonProfile(26, 1.1f)); Console.WriteLine("M5 Compton profile for Fe at pz = 1.1 : {0}", xl.ComptonProfile_Partial(26, XrayLib.M5_SHELL, 1.1)); Console.WriteLine("M1->M5 Coster-Kronig transition probability for Au : {0}", xl.CosKronTransProb(79, XrayLib.FM15_TRANS)); Console.WriteLine("L1->L3 Coster-Kronig transition probability for Fe : {0}", xl.CosKronTransProb(26, XrayLib.FL13_TRANS)); Console.WriteLine("Au Ma1 XRF production cs at 10.0 keV (Kissel): {0}", xl.CS_FluorLine_Kissel(79, XrayLib.MA1_LINE, 10.0)); Console.WriteLine("Au Mb XRF production cs at 10.0 keV (Kissel): {0}", xl.CS_FluorLine_Kissel(79, XrayLib.MB_LINE, 10.0)); Console.WriteLine("Au Mg XRF production cs at 10.0 keV (Kissel): {0}", xl.CS_FluorLine_Kissel(79, XrayLib.MG_LINE, 10.0)); Console.WriteLine("K atomic level width for Fe: {0}", xl.AtomicLevelWidth(26, XrayLib.K_SHELL)); Console.WriteLine("Bi L2-M5M5 Auger non-radiative rate: {0}", xl.AugerRate(86, XrayLib.L2_M5M5_AUGER)); cd = new CompoundData("SiO2", 0.4, "Ca(HCO3)2", 0.6); Console.WriteLine("Compound contains:"); Console.Write(cd.ToString()); String symbol = CompoundData.AtomicNumberToSymbol(26); Console.WriteLine("Symbol of element 26 is: {0}", symbol); Console.WriteLine("Number of element Fe is: {0}", CompoundData.SymbolToAtomicNumber("Fe")); Console.WriteLine("Pb Malpha XRF production cs at 20.0 keV with cascade effect: {0}", xl.CS_FluorLine_Kissel(82, XrayLib.MA1_LINE, 20.0)); Console.WriteLine("Pb Malpha XRF production cs at 20.0 keV with radiative cascade effect: {0}", xl.CS_FluorLine_Kissel_Radiative_Cascade(82, XrayLib.MA1_LINE, 20.0)); Console.WriteLine("Pb Malpha XRF production cs at 20.0 keV with non-radiative cascade effect: {0}", xl.CS_FluorLine_Kissel_Nonradiative_Cascade(82, XrayLib.MA1_LINE, 20.0)); Console.WriteLine("Pb Malpha XRF production cs at 20.0 keV without cascade effect: {0}", xl.CS_FluorLine_Kissel_No_Cascade(82, XrayLib.MA1_LINE, 20.0)); Console.WriteLine("Al mass energy-absorption cs at 20.0 keV: {0}", xl.CS_Energy(13, 20.0)); Console.WriteLine("Pb mass energy-absorption cs at 40.0 keV: {0}", xl.CS_Energy(82, 40.0)); Console.WriteLine("CdTe mass energy-absorption cs at 40.0 keV: {0}", xl.CS_Energy_CP("CdTe", 40.0)); double energy = 8.0; double debyeFactor = 1.0; double relativeAngle = 1.0; // Si crystal structure CrystalArray ca = new CrystalArray(); Crystal cryst = ca.GetCrystal("Si"); if (cryst != null) { Console.WriteLine(cryst.ToString()); // Si diffraction parameters Console.WriteLine("Si 111 at 8 KeV. Incidence at the Bragg angle:"); double bragg = cryst.BraggAngle(energy, 1, 1, 1); Console.WriteLine(" Bragg angle: {0} rad, {1} deg", bragg, bragg * 180.0 / Math.PI); double q = cryst.ScatteringVectorMagnitide(energy, 1, 1, 1, relativeAngle); Console.WriteLine(" Magnitude of scattering vector, Q: {0}", q); double f0 = 0.0, fp = 0.0, fpp = 0.0; cryst.AtomicScatteringFactors(14, energy, q, debyeFactor, ref f0, ref fp, ref fpp); Console.WriteLine(" Atomic scattering factors (Z = 14) f0, fp, fpp: {0}, {1}, i{2}", f0, fp, fpp); Complex FH, F0; FH = cryst.StructureFactor(energy, 1, 1, 1, debyeFactor, relativeAngle); Console.WriteLine(" FH(1,1,1) structure factor: ({0}, {1})", FH.Real, FH.Imaginary); F0 = cryst.StructureFactor(energy, 0, 0, 0, debyeFactor, relativeAngle); Console.WriteLine(" F0=FH(0,0,0) structure factor: ({0}, {1})", F0.Real, F0.Imaginary); Console.WriteLine(); } // Diamond diffraction parameters cryst = ca.GetCrystal("Diamond"); if (cryst != null) { Console.WriteLine("Diamond 111 at 8 KeV. Incidence at the Bragg angle:"); double bragg = cryst.BraggAngle(energy, 1, 1, 1); Console.WriteLine(" Bragg angle: {0} rad, {1} deg", bragg, bragg * 180.0 / Math.PI); double q = cryst.ScatteringVectorMagnitide(energy, 1, 1, 1, relativeAngle); Console.WriteLine(" Magnitude of scattering vector, Q: {0}", q); double f0 = 0.0, fp = 0.0, fpp = 0.0; cryst.AtomicScatteringFactors(6, energy, q, debyeFactor, ref f0, ref fp, ref fpp); Console.WriteLine(" Atomic scattering factors (Z = 6) f0, fp, fpp: {0}, {1}, i{2}", f0, fp, fpp); Complex FH, F0; FH = cryst.StructureFactor(energy, 1, 1, 1, debyeFactor, relativeAngle); Console.WriteLine(" FH(1,1,1) structure factor: ({0}, {1})", FH.Real, FH.Imaginary); F0 = cryst.StructureFactor(energy, 0, 0, 0, debyeFactor, relativeAngle); Console.WriteLine(" F0=FH(0,0,0) structure factor: ({0}, {1})", F0.Real, F0.Imaginary); Complex FHbar = cryst.StructureFactor(energy, -1, -1, -1, debyeFactor, relativeAngle); double dw = 1e10 * 2 * (XrayLib.R_E / cryst.Volume) * (XrayLib.KEV2ANGST * XrayLib.KEV2ANGST / (energy * energy)) * Math.Sqrt(Complex.Abs(FH * FHbar)) / Math.PI / Math.Sin(2 * bragg); Console.WriteLine(" Darwin width: {0} uRad", 1e6 * dw); Console.WriteLine(); } // Alpha Quartz diffraction parameters cryst = ca.GetCrystal("AlphaQuartz"); if (cryst != null) { Console.WriteLine("AlphaQuartz 020 at 8 KeV. Incidence at the Bragg angle:"); double bragg = cryst.BraggAngle(energy, 0, 2, 0); Console.WriteLine(" Bragg angle: {0} rad, {1} deg", bragg, bragg * 180.0 / Math.PI); double q = cryst.ScatteringVectorMagnitide(energy, 0, 2, 0, relativeAngle); Console.WriteLine(" Magnitude of scattering vector, Q: {0}", q); double f0 = 0.0, fp = 0.0, fpp = 0.0; cryst.AtomicScatteringFactors(8, energy, q, debyeFactor, ref f0, ref fp, ref fpp); Console.WriteLine(" Atomic scattering factors (Z = 8) f0, fp, fpp: {0}, {1}, i{2}", f0, fp, fpp); Complex FH, F0; FH = cryst.StructureFactor(energy, 0, 2, 0, debyeFactor, relativeAngle); Console.WriteLine(" FH(0,2,0) structure factor: ({0}, {1})", FH.Real, FH.Imaginary); F0 = cryst.StructureFactor(energy, 0, 0, 0, debyeFactor, relativeAngle); Console.WriteLine(" F0=FH(0,0,0) structure factor: ({0}, {1})", F0.Real, F0.Imaginary); Console.WriteLine(); } // Muscovite diffraction parameters cryst = ca.GetCrystal("Muscovite"); if (cryst != null) { Console.WriteLine("Muskovite 331 at 8 KeV. Incidence at the Bragg angle:"); double bragg = cryst.BraggAngle(energy, 3, 3, 1); Console.WriteLine(" Bragg angle: {0} rad, {1} deg", bragg, bragg * 180.0 / Math.PI); double q = cryst.ScatteringVectorMagnitide(energy, 3, 3, 1, relativeAngle); Console.WriteLine(" Magnitude of scattering vector, Q: {0}", q); double f0 = 0.0, fp = 0.0, fpp = 0.0; cryst.AtomicScatteringFactors(19, energy, q, debyeFactor, ref f0, ref fp, ref fpp); Console.WriteLine(" Atomic scattering factors (Z = 19) f0, fp, fpp: {0}, {1}, i{2}", f0, fp, fpp); Complex FH, F0; FH = cryst.StructureFactor(energy, 3, 3, 1, debyeFactor, relativeAngle); Console.WriteLine(" FH(3,3,1) structure factor: ({0}, {1})", FH.Real, FH.Imaginary); F0 = cryst.StructureFactor(energy, 0, 0, 0, debyeFactor, relativeAngle); Console.WriteLine(" F0=FH(0,0,0) structure factor: ({0}, {1})", F0.Real, F0.Imaginary); Console.WriteLine(); } // RadionuclideData tests RadionuclideData rd = new RadionuclideData("109Cd"); Console.WriteLine(rd.ToString()); Console.WriteLine(); rd = new RadionuclideData(XrayLib.RADIONUCLIDE_125I); Console.WriteLine(rd.ToString()); Console.WriteLine(); rd = new RadionuclideData(); string namesCsv = string.Join(", ", rd.Names.ToArray()); Console.WriteLine(namesCsv); Console.WriteLine(); sw.Stop(); Console.WriteLine("Time: {0} ms", sw.ElapsedMilliseconds); Console.ReadLine(); }
public void SphericalHarmonicSpecialCases() { // ell=0, m=0 foreach (double theta in GenerateRandomAngles(-Math.PI, Math.PI, 5)) { foreach (double phi in GenerateRandomAngles(0.0, 2.0 * Math.PI, 5)) { Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.SphericalHarmonic(0, 0, theta, phi), 1.0 / Math.Sqrt(4.0 * Math.PI))); } } // theta=0, m=0 foreach (int ell in TestUtilities.GenerateIntegerValues(1, 100, 5)) { foreach (double phi in GenerateRandomAngles(0.0, 2.0 * Math.PI, 5)) { Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.SphericalHarmonic(ell, 0, 0.0, phi), Math.Sqrt((2 * ell + 1) / (4.0 * Math.PI)))); } } }
static void Main(string[] args) { Process[] processes = Process.GetProcesses(); Process wzqProcess = null; foreach (var item in processes) { if (item.MainWindowTitle == "五子棋") { Console.WriteLine(item.ProcessName); Console.WriteLine(item.Id); //窗口名 Console.WriteLine(item.MainWindowTitle); Console.WriteLine(item.MainModule.FileName); Console.WriteLine(item.MainModule.FileVersionInfo.FileVersion); Console.WriteLine(item.MainModule.FileVersionInfo.FileDescription); Console.WriteLine(item.MainModule.FileVersionInfo.Comments); Console.WriteLine(item.MainModule.FileVersionInfo.CompanyName); Console.WriteLine(item.MainModule.FileVersionInfo.FileName); //产品名 Console.WriteLine(item.MainModule.FileVersionInfo.ProductName); Console.WriteLine(item.MainModule.FileVersionInfo.ProductVersion); Console.WriteLine(item.StartTime); Console.WriteLine(item.MainWindowHandle); wzqProcess = item; break; } } Bitmap bitmap = CaptureImage.Captuer(wzqProcess); if (bitmap == null) { return; } //bitmap.Save("a.bmp"); //Process.Start("mspaint", "a.bmp"); //左上角 //227 129 //右下角 //721 621 int width = 721 - 227; int height = 621 - 129; int step = width * 15 / 14 / 15; Bitmap wzqBoardImage = new Bitmap(width * 15 / 14, height * 15 / 14); Graphics g = Graphics.FromImage(wzqBoardImage); // // 摘要: // 在指定位置并且按指定大小绘制指定的 System.Drawing.Image 的指定部分。 // // 参数: // image: // 要绘制的 System.Drawing.Image。 // // destRect: // System.Drawing.Rectangle 结构,它指定所绘制图像的位置和大小。 将图像进行缩放以适合该矩形。 // // srcRect: // System.Drawing.Rectangle 结构,它指定 image 对象中要绘制的部分。 // // srcUnit: // System.Drawing.GraphicsUnit 枚举的成员,它指定 srcRect 参数所用的度量单位。 g.DrawImage(bitmap, new Rectangle(0, 0, wzqBoardImage.Width, wzqBoardImage.Height), new Rectangle(227 - step / 2, 129 - step / 2, wzqBoardImage.Width, wzqBoardImage.Height), GraphicsUnit.Pixel); g.Dispose(); //把Bitmap转换成Mat Mat boardMat = BitmapConverter.ToMat(wzqBoardImage); //因为霍夫圆检测对噪声比较敏感,所以首先对图像做一个中值滤波或高斯滤波(噪声如果没有可以不做) Mat blurBoardMat = new Mat(); Cv2.MedianBlur(boardMat, blurBoardMat, 9); //转为灰度图像 Mat grayBoardMat = new Mat(); Cv2.CvtColor(blurBoardMat, grayBoardMat, ColorConversionCodes.BGR2GRAY); //3:霍夫圆检测:使用霍夫变换查找灰度图像中的圆。 CircleSegment[] circleSegments = Cv2.HoughCircles(grayBoardMat, HoughMethods.Gradient, 1, step * 0.4, 70, 30, (int)(step * 0.3), (int)(step * 0.5)); foreach (var circleSegment in circleSegments) { Cv2.Circle(boardMat, (int)circleSegment.Center.X, (int)circleSegment.Center.Y, (int)circleSegment.Radius, Scalar.Red, 1, LineTypes.AntiAlias); } //判断棋子位置,遍历棋盘上的每个位置 int rows = 15; List <Tuple <int, int, int> > chessPointList = new List <Tuple <int, int, int> >(); //计算棋子颜色的阈值 Scalar scalarLower = new Scalar(128, 128, 128); Scalar scalarUpper = new Scalar(255, 255, 255); //行 for (int i = 0; i < rows; i++) { //列 for (int j = 0; j < rows; j++) { //棋盘棋子坐标 Point2f point = new Point2f(j * step + 0.5f * step, i * step + 0.5f * step); foreach (var circleSegment in circleSegments) { //有棋子 if (circleSegment.Center.DistanceTo(point) < 0.5 * step) { //检查棋子的颜色 //以棋子中心为中心点,截取一部分图片(圆内切正方形),来计算图片颜色 //r^2 = a^2 + a^2 //--> a= ((r^2)/2)^-2 double len = Math.Sqrt(circleSegment.Radius * circleSegment.Radius / 2); Rect rect = new Rect((int)(circleSegment.Center.X - len), (int)(circleSegment.Center.Y - len), (int)(len * 2), (int)(len * 2)); Mat squareMat = new Mat(grayBoardMat, rect); //计算颜色 Mat calculatedMat = new Mat(); Cv2.InRange(squareMat, scalarLower, scalarUpper, calculatedMat); float result = 100f * Cv2.CountNonZero(calculatedMat) / (calculatedMat.Width * calculatedMat.Height); chessPointList.Add(new Tuple <int, int, int>(i + 1, j + 1, result < 50 ? 0 : 1)); break; } } } } foreach (var item in chessPointList) { Console.WriteLine($"{item.Item1},{item.Item2},{item.Item3}"); } Cv2.ImShow("boardMat", boardMat); Cv2.WaitKey(); }