예제 #1
0
        private void Create3DObject(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show("Please make sure you put 'geometry.txt' under the same directory with this exe.");
                MessageBox.Show("Use 'J' to increase lines, 'K' to decrease lines.");
                String dataRoot  = @".";
                String modelGrid = "geometry.txt";

                var startTime = DateTime.Now;

                string gridPathFileName = Path.Combine(dataRoot, modelGrid);

                DynamicUnstructureGeometryLoader loader = new DynamicUnstructureGeometryLoader();

                DateTime start = DateTime.Now;
                // TODO: 如何取得实际业务中的11716?是否应在LoadSource中指定?
                DynamicUnstructuredGridderSource source = loader.LoadSource(gridPathFileName, 11716, 1, 1);
                DateTime stop    = DateTime.Now;
                double   seconds = (stop.Ticks - start.Ticks) / 1000.0d;

                this.element = new DynamicUnStructuredGridderElement(source, this.scientificVisual3DControl.Scene.CurrentCamera)
                {
                    Name = "UnStructuredGridderElement}"
                };
                element.Initialize(this.scientificVisual3DControl.OpenGL);

                ///模拟获得网格属性
                int   minValue = 100;
                int   maxValue = 10000;
                float step     = (maxValue * 1.0f - minValue * 1.0f) / 10;
                //设置色标的范围
                this.scientificVisual3DControl.SetColorIndicator(minValue, maxValue, step);

                this.scientificVisual3DControl.AddModelElement(element);

                // update ModelContainer's BoundingBox.
                BoundingBox boundingBox = this.scientificVisual3DControl.ModelContainer.BoundingBox;
                if (this.scientificVisual3DControl.ModelContainer.Children.Count > 1)
                {
                    boundingBox.Extend(source.Min);
                    boundingBox.Extend(source.Max);
                }
                else
                {
                    boundingBox.SetBounds(source.Min, source.Max);
                }
                //boundingBox.Expand();

                // update ViewType to UserView.
                this.scientificVisual3DControl.ViewType = ViewTypes.UserView;

                var endTime  = DateTime.Now;
                var interval = endTime.Subtract(startTime);
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pathFileName"></param>
        /// <param name="nx"></param>
        /// <param name="ny"></param>
        /// <param name="nz"></param>
        /// <returns></returns>
        public DynamicUnstructuredGridderSource LoadSource(string pathFileName, int nx, int ny, int nz)
        {
            DynamicUnstructuredGridderSource src = this.LoadSource(pathFileName);

            if (src.NX != nx || src.NY != ny || src.NZ != nz)
            {
                throw new ArgumentException(String.Format("grid dimens not match"));
            }
            return(src);
        }
예제 #3
0
        private void OnGridPropertyChanged(object sender, EventArgs e)
        {
            GridderSource source = this.sim3D.Tag as GridderSource;

            if (source == null)
            {
                return;
            }

            List <SimLabGrid> gridders = this.sim3D.Scene.SceneContainer.Traverse <SimLabGrid>().ToList <SimLabGrid>();

            if (gridders.Count <= 0)
            {
                return;
            }

            SimLabGrid grid = gridders[0];

            GridProperty prop = this.CurrentProperty;

            if (prop == null)
            {
                return;
            }
            float minValue = prop.MinValue;
            float maxValue = prop.MaxValue;
            float step     = (maxValue - minValue) / 10.0f;

            if (step <= 0.0f)
            {
                step = 1.0f;
            }

            this.sim3D.SetColorIndicator(minValue, maxValue, step);
            TexCoordBuffer textureCoordinates = source.CreateTextureCoordinates(prop.GridIndexes, prop.Values, minValue, maxValue);

            grid.SetTextureCoods(textureCoordinates);

            DynamicUnstructureGrid           dynamicUnstructureGrid          = gridders[0] as DynamicUnstructureGrid;
            DynamicUnstructuredGridderSource dynamicUnstructureGridderSource = source as DynamicUnstructuredGridderSource;

            if (dynamicUnstructureGrid != null && dynamicUnstructureGridderSource != null)
            {
                TexCoordBuffer anotherTextureCoordinates = dynamicUnstructureGridderSource.CreateFractureTextureCoordinates(prop.GridIndexes, prop.Values, minValue, maxValue);
                dynamicUnstructureGrid.SetFractionTextureCoords(anotherTextureCoordinates);
            }

            this.sim3D.Invalidate();
        }
        public DynamicUnstructuredGridderSource LoadSource(string pathFileName)
        {
            DynamicUnstructuredGridderSource src = new DynamicUnstructuredGridderSource();
            StreamReader reader = Open(pathFileName);
            try
            {
                int lineCounter = 0;
                String headerDescriptor = ReadLine(reader, ref lineCounter);
                if (headerDescriptor == null)
                    throw new FormatException("unexpected end of file");
                if (!headerDescriptor.StartsWith("node"))
                    throw new FormatException("bad format,header descriptor missing");
                String head = ReadLine(reader, ref lineCounter);
                if (String.IsNullOrEmpty(head))
                    throw new FormatException("bad format,header mising");

                string[] heads = head.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                if (heads.Length < 6)
                    throw new FormatException("bad format, head not match");

                int total = src.DimenSize;
                //src.NX = total;//TODO:是否应由此处指定NX?
                int nodeNum, elemNum, elemFormat, fracNum, fracFormat;

                #region read header

                nodeNum = System.Convert.ToInt32(heads[0], CultureInfo.InvariantCulture);
                elemNum = System.Convert.ToInt32(heads[1], CultureInfo.InvariantCulture);
                elemFormat = System.Convert.ToInt32(heads[2], CultureInfo.InvariantCulture);
                fracNum = System.Convert.ToInt32(heads[3], CultureInfo.InvariantCulture);
                fracFormat = System.Convert.ToInt32(heads[4], CultureInfo.InvariantCulture);

                if (elemFormat != ElEMENT_FORMAT3_TRIANGLE && elemFormat != ELEMENT_FORMAT4_TETRAHEDRON && elemFormat != ELEMENT_FORMAT6_TRIANGULAR_PRISM && elemFormat != 0)
                    throw new FormatException("bad format, unknown element format");
                if (fracFormat != FRACTURE_FORMAT2_LINE && fracFormat != FRACTURE_FORMAT3_TRIANGLE && fracFormat != FRACTURE_FORMAT4_QUAD)
                    throw new FormatException("bad format, unknown frac format");

                #endregion read header

                bool gotFirstMin = false; bool gotFirstMax = false;
                vec3 min = new vec3(), max = new vec3();

                #region read nodes

                vec3[] nodes = new vec3[nodeNum];
                for (int i = 0; i < nodeNum; i++)
                {
                    String nodeLine = ReadLine(reader, ref lineCounter);
                    if (nodeLine == null)
                        throw new FormatException("unexpected end of node");

                    String[] fields = nodeLine.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);

                    if (fields.Length < 3)
                        throw new FormatException(String.Format("node format error,line:{0}", lineCounter));

                    float x = System.Convert.ToSingle(fields[0], CultureInfo.InvariantCulture);
                    float y = System.Convert.ToSingle(fields[1], CultureInfo.InvariantCulture);
                    float z = System.Convert.ToSingle(fields[2], CultureInfo.InvariantCulture);
                    nodes[i] = new vec3(x, y, z);
                    if (!gotFirstMax)
                    {
                        max = nodes[i];
                        gotFirstMax = true;
                    }
                    else
                    {
                        max = vec3Helper.Max(max, nodes[i]);
                    }
                    if (!gotFirstMin)
                    {
                        min = nodes[i];
                        gotFirstMin = true;
                    }
                    else
                    {
                        min = vec3Helper.Min(min, nodes[i]);
                    }
                }

                #endregion read nodes

                src.Min = min;
                src.Max = max;

                #region read elements

                int[][] elements = new int[elemNum][];
                for (int i = 0; i < elemNum; i++)
                {
                    String elemLine = ReadLine(reader, ref lineCounter);
                    if (elemLine == null)
                        throw new FormatException("unexpected end of element");
                    String[] fields = elemLine.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                    if (fields.Length < elemFormat)
                        throw new FormatException(String.Format("element format error, line:{0}", lineCounter));

                    int[] elemnt = new int[elemFormat];
                    for (int j = 0; j < elemFormat; j++)
                    {
                        elemnt[j] = System.Convert.ToInt32(fields[j]);
                    }
                    elements[i] = elemnt;
                }

                #endregion read elements

                #region read fracture

                int[][] fractures = new int[fracNum][];
                for (int i = 0; i < fracNum; i++)
                {
                    String fracLine = ReadLine(reader, ref lineCounter);
                    if (fracLine == null)
                        throw new FormatException("unexpected end of element");
                    String[] fields = fracLine.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                    if (fields.Length < fracFormat)
                        throw new FormatException(String.Format("element format error, line:{0}", lineCounter));

                    int[] frac = new int[fracFormat];
                    for (int j = 0; j < fracFormat; j++)
                    {
                        frac[j] = System.Convert.ToInt32(fields[j]);
                    }
                    fractures[i] = frac;
                }

                #endregion read fracture

                src.NodeNum = nodeNum;
                src.Nodes = nodes;
                src.ElementFormat = elemFormat;
                src.ElementNum = elemNum;
                src.Elements = elements;
                src.FractureFormat = fracFormat;
                src.FractureNum = fracNum;
                src.Fractures = fractures;
                src.NX = src.ElementNum + src.FractureNum;
                src.NY = 1;
                src.NZ = 1;

                if (src.ElementNum <= 0)
                {
                    src.ElementNum = 0;
                    if (src.ElementFormat == 0)
                        src.ElementFormat = ELEMENT_FORMAT4_TETRAHEDRON;
                }
                if (src.FractureNum <= 0)
                {
                    src.FractureNum = 0;
                    if (src.FractureFormat == 0)
                        src.FractureFormat = FRACTURE_FORMAT3_TRIANGLE;
                }
                return src;
            }
            finally
            {
                reader.Close();
            }
        }
        protected static unsafe TexCoordBuffer DoCreateFractureTextureCoordinates(DynamicUnstructuredGridderSource src, int[] gridIndexes, float[] values, float minValue, float maxValue)
        {
            int fractureStartIndex = 0;
            int fractureEndIndex = src.FractureNum - 1;
            int[] invisibles = src.BindResultsAndActiveFractures(gridIndexes);
            float[] textures = new float[src.InvisibleFractureTextures.Length];
            Array.Copy(src.InvisibleFractureTextures, textures, textures.Length);

            for (int mixedIndex = 0; mixedIndex < gridIndexes.Length; mixedIndex++)
            {
                int gridIndex = gridIndexes[mixedIndex];
                if (gridIndex >= fractureStartIndex && gridIndex <= fractureEndIndex)
                {
                    float value = values[mixedIndex];
                    if (value < minValue)
                        value = minValue;
                    if (value > maxValue)
                        value = maxValue;
                    int matrixIndex = gridIndex;
                    if (invisibles[matrixIndex] > 0)
                    {
                        float distance = maxValue - minValue;
                        if (!(distance <= 0.0f))
                        {
                            textures[matrixIndex] = (value - minValue) / distance;
                            if (textures[matrixIndex] < 0.5f)
                            {
                                textures[matrixIndex] = 0.5f - (0.5f - textures[matrixIndex]) * 0.99f;
                            }
                            else
                            {
                                textures[matrixIndex] = (textures[matrixIndex] - 0.5f) * 0.99f + 0.5f;
                            }
                        }
                        else
                        {
                            //最小值最大值相等时,显示最小值的颜色
                            textures[matrixIndex] = 0.01f;
                            //textures[matrixIndex] = 0.01f;
                        }
                    }
                }
            }//end for

            //TextureCoordinatesBuffer textureCoordinates = new TextureCoordinatesBufferData();
            TexCoordBuffer textureCoordinates = null;

            int texturesCount = textures.Length;
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT3_TRIANGLE)
            {
                textureCoordinates = new TriangleFractureTexCoordBuffer();
                textureCoordinates.AllocMem(texturesCount);

                TriangleTexCoord* pTextures = (TriangleTexCoord*)textureCoordinates.Data;
                for (int i = 0; i <texturesCount; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT2_LINE)
            {
                textureCoordinates = new LineFractureTexCoordBufer();
                textureCoordinates.AllocMem(texturesCount);
                LineTexCoord* pTextures = (LineTexCoord*)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT4_QUAD)
            {
                textureCoordinates = new QuadFractureTexCoordBuffer();
                textureCoordinates.AllocMem(texturesCount);
                QuadTexCoord* pTextures = (QuadTexCoord*)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            return textureCoordinates;
        }
        private unsafe DynamicUnstructureGeometry DoCreateMesh(DynamicUnstructuredGridderSource src)
        {
            MatrixPositionBuffer matrixPositions = null;
            IndexBuffer matrixIndicesBuffer = null;
            FracturePositionBuffer fractionPositionsBuffer = null;

            //生成母体
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT4_TETRAHEDRON)
            {
                matrixPositions = new TetrahedronMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TETRAHEDRON;

                //int memSize = src.ElementNum * sizeof(TetrahedronPositions);
                //matrixPositions.AllocMem(memSize);
                matrixPositions.AllocMem(src.ElementNum);

                TetrahedronPosition* tets = (TetrahedronPosition*)matrixPositions.Data;
                int[][] matrixIndices = src.Elements;
                vec3[] positions = src.Nodes;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    tets[i].p1 = src.TranslateMatrix*positions[matrixIndices[i][0]-1];
                    tets[i].p2 = src.TranslateMatrix*positions[matrixIndices[i][1]-1];
                    tets[i].p3 = src.TranslateMatrix*positions[matrixIndices[i][2]-1];
                    tets[i].p4 = src.TranslateMatrix*positions[matrixIndices[i][3]-1];
                }

                matrixIndicesBuffer = new TetrahedronMatrixIndexBuffer();
                //int triangleCount = src.ElementNum * 4;
                //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex));
                matrixIndicesBuffer.AllocMem(src.ElementNum);
                TetrahedronIndex* header = (TetrahedronIndex*)matrixIndicesBuffer.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    header[i].dot0 = (uint)(i * 4 + 0);
                    header[i].dot1 = (uint)(i * 4 + 1);
                    header[i].dot2 = (uint)(i * 4 + 2);
                    header[i].dot3 = (uint)(i * 4 + 3);
                    header[i].dot4 = (uint)(i * 4 + 0);
                    header[i].dot5 = (uint)(i * 4 + 1);
                    header[i].restartIndex = uint.MaxValue;
                }
            }

            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT3_TRIANGLE)
            {
                matrixPositions = new TriangleMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE;
                matrixPositions.AllocMem(src.ElementNum);
                int[][] matrixIndices = src.Elements;
                vec3[] positions = src.Nodes;
                var triangles = (TrianglePosition*)matrixPositions.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    triangles[i].P1 = src.TranslateMatrix*positions[matrixIndices[i][0]-1];
                    triangles[i].P2 = src.TranslateMatrix*positions[matrixIndices[i][1]-1];
                    triangles[i].P3 = src.TranslateMatrix*positions[matrixIndices[i][2]-1];
                }
            }

            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT6_TRIANGULAR_PRISM)
            {
                matrixPositions = new TriangularPrismMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE;
                matrixPositions.AllocMem(src.ElementNum);
                int[][] matrixIndices = src.Elements;
                vec3[] positions = src.Nodes;
                TriangularPrismPosition* prism = (TriangularPrismPosition*)matrixPositions.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    try
                    {
                        prism[i].P1 = src.TranslateMatrix*positions[matrixIndices[i][0]-1];
                        prism[i].P2 = src.TranslateMatrix*positions[matrixIndices[i][1]-1];
                        prism[i].P3 = src.TranslateMatrix*positions[matrixIndices[i][2]-1];
                        prism[i].P4 = src.TranslateMatrix*positions[matrixIndices[i][3]-1];
                        prism[i].P5 = src.TranslateMatrix*positions[matrixIndices[i][4]-1];
                        prism[i].P6 = src.TranslateMatrix*positions[matrixIndices[i][5]-1];
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        throw new IndexOutOfRangeException(String.Format("Maxtrix positions row:{0},out of range", i + 1));
                    }
                }

                matrixIndicesBuffer = new TetrahedronMatrixTriangularPrismIndexBuffer();
                //int triangleCount = src.ElementNum * 4;
                //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex));
                matrixIndicesBuffer.AllocMem(src.ElementNum);
                TetrahedronTriangularPrismIndex* indexes = (TetrahedronTriangularPrismIndex*)matrixIndicesBuffer.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    indexes[i].dot0 = (uint)(i * 6 + 0);
                    indexes[i].dot1 = (uint)(i * 6 + 3);
                    indexes[i].dot2 = (uint)(i * 6 + 1);
                    indexes[i].dot3 = (uint)(i * 6 + 4);
                    indexes[i].dot4 = (uint)(i * 6 + 2);
                    indexes[i].dot5 = (uint)(i * 6 + 5);
                    indexes[i].dot6 = (uint)(i * 6 + 0);
                    indexes[i].dot7 = (uint)(i * 6 + 3);
                    indexes[i].restartIndex = uint.MaxValue;
                }
            }

            //生成裂缝
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT3_TRIANGLE)
            {
                fractionPositionsBuffer = new TriangleFracturePositionBuffer();

                //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_TRIANGLE;

                int triangleCount = src.FractureNum;

                //fractionPositionsBuffer.AllocMem(triangleCount * sizeof(TrianglePositions));
                fractionPositionsBuffer.AllocMem(triangleCount);

                int[][] triangleIndices = src.Fractures;
                vec3[] positions = src.Nodes;
                TrianglePosition* triangles = (TrianglePosition*)fractionPositionsBuffer.Data;
                for (int i = 0; i < triangleCount; i++)
                {
                    triangles[i].P1 = src.TranslateMatrix*positions[triangleIndices[i][0]-1];
                    triangles[i].P2 = src.TranslateMatrix*positions[triangleIndices[i][1]-1];
                    triangles[i].P3 = src.TranslateMatrix*positions[triangleIndices[i][2]-1];
                }
            }

            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT2_LINE)
            {
                fractionPositionsBuffer = new LineFracturePositionBuffer();

                //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_LINE;

                int lineCount = src.FractureNum;

                //fractionPositionsBuffer.AllocMem(lineCount * sizeof(LinePositions));
                fractionPositionsBuffer.AllocMem(lineCount);

                LinePosition* lines = (LinePosition*)fractionPositionsBuffer.Data;
                vec3[] positions = src.Nodes;
                int[][] lineIndices = src.Fractures;
                for (int i = 0; i < lineCount; i++)
                {
                    lines[i].P1 = src.TranslateMatrix*positions[lineIndices[i][0]-1];
                    lines[i].P2 = src.TranslateMatrix*positions[lineIndices[i][1]-1];
                }
            }

            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT4_QUAD)
            {
                fractionPositionsBuffer = new QuadFracturePositionBuffer();
                int fractureCount = src.FractureNum;
                fractionPositionsBuffer.AllocMem(fractureCount);
                QuadPosition* quad = (QuadPosition*)fractionPositionsBuffer.Data;
                vec3[] positions = src.Nodes;
                int[][] quadIndices = src.Fractures;
                for (int i = 0; i < fractureCount; i++)
                {
                    quad[i].P1 = src.TranslateMatrix*positions[quadIndices[i][0]-1];
                    quad[i].P2 = src.TranslateMatrix*positions[quadIndices[i][1]-1];
                    quad[i].P3 = src.TranslateMatrix*positions[quadIndices[i][3]-1];
                    quad[i].P4 = src.TranslateMatrix*positions[quadIndices[i][2]-1];
                }
            }

            DynamicUnstructureGeometry geometry = new DynamicUnstructureGeometry(matrixPositions);
            geometry.MatrixIndices = matrixIndicesBuffer;
            geometry.FracturePositions = fractionPositionsBuffer;
            geometry.Min = src.TransformedActiveBounds.Min;
            geometry.Max = src.TransformedActiveBounds.Max;
            return geometry;
        }
 public TexCoordBuffer CreateFractureTextureCoordinates(DynamicUnstructuredGridderSource src, int[] gridIndexes, float[] values, float minValue, float maxValue)
 {
     return DoCreateFractureTextureCoordinates(src, gridIndexes, values, minValue, maxValue);
 }
        /// <summary>
        /// 生成基质的纹理映射坐标
        /// </summary>
        /// <param name="src"></param>
        /// <param name="gridIndexes"></param>
        /// <param name="values"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        protected unsafe TexCoordBuffer DoCreateMatrixTextureCoordinates(DynamicUnstructuredGridderSource src, int[] gridIndexes, float[] values, float minValue, float maxValue)
        {
            int matrixStartIndex = src.FractureNum;
            int matrixEndIndex = src.DimenSize - 1;
            int[] invisibles = src.BindResultsAndActiveMatrix(gridIndexes);
            float[] textures = new float[src.InvisibleMatrixTextures.Length];
            Array.Copy(src.InvisibleMatrixTextures, textures, textures.Length);

            for (int mixedIndex = 0; mixedIndex < gridIndexes.Length; mixedIndex++)
            {
                int gridIndex = gridIndexes[mixedIndex];
                if (gridIndex >= matrixStartIndex && gridIndex < src.DimenSize)
                {
                    float value = values[mixedIndex];
                    if (value < minValue)
                        value = minValue;
                    if (value > maxValue)
                        value = maxValue;
                    int matrixIndex = gridIndex - matrixStartIndex;
                    if (invisibles[matrixIndex] > 0)
                    {
                        float distance = maxValue - minValue;
                        if (!(distance <= 0.0f))
                        {
                            textures[matrixIndex] = (value - minValue) / distance;
                            if (textures[matrixIndex] < 0.5f)
                            {
                                textures[matrixIndex] = 0.5f - (0.5f - textures[matrixIndex]) * 0.99f;
                            }
                            else
                            {
                                textures[matrixIndex] = (textures[matrixIndex] - 0.5f) * 0.99f + 0.5f;
                            }
                        }
                        else
                        {
                            //最小值最大值相等时,显示最小值的颜色
                            textures[matrixIndex] = 0.01f;
                            //textures[gridIndex] = 0;
                        }
                    }
                }
            }//end for

            //TextureCoordinatesBuffer textureCoordinates = new TextureCoordinatesBufferData();
            TexCoordBuffer textureCoordinates = null;

            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT3_TRIANGLE)
            {
                textureCoordinates = new TriangleMatrixTexCoordBuffer();

                //textureCoordinates.AllocMem(texturesCount * sizeof(TriangleUV));
                textureCoordinates.AllocMem(textures.Length);

                TriangleTexCoord* pTextures = (TriangleTexCoord*)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT4_TETRAHEDRON)
            {
                textureCoordinates = new TetrahedronMatrixTexCoordBuffer();

                //textureCoordinates.AllocMem(texturesCount * sizeof(TetrahedronUV));
                textureCoordinates.AllocMem(textures.Length);

                TetrahedronTexCoord* pTextures = (TetrahedronTexCoord*)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT6_TRIANGULAR_PRISM)
            {
                textureCoordinates = new TriangularPrismMatrixTexCoordBuffer();
                textureCoordinates.AllocMem(textures.Length);
                TriangularPrismTexCoord* pTextures = (TriangularPrismTexCoord*)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }

            }
            return textureCoordinates;
        }
