home *** CD-ROM | disk | FTP | other *** search
- /* *****************************************************************************
- *
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- *
- ***************************************************************************** */
- #include <gl.h>
- #include <device.h>
- #include <math.h>
- #include "fast.h"
- #include "event.h"
- #include "fft.h"
-
- #define MIN(a,b) ( ((a) < (b)) ? (a) : (b))
-
- void display();
- void InitFast();
- void matgen();
- void dir_fft(float *), inv_fft(float *);
- void inifil();
- void propagate();
-
- double second();
-
-
- extern int main_menu, forward_menu, backward_menu, quit_menu;
-
- int do_exit;
- int ask_quit;
-
- int do_propagate;
-
- int x_size, y_size;
- int this_time, this_deltat;
- int this_proc;
-
- float *mat, *save, *filter, *power, *copy;
- int * mycolors;
- int *ipvt, lda, info, x_0, y_0;
-
- double this_t, this_flop;
-
- char title[128];
-
-
- fast (progname)
- char *progname;
- {
- InitFast();
- Init(progname);
-
- do_exit = FALSE;
-
- while(! do_exit) {
- event();
- if( do_propagate)
- while( qtest() == 0)
- propagate();
- }
- }
-
- do_frame(){
-
- char str[32];
- float v[2];
-
- viewport(0,WIN_SIZE+WIN_RIGHT,0,WIN_SIZE);
-
- cpack (oBLACK);
- clear ();
-
- cpack (oGREEN);
- ortho2(0,WIN_SIZE+WIN_RIGHT,0, WIN_SIZE);
-
- sprintf( str, "SIZE = %d x %d", x_size, y_size);
- cmov2i( 250, WIN_SIZE - 30);
- charstr(str);
-
- cmov2i( 100, 20);
- charstr(title);
-
- bgnpolygon();
- v[0] = WIN_SIZE;
- v[1] = WIN_SIZE/2;
- cpack(oBLACK);
- v2f(v);
- v[1] -= 100;
- cpack(0x007fff);
- v2f(v);
- v[0] += 50;
- v2f(v);
- v[1] += 100;
- cpack(oBLACK);
- v2f(v);
- endpolygon();
- v[1] -= 100;
- bgnpolygon();
- cpack(0x007fff);
- v2f(v);
- v[1] -= 100;
- cpack(0x00ffff);
- v2f(v);
- v[0] -= 50;
- v2f(v);
- v[1] += 100;
- cpack(0x007fff);
- v2f(v);
- endpolygon();
-
- bgnpolygon();
- v[0] = WIN_SIZE;
- v[1] = WIN_SIZE/2;
- cpack(oBLACK);
- v2f(v);
- v[1] += 100;
- cpack(0xff7f00);
- v2f(v);
- v[0] += 50;
- v2f(v);
- cpack(0xff7f00);
- v2f(v);
- v[1] -= 100;
- cpack(oBLACK);
- v2f(v);
- endpolygon();
- v[1] += 100;
- bgnpolygon();
- cpack(0xff7f00);
- v2f(v);
- v[1] += 100;
- cpack(oCYAN);
- v2f(v);
- v[0] -= 50;
- v2f(v);
- v[1] -= 100;
- cpack(0xff7f00);
- v2f(v);
- endpolygon();
- cpack(oWHITE);
- rect(WIN_SIZE,WIN_SIZE/2-200,WIN_SIZE+50,WIN_SIZE/2+200);
-
- cmov2i( WIN_SIZE+55, WIN_SIZE/2 - 200 -4);
- charstr(" -Max");
-
- cmov2i( WIN_SIZE+55, WIN_SIZE/2 + 200 -4);
- charstr(" +Max");
-
- cmov2i( WIN_SIZE+55, WIN_SIZE/2 -4);
- charstr(" 0.0 ");
-
- }
- ShowFast()
- {
- x_0 = (WIN_SIZE/2)-(x_size/2);
- y_0 = (WIN_SIZE/2)-(y_size/2);
- do_frame();
- viewport( y_0, y_0 + y_size + 2, x_0, x_0 + x_size + 2);
- ortho2(-1, y_size+1, -1, x_size+1);
- DrawFast();
- }
-
- void do_quit()
- {
- if(mat)
- free(mat);
- gexit();
- exit(0);
- }
-
-
- void do_rightmouse()
- {
- ask_quit = 0;
-
- dopup(main_menu);
-
- if(ask_quit)
- dopup(quit_menu);
-
- }
-
- void display()
- {
- ShowFast();
- swapbuffers();
- }
-
- void do_resize(){
-
- lda = y_size + 1;
-
- if( mat){
- mat = (float *)realloc( mat, lda * x_size * sizeof(float));
- copy = (float *)realloc( copy, lda * x_size * sizeof(float));
- power = (float *)realloc( power, lda * x_size * sizeof(float));
- filter = (float *)realloc( filter, lda * x_size * sizeof(float));
- mycolors = (int *)realloc( mycolors, lda * x_size * sizeof(int));
- save = (float *)realloc( save, (y_size + 3*x_size + 4 * FACTOR_SPACE) * sizeof(float));
- }
- else{
- mat = (float *)malloc( lda * x_size * sizeof(float));
- copy = (float *)malloc( lda * x_size * sizeof(float));
- power = (float *)malloc( lda * x_size * sizeof(float));
- filter = (float *)malloc( lda * x_size * sizeof(float));
- mycolors = (int *)malloc( lda * x_size * sizeof(int));
- save = (float *)malloc( (y_size + 3*x_size + 4 * FACTOR_SPACE) * sizeof(float));
- }
- this_deltat = 1;
- this_time = 0;
-
- matgen();
- inifil();
- main_menu = forward_menu;
- sprintf(title,"Time T = %d", this_time);
- display();
- }
- void add_noise() {
-
- matnoise_( mat, &lda, &y_size, &x_size);
- matcolor_(&y_size, &x_size, mat, mycolors, &lda);
- sprintf(title,(main_menu == forward_menu) ? "XY domain" : "FOURIER domain");
- display();
- }
-
-
- void size256_256(){
-
- x_size = 256;
- y_size = 256;
- do_resize();
- }
-
- void size288_300(){
-
- x_size = 288;
- y_size = 300;
- do_resize();
- }
-
- void size512_512(){
-
- x_size = 512;
- y_size = 512;
- do_resize();
- }
-
- void dir_fft(float *mat){
- float alpha = 1./(float)(x_size*y_size);
-
- this_t = second();
- sfft2d( -1, y_size, x_size, mat, lda, save);
- this_t = second() - this_t;
- }
- void inv_fft(float *mat){
-
- this_t = second();
- sfft2d( 1, y_size, x_size, mat, lda, save);
- this_t = second() - this_t;
- }
-
- void this_quit(){
-
- ask_quit = 1;
- }
- void InitFast(){
-
- this_proc = mp_numthreads_();
- x_size = 512;
- y_size = 512;
- }
- #define SCALE 20
- #define XLENGTH 19*SCALE
- #define YLENGTH 11*SCALE
-
- void myrec( float *pt, int xl, int yl, double val){
- int i, j;
- for( j = 0 ; j < xl; j++, pt += lda)
- for( i = 0; i < yl ; i++)
- pt[i] += val;
- }
- myF( float *pt, double val){
- myrec( pt, SCALE, 7*SCALE, val);
- myrec( pt+SCALE*lda+3*SCALE, 2*SCALE, SCALE, val);
- myrec( pt+SCALE*lda+6*SCALE, 3*SCALE, SCALE, val);
- }
- myT( float *pt, double val){
- myrec( pt+0*SCALE*lda+6*SCALE, 5*SCALE, 1*SCALE, val);
- myrec( pt+2*SCALE*lda+0*SCALE, 1*SCALE, 6*SCALE, val);
- }
- void mywrite( float *pt){
- myrec( pt, XLENGTH, YLENGTH, 1.);
- myF( pt + 2 * SCALE + 2 * SCALE*lda, -2.);
- myF( pt + 2 * SCALE + 7 * SCALE*lda, -2.);
- myT( pt + 2 * SCALE + 12 * SCALE*lda, -2.);
- }
-
- void add_stick(){
- int xor, yor;
- xor = x_size/2 - XLENGTH/2;
- yor = y_size/2;
- myrec(mat+yor+xor*lda, XLENGTH, SCALE, 1.);
- }
- void add_box(){
- int xor, yor;
- xor = x_size/2 - XLENGTH/2;
- yor = y_size/2 - YLENGTH/2;
- mywrite( mat+yor+xor*lda);
- }
- void add_circle(){
- int i,j;
- int xor, yor;
- float radius, width, dx, dy, z;
-
- radius = MIN( x_size, y_size) / 7;
- width = radius/4;
- width = width * width;
-
- xor = x_size/2;
- yor = y_size/2;
-
- for(i = 0; i < x_size; i++){
- dx = i - xor;
- for(j = 0; j < y_size; j++){
- dy = j - yor;
- z = dx * dx + dy * dy;
- z = sqrt(z) - radius;
- z = z * z;
- mat[i*lda+j] = width / (z + width);
- }
- }
- }
- #define X_OFFSET 7
- #define Y_OFFSET 13
-
- void inifil(){
- float alpha = 1./(float)(x_size*y_size);
- bzero( filter, lda * x_size * sizeof(float));
- filter[X_OFFSET*lda + Y_OFFSET] = 1.0;
- sfft2d( -1, y_size, x_size, filter, lda, save);
- /*
- inifil_(filter, &lda, &x_size, &y_size);
- sscal2d_( &y_size, &x_size, &alpha, mat, &lda);
- */
- }
- void matgen(){
- float alpha = 1./(float)(x_size*y_size);
- bzero( mat, lda * x_size * sizeof(float));
- /*
- add_box();
- add_stick();
- inv_fft(mat);
- matcolor_(&y_size, &x_size, mat, mycolors, &lda);
- display();
- */
- add_circle_( &y_size, &x_size, mat, &lda);
-
- sfft2di( y_size, x_size, save);
- matcolor_(&y_size, &x_size, mat, mycolors, &lda);
- display();
- sscal2d_( &y_size, &x_size, &alpha, mat, &lda);
- dir_fft(mat);
-
- do_propagate = 0;
- }
-
- void propagate(){
- sprod2d( y_size, x_size, mat, lda, filter, lda);
- bcopy( mat, copy, x_size*lda*sizeof(float));
- inv_fft(copy);
- matcolor_(&y_size, &x_size, copy, mycolors, &lda);
- this_time += this_deltat;
- sprintf(title,"Time T = %d, 2D FFT done in %.2f Sec.", this_time, this_t);
- display();
- do_propagate ++;
- }
-
- void faster(){
- if( this_time % (2*this_deltat))
- propagate();
- sprod2d( y_size, x_size, filter, lda, filter, lda);
- this_deltat *= 2.;
- }
-
- void reverse(){
- reverse_(filter, &lda, &y_size, &x_size);
- this_deltat *= -1;
- propagate();
- }
-
- void do_power(){
- matpower_(&y_size, &x_size, mat, power, &lda);
- matlog_(&y_size, &x_size, power, &lda);
- matcolor_(&y_size, &x_size, power, mycolors, &lda);
- sprintf(title,"Power Spectrum (Log Scale)");
- display();
- }
-
- void do_fourier(){
- matpower_(&y_size, &x_size, mat, power, &lda);
- matcolor_(&y_size, &x_size, power, mycolors, &lda);
- sprintf(title,"Power Spectrum");
- display();
- }
- void do_filter(){
- bcopy( filter, mat, lda*x_size*sizeof(float));
- matcolor_(&y_size, &x_size, mat, mycolors, &lda);
- sprintf(title,"Power Spectrum");
- display();
- }
- void do_filpow(){
- bcopy( filter, power, lda*x_size*sizeof(float));
- matpower_(&y_size, &x_size, power, power, &lda);
- matcolor_(&y_size, &x_size, power, mycolors, &lda);
- sprintf(title,"Power Spectrum");
- display();
- }
-