FFSM++  1.1.0
French Forest Sector Model ++
ModelRegion.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2015 by Laboratoire d'Economie Forestière *
3  * http://ffsm-project.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version, given the compliance with the *
9  * exceptions listed in the file COPYING that is distribued together *
10  * with this file. *
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 *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 
23 #include "ThreadManager.h"
24 #include "ModelRegion.h"
25 #include "ModelData.h"
26 #include "Pixel.h"
27 #include "Gis.h"
28 
29 
30 /**
31 The constructor of REGION instances want:
32 @param MTHREAD_h Pointer to the main thread manager
33 */
34 ModelRegion::ModelRegion(ThreadManager* MTHREAD_h, int regId_h, string regSName_h, string regLName_h, int regLevel_h, int parRegId_h, bool isResidual_h){
35  MTHREAD=MTHREAD_h;
36  regId = regId_h;
37  regSName = regSName_h;
38  regLName = regLName_h;
39  regLevel = regLevel_h;
40  parRegId = parRegId_h;
41  isResidual = isResidual_h;
42 
43  // Create an empty vector of inventory bounds for each possible primary products combination
44  int nInBounds = pow(2,MTHREAD->MD->getStringVectorSetting("priProducts").size());
45  //int nInBounds = MTHREAD->MD->getStringVectorSetting("priProducts").size(); // TODO todo !Important
46  vector <double> inBounds(nInBounds,0.); // should have ceated a vector of size priProducts.size(), all filled with zeros
47  inResByAnyCombination = inBounds;
48  //inResByAnyCombination_deathTimber = inBounds;
49 }
50 
52 }
53 
54 vector<ModelRegion*>
55 ModelRegion::getChildren(bool excludeResidual){
56  if(excludeResidual){
57  vector<ModelRegion*> toReturn;
58  for(uint i=0;i<chRegions.size();i++){
59  if(!chRegions[i]->getIsResidual()){
60  toReturn.push_back(chRegions[i]);
61  }
62  }
63  return toReturn;
64  }
65  return chRegions;
66 }
67 
68 int
69 ModelRegion::getNChildren(bool excludeResidual){
70  /*if(excludeResidual){
71  int toReturn = 0; // Bug found 20180927
72  for(uint i=0;i<chRegions.size();i++){
73  if(!chRegions[i]->getIsResidual()){
74  toReturn++;
75  }
76  }
77  return toReturn;
78  }
79  return chRegions.size();
80  */
81  return getChildren(excludeResidual).size();
82 }
83 
84 vector<ModelRegion*>
85 ModelRegion::getSiblings(bool excludeResidual){
86  vector<ModelRegion*> toReturn;
87  vector<ModelRegion*> allRegions = MTHREAD->MD->getAllRegions(excludeResidual);
88  for (uint i=0;i<allRegions.size();i++){
89  if (allRegions[i]->parRegId == parRegId && allRegions[i]->getRegId() != regId){
90  toReturn.push_back(allRegions[i]);
91  }
92  }
93  return toReturn;
94 }
95 
96 
97 
98 
99 
100 
101 
102 
103 double
105  /// \todo Implement me (but really needed?)
106  return 0;
107 }
108 
109 vector<double>
111  /// \todo Implement me (but really needed?)
112  vector<double> toReturn;
113  return toReturn;
114 }
115 
116 vector < vector <double> >
117 ModelRegion::getVolumes(int fType_h, string dClass_h){
118  /// \todo Implement me (but really needed?)
119  vector < vector <double> > toReturn;
120  return toReturn;
121 }
122 
123 
124 double
125 ModelRegion::getArea(const string &fType_h, const string &dClass_h){
126  vector <string> dClasses = MTHREAD->MD->getStringVectorSetting("dClasses");
127  vector <string> fTypes= MTHREAD->MD->getForTypeIds();
128  int ft_pos = -1000;
129  int dc_pos = -1000;
130  for(uint j=0;j<fTypes.size();j++){
131  if (fTypes[j] == fType_h){
132  ft_pos = j;
133  break;
134  }
135  }
136  for(uint u=0;u<dClasses.size();u++){
137  if (dClasses[u] == dClass_h){
138  dc_pos = u;
139  break;
140  }
141  }
142  if(ft_pos<0) msgOut(MSG_CRITICAL_ERROR,"Forest type "+fType_h+" not found in getArea() function.");
143  if(dc_pos<0) msgOut(MSG_CRITICAL_ERROR,"Diameter class"+dClass_h+" not found in getArea() function.");
144 
145  return getArea(ft_pos, dc_pos);
146 }
147 
148 double
149 ModelRegion::getArea(const string &fType_h){
150  vector <string> fTypes= MTHREAD->MD->getForTypeIds();
151  int ft_pos = -1000;
152  for(uint j=0;j<fTypes.size();j++){
153  if (fTypes[j] == fType_h){
154  ft_pos = j;
155  break;
156  }
157  }
158  if(ft_pos<0) msgOut(MSG_CRITICAL_ERROR,"Forest type "+fType_h+" not found in getArea() function.");
159  return getArea(ft_pos);
160 }
161 
162 double
163 ModelRegion::getArea(const int& ft_pos, const int& dc_pos){
164  double totalarea = 0.0;
165  for(uint i=0;i<myPixels.size(); i++){
166  totalarea += myPixels[i]->area.at(ft_pos).at(dc_pos);
167  }
168  return totalarea;
169 }
170 
171 double
172 ModelRegion::getArea(const int& ft_pos){
173  double totalarea = 0.0;
174  for(uint i=0;i<myPixels.size(); i++){
175  totalarea += vSum(myPixels[i]->area.at(ft_pos));
176  }
177  return totalarea;
178 }
179 
180 double
182  vector<Pixel*> regPx = this->getMyPixels();
183  double totalarea = 0.0;
184  for(uint i=0;i<myPixels.size(); i++){
185  totalarea += vSum(myPixels[i]->area);
186  }
187  return totalarea;
188 }
189 
190 double
191 ModelRegion::getValue(string layerName, int op){
192  int nPx = myPixels.size();
193  double sumvalue=0;
194  for(uint i=0;i<nPx; i++){
195  sumvalue += myPixels[i]->getDoubleValue(layerName,true);
196  }
197  if(op==OP_SUM){
198  return sumvalue;
199  } else if (op == OP_AVG) {
200  return sumvalue/nPx;
201  } else {
202  string thisf = __PRETTY_FUNCTION__;
203  msgOut(MSG_CRITICAL_ERROR, "in "+thisf+", operation not supported");
204  }
205  return 0.;
206 }
207 
208 /***
209  * It establishs a link between pixels and regions.
210  *
211  * Pixels are stored in myPixels vectors and, only if this is a level2 region, a pointer to this region is passed to the pixel
212  *
213  * */
214 void
216  int xyNPixels = MTHREAD->GIS->getXyNPixels();
217  for(uint i=0;i<xyNPixels;i++){
218  Pixel* px = MTHREAD->GIS->getPixel(i);
219  if(px->getDoubleValue("regLev_1")==regId || px->getDoubleValue("regLev_2")==regId){
220  myPixels.push_back(px);
221  if(regLevel == 2){
222  px->setMyRegion(this);
223  }
224  }
225  }
226 }
227 
228 void
229 ModelRegion::swap(const int& swap_what){
230 
231  for(uint i=0;i<myPixels.size();i++) {
232  myPixels[i]->swap(swap_what);
233  }
234 
235 }
236 
237 
238 
void setMyPixels()
It sets a double link pixels <–> region.
int parRegId
Id of the parent region;.
Definition: ModelRegion.h:96
void setMyRegion(ModelRegion *region_h)
Definition: Pixel.h:90
int regLevel
The level of the region. 1: country, 2: regions.
Definition: ModelRegion.h:95
int getRegId() const
Definition: ModelRegion.h:66
ThreadManager * MTHREAD
Pointer to the Thread manager.
Definition: BaseClass.h:467
double getValue(string layerName, int op=OP_SUM)
return the values of its own pixels for the specified layer. Possible operations: OP_SUM or OP_AVG ...
ModelData * MD
the model data object
Definition: ThreadManager.h:72
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
Definition: BaseClass.cpp:50
double getXyNPixels() const
Return the number of pixels on Y.
Definition: Gis.h:131
Gis * GIS
GIS information and methods.
Definition: ThreadManager.h:73
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Definition: ThreadManager.h:65
vector< string > getStringVectorSetting(const string &name_h, int reg=WORLD) const
Definition: ModelData.cpp:1129
Perform a SUM operation.
Definition: BaseClass.h:77
vector< ModelRegion * > chRegions
Vector of level-1 children regions.
Definition: ModelRegion.h:99
vector< double > inResByAnyCombination
Vector of inventory resource for each possible combination of primary products. This store both alive...
Definition: ModelRegion.h:86
Print an error message and stop the model.
Definition: BaseClass.h:62
double getDoubleValue(const string &layerName_h, const bool &returnZeroForNoValue=false) const
Return the value for a specific layer.
Definition: Pixel.cpp:158
int vSum(const vector< int > &vector_h) const
Definition: BaseClass.h:276
bool isResidual
A flag if this region should be explicitelly modelled or it is just a residual.
Definition: ModelRegion.h:97
int regId
Regional unique ID.
Definition: ModelRegion.h:92
Pixel-level class.
Definition: Pixel.h:47
int getNChildren(bool excludeResidual=true)
Definition: ModelRegion.cpp:69
vector< string > getForTypeIds(bool all=false)
By default it doesn&#39;t return forTypes used only as input.
Definition: ModelData.cpp:430
vector< ModelRegion * > getSiblings(bool excludeResidual=true)
Return a vector of pointers to the siblings regions.
Definition: ModelRegion.cpp:85
Pixel * getPixel(int x_h, int y_h)
Definition: Gis.h:134
vector< Pixel * > myPixels
Vector of pixels for this region.
Definition: ModelRegion.h:102
double getVolumes()
bool getIsResidual() const
Definition: ModelRegion.h:71
vector< ModelRegion * > getChildren(bool excludeResidual=true)
Returns a pointer to the parent regions.
Definition: ModelRegion.cpp:55
Perform an AVERAGE operation.
Definition: BaseClass.h:78
vector< Pixel * > getMyPixels()
Definition: ModelRegion.h:86
ModelRegion(ThreadManager *MTHREAD_h, int regId_h, string regSName_h, string regLName_h, int regLevel_h, int parRegId_h, bool isResidual_h)
Constructor.
Definition: ModelRegion.cpp:34
string regLName
Region long name;.
Definition: ModelRegion.h:94
void swap(const int &swap_what)
vector< ModelRegion * > getAllRegions(bool excludeResidual=true)
Definition: ModelData.cpp:379
string regSName
A short name of the region.
Definition: ModelRegion.h:93
double getArea()
Get whole forest area (from pixel->area matrix)