예제 #9
0
        public DynamicUnstructuredGridderSource LoadSource(string pathFileName)
        {
            DynamicUnstructuredGridderSource src = new DynamicUnstructuredGridderSource();
            StreamReader reader = Open(pathFileName);

            try
            {
                int    lineCounter      = 0;
                String headerDescriptor = ReadLine(reader, ref lineCounter);
                if (headerDescriptor == null)
                {
                    throw new FormatException("unexpected end of file");
                }
                if (!headerDescriptor.StartsWith("node"))
                {
                    throw new FormatException("bad format,header descriptor missing");
                }
                String head = ReadLine(reader, ref lineCounter);
                if (String.IsNullOrEmpty(head))
                {
                    throw new FormatException("bad format,header mising");
                }

                string[] heads = head.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                if (heads.Length < 6)
                {
                    throw new FormatException("bad format, head not match");
                }

                int total = src.DimenSize;
                //src.NX = total;//TODO:是否应由此处指定NX?
                int nodeNum, elemNum, elemFormat, fracNum, fracFormat;

                #region read header

                nodeNum    = System.Convert.ToInt32(heads[0], CultureInfo.InvariantCulture);
                elemNum    = System.Convert.ToInt32(heads[1], CultureInfo.InvariantCulture);
                elemFormat = System.Convert.ToInt32(heads[2], CultureInfo.InvariantCulture);
                fracNum    = System.Convert.ToInt32(heads[3], CultureInfo.InvariantCulture);
                fracFormat = System.Convert.ToInt32(heads[4], CultureInfo.InvariantCulture);

                if (elemFormat != ElEMENT_FORMAT3_TRIANGLE && elemFormat != ELEMENT_FORMAT4_TETRAHEDRON && elemFormat != ELEMENT_FORMAT6_TRIANGULAR_PRISM && elemFormat != 0)
                {
                    throw new FormatException("bad format, unknown element format");
                }
                if (fracFormat != FRACTURE_FORMAT2_LINE && fracFormat != FRACTURE_FORMAT3_TRIANGLE && fracFormat != FRACTURE_FORMAT4_QUAD)
                {
                    throw new FormatException("bad format, unknown frac format");
                }

                #endregion read header

                bool gotFirstMin = false; bool gotFirstMax = false;
                vec3 min = new vec3(), max = new vec3();

                #region read nodes

                vec3[] nodes = new vec3[nodeNum];
                for (int i = 0; i < nodeNum; i++)
                {
                    String nodeLine = ReadLine(reader, ref lineCounter);
                    if (nodeLine == null)
                    {
                        throw new FormatException("unexpected end of node");
                    }

                    String[] fields = nodeLine.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);

                    if (fields.Length < 3)
                    {
                        throw new FormatException(String.Format("node format error,line:{0}", lineCounter));
                    }

                    float x = System.Convert.ToSingle(fields[0], CultureInfo.InvariantCulture);
                    float y = System.Convert.ToSingle(fields[1], CultureInfo.InvariantCulture);
                    float z = System.Convert.ToSingle(fields[2], CultureInfo.InvariantCulture);
                    nodes[i] = new vec3(x, y, z);
                    if (!gotFirstMax)
                    {
                        max         = nodes[i];
                        gotFirstMax = true;
                    }
                    else
                    {
                        max = vec3Helper.Max(max, nodes[i]);
                    }
                    if (!gotFirstMin)
                    {
                        min         = nodes[i];
                        gotFirstMin = true;
                    }
                    else
                    {
                        min = vec3Helper.Min(min, nodes[i]);
                    }
                }

                #endregion read nodes

                src.Min = min;
                src.Max = max;

                #region read elements

                int[][] elements = new int[elemNum][];
                for (int i = 0; i < elemNum; i++)
                {
                    String elemLine = ReadLine(reader, ref lineCounter);
                    if (elemLine == null)
                    {
                        throw new FormatException("unexpected end of element");
                    }
                    String[] fields = elemLine.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                    if (fields.Length < elemFormat)
                    {
                        throw new FormatException(String.Format("element format error, line:{0}", lineCounter));
                    }

                    int[] elemnt = new int[elemFormat];
                    for (int j = 0; j < elemFormat; j++)
                    {
                        elemnt[j] = System.Convert.ToInt32(fields[j]);
                    }
                    elements[i] = elemnt;
                }

                #endregion read elements

                #region read fracture

                int[][] fractures = new int[fracNum][];
                for (int i = 0; i < fracNum; i++)
                {
                    String fracLine = ReadLine(reader, ref lineCounter);
                    if (fracLine == null)
                    {
                        throw new FormatException("unexpected end of element");
                    }
                    String[] fields = fracLine.Split(delimeters, StringSplitOptions.RemoveEmptyEntries);
                    if (fields.Length < fracFormat)
                    {
                        throw new FormatException(String.Format("element format error, line:{0}", lineCounter));
                    }

                    int[] frac = new int[fracFormat];
                    for (int j = 0; j < fracFormat; j++)
                    {
                        frac[j] = System.Convert.ToInt32(fields[j]);
                    }
                    fractures[i] = frac;
                }

                #endregion read fracture

                src.NodeNum        = nodeNum;
                src.Nodes          = nodes;
                src.ElementFormat  = elemFormat;
                src.ElementNum     = elemNum;
                src.Elements       = elements;
                src.FractureFormat = fracFormat;
                src.FractureNum    = fracNum;
                src.Fractures      = fractures;
                src.NX             = src.ElementNum + src.FractureNum;
                src.NY             = 1;
                src.NZ             = 1;

                if (src.ElementNum <= 0)
                {
                    src.ElementNum = 0;
                    if (src.ElementFormat == 0)
                    {
                        src.ElementFormat = ELEMENT_FORMAT4_TETRAHEDRON;
                    }
                }
                if (src.FractureNum <= 0)
                {
                    src.FractureNum = 0;
                    if (src.FractureFormat == 0)
                    {
                        src.FractureFormat = FRACTURE_FORMAT3_TRIANGLE;
                    }
                }
                return(src);
            }
            finally
            {
                reader.Close();
            }
        }
        /// <summary>
        /// 生成基质的纹理映射坐标
        /// </summary>
        /// <param name="src"></param>
        /// <param name="gridIndexes"></param>
        /// <param name="values"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <returns></returns>
        protected unsafe TexCoordBuffer DoCreateMatrixTextureCoordinates(DynamicUnstructuredGridderSource src, int[] gridIndexes, float[] values, float minValue, float maxValue)
        {
            int matrixStartIndex = src.FractureNum;
            int matrixEndIndex   = src.DimenSize - 1;

            int[]   textureVisibles = src.BindTextureVisibleMatrix(gridIndexes);
            float[] textures        = new float[src.InvisibleMatrixTextures.Length];
            Array.Copy(src.InvisibleMatrixTextures, textures, textures.Length);

            for (int mixedIndex = 0; mixedIndex < gridIndexes.Length; mixedIndex++)
            {
                int   gridIndex       = gridIndexes[mixedIndex];
                int[] mapBlockIndexes = src.MapBlockIndexes(gridIndex);

                for (int j = 0; j < mapBlockIndexes.Length; j++)
                {
                    int blockIndex = mapBlockIndexes[j];
                    if (blockIndex >= matrixStartIndex && blockIndex < src.DimenSize)
                    {
                        float value = values[mixedIndex];
                        if (value < minValue)
                        {
                            value = minValue;
                        }
                        if (value > maxValue)
                        {
                            value = maxValue;
                        }
                        int matrixIndex = blockIndex - matrixStartIndex;
                        if (textureVisibles[matrixIndex] > 0)
                        {
                            float distance = maxValue - minValue;
                            if (!(distance <= 0.0f))
                            {
                                textures[matrixIndex] = (value - minValue) / distance;
                                if (textures[matrixIndex] < 0.5f)
                                {
                                    textures[matrixIndex] = 0.5f - (0.5f - textures[matrixIndex]) * 0.99f;
                                }
                                else
                                {
                                    textures[matrixIndex] = (textures[matrixIndex] - 0.5f) * 0.99f + 0.5f;
                                }
                            }
                            else
                            {
                                //最小值最大值相等时,显示最小值的颜色
                                textures[matrixIndex] = 0.01f;
                                //textures[gridIndex] = 0;
                            }
                        }
                    }
                }
            }//end for

            //TextureCoordinatesBuffer textureCoordinates = new TextureCoordinatesBufferData();
            TexCoordBuffer textureCoordinates = null;


            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT3_TRIANGLE)
            {
                textureCoordinates = new TriangleMatrixTexCoordBuffer();

                //textureCoordinates.AllocMem(texturesCount * sizeof(TriangleUV));
                textureCoordinates.AllocMem(textures.Length);

                TriangleTexCoord *pTextures = (TriangleTexCoord *)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT4_TETRAHEDRON)
            {
                textureCoordinates = new TetrahedronMatrixTexCoordBuffer();

                //textureCoordinates.AllocMem(texturesCount * sizeof(TetrahedronUV));
                textureCoordinates.AllocMem(textures.Length);

                TetrahedronTexCoord *pTextures = (TetrahedronTexCoord *)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT6_TRIANGULAR_PRISM)
            {
                textureCoordinates = new TriangularPrismMatrixTexCoordBuffer();
                textureCoordinates.AllocMem(textures.Length);
                TriangularPrismTexCoord *pTextures = (TriangularPrismTexCoord *)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            return(textureCoordinates);
        }
        protected unsafe TexCoordBuffer DoCreateFractureTextureCoordinates(DynamicUnstructuredGridderSource src, int[] gridIndexes, float[] values, float minValue, float maxValue)
        {
            int fractureStartIndex = 0;
            int fractureEndIndex   = src.FractureNum - 1;

            int[]   invisibles = src.BindResultsAndActiveFractures(gridIndexes);
            float[] textures   = new float[src.InvisibleFractureTextures.Length];
            Array.Copy(src.InvisibleFractureTextures, textures, textures.Length);

            for (int mixedIndex = 0; mixedIndex < gridIndexes.Length; mixedIndex++)
            {
                int   gridIndex       = gridIndexes[mixedIndex];
                int[] mapBlockIndexes = src.MapBlockIndexes(gridIndex);
                for (int j = 0; j < mapBlockIndexes.Length; j++)
                {
                    int blockIndex = mapBlockIndexes[j];
                    if (blockIndex >= fractureStartIndex && blockIndex <= fractureEndIndex)
                    {
                        float value = values[mixedIndex];
                        if (value < minValue)
                        {
                            value = minValue;
                        }
                        if (value > maxValue)
                        {
                            value = maxValue;
                        }
                        int fracIndex = blockIndex;
                        if (invisibles[fracIndex] > 0)
                        {
                            float distance = maxValue - minValue;
                            if (!(distance <= 0.0f))
                            {
                                textures[fracIndex] = (value - minValue) / distance;
                                if (textures[fracIndex] < 0.5f)
                                {
                                    textures[fracIndex] = 0.5f - (0.5f - textures[fracIndex]) * 0.99f;
                                }
                                else
                                {
                                    textures[fracIndex] = (textures[fracIndex] - 0.5f) * 0.99f + 0.5f;
                                }
                            }
                            else
                            {
                                //最小值最大值相等时,显示最小值的颜色
                                textures[fracIndex] = 0.01f;
                                //textures[matrixIndex] = 0.01f;
                            }
                        }
                    }
                }
            }//end for

            //TextureCoordinatesBuffer textureCoordinates = new TextureCoordinatesBufferData();
            TexCoordBuffer textureCoordinates = null;

            int texturesCount = textures.Length;

            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT3_TRIANGLE)
            {
                textureCoordinates = new TriangleFractureTexCoordBuffer();
                textureCoordinates.AllocMem(texturesCount);

                TriangleTexCoord *pTextures = (TriangleTexCoord *)textureCoordinates.Data;
                for (int i = 0; i < texturesCount; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT2_LINE)
            {
                textureCoordinates = new LineFractureTexCoordBufer();
                textureCoordinates.AllocMem(texturesCount);
                LineTexCoord *pTextures = (LineTexCoord *)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT4_QUAD)
            {
                textureCoordinates = new QuadFractureTexCoordBuffer();
                textureCoordinates.AllocMem(texturesCount);
                QuadTexCoord *pTextures = (QuadTexCoord *)textureCoordinates.Data;
                for (int i = 0; i < textures.Length; i++)
                {
                    pTextures[i].SetTextureCoord(textures[i]);
                }
            }
            return(textureCoordinates);
        }
 public TexCoordBuffer CreateFractureTextureCoordinates(DynamicUnstructuredGridderSource src, int[] gridIndexes, float[] values, float minValue, float maxValue)
 {
     return(this.DoCreateFractureTextureCoordinates(src, gridIndexes, values, minValue, maxValue));
 }
        private unsafe DynamicUnstructureGeometry DoCreateMesh(DynamicUnstructuredGridderSource src)
        {
            MatrixPositionBuffer   matrixPositions         = null;
            IndexBuffer            matrixIndicesBuffer     = null;
            FracturePositionBuffer fractionPositionsBuffer = null;

            //生成母体
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT4_TETRAHEDRON)
            {
                matrixPositions = new TetrahedronMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TETRAHEDRON;

                //int memSize = src.ElementNum * sizeof(TetrahedronPositions);
                //matrixPositions.AllocMem(memSize);
                matrixPositions.AllocMem(src.ElementNum);

                TetrahedronPosition *tets = (TetrahedronPosition *)matrixPositions.Data;
                int[][]  matrixIndices    = src.Elements;
                Vertex[] positions        = src.Nodes;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    tets[i].p1 = src.Transform * positions[matrixIndices[i][0] - 1];
                    tets[i].p2 = src.Transform * positions[matrixIndices[i][1] - 1];
                    tets[i].p3 = src.Transform * positions[matrixIndices[i][2] - 1];
                    tets[i].p4 = src.Transform * positions[matrixIndices[i][3] - 1];
                }

                matrixIndicesBuffer = new TetrahedronMatrixIndexBuffer();
                //int triangleCount = src.ElementNum * 4;
                //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex));
                matrixIndicesBuffer.AllocMem(src.ElementNum);
                TetrahedronIndex *header = (TetrahedronIndex *)matrixIndicesBuffer.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    header[i].dot0         = (uint)(i * 4 + 0);
                    header[i].dot1         = (uint)(i * 4 + 1);
                    header[i].dot2         = (uint)(i * 4 + 2);
                    header[i].dot3         = (uint)(i * 4 + 3);
                    header[i].dot4         = (uint)(i * 4 + 0);
                    header[i].dot5         = (uint)(i * 4 + 1);
                    header[i].restartIndex = uint.MaxValue;
                }
            }

            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT3_TRIANGLE)
            {
                matrixPositions = new TriangleMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE;
                matrixPositions.AllocMem(src.ElementNum);
                int[][]           matrixIndices = src.Elements;
                Vertex[]          positions     = src.Nodes;
                TrianglePosition *triangles     = (TrianglePosition *)matrixPositions.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    triangles[i].P1 = src.Transform * positions[matrixIndices[i][0] - 1];
                    triangles[i].P2 = src.Transform * positions[matrixIndices[i][1] - 1];
                    triangles[i].P3 = src.Transform * positions[matrixIndices[i][2] - 1];
                }
            }

            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT6_TRIANGULAR_PRISM)
            {
                matrixPositions = new TriangularPrismMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE;
                matrixPositions.AllocMem(src.ElementNum);
                int[][]  matrixIndices         = src.Elements;
                Vertex[] positions             = src.Nodes;
                TriangularPrismPosition *prism = (TriangularPrismPosition *)matrixPositions.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    try
                    {
                        prism[i].P1 = src.Transform * positions[matrixIndices[i][0] - 1];
                        prism[i].P2 = src.Transform * positions[matrixIndices[i][1] - 1];
                        prism[i].P3 = src.Transform * positions[matrixIndices[i][2] - 1];
                        prism[i].P4 = src.Transform * positions[matrixIndices[i][3] - 1];
                        prism[i].P5 = src.Transform * positions[matrixIndices[i][4] - 1];
                        prism[i].P6 = src.Transform * positions[matrixIndices[i][5] - 1];
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        throw new IndexOutOfRangeException(String.Format("Maxtrix positions row:{0},out of range", i + 1));
                    }
                }

                matrixIndicesBuffer = new TetrahedronMatrixTriangularPrismIndexBuffer();
                //int triangleCount = src.ElementNum * 4;
                //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex));
                matrixIndicesBuffer.AllocMem(src.ElementNum);
                TetrahedronTriangularPrismIndex *indexes = (TetrahedronTriangularPrismIndex *)matrixIndicesBuffer.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    indexes[i].dot0         = (uint)(i * 6 + 0);
                    indexes[i].dot1         = (uint)(i * 6 + 3);
                    indexes[i].dot2         = (uint)(i * 6 + 1);
                    indexes[i].dot3         = (uint)(i * 6 + 4);
                    indexes[i].dot4         = (uint)(i * 6 + 2);
                    indexes[i].dot5         = (uint)(i * 6 + 5);
                    indexes[i].dot6         = (uint)(i * 6 + 0);
                    indexes[i].dot7         = (uint)(i * 6 + 3);
                    indexes[i].restartIndex = uint.MaxValue;
                }
            }

            //生成裂缝
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT3_TRIANGLE)
            {
                fractionPositionsBuffer = new TriangleFracturePositionBuffer();

                //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_TRIANGLE;

                int triangleCount = src.FractureNum;

                //fractionPositionsBuffer.AllocMem(triangleCount * sizeof(TrianglePositions));
                fractionPositionsBuffer.AllocMem(triangleCount);

                int[][]           triangleIndices = src.Fractures;
                Vertex[]          positions       = src.Nodes;
                TrianglePosition *triangles       = (TrianglePosition *)fractionPositionsBuffer.Data;
                for (int i = 0; i < triangleCount; i++)
                {
                    triangles[i].P1 = src.Transform * positions[triangleIndices[i][0] - 1];
                    triangles[i].P2 = src.Transform * positions[triangleIndices[i][1] - 1];
                    triangles[i].P3 = src.Transform * positions[triangleIndices[i][2] - 1];
                }
            }

            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT2_LINE)
            {
                fractionPositionsBuffer = new LineFracturePositionBuffer();

                //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_LINE;

                int lineCount = src.FractureNum;

                //fractionPositionsBuffer.AllocMem(lineCount * sizeof(LinePositions));
                fractionPositionsBuffer.AllocMem(lineCount);

                LinePosition *lines       = (LinePosition *)fractionPositionsBuffer.Data;
                Vertex[]      positions   = src.Nodes;
                int[][]       lineIndices = src.Fractures;
                for (int i = 0; i < lineCount; i++)
                {
                    lines[i].P1 = src.Transform * positions[lineIndices[i][0] - 1];
                    lines[i].P2 = src.Transform * positions[lineIndices[i][1] - 1];
                }
            }

            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT4_QUAD)
            {
                fractionPositionsBuffer = new QuadFracturePositionBuffer();
                int fractureCount = src.FractureNum;
                fractionPositionsBuffer.AllocMem(fractureCount);
                QuadPosition *quad        = (QuadPosition *)fractionPositionsBuffer.Data;
                Vertex[]      positions   = src.Nodes;
                int[][]       quadIndices = src.Fractures;
                for (int i = 0; i < fractureCount; i++)
                {
                    quad[i].P1 = src.Transform * positions[quadIndices[i][0] - 1];
                    quad[i].P2 = src.Transform * positions[quadIndices[i][1] - 1];
                    quad[i].P3 = src.Transform * positions[quadIndices[i][3] - 1];
                    quad[i].P4 = src.Transform * positions[quadIndices[i][2] - 1];
                }
            }

            DynamicUnstructureGeometry geometry = new DynamicUnstructureGeometry(matrixPositions);

            geometry.MatrixIndices     = matrixIndicesBuffer;
            geometry.FracturePositions = fractionPositionsBuffer;
            geometry.Min = src.TransformedActiveBounds.Min;
            geometry.Max = src.TransformedActiveBounds.Max;
            return(geometry);
        }
