summaryrefslogtreecommitdiffstats
path: root/tksao/frame/compass.C
blob: a37a76397560ab4d26d59c4e26f7a654d0156d9d (plain)
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
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <tk.h>

#include "compass.h"
#include "fitsimage.h"

Compass::Compass(const Compass& a) : Marker(a)
{
  p1 = a.p1;
  p2 = a.p2;
  pp1 = a.pp1;
  pp2 = a.pp2;
  radius = a.radius;
  coordSystem = a.coordSystem;
  skyFrame = a.skyFrame;
  northText = dupstr(a.northText);
  eastText = dupstr(a.eastText);
  northArrow = a.northArrow;
  eastArrow = a.eastArrow;
}

Compass::Compass(Base* p, const Vector& ctr,
		 double r,
		 const char* n, const char* e, int na, int ea,
		 Coord::CoordSystem sys, Coord::SkyFrame sky, 
		 const char* clr, int* dsh, 
		 int w, const char* f, const char* t, 
		 unsigned short prop, const char* c,
		 const List<Tag>& tag, const List<CallBack>& cb) 
  : Marker(p, ctr, 0, clr, dsh, w, f, t, prop, c, tag, cb)
{
  coordSystem = sys;
  skyFrame = sky;
  radius = r;

  northText = dupstr(n);
  eastText = dupstr(e);
  northArrow = na;
  eastArrow = ea;

  strcpy(type_,"compass");
  handle = new Vector[3];
  numHandle = 3;

  updateBBox();
}

Compass::~Compass()
{
  if (northText)
    delete [] northText;

  if (eastText)
    delete [] eastText;
}

