Subversion Repositories public iLand

Rev

Rev 639 | Rev 697 | 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"
30 Werner 26
/** Stamp is the basic class for the FON field of a individual tree.
27
 
28
*/
29
class Stamp
30
{
31
public:
33 Werner 32
    /// @enum StampType defines different grid sizes for stamps (4x4 floats, ... 48x48 floats).
33
    /// the numeric value indicates also the size of the grid.
131 Werner 34
    enum StampType { est4x4=4, est8x8=8, est12x12=12, est16x16=16, est24x24=24, est32x32=32, est48x48=48, est64x64=64 };
30 Werner 35
    Stamp();
36
    ~Stamp();
35 Werner 37
    Stamp(const int size):m_data(NULL) { setup(size); }
34 Werner 38
    void setOffset(const int offset) { m_offset = offset; }
487 werner 39
    static void setDistanceGrid(const Grid<float> *grid) { mDistanceGrid = grid; }
145 Werner 40
    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.
41
    int count() const { return m_size*m_size; } ///< count of pixels (rectangle)
42
    int size() const { return m_offset*2+1; } ///< logical size of the stamp
43
    int dataSize() const { return m_size; } ///< internal size of the stamp; e.g. 4 -> 4x4 stamp with 16 pixels.
30 Werner 44
    /// get a full access pointer to internal data
45
    float *data() { return m_data; }
46
    /// get pointer to the element after the last element (iterator style)
32 Werner 47
    const float *end() const { return &m_data[m_size*m_size]; }
30 Werner 48
    /// get pointer to data item with indices x and y
40 Werner 49
    float *data(const int x, const int y) const { return m_data + index(x,y); }
34 Werner 50
    void setData(const int x, const int y, const float value) { *data(x,y) = value; }
30 Werner 51
    /// get index (e.g. for data()[index]) for indices x and y
32 Werner 52
    int index(const int x, const int y) const { return y*m_size + x; }
40 Werner 53
    inline float operator()(const int x, const int y) const { return *data(x,y); }
58 Werner 54
    inline float offsetValue(const int x, const int y, const int offset) const { return *data(x+offset, y+offset); }
47 Werner 55
    const Stamp *reader() const { return m_reader; }
149 werner 56
    void setReader(Stamp *reader) { m_reader = reader; setCrownRadius(reader->crownRadius()); /*calculates also the Area*/ }
400 werner 57
 
149 werner 58
    // property crown radius
59
    float crownRadius() const { return m_crownRadius; }
60
    float crownArea() const { return m_crownArea; }
61
    void setCrownRadius(const float r) { m_crownRadius = r; m_crownArea=r*r*M_PI; }
487 werner 62
    float distanceToCenter(const int ix, const int iy) const { return mDistanceGrid->constValueAtIndex(abs(ix-m_offset), abs(iy-m_offset)); }
32 Werner 63
    // loading/saving
64
    void loadFromFile(const QString &fileName);
33 Werner 65
    void load(QDataStream &in); ///< load from stream (predefined binary structure)
66
    void save(QDataStream &out); ///< save to stream (predefined binary structure)
321 werner 67
    QString dump() const;
30 Werner 68
private:
32 Werner 69
    void setup(const int size);
30 Werner 70
    float *m_data;
149 werner 71
    float m_crownRadius;
72
    float m_crownArea;
32 Werner 73
    int m_size;
33 Werner 74
    int m_offset;
47 Werner 75
    Stamp *m_reader; ///< pointer to the appropriate reader stamp (if available)
487 werner 76
    static const Grid<float> *mDistanceGrid;
30 Werner 77
};
78
 
34 Werner 79
// global functions
80
 
81
/// create a stamp from a FloatGrid with any size
82
/// @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)
83
/// @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)
84
/// @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.
85
Stamp *stampFromGrid(const FloatGrid& grid, const int width);
86
 
30 Werner 87
#endif // STAMP_H