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

#include "basebox.h"
#include "fitsimage.h"
#include "util.h"

BaseBox::BaseBox(Base* p, const Vector& ctr, double ang)
  : BaseMarker(p, ctr, ang)
{
  numPoints_ = 5;
  vertices_ = NULL;
}

BaseBox::BaseBox(Base* p, const Vector& ctr, 
		 double ang,
		 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)
  : BaseMarker(p, ctr, ang, clr, dsh, w, f, t, prop, c, tag, cb)
{
  numPoints_ = 5;
  vertices_ = NULL;
}

BaseBox::BaseBox(const BaseBox& a) : BaseMarker(a) 
{
  numPoints_ = a.numPoints_;
  vertices_ = NULL;
}

BaseBox::~BaseBox()
{
  deleteVertices();
}

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

  newVertices();
  for (int ii=0; ii<numAnnuli_; ii++) {
    XPoint* pp = new XPoint[numPoints_];
    for (int jj=0; jj<numPoints_; jj++) {
      Vector v = parent->mapFromRef(vertices_[ii][jj],sys);
      pp[jj].x = (short)v[0];
      pp[jj].y = (short)v[1];
    }
    renderXDraw(drawable, lgc, pp, mode);
    delete [] pp;
  }
  deleteVertices();
}

void BaseBox::renderXDraw(Drawable drawable, GC lgc, XPoint* pp, 
			  RenderMode mode)
{
  XDrawLines(display, drawable, lgc, pp, numPoints_, CoordModeOrigin);
}

void BaseBox::renderPS(PSColorSpace mode)
{
  renderPSGC(mode);

  newVertices();
  for (int ii=0; ii<numAnnuli_; ii++)
    renderPSDraw(ii);
  deleteVertices();
}

void BaseBox::renderPSDraw(int ii)
{
  ostringstream str;
  for (int jj=0; jj<numPoints_; jj++) {
    Vector v =  parent->mapFromRef(vertices_[ii][jj],Coord::CANVAS);
    if (jj==0)
      str << "newpath " 
	  << parent->TkCanvasPs(v) << " moveto" << endl;
    else
      str << parent->TkCanvasPs(v) << " lineto" << endl;
  }
  str << "stroke" << endl << ends;
  Tcl_AppendResult(parent->interp, str.str().c_str(), NULL);
}

void BaseBox::renderPSFillDraw(int ii)
{
  ostringstream str;
  for (int jj=0; jj<numPoints_; jj++) {
    Vector v =  parent->mapFromRef(vertices_[ii][jj],Coord::CANVAS);
    if (jj==0)
      str << "newpath " 
	  << parent->TkCanvasPs(v) << " moveto" << endl;
    else
      str << parent->TkCanvasPs(v) << " lineto" << endl;
  }
  str << "fill" << endl << ends;
  Tcl_AppendResult(parent->interp, str.str().c_str(), NULL);
}

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

  newVertices();
  for (int ii=0; ii<numAnnuli_; ii++) {
    Vector* vv = new Vector[numPoints_];
    for (int jj=0; jj<numPoints_; jj++)
      vv[jj] = parent->mapFromRef(vertices_[ii][jj],Coord::CANVAS);
    macosxDrawLines(vv, numPoints_);
    delete [] vv;
  }
  deleteVertices();
}

void BaseBox::renderMACOSXDraw(Vector* vv)
{
  macosxDrawLines(vv, numPoints_);
}
#endif

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

  newVertices();
  for (int ii=0; ii<numAnnuli_; ii++) {
    Vector* vv = new Vector[numPoints_];
    for (int jj=0; jj<numPoints_; jj++)
      vv[jj] = parent->mapFromRef(vertices_[ii][jj],Coord::CANVAS);
    win32DrawLines(vv, numPoints_);
    delete [] vv;
  }
  deleteVertices();
}

void BaseBox::renderWIN32Draw(Vector* vv)
{
  win32DrawLines(vv, numPoints_);
}
#endif

// Support

void BaseBox::updateHandles()
{
  // handles are in canvas coords
  // we can't garantee that the radii have been sorted yet
  // also, annuli[i] could be negative

  if (handle)
    delete [] handle;
  handle = new Vector[numHandle];

  Vector max;
  for(int ii=0; ii<numAnnuli_; ii++) {
    Vector aa = annuli_[ii].abs();
    if (max[0]<aa[0])
      max = aa;
  }
  Vector& s = max;

  handle[0] = fwdMap(Vector(-s[0],-s[1])/2,Coord::CANVAS);
  handle[1] = fwdMap(Vector( s[0],-s[1])/2,Coord::CANVAS);
  handle[2] = fwdMap(Vector( s[0], s[1])/2,Coord::CANVAS);
  handle[3] = fwdMap(Vector(-s[0], s[1])/2,Coord::CANVAS);

  // annuli
  if (numAnnuli_>1)
    for (int ii=0; ii<numAnnuli_; ii++)
      handle[ii+4] = fwdMap(Vector((annuli_[ii][0])/2.,0),Coord::CANVAS);
}


int BaseBox::isInRef(const Vector& vv, int nn)
{
  // during resize, annuli_ can be negative
  Vector ss = annuli_[nn].abs();

  // zero radius
  if (!ss[0] || !ss[1])
    return 0;

  Vector pp = (Vector&)vv;
  if (pp[0]<-ss[0]/2 || pp[0]>=ss[0]/2 || pp[1]<=-ss[1]/2 || pp[1]>ss[1]/2)
    return 0;
  else
    return 1;
}

