]> Shamusworld >> Repos - ttedit/commitdiff
Added individual polygon rotation tool.
authorShamus Hammons <jlhamm@acm.org>
Fri, 30 Nov 2012 02:20:18 +0000 (20:20 -0600)
committerShamus Hammons <jlhamm@acm.org>
Fri, 30 Nov 2012 02:20:18 +0000 (20:20 -0600)
src/editwindow.cpp
src/glyphpoints.cpp
src/glyphpoints.h
src/toolwindow.h

index 89a4ef85d056a1e5912fde8b68623346c8378e35..242d237bb14a877b1038e5af334fd137e641a5a2 100755 (executable)
@@ -163,8 +163,6 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/)
        {
                if (i == ptHighlight)
                {
-//                     dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));
-////                   SelectObject(hdc, hRedPen1);
                        p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
 
                        if (pts.GetOnCurve(i))
@@ -180,8 +178,6 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/)
                }
                else if ((i == ptHighlight || i == ptNextHighlight) && tool == TOOLAddPt)
                {
-//                     dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0xAF, 0x00), 1, wxSOLID)));
-////                   SelectObject(hdc, hGreenPen1);
                        p.setPen(QPen(Qt::green, 1.0, Qt::SolidLine));
 
                        if (pts.GetOnCurve(i))
@@ -197,8 +193,6 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/)
                }
                else
                {
-//                     dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));
-////                   SelectObject(hdc, hBlackPen1);
                        p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
 
                        if (pts.GetOnCurve(i))
@@ -209,129 +203,32 @@ void EditWindow::paintEvent(QPaintEvent * /*event*/)
 
                if (tool == TOOLDelPt && i == ptHighlight)
                {
-#if 0
-                       dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0xFF, 0x00, 0x00), 1, wxSOLID)));
-//                     SelectObject(hdc, hRedPen1);
-//                     MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5, NULL);
-//                     LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) + 5);
-//                     LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) - 5);//Lameness!
-//                     MoveToEx(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5, NULL);
-//                     LineTo(hdc, pts.GetX(i) + 5, pts.GetY(i) - 5);
-//                     LineTo(hdc, pts.GetX(i) - 5, pts.GetY(i) + 5);//More lameness!!
-#endif
                        p.setPen(QPen(Qt::red, 1.0, Qt::SolidLine));
                        p.drawLine(pts.GetX(i) - 5, pts.GetY(i) - 5, pts.GetX(i) + 5, pts.GetY(i) + 5);
                        p.drawLine(pts.GetX(i) + 5, pts.GetY(i) - 5, pts.GetX(i) - 5, pts.GetY(i) + 5);
                }
        }
 
-////           SelectObject(hdc, hBlackPen1);
-//     dc.SetPen(*(wxThePenList->FindOrCreatePen(wxColour(0x00, 0x00, 0x00), 1, wxSOLID)));
-       p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
-
        // Draw curve formed by points
 
