Subversion Repositories public iLand

Rev

Rev 6 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
3 Werner 2
#include <math.h>
3
 
4
#include "tree.h"
5
#include "../core/grid.h"
6
#include "stamp.h"
7
 
8
LogicExpression Tree::rScale=LogicExpression();
9
LogicExpression Tree::hScale=LogicExpression();
10
Tree::Tree()
11
{
12
}
13
float dist_and_direction(const QPointF &PStart, const QPointF &PEnd, float &rAngle)
14
{
15
    float dx = PEnd.x() - PStart.x();
16
    float dy = PEnd.y() - PStart.y();
17
    float d = sqrt(dx*dx + dy*dy);
18
    // direction:
19
    rAngle = atan2(dx, dy);
20
    return d;
21
}
22
 
23
void Tree::stampOnGrid(Stamp& stamp, FloatGrid& grid)
24
{
25
 
26
    // use formulas to derive scaling values...
27
    rScale.setVar("height", mHeight);
28
    rScale.setVar("dbh", mDbh);
29
    hScale.setVar("height", mHeight);
30
    hScale.setVar("dbh", mDbh);
31
 
32
    double r = rScale.execute();
33
    double h = hScale.execute();
34
    stamp.setScale(r, h); // stamp uses scaling values to calculate impact
35
 
36
    float cell_value, r_cell, phi_cell;
37
    QPoint ul = grid.indexAt(QPointF(mPosition.x()-r, mPosition.y()-r) );
38
    QPoint lr =  grid.indexAt( QPointF(mPosition.x()+r, mPosition.y()+r) );
39
    grid.validate(ul); grid.validate(lr);
40
    QPoint cell;
41
    QPointF cellcoord;
42
    int ix, iy;
43
    mOwnImpact=0.f;
44
    for (ix=ul.x(); ix<lr.x(); ix++)
45
        for (iy=ul.y(); iy<lr.y(); iy++) {
46
        cell.setX(ix); cell.setY(iy);
47
        cellcoord = grid.getCellCoordinates(cell);
48
        r_cell = dist_and_direction(mPosition, cellcoord, phi_cell);
49
        if (r_cell > r)
50
            continue;
51
        // get value from stamp at this location (given by radius and angle)
52
        cell_value = stamp.get(r_cell, phi_cell);
53
        // add value to cell
54
        mOwnImpact+=cell_value;
55
        grid.valueAtIndex(cell)+=cell_value;
56
    }
57
}
58
 
59
float Tree::retrieveValue(Stamp& stamp, FloatGrid& grid)
60
{
61
 
62
    rScale.setVar("height", mHeight);
63
    rScale.setVar("dbh", mDbh);
64
    hScale.setVar("height", mHeight);
65
    hScale.setVar("dbh", mDbh);
66
    double r = rScale.execute();
67
    double h = hScale.execute();
68
    stamp.setScale(r, h); // stamp uses scaling values to calculate impact
69
 
70
    float cell_value, r_cell, phi_cell;
71
    QPoint ul = grid.indexAt(QPointF(mPosition.x()-r, mPosition.y()-r) );
72
    QPoint lr =  grid.indexAt( QPointF(mPosition.x()+r, mPosition.y()+r) );
73
    grid.validate(ul); grid.validate(lr);
74
    QPoint cell;
75
    QPointF cellcoord;
76
    int ix, iy;
77
    float value=0.f;
78
 
79
    int counting_cells=0;
80
    for (ix=ul.x(); ix<lr.x(); ix++)
81
        for (iy=ul.y(); iy<lr.y(); iy++) {
82
 
83
        cell.setX(ix); cell.setY(iy);
84
        cellcoord = grid.getCellCoordinates(cell);
85
        r_cell = dist_and_direction(mPosition, cellcoord, phi_cell);
86
        if (r_cell>r)
87
            continue;
88
        counting_cells++;
89
        // get value from stamp at this location (given by radius and angle)
90
        //cell_value = stamp.get(r_cell, phi_cell);
91
        // sum up values of cell
92
        //value += cell_value;
93
        value += grid.valueAtIndex(QPoint(ix,iy)); // - cell_value;
94
    }
95
    mImpact = (value - mOwnImpact)/ float(counting_cells);
96
    return mImpact;
97
}
98