Vector BaseBox::intersect(Vector rr, double aa)
{
  // special cases
  if (!rr[0] || !rr[1])
    return Vector();

  double ang = zeroTWOPI(aa);
  double phi = rr.angle();

  if (ang >= 0 && ang < phi)
    return Vector(rr[0],-rr[0]*tan(ang));
  else if (ang >= phi && ang < M_PI-phi)
    return Vector(rr[1]/tan(ang),-rr[1]);
  else if (ang >= M_PI-phi && ang < M_PI+phi)
    return Vector(-rr[0],rr[0]*tan(ang));
  else if (ang >= M_PI+phi && ang < M_TWOPI-phi)
    return Vector(-rr[1]/tan(ang),rr[1]);
  else
    return Vector(rr[0],-rr[0]*tan(ang));
}

void BaseBox::newVertices()
{
  if (vertices_)
    deleteVertices();

  if (teq(startAng_,stopAng_-M_TWOPI,FLT_EPSILON))
    newVerticesA();
  else
    newVerticesB();
}

void BaseBox::newVerticesA()
{
  Matrix mm = fwdMatrix();

  numPoints_ = 5;
  vertices_ = new Vector*[numAnnuli_+1];
  for (int i=0; i<numAnnuli_; i++)
    vertices_[i]= new Vector[numPoints_];
  vertices_[numAnnuli_] = new Vector[2];

  for (int i=0; i<numAnnuli_; i++) {
    vertices_[i][0] = Vector(-annuli_[i][0]/2.,-annuli_[i][1]/2.) * mm;
    vertices_[i][1] = Vector( annuli_[i][0]/2.,-annuli_[i][1]/2.) * mm;
    vertices_[i][2] = Vector( annuli_[i][0]/2., annuli_[i][1]/2.) * mm;
    vertices_[i][3] = Vector(-annuli_[i][0]/2., annuli_[i][1]/2.) * mm;
    vertices_[i][4] = Vector(-annuli_[i][0]/2.,-annuli_[i][1]/2.) * mm;
  }

  vertices_[numAnnuli_][0] = vertices_[numAnnuli_-1][0];
  vertices_[numAnnuli_][1] = vertices_[numAnnuli_-1][2];
}

void BaseBox::newVerticesB()
{
  numPoints_ = 0; // we will increment later
  vertices_ = new Vector*[numAnnuli_+1];
  for (int ii=0; ii<numAnnuli_; ii++)
    vertices_[ii]= new Vector[7];
  vertices_[numAnnuli_] = new Vector[2];

  double a1 = zeroTWOPI(startAng_);
  double a2 = zeroTWOPI(stopAng_);
  if (a2 <= a1)
    a2 += M_TWOPI;

  int cnt = 0;
  for (int ii=0; ii<numAnnuli_; ii++) {
    Vector rr = (annuli_[ii]).abs();
    if (rr[0] && rr[1]) {
      double phi = rr.angle();
      int s1 =0;
      int s2 =0;
      cnt = 0;

      vertBTest(&s1, &s2, a1, a2, 0, phi, ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, phi, M_PI-phi, ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_PI-phi, M_PI+phi, ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_PI+phi, M_TWOPI-phi, ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_TWOPI-phi,M_TWOPI , ii, &cnt);

      vertBTest(&s1, &s2, a1, a2, M_TWOPI,M_TWOPI+phi , ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_TWOPI+phi,M_THREEPI-phi , ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_THREEPI-phi,M_THREEPI+phi , ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_THREEPI+phi,M_FOURPI-phi , ii, &cnt);
      vertBTest(&s1, &s2, a1, a2, M_FOURPI-phi,M_FOURPI, ii, &cnt);
    }
  }

  // final number of segments
  numPoints_ = ++cnt;

  // include/exclude
  Matrix mm = fwdMatrix();
  vertices_[numAnnuli_][0] = 
    Vector(-annuli_[numAnnuli_-1][0]/2.,-annuli_[numAnnuli_-1][1]/2.) * mm;
  vertices_[numAnnuli_][1] = 
    Vector( annuli_[numAnnuli_-1][0]/2., annuli_[numAnnuli_-1][1]/2.) * mm;
}

void BaseBox::vertBTest(int* s1, int* s2, double a1, double a2, 
			double b1, double b2, int ii, int* cnt)
{
  if ((!(*s1)) && (a1>=b1) && (a1<b2))
    *s1 =1;
  if ((!(*s2)) && (a2>b1) && (a2<=b2))
    *s2 =1;

  if (((*s1) && (!(*s2))) || ((*s1) && (*s2)))
    vertBPrep(a1,a2,b1,b2,ii,cnt);

  if (*s1&&*s2)
    *s1=*s2=0;
}

void BaseBox::vertBPrep(double a1, double a2, double ll, double ul, 
			int ii, int* cnt)
{
  if (!(a1 >= ll) && (a1 <= ul))
    a1 = ll;
  if (!(a2 >= ll) && (a2 <= ul))
    a2 = ul;

  if (a1 > a2) {
    vertBSeg(ll,a2,ii,cnt);
    vertBSeg(a1,ul,ii,cnt);
  }
  else
    vertBSeg(a1,a2,ii,cnt);
}

void BaseBox::vertBSeg(double ang1, double ang2, int ii, int* cnt)
{
  Vector rr = (annuli_[ii]/2).abs();
  Matrix mm = fwdMatrix();

  vertices_[ii][(*cnt)++] = intersect(rr,ang1)*mm;
  vertices_[ii][(*cnt)] = intersect(rr,ang2)*mm;
}

void BaseBox::deleteVertices()
{
  if (vertices_) {
    for (int i=0; i<numAnnuli_+1; i++)
      if (vertices_[i])
	delete [] vertices_[i];
    delete [] vertices_;
  }
  vertices_ = NULL;
}