-
Notifications
You must be signed in to change notification settings - Fork 59
/
arbitrarywavwidget.cpp
807 lines (689 loc) · 26.8 KB
/
arbitrarywavwidget.cpp
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#include "arbitrarywavwidget.h"
#include "bezier.h"
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QFileDialog>
ArbitraryWavWidget::ArbitraryWavWidget(QWidget *parent)
: QWidget{parent} {
mirror_null = 0;
mirror_radius = 12*25; // 24 inch mirror by default
wave_height=0.3; // by default we start at +/- .25 waves of vertical scale
ww_unit=in; // default inches
double bez_dist = mirror_radius/2;
pts.append(CPoint(0,0, bez_dist)); // mandatory
//pts.append(CPoint(mirror_radius,0.125, bez_dist)); // optional starting points
// debugging test pts[1].setLeft(pts[1].x()-3, pts[1].y()-.3,0.1);
bDragging=false;
bDraggingBezierPoint = false;
// the following are just initial values that will not let the transx() and transy() functions get a divide by zero
pos_y0=1;
graph_height=2;
graph_left = 1;
pos_edge = 2;
}
void ArbitraryWavWidget::setRadius(double radius) {
if (radius == mirror_radius)
return;
mirror_radius = radius;
double bez_dist = mirror_radius/2;
pts.clear();
pts.append(CPoint(0,0, bez_dist)); // mandatory
//pts.append(CPoint(mirror_radius,0.125, bez_dist)); // optional starting points
bDragging=false;
bDraggingBezierPoint = false;
}
void ArbitraryWavWidget::showPrepare() {
// for testing - display the prepared data in this graph widget
bDrawCalculatedPoints=true;
update(); // redraw
}
void ArbitraryWavWidget::setMode(int _mode) {
mode = _mode;
update(); // redraw
}
ArbitraryWavWidget::~ArbitraryWavWidget(){
pts.empty();
}
bool comparePoints(const QPointF &a, const QPointF &b) {
return a.x() < b.x();
}
void ArbitraryWavWidget::sortPoints() {
std::sort(pts.begin(), pts.end(), comparePoints);
}
QSize ArbitraryWavWidget::sizeHint() const {
return QSize(1400,400);
}
QSize ArbitraryWavWidget::minimumSizeHint() const {
return QSize(200,100);
}
void ArbitraryWavWidget::resizeEvent(QResizeEvent * /*event*/) {
}
// these next 4 functions: transx translates distance in mm to position in our graph and back
// transy translates wavelength to position in our graph and back
int ArbitraryWavWidget::transx(double r){
return (r/mirror_radius)*(pos_edge-graph_left) + graph_left;
}
int ArbitraryWavWidget::transy(double y){
// -y because positive pixels is in the negative y direction (more positive pixels towards bottom of graph)
return -y/wave_height*(graph_height-pos_y0) + pos_y0;
}
double ArbitraryWavWidget::transx(int x){
return (x-graph_left)*mirror_radius/(pos_edge-graph_left);
}
double ArbitraryWavWidget::transy(int y){
return (y-pos_y0)*(-wave_height)/(graph_height-pos_y0);
}
double ArbitraryWavWidget::transRatio(){
return fabs((wave_height)/(graph_height-pos_y0) / (mirror_radius/(pos_edge-graph_left)));
}
int ArbitraryWavWidget::findPoint(QPoint p1) {
for(int i=0; i<pts.size(); i++) {
QPointF p2 = pts.at(i);
if ( (abs(p1.x() - transx(p2.x())) < 8) &&
(abs(p1.y() - transy(p2.y())) < 8) ) {
return i;
}
}
return -1; // not found
}
void ArbitraryWavWidget::readJson(){
QSettings set;
QString last_path = set.value("userProfilePath", ".").toString();
QString fileName = QFileDialog::getOpenFileName((QWidget* )0, "Save PDF",
last_path + "/userProfile.json","*.json");
if (fileName.isEmpty())
return;
QFile loadFile(fileName);
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
return;
}
QFileInfo info(fileName);
QString lastPath = info.absolutePath();
set.setValue("userProfilePath",lastPath);
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
QJsonArray bzpoints = loadDoc["bzpoints"].toArray();
pts.clear();
for (int ndx = 0; ndx < bzpoints.size(); ++ndx){
QJsonObject jpt = bzpoints[ndx].toObject();
CPoint cpt(jpt);
pts << cpt;
}
update();
}
void ArbitraryWavWidget::saveJson(){
QSettings set;
QString last_path = set.value("userProfilePath", ".").toString();
QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Save PDF",
last_path + "/userProfile.json","*.json");
if (fileName.isEmpty())
return;
QJsonArray data;
foreach( CPoint pt, pts){
QJsonObject jpoint;
pt.toJson(jpoint);
data.append(jpoint);
}
QJsonObject jobj;
jobj["bzpoints"] = data;
QJsonDocument json = QJsonDocument(jobj);
QFile saveFile(fileName);
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return;
}
QFileInfo info(saveFile);
qDebug() << info.absoluteFilePath();
saveFile.write(json.toJson());
saveFile.close();
}
void ArbitraryWavWidget::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
dragging_point_index = findPoint(event->pos());
if (dragging_point_index >=0) {
bDragging=true;
return;
}
if (mode == 0) {
// we are in bezier mode. Search those control points now...
//dragging_bev_index = findBev(event->pos());
for(int i=0; i<pts.size(); i++) {
QPoint const &p1 = event->pos();
CPoint main_point = pts.at(i);
QPointF p2 = main_point.getLeft();
if ( (abs(p1.x() - transx(p2.x())) < 8) &&
(abs(p1.y() - transy(p2.y())) < 8) ) {
// found it
dragging_point_index = i;
bDragging_bez_left = true;
bDraggingBezierPoint=true;
return;
}
p2 = main_point.getRight();
if ( (abs(p1.x() - transx(p2.x())) < 8) &&
(abs(p1.y() - transy(p2.y())) < 8) ) {
// found it
dragging_point_index = i;
bDragging_bez_left = false; // it's the right point
bDraggingBezierPoint=true;
return;
}
}
}
// let's create a new point here
QPoint ip = event->pos();
CPoint newp(transx((int)ip.x()), transy((int)ip.y()), mirror_radius/bez_distance_ratio);
pts.append(newp);
sortPoints();
dragging_point_index = findPoint(event->pos());
// default handles to half way in X of nearest two points
if (dragging_point_index>0) {
// left handle of new point
CPoint &myPoint = pts[dragging_point_index];
CPoint &leftPoint = pts[dragging_point_index-1];
double desired_length = fabs(myPoint.x() - leftPoint.x()) / 2;
myPoint.lx = myPoint.x() - desired_length;
// right handle of prev point
leftPoint.setRight(leftPoint.x()+desired_length, leftPoint.ry,transRatio());
}
if (dragging_point_index == pts.size()-1) {
// added point is last point in list
CPoint &myPoint = pts[dragging_point_index];
myPoint.rx = myPoint.x() + 10; // last point - just make handle 10mm out. Always. Not used anyway.
} else {
// more points to the right
CPoint &myPoint = pts[dragging_point_index];
CPoint &nextPoint = pts[dragging_point_index+1];
double desired_length = fabs(myPoint.x() - nextPoint.x()) / 2;
myPoint.rx = myPoint.x() + desired_length;
nextPoint.setLeft(nextPoint.x() - desired_length, nextPoint.ly, transRatio());
}
if (dragging_point_index>=0)
bDragging=true; // hopefully it always gets here
update();
} else {
if (event->button() == Qt::RightButton) {
// delete point (if mouse over point)
int i = findPoint(event->pos());
if (i>0) // can't delete point 0 - it's permanent
pts.removeAt(i);
update();
}
}
}
void ArbitraryWavWidget::mouseMoveEvent(QMouseEvent *event) {
if (bDragging == false && bDraggingBezierPoint == false )
return;
if ( (event->buttons() & Qt::LeftButton) == 0)
return;
// update position of point
QPoint ip = event->pos();
pts[dragging_point_index].save();
if (bDragging) {
// user is moving primary point (not one of those side points)
pts[dragging_point_index].setX(transx(ip.x()));
pts[dragging_point_index].setY(transy(ip.y()));
if (dragging_point_index == 0)
pts[dragging_point_index].setX(0); // this point always at mirror center and can't be moved sideways
if (ip.x() > width)
pts[dragging_point_index].setX(transx(width)); // don't allow points to go outside graph area on right side
if (ip.x() < graph_left)
pts[dragging_point_index].setX(0); // don't allow points to go outside graph area on left side
if (testInflections(dragging_point_index) || testInflections(dragging_point_index-1)) {
// backwards slope - abort
pts[dragging_point_index].restore();
return;
}
sortPoints();// this messes up which point is being dragged so re-locate the point
int i = findPoint(event->pos());
if (i>=0) {
dragging_point_index=i;
}
}
if (bDraggingBezierPoint) {
// user is moving a side point - a control point
double x = transx(ip.x());
double y = transy(ip.y());
if (dragging_point_index == 0)
y = pts[0].y(); // first point (pts[0]) bez control point forced to be horizontal
if (bDragging_bez_left) {
pts[dragging_point_index-1].save();
pts[dragging_point_index].setLeft(x,y,transRatio());
if (testInflections(dragging_point_index-1) || testInflections(dragging_point_index)) {
// backwards slope - abort
pts[dragging_point_index].restore();
pts[dragging_point_index-1].restore();
return;
}
}
else {
// dragging right point
if (dragging_point_index>0)
pts[dragging_point_index-1].save();
pts[dragging_point_index].setRight(x,y,transRatio());
bool bAbort = testInflections(dragging_point_index);
if (dragging_point_index>0 && bAbort == false)
bAbort = testInflections(dragging_point_index-1);
if (bAbort) {
// backwards slope - abort
pts[dragging_point_index].restore();
if (dragging_point_index>0)
pts[dragging_point_index-1].restore();
return;
}
}
}
update(); // redraw
}
bool ArbitraryWavWidget::testInflections(int index) {
// test inflection slopes for bez to the right of this cpoint and return true if slope goes backwards
if (index < 0 || index >= (pts.size()-1)) {
// probably this is the last point. No bezier to the right of this point
return false; // no problems
}
QPointF p1 = pts[index];
QPointF p2 = pts[index].getRight();
QPointF p3 = pts[index+1].getLeft();
QPointF p4 = pts[index+1];
// sorin code starts here
double a0x, a1x, a2x, a0y, a1y, a2y;
double d0x, d1x, d0y, d1y;
double e0x, e0y;
a0x = p2.x() - p1.x();
a0y = p2.y() - p1.y();
a1x = p3.x() - p2.x();
a1y = p3.y() - p2.y();
a2x = p4.x() - p3.x();
a2y = p4.y() - p3.y();
d0x = a1x - a0x;
d0y = a1y - a0y;
d1x = a2x - a1x;
d1y = a2y - a1y;
e0x = d1x - d0x;
e0y = d1y - d0y;
double p = d0x * e0y - d0y * e0x;
double q = a0x * e0y - a0y * e0x;
double o = a0x * d0y - a0y * d0x;
double delta = q * q - 4 * p * o;
double t1, t2;
if(delta<0) {
//no inflection points
return true;
}
if (p==0) {
t1=-o/q;
t2=t1;
} else {
t1 = (-q + sqrt(delta)) / (2 * p);
t2 = (-q - sqrt(delta)) / (2 * p);
}
// sorin code ends here - now calculate tangent at t1,t2. If x is negative then at t increases, we are going backwards which is bad
Bezier::Point bez_pts[4];
bez_pts[0]= Bezier::Point(p1.x(), p1.y());
bez_pts[1]= Bezier::Point(p2.x(), p2.y());
bez_pts[2]= Bezier::Point(p3.x(), p3.y());
bez_pts[3]= Bezier::Point(p4.x(), p4.y());
bool bT1Within = (t1>=0 && t1 <= 1);
bool bT2Within = (t2>=0 && t2 <= 1);
Bezier::Bezier<3> bez(bez_pts,4);
if (bT1Within && bez.tangentAt(t1,false).x <= 0)
return true; // backwards slope
if (bT2Within && bez.tangentAt(t2,false).x <= 0)
return true; // backwards slope
return false; // no problems
}
void ArbitraryWavWidget::mouseReleaseEvent(QMouseEvent */* event */) {
bDragging = false;
bDraggingBezierPoint = false;
}
void ArbitraryWavWidget::wheelEvent(QWheelEvent *event) {
if (event->angleDelta().y() > 0) {
// scroll up. zoom in.
wave_height /= 1.1;
}
if (event->angleDelta().y() < 0) {
// scroll down. zoom out.
wave_height *= 1.1;
}
update(); // redraw
emit yScaleChanged(wave_height);
event->accept();
}
void ArbitraryWavWidget::setYscale(double value){
wave_height = value;
update();
}
void ArbitraryWavWidget::doCurve(QPainter &painter, int left_point_x, int left_point_y, int right_point_x, int right_point_y) {
// do a cubic double curve (an S curve) such that slope is zero at each end
// Formula is basically in the form of y=jerk*x^3 for each of the 2 curves
// I think of y=a*x^2 as a motion where a is acceleration. For cubics the term is called
// jerk (then snap, crackle, pop or sometimes snap is called jounce)
double half_dist = (right_point_x - left_point_x)/2; // horizontal distance to fill with each curve
double half_height = (right_point_y - left_point_y)/2; // vertical distance to fill with each curve
// y=jerk*(x-left_point_x)^3+left_point_y
double jerk = half_height/pow(half_dist,3);
int ix_prev=0, iy_prev=0;
bool bFirst;
// left half of curve
bFirst=true;
for (int ix = left_point_x; ix <= left_point_x+half_dist; ix++) {
double cubed = ix - left_point_x;
cubed = cubed*cubed*cubed;
int iy = jerk*cubed+left_point_y+0.5; // 0.5 is for rounding to nearest integer
if (bFirst) {
bFirst=false;
painter.drawPoint(ix, iy);
}
else
painter.drawLine(ix_prev,iy_prev,ix,iy);
ix_prev=ix;
iy_prev=iy;
}
// right half of curve
for (int ix = left_point_x+half_dist+1; ix <= right_point_x; ix++) {
double cubed = right_point_x - ix;
cubed = cubed*cubed*cubed;
int iy = right_point_y-jerk*cubed+0.5; // 0.5 is for rounding to nearest integer
if (bFirst) {
// seems impossible but possibly the earlier loop never had any points
bFirst=false;
painter.drawPoint(ix, iy);
}
else
painter.drawLine(ix_prev,iy_prev,ix,iy);
ix_prev=ix;
iy_prev=iy;
}
}
void ArbitraryWavWidget::paintEvent(QPaintEvent * /*event*/) {
QPainter painter(this);
QPen grayPen(Qt::gray, 1, Qt::SolidLine);
QPen blackPen(Qt::black, 1, Qt::SolidLine);
QPen bluePen(Qt::blue, 1.5, Qt::SolidLine);
QFont font = QFont("Arial",10);
painter.setFont(font);
//
// calculate some "constants" that we need to know: the size of the graph, the size of the margins and so on.
// Note that most of these are also used in transx() and transy() functions which are called on mouse
// operations as well as this function paintEvent(). That way we can think of and store most
// points as (radius, wave_height) and let the 4 transx(), transy() functions translate to and from pixel positions
//
QSize sz = size();
height = sz.height();
width = sz.width();
QFontMetrics fm(font);
const int y_axis_font_width = fm.horizontalAdvance("-1.125");
const int x_axis_font_width = fm.horizontalAdvance("9999 mm");
const int font_height = fm.height();
graph_left = y_axis_font_width+3; // space needed left of graph for vertical axis labels plus some extra pixels
graph_height = height
- (font_height+2); // space needed below graph for x axis labels
pos_y0 = graph_height/2;
pos_edge = width - (width-graph_left)*0.05; // .05 means 5% reserved to go beyond edge of mirror
// draw border
painter.drawLine(0,0,0,height-1);
painter.drawLine(0,height-1,width-1,height-1);
painter.drawLine(width-1,height-1,width-1,0);
painter.drawLine(width-1,0,0,0);
// graph grid
double y=0;
int iy;
// show horizontal lines. First determine spacing.
double vertical_spacing=0.125; // 1/8 wave - default
if (wave_height/.125 > 12)
vertical_spacing=1;
else if (wave_height/.125 > 6)
vertical_spacing=0.25;
while(y < wave_height) {
if (y==0)
painter.setPen(blackPen);
else
painter.setPen(grayPen);
//
// upper horizontal lines (+y) and lables
//
iy = transy(y);
painter.drawLine(graph_left, iy, pos_edge, iy);
// text
QRect rect1(2,iy-font_height/2, graph_left, font_height);
painter.drawText(rect1,QString{ "%1" }.arg(y, 0, 'f', 3));
//
// now do lower horizontal lines (-y) and labels (y=0 line drawn twice)
//
iy = transy(-y);
painter.drawLine(graph_left, iy, pos_edge, iy);
// text
QRect rect2(2,iy-font_height/2, graph_left, font_height);
painter.drawText(rect2,QString{ "%1" }.arg(-y, 0, 'f', 3));
y += vertical_spacing;
}
// determine frequency of vertical lines
double spacing=1; // always in mm
switch(ww_unit)
{
case in:
spacing=25.4;
break;
case cm:
case mm:
spacing=10;
break;
}
// now reduce the number of vertical lines - we want at least 150 pixels between lines
int pix_dist = transx(spacing);
int count=1;
while (pix_dist < 150) { // number here is minimum pixels per vertical line
count++;
pix_dist = transx(spacing*count);
}
spacing *= count;
// left edge of graph
int ix;
ix = pos_edge;
painter.drawLine(ix,0,ix,graph_height);
double x=0;
while(x <= mirror_radius) {
if (x==0)
painter.setPen(blackPen);
else
painter.setPen(grayPen);
ix = transx(x);
painter.drawLine(ix,0,ix,graph_height);
// text
QString s;
switch (ww_unit) {
case mm:
s = QString{ "%1 mm"}.arg(x, 0, 'f', 0);
break;
case cm:
s = QString{ "%1 cm"}.arg(x/10, 0, 'f', 0);
break;
case in:
default:
s = QString{ "%1 in"}.arg(x/25.4, 0, 'f', 1);
break;
}
QRect rect2(ix-x_axis_font_width/2, graph_height+1, x_axis_font_width, font_height);
painter.drawText(rect2, Qt::AlignCenter, s);
x+=spacing;
}
// draw the control points
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setBrush(QBrush(Qt::gray, Qt::SolidPattern));
painter.setPen(grayPen);
for(int i=0; i<pts.size(); i++) {
CPoint p = pts.at(i);
if (this->mode==0) {
// bezier mode - draw the control points
//left
QPointF pbez = p.getLeft();
if (i>0) {
// don't draw left of first point - that control point not used
painter.drawLine(transx(p.x()), transy(p.y()), transx(pbez.x()), transy(pbez.y()) );
painter.drawRect(transx(pbez.x())-3, transy(pbez.y())-2, 4, 4);
}
//right
pbez = p.getRight();
painter.drawLine(transx(p.x()), transy(p.y()), transx(pbez.x()), transy(pbez.y()) );
painter.drawRect(transx(pbez.x())-3, transy(pbez.y())-2, 4, 4);
}
// do the main point after the bez lines
painter.drawEllipse(transx(p.x())-5, transy(p.y())-4, 8, 8); // 10,10 is the width/height of the circle; -5 is because this function tells where corner goes
}
// draw the curves
painter.setPen(bluePen);
if (this->mode == 1) {
// cubic mode
QPointF leftPoint = pts.at(0);
int left_point_x = transx(leftPoint.x());
int left_point_y = transy(leftPoint.y());
int right_point_x=0, right_point_y=0;
for(int i=1; i<pts.size(); i++) {
QPointF p = pts.at(i);
right_point_x = transx(p.x());
right_point_y = transy(p.y());
doCurve(painter, left_point_x, left_point_y, right_point_x, right_point_y);
left_point_x=right_point_x;
left_point_y=right_point_y;
}
if (right_point_x < transx(mirror_radius)) {
doCurve(painter, left_point_x, left_point_y, width, left_point_y);
}
} else if (this->mode==0) {
// bezier mode
Bezier::Point bez_pts[4];
for(int i=1; i<pts.size(); i++) { // start at 1 and we will grab 2 points at a time
CPoint p1 = pts.at(i-1);
CPoint p4 = pts.at(i);
QPointF p2 = p1.getRight();
QPointF p3 = p4.getLeft();
bez_pts[0]= Bezier::Point(p1.x(), p1.y());
bez_pts[1]= Bezier::Point(p2.x(), p2.y());
bez_pts[2]= Bezier::Point(p3.x(), p3.y());
bez_pts[3]= Bezier::Point(p4.x(), p4.y());
Bezier::Bezier<3> bez(bez_pts,4);
Bezier::Point p_on_bez1, p_on_bez2;
p_on_bez1 = bez.valueAt(0);
const double t_increment = .01;
for (double t=t_increment; t<=1; t+=t_increment) {
p_on_bez2 = bez.valueAt(t);
if (p_on_bez1.x > mirror_radius)
painter.setPen(grayPen);
painter.drawLine(transx(p_on_bez1.x),transy(p_on_bez1.y),transx(p_on_bez2.x),transy(p_on_bez2.y));
p_on_bez1 = p_on_bez2;
}
}
}
if (bDrawCalculatedPoints) {
// for testing purposes - draw the prepare() data
QPen redPen(Qt::red, 1.5, Qt::SolidLine);
painter.setPen(redPen);
for (int i=0; i<wf_array_size; i++) {
int ix = transx(index_to_radius(i));
int iy = transy(wf_array[i]);
painter.drawPoint(ix,iy);
}
}
}
int ArbitraryWavWidget::radius_to_index(double r) {
if (mirror_radius==0)
return 0;
return (int)(r/mirror_radius*(wf_array_size-1)+0.5);
}
double ArbitraryWavWidget::rho_to_index(double r) {
// rho is radius but value goes from 0 to 1.00
// return value is a double on purpose! This is important
double index = (r*(wf_array_size-1)+0.5);
if (index < 0) return 0;
//if (index >= wf_array_size) return wf_array_size-1;
return index;
}
double ArbitraryWavWidget::index_to_radius(int i) { // used only for testing
if (wf_array_size==0)
return 0;
return mirror_radius*i/wf_array_size;
}
void ArbitraryWavWidget::prepare(int size) {
// create an array of doubles of size "size" and fill it with our wave heights for our drawn shape.
// The index into this array will be the radius.
if (size != wf_array_size || wf_array == 0) {
// create array
if (wf_array !=0)
delete wf_array;
wf_array_size = size;
wf_array = new double[size];
}
const double empty_val = -1000000;
for (int i=0; i< wf_array_size; ++i)
wf_array[i]= empty_val; // this is to keep track of which elements have been set to something
Bezier::Point bez_pts[4];
for(int i=1; i<pts.size(); i++) { // start at 1 and we will grab 2 points at a time
CPoint p1 = pts.at(i-1);
CPoint p4 = pts.at(i);
QPointF p2 = p1.getRight();
QPointF p3 = p4.getLeft();
bez_pts[0]= Bezier::Point(p1.x(), p1.y());
bez_pts[1]= Bezier::Point(p2.x(), p2.y());
bez_pts[2]= Bezier::Point(p3.x(), p3.y());
bez_pts[3]= Bezier::Point(p4.x(), p4.y());
Bezier::Bezier<3> bez(bez_pts,4);
const double t_increment = .005;
for (double t=0; t<=(1+t_increment/2); t+=t_increment) {
Bezier::Point p = bez.valueAt(t);
int index = radius_to_index(p.x);
if (index<0 || index >= wf_array_size)
continue;
wf_array[index] = p.y;
}
}
// fill in leading points with first filled value
int first_valid_entry;
for(first_valid_entry=0; first_valid_entry<wf_array_size; first_valid_entry++)
if (wf_array[first_valid_entry] != empty_val)
break;
for (int i=0; i < first_valid_entry; i++)
wf_array[i] = wf_array[first_valid_entry];
// now fill in trailing points with last filled value
int last_valid_entry;
for(last_valid_entry=wf_array_size-1; last_valid_entry>=0; last_valid_entry--)
if (wf_array[last_valid_entry] != empty_val)
break;
for (int i=last_valid_entry+1; i < wf_array_size; i++)
wf_array[i] = wf_array[last_valid_entry];
// now fill in gaps with interpolated values
for (int i=0; i<wf_array_size; i++) {
if (wf_array[i] != empty_val)
continue;
// found an empty starting at "i"
int j;
for (j=i; j<wf_array_size; j++)
if (wf_array[j] != empty_val)
break;
i--;
// okay i is first valid, j is valid. everything between is invalid.
for (int k=i+1; k<j; k++) {
double portion = 1.0*(k-i)/(j-i); // portion is value from 0 to 1.00 where 0 means
// use all of point at [i] and 1 means use all of
// point at [j] and .5 means use half of each
wf_array[k] = (1-portion)*wf_array[i] + portion*wf_array[j];
}// done with this section. Filled in.
i=j;
}
}
double ArbitraryWavWidget::getValue(double rho) { // rho is the radius of our mirror from 0.0 to 1.0 where 0 is the center and 1.0 is the edge
// using the array created in prepare() find the nearest entries in our array on either side of rho and return a linear interpolated wave height
double index = rho_to_index(rho); // returned value is a double - we need to get the values on either side of this one
int i1 = floor(index);
int i2 = i1 + 1;
if (i2 >= (wf_array_size-1)) {
i2 = wf_array_size-1;
i1 = i2 - 1;
}
double p1 = wf_array[i1];
double p2 = wf_array[i2];
double portion = index-i1;
return p1*(1-portion)+p2*portion; // this formula will work fine even if index isn't between i1 and i2. It will interpolate outwards.
}