void Compass::renderX(Drawable drawable, Coord::InternalSystem sys, RenderMode mode)
{
  GC lgc = renderXGC(mode);

  Vector aa = parent->mapFromRef(center,sys);
  Vector bb = parent->mapFromRef(p1,sys);
  Vector cc = parent->mapFromRef(p2,sys);
  Vector dd = parent->mapFromRef(pp1,sys);
  Vector ee = parent->mapFromRef(pp2,sys);

  if (northArrow) {
    bb = modifyArrow(center,p1,sys);
    renderXArrow(drawable, center, p1, sys, lgc);
  }

  if (eastArrow) {
    cc = modifyArrow(center,p2,sys);
    renderXArrow(drawable, center, p2, sys, lgc);
  }

  XDrawLine(display, drawable, lgc, aa[0], aa[1], bb[0], bb[1]);
  XDrawLine(display, drawable, lgc, aa[0], aa[1], cc[0], cc[1]);

  if (tkfont_) {
    XSetFont(display, lgc, Tk_FontId(tkfont_));

    Tk_FontMetrics metrics;
    Tk_GetFontMetrics(tkfont_, &metrics);

    if (northText) {
      float r1 = Tk_TextWidth(tkfont_, northText, strlen(northText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (bb-aa).angle();

      Vector ddd = dd * 
	Translate(r1*cos(angle),r2*sin(angle)) * 
	Translate(-r1,(metrics.ascent-metrics.descent)/2.);

      Tk_DrawChars(display, drawable, lgc, tkfont_, northText,
		   strlen(northText), ddd[0], ddd[1]);
    }

    if (eastText) {
      float r1 = Tk_TextWidth(tkfont_, eastText, strlen(eastText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (cc-aa).angle();
      Vector eee = ee *
	Translate(r1*cos(angle),r2*sin(angle)) * 
	Translate(-r1,(metrics.ascent-metrics.descent)/2.);

      Tk_DrawChars(display, drawable, lgc, tkfont_, eastText,
		   strlen(eastText), eee[0], eee[1]);
    }
  }
}

GC Compass::renderXGC(RenderMode mode)
{
  switch (mode) {
  case SRC:
    XSetForeground(display, gc, color); 
    renderXLineNoDash(gc);
    return gc;

  case XOR:
    renderXLineDash(gcxor);
    return gcxor;
  }

  // so compiler will not complain
  return gc;
}

void Compass::renderPS(int mode)
{
  renderPSGC(mode);

  Vector aa = parent->mapFromRef(center,Coord::CANVAS);
  Vector bb = parent->mapFromRef(p1,Coord::CANVAS);
  Vector cc = parent->mapFromRef(p2,Coord::CANVAS);
  Vector dd = parent->mapFromRef(pp1,Coord::CANVAS);
  Vector ee = parent->mapFromRef(pp2,Coord::CANVAS);

  if (northArrow) {
    bb = modifyArrow(center,p1,Coord::CANVAS);
    renderPSArrow(center,p1,Coord::CANVAS);
  }

  if (eastArrow) {
    cc = modifyArrow(center,p2,Coord::CANVAS);
    renderPSArrow(center,p2,Coord::CANVAS);
  }

  {
    ostringstream str;
    str << "newpath " 
	<< aa.TkCanvasPs(parent->canvas) << ' '
	<< "moveto "
	<< bb.TkCanvasPs(parent->canvas) << ' '
	<< "lineto stroke" << endl
	<< "newpath " 
	<< aa.TkCanvasPs(parent->canvas) << ' '
	<< "moveto "
	<< cc.TkCanvasPs(parent->canvas) << ' '
	<< "lineto stroke" << endl << ends;
    Tcl_AppendResult(parent->interp, str.str().c_str(), NULL);
  }

  if (psfont_) {
    ostringstream str;

    const char* ff = Tk_NameOfFont(psfont_);
    str << '/' << psFontName(ff)
    	<< " findfont " << int(psFontSize(ff)*parent->getDisplayRatio())
	<< " scalefont setfont" << endl;

    if (northText) {
      double angle = (bb-aa).angle();
      Vector ddd = dd.TkCanvasPs(parent->canvas);
      str << "gsave" << endl
	  << "newpath " << endl
	  << ddd << " moveto" << endl
	  << '(' << psQuote(northText) << ')' << endl
	// bbox
	  << "dup true charpath pathbbox " << endl
	  << "closepath " << endl
	// text llx lly urx ury

	// dup bbox 
	  << "3 index 3 index 3 index 3 index " << endl
	// text llx lly urx ury llx lly urx ury

	// Translate(-r1,(metrics.ascent-metrics.descent)/2.)
	  << "3 -1 roll sub 3.6 div neg " << endl
	  << "3 1 roll sub 2 div exch " << endl
	// text llx lly urx ury Sx Sy

	// reorder
	  << "6 1 roll 6 1 roll " << endl
	// text Sx Sy llx lly urx ury

	// Translate(r1*cos(angle),r2*sin(angle))
	  << "3 -1 roll sub 2 div neg " << endl
	  << "3 1 roll sub 2 div exch " << endl
	  << radToDeg(angle) << " sin mul exch " << endl
	  << radToDeg(angle) << " cos mul neg exch " << endl
	// text Sx Sy Tx Ty

	// add
	  << "3 -1 roll add " << endl
	  << "3 1 roll add exch " << endl
	// text Fx Fy

	  << ddd << " moveto rmoveto show " << endl
	  << "grestore" << endl;
    }

    if (eastText) {
      double angle = (cc-aa).angle();
      Vector eee = ee.TkCanvasPs(parent->canvas);
      str << "gsave" << endl
	  << "newpath " << endl
	  << eee << " moveto" << endl
	  << '(' << psQuote(eastText) << ')' << endl
	  << "dup true charpath pathbbox "
	  << "closepath " << endl

	  << "3 index 3 index 3 index 3 index " << endl

	  << "3 -1 roll sub 3.6 div neg "
	  << "3 1 roll sub 2 div exch " << endl

	  << "6 1 roll 6 1 roll " << endl

	  << "3 -1 roll sub 2 div neg "
	  << "3 1 roll sub 2 div exch " << endl
	  << radToDeg(angle) << " sin mul exch "
	  << radToDeg(angle) << " cos mul neg exch " << endl

	  << "3 -1 roll add "
	  << "3 1 roll add exch " << endl

	  << eee << " moveto rmoveto show " << endl
	  << "grestore" << endl;
    }

    str << ends;
    Tcl_AppendResult(parent->interp, str.str().c_str(), NULL);
  }
}

void Compass::renderPSGC(int mode)
{
  renderPSColor(mode, parent->getXColor(colorName));
  renderPSLineNoDash();
}

#ifdef MAC_OSX_TK
void Compass::renderMACOSX()
{
  renderMACOSXGC();

  Vector aa = parent->mapFromRef(center,Coord::CANVAS);
  Vector bb = parent->mapFromRef(p1,Coord::CANVAS);
  Vector cc = parent->mapFromRef(p2,Coord::CANVAS);
  Vector dd = parent->mapFromRef(pp1,Coord::CANVAS);
  Vector ee = parent->mapFromRef(pp2,Coord::CANVAS);

  if (northArrow) {
    bb = modifyArrow(center,p1,Coord::CANVAS);
    renderMACOSXArrow(center,p1,Coord::CANVAS);
  }

  if (eastArrow) {
    cc = modifyArrow(center,p2,Coord::CANVAS);
    renderMACOSXArrow(center,p2,Coord::CANVAS);
  }

  macosxDrawLine(aa,bb);
  macosxDrawLine(aa,cc);

  if (psfont_) {
    Tcl_DString psdstr;
    Tcl_DStringInit(&psdstr);
    int psSize = Tk_PostscriptFontName(psfont_, &psdstr);
    macosxFont(Tcl_DStringValue(&psdstr), psSize);
    Tcl_DStringFree(&psdstr);

    Tk_FontMetrics metrics;
    Tk_GetFontMetrics(psfont_, &metrics);

    if (northText) {
      float r1 = Tk_TextWidth(psfont_, northText, strlen(northText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (bb-aa).angle();
      Vector ddd = dd *
	Translate(r1*cos(angle),r2*sin(angle)) * 
	Translate(-r1,(metrics.ascent-metrics.descent)/2.);

      macosxDrawText(ddd, 0, northText);
    }

    if (eastText) {
      float r1 = Tk_TextWidth(psfont_, eastText, strlen(eastText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (cc-aa).angle();
      Vector eee = ee *
	Translate(r1*cos(angle),r2*sin(angle)) * 
	Translate(-r1,(metrics.ascent-metrics.descent)/2.);

      macosxDrawText(eee, 0, eastText);
    }
  }
}

void Compass::renderMACOSXGC()
{
  macosxColor(parent->getXColor(colorName));
  renderMACOSXLineNoDash();
}
#endif

#ifdef __WIN32
void Compass::renderWIN32()
{
  renderWIN32GC();

  Vector aa = parent->mapFromRef(center,Coord::CANVAS);
  Vector bb = parent->mapFromRef(p1,Coord::CANVAS);
  Vector cc = parent->mapFromRef(p2,Coord::CANVAS);
  Vector dd = parent->mapFromRef(pp1,Coord::CANVAS);
  Vector ee = parent->mapFromRef(pp2,Coord::CANVAS);

  if (northArrow) {
    bb = modifyArrow(center,p1,Coord::CANVAS);
    renderWIN32Arrow(center,p1,Coord::CANVAS);
  }

  if (eastArrow) {
    cc = modifyArrow(center,p2,Coord::CANVAS);
    renderWIN32Arrow(center,p2,Coord::CANVAS);
  }

  win32DrawLine(aa,bb);
  win32DrawLine(aa,cc);

  if (tkfont_) {
    win32Font(tkfont_);

    Tk_FontMetrics metrics;
    Tk_GetFontMetrics(tkfont_, &metrics);

    if (northText) {
      float r1 = Tk_TextWidth(tkfont_, northText, strlen(northText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (bb-aa).angle();
      Vector ddd = dd *
	Translate(r1*cos(angle),r2*sin(angle)) * 
	Translate(-r1,(metrics.ascent-metrics.descent)/2.);

      win32DrawText(ddd, 0, northText);
    }

    if (eastText) {
      float r1 = Tk_TextWidth(tkfont_, eastText, strlen(eastText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (cc-aa).angle();
      Vector eee = ee *
	Translate(r1*cos(angle),r2*sin(angle)) * 
	Translate(-r1,(metrics.ascent-metrics.descent)/2.);

      win32DrawText(eee, 0, eastText);
    }
  }
}

void Compass::renderWIN32GC()
{
  win32Color(parent->getXColor(colorName));
  renderWIN32LineNoDash();
}
#endif

void Compass::updateHandles()
{
  // calc p1, p2
  FitsImage* ptr = parent->findFits(coordSystem,center);
  Vector orval = ptr->mapFromRef(center, coordSystem, skyFrame);
  double delta = ptr->mapLenFromRef(1, coordSystem);
  Vector npix = ptr->mapToRef(Vector(orval[0],orval[1]+delta), 
			      coordSystem, skyFrame);
  Vector epix = ptr->mapToRef(Vector(orval[0]+delta,orval[1]), 
			      coordSystem, skyFrame);
  Vector north = (npix - center).normalize();
  Vector east =  (epix - center).normalize();

  // calc text points pp1,pp2
  Matrix r  = Scale(radius)*Scale(parent->zoom()).invert();
  Matrix rr = Scale(5)*Scale(parent->zoom()).invert();
  p1 = center + north*r;
  p2 = center + east*r;
  pp1 = p1 + north*rr;
  pp2 = p2 + east*rr;

  // generate handles
  handle[0] = parent->mapFromRef(center,Coord::CANVAS);
  handle[1] = parent->mapFromRef(p1,Coord::CANVAS);
  handle[2] = parent->mapFromRef(p2,Coord::CANVAS);
}

void Compass::calcAllBBox()
{
  if (tkfont_) {
    // make room for text
    Tk_FontMetrics metrics;
    Tk_GetFontMetrics(tkfont_, &metrics);

    Vector a = parent->mapFromRef(center,Coord::CANVAS);
    Vector b = parent->mapFromRef(p1,Coord::CANVAS);
    Vector c = parent->mapFromRef(p2,Coord::CANVAS);
    Vector bb = parent->mapFromRef(pp1,Coord::CANVAS);
    Vector cc = parent->mapFromRef(pp2,Coord::CANVAS);

    if (northText) {
      float r1 = Tk_TextWidth(tkfont_, northText, strlen(northText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (b-a).angle();

      Vector bbb = bb * Translate(r1*cos(angle),r2*sin(angle));

      bbox.bound(bbb * Translate(-r1,(metrics.ascent-metrics.descent)/2.));
      bbox.bound(bbb * Translate(-r1,-metrics.ascent/2.-3*metrics.descent/2.));
      bbox.bound(bbb * Translate(r1,(metrics.ascent-metrics.descent)/2.));
      bbox.bound(bbb * Translate(r1,-metrics.ascent/2.-3*metrics.descent/2.));
    }

    if (eastText) {
      float r1 = Tk_TextWidth(tkfont_, eastText, strlen(eastText))/2.;
      float r2 = metrics.linespace/2.;
      double angle = (c-a).angle();

      Vector ccc = cc * Translate(r1*cos(angle),r2*sin(angle));

      bbox.bound(ccc * Translate(-r1,(metrics.ascent-metrics.descent)/2.));
      bbox.bound(ccc * Translate(-r1,-metrics.ascent/2.-3*metrics.descent/2.));
      bbox.bound(ccc * Translate(r1,(metrics.ascent-metrics.descent)/2.));
      bbox.bound(ccc * Translate(r1,-metrics.ascent/2.-3*metrics.descent/2.));
    }
  }

  Marker::calcAllBBox();
}

void Compass::edit(const Vector& v, int h)
{
  radius = (v * Translate(-center) * Scale(parent->zoom())).length();

  updateBBox();
  doCallBack(CallBack::EDITCB);
}

int Compass::isIn(const Vector& vv)
{
  /*
    v[0]-- x value of point being tested
    v[1]-- y value of point being tested

    This algorithm is from "An Introduction to Ray Tracing", Academic Press,
    1989, edited by Andrew Glassner, pg 53
    -- a point lies in a polygon if a line is extended from the point to 
    infinite in any direction and the number of intersections with the 
    polygon is odd.
    This is valid for both concave and convex polygons.
    Points on a vertex are considered inside.
    Points on a edge are considered inside.
  */

  Vector v = parent->mapToRef(vv,Coord::CANVAS);
  int crossings = 0;   // number of crossings

  Vector bb[4];
  bb[0] = center - v;
  bb[1] = p1 - v;
  bb[2] = p2 - v;
  bb[3] = bb[0];

  Vector v1;
  Vector v2 = bb[0];

  int sign = ((v2[1])>=0) ? 1 : -1; // init sign

  // for all edges

  for (int j=1; j<4; j++) {
    // look at next two vertices
    v1 = v2;
    v2 = bb[j];

    int nextSign = (v2[1]>=0) ? 1 : -1; // sign holder for p2

    if (sign != nextSign) {
      if (v1[0]>0 && v2[0]>0)
	crossings++;
      else if (v1[0]>0 || v2[0]>0) {
	if (v1[0]-(v1[1]*(v2[0]-v1[0])/(v2[1]-v1[1])) > 0)
	  crossings++;
      }
      sign = nextSign;
    }
  }

  return fmod(float(crossings),float(2)) ? 1 : 0; // if odd, point is inside
}

void Compass::setRadius(double r)
{
  radius = r;

  updateBBox();
  doCallBack(CallBack::EDITCB);
}

void Compass::setArrows(int n, int e)
{
  northArrow = n;
  eastArrow = e;

  updateBBox();
  doCallBack(CallBack::EDITCB);
}

void Compass::setLabels(const char* n, const char* e)
{
  northText = dupstr(n);
  eastText = dupstr(e);

  updateBBox();
  doCallBack(CallBack::EDITCB);
}

void Compass::setCoordSystem(Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  coordSystem = sys;
  skyFrame = sky;

  updateBBox();
}

// list

void Compass::list(ostream& str, Coord::CoordSystem sys, Coord::SkyFrame sky,
		 Coord::SkyFormat format, int conj, int strip)
{
  if (!strip) {
    FitsImage* ptr = parent->findFits(sys,center);
    listPre(str, sys, sky, ptr, strip, 1);

    str << type_ << '(';
    ptr->listFromRef(str,center,sys,sky,format);
    str << ',';
    ptr->listLenFromRef(str,radius,sys,Coord::ARCSEC);
    if (ptr->hasWCSCel(sys))
      str << '"';
    str  << ')';

    if (conj)
      str << " ||";

    str << " compass=";
    coord.listCoordSystem(str, coordSystem, skyFrame, ptr);
    str << " {" << northText << "} {" << eastText << "} " 
	<< northArrow << ' ' << eastArrow;
    listProperties(str, 0);
  }
}

void Compass::listXML(ostream& str, Coord::CoordSystem sys, 
		      Coord::SkyFrame sky, Coord::SkyFormat format)
{
  FitsImage* ptr = parent->findFits(sys,center);

  XMLRowInit();
  XMLRow(XMLSHAPE,type_);

  XMLRowCenter(ptr,sys,sky,format);

  ostringstream rstr;
  ptr->listLenFromRef(rstr,radius,sys,Coord::ARCSEC);
  XMLRow(XMLR,(char*)rstr.str().c_str());

  ostringstream pstr;
  coord.listCoordSystem(pstr, coordSystem, skyFrame, ptr);
  XMLRow(XMLPARAM,(char*)(pstr.str().c_str()));

  XMLRow(XMLPARAM2,northText);
  XMLRow(XMLPARAM3,eastText);
  XMLRow(XMLPARAM4,northArrow);
  XMLRow(XMLPARAM5,eastArrow);

  XMLRowProps(ptr,sys);
  XMLRowEnd(str);
}