summaryrefslogtreecommitdiffstats
path: root/tksao/frame/fr3dcommand.C
blob: 178ed49485a5900aa8b247e55b3aa1ef86e5d73d (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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "frame3dbase.h"
#include "fitsimage.h"
#include "context.h"
#include "marker.h"

void Frame3dBase::binToFitCmd()
{
  if (!keyContext->fits)
    return;

  Vector3d dim(keyContext->fits->getHistDim(), keyContext->binDepth());
  double bf = 1/calcZoom3d(dim, Vector(options->width,options->height));
			   
  // round up to next power of 2
  if (bf < 1)
    keyContext->setBinToFactor(Vector(1,1));
  else {
    int p=1;
    while (p<bf)
      p*=2;
    keyContext->setBinToFactor(Vector(p,p));
  }
  
  if (keyContext->fits && keyContext->fits->isHist()) {
    Matrix mm = keyContext->binCursor();
    updateBin(mm);
  }
}

void Frame3dBase::blockToFitCmd()
{
  if (!keyContext->fits)
    return;

  Vector3d ss(keyContext->fits->osize(),keyContext->naxis(2));
  double bf = 1/calcZoom3d(ss, Vector(options->width,options->height));
			   
  // round down to next power of 2
  Vector vv;
  if (bf < 1)
    vv = keyContext->setBlockToFactor(Vector(1,1));
  else {
    int p=1;
    while (p<bf)
      p*=2;
    vv = keyContext->setBlockToFactor(Vector(p,p));
  }
  
  keyContext->block();
  keyContext->analysis();
  updateBlock(vv);
}

void Frame3dBase::crop3dBeginCmd(const Vector& vv, int which)
{
  // which =0 (zmin) which =1 (zmax)
  if (!keyContext->fits)
    return;

  cropBegin = vv * Scale(zoom_).invert();
  cropEnd = vv * Scale(zoom_).invert();

  // params is a BBOX in DATA coords 0-n
  FitsZBound* zparams = 
    keyContext->getDataParams(keyContext->secMode());
  if (!which)
    cropsl_ = zparams->zmin;
  else
    cropsl_ = zparams->zmax;
}

void Frame3dBase::crop3dMotionCmd(const Vector& vv, int which)
{
  // which =0 (zmin) which =1 (zmax)
  if (!keyContext->fits)
    return;

  // params is a BBOX in DATA coords 0-n
  FitsBound* params = 
    keyContext->fits->getDataParams(keyContext->secMode());
  FitsZBound* zparams = 
    keyContext->getDataParams(keyContext->secMode());
  Vector ss(params->xmin,params->ymin);
  Vector tt(params->xmax,params->ymax);

  // erase 
  if (cropBegin[0]!=cropEnd[0] || cropBegin[1]!=cropEnd[1]) {
    Vector ll = mapFromRef3d(ss,Coord::CANVAS,cropsl_);
    Vector lr = mapFromRef3d(Vector(tt[0],ss[1]),Coord::CANVAS,cropsl_);
    Vector ur = mapFromRef3d(tt,Coord::CANVAS,cropsl_);
    Vector ul = mapFromRef3d(Vector(ss[0],tt[1]),Coord::CANVAS,cropsl_);

    BBox bb(ll);
    bb.bound(lr);
    bb.bound(ur);
    bb.bound(ul);

    redrawNow(bb.expand(2));
  }

  cropEnd = vv * Scale(zoom_).invert();
  Vector diff = cropEnd-cropBegin;
  if (!which)
    cropsl_ = diff[0]+zparams->zmin;
  else
    cropsl_ = diff[0]+zparams->zmax;

  // this will be incorrect for multiple ext/file cubes
  double depth = keyContext->naxis(2);
  if (!which) {
    if (cropsl_<0)
      cropsl_ = 0;
    if (cropsl_>zparams->zmax-1)
      cropsl_ = zparams->zmax-1;
  }
  else {
    if (cropsl_<zparams->zmin+1)
      cropsl_ = zparams->zmin+1;
    if (cropsl_>depth)
      cropsl_ = depth;
  }

  // and draw to window
  {
    Vector ll = mapFromRef3d(ss,Coord::WINDOW,cropsl_);
    Vector lr = mapFromRef3d(Vector(tt[0],ss[1]),Coord::WINDOW,cropsl_);
    Vector ur = mapFromRef3d(tt,Coord::WINDOW,cropsl_);
    Vector ul = mapFromRef3d(Vector(ss[0],tt[1]),Coord::WINDOW,cropsl_);

    XDrawLine(display,Tk_WindowId(tkwin),selectGCXOR,ll[0],ll[1],lr[0],lr[1]);
    XDrawLine(display,Tk_WindowId(tkwin),selectGCXOR,lr[0],lr[1],ur[0],ur[1]);
    XDrawLine(display,Tk_WindowId(tkwin),selectGCXOR,ur[0],ur[1],ul[0],ul[1]);
    XDrawLine(display,Tk_WindowId(tkwin),selectGCXOR,ul[0],ul[1],ll[0],ll[1]);
  }
}

void Frame3dBase::crop3dEndCmd(const Vector& vv, int which)
{
  // which =0 (zmin) which =1 (zmax)
  if (!keyContext->fits)
    return;

  // params is a BBOX in DATA coords 0-n
  FitsBound* params = 
    keyContext->fits->getDataParams(keyContext->secMode());
  FitsZBound* zparams = 
    keyContext->getDataParams(keyContext->secMode());
  Vector ss(params->xmin,params->ymin);
  Vector tt(params->xmax,params->ymax);

  // erase 
  if (cropBegin[0]!=cropEnd[0] || cropBegin[1]!=cropEnd[1]) {
    Vector ll = mapFromRef3d(ss,Coord::CANVAS,cropsl_);
    Vector lr = mapFromRef3d(Vector(tt[0],ss[1]),Coord::CANVAS,cropsl_);
    Vector ur = mapFromRef3d(tt,Coord::CANVAS,cropsl_);
    Vector ul = mapFromRef3d(Vector(ss[0],tt[1]),Coord::CANVAS,cropsl_);

    BBox bb(ll);
    bb.bound(lr);
    bb.bound(ur);
    bb.bound(ul);

    redrawNow(bb.expand(2));
  }

  cropEnd = vv * Scale(zoom_).invert();
  Vector diff = cropEnd-cropBegin;
  if (!which)
    cropsl_ = diff[0]+zparams->zmin;
  else
    cropsl_ = diff[0]+zparams->zmax;

  // this will be incorrect for multiple ext/file cubes
  double depth = keyContext->naxis(2);
  if (!which) {
    if (cropsl_<0)
      cropsl_ = 0;
    if (cropsl_>zparams->zmax-1)
      cropsl_ = zparams->zmax-1;
  }
  else {
    if (cropsl_<zparams->zmin+1)
      cropsl_ = zparams->zmin+1;
    if (cropsl_>depth)
      cropsl_ = depth;
  }

  if (cropBegin[0]!=cropEnd[0] || cropBegin[1]!=cropEnd[1]) {
    keyContext->setSecMode(FrScale::CROPSEC);

    // params is a BBOX in DATA coords 0-n
    if (!which)
      keyContext->setCrop3dParams(int(cropsl_+.5),zparams->zmax);
    else
      keyContext->setCrop3dParams(zparams->zmin,int(cropsl_+.5));

    // set current slice if needed
    // setSlice() IMAGE (ranges 1-n)
    // context->slice() IMAGE (ranges 1-n)
    // cropsl_ ranges coords 0-n
    double sl = keyContext->slice(2)-.5;
    if (!which) {
      if (sl<=cropsl_)
	setSlice(2,int(cropsl_+1.5));
    }
    else {
      if (sl>cropsl_)
	setSlice(2,int(cropsl_));
    }
  }
  else {
    keyContext->resetSecMode();
    keyContext->setCrop3dParams();
    FitsImage* sptr = keyContext->fits;
    while (sptr) {
      sptr->setCropParams(keyContext->datasec());
      sptr = sptr->nextSlice();
    }
  }

  keyContext->updateClip();
  updateColorScale();

  update(MATRIX);
  updateMarkerCBs(&userMarkers);
  updateMarkerCBs(&catalogMarkers);
}

void Frame3dBase::get3dBorderCmd()
{
  if (border_)
    Tcl_AppendResult(interp, "1", NULL);
  else
    Tcl_AppendResult(interp, "0", NULL);
}

void Frame3dBase::get3dBorderColorCmd()
{
  Tcl_AppendResult(interp, borderColorName_, NULL);
}

void Frame3dBase::get3dCompassCmd()
{
  if (compass_)
    Tcl_AppendResult(interp, "1", NULL);
  else
    Tcl_AppendResult(interp, "0", NULL);
}

void Frame3dBase::get3dCompassColorCmd()
{
  Tcl_AppendResult(interp, compassColorName_, NULL);
}

void Frame3dBase::get3dHighliteCmd()
{
  if (highlite_)
    Tcl_AppendResult(interp, "1", NULL);
  else
    Tcl_AppendResult(interp, "0", NULL);
}

void Frame3dBase::get3dHighliteColorCmd()
{
  Tcl_AppendResult(interp, highliteColorName_, NULL);
}

void Frame3dBase::get3dRenderMethodCmd()
{
  ostringstream str;
  switch (renderMethod_) {
  case MIP:
    str << "mip" << ends;
    break;
  case AIP:
    str << "aip" << ends;
    break;
  }
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Frame3dBase::get3dScaleCmd()
{
  ostringstream str;
  str << zscale_ << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Frame3dBase::get3dViewCmd()
{
  ostringstream str;
  str << m180To180(radToDeg(az_)) << ' ' << m180To180(radToDeg(el_)) << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Frame3dBase::get3dViewPointCmd()
{
  ostringstream str;
  str << vp_ << ' ' << viewCursor_ << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Frame3dBase::get3dRenderBackgroundCmd()
{
  switch (render_) {
  case NONE:
    Tcl_AppendResult(interp, "none", NULL);
    break;
  case AZIMUTH:
    Tcl_AppendResult(interp, "azimuth", NULL);
    break;
  case ELEVATION:
    Tcl_AppendResult(interp, "elevation", NULL);
    break;
  }
}

void Frame3dBase::getCursorCmd(Coord::InternalSystem sys)
{
  Vector aa = Vector(options->width,options->height)/2.;
  Vector bb = mapToRef(aa,Coord::WIDGET);

  ostringstream str;
  str << mapFromRef(bb, sys) << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
} 

void Frame3dBase::getCursorCmd(Coord::CoordSystem sys, Coord::SkyFrame sky,
			       Coord::SkyFormat format)
{
  if (keyContext->fits) {
    Vector aa = Vector(options->width,options->height)/2.;
    Vector bb = mapToRef(aa,Coord::WIDGET);
    printFromRef(keyContext->fits, bb, sys, sky, format);
  }
} 

void Frame3dBase::gridCmd(Coord::CoordSystem sys, Coord::SkyFrame sky, 
			  Coord::SkyFormat format, Grid::GridType type, 
			  const char* ops, const char* vars)
{
  if (grid)
    delete grid;

  switch (type) {
  case Grid::ANALYSIS:
    grid = new Grid25d(this, sys, sky, format, type, ops, vars);
    break;
  case Grid::PUBLICATION:
    grid = new Grid3d(this, sys, sky, format, type, ops, vars);
    break;
  }

  update(PIXMAP);
}

void Frame3dBase::panCmd(const Vector& v1, const Vector& v2)
{
  viewCursor_ -= (v1-v2)*Scale(1/zoom_[0],1/zoom_[1]);
  update(MATRIX);
}

void Frame3dBase::panCmd(const Vector& vv)
{
  viewCursor_ -= vv*Scale(1/zoom_[0],1/zoom_[1]);
  update(MATRIX);
}

void Frame3dBase::panCmd(const Vector& vv, Coord::CoordSystem sys,
			 Coord::SkyFrame sky)
{
  if (!keyContext->fits)
    return;

  Vector cc = Vector(options->width,options->height)/2.;
  Vector dd = mapToRef(cc,Coord::WIDGET);
  Vector uu = keyContext->fits->mapFromRef(dd, sys, sky);
  uu += vv;
  Vector ee = keyContext->fits->mapToRef(uu, sys, sky);
  Vector ff = mapFromRef(ee,Coord::WIDGET);
  viewCursor_ += (cc-ff)*Scale(1/zoom_[0],1/zoom_[1]);

  update(MATRIX);
}

void Frame3dBase::panToCmd(const Vector& vv)
{
  Vector dd = Vector(options->width,options->height)/2. - vv*canvasToWidget;
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);

  update(MATRIX);
}

void Frame3dBase::panToCmd(const Vector& vv, Coord::CoordSystem sys,
			   Coord::SkyFrame sky)
{
  if (!keyContext->fits)
    return;

  Vector aa = keyContext->fits->mapToRef(vv, sys, sky);
  Vector dd = Vector(options->width,options->height)/2. -
    mapFromRef(aa,Coord::WIDGET);
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);

  update(MATRIX);
}

void Frame3dBase::panBBoxCmd(const Vector& vv)
{
  // vv is center of panBBox in panner coordinate
  Vector aa = vv*pannerToWidget3d;
  Vector dd = Vector(options->width,options->height)/2. - aa;
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);

  update(MATRIX);
}

void Frame3dBase::panEndCmd(const Vector& vv)
{
  if (panPM)
    Tk_FreePixmap(display, panPM);
  panPM = 0;

  Vector dd = vv*canvasToWidget - panCursor*canvasToWidget;
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);

  update(MATRIX);
}

void Frame3dBase::rotateBeginCmd()
{
  // save the current rotation
  rotateRotation = rotation;
}

void Frame3dBase::rotateMotionCmd(double angle)
{
  rotation = rotateRotation + angle;
  update(MATRIX);
}

void Frame3dBase::rotateEndCmd()
{
  update(MATRIX);
}

void Frame3dBase::set3dBorderCmd(int hh)
{
  border_ = hh;
  update(PIXMAP);
}

void Frame3dBase::set3dBorderColorCmd(const char* clr)
{
  if (borderColorName_)
    delete [] borderColorName_;
  borderColorName_ = dupstr(clr);

  if (threedGC)
    XSetForeground(display, threedGC, getColor(borderColorName_));
  update(PIXMAP);
}

void Frame3dBase::set3dCompassCmd(int hh)
{
  compass_ = hh;
  update(PIXMAP);
}

void Frame3dBase::set3dCompassColorCmd(const char* clr)
{
  if (compassColorName_)
    delete [] compassColorName_;
  compassColorName_ = dupstr(clr);

  if (threedGC)
    XSetForeground(display, threedGC, getColor(compassColorName_));
  update(PIXMAP);
}

void Frame3dBase::set3dHighliteCmd(int hh)
{
  highlite_ = hh;
  update(PIXMAP);
}

void Frame3dBase::set3dHighliteColorCmd(const char* clr)
{
  if (highliteColorName_)
    delete [] highliteColorName_;
  highliteColorName_ = dupstr(clr);

  if (threedGC)
    XSetForeground(display, threedGC, getColor(highliteColorName_));
  update(PIXMAP);
}

void Frame3dBase::set3dRenderMethodCmd(int m)
{
  RenderMethod mm = (RenderMethod)m;
  if (mm!=renderMethod_) {
    renderMethod_ = mm;
    update(MATRIX);
  }
}

void Frame3dBase::set3dScaleCmd(double ss)
{
  if (zscale_ == ss)
    return;

  zscale_ = ss;
  // just in case if pending az/el 
  preservecache_ =0;
  update(MATRIX);
}

void Frame3dBase::set3dViewCmd(float az, float el)
{
  az_ = zeroTWOPI(degToRad(az));
  el_ = zeroTWOPI(degToRad(el));
  preservecache_ =1;
  update(MATRIX);
}

void Frame3dBase::set3dViewPointCmd(const Vector3d& vv, const Vector& cc)
{
  vp_ = vv;
  viewCursor_ = cc;
  update(MATRIX);
}

void Frame3dBase::set3dRenderBackgroundCmd(int which)
{
  if (which != render_) {
    render_ = (MotionType)which;
    cancelDetach();
    preservecache_ =1;
    update(BASE);
  }
}

void Frame3dBase::set3dPreserveCmd()
{
  preservecache_ =1;
}

void Frame3dBase::zoomAboutCmd(const Vector& zz, const Vector& vv)
{
  Vector dd = Vector(options->width,options->height)/2. - vv*canvasToWidget;
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);

  Vector az = ((Vector&)zz).abs();
  zoom_[0] *= az[0];
  zoom_[1] *= az[1];

  update(MATRIX);
}

