Passed
Push — master ( 8f5e6a...061d3e )
by Felipe
03:26
created

MeshInterpolate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B IntSquare() 0 24 2
C Linear() 0 32 7
1
<?php
2
namespace Amenadiel\JpGraph\Plot;
3
4
/*=======================================================================
5
// File:        JPGRAPH_MESHINTERPOLATE.INC.PHP
6
// Description: Utility class to do mesh linear interpolation of a matrix
7
// Created:     2009-03-09
8
// Ver:         $Id: jpgraph_meshinterpolate.inc.php 1709 2009-07-30 08:00:08Z ljp $
9
//
10
// Copyright (c) Asial Corporation. All rights reserved.
11
//========================================================================
12
 */
13
14
/**
15
 * Utility function to do linear mesh interpolation
16
 * @param $aDat Matrix to interpolate
17
 * @param $aFactor Interpolation factor
18
 */
19
function doMeshInterpolate(&$aData, $aFactor)
20
{
21
    $m     = new MeshInterpolate();
22
    $aData = $m->Linear($aData, $aFactor);
23
}
24
25
/**
26
 * Utility class to interpolate a given data matrix
27
 *
28
 */
29
class MeshInterpolate
30
{
31
    private $data = array();
32
33
    /**
34
     * Calculate the mid points of the given rectangle which has its top left
35
     * corner at $row,$col. The $aFactordecides how many spliots should be done.
36
     * i.e. how many more divisions should be done recursively
37
     *
38
     * @param $row Top left corner of square to work with
39
     * @param $col Top left corner of square to work with
40
     * $param $aFactor In how many subsquare should we split this square. A value of 1 indicates that no action
41
     */
42
    public function IntSquare($aRow, $aCol, $aFactor)
43
    {
44
        if ($aFactor <= 1) {
45
            return;
46
        }
47
48
        $step = pow(2, $aFactor - 1);
49
50
        $v0 = $this->data[$aRow][$aCol];
51
        $v1 = $this->data[$aRow][$aCol + $step];
52
        $v2 = $this->data[$aRow + $step][$aCol];
53
        $v3 = $this->data[$aRow + $step][$aCol + $step];
54
55
        $this->data[$aRow][$aCol + $step / 2]             = ($v0 + $v1) / 2;
56
        $this->data[$aRow + $step / 2][$aCol]             = ($v0 + $v2) / 2;
57
        $this->data[$aRow + $step][$aCol + $step / 2]     = ($v2 + $v3) / 2;
58
        $this->data[$aRow + $step / 2][$aCol + $step]     = ($v1 + $v3) / 2;
59
        $this->data[$aRow + $step / 2][$aCol + $step / 2] = ($v0 + $v1 + $v2 + $v3) / 4;
60
61
        $this->IntSquare($aRow, $aCol, $aFactor - 1);
62
        $this->IntSquare($aRow, $aCol + $step / 2, $aFactor - 1);
63
        $this->IntSquare($aRow + $step / 2, $aCol, $aFactor - 1);
64
        $this->IntSquare($aRow + $step / 2, $aCol + $step / 2, $aFactor - 1);
65
    }
66
67
    /**
68
     * Interpolate values in a matrice so that the total number of data points
69
     * in vert and horizontal axis are $aIntNbr more. For example $aIntNbr=2 will
70
     * make the data matrice have tiwce as many vertical and horizontal dta points.
71
     *
72
     * Note: This will blow up the matrcide in memory size in the order of $aInNbr^2
73
     *
74
     * @param  $ &$aData The original data matricde
75
     * @param  $aInNbr Interpolation factor
76
     * @return the interpolated matrice
77
     */
78
    public function Linear(&$aData, $aIntFactor)
79
    {
80
        $step = pow(2, $aIntFactor - 1);
81
82
        $orig_cols = count($aData[0]);
83
        $orig_rows = count($aData);
84
        // Number of new columns/rows
85
        // N = (a-1) * 2^(f-1) + 1
1 ignored issue
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
        $p        = pow(2, $aIntFactor - 1);
87
        $new_cols = $p * ($orig_cols - 1) + 1;
88
        $new_rows = $p * ($orig_rows - 1) + 1;
89
90
        $this->data = array_fill(0, $new_rows, array_fill(0, $new_cols, 0));
91
        // Initialize the new matrix with the values that we know
92
        for ($i = 0; $i < $new_rows; $i++) {
93
            for ($j = 0; $j < $new_cols; $j++) {
94
                $v = 0;
95
                if (($i % $step == 0) && ($j % $step == 0)) {
96
                    $v = $aData[$i / $step][$j / $step];
97
                }
98
                $this->data[$i][$j] = $v;
99
            }
100
        }
101
102
        for ($i = 0; $i < $new_rows - 1; $i += $step) {
0 ignored issues
show
Comprehensibility Bug introduced by
Loop incrementor ($step) jumbling with inner loop
Loading history...
103
            for ($j = 0; $j < $new_cols - 1; $j += $step) {
104
                $this->IntSquare($i, $j, $aIntFactor);
105
            }
106
        }
107
108
        return $this->data;
109
    }
110
}
111