File: ArcsGraphicsItem.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (180 lines) | stat: -rw-r--r-- 4,376 bytes parent folder | download | duplicates (2)
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
#ifndef CGAL_QT_ARCS_GRAPHICS_ITEM_H
#define CGAL_QT_ARCS_GRAPHICS_ITEM_H

#include <CGAL/Bbox_2.h>
#include <CGAL/Qt/PainterOstream.h>
#include <CGAL/Qt/GraphicsItem.h>
#include <CGAL/Qt/Converter.h>

#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>

namespace CGAL {
namespace Qt {

template <typename CK>
class ArcsGraphicsItem : public GraphicsItem
{
  typedef typename CK::Circle_2 Circle_2;
  typedef typename CK::Circular_arc_2 Circular_arc_2;
  typedef typename CK::Circular_arc_point_2 Circular_arc_point_2;
  typedef typename CK::Line_arc_2 Line_arc_2;
  typedef std::variant<std::pair<Circular_arc_point_2,unsigned>, Circular_arc_2, Line_arc_2 > Inter_variant;
  typedef std::variant<Circular_arc_2, Line_arc_2 > Arc_variant;

  std::vector<Arc_variant>& arcs;
  std::vector<Inter_variant>& intersections;

public:
  ArcsGraphicsItem(std::vector<Arc_variant>& arcs_, std::vector<Inter_variant>& intersections_);

  void modelChanged();

public:
  QRectF boundingRect() const;

  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);


  const QPen& intersectionsPen() const
  {
    return intersections_pen;
  }

  const QPen& inputPen() const
  {
    return input_pen;
  }

  void setIntersectionsPen(const QPen& pen)
  {
    intersections_pen = pen;
  }

  void setInputPen(const QPen& pen)
  {
    input_pen = pen;
  }

protected:
  void updateBoundingBox();

  QPainter* m_painter;
  PainterOstream<CK> painterostream;

  QRectF bounding_rect;

  QPen intersections_pen;
  QPen input_pen;
  Converter<CK> convert;
};


template <typename CK>
ArcsGraphicsItem<CK>::ArcsGraphicsItem(std::vector<Arc_variant>& arcs_, std::vector<Inter_variant>& intersections_)
  :  arcs(arcs_), intersections(intersections_), painterostream(nullptr)
{
  setIntersectionsPen(QPen(::Qt::red, 3.));
  if(arcs.empty()){
    this->hide();
  }
  updateBoundingBox();
  setZValue(3);
}

template <typename CK>
QRectF
ArcsGraphicsItem<CK>::boundingRect() const
{
  return bounding_rect;
}



template <typename CK>
void
ArcsGraphicsItem<CK>::paint(QPainter *painter,
                                    const QStyleOptionGraphicsItem *,
                                    QWidget * )
{
  painter->setPen(this->inputPen());
  painterostream = PainterOstream<CK>(painter);

  for(typename std::vector<Arc_variant>::iterator it = arcs.begin(); it != arcs.end(); ++it){
    Circular_arc_2 ca;
    Line_arc_2 la;
    if(auto ca = std::get_if<Circular_arc_2>(&(*it))){
      painterostream << *ca;
    } else if(auto la = std::get_if<Line_arc_2>(&(*it))){
      painterostream << *la;
    }
  }


  painter->setPen(this->intersectionsPen());
  painterostream = PainterOstream<CK>(painter);
  for(typename std::vector<Inter_variant>::iterator it = intersections.begin(); it != intersections.end(); ++it){
    if(auto cap_ui = std::get_if<std::pair<Circular_arc_point_2,unsigned>>(&*it)){
      QTransform matrix = painter->worldTransform();
      painter->resetTransform();
      painter->drawPoint(matrix.map(convert(cap_ui->first)));
      painter->setWorldTransform(matrix);
    }if(auto ca = std::get_if<Circular_arc_2>(&(*it))){
      painterostream << *ca;
    } else if(auto la = std::get_if<Line_arc_2>(&(*it))){
      painterostream << *la;
    }
  }
}

template <typename CK>
void
ArcsGraphicsItem<CK>::updateBoundingBox()
{
  bounding_rect = QRectF(0,0,100, 100);
  Bbox_2 bb;
  bool initialized = false;
  for(typename std::vector<Arc_variant>::iterator it = arcs.begin(); it != arcs.end(); ++it){
    Circular_arc_2 ca;
    Line_arc_2 la;
    if(auto ca = std::get_if<Circular_arc_2>(&(*it))){
      if(initialized){
        bb = bb + ca->supporting_circle().bbox();
      } else {
        initialized = true;
        bb = ca->supporting_circle().bbox();
      }
    } else if(auto la = std::get_if<Line_arc_2>(&(*it))){
      if(initialized){
        bb = bb + la->bbox();
      } else {
        initialized = true;
        bb = la->bbox();
      }
    }
  }

  prepareGeometryChange();
  bounding_rect = convert(bb);
}


template <typename CK>
void
ArcsGraphicsItem<CK>::modelChanged()
{
  if((arcs.empty()) ){
    this->hide();
  } else if((! arcs.empty()) && (! this->isVisible())){
    this->show();
  }
  updateBoundingBox();
  update();
}


} // namespace Qt
} // namespace CGAL

#endif // CGAL_QT_ARCS_GRAPHICS_ITEM_H