Subversion Repositories public iLand

Rev

Rev 705 | Rev 779 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1
 
671 werner 2
/********************************************************************************************
3
**    iLand - an individual based forest landscape and disturbance model
4
**    http://iland.boku.ac.at
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
6
**
7
**    This program is free software: you can redistribute it and/or modify
8
**    it under the terms of the GNU General Public License as published by
9
**    the Free Software Foundation, either version 3 of the License, or
10
**    (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, see <http://www.gnu.org/licenses/>.
19
********************************************************************************************/
20
 
30 Werner 21
#ifndef STAMP_H
22
#define STAMP_H
23
 
487 werner 24
#include <QtCore>
83 Werner 25
#include "grid.h"
697 werner 26
/** Stamp is the basic class for the LIP field of a individual tree.
27
    @ingroup core
28
    In iLand jargon, a Stamp is a LIP (light influence pattern). These patterns are pre-calculated using the "LightRoom" (http://iland.boku.ac.at/Lightroom)
29
    and stand for a field of influence (w.r.t. light) of a individual tree of a given size and species.
30
    see http://iland.boku.ac.at/competition+for+light
30 Werner 31
*/
32
class Stamp
33
{
34
public:
33 Werner 35
    /// @enum StampType defines different grid sizes for stamps (4x4 floats, ... 48x48 floats).
36
    /// the numeric value indicates also the size of the grid.
131 Werner 37
    enum StampType { est4x4=4, est8x8=8, est12x12=12, est16x16=16, est24x24=24, est32x32=32, est48x48=48, est64x64=64 };
30 Werner 38
    Stamp();
39
    ~Stamp();
35 Werner 40
    Stamp(const int size):m_data(NULL) { setup(size); }
34 Werner 41
    void setOffset(const int offset) { m_offset = offset; }
487 werner 42
    static void setDistanceGrid(const Grid<float> *grid) { mDistanceGrid = grid; }
145 Werner 43
    int offset() const { return m_offset; } ///< delta between edge of the stamp and the logical center point (of the tree). e.g. a 5x5 stamp in an 8x8-grid has an offset from 2.
44
    int count() const { return m_size*m_size; } ///< count of pixels (rectangle)
45
    int size() const { return m_offset*2+1; } ///< logical size of the stamp
46
    int dataSize() const { return m_size; } ///< internal size of the stamp; e.g. 4 -> 4x4 stamp with 16 pixels.
30 Werner 47
    /// get a full access pointer to internal data
48
    float *data() { return m_data; }
49
    /// get pointer to the element after the last element (iterator style)
32 Werner 50
    const float *end() const { return &m_data[m_size*m_size]; }
30 Werner 51
    /// get pointer to data item with indices x and y
705 werner 52
    inline float *data(const int x, const int y) const { return m_data + index(x,y); }
34 Werner 53
    void setData(const int x, const int y, const float value) { *data(x,y) = value; }
30 Werner 54
    /// get index (e.g. for data()[index]) for indices x and y
739 werner 55
    inline int index(const int x, const int y) const {  Q_ASSERT(y*m_size + x < m_size*m_size); return y*m_size + x; }
705 werner 56
    /// retrieve the value of the stamp at given indices x and y
40 Werner 57
    inline float operator()(const int x, const int y) const { return *data(x,y); }
58 Werner 58
    inline float offsetValue(const int x, const int y, const int offset) const { return *data(x+offset, y+offset); }
47 Werner 59
    const Stamp *reader() const { return m_reader; }
149 werner 60
    void setReader(Stamp *reader) { m_reader = reader; setCrownRadius(reader->crownRadius()); /*calculates also the Area*/ }
400 werner 61
 
149 werner 62
    // property crown radius
63
    float crownRadius() const { return m_crownRadius; }
64
    float crownArea() const { return m_crownArea; }
65
    void setCrownRadius(const float r) { m_crownRadius = r; m_crownArea=r*r*M_PI; }
487 werner 66
    float distanceToCenter(const int ix, const int iy) const { return mDistanceGrid->constValueAtIndex(abs(ix-m_offset), abs(iy-m_offset)); }
32 Werner 67
    // loading/saving
68
    void loadFromFile(const QString &fileName);
33 Werner 69
    void load(QDataStream &in); ///< load from stream (predefined binary structure)
70
    void save(QDataStream &out); ///< save to stream (predefined binary structure)
321 werner 71
    QString dump() const;
30 Werner 72
private:
32 Werner 73
    void setup(const int size);
30 Werner 74
    float *m_data;
149 werner 75
    float m_crownRadius;
76
    float m_crownArea;
32 Werner 77
    int m_size;
33 Werner 78
    int m_offset;
47 Werner 79
    Stamp *m_reader; ///< pointer to the appropriate reader stamp (if available)
487 werner 80
    static const Grid<float> *mDistanceGrid;
30 Werner 81
};
82
 
34 Werner 83
// global functions
84
 
85
/// create a stamp from a FloatGrid with any size
86
/// @param grid source grid. It is assumed the actual stamp data is around the center point and the grid has an uneven size (e.g 13x13 or 25x25)
87
/// @param width number of pixels that should actually be used. e.g: grid 25x25, width=7 -> data is located from 9/9 to 16/16 (12+-3)
88
/// @return a stamp created on the heap with the fitting size. The data rect is aligned to 0/0. above example: stamp will be 8x8, with a 7x7-data-block from 0/0 to 6/6.
89
Stamp *stampFromGrid(const FloatGrid& grid, const int width);
90
 
30 Werner 91
#endif // STAMP_H