-
Notifications
You must be signed in to change notification settings - Fork 1
/
Function_Nodes.cc
716 lines (645 loc) · 14.1 KB
/
Function_Nodes.cc
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* Function_Nodes
HiROC CVS ID: $Id: Function_Nodes.cc,v 1.3 2012/03/09 02:13:55 castalia Exp $
Copyright (C) 2010-2011 Arizona Board of Regents on behalf of the
Planetary Image Research Laboratory, Lunar and Planetary Laboratory at
the University of Arizona.
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License, version 2.1,
as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*******************************************************************************/
#include "Function_Nodes.hh"
#include "HiView_Config.hh"
#include "HiView_Utilities.hh"
#include <iostream>
#include <iomanip>
using std::endl;
#if defined (DEBUG_SECTION)
/* DEBUG_SECTION controls
DEBUG_SECTION report selection options.
Define any of the following options to obtain the desired debug reports:
*/
#define DEBUG_OFF 0
#define DEBUG_ALL -1
#define DEBUG_CONSTRUCTORS (1 << 0)
#define DEBUG_ACCESSORS (1 << 1)
#define DEBUG_MANIPULATORS (1 << 2)
#define DEBUG_INTERPOLATORS (1 << 3)
#define DEBUG_DEFAULT DEBUG_CONSTRUCTORS
#if (DEBUG_SECTION +0) == 0
#undef DEBUG_SECTION
#define DEBUG_SECTION DEBUG_OFF
#endif
#include <iostream>
using std::clog;
using std::boolalpha;
using std::setw;
#endif // DEBUG_SECTION
namespace UA::HiRISE
{
/*==============================================================================
Constants
*/
const char* const
Function_Nodes::ID =
"UA::HiRISE::Function_Nodes ($Revision: 1.3 $ $Date: 2012/03/09 02:13:55 $)";
/*==============================================================================
Application configuration parameters
*/
#define MAX_DISPLAY_VALUE \
HiView_Config::MAX_DISPLAY_VALUE
/*==============================================================================
Constructor
*/
Function_Nodes::Function_Nodes
(
Data_Map* data_map
)
: QwtSeriesStore (),
Map (data_map)
{
#if ((DEBUG_SECTION) & DEBUG_CONSTRUCTORS)
clog << ">>> Function_Nodes @ " << (void*)this
<< ": Data_Map @ " << (void*)Map << endl;
#endif
reset ();
#if ((DEBUG_SECTION) & DEBUG_CONSTRUCTORS)
clog << "<<< Function_Nodes" << endl;
#endif
}
/*==============================================================================
Accessors
*/
void
Function_Nodes::data_map
(
Data_Map* data_map
)
{
if (data_map)
{
Map = data_map;
reset ();
}
}
int
Function_Nodes::max_y () const
{return MAX_DISPLAY_VALUE;}
int
Function_Nodes::node_index_of
(
int x
)
const
{
int
index = Nodes.size ();
if (index &&
x >= 0 &&
x <= max_x ())
while (index-- &&
Nodes[index].x () != x) ;
else
index = -1;
return index;
}
int
Function_Nodes::lower_bound () const
{
int
index = -1,
total_nodes = Nodes.size ();
while (++index < total_nodes &&
Nodes[index].y () == 0) ;
if (index)
index--;
return Nodes[index].x ();
}
int
Function_Nodes::lower_bound
(
int x
)
{
if (x < 0)
x = 0;
else
if (x > max_x ())
x = max_x ();
// Find the index of the current lower bound node.
int
changed = 0,
index = -1,
total_nodes = Nodes.size ();
while (++index < total_nodes &&
Nodes[index].ry () == 0) ;
if (index)
index--;
else
{
// Move low end anchor to zero.
Nodes[index].ry () = 0;
changed = SHIFTED_NODE;
}
if (Nodes[index].rx () != x)
changed |= move_node (Nodes[index], QPoint (x, 0));
return changed;
}
int
Function_Nodes::upper_bound () const
{
int
index = Nodes.size ();
while (index-- &&
Nodes[index].y () == max_y ()) ;
if (index < last_node_index ())
index++;
return Nodes[index].x ();
}
int
Function_Nodes::upper_bound
(
int x
)
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << ">>> Function_Nodes::upper_bound: " << x << endl;
#endif
if (x < 0)
x = 0;
else
if (x > max_x ())
x = max_x ();
int
changed = 0,
index = Nodes.size ();
while (index-- &&
Nodes[index].ry () == max_y ()) ;
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " selected upper bound node index = " << index << endl
<< " last_node_index = " << last_node_index () << endl;
#endif
if (index < last_node_index ())
index++;
else
{
// Move high end anchor to max_y.
Nodes[index].ry () = max_y ();
changed = SHIFTED_NODE;
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " repositioned anchor node[" << index << "] = "
<< Nodes[index] << endl;
#endif
}
if (Nodes[index].rx () != x)
changed |= move_node (Nodes[index], QPoint (x, max_y ()));
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << "<<< Function_Nodes::upper_bound: " << changed
<< " (removed " << (changed & REMOVED_NODES_MASK)
<< ", added " << ((changed & ADDED_NODE) ? '1' : '0')
<< ", shifted " << ((changed & SHIFTED_NODE) ? '1' : '0')
<< ')' <<endl;
#endif
return changed;
}
void
Function_Nodes::print
(
std::ostream& stream
)
const
{
for (int
index = 0;
index < Nodes.size ();
index++)
stream << " node " << index << " = " << Nodes.at (index) << endl;
}
/*==============================================================================
Manipulators
*/
int
Function_Nodes::add_node
(
int x,
int y
)
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << ">>> Function_Nodes::add_node: " << x << "x, " << y << 'y' << endl;
#endif
int
changed = 0;
if (x >= 0 &&
x <= max_x () &&
y >= 0 &&
y <= max_y ())
{
int
index = Nodes.size ();
while (index--)
{
if (Nodes[index].rx () > x)
continue;
if (Nodes[index].rx () == x)
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " replace node " << index
<< " = " << Nodes[index] << endl;
#endif
if (Nodes[index].ry () != y)
{
Nodes[index].ry () = y;
changed = SHIFTED_NODE;
}
}
else
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " insert node " << index << endl;
#endif
Nodes.insert (index, QPoint (x, y));
changed = ADDED_NODE;
}
break;
}
if (changed)
// Interpolate around the added node.
interpolate (index);
}
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << "<<< Function_Nodes::add_node: " << changed << " ("
<< ((changed & ADDED_NODE) ? "added" :
((changed & SHIFTED_NODE) ? "shifted" : "no change")) << ')' <<endl;
#endif
return changed;
}
int
Function_Nodes::move_node
(
const QPoint& from,
const QPoint& to
)
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << ">>> Function_Nodes::move_node: from " << from << " to " << to << endl;
#endif
int
changed = 0;
int
x0 = from.x (),
x1 = to.x (),
y1 = to.y ();
if (x0 >= 0 &&
x0 <= max_x () &&
x1 >= 0 &&
x1 <= max_x () &&
y1 >= 0 &&
y1 <= max_y ())
{
int
index = -1,
total_nodes = Nodes.size ();
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " total_nodes = " << total_nodes << endl;
#endif
// Find the node at or above the from node.
while (++index < total_nodes &&
Nodes[index].rx () < x0) ;
if (Nodes[index].rx () == x0)
{
// Move from existing node.
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " existing from node[" << index << "] = "
<< Nodes[index] << endl;
#endif
if (x0 == x1)
{
// The node does not move horizontally.
if (Nodes[index].ry () != y1)
{
// The node only moves vertically.
Nodes[index].ry () = y1;
changed = SHIFTED_NODE;
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " +from node[" << index << "] = "
<< Nodes[index] << endl;
#endif
}
goto Update_Data_Map;
}
else
if (index &&
index < last_node_index ())
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " -remove node[" << index << "] = "
<< Nodes[index] << endl;
#endif
Nodes.remove (index);
++changed;
}
}
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
else
clog << " next index = " << index << endl;
#endif
// Remove all intervening nodes.
if (x0 <= x1)
{
/* >>> CAUTION <<<
Nodes may be removed from the list during a bottom-up
traversal. When a node is removed the next node up becomes
the node at the index of the node that was removed, so in
this case the traversal index does not increment; however, it
does increment if no removal occurs.
*/
while (index < Nodes.size () &&
Nodes[index].rx () < x1)
{
if (index &&
index < last_node_index ())
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " -remove node[" << index << "] = "
<< Nodes[index] << endl;
#endif
Nodes.remove (index);
++changed;
}
else
++index;
}
}
else
{
while (index-- &&
Nodes[index].rx () > x1)
{
if (index &&
index < last_node_index ())
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " -remove node[" << index << "] = "
<< Nodes[index] << endl;
#endif
Nodes.remove (index);
++changed;
}
}
}
if (Nodes[index].rx () == x1)
{
// Move to existing node.
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " existing to node[" << index << "] = "
<< Nodes[index] << endl;
#endif
if (Nodes[index].ry () != y1)
{
Nodes[index].ry () = y1;
changed |= SHIFTED_NODE;
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " +to node[" << index << "] = "
<< Nodes[index] << endl;
#endif
}
}
else
{
// Insert new node.
if (x0 > x1)
++index;
Nodes.insert (index, to);
changed |= ADDED_NODE;
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " +add node[" << index << "] = "
<< Nodes[index] << endl;
#endif
}
Update_Data_Map:
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " total_nodes = " << total_nodes << endl;
#endif
if (changed)
{
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << " interpolate around node[" << index << "] = "
<< Nodes[index] << endl;
#endif
interpolate (index);
}
}
#if ((DEBUG_SECTION) & DEBUG_MANIPULATORS)
clog << "<<< Function_Nodes::move_node: " << changed
<< " (removed " << (changed & REMOVED_NODES_MASK)
<< ", added " << ((changed & ADDED_NODE) ? '1' : '0')
<< ", shifted " << ((changed & SHIFTED_NODE) ? '1' : '0')
<< ')' <<endl;
#endif
return changed;
}
bool
Function_Nodes::remove_node
(
int x
)
{
bool
changed = false;
int
index = node_index_of (x);
// Don't remove node end points.
if (index > 0 &&
index < max_x ())
{
changed = true;
// Remap between the bounding nodes.
linear_interpolation (Nodes[index - 1], Nodes[index + 1], Map);
// Remove the specified node.
Nodes.remove (index);
}
return changed;
}
bool
Function_Nodes::reset ()
{
QPoint
lower_anchor, // Low end anchor.
upper_anchor (max_x (), max_y ()); // High end anchor.
if (Nodes.size () != 2 ||
Nodes.at (0) != lower_anchor ||
Nodes.at (1) != upper_anchor)
{
Nodes.clear ();
Nodes
<< lower_anchor
<< upper_anchor;
interpolate ();
return true;
}
return false;
}
void
Function_Nodes::interpolate
(
int node_index
)
{
if (node_index)
linear_interpolation (Nodes[node_index - 1], Nodes[node_index], Map);
if (node_index < last_node_index ())
linear_interpolation (Nodes[node_index], Nodes[node_index + 1], Map);
}
void
Function_Nodes::interpolate ()
{
int
index = 0,
total_nodes = Nodes.size ();
while (++index < total_nodes)
linear_interpolation (Nodes[index -1], Nodes[index], Map);
}
/*==============================================================================
QwtData implementation
*/
// N.B.: No copying; just return self.
QwtSeriesStore<QPointF>*
Function_Nodes::copy () const
{return const_cast<Function_Nodes*>(this);}
size_t
Function_Nodes::size () const
{return Nodes.size ();}
double
Function_Nodes::x
(
size_t node_index
) const
{
if (node_index < (size_t)Nodes.size ())
return Nodes.at ((int)node_index).x ();
return -1;
}
double
Function_Nodes::y
(
size_t node_index
) const
{
if (node_index < (size_t)Nodes.size ())
return Nodes.at ((int)node_index).y ();
return -1;
}
QPointF Function_Nodes::sample (size_t node_index) const
{
return Nodes.at((int)node_index);
}
QRectF
Function_Nodes::boundingRect () const
{
return QRectF
(
0, // Left.
max_y (), // Top.
max_x (), // Width.
max_y () // Height.
);
}
/*==============================================================================
Utilities
*/
void
Function_Nodes::linear_interpolation
(
int x0,
int y0,
int x1,
int y1,
Data_Map* data_map
)
{
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
clog << ">>> Function_Nodes::linear_interpolation: from "
<< x0 << "x, " << y0 << "y to " << x1 << "x, " << y1 << "y"
" for Data_Map @ " << (void*)data_map << endl;
#endif
if (data_map &&
x0 != x1)
{
if (x0 > x1)
{
// Must be monotonically increasing from x0 to x1.
int
value;
value = x0;
x0 = x1;
x1 = value;
value = y0;
y0 = y1;
y1 = value;
}
// Slope of the line.
double
slope = double (y1 - y0) / (x1 - x0);
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
clog << " slope = " << slope << endl;
#endif
// Clip to the data range.
int
x = data_map->size () - 1;
if (x0 < 0)
x0 = 0;
else
if (x0 > x)
x0 = x;
if (x1 < 0)
x1 = 0;
else
if (x1 > x)
x1 = x;
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
clog << " data range " << x0 << " - " << x1 << endl;
#endif
if (x0 != x1)
{
Data_Map::value_type
datum;
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
clog << " interpolated values - " << endl
<< " " << setw (4) << x0 << ':';
#endif
for (x = x0;
x <= x1;
++x)
{
datum = static_cast<Data_Map::value_type>
(y0 + ((x - x0) * slope) + 0.5);
(*data_map)[x] = datum;
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
clog << ' ' << setw (3) << ((int)datum & 0xFF);
if (x &&
! ((x + 1) % 16))
{
clog << endl;
if (x != x1)
clog << " " << setw (4) << (x + 1) << ':';
}
#endif
}
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
if (x1 % 16)
clog << endl;
#endif
}
}
#if ((DEBUG_SECTION) & DEBUG_INTERPOLATORS)
clog << "<<< Function_Nodes::linear_interpolation" << endl;
#endif
}
} // namespace UA::HiRISE