OSDN Git Service

5
[psychlops/silverlight.git] / test5 / PsychlopsMain.cs
1 //Psychlops Code Template\r
2 //    Please visit following web site to get sample codes.\r
3 //    http://psychlops.sourceforge.jp/ja/?StartCode\r
4 //    CodeDresser at following address is also available to view the code.\r
5 //    http://visitope.org/Tools/codedresser.html\r
6 \r
7 ///+ 0 Setup Psychlops Circumstances\r
8 //// 0 Setup Psychlops Circumstances\r
9 using Psychlops;\r
10 \r
11 namespace PsychlopsSilverlightApp\r
12 {\r
13 \r
14         public class PsychlopsMain\r
15         {\r
16                 // Psychlops Win32 1.5.5 / 20110927\r
17                 ///- 0 Setup Psychlops Circumstances\r
18 \r
19                 Psychlops.Widgets.Slider SOAFrames, gap, wavelength, contrast;\r
20 \r
21                 public void psychlops_main() {\r
22 \r
23                         ///+ 1 Declaration /////////////////////////////////////////////////////////////\r
24                         //// 1 Declaration\r
25                         // declare default window and variables for its parameters\r
26                         Canvas cnvs = new Canvas(900,600, Canvas.window, Display.primary);\r
27                         double CANVAS_FRAMENUM;\r
28                         int CANVAS_REFRESHRATE;\r
29                         Color DEFAULT_BG_COLOR = new Color();\r
30 \r
31                         int gratingwidth = 500, gratingheight = 100;\r
32                         double shift=0.0;\r
33                         Color color = new Color(0.0,0.0,0.0);\r
34                         Rectangle grating = new Rectangle();\r
35 \r
36 \r
37                         Interval rng = new Interval();\r
38 \r
39                         //declare local variables around here\r
40 \r
41                         ///- 1 Declaration /////////////////////////////////////////////////////////////\r
42 \r
43                         ///+ 2 Initialization //////////////////////////////////////////////////////////\r
44                         //// 2 Initialization\r
45 \r
46                         // Set initial values for local variables\r
47                         CANVAS_REFRESHRATE = (int)cnvs.getRefreshRate();\r
48                         CANVAS_FRAMENUM = 0;\r
49                         DEFAULT_BG_COLOR.set(127.0/255.0,127.0/255.0,127.0/255.0,1.0); // default background color is 127/255 mid-gray\r
50 \r
51 \r
52                         SOAFrames = new Psychlops.Widgets.Slider("SOA Frames", 1  <=rng<=20  ,  1   , 1);\r
53                         SOAFrames.value = 5;\r
54                         gap = new Psychlops.Widgets.Slider("Grating gap" , 0  <=rng<=200 ,  1   , 10);\r
55                         gap.value = 20;\r
56                         wavelength = new Psychlops.Widgets.Slider("Wave Length", 1 <= rng <= 50, 1, 10);\r
57                         wavelength.value = 30.0;\r
58                         contrast = new Psychlops.Widgets.Slider("Contrast"    , 0.0<=rng<=1.0 ,  0.1 , 0.01);\r
59                         contrast.value = 0.1;\r
60 \r
61                         // Draw Offline images around here\r
62 \r
63                         // Offline Movie calculation using Image array around here\r
64 \r
65                         ///- 2 Initialization //////////////////////////////////////////////////////////\r
66 \r
67                         ///+ 3 Drawing /////////////////////////////////////////////////////////////////\r
68                         //// 3 Drawing\r
69                         while(!Keyboard.esc.pushed()) {\r
70                         cnvs.clear(DEFAULT_BG_COLOR);\r
71 \r
72                         //Write draw commands for realtime figure calculation and drawing around here\r
73 \r
74 \r
75                         if(Math.mod(CANVAS_FRAMENUM,SOAFrames) == 0){shift += 1;}\r
76 \r
77                         grating.set(1,gratingheight).centering().shift(-gratingwidth/2, 0);\r
78                         for(int i=0; i<gratingwidth; i++) {\r
79                                 color.set(\r
80                                                         + 0.25*contrast*Math.sin(2*Math.PI*(i*2.0/wavelength + 0.50 * shift))\r
81                                                         + 0.25*contrast*Math.sin(2*Math.PI*(i*3.0/wavelength + 0.75 * shift))\r
82                                                         +0.5\r
83                                                         );\r
84                                 grating.shift(1, 0);\r
85                                 grating.draw(color);\r
86                         }\r
87 \r
88                         grating.set(1,gratingheight).centering().shift(-gratingwidth/2, -gratingheight-gap);\r
89                         for(int i=0; i<gratingwidth; i++) {\r
90                                 color.set(\r
91                                                         + 0.25*contrast*Math.sin(2.0*Math.PI*(i*2.0/wavelength + 0.50 * shift))\r
92                                                         +0.5\r
93                                                         );\r
94                                 grating.shift(1,0);\r
95                                 grating.draw(color);\r
96                         }\r
97 \r
98                         grating.set(1,gratingheight).centering().shift(-gratingwidth/2, gratingheight+gap);\r
99                         for(int i=0; i<gratingwidth; i++) {\r
100                                 color.set(\r
101                                                         + 0.25*contrast*Math.sin(2.0*Math.PI*(i*3.0/wavelength + 0.75 * shift))\r
102                                                         +0.5\r
103                                                         );\r
104                                 grating.shift(1,0);\r
105                                 grating.draw(color);\r
106                         }\r
107 \r
108                         cnvs.flip();\r
109                         CANVAS_FRAMENUM++;\r
110                         }\r
111                         ///- 3 Drawing /////////////////////////////////////////////////////////////////\r
112 \r
113                 }\r
114 \r
115 \r
116         }\r
117 \r
118 }\r
119 \r
120 /*\r
121                 public void psychlops_main() {\r
122 \r
123        Canvas display = new Canvas(768,768);\r
124 \r
125        double rect_size = 100;\r
126        double rect_lum  = 0.5;\r
127        double bg_lum    = 0.5;\r
128        double size = 250.0;\r
129    double sigma;\r
130    sigma = size/6.0;\r
131    double lambda;\r
132    lambda = 6*sigma;\r
133    int envelopemode = 0;\r
134    double refresh = display.getRefreshRate();\r
135    double[] SF=new double[2];\r
136 \r
137    SF[0]=4.0;\r
138    SF[1]=4.0;\r
139 \r
140    double phase=0;\r
141 \r
142    double[] initphase = new double[2];\r
143    double _i, _j, _x, _y, _x2, _y2, col;\r
144    double amplitude = 1.0;\r
145 \r
146        Rectangle fixation1 = new Rectangle(3,9);\r
147    Rectangle fixation2 = new Rectangle(9,3);\r
148        fixation1.centering();\r
149        fixation2.centering();\r
150    Rectangle contrastrect = new Rectangle();\r
151    contrastrect.set(760,760);\r
152 \r
153    Image envelope = new Image();\r
154    Psychlops.Color bglum= new Color(0.5,0.5,0.5);\r
155    Psychlops.Color masklum = new Color(0.5,0.5,0.5,0.5);\r
156 \r
157        Interval rng = new Interval();\r
158 \r
159 \r
160        Psychlops.Widgets.Slider TF;\r
161        TF = new Psychlops.Widgets.Slider("TF", 0.25<=rng<=2.0 , 0.1);\r
162    TF.value=0.75;\r
163 \r
164 \r
165 \r
166        Psychlops.Widgets.Slider SFs;\r
167        SFs = new Psychlops.Widgets.Slider("SF", 4.0<=rng<=16.0 ,  4.0);\r
168        SFs.value = 4.0;\r
169 \r
170    Psychlops.Widgets.Slider contrast;\r
171        contrast= new Psychlops.Widgets.Slider("Contrast", 0.0<=rng<=1.0 ,  0.1);\r
172        contrast.value = 0.1;\r
173 \r
174 \r
175        var element =  StaticFunctions.NewArray<Figures.ShaderGabor>(2);\r
176     for (int i = 0; i < element.Length; i++){\r
177            element[i].setSigma(sigma); // set a size of patch as a halfwidth of Gaussian\r
178            element[i].orientation = Math.PI/4.0+Math.PI/2.0*(1-i);//Math.PI/2*(rand()%2);// //set orientations\r
179            initphase[i]=0;\r
180   }\r
181 \r
182 \r
183 \r
184       envelope.set(size, size);\r
185       envelope.clear(bglum);\r
186 \r
187    for(int i=0; i<size; i++){\r
188        for(int j=0; j<size; j++){\r
189            _i=i-size*0.5;\r
190            _j=j-size*0.5;\r
191            _x=_i*Math.cos(-Math.PI/4.0)+_j*Math.sin(-Math.PI/4.0);\r
192            _y=-_i*Math.sin(-Math.PI/4.0)+_j*Math.cos(-Math.PI/4.0);\r
193            col=Math.exp(-(_x*_x)/(2*Math.pow(sigma*2/6.0,2.0))-(_y*_y)/(2*Math.pow(sigma*2,2.0)));\r
194            envelope.alpha(i,j,1.0-col);\r
195 \r
196            }\r
197        }\r
198 \r
199 \r
200        while(!Keyboard.esc.pushed()) {\r
201            if(Keyboard.spc.pushed())envelopemode++; envelopemode%=2;\r
202        Display.clear(bglum);\r
203        phase ++;\r
204        //phase = fmod(phase,60);\r
205 \r
206        for(int i=0; i<2; i++) {\r
207            element[i].phase =(SF[i]/SF[0])*amplitude*\r
208 Math.PI*Math.sin(Math.PI/2*i+2*Math.PI*phase*TF/refresh);\r
209            element[i].wavelength = lambda/SF[i];\r
210            element[i].contrast = contrast;\r
211        }\r
212 \r
213 \r
214        element[0].centering().shift(-size * 0.6, -size * 0.6);\r
215        element[0].draw();\r
216        element[1].centering().shift(size*0.6,-size*0.6);\r
217        element[1].draw();\r
218                element[1].centering().shift(-size*0.6,size*0.6);\r
219        element[1].draw();\r
220                element[0].centering().shift(size*0.6,size*0.6);\r
221        element[0].draw();\r
222 \r
223 \r
224                if(envelopemode>0){\r
225                    envelope.centering().shift(size*0.6,-size*0.6).draw();\r
226                    envelope.centering().shift(-size*0.6,size*0.6).draw();\r
227                    }\r
228        masklum.set(bg_lum, bg_lum, bg_lum, 1-contrast);\r
229        contrastrect.centering();\r
230        contrastrect.draw(masklum);\r
231        fixation1.centering().shift(size*2,0.0);\r
232        fixation1.draw(Color.red);\r
233        fixation2.centering().shift(size*2,0.0);\r
234        fixation2.draw(Color.red);\r
235 \r
236            Display.var(Mouse.x, 300, 300);\r
237            Display.var(Mouse.y, 300, 320);\r
238 \r
239                Display.flip();\r
240 \r
241 \r
242        }\r
243 \r
244 }\r
245 \r
246         }\r
247 \r
248 }\r
249 */\r
250 \r
251 \r
252 /*\r
253 \r
254 using Psychlops;\r
255 //Position Bias Program\r
256 namespace PsychlopsSilverlightApp\r
257 {\r
258 \r
259         public class PsychlopsMain\r
260         {\r
261                 Canvas cnvs;\r
262                 Image img, img2, img3;\r
263                 int isize = 40;\r
264                 int frames;\r
265                 Psychlops.Widgets.Slider tfreq;\r
266                 Psychlops.Widgets.Slider contrast;\r
267                 Psychlops.Widgets.Slider lambda;\r
268 \r
269 \r
270 \r
271                 public void psychlops_main()\r
272                 {\r
273                         cnvs = new Canvas(300, 600);\r
274                         Interval rng = new Interval();\r
275                         tfreq = new Psychlops.Widgets.Slider("Temporal Frequency(Hz)", -5 <= rng <= 5, 3.0);\r
276                         contrast = new Psychlops.Widgets.Slider("Contrast", 0.0 <= rng <= 1.0, 0.25);\r
277                         lambda = new Psychlops.Widgets.Slider("Wave Length", 10.0 <= rng <= 120.0, 30);\r
278 \r
279                         img = new Image(isize * 2, isize * 2);\r
280                         img2 = new Image(isize * 2, isize * 2);\r
281                         img3 = new Image(isize * 2, isize * 2);\r
282 \r
283                         var gabor1 = StaticFunctions.NewArray<Figures.ShaderGabor>(100);\r
284                         foreach (var g in gabor1)\r
285                         {\r
286                                 g.setSigma(isize/8).centering().shift(Math.random(300) - 150, Math.random(600) - 300);\r
287                                 //g.setSize(isize).centering().shift(Math.random(300) - 150, Math.random(600) - 300);\r
288                                 g.orientation = Math.random(2 * Math.PI);\r
289                                 //g.orientation2 = Math.random(2 * Math.PI);\r
290                         }\r
291 \r
292                         while (true)\r
293                         {\r
294                                 cnvs.clear(new Color(0.5));\r
295 \r
296                                 Figures.drawGabor(ref img, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60);\r
297                                 Figures.drawGabor(ref img2, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * -tfreq / 60);\r
298                                 Figures.drawGabor(ref img3, isize / 8, 1 / lambda, contrast, 0.5 * Math.PI, (double)frames * 2.0 * Math.PI * tfreq / 60);\r
299 \r
300 \r
301                                 //img.centering().shift(0, -isize * 1.5).draw();\r
302                                 //img2.centering().draw();\r
303                                 //img3.centering().shift(0, isize * 1.5).draw();\r
304 \r
305 \r
306                                 //foreach (var g in gabor1)\r
307                                 for (int i = 0; i < gabor1.Length; i++)\r
308                                 {\r
309                                         gabor1[i].wavelength = lambda;\r
310                                         gabor1[i].phase = (double)frames * 2.0 * Math.PI * tfreq / 60;\r
311                                         gabor1[i].contrast = contrast;\r
312                                         //g.wavelength2 = lambda * 2;\r
313                                         //g.phase2 = (double)frames * 2.0 * Math.PI * tfreq / 60;\r
314                                         //g.contrast2 = contrast / 2;\r
315                                         gabor1[i].draw();\r
316                                 }\r
317 \r
318                                 if (!Mouse.left.pressed()) frames++;\r
319 \r
320                                 cnvs.flip();\r
321                         }\r
322                 }\r
323         }\r
324 }\r
325 \r
326 \r
327 /*\r
328 ///+ Prefix linkto BasicCode1\r
329 //// Lines for set up Psychlops environment\r
330 using Psychlops;\r
331 \r
332 namespace PsychlopsSilverlightApp\r
333 {\r
334 \r
335         public class PsychlopsMain\r
336         {\r
337                 ///- Prefix linkto BasicCode1\r
338 \r
339 \r
340                 ///+ Main Routine\r
341                 //// Psychlops runs at the first line of this function psychlops_main().\r
342                 public void psychlops_main()\r
343                 {\r
344 \r
345                         ///+ 0 SetGlobal\r
346                         ////Prepare global parameters\r
347                         Rectangle p1 = new Rectangle(1,1);\r
348                         Color col1 = new Color();\r
349                         double width=50;\r
350                         double height=10;\r
351                         double lambda=60;\r
352                         double lmean=0.25, contrast = 0.5;\r
353                         double x0 = 0;\r
354                         double y0 = 0;\r
355                     double pitch = 1;\r
356                         Canvas disp = new Canvas(Canvas.window, Display.secondary);\r
357                         \r
358                         var fieldW_ = Psychlops.Widgets.Browser.Element.byID("fieldW");\r
359                         var fieldH_ = Psychlops.Widgets.Browser.Element.byID("fieldH");\r
360                         var wL_     = Psychlops.Widgets.Browser.Element.byID("wL");\r
361                         var Cont_   = Psychlops.Widgets.Browser.Element.byID("Cont");\r
362                         var ix_     = Psychlops.Widgets.Browser.Element.byID("ix");\r
363                         var iy_     = Psychlops.Widgets.Browser.Element.byID("iy");\r
364                         var gap_    = Psychlops.Widgets.Browser.Element.byID("gap");\r
365                         ///- 0 SetGlobal\r
366 \r
367                         ///+ 1 Initialize\r
368                         ////initialize\r
369                         p1.centering().shift(-width*0.5 + x0, -height*0.5 + y0); //Move a point to the initial position.\r
370                         ///- 1 Initialize\r
371 \r
372                         ///+ 2 Drawing\r
373                          ////drawing objects\r
374                         for(int i=0; i < width; i++)\r
375                         {\r
376                                 col1.set(lmean*((contrast*Math.sin((2*Math.PI*i/lambda)))+1)); //Set a color.\r
377 \r
378                                 ///+ 2.1 Drawing a column\r
379                                 ////drawing a column\r
380                                 for(int j=0;j < height; j++)\r
381                                 {\r
382                                         p1.draw(col1);\r
383                                         p1.shift(0, pitch);\r
384                                 }\r
385                                 ///- 2.1 Drawing a column\r
386 \r
387                                 p1.shift(1, -height * pitch); //back to vertical intitial point after drawing one column.\r
388                         }\r
389                         disp.flip();\r
390                         ///- 2 Drawing\r
391 \r
392                         while (!Keyboard.spc.pushed()) { }; //Wait until space key is pressed.\r
393                 }\r
394 \r
395 \r
396         }\r
397 \r
398 }\r
399  * */\r
400 \r
401 \r
402 /*\r
403 ///+ Prefix\r
404 //// Lines for set up Psychlops environment\r
405 using Psychlops;\r
406 \r
407 namespace PsychlopsSilverlightApp\r
408 {\r
409 \r
410         public class PsychlopsMain\r
411         {///- Prefix\r
412 \r
413 \r
414 \r
415                 ///+ Main Routine\r
416                 //// Psychlops runs at the first line of this function psychlops_main().\r
417                 public void psychlops_main()\r
418                 {\r
419                         Canvas window = new Canvas(300, 300); //Create a window. Here, window variables are preset mode.\r
420 \r
421                         var FIGURE = Psychlops.Widgets.Browser.Element.byID("FIGURE");\r
422                         var SIZE_X_ = Psychlops.Widgets.Browser.Element.byID("SIZE_X");\r
423                         var SIZE_Y_ = Psychlops.Widgets.Browser.Element.byID("SIZE_Y");\r
424                         var COLOR_R = Psychlops.Widgets.Browser.Element.byID("COLOR_R");\r
425                         var COLOR_G = Psychlops.Widgets.Browser.Element.byID("COLOR_G");\r
426                         var COLOR_B = Psychlops.Widgets.Browser.Element.byID("COLOR_B");\r
427                         var COLOR_BG = Psychlops.Widgets.Browser.Element.byID("COLOR_BG");\r
428                         var Period_Size = Psychlops.Widgets.Browser.Element.byID("Period_Size");\r
429                         var Period_Position = Psychlops.Widgets.Browser.Element.byID("Period_Position");\r
430                         var Speed_HSize_ = Psychlops.Widgets.Browser.Element.byID("Speed_HSize");\r
431                         var Speed_VSize = Psychlops.Widgets.Browser.Element.byID("Speed_VSize");\r
432                         var Speed_VMotion = Psychlops.Widgets.Browser.Element.byID("Speed_VMotion");\r
433                         double SIZE_X = SIZE_X_, SIZE_Y = SIZE_Y_;\r
434 \r
435                         ///+ 2\r
436                         ////  Set a figure size, position and color.\r
437                         Rectangle figure = new Rectangle();\r
438                         Ellipse figure2 = new Ellipse();\r
439                         figure.set(SIZE_X_, SIZE_Y_); // Set the size of figure.\r
440                         figure2.set(SIZE_X_, SIZE_Y_); // Set the size of figure.\r
441                         figure.centering().shift(0, 0); // Move the figure to the starting point.\r
442                         figure2.centering().shift(0, 0); // Move the figure to the starting point.\r
443                         ///- 2\r
444 \r
445                         int frame = 0;\r
446                         int looming_direction = 1, motion_dir = 1;\r
447                         double direction = 1.0;\r
448                         //looming_direction is a variable for size change.\r
449                         //motion_dir is a variable for motion direction.\r
450                         ///+ 3 Drawing loop\r
451                         //// Draw each frames in a "while loop".\r
452                         while (!Keyboard.esc.pushed())\r
453                         { //exit a program when the escape key is pressed down.\r
454                                 window.clear(COLOR_BG); // Clear the window with a designated gray-scale level.\r
455 \r
456                                 ///+ 3.1\r
457                                 //// Calculate object's position and size for each frame.\r
458 \r
459                                 if (frame % Period_Size == 0)\r
460                                 { // a direction of size change will reverse at designated frames.\r
461                                         looming_direction = looming_direction * -1;\r
462                                 }\r
463 \r
464                                 if (frame % Period_Position == 0)\r
465                                 { // motion direction will reverse at designated frames.\r
466                                         motion_dir *= -1;\r
467                                 }\r
468 \r
469                                 ///- 3.1\r
470 \r
471                                 ///+ 3.2 \r
472                                 //// Settting figure's properties\r
473                                 figure.resize(figure.getWidth() + looming_direction * 1, // Scaling the figure in a direction given by "looming_direction".\r
474                                                                  figure.getHeight() + looming_direction * 1);// Scaling the figure in a direction given by "looming_direction".\r
475                                 figure.shift(motion_dir * 1.0, motion_dir * Speed_VMotion);// Move the figure for 1 pixel in direction given by "motion_dir". \r
476                                 figure2.resize(figure.getWidth() + looming_direction * 1, // Scaling the figure in a direction given by "looming_direction".\r
477                                                                 figure.getHeight() + direction * Speed_VSize);// Scaling the figure in a direction given by "looming_direction".\r
478                                 figure2.shift(motion_dir * 1.0, motion_dir * Speed_VMotion);// Move the figure for 1 pixel in direction given by "motion_dir". \r
479                                 /*\r
480                                 figure.resize(figure.getWidth() + looming_direction * 1, // Scaling the figure in a direction given by "looming_direction".\r
481                                                          figure.getHeight() + looming_direction * 1);// Scaling the figure in a direction given by "looming_direction".\r
482                                 figure.shift(motion_dir * 1.0, motion_dir * Speed_VMotion);// Move the figure for 1 pixel in direction given by "motion_dir". \r
483                                 figure2.resize(figure.getWidth() + looming_direction * 1, // Scaling the figure in a direction given by "looming_direction".\r
484                                                                 figure.getHeight() + direction * Speed_VSize);// Scaling the figure in a direction given by "looming_direction".\r
485                                 figure2.shift(motion_dir * 1.0, motion_dir * Speed_VMotion);// Move the figure for 1 pixel in direction given by "motion_dir". \r
486                                  * * /\r
487                                 ///- 3.2\r
488 \r
489                                 ///+ 3.3 \r
490                                 ////Drawing\r
491                                 if ("Rectangle".Equals(FIGURE))\r
492                                 {\r
493                                         figure.draw(new Color(COLOR_R, COLOR_G, COLOR_B)); // Drawing the figure with a designated color at designated position.\r
494                                 }\r
495                                 else\r
496                                 {\r
497                                         figure2.draw(new Color(COLOR_R, COLOR_G, COLOR_B)); // Drawing the figure with a designated color at designated position.\r
498                                 }\r
499                                 window.flip(); // Reflect the drawing for the display by flipping frame buffers.\r
500                                 ///- 3.3\r
501 \r
502                                 ///+ 3.4\r
503                                 ////make a step for next frames \r
504                                 frame++;\r
505                                 ///- 3.4\r
506                         }\r
507                         ///- 3 Drawing loop\r
508 \r
509                 }\r
510                 ///- Main Routine\r
511 \r
512 \r
513         }\r
514 \r
515 }\r
516 \r
517 \r
518 \r
519 /*\r
520 //The reversed-phi motion.\r
521 //Anstis (1970)\r
522 //Phi movement as a subtraction process. Vision Res 10:1411?1430\r
523 \r
524 ///+ Prefix\r
525 //// Include Psychlops Package\r
526 using Psychlops;\r
527 \r
528 namespace PsychlopsSilverlightApp\r
529 {\r
530 \r
531         public class PsychlopsMain\r
532         {\r
533 \r
534                 ///- Prefix\r
535 \r
536                 ///+ Stimulus drawing function\r
537                 //// A function for stimulus drawing (main body)\r
538                 void RectLuminance()\r
539                 {\r
540 \r
541                         ///+ Preperation\r
542                         //// Declare and initialize local variables\r
543                         double rect_size = 20;\r
544 \r
545                         double bg_lum = 0.5;\r
546                         double radii = 200;\r
547                         double rect_lum;\r
548                         int element_number = 25;\r
549                         int radial_row = 5;\r
550 \r
551                         double rotation_tf = 0.2;\r
552                         double polarity = 1;\r
553                         double rotation;\r
554 \r
555                         double contrastflag = 1;\r
556                         int period = 3;\r
557                         double refresh;\r
558 \r
559                         Canvas display = new Canvas(Canvas.window); //Prepare drawing window\r
560                         refresh = Display.getRefreshRate();\r
561 \r
562                         Ellipse rect = new Ellipse();\r
563                         rect.set(rect_size, rect_size);\r
564 \r
565                         Ellipse fixation = new Ellipse(5, 5);\r
566                         fixation.centering();\r
567 \r
568                         Letters let1 = new Letters("Press Space key to change stimulus type");\r
569                         let1.centering().shift(-180, 220);\r
570                         Letters let2 = new Letters("Phi");\r
571                         let2.centering().shift(-10, 200);\r
572                         Letters let3 = new Letters("Reversed-Phi");\r
573                         let3.centering().shift(-60, 200);\r
574 \r
575                         rotation = Math.random(2 * Math.PI);\r
576 \r
577                         ///+ user interface\r
578                         ////register variables to demo circumstances\r
579                         Interval rng = new Interval();\r
580 \r
581                         Psychlops.Widgets.Slider rect_contrast;\r
582                         rect_contrast = new Psychlops.Widgets.Slider("Contrast", 0.1 <= rng <= 1.0, 0.1);\r
583                         rect_contrast.value = 0.5;\r
584                         ///- user interface\r
585                         ///- Preperation\r
586 \r
587                         ///+ Main loop\r
588                         ////Main loop\r
589                         int frame = 0;\r
590                         //AppState::setThreadPriority(AppState::HIGH);\r
591                         while (true)\r
592                         {\r
593                                 frame++;\r
594                                 if (Keyboard.spc.pushed()) contrastflag = -contrastflag;\r
595                                 if (contrastflag < 0) polarity = -polarity;\r
596                                 rotation = 2 * Math.PI * rotation_tf * frame * period / refresh;\r
597 \r
598                                 for (int frame_now = 0; frame_now < period; frame_now++)\r
599                                 {\r
600                                         Display.clear(new Color(bg_lum));\r
601                                         for (int j = 0; j < radial_row; j++)\r
602                                         {\r
603                                                 rect.resize(rect_size * (j + radial_row) / 10.0, rect_size * (j + radial_row) / 10.0);\r
604                                                 for (double i = 0; i < element_number; i++)\r
605                                                 {\r
606                                                         rect_lum = bg_lum * (1.0 + polarity * rect_contrast);\r
607                                                         rect.centering().shift(radii * (j + radial_row) / 10.0 * Math.cos(rotation + i * 2 * Math.PI / element_number), radii * (j + radial_row) / 10.0 * Math.sin(rotation + i * 2 * Math.PI / element_number));\r
608                                                         rect.draw(rect_lum);\r
609                                                 }\r
610                                         }\r
611                                         fixation.draw(Color.red);\r
612                                         let1.draw();\r
613                                         if (contrastflag > 0) let2.draw();\r
614                                         else let3.draw();\r
615                                         Display.flip();\r
616                                 }\r
617                         }\r
618                         ///- Main loop\r
619                         //AppState::setThreadPriority(AppState::NORMAL);\r
620                 }\r
621                 ///- Stimulus drawing function\r
622 \r
623                 ///+ Main function for demo circumstances\r
624                 //// Psychlops Main function\r
625                 public void psychlops_main()\r
626                 {\r
627                         ///+ Demo circumstances\r
628                         //// Spells for run demonstration circumstances\r
629                         Procedure p = new Procedure();\r
630                         //p.setDesign(Procedure::DEMO); //Designate that this is a demo.\r
631                         p.setProcedure(RectLuminance);  //The argument name is a name of drawing function.\r
632                         p.run();\r
633                         ///- Demo circumstances\r
634                 }\r
635                 ///- Main function for demo circumstances\r
636 \r
637         }\r
638 \r
639 }\r
640 \r
641 \r
642 \r
643 /*\r
644 //Two types of plaid motion\r
645 //E. H. Adelson and J. A. Movshon (1982).\r
646 //Phenomenal coherence of moving visual patterns. Nature 300, 523-525\r
647 \r
648 ///+ Prefix\r
649 //// Include Psychlops Package\r
650 using Psychlops;\r
651 \r
652         namespace PsychlopsSilverlightApp\r
653         {\r
654 \r
655                 public class PsychlopsMain\r
656                 {\r
657                         ///- Prefix\r
658 \r
659                         ///+ Global\r
660                         // Struct for component paremeters\r
661                         struct component\r
662                         {\r
663                                 public double contrast;\r
664                                 public double orientation;\r
665                                 public double lambda;\r
666                                 public double tf;\r
667                         }\r
668                         ///- Global\r
669 \r
670                         ///+ Stimulus drawing function\r
671                         //// A function for stimulus drawing (main body)\r
672                         void drawgratingmovie(Image[] img, component c1, component c2, double contrast, int start, int maxframe, double bg_lum, double alpha)\r
673                         {\r
674                                 double _xp, _xp2, col1, col2;\r
675                                 double contrast1, contrast2;\r
676                                 double imageheight, imagewidth;\r
677 \r
678                                 contrast1 = contrast*c1.contrast/(c1.contrast+c2.contrast);\r
679                                 contrast2 = contrast*c2.contrast/(c1.contrast+c2.contrast);\r
680 \r
681                                 imageheight = img[0].getHeight();\r
682                                 imagewidth = img[0].getWidth();\r
683 \r
684                                 for(int frame=start; frame<start+maxframe+1; frame++){\r
685                                         //Display.progressbar(frame, maxframe+1);\r
686                                         //img[frame].convert(Image::RGBA);\r
687                                         for(int i=0; i<imagewidth; i++){\r
688                                                 for(int j=0; j<imageheight; j++){\r
689                                                         _xp = Math.sin(c1.orientation)*(i-imagewidth*0.5) + Math.cos(c1.orientation) * (j-imageheight *0.5);\r
690                                                         _xp2 = Math.sin(c2.orientation)*(i-imagewidth*0.5) + Math.cos(c2.orientation) * (j-imageheight *0.5);\r
691                                                         col1 = bg_lum*contrast1*Math.sin(2*Math.PI*_xp/c1.lambda + 2*Math.PI*c1.tf*frame/(double)refresh);\r
692                                                         col2 = bg_lum*contrast2*Math.sin(2*Math.PI*_xp2/c2.lambda + 2*Math.PI*c2.tf*frame/(double)refresh);\r
693                                                         img[frame].pix(i,j,new Color(col1+col2+bg_lum));\r
694                                                         img[frame].alpha(i,j,alpha);\r
695                                                 }\r
696                                         }\r
697                                         //img[frame].cache();\r
698                                 }\r
699                         }\r
700 \r
701                         ///+ Stimulus drawing function\r
702                         //// A function for stimulus drawing (main body)\r
703                         void drawplaid() {\r
704                                 Canvas display = new Canvas(Canvas.window);\r
705                                 ///+ Preperation\r
706                                 //// Declare and initialize local variables\r
707                                 int rect_size = 150;\r
708                                 double rect_lum  = 0.5;\r
709                                 double bg_lum    = 0.2;\r
710                                 double duration = 400;\r
711 \r
712                                 double[] lambda = new double[2], contrast = new double[2], tf = new double[2];\r
713                                 double center_orientation = 0.0;\r
714                                 Image[,] movie = StaticFunctions.NewArray<Image>(2, 120);\r
715                                 Image[,] component_movie = StaticFunctions.NewArray<Image>(4, 120);\r
716                                 Image envelope = new Image(), envelope_small = new Image();\r
717 \r
718                                 for(int i=0; i<120; i++)\r
719                                 {\r
720                                         movie[0, i].set(rect_size, rect_size);\r
721                                         movie[1, i].set(rect_size, rect_size);\r
722                                         component_movie[0, i].set(rect_size/2, rect_size/2);\r
723                                         component_movie[1, i].set(rect_size/2, rect_size/2);\r
724                                         component_movie[2, i].set(rect_size/2, rect_size/2);\r
725                                         component_movie[3, i].set(rect_size/2, rect_size/2);\r
726                                 }\r
727                                 envelope.set(rect_size, rect_size);\r
728                                 envelope_small.set(rect_size/2, rect_size/2);\r
729 \r
730                                 Letters let1 = new Letters(), let2 = new Letters(), let3 = new Letters();\r
731                                 let1.str = "Component1";\r
732                                 let2.str = "Component2";\r
733                                 let3.str = "Superposition";\r
734                                 let1.centering().shift(-rect_size*1.25, -rect_size);\r
735                                 //let1.cache();\r
736                                 let2.centering().shift(rect_size*1.25, -rect_size);\r
737                                 //let2.cache();\r
738                                 let3.centering().shift(0.0, rect_size*0.75);\r
739                                 //let3.cache();\r
740 \r
741                                 component c1, c2, c0;\r
742                                 c0 = new component{ 0.0,0.0,1.0,1.0 }; //dummy for component movie\r
743                                 ///+ type-I\r
744                                 // Prepare Type-I plaid movie\r
745                                 center_orientation = 0.0;\r
746                                 orientation_offset = Math.PI/6;\r
747                                 contrast[0] = 1.0, contrast[1] = 1.0;\r
748                                 lambda[0] = 30.0, lambda[1] = 30.0;\r
749                                 tf[0] = 1.0; tf[1] = 1.0;\r
750 \r
751                                 c1 = {contrast[0], center_orientation-orientation_offset, lambda[0], tf[0]};\r
752                                 c2 = {contrast[1], center_orientation+orientation_offset, lambda[1], tf[1]};\r
753 \r
754                         drawgratingmovie(movie[0],c1, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
755                         drawgratingmovie(component_movie[0],c0, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
756                         drawgratingmovie(component_movie[1],c1, c0, 0.5, 0, refresh_int, bg_lum, 1.0);\r
757                         ///- type-I\r
758 \r
759                         ///+ type-II\r
760                         // Prepare Type-II plaid movie\r
761                         center_orientation = -0.0, orientation_offset = Math.PI/12;\r
762                         contrast[0] = 1.0, contrast[1] = 1.0;\r
763                         lambda[0] = 30.0, lambda[1] = 30.0;\r
764                         tf[0] = 1.0; tf[1] = 4.0;\r
765 \r
766                         c1 = {contrast[0], center_orientation-orientation_offset, lambda[0], tf[0]};\r
767                         c2 = {contrast[1], center_orientation+orientation_offset, lambda[1], tf[1]};\r
768 \r
769                         drawgratingmovie(movie[1],c1, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
770                         drawgratingmovie(component_movie[2],c0, c2, 0.5, 0, refresh_int, bg_lum, 1.0);\r
771                         drawgratingmovie(component_movie[3],c1, c0, 0.5, 0, refresh_int, bg_lum, 1.0);\r
772                         ///- type-II\r
773 \r
774                         ///+ gaussian\r
775                         //draw Gaussian envelopes\r
776                         envelope.clear(new Color(bg_lum)); //clear offscreen image\r
777                         double _x, _y;\r
778                         for(int i=0; i<rect_size; i++){\r
779                         _x=i-0.5*rect_size;\r
780                         for(int j=0; j<rect_size; j++){\r
781                         _y=j-0.5*rect_size;\r
782                         envelope.alpha(i,j,1.0-Math.exp(-((_x*_x+_y*_y)/ (2.0*pow(rect_size/6.0, 2.0) ))));\r
783                         }\r
784                         }\r
785 \r
786                         envelope_small.clear(new Color(bg_lum)); //clear offscreen image\r
787                         for(int i=0; i<rect_size*0.5; i++){\r
788                         _x=i-0.25*rect_size;\r
789                         for(int j=0; j<rect_size*0.5; j++){\r
790                         _y=j-0.25*rect_size;\r
791                         envelope_small.alpha(i,j,1.0-Math.exp(-((_x*_x+_y*_y)/ (2.0*pow(rect_size/12.0, 2.0) ))));\r
792                         }\r
793                         }\r
794                         envelope.cache(); //send offscreen image from main memory to GPU\r
795                         envelope_small.cache(); //send offscreen image from main memory to GPU\r
796                         ///- gaussian\r
797 \r
798                         ///+ user interface\r
799                         // Draw user interface\r
800                         Widgets::SelectBox stimulus_type;\r
801                         stimulus_type.area.set(120, 20);\r
802                         stimulus_type.append(L"TypeI");\r
803                         stimulus_type.append(L"TypeII");\r
804                         stimulus_type.centering().shift(-120.0,250.0);\r
805 \r
806                         Psychlops::Widgets::SelectBox stimulus_type2;\r
807                         stimulus_type2.area.set(120, 20);\r
808                         stimulus_type2.append(L"Continuous");\r
809                         stimulus_type2.append(L"Periodic");\r
810                         stimulus_type2.centering().shift(120.0,250.0);\r
811 \r
812                         Psychlops::Widgets::Slider duration_slider;\r
813                         duration_slider.area.set(100,20);\r
814                         duration_slider.centering().shift(120,280.0);\r
815                         Interval rng = new Interval();\r
816                         duration_slider.link(duration, 50<=rng<=950, 50.0, 50.0);\r
817                         ///- user interface\r
818                         ///- Preperation\r
819 \r
820                         ///+ Main loop\r
821                         int frame = 0;\r
822                         int period = refresh_int;\r
823                         int movienum;\r
824                         while(!Keyboard.esc.pushed()) {\r
825                         Display.clear(new Color(bg_lum));\r
826                         movienum = stimulus_type.getSelected(); //if "Type-I" is selected, 0, else frame refresh per sec\r
827 \r
828                         in;\r
829                         else period = refresh_int;\r
830 \r
831                         if(frame>0){\r
832                         movie[movienum, frame].centering().draw();\r
833                         envelope.centering().draw();\r
834                         component_movie[movienum*2, frame].centering().shift(-rect_size*0.5, -rect_size).draw();\r
835                         envelope_small.centering().shift(-rect_size*0.5, -rect_size).draw();\r
836                         component_movie[movienum*2+1, frame].centering().shift(rect_size*0.5, -rect_size).draw();\r
837                         envelope_small.centering().shift(rect_size*0.5, -rect_size).draw();\r
838                         }\r
839 \r
840                         ///+ draw user interface\r
841                         let1.draw(0.75);\r
842                         let2.draw(0.75);\r
843                         let3.draw(0.75);\r
844                         stimulus_type.draw();\r
845                         stimulus_type2.draw();\r
846                         if(stimulus_type2.getSelected())duration_slider.draw();\r
847                         ///- draw user interface\r
848                         Display.flip();\r
849                         if(frame++ >= period) frame -= refresh_int;\r
850                         }\r
851                         ///- Main loop\r
852 \r
853                         }\r
854                         ///- Stimulus drawing function\r
855 \r
856                 ///+ Main function for demo circumstances\r
857                 public void psychlops_main() {\r
858                         ///+ Demo circumstances\r
859                         //// Spells for run demonstration circumstances\r
860                         Procedure p = new Procedure();\r
861                         //p.setDesign(Procedure::DEMO); //Designate that this is a demo.\r
862                         p.setProcedure(drawplaid);  //The argument name is a name of drawing function.\r
863                         p.run();\r
864                         ///- Demo circumstances\r
865                 }\r
866         }\r
867 }\r
868 */\r