Beispiel #1
0
        public void CalculateLambda2CriterionIrregularGrids(List <Vector3d> pointsToRegistrate, Dictionary <string, List <float> > scalarValues, List <string> scalarNames, List <Vector4> tetraPoints)
        {
            List <float> scalarValuesX = new List <float>();
            List <float> scalarValuesY = new List <float>();
            List <float> scalarValuesZ = new List <float>();

            //if (scalarNames.Count() <= 4)
            //{
            scalarValues.TryGetValue(scalarNames[0], out scalarValuesX);
            scalarValues.TryGetValue(scalarNames[1], out scalarValuesY);
            scalarValues.TryGetValue(scalarNames[2], out scalarValuesZ);
            //}
            //else
            //{
            //    scalarValues.TryGetValue(scalarNames[1], out scalarValuesX);
            //    scalarValues.TryGetValue(scalarNames[2], out scalarValuesY);
            //    scalarValues.TryGetValue(scalarNames[3], out scalarValuesZ);
            //}

            vtkDoubleArray lambda2Values     = new vtkDoubleArray();
            List <double>  lambda2ValuesList = new List <double>();

            // create vectorfield
            List <Vector3d> vectorField = new List <Vector3d>();

            for (int i = 0; i < scalarValuesX.Count(); i++)
            {
                Vector3d vec = new Vector3d(scalarValuesX[i], scalarValuesY[i], scalarValuesZ[i]);

                vectorField.Add(vec);
            }

            int count = 0;

            double[,] lambda2ValuesArray = new double[pointsToRegistrate.Count(), 2];
            for (int k = 0; k < tetraPoints.Count(); k++)
            {
                // first rows and than columns
                double[,] A         = new double[12, 12];
                double[,] rightSide = new double[12, 1];
                int i = 0, j = 0;
                int s = 0;

                if (k == 779834)
                {
                    Console.WriteLine("Test");
                }

                for (int g = 0; g < 12; g += 3, i++, s++)
                {
                    //1-3 columns of 4x3 Matrix
                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 0; m < 3; m++)
                        {
                            if (l == m)
                            {
                                // Warum tetraPoints[k]
                                A[g + l, m] = pointsToRegistrate[(int)tetraPoints[k][i]].X;
                            }
                            else
                            {
                                A[g + l, m] = 0;
                            }
                        }
                    }
                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 3; m < 6; m++)
                        {
                            if (l == (m - 3))
                            {
                                A[g + l, m] = pointsToRegistrate[(int)tetraPoints[k][i]].Y;
                            }
                            else
                            {
                                A[g + l, m] = 0;
                            }
                        }
                    }
                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 6; m < 9; m++)
                        {
                            if (l == (m - 6))
                            {
                                A[g + l, m] = pointsToRegistrate[(int)tetraPoints[k][i]].Z;
                            }
                            else
                            {
                                A[g + l, m] = 0;
                            }
                        }
                    }

                    for (int l = 0; l < 3; l++)
                    {
                        for (int m = 9; m < 12; m++)
                        {
                            if (l == (m - 9))
                            {
                                A[g + l, m] = 1;
                            }
                            else
                            {
                                A[g + l, m] = 0;
                            }
                        }
                    }

                    rightSide[g, 0]     = vectorField[(int)tetraPoints[k][s]].X;
                    rightSide[g + 1, 0] = vectorField[(int)tetraPoints[k][s]].Y;
                    rightSide[g + 2, 0] = vectorField[(int)tetraPoints[k][s]].Z;
                }

                //for (int l = 0; l < 12; l++)
                //{
                //    for (int m = 0; m < 12; m++)
                //    {
                //        Console.Write(A[l, m] + " ");
                //    }
                //    Console.WriteLine("");
                //}



                double[,] Vold = Accord.Math.Matrix.Solve(A, rightSide, leastSquares: true);
                // Solve matrix with matlab
                //LGS lgsObj = new LGS();
                //MWNumericArray mwV = (MWNumericArray)lgsObj.solveLGS((MWNumericArray)A, (MWNumericArray)rightSide);
                //double[,] V = new double[mwV.Dimensions[0], 1];
                //for (i = 1; i <= mwV.Dimensions[1]; i++)
                //{
                //    V[i, 0] = (double)mwV[1, i];
                //}

                double[,] jacobiMat = new double[3, 3];
                int n = 0;
                for (i = 0; i < 3; i++)
                {
                    for (j = 0; j < 3; j++)
                    {
                        // first increas rows and than columns
                        jacobiMat[j, i] = Vold[n, 0];
                        //jacobiMat[i, j] = Vold[n, 0];
                        n++;
                    }
                }

                if (jacobiMat[1, 1] == 0 && jacobiMat[0, 0] == 0 && jacobiMat[2, 2] == 0)
                {
                    //lambda2Values.InsertNextValue(0);
                    //lambda2ValuesList.Add(0);

                    count++;
                }
                //only if diagonal elements are not equal zero
                else
                {
                    MatrixOperations classObj = new MatrixOperations();

                    double[,] transposedJacobiMat = classObj.TransposeRowsAndColumns(jacobiMat);
                    // calculation of S und Omega
                    double[,] S        = classObj.DivideArrayByValue(classObj.AddArrays(jacobiMat, transposedJacobiMat), 2);
                    double[,] squaredS = classObj.MultiplyArrays(S, S);

                    double[,] Omega        = classObj.DivideArrayByValue(classObj.SubtractArrays(jacobiMat, transposedJacobiMat), 2);
                    double[,] squaredOmega = classObj.MultiplyArrays(Omega, Omega);

                    double[,] summation = classObj.AddArrays(squaredS, squaredOmega);

                    var    eigenvalueDecomposition = new Accord.Math.Decompositions.EigenvalueDecomposition(summation, false, true, true);
                    var    eigenVec   = eigenvalueDecomposition.Eigenvectors;
                    var    eigenValue = eigenvalueDecomposition.RealEigenvalues;
                    double lambda2    = eigenValue[1];

                    //int t = (int)lambda2Values.InsertNextValue(lambda2);

                    //lambda2ValuesList.Add(lambda2);

                    lambda2ValuesArray[(int)tetraPoints[k][0], 0] += lambda2;
                    lambda2ValuesArray[(int)tetraPoints[k][0], 1] += 1;


                    lambda2ValuesArray[(int)tetraPoints[k][1], 0] += lambda2;
                    lambda2ValuesArray[(int)tetraPoints[k][1], 1] += 1;

                    lambda2ValuesArray[(int)tetraPoints[k][2], 0] += lambda2;
                    lambda2ValuesArray[(int)tetraPoints[k][2], 1] += 1;

                    lambda2ValuesArray[(int)tetraPoints[k][3], 0] += lambda2;
                    lambda2ValuesArray[(int)tetraPoints[k][3], 1] += 1;

                    //if (k == 779834)
                    //    Console.WriteLine("Test");

                    //if (lambda2 < 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][0], 0] = lambda2;
                    //    lambda2ValuesArray[(int)tetraPoints[k][0], 1] += 1;
                    //}
                    ////else
                    ////{
                    ////    lambda2ValuesArray[(int)tetraPoints[k][0], 0] += lambda2;
                    ////    lambda2ValuesArray[(int)tetraPoints[k][0], 1] += 1;
                    ////}
                    //if (lambda2 < 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][1], 0] = lambda2;
                    //    lambda2ValuesArray[(int)tetraPoints[k][1], 1] += 1;

                    //}
                    ////else
                    ////{
                    ////    lambda2ValuesArray[(int)tetraPoints[k][1], 0] += lambda2;
                    ////    lambda2ValuesArray[(int)tetraPoints[k][1], 1] += 1;
                    ////}
                    //if (lambda2 < 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][2], 0] = lambda2;
                    //    lambda2ValuesArray[(int)tetraPoints[k][2], 1] += 1;

                    //}
                    ////else
                    ////{
                    ////    lambda2ValuesArray[(int)tetraPoints[k][2], 0] += lambda2;
                    ////    lambda2ValuesArray[(int)tetraPoints[k][2], 1] += 1;
                    ////}
                    //if (lambda2 < 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][3], 0] = lambda2;
                    //    lambda2ValuesArray[(int)tetraPoints[k][3], 1] += 1;

                    //}

                    // if (lambda2ValuesArray[(int)tetraPoints[k][0], 1] == 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][0], 0] = lambda2;
                    //}
                    // if (lambda2ValuesArray[(int)tetraPoints[k][1], 1] == 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][1], 0] = lambda2;
                    //}
                    // if (lambda2ValuesArray[(int)tetraPoints[k][2], 1] == 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][2], 0] = lambda2;
                    //}
                    // if (lambda2ValuesArray[(int)tetraPoints[k][3], 1] == 0)
                    //{
                    //    lambda2ValuesArray[(int)tetraPoints[k][3], 0] = lambda2;
                    //}
                }
            }
            for (int i = 0; i < pointsToRegistrate.Count(); i++)
            {
                if (lambda2ValuesArray[i, 1] > 1)
                {
                    double lambda2 = lambda2ValuesArray[i, 0] / lambda2ValuesArray[i, 1];
                    //if (lambda2 < 0)
                    //    Console.WriteLine("lambda2 ist " + lambda2);
                    lambda2ValuesList.Add(lambda2);
                    lambda2Values.InsertNextValue(lambda2);
                    count++;
                }
                else
                {
                    count++;
                    lambda2Values.InsertNextValue(lambda2ValuesArray[i, 0]);
                }
            }

            //WriteScalarDifferences(lambda2ValuesList, "..\\..\\..\\..\\..\\03Daten\\lambda2\\Tomo_lambda2regularGridsOwnEigen.txt");
            vtkDataArray dataArray;

            dataArray = lambda2Values;
            dataArray.SetName("lambda2");
            if (this.scalar_dataArray.ContainsKey("lambda2"))
            {
                this.scalar_dataArray["lambda2"] = lambda2Values;
            }

            else
            {
                this.scalar_dataArray.Add("lambda2", dataArray);
                this.arrayNames.Add("lambda2");
            }
        }
