OSDN Git Service

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