-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharv_fct.cpp
83 lines (62 loc) · 2.82 KB
/
harv_fct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//rand_harvest_byindiv(BAToflargestclass, BAtotalharvested);
rand_harvest_byindiv(double BAToflargestclass, double BAtotalharvested) {
int i, index, randindex, sizeclass, count, trees2protect;
double BAToflargestclass(0), BAtotalharvested(0);
bool test;
PlotTree* ptr = NULL; PlotTree* nbor = NULL;
while( basalarea_tracker[sizeclasses.size() -1] < BAToflargestclass
&& BAtotalharvested < maxtotalharvested)
{
//PT_hashtable contains pointers to trees in treemap and
//pointers to each tree's immediate neighbor.
//get random number between 0 and number of trees in hashtable
randindex = ( rand() % PT_hashtable.size() );
//Get pointer to a random tree
ptr = PT_hashtable[randindex]->getPTptr();
sizeclass = ptr->get_stemclass();
//test whether to cut tree pointed to by ptr
//if cutting won't overshoot harvest target, run test
if( (tree_basalarea(ptr)/10000 + basalarea_tracker[sizeclass -1])
< basalareatargets[sizeclass -1]
&& (tree_basalarea(ptr)/10000 + BAtotalharvested) < maxtotalharvested)
test = cuttarget(ptr);
//If tree is cut, add its basal area to appropriate target
//retrieve neighbor of tree pointed to by ptr and
//point the PT pointer, nbor, to neighbor of ptr
if(test == true) {
//add basal area of ptr in meters square
//tree basal area is per square meter, but group basal area is per hectares
//divide by tree BA by 10000 to convert to BA per hectare
basalarea_tracker[sizeclass -1] += tree_basalarea(ptr)/10000;
BAtotalharvested += tree_basalarea(ptr)/10000;
//continue cutting neighbors if harvest target not met
//find neighbor of current ptr
nbor = PT_hashtable[randindex]->getLink()->getPTptr();
sizeclass = nbor->get_stemclass();
//test whether to cut nbor
if( (tree_basalarea(nbor)/10000 + basalarea_tracker[sizeclass -1])
< basalareatargets[sizeclass -1]
&& (tree_basalarea(ptr)/10000 + BAtotalharvested) < maxtotalharvested)
test = cuttarget(nbor);
if(test == true) {
//if nbor is cut, find and test neighbor of nbor, etc
//index is used to prevent infinite looping
//two trees have been examined so set index to 2
index = 2;
while(test == true
&& index < PT_hashtable.size()
&& BAtotalharvested < maxtotalharvested) {
//add basal area of current nbor to sum for class
basalarea_tracker[sizeclass -1] += tree_basalarea(nbor)/10000;
BAtotalharvested += tree_basalarea(ptr)/10000;
//test whether to cut next nbor
//test_nextnbor() redirects the pointer, nbor, to its immediate neighbor
test = test_nextnbor(nbor);
//get sizeclass of current nbor
sizeclass = nbor->get_stemclass();
index++;
}
}
}
} // end while
}