1 OpenFileDialog dlg = new OpenFileDialog(); 2 dlg.Filter = "Result (*.txt)|*.txt|All Files(*.*)|*.*"; 3 4 if (DialogResult.OK != dlg.ShowDialog()) 5 return; 6 7 double[] xdata = { -8.37, -7.87, -7.37, -6.87, -6.37, -5.87, -5.37, -4.87, -4.37, -3.87, -3.37, -2.87, -2.37, -1.87, -1.37, -0.87, -0.37, 0.13, 0.63, 1.13, 1.63, 2.13, 2.63, 3.13, 3.63, 4.13, 4.63, 5.13, 5.63, 6.13, 6.63, 7.13, 7.63, 8.13 }; 8 double[] ydata = { 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5, 13, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5, 20, 20.5 }; 9 double[,] results = new double[xdata.Length, ydata.Length]; 10 11 double minValue = 10000000; 12 double maxValue = -10000000; 13 StreamReader sr = new StreamReader(dlg.FileName, Encoding.Default); 14 String line; 15 int numLine = 0; 16 while ((line = sr.ReadLine()) != null) 17 { 18 String[] items = line.Split(‘\t‘); 19 if (items.Length < 1) 20 continue; 21 22 for (int ii = 0; ii < items.Length; ++ii) 23 { 24 double val = double.Parse(items[ii]); 25 results[ii, numLine] = val; 26 27 minValue = Math.Min(minValue, val); 28 maxValue = Math.Max(maxValue, val); 29 } 30 31 ++numLine; 32 } 33 34 double range = maxValue - minValue; 35 const long MaxValue = 0xff0000; 36 const long MinValue = 0xffff00; 37 const double Range = MaxValue - MinValue; 38 39 40 float[] positionBuffer = new float[xdata.Length * ydata.Length * 3]; 41 float[] normalBuffer = new float[positionBuffer.Length]; 42 float[] colorBuffer = new float[positionBuffer.Length / 3 * 4]; 43 44 for (int jj = 0, lenjj = ydata.Length; jj < lenjj; ++jj) 45 for (int ii = 0, lenii = xdata.Length; ii < lenii; ++ii) 46 { 47 int idx = jj * lenii + ii; 48 49 50 51 positionBuffer[idx * 3] = (float)xdata[ii] * 10; 52 positionBuffer[idx * 3 + 1] = (float)ydata[jj] * 10; 53 positionBuffer[idx * 3 + 2] = 0; 54 55 normalBuffer[idx * 3] = 0; 56 normalBuffer[idx * 3 + 1] = 0; 57 normalBuffer[idx * 3 + 2] = 1; 58 59 double rst = results[ii, jj]; 60 double ratio = (rst - minValue) / range; 61 long rgb = (long)(Range * ratio) + MinValue; 62 63 long red = rgb >> 16 & 0xFF; 64 long green = rgb >> 8 & 0xFF; 65 long blue = rgb & 0xFF; 66 67 colorBuffer[idx * 4] = red / 255.0f; 68 colorBuffer[idx * 4 + 1] = green / 255.0f; 69 colorBuffer[idx * 4 + 2] = blue / 255.0f; 70 colorBuffer[idx * 4 + 3] = 1.0f; 71 } 72 73 74 int faceCount = (xdata.Length - 1) * (ydata.Length - 1) * 2; 75 uint[] facets = new uint[faceCount * 3]; 76 int faceId = 0; 77 for (uint jj = 0, lenjj = (uint)ydata.Length; jj < lenjj - 1; ++jj) 78 for (uint ii = 0, lenii = (uint)xdata.Length; ii < lenii - 1; ++ii) 79 { 80 uint a = jj * lenii + ii; 81 uint b = a + 1; 82 uint c = (jj + 1) * lenii + ii; 83 uint d = c + 1; 84 /* 85 c----------d 86 | | 87 | | 88 | | 89 a----------b 90 */ 91 facets[faceId * 3] = a; 92 facets[faceId * 3 + 1] = d; 93 facets[faceId * 3 + 2] = c; 94 95 ++faceId; 96 facets[faceId * 3] = a; 97 facets[faceId * 3 + 1] = b; 98 facets[faceId * 3 + 2] = d; 99 100 ++faceId; 101 } 102 103 AABox bbox = new AABox(); 104 bbox.MinPt = new Vector3(positionBuffer[0], positionBuffer[1], positionBuffer[3]); 105 bbox.MaxPt = new Vector3(positionBuffer[positionBuffer.Length - 3], positionBuffer[positionBuffer.Length - 2], positionBuffer[positionBuffer.Length - 1]); 106 107 var entity = GlobalInstance.TopoShapeConvert.CreateColoredFaceEntity(positionBuffer, facets, normalBuffer, colorBuffer, bbox); 108 109 var node = new EntitySceneNode(); 110 node.SetEntity(entity); 111 112 113 renderView.ShowSceneNode(node);
原文:https://www.cnblogs.com/anycad/p/8834583.html