| 1 |
/* |
| 2 |
* kali.c - Nina Amenta, Aug. 1989 |
| 3 |
* rewritten by Ed Chi, summer 1994 |
| 4 |
* |
| 5 |
* $Id: kali.c,v 1.20 1996/10/07 16:55:24 slevy Exp $ |
| 6 |
* $Log: kali.c,v $ |
| 7 |
* Revision 1.20 1996/10/07 16:55:24 slevy |
| 8 |
* Back-port to XForms 0.75 -- the only version available for Irix 4. |
| 9 |
* |
| 10 |
* Revision 1.19 1996/10/07 15:37:33 slevy |
| 11 |
* Remove SGI & VOGL dregs -- we're now pure X, using only XForms. |
| 12 |
* Consume outstanding events in idle callback -- it seems to get called |
| 13 |
* even when we're not really idle yet. |
| 14 |
* |
| 15 |
* Revision 1.18 1996/10/04 21:36:23 slevy |
| 16 |
* Finish porting to vogl-less mode. |
| 17 |
* Rotate correctly now that X has mirrored our coordinates. |
| 18 |
* Redraw only as an idle callback. (This doesn't seem to help responsiveness, |
| 19 |
* though.) |
| 20 |
* Toss KALIVOGL code. |
| 21 |
* |
| 22 |
* Revision 1.16 1996/09/28 15:04:46 slevy |
| 23 |
* Allow interaction with current line at all times; always show |
| 24 |
* which line is current by coloring. |
| 25 |
* Allow loading a file just by mentioning it on command line |
| 26 |
* (but still accept -k). |
| 27 |
* Reimplement move-closest-endpoint feature. |
| 28 |
* Move getsize() etc. here -- flqueue.c no longer needed. |
| 29 |
* |
| 30 |
* Revision 1.15 1996/06/11 06:18:05 slevy |
| 31 |
* Major changes. Make compatible with released XForms library. |
| 32 |
* Implement an object-style I/O structure, with "GL" (actually VOGL), |
| 33 |
* Postscript, and Pick devices. Implement picking in software, |
| 34 |
* not requiring help from GL. |
| 35 |
* Add keyboard shortcuts for mode selection: (z)oom, (d)raw, (m)ove, |
| 36 |
* (x)delete, (r)otate, (a)ngle, (R)atio. Also, backspace and delete keys |
| 37 |
* delete any selected line. |
| 38 |
* |
| 39 |
* Revision 1.14 1994/12/22 18:46:29 munzner |
| 40 |
* make sure to set the proper window at startup. |
| 41 |
* |
| 42 |
* Revision 1.13 1994/12/15 06:18:46 munzner |
| 43 |
* add options to load file, specify window placement on command line. |
| 44 |
* on SGI, use RGBmode not colormap mode to avoid flashing. |
| 45 |
* |
| 46 |
* Revision 1.12 1994/12/14 23:57:30 munzner |
| 47 |
* Ed Chi's changes: port to X11. |
| 48 |
* |
| 49 |
* Revision 1.2 1994/08/15 20:05:38 chi |
| 50 |
* significant code cleanup. |
| 51 |
* header files created. |
| 52 |
* main.h created |
| 53 |
* changed #defines in main.h from DRAW to KALIDRAW, CUT to .... etc |
| 54 |
*/ |
| 55 |
|
| 56 |
#include <stdio.h> |
| 57 |
#include <stdlib.h> |
| 58 |
#include <math.h> |
| 59 |
|
| 60 |
|
| 61 |
#define SET_COLOR cpack |
| 62 |
#define BACKCOLOR 0xFFFFFF /* 255,255,255 -- white */ |
| 63 |
#define POINTCOLOR 0x0 /* 0,0,0 -- black */ |
| 64 |
#define LINECOLOR 0xFF0000 /* 0,0,255 -- blue */ |
| 65 |
#define MOVECOLOR 0x8000FF /* 255,0,255 -- magenta */ |
| 66 |
#define DELETECOLOR 0x00FF00 /* 0,255,0 -- ugly green */ |
| 67 |
#define GRIDCOLOR 0x3F6DFF /* 255,109,63 -- yellow-green */ |
| 68 |
|
| 69 |
#include "forms.h" |
| 70 |
|
| 71 |
#include "main.h" |
| 72 |
#include "io.h" |
| 73 |
#include "symmetry.h" |
| 74 |
#include "kali.h" |
| 75 |
|
| 76 |
#ifndef TRUE |
| 77 |
#define TRUE 1 |
| 78 |
#endif |
| 79 |
|
| 80 |
#define NAMESTACKSIZE 50 |
| 81 |
|
| 82 |
#define STREQ(s1,s2) (strcmp(s1,s2)==0) |
| 83 |
|
| 84 |
/*------------------------------------------------ variable decl */ |
| 85 |
float zoom; /* used in callback.c */ |
| 86 |
RECTANGLE win_rect,sym_rect; /* used in callback.c */ |
| 87 |
RECTANGLE kwin, pwin; /* size & position for kali, panel windows */ |
| 88 |
int count; |
| 89 |
MATRIX xforms[5]; |
| 90 |
int sym_index; |
| 91 |
int pick_for; |
| 92 |
void (*xformfnc) (); |
| 93 |
int lit; /* a line is colored for delete or move */ |
| 94 |
WINDOW win; |
| 95 |
int mode; |
| 96 |
|
| 97 |
int debug=0; |
| 98 |
|
| 99 |
extern DRAWER GXDraw; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
/* the following three are used in handlers() and main() */ |
| 104 |
int oldxposition; |
| 105 |
POINT tmppoint; |
| 106 |
POINT refpoint; |
| 107 |
|
| 108 |
|
| 109 |
POINT *sym_pts=NULL; |
| 110 |
LINE *Lines = NULL; |
| 111 |
SYMMETRY sym_storage; |
| 112 |
SYMMETRY *sym = &sym_storage; |
| 113 |
|
| 114 |
int frieze = 0; /* wallpaper or frieze groups? */ |
| 115 |
int moron = 0; /* don't disable various buttons */ |
| 116 |
int GridDisplay = TRUE; |
| 117 |
void MapCursor(int x, int y, POINT *p, RECTANGLE *r, float zoom); |
| 118 |
|
| 119 |
|
| 120 |
/*--------------------------------------------------- event handlers */ |
| 121 |
|
| 122 |
void handleRedraw() |
| 123 |
{ |
| 124 |
reshapeviewport(); |
| 125 |
AdjustWindowRectangle(&win_rect); |
| 126 |
DefineSymWindow(&sym_rect,sym,&win_rect,zoom); |
| 127 |
count = SetUpSymmetry(sym,&sym_pts,xforms,&sym_rect,&win_rect); |
| 128 |
DrawCurrent(&sym_rect,Lines,sym,sym_pts,xforms,count,&GXDraw,GridDisplay); |
| 129 |
if(lit) |
| 130 |
HighLiteLine(&sym_rect, Lines, sym, sym_pts, xforms, count, MOVECOLOR); |
| 131 |
swapbuffers(); |
| 132 |
} |
| 133 |
|
| 134 |
static int scheduled = 0; |
| 135 |
|
| 136 |
int doRedraw(XEvent *ev, void *junk) |
| 137 |
{ |
| 138 |
XEvent newev; |
| 139 |
|
| 140 |
/* XForms documentation suggests that this idle callback is only called |
| 141 |
* when no events are pending for any window. This doesn't seem to be true, |
| 142 |
* so we force any outstanding clicks & drags to be processed now |
| 143 |
* before wasting time redrawing. |
| 144 |
*/ |
| 145 |
while(XCheckWindowEvent(fl_display, win, |
| 146 |
ButtonPressMask|ButtonMotionMask|ButtonReleaseMask|KeyPressMask, |
| 147 |
&newev)) { |
| 148 |
user_event_cb(&newev, NULL); |
| 149 |
} |
| 150 |
|
| 151 |
DrawCurrent(&sym_rect,Lines,sym,sym_pts,xforms,count,&GXDraw,GridDisplay); |
| 152 |
if(lit) |
| 153 |
HighLiteLine(&sym_rect, Lines, sym, sym_pts, xforms, count, MOVECOLOR); |
| 154 |
swapbuffers(); |
| 155 |
fl_set_idle_callback(NULL, NULL); |
| 156 |
scheduled = 0; |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
void scheduleRedraw() |
| 161 |
{ |
| 162 |
if(!scheduled) { |
| 163 |
fl_set_idle_callback(doRedraw, NULL); |
| 164 |
scheduled = 1; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
static char modechars[] = "?dcptmza/rD"; /* draw, cut, pick, transform, |
| 170 |
move, zoom, angle, /ratio, |
| 171 |
rotate, Delete */ |
| 172 |
|
| 173 |
|
| 174 |
void handleLeftmouse(int how, int x, int y) |
| 175 |
{ |
| 176 |
MapCursor(x, y, &tmppoint, &win_rect, zoom); |
| 177 |
|
| 178 |
if(how != 0) { /* Button pressed or dragged */ |
| 179 |
switch(mode) { |
| 180 |
case KALIDRAW: |
| 181 |
case KALIMOVE: |
| 182 |
case KALIPICK: |
| 183 |
if(RectIncludesPoint(sym_rect, tmppoint)) { |
| 184 |
tmppoint.x -= refpoint.x; |
| 185 |
tmppoint.y -= refpoint.y; |
| 186 |
VectorMatrixMult(&tmppoint,xforms[4],&tmppoint); |
| 187 |
if(how > 0 && (mode == KALIPICK || mode == KALIMOVE)) { |
| 188 |
/* On initial click, swap endpoints to find closest */ |
| 189 |
if(distance(tmppoint.x, tmppoint.y, Lines->m[EX], Lines->m[EY]) > |
| 190 |
distance(tmppoint.x, tmppoint.y, Lines->m[SX], Lines->m[SY])) { |
| 191 |
float t; |
| 192 |
t = Lines->m[EX]; Lines->m[EX] = Lines->m[SX]; Lines->m[SX] = t; |
| 193 |
t = Lines->m[EY]; Lines->m[EY] = Lines->m[SY]; Lines->m[SY] = t; |
| 194 |
} |
| 195 |
} |
| 196 |
Lines->m[EX]=tmppoint.x; |
| 197 |
Lines->m[EY]=tmppoint.y; |
| 198 |
scheduleRedraw(); |
| 199 |
} |
| 200 |
return; |
| 201 |
|
| 202 |
case KALITRANSFORM: |
| 203 |
if(how > 0) /* Record initial X */ |
| 204 |
oldxposition = x; |
| 205 |
xformfnc( (x - oldxposition) / win_rect.width, sym ); |
| 206 |
oldxposition = x; |
| 207 |
DefineSymWindow(&sym_rect,sym,&win_rect,zoom); |
| 208 |
count = SetUpSymmetry(sym,&sym_pts,xforms,&sym_rect,&win_rect); |
| 209 |
scheduleRedraw(); |
| 210 |
return; |
| 211 |
} |
| 212 |
} else { /* Button release */ |
| 213 |
if ((mode == KALIDRAW) || (mode == KALIMOVE)) { /* fix endpoint */ |
| 214 |
POINT p; |
| 215 |
/* Reference to closest center of symmetry */ |
| 216 |
p.x = tmppoint.x-refpoint.x; |
| 217 |
p.y = tmppoint.y-refpoint.y; |
| 218 |
/* Convert to standard basis */ |
| 219 |
VectorMatrixMult(&p,xforms[4],&p); |
| 220 |
Lines->m[EX]=p.x; |
| 221 |
Lines->m[EY]=p.y; |
| 222 |
} |
| 223 |
if (mode == KALIMOVE) mode = KALIPICK; |
| 224 |
if (mode == 0) mode = KALIDRAW; |
| 225 |
|
| 226 |
/* start drawing new line */ |
| 227 |
if (mode == KALIDRAW) { |
| 228 |
refpoint = sym_pts[closest(&tmppoint,sym_pts,count)]; |
| 229 |
Lines = NewLine(Lines); |
| 230 |
NewId(Lines); |
| 231 |
/* Reference to closest center of symmetry */ |
| 232 |
tmppoint.x -= refpoint.x; |
| 233 |
tmppoint.y -= refpoint.y; |
| 234 |
/* Convert to standard basis */ |
| 235 |
VectorMatrixMult(&tmppoint,xforms[4],&tmppoint); |
| 236 |
Lines->m[EX]=Lines->m[SX]=tmppoint.x; |
| 237 |
Lines->m[EY]=Lines->m[SY]=tmppoint.y; |
| 238 |
scheduleRedraw(); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* Discard a line if the time is ripe -- if we're in delete mode and |
| 245 |
* one is visibly selected, or if we're in draw mode (delete last-drawn line). |
| 246 |
*/ |
| 247 |
void mayTossLine() |
| 248 |
{ |
| 249 |
if(Lines != NULL && (lit || mode == KALIDRAW)) { |
| 250 |
Lines = DropLine(Lines); |
| 251 |
scheduleRedraw(); |
| 252 |
} |
| 253 |
if(mode == KALIDRAW) |
| 254 |
mode = 0; |
| 255 |
lit = (Lines != NULL); |
| 256 |
} |
| 257 |
|
| 258 |
void handleMiddlemouse(int how, int x, int y) |
| 259 |
{ |
| 260 |
if(how > 0) |
| 261 |
mayTossLine(); |
| 262 |
} |
| 263 |
|
| 264 |
void |
| 265 |
handleKeyboard(int ch) |
| 266 |
{ |
| 267 |
switch(ch) { |
| 268 |
case '\b': |
| 269 |
case 0177: |
| 270 |
mayTossLine(); |
| 271 |
break; |
| 272 |
|
| 273 |
case '<': StartLoadProc(NULL, 0); break; |
| 274 |
|
| 275 |
case '>': StartSaveProc(NULL, 0); break; |
| 276 |
|
| 277 |
case 'z': set_mode(KALIZOOM); break; |
| 278 |
case 'd': set_mode(KALIDRAW); break; |
| 279 |
case 'm': set_mode(KALIMOVE); break; |
| 280 |
case 'x': set_mode(KALIDELETE); break; |
| 281 |
case 'r': set_mode(KALIROTATE); break; |
| 282 |
case 'a': set_mode(KALIANGLE); break; |
| 283 |
case 'R': set_mode(KALIRATIO); break; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
struct Best { |
| 289 |
float r2; /* squared distance to nearest point */ |
| 290 |
int found; |
| 291 |
LINE line; /* The line picked */ |
| 292 |
POINT pt; /* Translation reference-point of best line */ |
| 293 |
} best; |
| 294 |
|
| 295 |
extern void PickStart(float cursx, float cursy, float maxdist); |
| 296 |
|
| 297 |
void handleRightmouse(int how, int cursx, int cursy) |
| 298 |
{ |
| 299 |
int truex = cursx * zoom; |
| 300 |
int truey = cursy * zoom; |
| 301 |
|
| 302 |
PickStart(truex, truey, 2*zoom); |
| 303 |
|
| 304 |
/* don't draw the grid when picking to avoid picking the grid! */ |
| 305 |
DrawCurrent(&sym_rect, Lines, sym, sym_pts, xforms, count, &PickDraw, 0); |
| 306 |
|
| 307 |
/* Redraw, so we can overlay to highlight the selected one */ |
| 308 |
DrawCurrent(&sym_rect,Lines,sym, |
| 309 |
sym_pts,xforms,count,&GXDraw,GridDisplay); |
| 310 |
|
| 311 |
if(best.found) { |
| 312 |
Lines = GrabLine(best.line.id,Lines); /* Move chosen one first */ |
| 313 |
refpoint = best.pt; |
| 314 |
/* get correct rot & ref */ |
| 315 |
UseRightCopy(Lines,sym,xforms,best.line.obj_pos); |
| 316 |
/* get cursor relative to ref */ |
| 317 |
tmppoint.x = truex - refpoint.x; |
| 318 |
tmppoint.y = truey - refpoint.y; |
| 319 |
/* Convert to standard basis */ |
| 320 |
VectorMatrixMult(&tmppoint,xforms[4],&tmppoint); |
| 321 |
ClosestEndpoint(Lines,&tmppoint); |
| 322 |
HighLiteLine(&sym_rect,Lines,sym, |
| 323 |
sym_pts,xforms,count, |
| 324 |
(pick_for == KALIDELETE) ? DELETECOLOR : MOVECOLOR); |
| 325 |
} |
| 326 |
else lit = FALSE; |
| 327 |
swapbuffers(); |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
int user_event_cb(XEvent *xev, void *userdata) { |
| 333 |
int x, y, how = 0, button; |
| 334 |
|
| 335 |
switch(xev->type) { |
| 336 |
case MotionNotify: /* Drag: how = -1 */ |
| 337 |
how = -1; |
| 338 |
button = xev->xbutton.state & Button1Mask ? Button1 |
| 339 |
: xev->xbutton.state & Button2Mask ? Button2 |
| 340 |
: xev->xbutton.state & Button3Mask ? Button3 : 0; |
| 341 |
goto mote; |
| 342 |
|
| 343 |
case ButtonPress: /* Button press: how = 1 */ |
| 344 |
how = 1; |
| 345 |
case ButtonRelease: /* Button release: how = 0 */ |
| 346 |
button = xev->xbutton.button; |
| 347 |
mote: |
| 348 |
x = xev->xbutton.x; |
| 349 |
y = xev->xbutton.y; |
| 350 |
switch(button) { |
| 351 |
case Button1: handleLeftmouse(how, xev->xbutton.x, xev->xbutton.y); break; |
| 352 |
case Button2: handleMiddlemouse(how, xev->xbutton.x, xev->xbutton.y); break; |
| 353 |
case Button3: handleRightmouse(how, xev->xbutton.x, xev->xbutton.y); break; |
| 354 |
default: |
| 355 |
fprintf(stderr, "Kali small error: hmmm.... X doesn't\ |
| 356 |
understand button %d\n", button); |
| 357 |
break; |
| 358 |
} |
| 359 |
break; |
| 360 |
|
| 361 |
case ConfigureNotify: |
| 362 |
case Expose: |
| 363 |
/* redraw event */ |
| 364 |
handleRedraw(); |
| 365 |
break; |
| 366 |
|
| 367 |
case KeyPress: /* keyboard device */ |
| 368 |
{ |
| 369 |
char chars[1]; |
| 370 |
if(XLookupString(&xev->xkey, chars, sizeof(chars), NULL, NULL) > 0) { |
| 371 |
handleKeyboard( chars[0] ); |
| 372 |
} |
| 373 |
break; |
| 374 |
} |
| 375 |
|
| 376 |
case EnterNotify: |
| 377 |
case LeaveNotify: /* inputchange */ |
| 378 |
break; |
| 379 |
|
| 380 |
case ClientMessage: |
| 381 |
case CirculateNotify: |
| 382 |
case CreateNotify: |
| 383 |
case DestroyNotify: |
| 384 |
case GravityNotify: |
| 385 |
case ReparentNotify: |
| 386 |
case MapNotify: |
| 387 |
case UnmapNotify: /* Ignore these Events... */ |
| 388 |
break; |
| 389 |
default: |
| 390 |
#ifdef DEBUG |
| 391 |
fprintf(stderr, "Kali small error: user window does not respond to\ |
| 392 |
this type of event"); |
| 393 |
#endif |
| 394 |
break; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
/*--------------------------------------------------------- main */ |
| 402 |
|
| 403 |
int main(int argc, char** argv) { |
| 404 |
FILE *f = NULL; |
| 405 |
char *fname = NULL; |
| 406 |
/* window defaults */ |
| 407 |
kwin.x = 243; |
| 408 |
kwin.y = 8; |
| 409 |
kwin.width = 626; |
| 410 |
kwin.height = 670; |
| 411 |
pwin.x = 8; |
| 412 |
pwin.y = 8; |
| 413 |
pwin.width = 220; |
| 414 |
pwin.height = 670; |
| 415 |
|
| 416 |
for (++argv; --argc > 0; argv++) { |
| 417 |
if (STREQ(*argv, "-f")) |
| 418 |
frieze=1; |
| 419 |
else if (STREQ(*argv, "-w")) |
| 420 |
frieze=0; |
| 421 |
else if (STREQ(*argv, "-m")) |
| 422 |
moron=1; |
| 423 |
else if (STREQ(*argv, "-k") && ++argv) { |
| 424 |
--argc; |
| 425 |
fname = *argv; |
| 426 |
} else if (STREQ(*argv, "-g") && ++argv) { |
| 427 |
sscanf(*argv, "%f,%f,%f,%f", &kwin.x, &kwin.y, &kwin.width, &kwin.height); |
| 428 |
argc--; |
| 429 |
} else if (STREQ(*argv, "-p") && ++argv) { |
| 430 |
sscanf(*argv, "%f,%f,%f,%f", &pwin.x, &pwin.y, &pwin.width, &pwin.height); |
| 431 |
argc--; |
| 432 |
} else if((*argv)[0] != '-') { |
| 433 |
fname = *argv; |
| 434 |
} else { |
| 435 |
fprintf(stderr,"\ |
| 436 |
kali [-f] [-w] [-m] [kalifile]\n\ |
| 437 |
Draw Escher-like tilings interactively.\n\ |
| 438 |
-f Begin with the 7 1D frieze symmetry groups.\n\ |
| 439 |
-w Begin with the 17 2D planar symmetry groups.\n\ |
| 440 |
-m Disable switching between frieze and planar groups.\n\ |
| 441 |
-g x,y,w,h Position and size of main window.\n\ |
| 442 |
-p x,y,w,h Position and size of control panel.\n\ |
| 443 |
(0,0) is lower left on SGIs, upper left in generic X.\n" |
| 444 |
); |
| 445 |
exit(-1); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
if (fname != NULL && (f = fopen(fname, "r")) == NULL) |
| 450 |
fprintf(stderr, "Can't find file %s.",fname); |
| 451 |
|
| 452 |
zoom = 1.0; |
| 453 |
sym_index = P1; |
| 454 |
|
| 455 |
{ |
| 456 |
static int fakeargc = 1; |
| 457 |
static char *fakeargv[2] = { "kali", NULL }; |
| 458 |
#if FL_INCLUDE_VERSION <= 75 |
| 459 |
fl_initialize("kali", "Kali", NULL, 0, &fakeargc, fakeargv); |
| 460 |
#else /* modern versions */ |
| 461 |
fl_initialize(&fakeargc, fakeargv, "Kali", 0, 0); |
| 462 |
#endif |
| 463 |
} |
| 464 |
|
| 465 |
foreground(); |
| 466 |
|
| 467 |
win = MakeWindow(&win_rect, &kwin); |
| 468 |
sym_storage = SYMTAB[sym_index]; |
| 469 |
DefineSymWindow(&sym_rect,sym,&win_rect,zoom); |
| 470 |
count = SetUpSymmetry(sym,&sym_pts,xforms,&sym_rect,&win_rect); |
| 471 |
DrawCurrent(&sym_rect,Lines,sym,sym_pts,xforms,count,&GXDraw,GridDisplay); |
| 472 |
swapbuffers(); |
| 473 |
|
| 474 |
forms_init(&pwin); |
| 475 |
winset(win); |
| 476 |
if (f) load_a_file(f); |
| 477 |
|
| 478 |
mode = 0; |
| 479 |
|
| 480 |
fl_set_event_callback(user_event_cb, NULL); |
| 481 |
|
| 482 |
for(;;) { |
| 483 |
mode = 0; |
| 484 |
fl_do_forms(); |
| 485 |
#ifdef DEBUG |
| 486 |
fprintf(stderr, "hmmm.... what am I doing here\n"); |
| 487 |
#endif |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
/*--------------------------------------------------------------*/ |
| 495 |
|
| 496 |
void ChangeScale(double amount, SYMMETRY *sym) { |
| 497 |
zoom *= 1 - amount/2; |
| 498 |
} |
| 499 |
|
| 500 |
void ChangeRotation(double amount, SYMMETRY *sym) { |
| 501 |
double angle; |
| 502 |
MATRIX rotation; |
| 503 |
|
| 504 |
angle = -amount * M_PI / 2; |
| 505 |
SetUpRot(angle,rotation); |
| 506 |
VectorMatrixMult(&(sym->v1),rotation,&(sym->v1)); |
| 507 |
VectorMatrixMult(&(sym->v2),rotation,&(sym->v2)); |
| 508 |
} |
| 509 |
|
| 510 |
void ChangeRatio(double amount, SYMMETRY* sym) { |
| 511 |
VectorScalarMult( &sym->v1, 1 + amount/2 ); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
void ChangeAngle(double amount, SYMMETRY *sym) { |
| 516 |
double angle; |
| 517 |
MATRIX rotation; |
| 518 |
|
| 519 |
angle = amount * M_PI / 4; |
| 520 |
SetUpRot(angle,rotation); |
| 521 |
VectorMatrixMult(&(sym->v1),rotation,&(sym->v1)); |
| 522 |
/* modify rotation matrix for opposite angle */ |
| 523 |
rotation[1] = -rotation[1]; |
| 524 |
rotation[2] = -rotation[2]; |
| 525 |
VectorMatrixMult(&(sym->v2),rotation,&(sym->v2)); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
/* Move the selected line to first in the list */ |
| 530 |
LINE *GrabLine(short i, LINE* first) { |
| 531 |
|
| 532 |
LINE *cur,*last; |
| 533 |
|
| 534 |
last = NULL; /* previous line we looked at, NOT last in the list */ |
| 535 |
for (cur=first; cur != NULL; cur=cur->next) { |
| 536 |
if (cur->id == i) { |
| 537 |
if (last != NULL) { |
| 538 |
last->next = cur->next; |
| 539 |
cur->next = first; |
| 540 |
} |
| 541 |
first = cur; |
| 542 |
break; |
| 543 |
} |
| 544 |
last = cur; |
| 545 |
} |
| 546 |
return(first); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
LINE *DropLine(LINE *L) { |
| 551 |
LINE *cur; |
| 552 |
cur = L->next; |
| 553 |
free(L); |
| 554 |
return(cur); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
void MapCursor(int x, int y, POINT *p, RECTANGLE *r, float zoom) { |
| 559 |
p->x = (x - r->x)*zoom; |
| 560 |
p->y = (y - r->y)*zoom; |
| 561 |
} |
| 562 |
|
| 563 |
void DrawCurrent(RECTANGLE *rect, LINE *Lines, SYMMETRY *sym, |
| 564 |
POINT *pts, MATRIX *xforms, int count, |
| 565 |
DRAWER *drawer, int GridDisplay) { |
| 566 |
LINE *obj; |
| 567 |
RECTANGLE bounds; |
| 568 |
|
| 569 |
fl_winset(win); |
| 570 |
|
| 571 |
SET_COLOR(BACKCOLOR); |
| 572 |
clear(); |
| 573 |
|
| 574 |
SET_COLOR(POINTCOLOR); |
| 575 |
DrawPoints(drawer, pts,count); |
| 576 |
if (GridDisplay) { |
| 577 |
obj = MakeCurrentObject(sym->grid,sym,xforms,&bounds); |
| 578 |
SET_COLOR(GRIDCOLOR); |
| 579 |
ReplicateObject(rect,obj,pts,count,&bounds,sym,drawer); |
| 580 |
FreeObject(obj); |
| 581 |
} |
| 582 |
obj = MakeCurrentObject(Lines,sym,xforms,&bounds); |
| 583 |
SET_COLOR(LINECOLOR); |
| 584 |
ReplicateObject(rect,obj,pts,count,&bounds,sym,drawer); |
| 585 |
FreeObject(obj); |
| 586 |
} |
| 587 |
|
| 588 |
void HighLiteLine(RECTANGLE *rect, LINE *cur, SYMMETRY *sym, |
| 589 |
POINT *pts, MATRIX *xforms, int count, |
| 590 |
int colour) { |
| 591 |
LINE *obj,*temp; |
| 592 |
RECTANGLE bounds; |
| 593 |
|
| 594 |
if(cur == NULL) { |
| 595 |
return; |
| 596 |
} |
| 597 |
lit = TRUE; |
| 598 |
winset(win); |
| 599 |
temp = cur->next; |
| 600 |
cur->next = NULL; |
| 601 |
obj = MakeCurrentObject(cur,sym,xforms,&bounds); |
| 602 |
cur->next = temp; |
| 603 |
SET_COLOR(colour); |
| 604 |
ReplicateObject(rect,obj,pts,count,&bounds,sym,&GXDraw); |
| 605 |
FreeObject(obj); |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
/* UseRightCopy - changes the endpoints of the line cur according |
| 611 |
to the pos'th transformation that goes into making an object. |
| 612 |
Since all transformations of a given line are eqivalent, this makes |
| 613 |
no difference in the picture */ |
| 614 |
|
| 615 |
void UseRightCopy(LINE *cur, SYMMETRY *sym, MATRIX *xforms, short pos) { |
| 616 |
RECTANGLE bounds; |
| 617 |
LINE *temp,*l,*obj; |
| 618 |
|
| 619 |
temp = cur->next; |
| 620 |
cur->next = NULL; |
| 621 |
obj = MakeCurrentObject(cur,sym,xforms,&bounds); |
| 622 |
for (l = obj; l->next != NULL; l = l->next) { |
| 623 |
if (l->obj_pos == pos) break; |
| 624 |
} |
| 625 |
*cur = *l; /* copy line description */ |
| 626 |
FreeObject(obj); |
| 627 |
MatrixMultiply(cur->m,xforms[4],cur->m); |
| 628 |
cur->next = temp; |
| 629 |
} |
| 630 |
|
| 631 |
long MakeWindow(RECTANGLE *r, RECTANGLE *kwin) { |
| 632 |
long rwin; |
| 633 |
|
| 634 |
GXinit(&GXDraw, fl_display, 0); |
| 635 |
prefposition( kwin->x, kwin->x+kwin->width, kwin->y, kwin->y+kwin->height); |
| 636 |
rwin = winopen("kali"); |
| 637 |
RGBmode(); |
| 638 |
|
| 639 |
winconstraints(); |
| 640 |
AdjustWindowRectangle(r); |
| 641 |
doublebuffer(); |
| 642 |
gconfig(); |
| 643 |
return(rwin); |
| 644 |
} |
| 645 |
|
| 646 |
void AdjustWindowRectangle(RECTANGLE *r) { |
| 647 |
long x,y; |
| 648 |
|
| 649 |
r->x = r->y = 0; |
| 650 |
getsize(&x,&y); |
| 651 |
r->width = x; |
| 652 |
r->height = y; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
/* Find closest center of symmetry to new motif point */ |
| 658 |
int closest(POINT *new_pt, POINT *pts, int maxpts) { |
| 659 |
int i,min_i; |
| 660 |
float dist,min=HUGE; |
| 661 |
|
| 662 |
for (i=0; i<maxpts; i++) |
| 663 |
{ |
| 664 |
dist = distance(new_pt->x, new_pt->y, pts[i].x, pts[i].y); |
| 665 |
if (dist < min) { min = dist; min_i = i; } |
| 666 |
} |
| 667 |
return(min_i); |
| 668 |
} |
| 669 |
|
| 670 |
void PrintLine(LINE* l) { |
| 671 |
int i; |
| 672 |
printf("%x ",l); |
| 673 |
for (i=0;i<4;i++) printf("%6.3f ",l->m[i]); |
| 674 |
printf("%d %x \n",l->id,l->next); |
| 675 |
} |
| 676 |
|
| 677 |
void DumpLines(LINE* Lines) { |
| 678 |
LINE* cur; |
| 679 |
int i; |
| 680 |
i = 0; |
| 681 |
for (cur=Lines; cur != NULL; cur=cur->next) { |
| 682 |
printf("Line %d ",i++); |
| 683 |
PrintLine(cur); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
LINE* ThrowAwayLines(LINE *Lines) |
| 688 |
{ |
| 689 |
LINE *cur,*old_cur; |
| 690 |
cur = Lines; |
| 691 |
while (cur) { |
| 692 |
old_cur = cur; |
| 693 |
cur = cur->next; |
| 694 |
free(old_cur); |
| 695 |
} |
| 696 |
return(NULL); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
void SaveProc(FILE *pat) |
| 701 |
{ |
| 702 |
LINE *cur; |
| 703 |
|
| 704 |
fprintf(pat,"%d\n",sym_index); |
| 705 |
fprintf(pat,"%6.3f %6.3f\n", |
| 706 |
sym_rect.width,sym_rect.height); |
| 707 |
fprintf(pat,"%6.3f %6.3f\n", |
| 708 |
sym->v1.x,sym->v1.y); |
| 709 |
fprintf(pat,"%6.3f %6.3f\n", |
| 710 |
sym->v2.x,sym->v2.y); |
| 711 |
fprintf(pat,"%6.3f \n", |
| 712 |
zoom); |
| 713 |
for (cur=Lines; cur != NULL; cur=cur->next) |
| 714 |
fprintf(pat,"%6.3f %6.3f %6.3f %6.3f\n", |
| 715 |
cur->m[0],cur->m[1],cur->m[2],cur->m[3]); |
| 716 |
fclose(pat); |
| 717 |
} |
| 718 |
|
| 719 |
void LoadProc(FILE *pat) |
| 720 |
{ |
| 721 |
float dummy; |
| 722 |
fscanf(pat,"%d\n",&sym_index); |
| 723 |
fscanf(pat,"%f %f\n",&dummy,&dummy); |
| 724 |
sym_storage = SYMTAB[sym_index]; |
| 725 |
fscanf(pat,"%f %f\n", |
| 726 |
&(sym->v1.x),&(sym->v1.y)); |
| 727 |
fscanf(pat,"%f %f\n", |
| 728 |
&(sym->v2.x),&(sym->v2.y)); |
| 729 |
fscanf(pat,"%f \n", |
| 730 |
&zoom); |
| 731 |
DefineSymWindow(&sym_rect,sym,&win_rect,zoom); |
| 732 |
/* ortho2(0.0,zoom*win_rect.width,0.0, |
| 733 |
zoom*win_rect.height); */ |
| 734 |
count = SetUpSymmetry(sym,&sym_pts, |
| 735 |
xforms,&sym_rect,&win_rect); |
| 736 |
Lines = ThrowAwayLines(Lines); |
| 737 |
Lines = ReadPattern(Lines,pat); |
| 738 |
|
| 739 |
scheduleRedraw(); |
| 740 |
mode = 0; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
void DefineSymWindow(RECTANGLE *s_rect, SYMMETRY *sym, |
| 746 |
RECTANGLE *w_rect, float zoom) { |
| 747 |
float x,y; |
| 748 |
extern WINDOW win; |
| 749 |
|
| 750 |
s_rect->width = zoom * w_rect->width; |
| 751 |
s_rect->height = zoom * w_rect->height; |
| 752 |
|
| 753 |
winset(win); |
| 754 |
ortho2(0.0,zoom*w_rect->width,0.0,zoom*w_rect->height); |
| 755 |
} |
| 756 |
|
| 757 |
/* Picking */ |
| 758 |
|
| 759 |
#define MAXDEPTH 25 |
| 760 |
|
| 761 |
#define VADD(dest, a, b) (dest).x = (a).x + (b).x, (dest).y = (a).y + (b).y |
| 762 |
#define VSUB(dest, a, b) (dest).x = (a).x - (b).x, (dest).y = (a).y - (b).y |
| 763 |
|
| 764 |
static POINT pickpt, pick0, pickT; |
| 765 |
static POINT pstack[MAXDEPTH]; |
| 766 |
static int pdepth; |
| 767 |
|
| 768 |
void |
| 769 |
PickStart(float cursorx, float cursory, float maxdist) |
| 770 |
{ |
| 771 |
pickpt.x = cursorx; |
| 772 |
pickpt.y = cursory; |
| 773 |
best.r2 = maxdist*maxdist; |
| 774 |
best.found = 0; |
| 775 |
pdepth = 0; |
| 776 |
} |
| 777 |
|
| 778 |
static void PickPush(DRAWER *drawer) { |
| 779 |
pstack[pdepth] = pickT; |
| 780 |
if(++pdepth >= MAXDEPTH) { |
| 781 |
fprintf(stderr, "kali error: PickPush(%d)\n", pdepth); |
| 782 |
pdepth = MAXDEPTH-1; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
static void PickPop(DRAWER *drawer) { |
| 787 |
if(--pdepth < 0) { |
| 788 |
fprintf(stderr, "kali error: PickPop(%d)\n", pdepth); |
| 789 |
pdepth = 0; |
| 790 |
} |
| 791 |
pickT = pstack[pdepth]; |
| 792 |
VSUB(pick0, pickpt, pickT); |
| 793 |
} |
| 794 |
|
| 795 |
static void PickTranslate(DRAWER *drawer, double x, double y) { |
| 796 |
pickT.x += x; pickT.y += y; |
| 797 |
|
| 798 |
VSUB(pick0, pickpt, pickT); |
| 799 |
} |
| 800 |
|
| 801 |
static void PickLine(DRAWER *drawer, LINE *lp) |
| 802 |
{ |
| 803 |
float midx, midy, dx, dy, linex, liney; |
| 804 |
float l2, dot, r2; |
| 805 |
midx = .5*(lp->m[SX] + lp->m[EX]) - pick0.x; |
| 806 |
midy = .5*(lp->m[SY] + lp->m[EY]) - pick0.y; |
| 807 |
dx = .5*(lp->m[SX] - lp->m[EX]); |
| 808 |
dy = .5*(lp->m[SY] - lp->m[EY]); |
| 809 |
l2 = dx*dx + dy*dy; |
| 810 |
|
| 811 |
if(l2 < best.r2 + midx*midx + midy*midy) |
| 812 |
return; /* Too far away */ |
| 813 |
|
| 814 |
/* Project origin to our line, clamping it to the ends of the line. */ |
| 815 |
dot = midx*dx + midy*dy; /* p.l */ |
| 816 |
if(dot > l2) dot = l2; |
| 817 |
else if(dot < -l2) dot = -l2; |
| 818 |
if(dot != 0) { /* Slide "midx,midy" along line to nearest pt */ |
| 819 |
linex = midx - dx*dot/l2; |
| 820 |
liney = midy - dy*dot/l2; |
| 821 |
} else { |
| 822 |
linex = midx; |
| 823 |
liney = midy; |
| 824 |
} |
| 825 |
r2 = linex*linex + liney*liney; |
| 826 |
if(r2 < best.r2) { |
| 827 |
/* Got one! */ |
| 828 |
best.found = 1; |
| 829 |
best.line = *lp; |
| 830 |
best.r2 = r2; |
| 831 |
best.pt = pickT; |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
void |
| 836 |
PickLines(DRAWER *drawer, LINE *obj) |
| 837 |
{ |
| 838 |
while(obj) { |
| 839 |
PickLine(drawer, obj); |
| 840 |
obj = obj->next; |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
DRAWER PickDraw = { |
| 845 |
"Pick", |
| 846 |
PickLines, |
| 847 |
PickPush, |
| 848 |
PickTranslate, |
| 849 |
PickPop, |
| 850 |
PickLine, |
| 851 |
NULL, |
| 852 |
NULL |
| 853 |
}; |