summaryrefslogtreecommitdiff
path: root/NT/src/rotor.h
blob: 2ddf3ee76faf2120b128b55795650fb5ec390541 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#ifndef ROTOR_H
#define ROTOR_H

/*
ROTORD rewrite

TJR-Jan-2014

NB when plugging in to framework - convert all cerr messages to logged

Test suite

pipe to send commands?

seperate framework? ie in Python- load server- send http commands
*/

#define ENABLE_TYPENAME(A) template<> struct TypeName<A> { static const char *Get() { return #A; }};

#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>
#include <unordered_map>
#include <json/json.h>
#include <typeinfo>

#include "xmlIO.h"
#include "libavwrapper.h"
#include "cvimage.h"

#include "Poco/Logger.h"
#include "Poco/Channel.h"
#include "Poco/SplitterChannel.h"
#include "Poco/ConsoleChannel.h"
#include "Poco/FormattingChannel.h"
#include "Poco/FileChannel.h"
#include "Poco/Message.h"
#include "Poco/Formatter.h"
#include "Poco/PatternFormatter.h"
#include "Poco/AutoPtr.h"
#include "Poco/Net/HTTPResponse.h"

using Poco::Logger;
using Poco::Channel;
using Poco::SplitterChannel;
using Poco::ConsoleChannel;
using Poco::FormattingChannel;
using Poco::Formatter;
using Poco::PatternFormatter;
using Poco::FileChannel;
using Poco::Message;
using Poco::AutoPtr;

template <typename T>
struct TypeName
{
    static const char* Get()
    {
        return typeid(T).name();
    }
};

template <>
struct TypeName<int> {
	static const char* Get() {
		return "int";
	}
};
template <>
struct TypeName<double> {
	static const char* Get() {
		return "number";
	}
};
template <>
struct TypeName<std::string> {
	static const char* Get() {
		return "string";
	}
};





namespace Rotor {

	class Node;
	template <class NT> class Node_type;

	class Enum{
		//enumerated string type for menus
		public:
			Enum(std::initializer_list<std::string> init={},int def=0) : labels(init), value(def){};
			int get_value(){return value;};
			operator int () const { //overload C style cast to int
				return value;
			}
			//only works inside class definition?
			friend std::istream &operator>> (std::istream &in, Rotor::Enum &en) {
			    in >> en.value;
			    return in;
			};
		protected:
			int value;
		private:
    		std::vector<std::string> labels;
	};
		