void Frame3dBase::zoomAboutCmd(const Vector& zz, const Vector& vv, 
			       Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  if (!keyContext->fits)
    return;

  Vector aa = keyContext->fits->mapToRef(vv, sys, sky);
  Vector dd = Vector(options->width,options->height)/2. -
    mapFromRef(aa,Coord::WIDGET);
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);

  Vector az = ((Vector&)zz).abs();
  zoom_[0] *= az[0];
  zoom_[1] *= az[1];

  update(MATRIX);
}

void Frame3dBase::zoomToAboutCmd(const Vector& zz, const Vector& vv)
{
  Vector dd = Vector(options->width,options->height)/2. - vv*canvasToWidget;
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);
  zoom_ = ((Vector&)zz).abs();

  update(MATRIX);
}

void Frame3dBase::zoomToAboutCmd(const Vector& zz, const Vector& vv, 
			       Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  if (!keyContext->fits)
    return;

  Vector aa = keyContext->fits->mapToRef(vv, sys, sky);
  Vector dd = Vector(options->width,options->height)/2. -
    mapFromRef(aa,Coord::WIDGET);
  viewCursor_ += dd*Scale(1/zoom_[0],1/zoom_[1]);
  zoom_ = ((Vector&)zz).abs();

  update(MATRIX);
}

void Frame3dBase::zoomToFitCmd(double ss)
{
  if (!keyContext->fits)
    return;

  centerImage();

  Vector3d tt = imageSize3d(keyContext->secMode());
  double zz = calcZoom3d(tt,Vector(options->width,options->height)) * ss;
  zoom_ = Vector(zz,zz);

  update(MATRIX);
}