-       for(int poly=0; poly<pts.GetNumPolys(); poly++)
-       {
-               if (pts.GetNumPoints(poly) > 2)
-               {
-                       // Initial move...
-                       // If it's not on curve, then move to it, otherwise move to last point...
-
-                       int x, y;
-
-                       if (pts.GetOnCurve(poly, pts.GetNumPoints(poly) - 1))
-                               x = (int)pts.GetX(poly, pts.GetNumPoints(poly) - 1), y = (int)pts.GetY(poly, pts.GetNumPoints(poly) - 1);
-                       else
-                               x = (int)pts.GetX(poly, 0), y = (int)pts.GetY(poly, 0);
-
-                       for(int i=0; i<pts.GetNumPoints(poly); i++)
-                       {
-                               if (pts.GetOnCurve(poly, i))
-//                                     LineTo(hdc, pts.GetX(poly, i), pts.GetY(poly, i));
-                               {
-                                       p.drawLine(x, y, pts.GetX(poly, i), pts.GetY(poly, i));
-                                       x = (int)pts.GetX(poly, i), y = (int)pts.GetY(poly, i);
-                               }
-                               else
-                               {
-                                       uint32 prev = pts.GetPrev(poly, i), next = pts.GetNext(poly, i);
-                                       float px = pts.GetX(poly, prev), py = pts.GetY(poly, prev),
-                                               nx = pts.GetX(poly, next), ny = pts.GetY(poly, next);
-
-                                       if (!pts.GetOnCurve(poly, prev))
-                                               px = (px + pts.GetX(poly, i)) / 2.0f,
-                                               py = (py + pts.GetY(poly, i)) / 2.0f;
-
-                                       if (!pts.GetOnCurve(poly, next))
-                                               nx = (nx + pts.GetX(poly, i)) / 2.0f,
-                                               ny = (ny + pts.GetY(poly, i)) / 2.0f;
-
-                                       Bezier(p, point(px, py), point(pts.GetX(poly, i), pts.GetY(poly, i)), point(nx, ny));
-                                       x = (int)nx, y = (int)ny;
-
-                                       if (pts.GetOnCurve(poly, next))
-                                               i++;                                    // Following point is on curve, so move past it
-                               }
-                       }
-               }
-       }
+       p.setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
+       DrawGlyph(p, pts);
 
        if (haveZeroPoint)
        {
                // Rotation code
                GlyphPoints rotated = pts;
-               rotated.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
-               p.setPen(QPen(QColor(255, 0, 255), 1.0, Qt::SolidLine));
-#if 0
-               for(int poly=0; poly<rotated.GetNumPolys(); poly++)
-               {
-                       if (rotated.GetNumPoints(poly) > 2)
-                       {
-                               // Initial move...
-                               // If it's not on curve, then move to it, otherwise move to last point...
-
-                               int x, y;
 
-                               if (rotated.GetOnCurve(poly, rotated.GetNumPoints(poly) - 1))
-                                       x = (int)rotated.GetX(poly, rotated.GetNumPoints(poly) - 1), y = (int)rotated.GetY(poly, rotated.GetNumPoints(poly) - 1);
-                               else
-                                       x = (int)rotated.GetX(poly, 0), y = (int)rotated.GetY(poly, 0);
-
-                               for(int i=0; i<rotated.GetNumPoints(poly); i++)
-                               {
-                                       if (rotated.GetOnCurve(poly, i))
-       //                                      LineTo(hdc, rotated.GetX(poly, i), rotated.GetY(poly, i));
-                                       {
-                                               p.drawLine(x, y, rotated.GetX(poly, i), rotated.GetY(poly, i));
-                                               x = (int)rotated.GetX(poly, i), y = (int)rotated.GetY(poly, i);
-                                       }
-                                       else
-                                       {
-                                               uint32 prev = rotated.GetPrev(poly, i), next = rotated.GetNext(poly, i);
-                                               float px = rotated.GetX(poly, prev), py = rotated.GetY(poly, prev),
-                                                       nx = rotated.GetX(poly, next), ny = rotated.GetY(poly, next);
-
-                                               if (!rotated.GetOnCurve(poly, prev))
-                                                       px = (px + rotated.GetX(poly, i)) / 2.0f,
-                                                       py = (py + rotated.GetY(poly, i)) / 2.0f;
-
-                                               if (!rotated.GetOnCurve(poly, next))
-                                                       nx = (nx + rotated.GetX(poly, i)) / 2.0f,
-                                                       ny = (ny + rotated.GetY(poly, i)) / 2.0f;
-
-                                               Bezier(p, point(px, py), point(rotated.GetX(poly, i), rotated.GetY(poly, i)), point(nx, ny));
-                                               x = (int)nx, y = (int)ny;
-
-                                               if (rotated.GetOnCurve(poly, next))
-                                                       i++;                                    // Following point is on curve, so move past it
-                                       }
-                               }
-                       }
+               if (tool == TOOLRotate)
+                       rotated.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
+               else if (tool == TOOLRotatePoly)
+               {
+                       uint16 poly = rotated.GetPolyForPointNumber(ptHighlight);
+                       rotated.RotatePolyAroundCentroid(poly, rotationAngle);
                }
-#else
+
+               p.setPen(QPen(QColor(255, 0, 255), 1.0, Qt::SolidLine));
                DrawGlyph(p, rotated);
-#endif
        }
 }
 
@@ -473,6 +370,18 @@ WriteLogMsg(" --> [# polys: %u, # points: %u]\n", pts.GetNumPolys(), pts.GetNumP
                        rotationAngle = 0;
                        update();
                }
+               else if (tool == TOOLRotatePoly)
+               {
+                       IPoint centroid = pts.GetPolyCentroid(pts.GetPolyForPointNumber(ptHighlight));
+                       rotationCenter = QPoint(centroid.x, centroid.y);
+                       showRotationCenter = true;
+                       pt = GetAdjustedClientPosition(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
+                       QCursor::setPos(mapToGlobal(pt));
+                       rotationZeroPoint = QPoint(pts.GetX(ptHighlight), pts.GetY(ptHighlight));
+                       haveZeroPoint = true;
+                       rotationAngle = 0;
+                       update();
+               }
        }
 
        event->accept();
@@ -529,7 +438,7 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event)
                                update();
                        }
                }
