File: shape.cpp

package info (click to toggle)
musescore3 3.2.3%2Bdfsg2-16
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 214,188 kB
  • sloc: cpp: 291,198; xml: 200,238; sh: 3,779; ansic: 1,447; python: 393; makefile: 244; perl: 82; pascal: 79
file content (356 lines) | stat: -rw-r--r-- 10,208 bytes parent folder | download | duplicates (4)
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
//=============================================================================
//  MuseScore
//  Music Composition & Notation
//
//  Copyright (C) 2016 Werner Schweer
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2
//  as published by the Free Software Foundation and appearing in
//  the file LICENCE.GPL
//=============================================================================

#include "shape.h"
#include "segment.h"

namespace Ms {

//---------------------------------------------------------
//   addHorizontalSpacing
//    Currently implemented by adding rectangles of zero
//    height to the Y position corresponding to the type.
//    This is a simple solution but has its drawbacks too.
//---------------------------------------------------------

void Shape::addHorizontalSpacing(HorizontalSpacingType type, qreal leftEdge, qreal rightEdge)
      {
      constexpr qreal eps = 100 * std::numeric_limits<qreal>::epsilon();
      const qreal y = eps * int(type);
      if (leftEdge == rightEdge) // HACK zero-width shapes collide with everything currently.
            rightEdge += eps;
      add(QRectF(leftEdge, y, rightEdge - leftEdge, 0));
      }

//---------------------------------------------------------
//   translate
//---------------------------------------------------------

void Shape::translate(const QPointF& pt)
      {
      for (QRectF& r : *this)
            r.translate(pt);
      }

void Shape::translateX(qreal xo)
      {
      for (QRectF& r : *this) {
            r.setLeft(r.left() + xo);
            r.setRight(r.right() + xo);
            }
      }
void Shape::translateY(qreal yo)
      {
      for (QRectF& r : *this) {
            r.setTop(r.top() + yo);
            r.setBottom(r.bottom() + yo);
            }
      }

//---------------------------------------------------------
//   translated
//---------------------------------------------------------

Shape Shape::translated(const QPointF& pt) const
      {
      Shape s;
      for (const ShapeElement& r : *this)
#ifndef NDEBUG
            s.add(r.translated(pt), r.text);
#else
            s.add(r.translated(pt));
#endif
      return s;
      }

//-------------------------------------------------------------------
//   minHorizontalDistance
//    a is located right of this shape.
//    Calculates the minimum horizontal distance between the two shapes
//    so they don’t touch.
//-------------------------------------------------------------------

qreal Shape::minHorizontalDistance(const Shape& a) const
      {
      qreal dist = -1000000.0;      // min real
      for (const QRectF& r2 : a) {
            qreal by1 = r2.top();
            qreal by2 = r2.bottom();
            for (const QRectF& r1 : *this) {
                  qreal ay1 = r1.top();
                  qreal ay2 = r1.bottom();
                  if (Ms::intersects(ay1, ay2, by1, by2)
                     || ((r1.height() == 0.0) && (r2.height() == 0.0) && (ay1 == by1))
                     || ((r1.width() == 0.0) || (r2.width() == 0.0)))
                        dist = qMax(dist, r1.right() - r2.left());
                  }
            }
      return dist;
      }

//-------------------------------------------------------------------
//   minVerticalDistance
//    a is located below this shape.
//    Calculates the minimum distance between two shapes.
//-------------------------------------------------------------------

qreal Shape::minVerticalDistance(const Shape& a) const
      {
      qreal dist = -1000000.0;      // min real
      for (const QRectF& r2 : a) {
            if (r2.height() <= 0.0)
                  continue;
            qreal bx1 = r2.left();
            qreal bx2 = r2.right();
            for (const QRectF& r1 : *this) {
                  if (r1.height() <= 0.0)
                        continue;
                  qreal ax1 = r1.left();
                  qreal ax2 = r1.right();
                  if (Ms::intersects(ax1, ax2, bx1, bx2))
                        dist = qMax(dist, r1.bottom() - r2.top());
                  }
            }
      return dist;
      }

//---------------------------------------------------------
//   left
//    compute left border
//---------------------------------------------------------

qreal Shape::left() const
      {
      qreal dist = 0.0;
      for (const QRectF& r : *this) {
            if (r.height() != 0.0 && r.left() < dist)
            // if (r.left() < dist)
                  dist = r.left();
            }
      return -dist;
      }

//---------------------------------------------------------
//   right
//    compute right border
//---------------------------------------------------------

qreal Shape::right() const
      {
      qreal dist = 0.0;
      for (const QRectF& r : *this) {
            if (r.right() > dist)
                  dist = r.right();
            }
      return dist;
      }

//---------------------------------------------------------
//   top
//---------------------------------------------------------

qreal Shape::top() const
      {
      qreal dist = 1000000.0;
      for (const QRectF& r : *this) {
            if (r.top() < dist)
                  dist = r.top();
            }
      return dist;
      }

//---------------------------------------------------------
//   bottom
//---------------------------------------------------------

qreal Shape::bottom() const
      {
      qreal dist = -1000000.0;
      for (const QRectF& r : *this) {
            if (r.bottom() > dist)
                  dist = r.bottom();
            }
      return dist;
      }

//---------------------------------------------------------
//   topDistance
//    p is on top of shape
//    returns negative values if there is an overlap
//---------------------------------------------------------

qreal Shape::topDistance(const QPointF& p) const
      {
      qreal dist = 1000000.0;
      for (const QRectF& r : *this) {
            if (p.x() >= r.left() && p.x() < r.right())
                  dist = qMin(dist, r.top() - p.y());
            }
      return dist;
      }

//---------------------------------------------------------
//   bottomDistance
//    p is below the shape
//    returns negative values if there is an overlap
//---------------------------------------------------------

qreal Shape::bottomDistance(const QPointF& p) const
      {
      qreal dist = 1000000.0;
      for (const QRectF& r : *this) {
            if (p.x() >= r.left() && p.x() < r.right())
                  dist = qMin(dist, p.y() - r.bottom());
            }
      return dist;
      }

//---------------------------------------------------------
//   remove
//---------------------------------------------------------

void Shape::remove(const QRectF& r)
      {
      for (auto i = begin(); i != end(); ++i) {
            if (*i == r) {
                  erase(i);
                  return;
                  }
            }
      // qWarning("Shape::remove: QRectF not found in Shape");
      qFatal("Shape::remove: QRectF not found in Shape");
      }

void Shape::remove(const Shape& s)
      {
      for (const QRectF& r : s)
            remove(r);
      }

//---------------------------------------------------------
//   contains
//---------------------------------------------------------

bool Shape::contains(const QPointF& p) const
      {
      for (const QRectF& r : *this) {
            if (r.contains(p))
                  return true;
            }
      return false;
      }

//---------------------------------------------------------
//   intersects
//---------------------------------------------------------

bool Shape::intersects(const QRectF& rr) const
      {
      for (const QRectF& r : *this) {
            if (r.intersects(rr))
                  return true;
            }
      return false;
      }

//---------------------------------------------------------
//   intersects
//---------------------------------------------------------

bool Shape::intersects(const Shape& other) const
      {
      for (const QRectF& r : other) {
            if (intersects(r))
                  return true;
            }
      return false;
      }

//---------------------------------------------------------
//   paint
//---------------------------------------------------------

void Shape::paint(QPainter& p) const
      {
      for (const QRectF& r : *this)
            p.drawRect(r);
      }

#ifndef NDEBUG
//---------------------------------------------------------
//   dump
//---------------------------------------------------------

void Shape::dump(const char* p) const
      {
      qDebug("Shape dump: %p %s size %zu", this, p, size());
      for (const ShapeElement& r : *this) {
            r.dump();
            }
      }

void ShapeElement::dump() const
      {
      qDebug("   %s: %f %f %f %f", text ? text : "", x(), y(), width(), height());
      }

//---------------------------------------------------------
//   add
//---------------------------------------------------------

void Shape::add(const QRectF& r, const char* t)
      {
      push_back(ShapeElement(r, t));
      }

#endif

#ifdef DEBUG_SHAPES
//---------------------------------------------------------
//   testShapes
//---------------------------------------------------------

void testShapes()
      {
      printf("======test shapes======\n");

      //=======================
      //    minDistance()
      //=======================
      Shape a;
      Shape b;

      a.add(QRectF(-10, -10, 20, 20));
      qreal d = a.minHorizontalDistance(b);           // b is empty
      printf("      minHDistance (0.0): %f", d);
      if (d != 0.0)
            printf("   =====error");
      printf("\n");

      b.add(QRectF(0, 0, 10, 10));
      d = a.minHorizontalDistance(b);
      printf("      minHDistance (10.0): %f", d);
      if (d != 10.0)
            printf("   =====error");
      printf("\n");

      d = a.minVerticalDistance(b);
      printf("      minVDistance (10.0): %f", d);
      if (d != 10.0)
            printf("   =====error");
      printf("\n");
      }
#endif // DEBUG_SHAPES


} // namespace Ms