	class Audio_frame{
		public:
			Audio_frame(uint16_t *_samples,int _channels,int _numsamples){
				samples=_samples;
				channels=_channels;
				numsamples=_numsamples;
			}
			uint16_t *samples;
			int channels,numsamples;
	};
	class Frame_parameters{
		//chosen to be used as a struct: less overhead etc
		public:
			Frame_parameters(double _time=0.0,double _framerate=25.0,double _duration=20.0,int _w=640,int _h=360,Audio_frame *_audio=nullptr)
			{ time=_time; framerate=_framerate; duration=_duration; w=_w; h=_h;audio=_audio;};
			Frame_parameters(int _frame,double _framerate=25.0,double _duration=20.0,int _w=640,int _h=360,Audio_frame *_audio=nullptr)
			{ time=((double)_frame)/_framerate; framerate=_framerate; duration=_duration; w=_w; h=_h;audio=_audio;};
			int h,w;
			Frame_parameters lastframe() const{
				return Frame_parameters(time-(1.0/framerate),framerate,duration,w,h);
			}
			Frame_parameters nextframe() const{
				return Frame_parameters(time+(1.0/framerate),framerate,duration,w,h);
			}
			double time; //num/denom ?
			double framerate;
			double duration;
		private:
			Audio_frame *audio;
	};
	class Variable { //pure virtual base type for variable pointers
		public:
			Variable(std::string _name="",std::string _description="",std::string _title=""):name(_name),description(_description),title(_title),connection(nullptr){};
			virtual ~Variable(){};
			virtual Json::Value to_json()=0;
			virtual void init(Json::Value s)=0;
			virtual bool connect(Node* target)=0;
	    	virtual std::string get_type()=0;
	    	virtual bool create_connection(std::unordered_map<std::string,Node*> &nodes)=0;
			bool is_connected(){
				if (connection) return true;
				return false;
			}
			std::string get_connection_id();
			std::string get_name();
		protected:
			std::string name,description,title;
			Node* connection;
			bool connectable;
			std::string input;
	};
	class Node { //base type for node pointers
		public:
			Node(){type="";type_id="";id="";description="";};
			virtual ~Node(){
				for (auto v:vars){
					delete v.second;
				}
			}
			bool connect(std::string v,Node *t){
				auto var=vars.find(v);
				if (var!=vars.end()){
					if ((*var).second->connect(t)){
						return true;
					}
				}
				return false;
			}
			std::string get_type(){return type;};
			std::string& get_id(){return id;};
			std::string& get_description(){return description;};
			Json::Value to_json();
			virtual Node* clone(Json::Value &_settings)=0;
			virtual std::string get_output_type()=0;
			void create_connections(std::unordered_map<std::string,Node*> &nodes){
				Logger& logger = Logger::get(log_id);
				for (auto var:vars) {
					if (var.second->create_connection(nodes)) {
						logger.information("Connected input '"+var.second->get_name()+"' of node '"+id+"' to node "+var.second->get_connection_id());
					}
				}
			}
		protected:
			std::unordered_map<std::string,Variable*> vars;
			std::string type;
			std::string id;
			std::string type_id;
			std::string description;
			std::string title;
			std::string ui_type;
			std::string log_id;
	};
	template <class T> class Variable_type : public Variable {
		public:
			Variable_type(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true): Variable(_name,_description,_title){
				connectable=_connectable;
			};
			void init(Json::Value s){
				std::istringstream cur(s["value"].asString());
				cur >> value;
				name=s["name"].asString();
				input=s["input"].asString();
			}
			void set(const T& val){
				value=val;
			}
			bool create_connection(std::unordered_map<std::string,Node*> &nodes){
				for (auto node:nodes){
					if (node.first==input){
						if (connect(node.second)) return true;
					}
				}
				return false;
			}
			Json::Value to_json(){
				Json::Value json;
				json["type"]=get_type();
				json["name"]=name;
				json["connectable"]=connectable?"yes":"no";
				json["input"]=connection?connection->get_id():"";
				return json;
			}
    		std::string get_type(){ //need this to output node templates
    	    	return TypeName<T>::Get();
    		}
//have to cast connect and get_output to use templated return types
	    	bool connect(Node* target){
				if (connectable){
					if (dynamic_cast<Node_type<T>*>(target)){
						connection=target;
						return true;
					}
				}
				return false;
			}
			const T& get(const Frame_parameters &frame=Frame_parameters()){
				//if (connection) std::cerr<<"still connected to '"<<(dynamic_cast<Node_type<T>*>(connection))->get_type()<<"'"<<std::endl;
				//else std::cerr<<"connection disappeared"<<std::endl;
				if (connection){
					//std::cerr<<"retreiving value from '"<<(dynamic_cast<Node_type<T>*>(connection))->get_type()<<"' node ("<<get_type()<<"):"<<(dynamic_cast<Node_type<T>*>(connection))->get_output(frame)<<std::endl;
					value=(dynamic_cast<Node_type<T>*>(connection))->get_output(frame);
				}
				//else std::cerr<<"variable: returning default"<<std::endl;
				return value;
			}
		protected:
			T value;
	};
	class Variable_array: public Variable {
		//base type for a variable amount of inlets
		public:
			Variable_array(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true): Variable(_name,_description,_title){
				connectable=_connectable;
			};
		protected:
			std::vector<Variable> values;
	};
	template <class T> class Variable_array_type: public Variable_array {
	//is it possible to overload array access operator here?
		public:
			Variable_array_type(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true): Variable_array(_name,_description,_title,connectable){};
			void init(Json::Value s){
				name=s["name"].asString();
				if (!s["input"].empty()){
					for (int i;i<s["input"].size();i++){
						add(s["input"][i]);
						values[i].init(s["input"][i]);
					}
				}
			}
			bool create_connection(std::unordered_map<std::string,Node*> &nodes){
				bool success=false;
				//for (auto v:values){ //weirdly does not work even though it seems to! maybe it returns a copy of of the object?
				//	v.create_connection(nodes); 
				//}
				for (uint32_t i=0;i<values.size();i++) {
					if (values[i].create_connection(nodes)) {
						success=true;
					}
				}
				return success;
			}
			Json::Value to_json();
    		std::string get_type(){
    	    	return TypeName<T>::Get();
    		}
	    	bool connect(Node* target){
				//array does not connect this way- just connect 1st inlet?
				return false;
			}
			void add(Json::Value s){
				values.push_back(Variable_type<T>(s.get("name","").asString(),s.get("description","").asString(),s.get("title","").asString(),s.get("connectable",false).asBool()));
			}
			void add(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true){
				values.push_back(Variable_type<T>(_name,_description,_title,_connectable));
			}
	    	void add(int num,std::string _name="",std::string _description="",std::string _title="",bool _connectable=true){
				for (int i=0;i<num;i++) values.push_back(Variable_type<T>(_name,_description,_title,_connectable));
			}
	    	bool connect(uint32_t which,Node* target){
				if (values.size()>which){
					return values[which].connect(target);
				}
				return false;
			}
			const T& get(uint32_t which,const Frame_parameters &frame){
				if (values.size()>which){
					//std::cerr<<"array: requested value "<<which<<":"<<values[which].get(frame)<<std::endl;
					return values[which].get(frame);
				}
				//else std::cerr<<"array: returning default"<<std::endl;
				return value;
			}
			const std::vector<Variable_type<T>>& get_values(){return values;};
			uint32_t get_number(){return values.size();};
		protected:
			std::vector<Variable_type<T>> values;
			T value;
	};
	
