b5f3795092ca94ec1a4b837352347a83652c24c2
[xscreensaver] / hacks / strange.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* strange --- strange attractors */
3
4 #if 0
5 static const char sccsid[] = "@(#)strange.c     5.00 2000/11/01 xlockmore";
6 #endif
7
8 /*-
9  * Copyright (c) 1997 by Massimino Pascal <Pascal.Massimon@ens.fr>
10  *
11  * Permission to use, copy, modify, and distribute this software and its
12  * documentation for any purpose and without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and that
14  * both that copyright notice and this permission notice appear in
15  * supporting documentation.
16  *
17  * This file is provided AS IS with no warranties of any kind.  The author
18  * shall have no liability with respect to the infringement of copyrights,
19  * trade secrets or any patents by this file or any part thereof.  In no
20  * event will the author be liable for any lost revenue or profits or
21  * other special, indirect and consequential damages.
22  *
23  * Revision History:
24  * 01-Nov-2000: Allocation checks
25  * 10-May-1997: jwz@jwz.org: turned into a standalone program.
26  *              Made it render into an offscreen bitmap and then copy
27  *              that onto the screen, to reduce flicker.
28  *
29  * strange attractors are not so hard to find...
30  */
31
32 #ifdef STANDALONE
33 # define MODE_strange
34 # define DEFAULTS       "*delay: 10000 \n" \
35                                         "*ncolors: 100 \n" \
36                                         "*fpsClear: True \n"
37
38 # define SMOOTH_COLORS
39 # define refresh_strange 0
40 # define reshape_strange 0
41 # define strange_handle_event 0
42 # include "xlockmore.h"         /* from the xscreensaver distribution */
43 #else /* !STANDALONE */
44 # include "xlock.h"             /* from the xlockmore distribution */
45 #endif /* !STANDALONE */
46
47 #ifdef MODE_strange
48
49 ENTRYPOINT ModeSpecOpt strange_opts =
50 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
51
52 #ifdef USE_MODULES
53 ModStruct   strange_description =
54 {"strange", "init_strange", "draw_strange", "release_strange",
55  "init_strange", "init_strange", (char *) NULL, &strange_opts,
56  1000, 1, 1, 1, 64, 1.0, "",
57  "Shows strange attractors", 0, NULL};
58
59 #endif
60
61 #ifdef HAVE_COCOA
62 # define NO_DBUF
63 #endif
64
65 typedef float DBL;
66 typedef int PRM;
67
68 #define UNIT (1<<12)
69 #define UNIT2 (1<<14)
70 /* #define UNIT2 (3140*UNIT/1000) */
71
72 #define SKIP_FIRST      100
73 #define MAX_POINTS      5500
74 #define DBL_To_PRM(x)  (PRM)( (DBL)(UNIT)*(x) )
75
76
77 #define DO_FOLD(a) (a)<0 ? -A->Fold[ (-(a))&(UNIT2-1) ] : A->Fold[ (a)&(UNIT2-1) ]
78
79 #if 0
80 #define DO_FOLD(a) (a)<-UNIT2 ? -A->Fold[(-(a))%UNIT2] : (a)<0 ? -A->Fold[ -(a) ] :\
81 (a)>UNIT2 ? A->Fold[ (a)%UNIT2 ] : A->Fold[ (a) ]
82 #define DO_FOLD(a) DBL_To_PRM( sin( (DBL)(a)/UNIT ) )
83 #define DO_FOLD(a) (a)<0 ? DBL_To_PRM( exp( 16.0*(a)/UNIT2 ) )-1.0 : \
84 DBL_To_PRM( 1.0-exp( -16.0*(a)/UNIT2 ) )
85 #endif
86
87 /******************************************************************/
88
89 #define MAX_PRM 3*5
90
91 typedef struct _ATTRACTOR {
92         DBL         Prm1[MAX_PRM], Prm2[MAX_PRM];
93         PRM         Prm[MAX_PRM], *Fold;
94         void        (*Iterate) (struct _ATTRACTOR *, PRM, PRM, PRM *, PRM *);
95         XPoint     *Buffer1, *Buffer2;
96         int         Cur_Pt, Max_Pt;
97         int         Col, Count, Speed;
98         int         Width, Height;
99         Pixmap      dbuf;       /* jwz */
100         GC          dbuf_gc;
101 } ATTRACTOR;
102
103 static ATTRACTOR *Root = (ATTRACTOR *) NULL; 
104
105 static DBL  Amp_Prm[MAX_PRM] =
106 {
107         1.0, 3.5, 3.5, 2.5, 4.7,
108         1.0, 3.5, 3.6, 2.5, 4.7,
109         1.0, 1.5, 2.2, 2.1, 3.5
110 };
111 static DBL  Mid_Prm[MAX_PRM] =
112 {
113         0.0, 1.5, 0.0, .5, 1.5,
114         0.0, 1.5, 0.0, .5, 1.5,
115         0.0, 1.5, -1.0, -.5, 2.5,
116 };
117
118 static      DBL
119 Gauss_Rand(DBL c, DBL A, DBL S)
120 {
121         DBL         y;
122
123         y = (DBL) LRAND() / MAXRAND;
124         y = A * (1.0 - exp(-y * y * S)) / (1.0 - exp(-S));
125         if (NRAND(2))
126                 return (c + y);
127         else
128                 return (c - y);
129 }
130
131 static void
132 Random_Prm(DBL * Prm)
133 {
134         int         i;
135
136         for (i = 0; i < MAX_PRM; ++i)
137                 Prm[i] = Gauss_Rand(Mid_Prm[i], Amp_Prm[i], 4.0);
138 }
139
140 /***************************************************************/
141
142    /* 2 examples of non-linear map */
143
144 static void
145 Iterate_X2(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
146 {
147         PRM         xx, yy, xy, x2y, y2x, Tmp;
148
149         xx = (x * x) / UNIT;
150         x2y = (xx * y) / UNIT;
151         yy = (y * y) / UNIT;
152         y2x = (yy * x) / UNIT;
153         xy = (x * y) / UNIT;
154
155         Tmp = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
156         Tmp = A->Prm[0] - y + (Tmp / UNIT);
157         *xo = DO_FOLD(Tmp);
158         Tmp = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
159         Tmp = A->Prm[5] + x + (Tmp / UNIT);
160         *yo = DO_FOLD(Tmp);
161 }
162
163 static void
164 Iterate_X3(ATTRACTOR * A, PRM x, PRM y, PRM * xo, PRM * yo)
165 {
166         PRM         xx, yy, xy, x2y, y2x, Tmp_x, Tmp_y, Tmp_z;
167
168         xx = (x * x) / UNIT;
169         x2y = (xx * y) / UNIT;
170         yy = (y * y) / UNIT;
171         y2x = (yy * x) / UNIT;
172         xy = (x * y) / UNIT;
173
174         Tmp_x = A->Prm[1] * xx + A->Prm[2] * xy + A->Prm[3] * yy + A->Prm[4] * x2y;
175         Tmp_x = A->Prm[0] - y + (Tmp_x / UNIT);
176         Tmp_x = DO_FOLD(Tmp_x);
177
178         Tmp_y = A->Prm[6] * xx + A->Prm[7] * xy + A->Prm[8] * yy + A->Prm[9] * y2x;
179         Tmp_y = A->Prm[5] + x + (Tmp_y / UNIT);
180
181         Tmp_y = DO_FOLD(Tmp_y);
182
183         Tmp_z = A->Prm[11] * xx + A->Prm[12] * xy + A->Prm[13] * yy + A->Prm[14] * y2x;
184         Tmp_z = A->Prm[10] + x + (Tmp_z / UNIT);
185         Tmp_z = UNIT + Tmp_z * Tmp_z / UNIT;
186
187         *xo = (Tmp_x * UNIT) / Tmp_z;
188         *yo = (Tmp_y * UNIT) / Tmp_z;
189 }
190
191 static void (*Funcs[2]) (ATTRACTOR *, PRM, PRM, PRM *, PRM *) = {
192         Iterate_X2, Iterate_X3
193 };
194
195 /***************************************************************/
196
197 static void
198 free_strange(Display *display, ATTRACTOR *A)
199 {
200         if (A->Buffer1 != NULL) {
201                 (void) free((void *) A->Buffer1);
202                 A->Buffer1 = (XPoint *) NULL;
203         }
204         if (A->Buffer2 != NULL) {
205                 (void) free((void *) A->Buffer2);
206                 A->Buffer2 = (XPoint *) NULL;
207         }
208         if (A->dbuf) {
209                 XFreePixmap(display, A->dbuf);
210                 A->dbuf = None;
211         }
212         if (A->dbuf_gc) {
213                 XFreeGC(display, A->dbuf_gc);
214                 A->dbuf_gc = None;
215         }
216         if (A->Fold != NULL) {
217                 (void) free((void *) A->Fold);
218                 A->Fold = (PRM *) NULL;
219         }
220 }
221
222 ENTRYPOINT void
223 draw_strange(ModeInfo * mi)
224 {
225         int         i, j, n, Cur_Pt;
226         PRM         x, y, xo, yo;
227         DBL         u;
228         XPoint     *Buf;
229         Display    *display = MI_DISPLAY(mi);
230         Window      window = MI_WINDOW(mi);
231         GC          gc = MI_GC(mi);
232         DBL         Lx, Ly;
233         void        (*Iterate) (ATTRACTOR *, PRM, PRM, PRM *, PRM *);
234         PRM         xmin, xmax, ymin, ymax;
235         ATTRACTOR  *A;
236
237         if (Root == NULL)
238                 return;
239         A = &Root[MI_SCREEN(mi)];
240         if (A->Fold == NULL)
241                 return;
242
243         Cur_Pt = A->Cur_Pt;
244         Iterate = A->Iterate;
245
246         u = (DBL) (A->Count) / 1000.0;
247         for (j = MAX_PRM - 1; j >= 0; --j)
248                 A->Prm[j] = DBL_To_PRM((1.0 - u) * A->Prm1[j] + u * A->Prm2[j]);
249
250         x = y = DBL_To_PRM(.0);
251         for (n = SKIP_FIRST; n; --n) {
252                 (*Iterate) (A, x, y, &xo, &yo);
253                 x = xo + NRAND(8) - 4;
254                 y = yo + NRAND(8) - 4;
255         }
256
257         xmax = 0;
258         xmin = UNIT * 4;
259         ymax = 0;
260         ymin = UNIT * 4;
261         A->Cur_Pt = 0;
262         Buf = A->Buffer2;
263         Lx = (DBL) A->Width / UNIT / 2.2;
264         Ly = (DBL) A->Height / UNIT / 2.2;
265         for (n = A->Max_Pt; n; --n) {
266                 (*Iterate) (A, x, y, &xo, &yo);
267                 Buf->x = (int) (Lx * (x + DBL_To_PRM(1.1)));
268                 Buf->y = (int) (Ly * (DBL_To_PRM(1.1) - y));
269                 /* (void) fprintf( stderr, "X,Y: %d %d    ", Buf->x, Buf->y ); */
270                 Buf++;
271                 A->Cur_Pt++;
272                 if (xo > xmax)
273                         xmax = xo;
274                 else if (xo < xmin)
275                         xmin = xo;
276                 if (yo > ymax)
277                         ymax = yo;
278                 else if (yo < ymin)
279                         ymin = yo;
280                 x = xo + NRAND(8) - 4;
281                 y = yo + NRAND(8) - 4;
282         }
283
284         MI_IS_DRAWN(mi) = True;
285
286         XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
287
288         if (A->dbuf != None) {          /* jwz */
289                 XSetForeground(display, A->dbuf_gc, 0);
290 /* XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer1,
291    Cur_Pt,CoordModeOrigin); */
292                 XFillRectangle(display, A->dbuf, A->dbuf_gc, 0, 0, A->Width, A->Height);
293         } else
294                 XDrawPoints(display, window, gc, A->Buffer1, Cur_Pt, CoordModeOrigin);
295
296         if (MI_NPIXELS(mi) < 2)
297                 XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
298         else
299                 XSetForeground(display, gc, MI_PIXEL(mi, A->Col % MI_NPIXELS(mi)));
300
301         if (A->dbuf != None) {
302                 XSetForeground(display, A->dbuf_gc, 1);
303                 XDrawPoints(display, A->dbuf, A->dbuf_gc, A->Buffer2, A->Cur_Pt,
304                             CoordModeOrigin);
305                 XCopyPlane(display, A->dbuf, window, gc, 0, 0, A->Width, A->Height, 0, 0, 1);
306         } else
307                 XDrawPoints(display, window, gc, A->Buffer2, A->Cur_Pt, CoordModeOrigin);
308
309         Buf = A->Buffer1;
310         A->Buffer1 = A->Buffer2;
311         A->Buffer2 = Buf;
312
313         if ((xmax - xmin < DBL_To_PRM(.2)) && (ymax - ymin < DBL_To_PRM(.2)))
314                 A->Count += 4 * A->Speed;
315         else
316                 A->Count += A->Speed;
317         if (A->Count >= 1000) {
318                 for (i = MAX_PRM - 1; i >= 0; --i)
319                         A->Prm1[i] = A->Prm2[i];
320                 Random_Prm(A->Prm2);
321                 A->Count = 0;
322         }
323         A->Col++;
324 }
325
326
327 /***************************************************************/
328
329 ENTRYPOINT void
330 init_strange(ModeInfo * mi)
331 {
332         Display    *display = MI_DISPLAY(mi);
333 #ifndef NO_DBUF
334         Window      window = MI_WINDOW(mi);
335         GC          gc = MI_GC(mi);
336 #endif
337         ATTRACTOR  *Attractor;
338
339         if (Root == NULL) {
340                 if ((Root = (ATTRACTOR *) calloc(MI_NUM_SCREENS(mi),
341                                 sizeof (ATTRACTOR))) == NULL)
342                         return;
343         }
344         Attractor = &Root[MI_SCREEN(mi)];
345
346         if (Attractor->Fold == NULL) {
347                 int         i;
348
349                 if ((Attractor->Fold = (PRM *) calloc(UNIT2 + 1,
350                                 sizeof (PRM))) == NULL) {
351                         free_strange(display, Attractor);
352                         return;
353                 }
354                 for (i = 0; i <= UNIT2; ++i) {
355                         DBL         x;
356
357                         /* x = ( DBL )(i)/UNIT2; */
358                         /* x = sin( M_PI/2.0*x ); */
359                         /* x = sqrt( x ); */
360                         /* x = x*x; */
361                         /* x = x*(1.0-x)*4.0; */
362                         x = (DBL) (i) / UNIT;
363                         x = sin(x);
364                         Attractor->Fold[i] = DBL_To_PRM(x);
365                 }
366         }
367         if (Attractor->Buffer1 == NULL)
368                 if ((Attractor->Buffer1 = (XPoint *) calloc(MAX_POINTS,
369                                 sizeof (XPoint))) == NULL) {
370                         free_strange(display, Attractor);
371                         return;
372                 }
373         if (Attractor->Buffer2 == NULL)
374                 if ((Attractor->Buffer2 = (XPoint *) calloc(MAX_POINTS,
375                                 sizeof (XPoint))) == NULL) {
376                         free_strange(display, Attractor);
377                         return;
378                 }
379         Attractor->Max_Pt = MAX_POINTS;
380
381         Attractor->Width = MI_WIDTH(mi);
382         Attractor->Height = MI_HEIGHT(mi);
383         Attractor->Cur_Pt = 0;
384         Attractor->Count = 0;
385         Attractor->Col = NRAND(MI_NPIXELS(mi));
386         Attractor->Speed = 4;
387
388         Attractor->Iterate = Funcs[NRAND(2)];
389         Random_Prm(Attractor->Prm1);
390         Random_Prm(Attractor->Prm2);
391 #ifndef NO_DBUF
392         if (Attractor->dbuf != None)
393                 XFreePixmap(display, Attractor->dbuf);
394         Attractor->dbuf = XCreatePixmap(display, window,
395              Attractor->Width, Attractor->Height, 1);
396         /* Allocation checked */
397         if (Attractor->dbuf != None) {
398                 XGCValues   gcv;
399
400                 gcv.foreground = 0;
401                 gcv.background = 0;
402 #ifndef HAVE_COCOA
403                 gcv.graphics_exposures = False;
404 #endif /* HAVE_COCOA */
405                 gcv.function = GXcopy;
406
407                 if (Attractor->dbuf_gc != None)
408                         XFreeGC(display, Attractor->dbuf_gc);
409
410                 if ((Attractor->dbuf_gc = XCreateGC(display, Attractor->dbuf,
411 #ifndef HAVE_COCOA
412                                 GCGraphicsExposures |
413 #endif /* HAVE_COCOA */
414                                 GCFunction | GCForeground | GCBackground,
415                                 &gcv)) == None) {
416                         XFreePixmap(display, Attractor->dbuf);
417                         Attractor->dbuf = None;
418                 } else {
419                         XFillRectangle(display, Attractor->dbuf, Attractor->dbuf_gc,
420                                 0, 0, Attractor->Width, Attractor->Height);
421                         XSetBackground(display, gc, MI_BLACK_PIXEL(mi));
422                         XSetFunction(display, gc, GXcopy);
423                 }
424         }
425 #endif
426
427         MI_CLEARWINDOW(mi);
428
429         /* Do not want any exposure events from XCopyPlane */
430         XSetGraphicsExposures(display, MI_GC(mi), False);
431 }
432
433 /***************************************************************/
434
435 ENTRYPOINT void
436 release_strange(ModeInfo * mi)
437 {
438         if (Root != NULL) {
439                 int         screen;
440
441                 for (screen = 0; screen < MI_NUM_SCREENS(mi); ++screen)
442                         free_strange(MI_DISPLAY(mi), &Root[screen]);
443                 (void) free((void *) Root);
444                 Root = (ATTRACTOR *) NULL; 
445         }
446 }
447
448 XSCREENSAVER_MODULE ("Strange", strange)
449
450 #endif /* MODE_strange */