예제 #14
0
        private unsafe DynamicUnstructureGeometry DoCreateMesh(DynamicUnstructuredGridderSource src)
        {
            MatrixPositionBuffer         matrixPositions         = null;
            TetrahedronMatrixIndexBuffer matrixIndicesBuffer     = null;
            FracturePositionBuffer       fractionPositionsBuffer = null;

            //生成母体
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT4_TETRAHEDRON)
            {
                matrixPositions = new TetrahedronMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TETRAHEDRON;

                //int memSize = src.ElementNum * sizeof(TetrahedronPositions);
                //matrixPositions.AllocMem(memSize);
                matrixPositions.AllocMem(src.ElementNum);

                TetrahedronPosition *tets = (TetrahedronPosition *)matrixPositions.Data;
                int[][]  matrixIndices    = src.Elements;
                Vertex[] positions        = src.Nodes;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    tets[i].p1 = positions[matrixIndices[i][0] - 1];
                    tets[i].p2 = positions[matrixIndices[i][1] - 1];
                    tets[i].p3 = positions[matrixIndices[i][2] - 1];
                    tets[i].p4 = positions[matrixIndices[i][3] - 1];
                }

                matrixIndicesBuffer = new TetrahedronMatrixIndexBuffer();
                //int triangleCount = src.ElementNum * 4;
                //matrixIndicesBuffer.AllocMem(triangleCount * sizeof(TriangleIndex));
                matrixIndicesBuffer.AllocMem(src.ElementNum);
                TetrahedronIndex *header = (TetrahedronIndex *)matrixIndicesBuffer.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    header[i].dot0         = (uint)(i * 4 + 0);
                    header[i].dot1         = (uint)(i * 4 + 1);
                    header[i].dot2         = (uint)(i * 4 + 2);
                    header[i].dot3         = (uint)(i * 4 + 3);
                    header[i].dot4         = (uint)(i * 4 + 0);
                    header[i].dot5         = (uint)(i * 4 + 1);
                    header[i].restartIndex = uint.MaxValue;
                }
            }
            if (src.ElementFormat == DynamicUnstructuredGridderSource.MATRIX_FORMAT3_TRIANGLE)
            {
                matrixPositions = new TriangleMatrixPositionBuffer();
                //matrixPositions.Shape = MatrixPositionBufferData.SHAPE_TRIANGLE;
                matrixPositions.AllocMem(src.ElementNum);
                int[][]           matrixIndices = src.Elements;
                Vertex[]          positions     = src.Nodes;
                TrianglePosition *triangles     = (TrianglePosition *)matrixPositions.Data;
                for (int i = 0; i < src.ElementNum; i++)
                {
                    triangles[i].P1 = positions[matrixIndices[i][0] - 1];
                    triangles[i].P2 = positions[matrixIndices[i][1] - 1];
                    triangles[i].P3 = positions[matrixIndices[i][2] - 1];
                }
            }

            //生成裂缝
            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT3_TRIANGLE)
            {
                fractionPositionsBuffer = new TriangleFracturePositionBuffer();

                //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_TRIANGLE;

                int triangleCount = src.FractureNum;

                //fractionPositionsBuffer.AllocMem(triangleCount * sizeof(TrianglePositions));
                fractionPositionsBuffer.AllocMem(triangleCount);

                int[][]           triangleIndices = src.Fractures;
                Vertex[]          positions       = src.Nodes;
                TrianglePosition *triangles       = (TrianglePosition *)fractionPositionsBuffer.Data;
                for (int i = 0; i < triangleCount; i++)
                {
                    triangles[i].P1 = positions[triangleIndices[i][0] - 1];
                    triangles[i].P2 = positions[triangleIndices[i][1] - 1];
                    triangles[i].P3 = positions[triangleIndices[i][2] - 1];
                }
            }

            if (src.FractureFormat == DynamicUnstructuredGridderSource.FRACTURE_FORMAT2_LINE)
            {
                fractionPositionsBuffer = new LineFracturePositionBuffer();

                //fractionPositionsBuffer.Shape = FracturePositionBufferData.SHAPE_LINE;

                int lineCount = src.FractureNum;

                //fractionPositionsBuffer.AllocMem(lineCount * sizeof(LinePositions));
                fractionPositionsBuffer.AllocMem(lineCount);

                LinePosition *lines       = (LinePosition *)fractionPositionsBuffer.Data;
                Vertex[]      positions   = src.Nodes;
                int[][]       lineIndices = src.Fractures;
                for (int i = 0; i < lineCount; i++)
                {
                    lines[i].P1 = positions[lineIndices[i][0] - 1];
                    lines[i].P2 = positions[lineIndices[i][1] - 1];
                }
            }

            DynamicUnstructureGeometry geometry = new DynamicUnstructureGeometry(matrixPositions);

            geometry.MatrixIndices     = matrixIndicesBuffer;
            geometry.FracturePositions = fractionPositionsBuffer;
            geometry.Min = src.Min;
            geometry.Max = src.Max;
            return(geometry);
        }
