source: branches/plotter2/src/Plotter2.cpp@ 2937

Last change on this file since 2937 was 2827, checked in by WataruKawasaki, 11 years ago

New Development: No

JIRA Issue: Yes CAS-3620

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): sd

Description: modified plotter2.save() so that the previous filename and device are restored finally.


File size: 28.5 KB
Line 
1#include "Plotter2.h"
2
3namespace asap {
4
5Plotter2RectInfo::Plotter2RectInfo() {
6 xmin = 0.0;
7 xmax = 1.0;
8 ymin = 0.0;
9 ymax = 1.0;
10 color = 15; // gray
11 fill = 4; // hatch
12 width = 1;
13}
14
15Plotter2RectInfo::~Plotter2RectInfo() {
16}
17
18Plotter2DataInfo::Plotter2DataInfo() {
19 xData.clear();
20 yData.clear();
21
22 drawLine = true;
23 lineStyle = 1; // solid line
24 lineWidth = 1;
25 lineColor = -1; // undefined (default color should be assigned)
26
27 drawMarker = false;
28 markerType = 20; // small circle
29 markerSize = 1.0;
30 markerColor = 1; // default foreground color (black)
31
32 hasData = false; // has no data
33}
34
35Plotter2DataInfo::~Plotter2DataInfo() {
36}
37
38Plotter2ViewportInfo::Plotter2ViewportInfo() {
39 showViewport = true;
40
41 vpPosXMin = 0.1;
42 vpPosXMax = 0.9;
43 vpPosYMin = 0.1;
44 vpPosYMax = 0.9;
45
46 vpRangeXMin = 0.0;
47 vpRangeXMax = 1.0;
48 vpRangeYMin = 0.0;
49 vpRangeYMax = 1.0;
50 isAutoRangeX = true;
51 isAutoRangeY = true;
52 autoRangeMarginX = 0.0;
53 autoRangeMarginY = 0.1;
54
55 hasDataRange = false;
56
57 nMajorTickWithinTickNumsX = 2;
58 nMajorTickWithinTickNumsY = 2;
59 isAutoTickIntervalX = true;
60 isAutoTickIntervalY = true;
61
62 numLocationX = "l";
63 numLocationY = "b";
64
65 fontSizeDef = 1.0;
66
67 vpBColor = -1;
68
69 vData.clear();
70}
71
72Plotter2ViewportInfo::~Plotter2ViewportInfo() {
73 vData.clear();
74}
75
76void Plotter2ViewportInfo::adjustRange() {
77 if (hasDataRange) {
78 if (isAutoRangeX) {
79 adjustRangeX(&vpRangeXMin, &vpRangeXMax);
80 }
81 if (isAutoRangeY) {
82 adjustRangeY(&vpRangeYMin, &vpRangeYMax);
83 }
84 }
85}
86
87void Plotter2ViewportInfo::adjustRangeX(float* xmin, float* xmax) {
88 float xmargin = (maxXData - minXData) * autoRangeMarginX;
89 *xmin = minXData - xmargin;
90 *xmax = maxXData + xmargin;
91}
92
93void Plotter2ViewportInfo::adjustRangeY(float* ymin, float* ymax) {
94 float ymargin = (maxYData - minYData) * autoRangeMarginY;
95 *ymin = minYData - ymargin;
96 *ymax = maxYData + ymargin;
97}
98
99std::vector<float> Plotter2ViewportInfo::getRangeX() {
100 float minX, maxX;
101
102 if (isAutoRangeX) {
103 adjustRangeX(&minX, &maxX);
104 } else {
105 minX = vpRangeXMin;
106 maxX = vpRangeXMax;
107 }
108
109 std::vector<float> res;
110 res.clear();
111 res.push_back(minX);
112 res.push_back(maxX);
113
114 return res;
115}
116
117std::vector<float> Plotter2ViewportInfo::getRangeY() {
118 float minY, maxY;
119
120 if (isAutoRangeY) {
121 adjustRangeY(&minY, &maxY);
122 } else {
123 minY = vpRangeYMin;
124 maxY = vpRangeYMax;
125 }
126
127 std::vector<float> res;
128 res.clear();
129 res.push_back(minY);
130 res.push_back(maxY);
131
132 return res;
133}
134
135void Plotter2ViewportInfo::adjustTickInterval() {
136 if (hasDataRange) {
137 if (isAutoTickIntervalX) {
138 adjustTickIntervalX();
139 }
140 if (isAutoTickIntervalY) {
141 adjustTickIntervalY();
142 }
143 }
144}
145
146void Plotter2ViewportInfo::adjustTickIntervalX() {
147 adjustTickIntervalX(vpRangeXMin, vpRangeXMax);
148}
149
150void Plotter2ViewportInfo::adjustTickIntervalX(const float xmin, const float xmax) {
151 majorTickIntervalX = (float)pow(10.0, ceil(log10((xmax - xmin)/10.0)));
152 if ((xmax - xmin) / majorTickIntervalX < 2.0) {
153 majorTickIntervalX /= 10.0;
154 }
155 nMinorTickWithinMajorTicksX = 5;
156}
157
158void Plotter2ViewportInfo::adjustTickIntervalY() {
159 adjustTickIntervalY(vpRangeYMin, vpRangeYMax);
160}
161
162void Plotter2ViewportInfo::adjustTickIntervalY(const float ymin, const float ymax) {
163 majorTickIntervalY = (float)pow(10.0, ceil(log10((ymax - ymin)/10.0)));
164 if ((ymax - ymin) / majorTickIntervalY < 2.0) {
165 majorTickIntervalY /= 10.0;
166 }
167 nMinorTickWithinMajorTicksY = 5;
168}
169
170void Plotter2ViewportInfo::setData(const std::vector<float>& inXData, const std::vector<float>& inYData, const int id) {
171 if (!hasDataRange) {
172 minXData = inXData[0];
173 maxXData = inXData[0];
174 minYData = inYData[0];
175 maxYData = inYData[0];
176
177 hasDataRange = true;
178 }
179
180 Plotter2DataInfo* info = &vData[id];
181
182 info->xData.clear();
183 info->xData.reserve(inXData.size());
184 for (uint i = 0; i < inXData.size(); ++i) {
185 info->xData.push_back(inXData[i]);
186
187 if (!info->hasData) {
188 updateXDataRange(inXData[i]);
189 }
190 }
191
192 info->yData.clear();
193 info->yData.reserve(inYData.size());
194 for (uint i = 0; i < inYData.size(); ++i) {
195 info->yData.push_back(inYData[i]);
196
197 if (!info->hasData) {
198 updateYDataRange(inYData[i]);
199 }
200 }
201
202 if (info->hasData) {
203 updateAllDataRanges();
204 } else {
205 info->hasData = true;
206 }
207}
208
209void Plotter2ViewportInfo::updateXDataRange(const float data) {
210 if (data < minXData) {
211 minXData = data;
212 }
213 if (maxXData < data) {
214 maxXData = data;
215 }
216}
217
218void Plotter2ViewportInfo::updateYDataRange(const float data) {
219 if (data < minYData) {
220 minYData = data;
221 }
222 if (maxYData < data) {
223 maxYData = data;
224 }
225}
226
227void Plotter2ViewportInfo::updateAllDataRanges() {
228 minXData = vData[0].xData[0];
229 maxXData = minXData;
230 minYData = vData[0].yData[0];
231 maxYData = minYData;
232
233 for (uint i = 0; i < vData.size(); ++i) {
234 for (uint j = 0; j < vData[i].xData.size(); ++j) {
235 updateXDataRange(vData[i].xData[j]);
236 updateYDataRange(vData[i].yData[j]);
237 }
238 }
239}
240
241void Plotter2ViewportInfo::getWorldCoordByWindowCoord(const float winX, const float winY, float* worldX, float* worldY) {
242 float xratio = (winX - vpPosXMin) / (vpPosXMax - vpPosXMin);
243 if (winX < 0.0) {
244 xratio = 0.5;
245 }
246 *worldX = vpRangeXMin + xratio * (vpRangeXMax - vpRangeXMin);
247 float yratio = (winY - vpPosYMin) / (vpPosYMax - vpPosYMin);
248 if (winY < 0.0) {
249 yratio = 0.5;
250 }
251 *worldY = vpRangeYMin + yratio * (vpRangeYMax - vpRangeYMin);
252}
253
254Plotter2::Plotter2() {
255 filename = "";
256 device = "xwindow";
257 hasDevice = false;
258
259 vInfo.clear();
260 Plotter2ViewportInfo vi;
261 vInfo.push_back(vi);
262
263 hasDefaultViewport = true;
264 currentViewportId = 0;
265}
266
267Plotter2::~Plotter2() {
268 close();
269 vInfo.clear();
270}
271
272std::string Plotter2::getFileName() {
273 return filename;
274}
275
276void Plotter2::setFileName(const std::string& inFilename) {
277 filename = inFilename;
278}
279
280std::string Plotter2::getDevice() {
281 return device;
282}
283
284void Plotter2::setDevice(const std::string& inDevice) {
285 device = inDevice;
286}
287
288void Plotter2::open() {
289 cpgopen((filename + "/" + device).c_str());
290 hasDevice = true;
291}
292
293int Plotter2::addViewport(const float xmin, const float xmax, const float ymin, const float ymax) {
294 Plotter2ViewportInfo vi;
295
296 vi.vpPosXMin = xmin;
297 vi.vpPosXMax = xmax;
298 vi.vpPosYMin = ymin;
299 vi.vpPosYMax = ymax;
300
301 vInfo.push_back(vi);
302 currentViewportId = vInfo.size() - 1;
303
304 return currentViewportId;
305}
306
307void Plotter2::setViewport(const float xmin, const float xmax, const float ymin, const float ymax, const int id) {
308 Plotter2ViewportInfo* vi = &vInfo[id];
309
310 vi->vpPosXMin = xmin;
311 vi->vpPosXMax = xmax;
312 vi->vpPosYMin = ymin;
313 vi->vpPosYMax = ymax;
314
315 hasDefaultViewport = false;
316}
317
318void Plotter2::showViewport(const int inVpid) {
319 int vpid = inVpid;
320 if (vpid < 0) {
321 vpid = vInfo.size() - 1;
322 }
323 if (vpid < 0) {
324 exit(0);
325 }
326
327 Plotter2ViewportInfo* vi = &vInfo[vpid];
328 vi->showViewport = true;
329}
330
331void Plotter2::hideViewport(const int inVpid) {
332 int vpid = inVpid;
333 if (vpid < 0) {
334 vpid = vInfo.size() - 1;
335 }
336 if (vpid < 0) {
337 exit(0);
338 }
339
340 Plotter2ViewportInfo* vi = &vInfo[vpid];
341 vi->showViewport = false;
342}
343
344bool Plotter2::getHasDefaultViewport() {
345 return hasDefaultViewport;
346}
347
348int Plotter2::getCurrentViewportId() {
349 return currentViewportId;
350}
351
352void Plotter2::getViewInfo() {
353 std::cout << "===================" << std::endl << std::flush;
354 for (uint i = 0; i < vInfo.size(); ++i) {
355 std::cout << "[" << i << "] " << std::endl;
356 std::cout << "vpPos: ";
357 std::cout << "xmin=" << vInfo[i].vpPosXMin << ", ";
358 std::cout << "xmax=" << vInfo[i].vpPosXMax << ", ";
359 std::cout << "ymin=" << vInfo[i].vpPosYMin << ", ";
360 std::cout << "ymax=" << vInfo[i].vpPosYMax << std::endl;
361
362 std::cout << "vpRange: ";
363 std::cout << "xmin=" << vInfo[i].vpRangeXMin << ", ";
364 std::cout << "xmax=" << vInfo[i].vpRangeXMax << ", ";
365 std::cout << "ymin=" << vInfo[i].vpRangeYMin << ", ";
366 std::cout << "ymax=" << vInfo[i].vpRangeYMax << std::endl;;
367
368 std::cout << "vdatasize=" << vInfo[i].vData.size() << std::endl;
369 for (uint j = 0; j < vInfo[i].vData.size(); ++j) {
370 std::cout << "vdataxdatasize=" << vInfo[i].vData[j].xData.size() << ", ";
371 for (uint k = 0; k < vInfo[i].vData[j].xData.size(); ++k) {
372 std::cout << "(" << vInfo[i].vData[j].xData[k] << ", ";
373 std::cout << vInfo[i].vData[j].yData[k] << ") ";
374 }
375 std::cout << std::endl;
376 }
377 std::cout << "===================" << std::endl << std::flush;
378 }
379}
380
381void Plotter2::setRange(const float xmin, const float xmax, const float ymin, const float ymax, const int inVpid) {
382 setRangeX(xmin, xmax, inVpid);
383 setRangeY(ymin, ymax, inVpid);
384}
385
386void Plotter2::setRangeX(const float xmin, const float xmax, const int inVpid) {
387 int vpid = inVpid;
388 if (vpid < 0) {
389 vpid = vInfo.size() - 1;
390 }
391 if (vpid < 0) {
392 Plotter2ViewportInfo vi;
393 vInfo.push_back(vi);
394 vpid = 0;
395 }
396
397 Plotter2ViewportInfo* vi = &vInfo[vpid];
398 vi->vpRangeXMin = xmin;
399 vi->vpRangeXMax = xmax;
400 vi->isAutoRangeX = false;
401}
402
403void Plotter2::setRangeY(const float ymin, const float ymax, const int inVpid) {
404 int vpid = inVpid;
405 if (vpid < 0) {
406 vpid = vInfo.size() - 1;
407 }
408 if (vpid < 0) {
409 Plotter2ViewportInfo vi;
410 vInfo.push_back(vi);
411 vpid = 0;
412 }
413
414 Plotter2ViewportInfo* vi = &vInfo[vpid];
415 vi->vpRangeYMin = ymin;
416 vi->vpRangeYMax = ymax;
417 vi->isAutoRangeY = false;
418}
419
420std::vector<float> Plotter2::getRangeX(const int inVpid) {
421 int vpid = inVpid;
422 if (vpid < 0) {
423 vpid = vInfo.size() - 1;
424 }
425 if (vpid < 0) {
426 exit(0);
427 }
428
429 return vInfo[vpid].getRangeX();
430}
431
432std::vector<float> Plotter2::getRangeY(const int inVpid) {
433 int vpid = inVpid;
434 if (vpid < 0) {
435 vpid = vInfo.size() - 1;
436 }
437 if (vpid < 0) {
438 exit(0);
439 }
440
441 return vInfo[vpid].getRangeY();
442}
443
444void Plotter2::setAutoRange(const int inVpid) {
445 setAutoRangeX(inVpid);
446 setAutoRangeY(inVpid);
447}
448
449void Plotter2::setAutoRangeX(const int inVpid) {
450 int vpid = inVpid;
451 if (vpid < 0) {
452 vpid = vInfo.size() - 1;
453 }
454 if (vpid < 0) {
455 Plotter2ViewportInfo vi;
456 vInfo.push_back(vi);
457 vpid = 0;
458 }
459
460 Plotter2ViewportInfo* vi = &vInfo[vpid];
461 vi->isAutoRangeX = true;
462}
463
464void Plotter2::setAutoRangeY(const int inVpid) {
465 int vpid = inVpid;
466 if (vpid < 0) {
467 vpid = vInfo.size() - 1;
468 }
469 if (vpid < 0) {
470 Plotter2ViewportInfo vi;
471 vInfo.push_back(vi);
472 vpid = 0;
473 }
474
475 Plotter2ViewportInfo* vi = &vInfo[vpid];
476 vi->isAutoRangeY = true;
477}
478
479void Plotter2::setFontSizeDef(const float size, const int inVpid) {
480 int vpid = inVpid;
481 if (vpid < 0) {
482 vpid = vInfo.size() - 1;
483 }
484 if (vpid < 0) {
485 Plotter2ViewportInfo vi;
486 vInfo.push_back(vi);
487 vpid = 0;
488 }
489
490 Plotter2ViewportInfo* vi = &vInfo[vpid];
491 vi->fontSizeDef = size;
492}
493
494void Plotter2::setTicksX(const float interval, const int num, const int inVpid) {
495 int vpid = inVpid;
496 if (vpid < 0) {
497 vpid = vInfo.size() - 1;
498 }
499 if (vpid < 0) {
500 Plotter2ViewportInfo vi;
501 vInfo.push_back(vi);
502 vpid = 0;
503 }
504
505 Plotter2ViewportInfo* vi = &vInfo[vpid];
506 vi->majorTickIntervalX = interval;
507 vi->nMinorTickWithinMajorTicksX = num;
508 vi->isAutoTickIntervalX = false;
509}
510
511void Plotter2::setTicksY(const float interval, const int num, const int inVpid) {
512 int vpid = inVpid;
513 if (vpid < 0) {
514 vpid = vInfo.size() - 1;
515 }
516 if (vpid < 0) {
517 Plotter2ViewportInfo vi;
518 vInfo.push_back(vi);
519 vpid = 0;
520 }
521
522 Plotter2ViewportInfo* vi = &vInfo[vpid];
523 vi->majorTickIntervalY = interval;
524 vi->nMinorTickWithinMajorTicksY = num;
525 vi->isAutoTickIntervalY = false;
526}
527
528void Plotter2::setAutoTicks(const int inVpid) {
529 setAutoTicksX(inVpid);
530 setAutoTicksY(inVpid);
531}
532
533void Plotter2::setAutoTicksX(const int inVpid) {
534 int vpid = inVpid;
535 if (vpid < 0) {
536 vpid = vInfo.size() - 1;
537 }
538 if (vpid < 0) {
539 Plotter2ViewportInfo vi;
540 vInfo.push_back(vi);
541 vpid = 0;
542 }
543
544 Plotter2ViewportInfo* vi = &vInfo[vpid];
545 vi->isAutoTickIntervalX = true;
546}
547
548void Plotter2::setAutoTicksY(const int inVpid) {
549 int vpid = inVpid;
550 if (vpid < 0) {
551 vpid = vInfo.size() - 1;
552 }
553 if (vpid < 0) {
554 Plotter2ViewportInfo vi;
555 vInfo.push_back(vi);
556 vpid = 0;
557 }
558
559 Plotter2ViewportInfo* vi = &vInfo[vpid];
560 vi->isAutoTickIntervalY = true;
561}
562
563void Plotter2::setNumIntervalX(const float interval, const int inVpid) {
564 int vpid = inVpid;
565 if (vpid < 0) {
566 vpid = vInfo.size() - 1;
567 }
568 if (vpid < 0) {
569 Plotter2ViewportInfo vi;
570 vInfo.push_back(vi);
571 vpid = 0;
572 }
573
574 Plotter2ViewportInfo* vi = &vInfo[vpid];
575 vi->nMajorTickWithinTickNumsX = (int)(interval / vi->majorTickIntervalX);
576}
577
578void Plotter2::setNumIntervalY(const float interval, const int inVpid) {
579 int vpid = inVpid;
580 if (vpid < 0) {
581 vpid = vInfo.size() - 1;
582 }
583 if (vpid < 0) {
584 Plotter2ViewportInfo vi;
585 vInfo.push_back(vi);
586 vpid = 0;
587 }
588
589 Plotter2ViewportInfo* vi = &vInfo[vpid];
590 vi->nMajorTickWithinTickNumsY = (int)(interval / vi->majorTickIntervalY);
591}
592
593void Plotter2::setNumLocationX(const std::string& side, const int inVpid) {
594 int vpid = inVpid;
595 if (vpid < 0) {
596 vpid = vInfo.size() - 1;
597 }
598 if (vpid < 0) {
599 Plotter2ViewportInfo vi;
600 vInfo.push_back(vi);
601 vpid = 0;
602 }
603
604 Plotter2ViewportInfo* vi = &vInfo[vpid];
605 vi->numLocationX = side;
606}
607
608void Plotter2::setNumLocationY(const std::string& side, const int inVpid) {
609 int vpid = inVpid;
610 if (vpid < 0) {
611 vpid = vInfo.size() - 1;
612 }
613 if (vpid < 0) {
614 Plotter2ViewportInfo vi;
615 vInfo.push_back(vi);
616 vpid = 0;
617 }
618
619 Plotter2ViewportInfo* vi = &vInfo[vpid];
620 vi->numLocationY = side;
621}
622
623void Plotter2::setData(const std::vector<float>& xdata, const std::vector<float>& ydata, const int inVpid, const int inDataid) {
624 int vpid = inVpid;
625 if (vpid < 0) {
626 vpid = vInfo.size() - 1;
627 }
628 if (vpid < 0) {
629 Plotter2ViewportInfo vi;
630 vInfo.push_back(vi);
631 vpid = 0;
632 }
633
634 Plotter2ViewportInfo* vi = &vInfo[vpid];
635
636 int dataid = inDataid;
637 if (dataid < 0) {
638 Plotter2DataInfo di;
639 vi->vData.push_back(di);
640 dataid = vi->vData.size() - 1;
641 }
642
643 vi->setData(xdata, ydata, dataid);
644}
645
646void Plotter2::setLine(const int color, const int width, const int style, const int inVpid, const int inDataid) {
647 int vpid = inVpid;
648 if (vpid < 0) {
649 vpid = vInfo.size() - 1;
650 }
651 if (vpid < 0) {
652 Plotter2ViewportInfo vi;
653 vInfo.push_back(vi);
654 vpid = 0;
655 }
656
657 int dataid = inDataid;
658 if (dataid < 0) {
659 dataid = vInfo[vpid].vData.size() - 1;
660 }
661
662 Plotter2ViewportInfo* vi = &vInfo[vpid];
663 vi->vData[dataid].drawLine = true;
664 vi->vData[dataid].lineColor = color;
665 vi->vData[dataid].lineWidth = width;
666 vi->vData[dataid].lineStyle = style;
667}
668
669void Plotter2::showLine(const int inVpid, const int inDataid) {
670 int vpid = inVpid;
671 if (vpid < 0) {
672 vpid = vInfo.size() - 1;
673 }
674 if (vpid < 0) {
675 exit(0);
676 }
677
678 int dataid = inDataid;
679 if (dataid < 0) {
680 dataid = vInfo[vpid].vData.size() - 1;
681 }
682 if (dataid < 0) {
683 exit(0);
684 }
685
686 Plotter2ViewportInfo* vi = &vInfo[vpid];
687 vi->vData[dataid].drawLine = true;
688}
689
690void Plotter2::hideLine(const int inVpid, const int inDataid) {
691 int vpid = inVpid;
692 if (vpid < 0) {
693 vpid = vInfo.size() - 1;
694 }
695 if (vpid < 0) {
696 exit(0);
697 }
698
699 int dataid = inDataid;
700 if (dataid < 0) {
701 dataid = vInfo[vpid].vData.size() - 1;
702 }
703 if (dataid < 0) {
704 exit(0);
705 }
706
707 Plotter2ViewportInfo* vi = &vInfo[vpid];
708 vi->vData[dataid].drawLine = false;
709}
710
711void Plotter2::setPoint(const int type, const float size, const int color, const int inVpid, const int inDataid) {
712 int vpid = inVpid;
713 if (vpid < 0) {
714 vpid = vInfo.size() - 1;
715 }
716 if (vpid < 0) {
717 Plotter2ViewportInfo vi;
718 vInfo.push_back(vi);
719 vpid = 0;
720 }
721
722 int dataid = inDataid;
723 if (dataid < 0) {
724 dataid = vInfo[vpid].vData.size() - 1;
725 }
726
727 Plotter2ViewportInfo* vi = &vInfo[vpid];
728 vi->vData[dataid].drawMarker = true;
729 vi->vData[dataid].markerType = type;
730 vi->vData[dataid].markerSize = size;
731 vi->vData[dataid].markerColor = color;
732}
733
734void Plotter2::showPoint(const int inVpid, const int inDataid) {
735 int vpid = inVpid;
736 if (vpid < 0) {
737 vpid = vInfo.size() - 1;
738 }
739 if (vpid < 0) {
740 exit(0);
741 }
742
743 int dataid = inDataid;
744 if (dataid < 0) {
745 dataid = vInfo[vpid].vData.size() - 1;
746 }
747 if (dataid < 0) {
748 exit(0);
749 }
750
751 Plotter2ViewportInfo* vi = &vInfo[vpid];
752 vi->vData[dataid].drawMarker = true;
753}
754
755void Plotter2::hidePoint(const int inVpid, const int inDataid) {
756 int vpid = inVpid;
757 if (vpid < 0) {
758 vpid = vInfo.size() - 1;
759 }
760 if (vpid < 0) {
761 exit(0);
762 }
763
764 int dataid = inDataid;
765 if (dataid < 0) {
766 dataid = vInfo[vpid].vData.size() - 1;
767 }
768 if (dataid < 0) {
769 exit(0);
770 }
771
772 Plotter2ViewportInfo* vi = &vInfo[vpid];
773 vi->vData[dataid].drawMarker = false;
774}
775
776void Plotter2::setMaskX(const float xmin, const float xmax, const int color, const int fill, const int width, const float hsep, const int inVpid) {
777 int vpid = inVpid;
778 if (vpid < 0) {
779 vpid = vInfo.size() - 1;
780 }
781 if (vpid < 0) {
782 Plotter2ViewportInfo vi;
783 vInfo.push_back(vi);
784 vpid = 0;
785 }
786
787 Plotter2ViewportInfo* vi = &vInfo[vpid];
788
789 Plotter2RectInfo ri;
790 ri.xmin = xmin;
791 ri.xmax = xmax;
792 std::vector<float> yrange = vi->getRangeY();
793 float yexcess = 0.1*(yrange[1] - yrange[0]);
794 ri.ymin = yrange[0] - yexcess;
795 ri.ymax = yrange[1] + yexcess;
796 ri.color = color;
797 ri.fill = fill;
798 ri.width = width;
799 ri.hsep = hsep;
800
801 vi->vRect.push_back(ri);
802}
803
804void Plotter2::setLabelX(const std::string& label, const float inPosx, const float inPosy, const float size, const std::string& style, const int color, const int bgcolor, const int inVpid) {
805 int vpid = inVpid;
806 if (vpid < 0) {
807 vpid = vInfo.size() - 1;
808 }
809 if (vpid < 0) {
810 Plotter2ViewportInfo vi;
811 vInfo.push_back(vi);
812 vpid = 0;
813 }
814
815 Plotter2ViewportInfo* vi = &vInfo[vpid];
816
817 std::string styleString;
818 if (style == "") {
819 styleString = "";
820 } else if (style == "roman") {
821 styleString = "\\fr";
822 } else if (style == "italic") {
823 styleString = "\\fi";
824 } else if (style == "script") {
825 styleString = "\\fs";
826 }
827 vi->labelXString = styleString + label;
828
829 float posx = inPosx;
830 if (posx < 0.0) {
831 posx = 0.5*(vi->vpPosXMin + vi->vpPosXMax);
832 }
833 vi->labelXPosX = posx;
834
835 float posy = inPosy;
836 if (posy < 0.0) {
837 posy = 0.35*vi->vpPosYMin;
838 }
839 vi->labelXPosY = posy;
840
841 vi->labelXAngle = 0.0;
842 vi->labelXFJust = 0.5;
843 vi->labelXSize = size;
844 vi->labelXColor = color;
845 vi->labelXBColor = bgcolor;
846}
847
848void Plotter2::setLabelY(const std::string& label, const float inPosx, const float inPosy, const float size, const std::string& style, const int color, const int bgcolor, const int inVpid) {
849 int vpid = inVpid;
850 if (vpid < 0) {
851 vpid = vInfo.size() - 1;
852 }
853 if (vpid < 0) {
854 Plotter2ViewportInfo vi;
855 vInfo.push_back(vi);
856 vpid = 0;
857 }
858
859 Plotter2ViewportInfo* vi = &vInfo[vpid];
860
861 std::string styleString;
862 if (style == "") {
863 styleString = "";
864 } else if (style == "roman") {
865 styleString = "\\fr";
866 } else if (style == "italic") {
867 styleString = "\\fi";
868 } else if (style == "script") {
869 styleString = "\\fs";
870 }
871 vi->labelYString = styleString + label;
872
873 float posx = inPosx;
874 if (posx < 0.0) {
875 posx = 0.35*vi->vpPosXMin;
876 }
877 vi->labelYPosX = posx;
878
879 float posy = inPosy;
880 if (posy < 0.0) {
881 posy = 0.5*(vi->vpPosYMin + vi->vpPosYMax);
882 }
883 vi->labelYPosY = posy;
884
885 vi->labelYAngle = 90.0;
886 vi->labelYFJust = 0.5;
887 vi->labelYSize = size;
888 vi->labelYColor = color;
889 vi->labelYBColor = bgcolor;
890}
891
892void Plotter2::setTitle(const std::string& label, const float inPosx, const float inPosy, const float size, const std::string& style, const int color, const int bgcolor, const int inVpid) {
893 int vpid = inVpid;
894 if (vpid < 0) {
895 vpid = vInfo.size() - 1;
896 }
897 if (vpid < 0) {
898 Plotter2ViewportInfo vi;
899 vInfo.push_back(vi);
900 vpid = 0;
901 }
902
903 Plotter2ViewportInfo* vi = &vInfo[vpid];
904
905 std::string styleString;
906 if (style == "") {
907 styleString = "";
908 } else if (style == "roman") {
909 styleString = "\\fr";
910 } else if (style == "italic") {
911 styleString = "\\fi";
912 } else if (style == "script") {
913 styleString = "\\fs";
914 }
915 vi->titleString = styleString + label;
916
917 float posx = inPosx;
918 if (posx < 0.0) {
919 posx = 0.5*(vi->vpPosXMin + vi->vpPosXMax);
920 }
921 vi->titlePosX = posx;
922
923 float posy = inPosy;
924 if (posy < 0.0) {
925 posy = vi->vpPosYMax + 0.25*(1.0 - vi->vpPosYMax);
926 }
927 vi->titlePosY = posy;
928
929 vi->titleAngle = 0.0;
930 vi->titleFJust = 0.5;
931 vi->titleSize = size;
932 vi->titleColor = color;
933 vi->titleBColor = bgcolor;
934}
935
936void Plotter2::setViewportBackgroundColor(const int bgcolor, const int inVpid) {
937 int vpid = inVpid;
938 if (vpid < 0) {
939 vpid = vInfo.size() - 1;
940 }
941 if (vpid < 0) {
942 exit(0);
943 }
944
945 Plotter2ViewportInfo* vi = &vInfo[vpid];
946 vi->vpBColor = bgcolor;
947}
948
949 /*
950void Plotter2::setAnnotation(const std::string& label, const float posx, const float posy, const float angle, const float fjust, const float size, const std::string& style, const int color, const int bgcolor, const int inVpid) {
951 int vpid = inVpid;
952 if (vpid < 0) {
953 vpid = vInfo.size() - 1;
954 }
955 if (vpid < 0) {
956 Plotter2ViewportInfo vi;
957 vInfo.push_back(vi);
958 vpid = 0;
959 }
960
961 std::string styleString;
962 if (style == "") {
963 styleString = "";
964 } else if (style == "roman") {
965 styleString = "\\fr";
966 } else if (style == "italic") {
967 styleString = "\\fi";
968 } else if (style == "script") {
969 styleString = "\\fs";
970 }
971
972 Plotter2ViewportInfo* vi = &vInfo[vpid];
973 //vi->titleString = styleString + label;
974 //vi->titlePosX = posx;
975 //vi->titlePosY = posy;
976 //vi->titleAngle = angle;
977 //vi->titleFJust = fjust;
978 //vi->titleSize = size;
979 //vi->titleColor = color;
980 //vi->titleBColor = bgcolor;
981}
982 */
983
984void Plotter2::close() {
985 if (hasDevice) {
986 cpgclos();
987 hasDevice = false;
988 }
989}
990
991void Plotter2::plot() {
992 open();
993
994 cpgscr(0, 1.0, 1.0, 1.0); // set background color white
995 cpgscr(1, 0.0, 0.0, 0.0); // set foreground color black
996
997 for (uint i = 0; i < vInfo.size(); ++i) {
998 Plotter2ViewportInfo vi = vInfo[i];
999
1000 if (vi.showViewport) {
1001 cpgstbg(0); // reset background colour to the initial one (white)
1002 cpgsci(1); // reset foreground colour to the initial one (black)
1003 cpgsls(1); // reset line style to solid
1004 cpgslw(1); // reset line width to 1
1005 cpgscf(1); // reset font style to normal
1006 cpgsch(vi.fontSizeDef);// reset font size
1007 cpgsfs(1); // reset fill style (solid)
1008
1009 cpgsvp(vi.vpPosXMin, vi.vpPosXMax, vi.vpPosYMin, vi.vpPosYMax);
1010 vi.adjustRange();
1011 vi.adjustTickInterval();
1012
1013 cpgswin(vi.vpRangeXMin, vi.vpRangeXMax, vi.vpRangeYMin, vi.vpRangeYMax);
1014
1015 if (vi.vpBColor >= 0) {
1016 cpgsci(vi.vpBColor);
1017 cpgrect(vi.vpRangeXMin, vi.vpRangeXMax, vi.vpRangeYMin, vi.vpRangeYMax);
1018 cpgsci(1); // reset foreground colour to the initial one (black)
1019 }
1020
1021 // data
1022 for (uint j = 0; j < vi.vData.size(); ++j) {
1023 cpgstbg(0); // reset background colour to the initial one (white)
1024 cpgsci(1); // reset foreground colour to the initial one (black)
1025 cpgsls(1); // reset line style to solid
1026 cpgslw(1); // reset line width to 1
1027 cpgscf(1); // reset font style to normal
1028 cpgsch(vi.fontSizeDef);// reset font size
1029
1030 Plotter2DataInfo di = vi.vData[j];
1031 std::vector<float> vxdata = di.xData;
1032 int ndata = vxdata.size();
1033 float* pxdata = new float[ndata];
1034 float* pydata = new float[ndata];
1035 for (int k = 0; k < ndata; ++k) {
1036 pxdata[k] = di.xData[k];
1037 pydata[k] = di.yData[k];
1038 }
1039
1040 if (di.drawLine) {
1041 cpgsls(di.lineStyle);
1042 cpgslw(di.lineWidth);
1043 int colorIdx = di.lineColor;
1044 if (colorIdx < 0) {
1045 colorIdx = (j + 1) % 15 + 1;
1046 }
1047 cpgsci(colorIdx);
1048 cpgline(ndata, pxdata, pydata);
1049 }
1050
1051 if (di.drawMarker) {
1052 cpgsch(di.markerSize);
1053 cpgsci(di.markerColor);
1054 cpgpt(ndata, pxdata, pydata, di.markerType);
1055 }
1056
1057 delete [] pxdata;
1058 delete [] pydata;
1059 }
1060
1061 // masks
1062 for (uint j = 0; j < vi.vRect.size(); ++j) {
1063 cpgstbg(0); // reset background colour to the initial one (white)
1064 cpgsci(1); // reset foreground colour to the initial one (black)
1065 cpgsls(1); // reset line style to solid
1066 cpgslw(1); // reset line width to 1
1067 cpgscf(1); // reset font style to normal
1068 cpgsch(vi.fontSizeDef);// reset font size
1069 cpgsfs(1); // reset fill style (solid)
1070
1071 Plotter2RectInfo ri = vi.vRect[j];
1072 cpgsci(ri.color);
1073 cpgsfs(ri.fill);
1074 cpgslw(ri.width);
1075 cpgshs(45.0, ri.hsep, 0.0);
1076 float* mxdata = new float[4];
1077 float* mydata = new float[4];
1078 mxdata[0] = ri.xmin;
1079 mxdata[1] = ri.xmax;
1080 mxdata[2] = ri.xmax;
1081 mxdata[3] = ri.xmin;
1082 mydata[0] = ri.ymin;
1083 mydata[1] = ri.ymin;
1084 mydata[2] = ri.ymax;
1085 mydata[3] = ri.ymax;
1086 cpgpoly(4, mxdata, mydata);
1087 }
1088
1089 cpgstbg(0); // reset background colour to the initial one (white)
1090 cpgsci(1); // reset foreground colour to the initial one (black)
1091 cpgsls(1); // reset line style to solid
1092 cpgslw(1); // reset line width to 1
1093 cpgscf(1); // reset font style to normal
1094 cpgsch(vi.fontSizeDef);// reset font size
1095 cpgsfs(1); // reset fill style (solid)
1096
1097 cpgbox("BCTS", vi.majorTickIntervalX, vi.nMinorTickWithinMajorTicksX,
1098 "BCTSV", vi.majorTickIntervalY, vi.nMinorTickWithinMajorTicksY);
1099
1100 std::string numformatx, numformaty;
1101 if (vi.numLocationX == "l") {
1102 numformatx = "N";
1103 } else if (vi.numLocationX == "r") {
1104 numformatx = "M";
1105 } else if (vi.numLocationX == "") {
1106 numformatx = "";
1107 }
1108 if (vi.numLocationY == "b") {
1109 numformaty = "NV";
1110 } else if (vi.numLocationY == "t") {
1111 numformaty = "MV";
1112 } else if (vi.numLocationY == "") {
1113 numformaty = "";
1114 }
1115
1116 cpgbox(numformatx.c_str(), vi.majorTickIntervalX * vi.nMajorTickWithinTickNumsX, 0,
1117 numformaty.c_str(), vi.majorTickIntervalY * vi.nMajorTickWithinTickNumsY, 0);
1118
1119 float xpos, ypos;
1120
1121 // x-label
1122 vi.getWorldCoordByWindowCoord(vi.labelXPosX, vi.labelXPosY, &xpos, &ypos);
1123 cpgsch(vi.labelXSize);
1124 cpgsci(vi.labelXColor);
1125 cpgstbg(vi.labelXBColor); //outside viewports, works ONLY with /xwindow
1126 cpgptxt(xpos, ypos, vi.labelXAngle, vi.labelXFJust, vi.labelXString.c_str());
1127
1128 // y-label
1129 vi.getWorldCoordByWindowCoord(vi.labelYPosX, vi.labelYPosY, &xpos, &ypos);
1130 cpgsch(vi.labelYSize);
1131 cpgsci(vi.labelYColor);
1132 cpgstbg(vi.labelYBColor); //outside viewports, works ONLY with /xwindow
1133 cpgptxt(xpos, ypos, vi.labelYAngle, vi.labelYFJust, vi.labelYString.c_str());
1134
1135 // title
1136 vi.getWorldCoordByWindowCoord(vi.titlePosX, vi.titlePosY, &xpos, &ypos);
1137 cpgsch(vi.titleSize);
1138 cpgsci(vi.titleColor);
1139 cpgstbg(vi.titleBColor); //outside viewports, works ONLY with /xwindow
1140 cpgptxt(xpos, ypos, vi.titleAngle, vi.titleFJust, vi.titleString.c_str());
1141 }
1142
1143 }
1144
1145 close();
1146}
1147
1148} // namespace asap
Note: See TracBrowser for help on using the repository browser.