Beispiel #2
0
        public void CalculateLambdaCriterionRegularGrids(List <Vector3d> pointsToRegistrate, Dictionary <string, List <float> > scalarValues, List <string> scalarNames, int[] dimensions)
        {
            List <float> scalarValuesX = new List <float>();

            scalarValues.TryGetValue(scalarNames[0], out scalarValuesX);
            List <float> scalarValuesY = new List <float>();

            scalarValues.TryGetValue(scalarNames[1], out scalarValuesY);
            List <float> scalarValuesZ = new List <float>();

            scalarValues.TryGetValue(scalarNames[2], out scalarValuesZ);

            // create vectorfield
            List <Vector3d> vectorField = new List <Vector3d>();

            for (int i = 0; i < scalarValuesX.Count(); i++)
            {
                Vector3d vec = new Vector3d(scalarValuesX[i], scalarValuesY[i], scalarValuesZ[i]);

                vectorField.Add(vec);
            }

            vtkDoubleArray lambda2Values = new vtkDoubleArray();
            // lambda2Values.SetNumberOfValues(vectorField.Count());

            int sizeJacobimat = 3;

            // iterating over all points
            for (int k = 0; k < vectorField.Count(); k++)
            {
                if (k == 356003)
                {
                    Console.WriteLine("ID");
                }
                double[,] jacobiMat = new double[sizeJacobimat, sizeJacobimat];

                for (int j = 0, i = 0; j < sizeJacobimat; j++, i++)
                {
                    Vector3d vec = vectorField[k];

                    Vector3d gradVec = new Vector3d();
                    // first column of jacobi matrix
                    if (i == 0)
                    {
                        // forward difference
                        if (k == 0)
                        {
                            double hx = Math.Abs(pointsToRegistrate[k + 1].X - pointsToRegistrate[k].X);
                            gradVec = (vectorField[k + 1] - vec) / hx;
                        }
                        // backward difference
                        else if (k == vectorField.Count() - 1)
                        {
                            double hx = Math.Abs(pointsToRegistrate[k - 1].X - pointsToRegistrate[k].X);
                            gradVec = (vec - vectorField[k - 1]) / hx;
                        }
                        // central difference
                        else
                        {
                            double   hx = Math.Abs(pointsToRegistrate[k + 1].X - pointsToRegistrate[k].X);
                            Vector3d f  = vectorField[k + 1];
                            Vector3d b  = vectorField[k - 1];
                            gradVec = (f - b) / (2 * hx);
                        }
                    }
                    // second column of jacobi matrix
                    else if (i == 1)
                    {
                        // forward difference
                        if (k - dimensions[0] < 0)
                        {
                            double hy = Math.Abs(pointsToRegistrate[k + dimensions[0]].Y - pointsToRegistrate[k].Y);
                            gradVec = (vectorField[k + dimensions[0]] - vec) / hy;
                        }
                        // backward difference
                        else if (k + dimensions[0] > vectorField.Count() - 1)
                        {
                            double hy = Math.Abs(pointsToRegistrate[k - dimensions[0]].Y - pointsToRegistrate[k].Y);
                            gradVec = (vec - vectorField[k - dimensions[0]]) / hy;
                        }
                        // central difference
                        else
                        {
                            double   hy = Math.Abs(pointsToRegistrate[k + dimensions[0]].Y - pointsToRegistrate[k].Y);
                            Vector3d f  = vectorField[k + dimensions[0]];
                            Vector3d b  = vectorField[k - dimensions[0]];
                            gradVec = (f - b) / (2 * hy);
                        }
                    }
                    // third column of jacobi matrix
                    else if (i == 2)
                    {
                        // forward difference
                        if (k - (dimensions[0] * dimensions[1]) < 0)
                        {
                            double hz = Math.Abs(pointsToRegistrate[k + (dimensions[0] * dimensions[1])].Z - pointsToRegistrate[k].Z);
                            gradVec = (vectorField[k + (dimensions[0] * dimensions[1])] - vec) / hz;
                        }
                        // backward difference
                        else if (k + (dimensions[0] * dimensions[1]) > vectorField.Count() - 1)
                        {
                            double hz = Math.Abs(pointsToRegistrate[k - (dimensions[0] * dimensions[1])].Z - pointsToRegistrate[k].Z);
                            gradVec = (vec - vectorField[k - (dimensions[0] * dimensions[1])]) / hz;
                        }
                        // central difference
                        else
                        {
                            double   hz = Math.Abs(pointsToRegistrate[k + (dimensions[0] * dimensions[1])].Z - pointsToRegistrate[k].Z);
                            Vector3d f  = vectorField[k + (dimensions[0] * dimensions[1])];
                            Vector3d b  = vectorField[k - (dimensions[0] * dimensions[1])];
                            gradVec = (f - b) / (2 * hz);
                        }
                    }
                    // fill jacobi matrix
                    jacobiMat[0, j] = gradVec[0];
                    jacobiMat[1, j] = gradVec[1];
                    if (sizeJacobimat == 3)
                    {
                        jacobiMat[2, j] = gradVec[2];
                    }
                }
                // only if diagonal elements are not equal zero
                if (jacobiMat[1, 1] == 0 && jacobiMat[0, 0] == 0 && jacobiMat[2, 2] == 0)
                {
                    lambda2Values.InsertNextValue(0);
                }
                else
                {
                    MatrixOperations classObj = new MatrixOperations();

                    double[,] transposedJacobiMat = classObj.TransposeRowsAndColumns(jacobiMat);
                    // calculation of S und Omega
                    double[,] S        = classObj.DivideArrayByValue(classObj.AddArrays(jacobiMat, transposedJacobiMat), 2);
                    double[,] squaredS = classObj.MultiplyArrays(S, S);

                    double[,] Omega        = classObj.DivideArrayByValue(classObj.SubtractArrays(jacobiMat, transposedJacobiMat), 2);
                    double[,] squaredOmega = classObj.MultiplyArrays(Omega, Omega);

                    double[,] summation = classObj.AddArrays(squaredS, squaredOmega);

                    var    eigenvalueDecomposition = new Accord.Math.Decompositions.EigenvalueDecomposition(summation, false, false, true);
                    var    eigenVec   = eigenvalueDecomposition.Eigenvectors;
                    var    eigenValue = eigenvalueDecomposition.RealEigenvalues;
                    double lambda2    = eigenValue[1];


                    lambda2Values.InsertNextValue(lambda2);
                }
            }
            vtkDataArray dataArray;

            dataArray = lambda2Values;
            dataArray.SetName("lambda2");
            Console.WriteLine("All lambda2 were calculated!");
            if (this.scalar_dataArray.ContainsKey("lambda2"))
            {
                this.scalar_dataArray["lambda2"] = lambda2Values;
            }

            else
            {
                this.scalar_dataArray.Add("lambda2", dataArray);
                this.arrayNames.Add("lambda2");
            }
        }