1 /*
2  *
3  * Copyright (C) 2007,2008  Markko Merzin (markko.merzin@ut.ee)
4  *
5  * This file is part of ImageJ plugin Volumest.
6  * 
7  * Volumest plugin is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *****************************************************************************
21 */
22package ee.ut.mrz.volumest;
23
24/**
25 * Does "businesslogic" calculations. Has algorithms for area, volume and
26 * Coefficent of Error calculations.
27 * @author Markko Merzin, markko.merzin@ut.ee
28 */
29public class Calculations {
30
31    /**
32     * Returns selected area of single grid.     
33     */
34    public static double getGridArea(int activePointsCount) {
35        return Math.pow(Settings.getInstance().getGridWidth(), 2) * activePointsCount;
36    }
37
38    /** Returns object volume. */
39    public static double getObjectVolume(int[] grids) {
40        int sumPoints = 0;
41        for (int c = 0; c < grids.length; c++) {
42            sumPoints = sumPoints + grids[c];
43        }
44
45        return Math.pow(Settings.getInstance().getGridWidth(), 2) *
46                Settings.getInstance().getSliceThickness() *
47                Settings.getInstance().getNumberOfSteps() * sumPoints;
48
49    }
50
51    /** Returns coefficent of error assuming object is regular shaped.*/
52    public static double getRegularObjectCE(int[] grids, int wholeObjectIntersections) {
53        long sumPoints = 0;
54        long pointsCountSquares = 0;
55        for (int c = 0; c < grids.length; c++) {
56            sumPoints = sumPoints + grids[c];
57            pointsCountSquares = pointsCountSquares + grids[c] * grids[c];
58        }
59
60        double shapeCoefficent = (Math.PI * wholeObjectIntersections) /
61                (4 * Math.sqrt(sumPoints) * Math.sqrt(grids.length));
62
63        long sumovertwo = 0;
64        for (int c = 0; c < grids.length - 2; c++) {
65            int pi = grids[c];
66            int piplus2 = grids[c + 2];
67            sumovertwo = sumovertwo + pi * piplus2;
68        }
69
70        long sumoverone = 0;
71        for (int c = 0; c < grids.length - 1; c++) {
72            int pi = grids[c];
73            int piplus1 = grids[c + 1];
74            sumoverone = sumoverone + pi * piplus1;
75        }
76
77        double gridDifImpact = 3 * pointsCountSquares + sumovertwo - 4 * sumoverone;
78
79        double CE = Math.sqrt((gridDifImpact / 240) + 0.0715 * shapeCoefficent *
80                Math.sqrt(grids.length * sumPoints)) / sumPoints;
81
82        return CE;
83
84    }
85
86    /** Returns coefficent of error assuming object is irreguler shape.*/
87    public static double getIrregularObjectCE(int[] grids, int wholeObjectIntersections) {
88        long sumPoints = 0;
89        long pointsCountSquares = 0;
90        for (int c = 0; c < grids.length; c++) {
91            sumPoints = sumPoints + grids[c];
92            pointsCountSquares = pointsCountSquares + grids[c] * grids[c];
93        }
94
95        double shapeCoefficent = (Math.PI * wholeObjectIntersections) /
96                (4 * Math.sqrt(sumPoints) * Math.sqrt(grids.length));
97
98        long sumovertwo = 0;
99        for (int c = 0; c < grids.length - 2; c++) {
00            int pi = grids[c];
01            int piplus2 = grids[c + 2];
02            sumovertwo = sumovertwo + pi * piplus2;
03        }
04
05        long sumoverone = 0;
06        for (int c = 0; c < grids.length - 1; c++) {
07            int pi = grids[c];
08            int piplus1 = grids[c + 1];
09            sumoverone = sumoverone + pi * piplus1;
10        }
11
12        double gridDifImpact = 3 * pointsCountSquares + sumovertwo - 4 * sumoverone;
13
14        double CE = Math.sqrt((gridDifImpact / 12) + 0.0543 * shapeCoefficent * 
15                Math.sqrt(grids.length * sumPoints)) / sumPoints;
16
17        return CE;
18
19    }
20}
21