-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvxFollower.cpp
131 lines (103 loc) · 2.48 KB
/
vxFollower.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
*
* file vxFollower.cpp
*
* This source file is a part of VoxelBrain software.
*
* (c) Nanyang Technological University
*
* Author: Konstantin Levinski
*
*/
#include <vector>
#include <stdio.h>
#include <string.h>
#include "vxFollower.h"
#include "vxVector.h"
using namespace std;
/* Hashers */
void Followable::Invalidate(){
hash++;
};
/**
Base class, allows operate on followers uniformly;
*/
struct BaseFollower {
virtual bool Changed() = 0;
virtual void Reset() = 0;
FollowingAction * action;
};
/**
Basic follower is a template for a usable follower,
parameter is the type being followed;
Watch() to set the focus.
*/
template<class T>
struct BasicFollower: BaseFollower {
unsigned int hash;
T * data;
virtual bool Changed(){
return hash != GetHash(data);};
virtual void Reset(){
hash = GetHash(data);
};
virtual BaseFollower * Watch(T * in){
data = in;
Reset(); hash++; //Make sure it is seen as changed.
return this;
};
};
/// Hashers
unsigned int GetHash(int * in){
unsigned int res;
memcpy(&res, in, sizeof(int));
return res;
};
unsigned int GetHash(float * in){
unsigned int res;
memcpy(&res, in, sizeof(int));
return res;
};
unsigned int GetHash(V3f * in){
float a = in->x+in->y*1.23+in->z*0.45; //Random multiplication
return GetHash(&a);
};
unsigned int GetHash(Followable * in){
return in->hash;
};
///Storage
struct FollwerRecordList{
typedef vector<BaseFollower *> list_t;
list_t r;
void Reset(){
for(list_t::iterator i = r.begin(); i != r.end(); i++)delete (*i);
r.clear();
};
void Kick(){
for(list_t::iterator i = r.begin(); i != r.end(); i++){
if((*i)->Changed()){
(*i)->action->Do();
(*i)->Reset();
};
};
};
~FollwerRecordList(){Reset();};
} __follower_record__;
///Watcher constructors
template<class T>
void WatchTemplate(T * in, FollowingAction * act){
BasicFollower<T> * res = new BasicFollower<T>();
res->Watch(in); res->action = act;
__follower_record__.r.push_back(res);
};
void Watch(int * in, FollowingAction * act){ WatchTemplate<int>(in, act); };
void Watch(float * in, FollowingAction * act){ WatchTemplate<float>(in, act); };
void Watch(V3f * in, FollowingAction * act){ WatchTemplate<V3f>(in, act); };
void Watch(Followable * in, FollowingAction * act){ WatchTemplate<Followable>(in, act); };
void KickFollowers(){
__follower_record__.Kick();
};
void ResetFollowers(){
__follower_record__.Reset();
};
// End of vxFollower.cpp