-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
1988 lines (1648 loc) · 76.2 KB
/
draw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* NeuralNet - draw.c
* Version 2.0
* by Eugene Hodges, IV (ewhodges@eos.ncsu.edu)
*
* Copyright 1992, 1993 by Eugene Hodges
* All Rights Reserved
*
* Permission to use, copy, and distribute this software and text for
* non-commercial purposes and without fee is hereby granted, provided
* that this notice appears in all copies.
*
* The author disclaims all warranties with regard to the software or
* text including all implied warranties of merchantability and fitness.
*
* In no event shall the author or NCSU be liable for any special,
* indirect or cosequential damages or any damages whatsoever
* resulting from loss of use, data or profits, whether in an
* action of contract, negligence or other tortious action,
* arising out of or in connection with the use or performance
* of this software or text.
*
*/
/* header file for this file */
#include "draw.h"
/* header file for for ai files */
#include "header.h"
/* include bitmaps (cursor and mask) for custom cursors--Not used. Left for the future?
#include "one.cursor"
#include "two.cursor"
#include "three.cursor"
#include "four.cursor"
#include "one_mask"
#include "two_mask"
#include "three_mask"
#include "four_mask" */
/* classifications of nodes */
#define INPUT 1
#define HID1 2
#define HID2 3
#define HID3 4
#define OUTPUT 5
/* the numbers aren't important, just that they're distinct for use in callbacks */
#define OK 1
#define CANCEL 2
/* the max num of links from one layer to the next that will be drawn before a
trapezoid is drawn instead */
#define MAX_DRAW_LINES 1000
/* Min and Max macros */
#define Min(x,y) ((x < y) ? (x) : (y))
#define Max(x,y) ((x > y) ? (x) : (y))
/* external functions and variables (in interface.c) */
extern void create_net_save_dialog();
extern void wts_menu_settings();
extern Widget toplevel,
main_form,
scroll,
net_save_dialog,
weights_save_option,
wts_file_text,
total_iter_text,
tss_text,
network_save_option,
network_disconnect_option,
pattern_test_option;
extern int num_hid_layers,
num_in_nodes,
num_out_nodes,
num_hid_nodes[3],
total_iterations;
extern XmStringCharSet char_set;
extern Boolean disconnect,
net_file_open,
wts_file_open,
net_changed;
/* Global and file declarations. */
Widget drawing_area,
ques_dialog;
GC draw_gc, /* regular drawing gc */
title_gc, /* gc for drawing title */
highlight_gc, /* xor gc for selecting/unselecting nodes */
eraser_gc, /* erases lines in disconnection */
copy_gc, /* for copying from net_pix to drawing_area (has depth
of screen) */
da_highlight_gc; /* for doing highlighting in drawing area (has depth of
screen) */
int da_height, /* height of the drawing area (from its resource) */
da_width, /* width of the drawing area (from its resource) */
slider_value, /* value of the scroll bar */
pix_height; /* height of the pixmap net_pix */
static int foreground, /* foreground color of the drawing area */
background, /* background color of the drawing area */
depth, /* depth of the display */
largest, /* largest number of nodes in any layer */
total_circles; /* total number of nodes in the network */
static Window root_window; /* root window id */
/* Not used. Future? */
/* static Cursor one_cursor, two_cursor, three_cursor, four_cursor; */
Pixmap net_pix; /* pixmap of network with drawing, etc. */
static XFontStruct *title_font, /* drawing title font */
*draw_font; /* font for numbers in node circles */
Boolean draw=False, /* true when the user is creating a network and
when the modification is allowed (i.e. not
while other things are being calculated, etc.) */
reset=False, /* true when the node_monitor function is to be reset
(when network closed) */
net_complete=False; /* true only when all nodes are connected and
signifies the net is complete and can be
trained and/or saved */
Node *node_info; /* array of Node in which an index is a node
number; nodes are numbered consecutively
beginning at 0 for the first input node */
static NetList *net_list=NULL, /* single linked list in which each list element
represents a line from the .net file format's
'network:' section */
*temp=NULL; /* temporary variable used to go into linked list */
char new_net_filename[200]; /* current network file name (.net) */
/* callback for disconnect question dialog. */
static void quesCB(Widget w, int client_data, XmAnyCallbackStruct *call_data)
{
switch(client_data)
{
case OK:
/* go ahead and disconnect highlighted nodes */
disconnect_nodes(NULL, 2);
break;
case CANCEL:
/* cancel disconnect operation */
disconnect_nodes(NULL, 3);
break;
}
XtUnmanageChild(w);
}
/* question dialog which asks user to if it is ok to disconnect highlighted nodes */
void create_question_dialog()
{
Arg al[4];
int ac;
ac=0;
XtSetArg(al[ac], XmNdialogTitle,XmStringCreateLtoR(
"NeuralNet: Question Dialog", XmSTRING_DEFAULT_CHARSET)); ac++;
XtSetArg(al[ac], XmNmessageAlignment, XmALIGNMENT_CENTER); ac++;
ques_dialog = XmCreateQuestionDialog(toplevel,"ques_dialog", al, ac);
XtAddCallback(ques_dialog, XmNokCallback, quesCB, OK);
XtAddCallback(ques_dialog, XmNcancelCallback, quesCB, CANCEL);
XtUnmanageChild(XmMessageBoxGetChild(ques_dialog, XmDIALOG_HELP_BUTTON));
}
/* sets the messageString resource of the disconnect question dialog and manages it */
void question (char *s)
{
Arg al[2];
int ac;
XmString question;
question=XmStringCreateLtoR(s, char_set);
ac = 0;
XtSetArg(al[ac], XmNmessageString, question); ac++;
XtSetValues(ques_dialog,al,ac);
XmStringFree(question);
XtManageChild(ques_dialog);
}
/* Gets the following information about the display:
foreground and background colors
root window of screen
depth of screen */
void get_display_info()
{
Arg al[10];
int ac;
/* get the current fg and bg colors. */
ac=0;
XtSetArg(al[ac], XmNforeground, &foreground); ac++;
XtSetArg(al[ac], XmNbackground, &background); ac++;
XtGetValues(drawing_area, al, ac);
root_window=RootWindowOfScreen(XtScreen(drawing_area));
depth=DefaultDepthOfScreen(XtScreen(drawing_area));
}
/* Creates and initializes the graphics contexts (gc's) used by the program */
void setup_gcs()
/* set up the graphics contexts */
{
XGCValues vals;
Pixmap p;
p=XCreatePixmap(XtDisplay(drawing_area), root_window, 1, 1, 1);
vals.plane_mask = 0x1L;
/* set the gc values and create the drawing gc */
vals.foreground = foreground;
vals.background = background;
draw_font=XLoadQueryFont(XtDisplay(drawing_area),
"-Adobe-Helvetica-Medium-r-Normal--10-100-*");
vals.font = draw_font->fid;
draw_gc=XCreateGC(XtDisplay(drawing_area), p, GCFont | GCForeground | GCBackground |
GCPlaneMask, &vals);
copy_gc=XCreateGC(XtDisplay(drawing_area), XtWindow(drawing_area), GCFont | GCForeground
| GCBackground, &vals);
/* set the gc values and create the eraser (fg=bg) gc */
vals.foreground = 0;
vals.background = 0;
eraser_gc=XCreateGC(XtDisplay(drawing_area), p, GCForeground | GCBackground |
GCPlaneMask, &vals);
/* set the gc values and create the highlight (xor) gc */
vals.foreground = 1;
vals.background = 0;
vals.font = draw_font->fid;
vals.function = GXxor;
highlight_gc=XCreateGC(XtDisplay(drawing_area), p, GCFunction | GCFont | GCForeground |
GCBackground | GCPlaneMask, &vals);
vals.foreground = foreground ^ background;
vals.background = background;
da_highlight_gc=XCreateGC(XtDisplay(drawing_area), XtWindow(drawing_area), GCFunction |
GCFont | GCForeground | GCBackground, &vals);
/* set the gc values and create the title gc (only to draw title text) */
vals.foreground = 1;
vals.background = 0;
title_font=XLoadQueryFont(XtDisplay(drawing_area),
"-Adobe-Helvetica-Bold-r-Normal--12-120-*");
vals.font = title_font->fid;
title_gc=XCreateGC(XtDisplay(drawing_area), p, GCFont | GCForeground | GCBackground |
GCPlaneMask, &vals);
}
/* Handles the resizing of the drawing area widget (drawing_area); reconstructs
the pixmap for the new window size and redraws all the information on it */
void da_resizeCB(Widget w, caddr_t client_data, XmDrawingAreaCallbackStruct *call_data)
{
Arg al[10];
int ac,i;
Dimension temp_width, temp_height;
Pixmap temp1,temp2;
Node *temp_node_info;
if (XtIsRealized && draw)
{
/* get new height, width */
temp_height=da_height;
ac=0;
XtSetArg(al[ac], XmNheight, &da_height); ac++;
XtSetArg(al[ac], XmNwidth, &temp_width); ac++;
XtGetValues(drawing_area, al, ac);
/* if (((temp_value + da_height)>pix_height) && (pix_height>da_height))
slider_value=pix_height-da_height;
else
slider_value=temp_value;
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
slider_value+TopMargin, da_width, da_height-TopMargin, 0, TopMargin, 1);
*/
/* make appropriate changes to scroll bar */
ac=0;
XtSetArg(al[ac], XmNsliderSize, da_height); ac++;
XtSetArg(al[ac], XmNpageIncrement, da_height); ac++;
XtSetValues(scroll, al, ac);
/* if the width has changed, copy the input and output values, recreate
the network on a new pixmap, and copy the input and output values to the
new pixmap */
if (temp_width != da_width)
{
/* copy the input and output values */
temp1 = XCreatePixmap (XtDisplay(drawing_area), root_window, SideMargin,
pix_height, 1);
temp2 = XCreatePixmap (XtDisplay(drawing_area), root_window, SideMargin,
pix_height, 1);
XCopyPlane(XtDisplay(drawing_area), net_pix, temp1, draw_gc, 0, 0, SideMargin,
pix_height, 0, 0, 1);
XCopyPlane(XtDisplay(drawing_area), net_pix, temp2, draw_gc, da_width-SideMargin,
0, SideMargin, pix_height, 0, 0, 1);
/* recreate network pixmap, freeing old one, and don't draw to screen until
completely finished */
draw=False;
temp_node_info=node_info;
XFreePixmap(XtDisplay(drawing_area), net_pix);
set_up_net();
draw_links();
/* put connection info back into node_info and free old node info
(temp_node_info) */
for(i=0; i < total_circles; i++)
{
node_info[i].in_connected=temp_node_info[i].in_connected;
node_info[i].out_connected=temp_node_info[i].out_connected;
}
free(temp_node_info);
/* redraw input and output values to the pixmap here */
XCopyPlane(XtDisplay(drawing_area), temp1, net_pix, draw_gc, 0, 0, SideMargin,
pix_height, 0, 0, 1);
XCopyPlane(XtDisplay(drawing_area), temp2, net_pix, draw_gc, 0, 0, SideMargin,
pix_height, da_width-SideMargin, 0, 1);
XFreePixmap(XtDisplay(drawing_area), temp1);
XFreePixmap(XtDisplay(drawing_area), temp2);
}
}
}
/* Handles the expose event for the drawing area widget (drawing_area) by
copying from the pixmap the exposed area to the window */
void da_exposeCB(Widget w, caddr_t client_data, XmDrawingAreaCallbackStruct *call_data)
{
XExposeEvent *event = (XExposeEvent *) call_data->event;
if (XtIsRealized && draw)
{
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, event->x,
event->y+slider_value, event->width, event->height, event->x, event->y, 1);
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
0, da_width, TopMargin, 0, 0, 1);
}
}
/* Not used.
void create_cursors()
{
one_cursor=XbCreateCursor(drawing_area, one_bits, one_mask_bits, one_width,
one_height, one_x_hot, one_y_hot);
two_cursor=XbCreateCursor(drawing_area, two_bits, two_mask_bits, two_width,
two_height, two_x_hot, two_y_hot);
three_cursor=XbCreateCursor(drawing_area, three_bits, three_mask_bits, three_width,
three_height, three_x_hot, three_y_hot);
four_cursor=XbCreateCursor(drawing_area, four_bits, four_mask_bits, four_width,
four_height, four_x_hot, four_y_hot);
}
*/
/* Handles the callback from the scrollbar (valueChanged, drag) */
void scrollCB(Widget w, XtPointer client_data, XmScrollBarCallbackStruct *call_data)
{
slider_value=call_data->value;
if (draw)
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
slider_value+TopMargin, da_width, da_height-TopMargin, 0, TopMargin, 1);
}
/* draws a string centered about the coordinates x, y */
void draw_string_centered(Widget w, Window drawable, GC gc, int x, int y, char *str,
XFontStruct *font)
{
int str_width;
/* get the font characteristics and compute offset */
str_width=XTextWidth(font, str, strlen(str));
x-=str_width/2;
y+=(font->ascent)/2;
XDrawString(XtDisplay(w), drawable, gc, x, y, str, strlen(str));
}
/* Constructs an unconnected layout of nodes on the pixmap from the following parameters
(obtained from the 'Network -> New' menu option (dialog)):
number of input nodes (num_in_nodes)
number of output nodes (num_out_nodes)
number of hidden layers (num_hid_layers)
number of nodes in each hidden layer (num_hid_nodes[3])
The layout of nodes is centered according to the layer with the highest number of
nodes and the following visual characteristics of the layout:
BlockHeight 60 -- height of node circle plus border area (20 + 20 +20)
TopMargin 25 -- margin where the title (Values, etc) is written
NodeRadius 10 -- the radius of a node circle
SideMargin 80 -- margin where the input and output values are shown
Also, the array node_info (of type Node) is initialized with the info about each
node (x and y coordinates of node center, in_connected and out_connected set to
False except for input layer nodes which have in_connected initialized to True and
output layer nodes which have out_connected to True for the obvious reasons). */
void set_up_net()
{
Arg al[10];
int ac, i, start_y, cnt=0;
Dimension orig_width, width, height;
char temp[10];
XArc *circles;
XGCValues vals;
XFontStruct *font;
int str_width;
XbWatchCursor(toplevel);
/* find the largest number of nodes in any layer (for centering purposes) */
if (num_in_nodes>num_out_nodes)
largest=num_in_nodes;
else
largest=num_out_nodes;
for (i=0;i<3;i++)
if (largest<num_hid_nodes[i])
largest=num_hid_nodes[i];
/* compute total number of nodes and allocate a XArc structure to store the
characteristics of each circle */
total_circles=num_in_nodes + num_out_nodes + num_hid_nodes[0] +
num_hid_nodes[1] + num_hid_nodes[2];
circles=(XArc *)malloc(total_circles * sizeof(XArc));
/* allocate the node_info array with one index per node in network */
node_info=(Node *)malloc(total_circles * sizeof(Node));
/* get the current height and width of the drawing area */
ac=0;
XtSetArg(al[ac], XmNwidth, &orig_width); ac++;
XtSetArg(al[ac], XmNheight, &height); ac++;
XtGetValues(drawing_area, al, ac);
da_width=width=orig_width;
da_height=height;
/* compute the height of the pixmap */
pix_height=largest*BlockHeight+TopMargin;
/* set the appropriate resources of the scroll bar to properly reflect drawing area
and pixmap heights */
ac=0;
XtSetArg(al[ac], XmNmaximum, (pix_height < height) ? height : pix_height); ac++;
XtSetArg(al[ac], XmNsliderSize, height ); ac++;
XtSetArg(al[ac], XmNpageIncrement, height); ac++;
XtSetValues(scroll, al, ac);
/* allocate the pixmap net_pix and clear it */
net_pix = XCreatePixmap(XtDisplay(drawing_area), root_window, width, pix_height, 1);
XFillRectangle(XtDisplay(drawing_area), net_pix, eraser_gc, 0, 0, width, pix_height);
draw=True;
/* draw the layer titles in the title */
XGetGCValues(XtDisplay(drawing_area), title_gc, GCFont, &vals);
font=XQueryFont(XtDisplay(drawing_area),vals.font);
draw_string_centered(drawing_area, net_pix, title_gc, SideMargin, 10, "Input", font);
draw_string_centered(drawing_area, net_pix, title_gc, width/2, 10, "Hidden", font);
draw_string_centered(drawing_area, net_pix, title_gc, width-SideMargin, 10, "Output",
font);
/* center the first Values in the left side margin and the second Values in right
side margin */
str_width=XTextWidth(font, "Values", strlen("Values"));
XDrawString(XtDisplay(drawing_area), net_pix, title_gc, 5, 10+(font->ascent)/2, "Values",
strlen("Values"));
XDrawString(XtDisplay(drawing_area), net_pix, title_gc, width-str_width-5,
10+(font->ascent)/2, "Values", strlen("Values"));
/* draw line under title */
XDrawLine(XtDisplay(drawing_area), net_pix, draw_gc, 0, 15+(font->ascent)/2, width,
15+(font->ascent)/2);
XFreeFontInfo(NULL, font, 1);
XGetGCValues(XtDisplay(drawing_area), draw_gc, GCFont, &vals);
font=XQueryFont(XtDisplay(drawing_area),vals.font);
/* compute the starting y coordinate for the first input node and then draw the input
nodes in the first column of nodes; initialize the node_info array appropriately;
number the nodes */
start_y=(pix_height - TopMargin - (num_in_nodes * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_in_nodes;i++,cnt++)
{
circles[cnt].x=(short )(SideMargin-NodeRadius);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short )0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=True;
node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, SideMargin, start_y+i*BlockHeight, temp, font);
}
if (num_hid_layers==1)
{
/* adjust width so that round off error will not affect appearance (width now a
multiple of 2) */
width=(orig_width/2)*2;
/* compute the starting y coordinate for the first node in the only middle layer and
then draw the nodes in the middle column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_hid_nodes[0] * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_hid_nodes[0];i++,cnt++)
{
circles[cnt].x=(short )((width/2)-NodeRadius);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=width/2;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, width/2, start_y+i*BlockHeight, temp, font);
}
}
if (num_hid_layers==2)
{
/* adjust width so that round off error will not affect appearance (width now a
multiple of 3) */
width=(orig_width/3)*3;
/* compute the starting y coordinate for the first node in the first middle layer and
then draw the nodes in the second column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_hid_nodes[0] * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_hid_nodes[0];i++,cnt++)
{
circles[cnt].x=(short )(((width-150)/3)-NodeRadius+SideMargin);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=(width-150)/3 + SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, (width-150)/3+SideMargin, start_y+i*BlockHeight, temp, font);
}
/* compute the starting y coordinate for the first node in the second middle layer and
then draw the nodes in the third column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_hid_nodes[1] * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_hid_nodes[1];i++,cnt++)
{
circles[cnt].x=(short )((2*(width-150)/3)-NodeRadius+SideMargin);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=2*(width-150)/3 + SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, 2*(width-150)/3+SideMargin, start_y+i*BlockHeight, temp, font);
}
}
if (num_hid_layers==3)
{
/* adjust width so that round off error will not affect appearance (width now a
multiple of 4) */
width=(orig_width/4)*4;
/* compute the starting y coordinate for the first node in the first middle layer and
then draw the nodes in the second column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_hid_nodes[0] * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_hid_nodes[0];i++,cnt++)
{
circles[cnt].x=(short )((width-150)/4-NodeRadius+SideMargin);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=(width-150)/4 + SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, (width-150)/4+SideMargin, start_y+i*BlockHeight, temp, font);
}
/* compute the starting y coordinate for the first node in the second middle layer and
then draw the nodes in the third column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_hid_nodes[1] * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_hid_nodes[1];i++,cnt++)
{
circles[cnt].x=(short )((2*(width-150)/4)-NodeRadius+SideMargin);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=2*(width-150)/4 + SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, 2*(width-150)/4+SideMargin, start_y+i*BlockHeight, temp, font);
}
/* compute the starting y coordinate for the first node in the third middle layer and
then draw the nodes in the fourth column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_hid_nodes[2] * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_hid_nodes[2];i++,cnt++)
{
circles[cnt].x=(short )((3*(width-150)/4)-NodeRadius+SideMargin);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=3*(width-150)/4 + SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=node_info[cnt].out_connected=False;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, 3*(width-150)/4+SideMargin, start_y+i*BlockHeight, temp, font);
}
}
/* compute the starting y coordinate for the first node in the output layer and
then draw the nodes in the last column of nodes; initialize the node_info
array appropriately; number the nodes */
start_y=(pix_height - TopMargin - (num_out_nodes * BlockHeight))/2 + TopMargin + BlockHeight/2;
for (i=0;i<num_out_nodes;i++,cnt++)
{
circles[cnt].x=(short )(orig_width-SideMargin-NodeRadius);
circles[cnt].y=(short )(start_y+i*BlockHeight-NodeRadius);
circles[cnt].width=circles[cnt].height=(unsigned short )(NodeRadius*2);
circles[cnt].angle1=(short)0;
circles[cnt].angle2=(short )(360*64);
node_info[cnt].center_x=orig_width - SideMargin;
node_info[cnt].center_y=start_y + i*BlockHeight;
node_info[cnt].in_connected=False;
node_info[cnt].out_connected=True;
sprintf(temp,"%d",cnt);
draw_string_centered(drawing_area, net_pix, draw_gc, orig_width-SideMargin, start_y+i*BlockHeight, temp, font);
}
XFreeFontInfo(NULL, font, 1);
/* draw the circles (nodes) onto the pixmap */
XDrawArcs(XtDisplay(drawing_area), net_pix, draw_gc, circles, total_circles);
free(circles);
/* clear the drawing area and generate an expose event to display network */
XClearArea(XtDisplay(drawing_area), XtWindow(drawing_area), 0, 0, 0, 0, TRUE);
XbNormalCursor(toplevel);
}
/* Not used
void cursor_monitor(Widget w, caddr_t data, XEvent *event)
{
printf("x: %d, y: %d\n", event->xmotion.x, event->xmotion.y);
}
*/
/* returns the layer of a node given the node number; used in checking to make sure layers
are connected from left to right */
int layer(int node_num)
{
if (num_hid_nodes[1]==0)
return( (node_num < num_in_nodes) ? INPUT :
(node_num < num_in_nodes + num_hid_nodes[0]) ? HID1 : 3);
if (num_hid_nodes[2]==0)
return( (node_num < num_in_nodes) ? INPUT :
(node_num < num_in_nodes + num_hid_nodes[0]) ? HID1 :
(node_num < num_in_nodes + num_hid_nodes[0] + num_hid_nodes[1]) ? HID2 : 4 );
else
return( (node_num < num_in_nodes) ? INPUT :
(node_num < num_in_nodes + num_hid_nodes[0]) ? HID1 :
(node_num < num_in_nodes + num_hid_nodes[0] + num_hid_nodes[1]) ? HID2 :
(node_num < num_in_nodes + num_hid_nodes[0] + num_hid_nodes[1] + num_hid_nodes[2]) ? HID3 : OUTPUT);
}
/* Disconnects the nodes selected through the use of the 'Network -> Disconnect'
menu option. The nodes disconnected are all the nodes in a line of the .net
file format (which is the equivalent of one element in the single linked list
net_list. */
void disconnect_nodes(int i, int choice)
{
static NetList *curr=NULL;
static int j,k,cnt,num_circles;
static XArc *circles;
XSegment *lines;
XPoint vertices[4];
switch(choice)
{
case(1):
/* 'Network -> Disconnect just chosen */
/* display error message and return if no nodes connected */
if (!net_list)
{
XbNormalCursor(drawing_area);
XbError("There are no connected nodes.");
disconnect=False;
return;
}
/* find the appropriate element in single linked list (represents a
line in the 'network:' section of the .net file) */
for(curr=net_list;curr;curr=curr->next)
if ((i<=curr->end_out) && (i>=curr->begin_out))
break;
/* allocate space for all the circles to be highlighted */
circles=(XArc *)malloc(sizeof(XArc)*((curr->end_out-curr->begin_out+1)+
(curr->end_in-curr->begin_in+1)));
/* store all of the nodes sending output that are to be highlighted */
for(k=0,j=curr->begin_out;j<=curr->end_out;j++,k++)
{
circles[k].x=(short )(node_info[j].center_x-NodeRadius);
circles[k].y=(short )(node_info[j].center_y-NodeRadius);
circles[k].width=circles[k].height=(unsigned short )(2*NodeRadius);
circles[k].angle1=(short )0;
circles[k].angle2=(short )360*64;
}
/* store all of the nodes receiving input that are to be highlighted */
for(j=curr->begin_in;j<=curr->end_in;j++,k++)
{
circles[k].x=(short )(node_info[j].center_x-NodeRadius);
circles[k].y=(short )(node_info[j].center_y-NodeRadius);
circles[k].width=circles[k].height=(unsigned short )(2*NodeRadius);
circles[k].angle1=(short )0;
circles[k].angle2=(short )360*64;
}
/* record the number of circles highlighted */
num_circles=k;
/* highlight the circles on the pixmap and then copy it to the display */
XFillArcs(XtDisplay(drawing_area), net_pix, highlight_gc, circles, num_circles);
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
TopMargin+slider_value, da_width, da_height-TopMargin, 0, TopMargin, 1);
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
0, da_width, TopMargin, 0, 0, 1);
XbNormalCursor(drawing_area);
/* give the user a chance to change mind */
question("Okay to disconnect selected nodes?");
break;
case(2):
/* go ahead with disconnection */
/* unhighlight the hightlighted nodes */
XFillArcs(XtDisplay(drawing_area), net_pix, highlight_gc, circles, num_circles);
free(circles);
if (((curr->end_out - curr->begin_out + 1) *
(curr->end_in - curr->begin_in + 1)) <= MAX_DRAW_LINES)
{
/* allocate storage for the links to be erased */
lines=(XSegment *)malloc(((curr->end_out)-(curr->begin_out)+1)*
((curr->end_in)-(curr->begin_in)+1)*sizeof(XSegment));
/* store the info for the lines to be erased */
for(cnt=0,j=curr->begin_out;j<=(curr->end_out);j++)
for(k=curr->begin_in;k<=(curr->end_in);k++,cnt++)
{
lines[cnt].x1=(short )(node_info[j].center_x + NodeRadius + 1);
lines[cnt].y1=(short )(node_info[j].center_y);
lines[cnt].x2=(short )(node_info[k].center_x - NodeRadius - 1);
lines[cnt].y2=(short )(node_info[k].center_y);
}
XDrawSegments(XtDisplay(drawing_area), net_pix, eraser_gc, lines,
((curr->end_out)-(curr->begin_out)+1)*(curr->end_in-curr->begin_in+1));
free(lines);
}
else
{
vertices[0].x=(short )(node_info[curr->end_out].center_x + NodeRadius + 1);
vertices[0].y=(short )(node_info[curr->end_out].center_y);
vertices[1].x=(short )(node_info[curr->begin_out].center_x + NodeRadius + 1);
vertices[1].y=(short )(node_info[curr->begin_out].center_y);
vertices[2].x=(short )(node_info[curr->begin_in].center_x - NodeRadius - 1);
vertices[2].y=(short )(node_info[curr->begin_in].center_y);
vertices[3].x=(short )(node_info[curr->end_in].center_x - NodeRadius - 1);
vertices[3].y=(short )(node_info[curr->end_in].center_y);
XFillPolygon(XtDisplay(drawing_area), net_pix, eraser_gc, vertices, 4,
Convex, CoordModeOrigin);
}
/* update data: unconnect the output of the nodes that were sending output
and unconnect the input of the nodes that were receiving input */
for(k=curr->begin_out;k<=curr->end_out;k++)
node_info[k].out_connected=False;
for(k=curr->begin_in;k<=curr->end_in;k++)
node_info[k].in_connected=False;
/* redraw the nodes in the operation to restore pixel lost in the circle; free
the memory allocations */
/* delete nodes from net_list */
delete_node(&curr);
/* signify disconnect operation complete; set other status variables
appropriately */
disconnect=False;
net_complete=False;
net_changed=True;
XtSetSensitive(network_save_option,False);
XtSetSensitive(pattern_test_option,False);
wts_file_open=False;
wts_menu_settings();
XmTextSetString(wts_file_text,"No file open.");
/* copy changes to the screen */
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
TopMargin+slider_value, da_width, da_height-TopMargin, 0, TopMargin, 1);
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
0, da_width, TopMargin, 0, 0, 1);
break;
case(3):
/* cancel disconnect operation */
/* unhighlight the highlighted nodes */
XFillArcs(XtDisplay(drawing_area), net_pix, highlight_gc, circles, num_circles);
free(circles);
/* copy changes to the screen */
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
TopMargin+slider_value, da_width, da_height-TopMargin, 0, TopMargin, 1);
XCopyPlane(XtDisplay(drawing_area), net_pix, XtWindow(drawing_area), copy_gc, 0,
0, da_width, TopMargin, 0, 0, 1);
/* signify disconnect operation complete (cancelled in this case) */
disconnect=False;
break;
}
}
/* Monitors the button clicks and makes the appropriate action. For the first
button it allows the nodes to be connected whenever a network is being created.
For the second button, it allows a node to be unselected (if it was the previous
choice through the first button being clicked). In both cases it determines the
number of the node clicked on and checks the node_info array for information to
exclude all but valid nodes for clicking upon. It also decides if the click is
to select (highlight) or unselect (unhighlight) a node. This function is the
heart of network creation and modification. To understand more, read the
comments in the function. */
void node_monitor(Widget w, caddr_t data, XEvent *event)
{
int x, y, i, j, k, cnt, swap;
static int num_click=1, first_layer_sel, second_layer_sel, prev_sel=(-1), num_connected=0,
first_node_selected;
static XArc circles[3], circle;
XSegment *lines;
XPoint vertices[4];
static Boolean single_node_disc=True;
/* reset the status of node_monitor() for the 'Network -> Close' option */
if (reset)
{
num_click=1;
num_connected=0;
delete_sll();
net_complete=False;
reset=False;
return;
}
/* return if no drawing allowed or click was in a margin */
if (!draw || (event->xbutton.y<=TopMargin) || (event->xbutton.x<=(SideMargin-NodeRadius))
|| (event->xbutton.x>=(da_width-SideMargin+NodeRadius)))
return;
/* simplify references to click coordinates */
x=event->xbutton.x;
y=event->xbutton.y;
/* check to see if a node is selected */
for(i=0; i < total_circles; i++)
{
if ((x < node_info[i].center_x + NodeRadius) &&
(x > node_info[i].center_x - NodeRadius) &&
((y + slider_value) < node_info[i].center_y + NodeRadius) &&
((y + slider_value) > node_info[i].center_y - NodeRadius))
break;
}
/* no node clicked upon */
if (i==total_circles)
return;
/* for a disconnect operation */
if (disconnect)
{
if (node_info[i].out_connected && (i<(total_circles-num_out_nodes)))
{
/* show nodes selected by the click (highlight) */
disconnect_nodes(i,1);
return;
}
else
{
/* nothing to disconnect */
XbError("This node is not sending output to any other nodes.");
XbNormalCursor(drawing_area);
disconnect=False;
return;
}
}
/* allow unselecting of node highlighted on previous click through the use of second
mouse button; single_node_disc ensures that operation is only on previous node
selected */
if ((num_click!=1) && (Button2==(event->xbutton.button)) && (single_node_disc))
{
if (prev_sel==i) /* redundant check */
{
/* this make sure all cases properly taken care of; too hard to explain here,
but if you run the program and play around, it will become clear */
if (temp->begin_out!=i)
{
circle.x=(short )(node_info[i].center_x-NodeRadius);
circle.y=(short )(node_info[i].center_y-NodeRadius);
circle.width=circle.height=(unsigned short )(2*NodeRadius);
circle.angle1=(short )0;
circle.angle2=(short )360*64;
XFillArc(XtDisplay(drawing_area), net_pix, highlight_gc,
node_info[i].center_x - NodeRadius, node_info[i].center_y - NodeRadius,
2*NodeRadius, 2*NodeRadius, 0, 360*64);