예제 #15
0
        private void CreateCatesianGridVisual3D(object sender, EventArgs e)
        {
            try
            {
                string fileName = @"geometry.txt";
                DynamicUnstructureGeometryLoader loader = new DynamicUnstructureGeometryLoader();
                // use CatesianGridderSource to fill HexahedronGridderElement's content.
                DynamicUnstructuredGridderSource source = loader.LoadSource(fileName);
                source.Init();

                int nx        = source.NX;
                int ny        = source.NY;
                int nz        = source.NZ;
                int dimenSize = nx * ny * nz;

                float step = System.Convert.ToSingle(tbColorIndicatorStep.Text);
                //float radius = System.Convert.ToSingle(this.tbRadius.Text);
                float propMin = System.Convert.ToSingle(this.tbxPropertyMinValue.Text, CultureInfo.InvariantCulture);
                float propMax = System.Convert.ToSingle(this.tbxPropertyMaxValue.Text, CultureInfo.InvariantCulture);

                float   dx      = System.Convert.ToSingle(this.tbDX.Text);
                float   dy      = System.Convert.ToSingle(this.gbDY.Text);
                float   dz      = System.Convert.ToSingle(this.tbDZ.Text);
                float[] dxArray = initArray(dimenSize, dx);
                float[] dyArray = initArray(dimenSize, dy);
                float[] dzArray = initArray(dimenSize, dz);



                InitPropertiesAndSelectDefault(dimenSize, propMin, propMax);


                ///模拟获得网格属性
                ///获得当前选中的属性


                float minValue = this.CurrentProperty.MinValue;
                float maxValue = this.CurrentProperty.MaxValue;
                step = (maxValue * 1.0f - minValue * 1.0f) / 10;
                int[]   gridIndexes = this.CurrentProperty.GridIndexes;
                float[] gridValues  = this.CurrentProperty.Values;


                //设置色标的范围
                this.sim3D.SetColorIndicator(minValue, maxValue, step);


                // use HexahedronGridderElement
                DateTime t0 = DateTime.Now;
                DynamicUnstructureGeometry geometry = source.CreateMesh() as DynamicUnstructureGeometry;
                TexCoordBuffer             matrixTextureCoordinates    = source.CreateTextureCoordinates(gridIndexes, gridValues, minValue, maxValue);
                TexCoordBuffer             fractureTextureCoordindates = source.CreateFractureTextureCoordinates(gridIndexes, gridValues, minValue, maxValue);
                Bitmap texture = ColorPaletteHelper.GenBitmap(this.sim3D.uiColorIndicator.Data.ColorPalette);
                //geometry.Positions.Dump();
                //geometry.TriangleIndices.Dump();
                //MeshGeometry mesh = HexahedronGridderHelper.CreateMesh(source);
                DynamicUnstructureGrid gridder = new DynamicUnstructureGrid(this.sim3D.OpenGL, this.sim3D.Scene.CurrentCamera);
                gridder.Init(geometry);
                gridder.RenderGrid          = true;
                gridder.RenderGridWireframe = this.IsShowWireframe;
                gridder.SetTexture(texture);
                gridder.SetMatrixTextureCoords(matrixTextureCoordinates);
                gridder.SetFractionTextureCoords(fractureTextureCoordindates);

                this.element = gridder;

                //textureCoodinates.Dump();


                DateTime t1  = DateTime.Now;
                TimeSpan ts1 = t1 - t0;

                //mesh.VertexColors = HexahedronGridderHelper.FromColors(source, gridIndexes, colors, mesh.Visibles);
                //this.DebugMesh(mesh);

                //HexahedronGridderElement gridderElement = new HexahedronGridderElement(source, this.scientificVisual3DControl.Scene.CurrentCamera);
                //gridderElement.renderWireframe = false;
                //method1
                //gridderElement.Initialize(this.scientificVisual3DControl.OpenGL);

                //method2
                //gridderElement.Initialize(this.scientificVisual3DControl.OpenGL, mesh);
                DateTime t2 = DateTime.Now;

                //gridderElement.SetBoundingBox(mesh.Min, mesh.Max);
                this.sim3D.Tag = source;


                //gridderElement.Name = string.Format("element {0}", elementCounter++);
                //this.scientificVisual3DControl.AddModelElement(gridderElement);

                DateTime t3 = DateTime.Now;
                // update ModelContainer's BoundingBox.
                BoundingBox boundingBox = this.sim3D.ModelContainer.BoundingBox;
                boundingBox.SetBounds(geometry.Min, geometry.Max);

                // update ViewType to UserView.
                this.sim3D.ViewType = ViewTypes.UserView;
                this.sim3D.AddModelElement(gridder);
                //mesh.Dispose();

                StringBuilder msgBuilder = new StringBuilder();
                msgBuilder.AppendLine(String.Format("create mesh in {0} secs", (t1 - t0).TotalSeconds));
                msgBuilder.AppendLine(String.Format("init SceneElement in {0} secs", (t2 - t1).TotalSeconds));
                msgBuilder.AppendLine(String.Format("total load in {0} secs", (t2 - t0).TotalSeconds));
                String msg = msgBuilder.ToString();
                MessageBox.Show(msg, "Summary", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }
        }