/* * DISTRHO Plugin Framework (DPF) * Copyright (C) 2012-2019 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this * permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "../Geometry.hpp" #include START_NAMESPACE_DGL static const float M_2PIf = 3.14159265358979323846f*2.0f; // ----------------------------------------------------------------------- // Point template Point::Point() noexcept : x(0), y(0) {} template Point::Point(const T& x2, const T& y2) noexcept : x(x2), y(y2) {} template Point::Point(const Point& pos) noexcept : x(pos.x), y(pos.y) {} template const T& Point::getX() const noexcept { return x; } template const T& Point::getY() const noexcept { return y; } template void Point::setX(const T& x2) noexcept { x = x2; } template void Point::setY(const T& y2) noexcept { y = y2; } template void Point::setPos(const T& x2, const T& y2) noexcept { x = x2; y = y2; } template void Point::setPos(const Point& pos) noexcept { x = pos.x; y = pos.y; } template void Point::moveBy(const T& x2, const T& y2) noexcept { x = static_cast(x+x2); y = static_cast(y+y2); } template void Point::moveBy(const Point& pos) noexcept { x = static_cast(x+pos.x); y = static_cast(y+pos.y); } template bool Point::isZero() const noexcept { return x == 0 && y == 0; } template bool Point::isNotZero() const noexcept { return x != 0 || y != 0; } template Point Point::operator+(const Point& pos) noexcept { return Point(x+pos.x, y+pos.y); } template Point Point::operator-(const Point& pos) noexcept { return Point(x-pos.x, y-pos.y); } template Point& Point::operator=(const Point& pos) noexcept { x = pos.x; y = pos.y; return *this; } template Point& Point::operator+=(const Point& pos) noexcept { x = static_cast(x+pos.x); y = static_cast(y+pos.y); return *this; } template Point& Point::operator-=(const Point& pos) noexcept { x = static_cast(x-pos.x); y = static_cast(y-pos.y); return *this; } template bool Point::operator==(const Point& pos) const noexcept { return (x == pos.x && y == pos.y); } template bool Point::operator!=(const Point& pos) const noexcept { return (x != pos.x || y != pos.y); } // ----------------------------------------------------------------------- // Size template Size::Size() noexcept : fWidth(0), fHeight(0) {} template Size::Size(const T& width, const T& height) noexcept : fWidth(width), fHeight(height) {} template Size::Size(const Size& size) noexcept : fWidth(size.fWidth), fHeight(size.fHeight) {} template const T& Size::getWidth() const noexcept { return fWidth; } template const T& Size::getHeight() const noexcept { return fHeight; } template void Size::setWidth(const T& width) noexcept { fWidth = width; } template void Size::setHeight(const T& height) noexcept { fHeight = height; } template void Size::setSize(const T& width, const T& height) noexcept { fWidth = width; fHeight = height; } template void Size::setSize(const Size& size) noexcept { fWidth = size.fWidth; fHeight = size.fHeight; } template void Size::growBy(double multiplier) noexcept { fWidth = static_cast(static_cast(fWidth)*multiplier); fHeight = static_cast(static_cast(fHeight)*multiplier); } template void Size::shrinkBy(double divider) noexcept { fWidth = static_cast(static_cast(fWidth)/divider); fHeight = static_cast(static_cast(fHeight)/divider); } template bool Size::isNull() const noexcept { return fWidth == 0 && fHeight == 0; } template bool Size::isNotNull() const noexcept { return fWidth != 0 || fHeight != 0; } template bool Size::isValid() const noexcept { return fWidth > 0 && fHeight > 0; } template bool Size::isInvalid() const noexcept { return fWidth <= 0 || fHeight <= 0; } template Size Size::toInt() const noexcept { return Size(static_cast(fWidth), static_cast(fHeight)); } template<> Size Size::toInt() const noexcept { return Size(static_cast(fWidth + 0.5), static_cast(fHeight + 0.5)); } template<> Size Size::toInt() const noexcept { return Size(static_cast(fWidth + 0.5f), static_cast(fHeight + 0.5f)); } template Size Size::operator+(const Size& size) noexcept { return Size(fWidth+size.fWidth, fHeight+size.fHeight); } template Size Size::operator-(const Size& size) noexcept { return Size(fWidth-size.fWidth, fHeight-size.fHeight); } template Size& Size::operator=(const Size& size) noexcept { fWidth = size.fWidth; fHeight = size.fHeight; return *this; } template Size& Size::operator+=(const Size& size) noexcept { fWidth = static_cast(fWidth+size.fWidth); fHeight = static_cast(fHeight+size.fHeight); return *this; } template Size& Size::operator-=(const Size& size) noexcept { fWidth = static_cast(fWidth-size.fWidth); fHeight = static_cast(fHeight-size.fHeight); return *this; } template Size& Size::operator*=(double m) noexcept { fWidth = static_cast(static_cast(fWidth)*m); fHeight = static_cast(static_cast(fHeight)*m); return *this; } template Size& Size::operator/=(double d) noexcept { fWidth = static_cast(static_cast(fWidth)/d); fHeight = static_cast(static_cast(fHeight)/d); return *this; } template bool Size::operator==(const Size& size) const noexcept { return (fWidth == size.fWidth && fHeight == size.fHeight); } template bool Size::operator!=(const Size& size) const noexcept { return (fWidth != size.fWidth || fHeight != size.fHeight); } // ----------------------------------------------------------------------- // Line template Line::Line() noexcept : posStart(0, 0), posEnd(0, 0) {} template Line::Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept : posStart(startX, startY), posEnd(endX, endY) {} template Line::Line(const T& startX, const T& startY, const Point& endPos) noexcept : posStart(startX, startY), posEnd(endPos) {} template Line::Line(const Point& startPos, const T& endX, const T& endY) noexcept : posStart(startPos), posEnd(endX, endY) {} template Line::Line(const Point& startPos, const Point& endPos) noexcept : posStart(startPos), posEnd(endPos) {} template Line::Line(const Line& line) noexcept : posStart(line.posStart), posEnd(line.posEnd) {} template const T& Line::getStartX() const noexcept { return posStart.x; } template const T& Line::getStartY() const noexcept { return posStart.y; } template const T& Line::getEndX() const noexcept { return posEnd.x; } template const T& Line::getEndY() const noexcept { return posEnd.y; } template const Point& Line::getStartPos() const noexcept { return posStart; } template const Point& Line::getEndPos() const noexcept { return posEnd; } template void Line::setStartX(const T& x) noexcept { posStart.x = x; } template void Line::setStartY(const T& y) noexcept { posStart.y = y; } template void Line::setStartPos(const T& x, const T& y) noexcept { posStart = Point(x, y); } template void Line::setStartPos(const Point& pos) noexcept { posStart = pos; } template void Line::setEndX(const T& x) noexcept { posEnd.x = x; } template void Line::setEndY(const T& y) noexcept { posEnd.y = y; } template void Line::setEndPos(const T& x, const T& y) noexcept { posEnd = Point(x, y); } template void Line::setEndPos(const Point& pos) noexcept { posEnd = pos; } template void Line::moveBy(const T& x, const T& y) noexcept { posStart.moveBy(x, y); posEnd.moveBy(x, y); } template void Line::moveBy(const Point& pos) noexcept { posStart.moveBy(pos); posEnd.moveBy(pos); } template bool Line::isNull() const noexcept { return posStart == posEnd; } template bool Line::isNotNull() const noexcept { return posStart != posEnd; } template Line& Line::operator=(const Line& line) noexcept { posStart = line.posStart; posEnd = line.posEnd; return *this; } template bool Line::operator==(const Line& line) const noexcept { return (posStart == line.posStart && posEnd == line.posEnd); } template bool Line::operator!=(const Line& line) const noexcept { return (posStart != line.posStart || posEnd != line.posEnd); } // ----------------------------------------------------------------------- // Circle template Circle::Circle() noexcept : fPos(0, 0), fSize(0.0f), fNumSegments(0), fTheta(0.0f), fCos(0.0f), fSin(0.0f) {} template Circle::Circle(const T& x, const T& y, const float size, const uint numSegments) : fPos(x, y), fSize(size), fNumSegments(numSegments >= 3 ? numSegments : 3), fTheta(M_2PIf / static_cast(fNumSegments)), fCos(std::cos(fTheta)), fSin(std::sin(fTheta)) { DISTRHO_SAFE_ASSERT(fSize > 0.0f); } template Circle::Circle(const Point& pos, const float size, const uint numSegments) : fPos(pos), fSize(size), fNumSegments(numSegments >= 3 ? numSegments : 3), fTheta(M_2PIf / static_cast(fNumSegments)), fCos(std::cos(fTheta)), fSin(std::sin(fTheta)) { DISTRHO_SAFE_ASSERT(fSize > 0.0f); } template Circle::Circle(const Circle& cir) noexcept : fPos(cir.fPos), fSize(cir.fSize), fNumSegments(cir.fNumSegments), fTheta(cir.fTheta), fCos(cir.fCos), fSin(cir.fSin) { DISTRHO_SAFE_ASSERT(fSize > 0.0f); } template const T& Circle::getX() const noexcept { return fPos.x; } template const T& Circle::getY() const noexcept { return fPos.y; } template const Point& Circle::getPos() const noexcept { return fPos; } template void Circle::setX(const T& x) noexcept { fPos.x = x; } template void Circle::setY(const T& y) noexcept { fPos.y = y; } template void Circle::setPos(const T& x, const T& y) noexcept { fPos.x = x; fPos.y = y; } template void Circle::setPos(const Point& pos) noexcept { fPos = pos; } template float Circle::getSize() const noexcept { return fSize; } template void Circle::setSize(const float size) noexcept { DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,); fSize = size; } template uint Circle::getNumSegments() const noexcept { return fNumSegments; } template void Circle::setNumSegments(const uint num) { DISTRHO_SAFE_ASSERT_RETURN(num >= 3,); if (fNumSegments == num) return; fNumSegments = num; fTheta = M_2PIf / static_cast(fNumSegments); fCos = std::cos(fTheta); fSin = std::sin(fTheta); } template Circle& Circle::operator=(const Circle& cir) noexcept { fPos = cir.fPos; fSize = cir.fSize; fTheta = cir.fTheta; fCos = cir.fCos; fSin = cir.fSin; fNumSegments = cir.fNumSegments; return *this; } template bool Circle::operator==(const Circle& cir) const noexcept { return (fPos == cir.fPos && d_isEqual(fSize, cir.fSize) && fNumSegments == cir.fNumSegments); } template bool Circle::operator!=(const Circle& cir) const noexcept { return (fPos != cir.fPos || d_isNotEqual(fSize, cir.fSize) || fNumSegments != cir.fNumSegments); } // ----------------------------------------------------------------------- // Triangle template Triangle::Triangle() noexcept : pos1(0, 0), pos2(0, 0), pos3(0, 0) {} template Triangle::Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept : pos1(x1, y1), pos2(x2, y2), pos3(x3, y3) {} template Triangle::Triangle(const Point& p1, const Point& p2, const Point& p3) noexcept : pos1(p1), pos2(p2), pos3(p3) {} template Triangle::Triangle(const Triangle& tri) noexcept : pos1(tri.pos1), pos2(tri.pos2), pos3(tri.pos3) {} template bool Triangle::isNull() const noexcept { return pos1 == pos2 && pos1 == pos3; } template bool Triangle::isNotNull() const noexcept { return pos1 != pos2 || pos1 != pos3; } template bool Triangle::isValid() const noexcept { return pos1 != pos2 && pos1 != pos3; } template bool Triangle::isInvalid() const noexcept { return pos1 == pos2 || pos1 == pos3; } template Triangle& Triangle::operator=(const Triangle& tri) noexcept { pos1 = tri.pos1; pos2 = tri.pos2; pos3 = tri.pos3; return *this; } template bool Triangle::operator==(const Triangle& tri) const noexcept { return (pos1 == tri.pos1 && pos2 == tri.pos2 && pos3 == tri.pos3); } template bool Triangle::operator!=(const Triangle& tri) const noexcept { return (pos1 != tri.pos1 || pos2 != tri.pos2 || pos3 != tri.pos3); } // ----------------------------------------------------------------------- // Rectangle template Rectangle::Rectangle() noexcept : pos(0, 0), size(0, 0) {} template Rectangle::Rectangle(const T& x, const T& y, const T& w, const T& h) noexcept : pos(x, y), size(w, h) {} template Rectangle::Rectangle(const T& x, const T& y, const Size& s) noexcept : pos(x, y), size(s) {} template Rectangle::Rectangle(const Point& p, const T& w, const T& h) noexcept : pos(p), size(w, h) {} template Rectangle::Rectangle(const Point& p, const Size& s) noexcept : pos(p), size(s) {} template Rectangle::Rectangle(const Rectangle& rect) noexcept : pos(rect.pos), size(rect.size) {} template const T& Rectangle::getX() const noexcept { return pos.x; } template const T& Rectangle::getY() const noexcept { return pos.y; } template const T& Rectangle::getWidth() const noexcept { return size.fWidth; } template const T& Rectangle::getHeight() const noexcept { return size.fHeight; } template const Point& Rectangle::getPos() const noexcept { return pos; } template const Size& Rectangle::getSize() const noexcept { return size; } template void Rectangle::setX(const T& x) noexcept { pos.x = x; } template void Rectangle::setY(const T& y) noexcept { pos.y = y; } template void Rectangle::setPos(const T& x, const T& y) noexcept { pos.x = x; pos.y = y; } template void Rectangle::setPos(const Point& pos2) noexcept { pos = pos2; } template void Rectangle::moveBy(const T& x, const T& y) noexcept { pos.moveBy(x, y); } template void Rectangle::moveBy(const Point& pos2) noexcept { pos.moveBy(pos2); } template void Rectangle::setWidth(const T& width) noexcept { size.fWidth = width; } template void Rectangle::setHeight(const T& height) noexcept { size.fHeight = height; } template void Rectangle::setSize(const T& width, const T& height) noexcept { size.fWidth = width; size.fHeight = height; } template void Rectangle::setSize(const Size& size2) noexcept { size = size2; } template void Rectangle::growBy(double multiplier) noexcept { size.growBy(multiplier); } template void Rectangle::shrinkBy(double divider) noexcept { size.shrinkBy(divider); } template void Rectangle::setRectangle(const Point& pos2, const Size& size2) noexcept { pos = pos2; size = size2; } template void Rectangle::setRectangle(const Rectangle& rect) noexcept { pos = rect.pos; size = rect.size; } template bool Rectangle::contains(const T& x, const T& y) const noexcept { return (x >= pos.x && y >= pos.y && x <= pos.x+size.fWidth && y <= pos.y+size.fHeight); } template bool Rectangle::contains(const Point& p) const noexcept { return contains(p.x, p.y); } template bool Rectangle::containsX(const T& x) const noexcept { return (x >= pos.x && x <= pos.x + size.fWidth); } template bool Rectangle::containsY(const T& y) const noexcept { return (y >= pos.y && y <= pos.y + size.fHeight); } template bool Rectangle::isNull() const noexcept { return size.isNull(); } template bool Rectangle::isNotNull() const noexcept { return size.isNotNull(); } template bool Rectangle::isValid() const noexcept { return size.isValid(); } template bool Rectangle::isInvalid() const noexcept { return size.isInvalid(); } template Rectangle& Rectangle::operator=(const Rectangle& rect) noexcept { pos = rect.pos; size = rect.size; return *this; } template Rectangle& Rectangle::operator*=(double m) noexcept { size *= m; return *this; } template Rectangle& Rectangle::operator/=(double d) noexcept { size /= d; return *this; } template bool Rectangle::operator==(const Rectangle& rect) const noexcept { return (pos == rect.pos && size == rect.size); } template bool Rectangle::operator!=(const Rectangle& rect) const noexcept { return (pos != rect.pos || size != rect.size); } // ----------------------------------------------------------------------- // Possible template data types template class Point; template class Point; template class Point; template class Point; template class Point; template class Point; template class Size; template class Size; template class Size; template class Size; template class Size; template class Size; template class Line; template class Line; template class Line; template class Line; template class Line; template class Line; template class Circle; template class Circle; template class Circle; template class Circle; template class Circle; template class Circle; template class Triangle; template class Triangle; template class Triangle; template class Triangle; template class Triangle; template class Triangle; template class Rectangle; template class Rectangle; template class Rectangle; template class Rectangle; template class Rectangle; template class Rectangle; // ----------------------------------------------------------------------- END_NAMESPACE_DGL