-               else if (tool == TOOLRotate)
+               else if (tool == TOOLRotate || tool == TOOLRotatePoly)
                {
                        if (pts.GetNumPoints() > 0)
                        {
@@ -563,7 +472,7 @@ void EditWindow::mouseMoveEvent(QMouseEvent * event)
        {
                // Moving, not dragging...
                if (tool == TOOLSelect || tool == TOOLDelPt || tool == TOOLAddPt
-                       || tool == TOOLPolySelect)// || tool == TOOLAddPoly)
+                       || tool == TOOLPolySelect || tool == TOOLRotatePoly)
                {
                        QPoint pt2 = GetAdjustedMousePosition(event);
                        double closest = 1.0e+99;
@@ -694,7 +603,15 @@ void EditWindow::mouseReleaseEvent(QMouseEvent * event)
                {
                        showRotationCenter = false;
                        haveZeroPoint = false;
-                       pts.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
+
+                       if (tool == TOOLRotate)
+                               pts.RotatePoints(rotationAngle, IPoint(rotationCenter.x(), rotationCenter.y()));
+                       else
+                       {
+                               uint16 poly = pts.GetPolyForPointNumber(ptHighlight);
+                               pts.RotatePolyAroundCentroid(poly, rotationAngle);
+                       }
+
                        update();
                        ((TTEdit *)qApp)->mainWindow->statusBar()->showMessage("");
                }
index e6dc52c11ca5ec7ffef3dad62ddcbef2fa1c0fe0..05edc828d791d44bba987e683b0ed935f1c1b858 100755 (executable)
@@ -145,19 +145,6 @@ GlyphPoints& GlyphPoints::operator=(const GlyphPoints &c)
        if (this == &c)
                return *this;                                                   // Take care of self-assignment
 
-#if 0
-       if (x)
-               delete[] x;
-
-       if (y)
-               delete[] y;
-
-       if (onCurve)
-               delete[] onCurve;
-
-       if (polyEnd)
-               delete[] polyEnd;
-#endif
        FreeAllocatedMemory();
        AllocateAndCopy(c.numPoints, c.numPolys, c.x, c.y, c.onCurve, c.polyEnd);
 
@@ -454,6 +441,15 @@ IPoint GlyphPoints::GetPoint(uint16 poly, uint16 pt)
 }
 
 
+IPoint GlyphPoints::GetPoint(uint16 pointNumber)
+{
+       if (pointNumber > numPoints)
+               throw GP_OUT_OF_RANGE;
+
+       return IPoint(x[pointNumber], y[pointNumber]);
+}
+
+
 bool GlyphPoints::GetOnCurve(uint16 poly, uint16 pt)
 {
        if (pt >= GetNumPoints(poly))
@@ -655,6 +651,7 @@ uint16 GlyphPoints::GetNext(uint16 poly, uint16 pt)
 }
 
 
+#warning "!!! This function returns incorrect results !!!"
 uint16 GlyphPoints::GetPoly(uint16 pt)
 {
        if (pt >= numPoints)
@@ -730,6 +727,40 @@ IPoint GlyphPoints::GetNextPoint(uint16 poly, uint16 pt)
 }
 
 
+uint16 GlyphPoints::GetPolyForPoint(IPoint point)
+{
+       uint16 poly = 0;
+
+       for(uint16 i=0; i<numPoints; i++)
+       {
+               if (i > polyEnd[poly])
+                       poly++;
+
+               if (IPoint(x[i], y[i]) == point)
+                       return poly;
+       }
+
+       return 0xFFFF;
+}
+
+
+uint16 GlyphPoints::GetPolyForPointNumber(uint16 pointNumber)
+{
+       // If there's only one poly, we know where the point is...
+       if (numPolys <= 1)
+               return 0;
+
+       // Otherwise, do a linear search through the polys to find the right one
+       for(uint16 i=0; i<numPolys; i++)
+       {
+               if (pointNumber >= GetPolyStart(i) && pointNumber <= polyEnd[i])
+                       return i;
+       }
+
+       return 0xFFFF;
+}
+
+
 //
 // Rotate a point by "angle" around point "center"
 //
index 09b422cde5967b107901e686e68c8d4dc0845060..fc0dd2b40fd17d1d56909c8c39bff35c79aba19e 100755 (executable)
@@ -21,6 +21,7 @@ struct IPoint
        bool onCurve;
 
        IPoint(int32 xx=0, int32 yy=0, bool oc=true): x(xx), y(yy), onCurve(oc) {}
+       bool operator==(const IPoint & p) { return (p.x == x && p.y == y ? true: false); };
 };
 
 // Throws the following exceptions:
@@ -63,6 +64,7 @@ class GlyphPoints
                int GetY(uint16, uint16);
                int GetNextY(uint16, uint16);
                IPoint GetPoint(uint16, uint16);
+               IPoint GetPoint(uint16);
                bool GetOnCurve(uint16, uint16);
                bool GetPrevOnCurve(uint16, uint16);
                bool GetNextOnCurve(uint16, uint16);
@@ -83,6 +85,8 @@ class GlyphPoints
                IPoint GetMidpointToNext(uint16, uint16);
                IPoint GetPrevPoint(uint16, uint16);
                IPoint GetNextPoint(uint16, uint16);
+               uint16 GetPolyForPoint(IPoint point);
+               uint16 GetPolyForPointNumber(uint16 pointNumber);
 
                IPoint RotatePoint(const double angle, const IPoint point, const IPoint center);
                void RotatePoints(const double angle, const IPoint point);
index 323b563c5a1ccbe2d993785eb24562d033b0d17f..f9134bc2f0392ecde8ec627f544f2e2abcb504ad 100755 (executable)
@@ -23,7 +23,7 @@ enum ToolType {
        TOOLDelPt,                                                              // Delete point tool
        TOOLDelPoly,                                                    // Delete polygon tool
        TOOLRotate,                                                             // Rotate tool
-       TOOLRotatePoly                                                  // ROtate polygon around centroid tool
+       TOOLRotatePoly                                                  // Rotate polygon around centroid tool
 };
 
 class ToolWindow: public QWidget