	template <class NT> class Node_type : public Node {
		public:
			virtual const NT& get_output(const Frame_parameters &frame){return result;};
			void init(Json::Value settings){
				if (!settings["vars"].empty()){
					for ( uint32_t i = 0; i < settings["vars"].size(); ++i ) {
						if (!settings["id"].empty()) settings["vars"][i]["log_id"]=settings["log_id"].asString();
						vars[settings["vars"][i]["name"].asString()]->init(settings["vars"][i]);
					}
				}
				if (!settings["id"].empty()) id=settings["id"].asString();
				if (!settings["log_id"].empty()) log_id=settings["log_id"].asString();
			}
			std::string get_output_type(){return TypeName<NT>::Get();};
			template <class IT> Variable_type<IT>* create_inlet(std::string name="",std::string description="",std::string title="",IT _default=IT()){
				vars[name]=new Variable_type<IT>(name,description,title,true);
				dynamic_cast<Variable_type<IT>*>(vars[name])->set(_default);
				return (dynamic_cast<Variable_type<IT>*>(vars[name]));
			}
			template <class IT> Variable_type<IT>* create_attribute(std::string name="",std::string description="",std::string title="",IT _default=IT()){
				vars[name]=new Variable_type<IT>(name,description,title,false);
				dynamic_cast<Variable_type<IT>*>(vars[name])->set(_default);
				return (dynamic_cast<Variable_type<IT>*>(vars[name]));
			}
			template <class IT> Variable_array_type<IT>* create_array(std::string name="",std::string description="",std::string title="",IT _default=IT()){
				vars[name]=new Variable_array_type<IT>(name,description,title);
				return (dynamic_cast<Variable_array_type<IT>*>(vars[name]));
			}
		protected:
			NT result; //internal value is returned as a reference
	};
}

#endif //ROTOR_H