summaryrefslogtreecommitdiffstats
path: root/src/tkbltGrMarker.C
blob: 7edd1193aa8ed89e1ac4c9e468e2fc14ccdc6af7 (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
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

/*
 *	Copyright 1993-2004 George A Howlett.
 *
 *	Permission is hereby granted, free of charge, to any person obtaining
 *	a copy of this software and associated documentation files (the
 *	"Software"), to deal in the Software without restriction, including
 *	without limitation the rights to use, copy, modify, merge, publish,
 *	distribute, sublicense, and/or sell copies of the Software, and to
 *	permit persons to whom the Software is furnished to do so, subject to
 *	the following conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>
#include <math.h>
#include <float.h>

#include "bltGraph.h"
#include "bltGrBind.h"
#include "bltGrMarker.h"
#include "tkbltGrAxis.h"
#include "bltGrMisc.h"

using namespace Blt;

Marker::Marker(Graph* graphPtr, const char* name, Tcl_HashEntry* hPtr)
{
  optionTable_ =NULL;
  ops_ =NULL;

  graphPtr_ =graphPtr;  
  name_ = dupstr(name);
  hashPtr_ = hPtr;
  link =NULL;
  flags =0;
  clipped_ =0;
}

Marker::~Marker()
{
  graphPtr_->bindTable_->deleteBindings(this);

  if (link)
    graphPtr_->markers_.displayList->deleteLink(link);

  if (hashPtr_)
    Tcl_DeleteHashEntry(hashPtr_);

  if (name_)
    delete [] name_;

  Tk_FreeConfigOptions((char*)ops_, optionTable_, graphPtr_->tkwin_);
  free(ops_);
}

double Marker::HMap(Axis *axisPtr, double x)
{
  AxisOptions* ops = (AxisOptions*)axisPtr->ops();

  if (x == DBL_MAX)
    x = 1.0;
  else if (x == -DBL_MAX)
    x = 0.0;
  else {
    if (ops->logScale) {
      if (x > 0.0)
	x = log10(x);
      else if (x < 0.0)
	x = 0.0;
    }
    x = (x - axisPtr->axisRange_.min) * axisPtr->axisRange_.scale;
  }
  if (ops->descending)
    x = 1.0 - x;

  // Horizontal transformation
  return (x * axisPtr->screenRange_ + axisPtr->screenMin_);
}

double Marker::VMap(Axis *axisPtr, double y)
{
  AxisOptions* ops = (AxisOptions*)axisPtr->ops();

  if (y == DBL_MAX)
    y = 1.0;
  else if (y == -DBL_MAX)
    y = 0.0;
  else {
    if (ops->logScale) {
      if (y > 0.0)
	y = log10(y);
      else if (y < 0.0)
	y = 0.0;
    }
    y = (y - axisPtr->axisRange_.min) * axisPtr->axisRange_.scale;
  }
  if (ops->descending)
    y = 1.0 - y;

  // Vertical transformation
  return (((1.0 - y) * axisPtr->screenRange_) + axisPtr->screenMin_);
}

Point2d Marker::mapPoint(Point2d* pointPtr, Axis* xAxis, Axis* yAxis)
{
  GraphOptions* gops = (GraphOptions*)graphPtr_->ops_;
  Point2d result;
  if (gops->inverted) {
    result.x = HMap(yAxis, pointPtr->y);
    result.y = VMap(xAxis, pointPtr->x);
  }
  else {
    result.x = HMap(xAxis, pointPtr->x);
    result.y = VMap(yAxis, pointPtr->y);
  }

  return result;
}

int Marker::boxesDontOverlap(Graph* graphPtr_, Region2d *extsPtr)
{
  return (((double)graphPtr_->right_ < extsPtr->left) ||
	  ((double)graphPtr_->bottom_ < extsPtr->top) ||
	  (extsPtr->right < (double)graphPtr_->left_) ||
	  (extsPtr->bottom < (double)graphPtr_->top_));
}

int Marker::regionInPolygon(Region2d *regionPtr, Point2d *points, int nPoints,
			    int enclosed)
{
  if (enclosed) {
    // All points of the polygon must be inside the rectangle.
    for (Point2d *pp = points, *pend = pp + nPoints; pp < pend; pp++) {
      if ((pp->x < regionPtr->left) || (pp->x > regionPtr->right) ||
	  (pp->y < regionPtr->top) || (pp->y > regionPtr->bottom)) {
	return 0;	/* One point is exterior. */
      }
    }
    return 1;
  }
  else {
    // If any segment of the polygon clips the bounding region, the
    // polygon overlaps the rectangle.
    points[nPoints] = points[0];
    for (Point2d *pp = points, *pend = pp + nPoints; pp < pend; pp++) {
      Point2d p = *pp;
      Point2d q = *(pp + 1);
      if (lineRectClip(regionPtr, &p, &q))
	return 1;
    }

    // Otherwise the polygon and rectangle are either disjoint or
    // enclosed.  Check if one corner of the rectangle is inside the polygon.
    Point2d r;
    r.x = regionPtr->left;
    r.y = regionPtr->top;

    return pointInPolygon(&r, points, nPoints);
  }
}