Example #1
0
        public IHeatMap CalculateCurvature(IHeatMap hm, string rowId, short rowType, IList <IBody> bodyList, int scansPerNode, int skipSize)
        {
            var           b      = bodyList;
            int           N      = scansPerNode;
            List <double> cv     = new List <double>();
            List <string> idList = new List <string>();

            for (var i = 0; i < (b.Count - 2 * N - 1); i += skipSize)
            {
                var iN  = i + N;
                var iN2 = i + 2 * N;

                var x0 = b[iN].X - b[i].X;
                var y0 = b[iN].Y - b[i].Y;
                var z0 = b[iN].Z - b[i].Z;

                var x1 = b[iN2].X - b[iN].X;
                var y1 = b[iN2].Y - b[iN].Y;
                var z1 = b[iN2].Z - b[iN].Z;


                var x2 = x0 + x1;
                var y2 = y0 + y1;
                var z2 = z0 + z1;

                // K = 2*sin(A)/s,
                // where A is the angle between B[k+1]-B[k] and B[k+2]-B[k+1],
                // s is the length of B[k+2]-B[k].

                var n0 = x0 * x0 + y0 * y0 + z0 * z0;
                var n1 = x1 * x1 + y1 * y1 + z1 * z1;
                var n2 = x2 * x2 + y2 * y2 + z2 * z2;
                if ((n0 > 0) && (n1 > 0) && (n2 > 0))
                {
                    var dot   = x0 * x1 + y0 * y1 + z0 * z1;
                    var sinA2 = 1 - (dot * dot) / (n0 * n1);         // sinA2 = sin(A)^2.
                    if (sinA2 > 0)
                    {
                        cv.Add(2 * Math.Sqrt(sinA2 / n2));
                    }
                }
                else
                {
                    cv.Add(0);
                }
                idList.Add(b[iN].Id);
            }

            var app = GeneticAnalysis.App.ScriptApp;

            if (hm == null)
            {
                var fs = app.FindFormList("HeatMap");
                if (fs.Count > 0)
                {
                    hm = fs[0] as IHeatMap;
                }
            }

            if (hm == null)
            {
                var tb = app.New.NumberTable(cv.ToArray());
                for (var col = 0; col < tb.Columns; col++)
                {
                    tb.ColumnSpecList[col].Id = idList[col];
                }
                tb.RowSpecList[0].Type = rowType;
                if (rowId != null)
                {
                    tb.RowSpecList[0].Id = rowId;
                }
                hm              = tb.ShowHeatMap();
                hm.ReadOnly     = true;
                hm.SpectrumType = 2;
            }
            else
            {
                var tb = hm.GetNumberTable();
                if (tb.Columns < cv.Count)
                {
                    int newColumns = cv.Count - tb.Columns;
                    tb.AddColumns(newColumns);
                    for (var i = 0; i < newColumns; i++)
                    {
                        int col = cv.Count - newColumns + i;
                        tb.ColumnSpecList[col].Id = idList[col];
                    }
                }
                var uf = (rowId != null) ? new UniqueNameFinder(tb.RowSpecList.Select(rs => rs.Id)) : null;

                tb.AddRows(1);
                for (var col = 0; col < cv.Count; col++)
                {
                    tb.Matrix[tb.Rows - 1][col] = cv[col];
                }
                tb.RowSpecList[tb.Rows - 1].Type = rowType;
                if (rowId != null)
                {
                    tb.RowSpecList[tb.Rows - 1].Id = uf.LookupName(rowId);
                }
                hm.Title = "N: " + tb.Rows;
                hm.Redraw();
            }
            hm.CentralizeColorSpectrum();
            return(hm);
        }