OSDN Git Service

first
[psychlops/cpp.git] / osx / test / PsychlopsTest.cpp
1 #include <psychlops.h>
2 using namespace Psychlops;
3 ///- Prefix
4
5
6 ///+ Main Routine
7 //// Psychlops runs at the first line of this function psychlops_main().
8 void psychlops_main() {
9     Canvas window(Canvas::window); //Create a window. Here, window variables are preset mode.
10         
11         Image img[100];
12         Clock before, after;
13         before.update();
14         for(int i=0; i<100; i++) {
15                 img[i].set(300,300,Image::RGBA);
16                 img[i].shift(100,100);
17                 img[i].clear(Color(1.0,0.7,0.5, 1.0));
18                 img[i].cache();
19         }
20         img[0].line(0,0,0,79,Color::cyan);
21         img[0].line(0,79,79,79,Color::cyan);
22         img[0].line(79,79,79,0,Color::cyan);
23         img[0].line(79,0,0,0,Color::cyan);
24         img[0].cache();
25         after.update();
26         
27         std::cout << (after - before).at_msec() << " ms " << std::endl;
28         
29         ///+ 2
30         ////  Set a figure size, position and color.
31     Psychlops::Rectangle
32         /*ID:FIGURE selector Rectangle Ellipse*/ figure; //Create a figure (Rectangle or Ellipse) variable.
33     figure.set( 100, 100 ); // Set the size of figure.
34     figure.fill = Psychlops::Color( 1, 1, 1 ); // Set the drawing color of figure in (R, G ,B) format.
35     figure.centering().shift( 0, 0 ); // Move the figure to the starting point.
36     ///- 2
37         
38     int frame=0;
39     int looming_direction=1, motion_dir=1; // Create variables for looming motion.
40         //looming_direction is a variable for size change.
41         //motion_dir is a variable for motion direction.
42         ///+ 3 Drawing loop
43     //// Draw each frames in a "while loop".
44     while(!Keyboard::esc.pushed()) { //exit a program when the escape key is pressed down.
45         window.clear(0); // Clear the window with a designated gray-scale level.
46                 img[0].draw();
47                 
48                 
49                 ///+ 3.1
50         //// Calculate object's position and size for each frame.
51                 
52         if(frame% 30==0){ // a direction of size change will reverse at designated frames.
53             looming_direction=looming_direction*-1;
54         }
55                 
56         if(frame% 90==0){ // motion direction will reverse at designated frames.
57                         motion_dir*=-1;
58         }
59                 
60         ///- 3.1
61                 
62                 ///+ 3.2
63         //// Settting figure's properties
64         figure.resize(figure.getWidth()+looming_direction*1, // Scaling the figure in a direction given by "looming_direction".
65                                           figure.getHeight()+looming_direction*1);// Scaling the figure in a direction given by "looming_direction".
66         figure.shift(motion_dir*1, motion_dir*0);// Move the figure for 1 pixel in direction given by "motion_dir".
67         ///- 3.2
68                 
69                 ///+ 3.3
70         ////Drawing
71         figure.draw(); // Drawing the figure with a designated color at designated position.
72         window.flip(); // Reflect the drawing for the display by flipping frame buffers.
73         ///- 3.3
74                 
75                 ///+ 3.4
76         ////make a step for next frames
77         frame++;
78         ///- 3.4
79     }
80     ///- 3 Drawing loop
81         
82 }
83 ///- Main Routine
84
85
86 /*
87 void func() {
88         Canvas cnvs(700, 700, Canvas::window);
89
90         int wid=200, hei=200, left=0, right=0;
91         bool typ=0;
92
93         Independent << typ   | "typ";
94         Independent << wid   | "wid"   | 10 | 1;
95         Independent << hei   | "hei"   | 10 | 1;
96         Independent << left  | "left"  | 10 | 1;
97         Independent << right | "right" | 10 | 1;
98         
99         //Image source("TocoToucan.png");
100         Image source("test.png"), ref("test.png");
101         source.cache();
102         Rectangle rect(100,100);
103         rect.shift(500,200);
104         
105         source.centering();
106         
107         while(!Input::get(Keyboard::esc)) {
108                 cnvs.clear(Color::gray);
109                 if(typ)
110                         source(rect.set(source.getWidth()-left, source.getHeight()-right)).centering(Drawable::prime->getCenter()).draw();
111                         //ref.centering(Drawable::prime->getCenter()).draw();
112                 else
113                         source.centering(Drawable::prime->getCenter()).draw();
114                 cnvs.flip(10);
115         }
116 }
117
118 void psychlops_main() {
119         Procedure proc;
120         proc.setDesign(Procedure::DEMO);
121         proc.setProcedure(func);
122         proc.run();
123 }                          
124 */
125
126 /*
127 double bellcurve(const double x, const double sigma) {
128         return exp( -(x*x) / (2.0*sigma*sigma) );
129 }
130 double bellcurve_(const double x) {
131         return bellcurve(x,5);
132 }
133
134 AnalogInput *analog;
135 static double hoge = 0;
136 void addToS() {
137         AppState::setThreadPriority(AppState::LOW);
138         while(true) {
139                 hoge = analog->get();
140         }
141 }
142
143 class RectLuminance : public ExperimentalMethods::Demo {
144         Canvas display;
145         double tf;
146         int numstim;
147         int orientation;
148         
149         Image base;
150         Group gp;
151         Prototype::Thread recorder;
152         
153         void initialize() {
154                 display.set(Canvas::window);
155                 //AnalogOutput *analogout = new Devices::AnalogOutput_NIDAQmxBase();
156                 //analogout->put(5);
157                 //delete analogout;
158                 //analog = new Devices::AnalogInput_NIDAQmxBase(1,1,0);
159                 //recorder.create(&addToS);
160                 
161                 Interval itv;
162                 Independent << tf          | "Temporal Frequency"   | -1.0<=itv<=3.0 |  1 | 0.5 , 1.0;
163                 Independent << orientation | "Stimulus Orientation" |  0.0<=itv<=3   |  1 | 1 , 0,1,2,3;
164                 Independent << numstim     | "Stimulus Alignment"   |  0.0<=itv<=2   |  1 | 1 , 0;
165
166                 Color col;
167
168                 base.set(100,100);
169                 for(int x=-50; x<50; x++) for(int y=-50; y<50; y++) base.pix(x+50,y+50,col.set(x*x+y*y>50*50? 0.5 : (y+50)/100.0));
170                 base.quicken();
171                 base.join(gp).centering(gp);
172                 gp.centering();
173         }
174
175         void trial() {
176                 Rectangle mask(102,102);
177                 Font font(L"Meirio", 10);
178                 Letters let(L"");
179                 double phase = 0;
180                 Color col;
181
182                 Figures::FunctionalPlot plot(300,300);
183                 plot.append(&sin);
184                 plot.append(&bellcurve_);
185                 plot.x_min = -10;
186                 plot.x_max =  10;
187                 plot.y_min = -10;
188                 plot.y_max =  10;
189
190                 while(!Input::get(Keyboard::esc)) {
191                         phase = Math::mod(phase+2*PI*pow(2,tf)/display.getRefreshRate(), 2*PI);
192                         
193                         display.clear(Color::gray);
194                         
195                         if(numstim>0) {
196                                 for(int x=-1; x<=1; x++ ) {
197                                         for(int y=-1; y<=1; y++ ) {
198                                                 gp.rotation = orientation*90 +180*(cos(phase)<0 ? 1 : 0);
199                                                 gp.centering().shift(x*102, y*102).draw();
200                                                 mask.centering(gp);
201                                                 mask.draw(col.set(.5,.5,.5,sin(phase)*(sin(phase)<0 ? -1 : 1)));
202                                         }
203                                 }
204                         }
205
206                         gp.rotation = orientation*90 +180*(cos(phase)<0 ? 1 : 0) + 180*(numstim==2 ? 1 : 0);
207                         gp.centering().draw();
208                         
209                         mask.centering(gp);
210                         mask.draw(col.set(.5,.5,.5,sin(phase)*(sin(phase)<0 ? -1 : 1)));
211                         if(Mouse::right.pressed()) display.var(Mouse::getWheelDelta().x, 300,300);
212                         display.var(Mouse::getWheelDelta().y, 300,320);
213                         let.centering();
214                         let.draw(Color::white);
215                         display.msg(L"fd", 500,500);
216
217                         plot.draw();
218                         
219                         display.var(hoge, 400, 400);
220                         display.flip();
221                 }
222         }
223         
224 };
225
226 void psychlops_main() {
227         RectLuminance * exp = new RectLuminance;
228         exp->run();
229         delete exp;
230 }
231
232 */
233
234 /*
235 Letters let;
236
237 void psychlops_main() {
238         Canvas c(300,300,Canvas::window);
239
240         Rectangle a(100,100), b(100,100);
241         a.stroke = Stroke(Color::blue, 5, Stroke::DASHED);
242         b.shift(100,109).fill = Color::black;
243         Rectangle cr(10,10);
244         cr.fill = Color::red;
245         Figures::Arrow arrow(0,0,100,50);
246         arrow.stroke = Stroke(Color::green, 5, Stroke::SOLID);
247         //arrow.fill = Color::green;
248
249
250         Group g;
251         g.append(a);
252         b.join(g);
253         cr.join(g);
254         arrow.join(g);
255         g.centering();
256
257         Rectangle clipper(200,200);
258         clipper.centering();
259
260         let = L"日本語";
261         let.cache();
262         let.centering();
263
264         //g.clip(clipper);
265
266         while(!Keyboard::esc.pushed()) {
267                 c.clear(Color::gray);
268
269                 g.rotation+=1;
270                 g.draw();
271                 let.draw(Color::red);
272
273                 c.flip();
274         }
275 }
276 */
277
278
279 /*
280
281 class RectLuminance : public ExperimentalMethods::Demo {
282         Canvas display;
283         double bg_lum, x;
284 //      Widgets::SequentialScreenshot sc;
285         Gabor *gabor;
286         Rectangle rect;
287         FigureGroup g;
288         Figures::Cross xr;
289         Figures::Arrow arr;
290
291         void initialize() {
292                 display.set(640,400, Canvas::window);
293                 gabor = new QuickGabor(10.0, 10.0, 1.0);
294                 g.add(gabor);
295                 g.add(rect);
296                 g.add(xr);
297                 rect.set(100, 100);
298                 xr.set(100,100).shift(200,200);
299                 xr.brush = Stroke(Color::red, 2, Stroke::SOLID);
300                 g.shift(100,0);
301                 g.rotation = -5;
302                 g.axis = Psychlops::Point(1,1,1);
303                 arr.centering();
304                 arr.vec = Psychlops::Point(100,50);
305
306                 Interval itv;
307                 Independent << bg_lum    | "BG Luminance"   | 0.0<=itv<=4095 |  128 | 1 , 1;
308                 Independent << x         | "X"              | 0.0<=itv<=4095 |  128 | 1 , 1;
309
310                 display.watchFPS();
311                 display.showFPS();
312         }
313
314         void trial() {
315                 Letters let(L"日本語");
316                 let.color = Color::green;
317                 while(!Input::get(Keyboard::esc)) {
318                         display.clear(Color(bg_lum/4095));
319                         (*gabor).centering().draw();
320                         let.centering();
321                         let.draw();
322                         g.draw();
323                         arr.vec.x -= 1;
324                         arr.draw(Color::white);
325                         display.flip();
326                 }
327         }
328         
329 };
330
331 void psychlops_main() {
332         RectLuminance * exp = new RectLuminance;
333         exp->run();
334         delete exp;
335 }
336
337 */
338
339 /*
340  void psychlops_main() {
341         Color col, col2;
342
343         Image pic;
344         Image pic2(pic.getWidth(), pic.getHeight());
345         pic.load("pic1.png");
346
347         display.clear(Color::white);
348         display.flip();
349
350
351         for(int x=0; x<pic.getWidth(); x++) {
352                 display.progressbar(x, pic.getWidth());
353                 for(int y=0; y<pic.getHeight(); y++) {
354 //                      col = pic.getPix(x,y);
355 //                      col2.set(1-col.getR(), 1-col.getG(), 1-col.getB());
356 //                      pic2.pix(x,y,col2);
357                 }
358         }
359 */
360 /*
361         pic.centering();
362         pic2.centering();
363         Display::clear(Color::white);
364
365         while(!Input::get(Keyboard::esc)) {
366                 pic.draw();
367                 Display::flip();
368                 pic2.draw();
369                 Display::flip();
370         }
371 }
372 */
373
374
375
376
377 /*
378 class Test : public ExperimentalMethods::Demo {
379         
380 public:
381         Canvas d;
382         double l,r,g;
383
384         void initialize() {
385                 d.set(Canvas::window);
386                 l=r=g=0;
387                 Range rng;
388                 Independent << r | 0<=rng<=1 | 1.0/255 | 16.0/255;
389                 Independent << g | 0<=rng<=1 | 1.0/255 | 16.0/255;
390                 Independent << l | 0<=rng<=1023 | 1 | 64;
391                 Color::setGammaValue(6.75,1.0,1.0,Color::BITS_MONO);
392         }
393         void terminate() {
394         }
395         void trial() {
396                 Rectangle rect(100,100), rect1(100,100), rect2(100,100);
397                 rect.centering();
398                 rect1.centering().shift(-100,0);
399                 rect2.centering().shift(-50,-100);
400                 Color col, ref1, ref2, bg;
401                 while(!Input::get(Keyboard::esc)) {
402                         bg.set(l/1023);
403                         d.clear(bg);
404                         col.set(r,g,0);
405                         rect.draw(col);
406                         ref1.set(127/255.0,0,0);
407                         rect1.draw(ref1);
408                         ref2.set(127/255.0,127/255.0,0);
409                         rect2.draw(ref2);
410                         d.flip();
411                 }
412         }
413         
414 };
415
416 void psychlops_main() {
417         Test *exp = new Test;
418         exp->run();
419         delete exp;
420 }
421 */