2014-12-12 15:08:31 +01:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vobjengine.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 12 12, 2014
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2014-12-12 15:08:31 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2014-12-12 15:08:31 +01:00
|
|
|
**
|
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vobjengine.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QFlag>
|
|
|
|
#include <QFlags>
|
|
|
|
#include <QIODevice>
|
|
|
|
#include <QLatin1Char>
|
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <QPaintEngineState>
|
|
|
|
#include <QPainterPath>
|
|
|
|
#include <QPointF>
|
|
|
|
#include <QString>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QVector>
|
2023-07-15 12:47:14 +02:00
|
|
|
#include <QtDebug>
|
2023-07-15 14:12:17 +02:00
|
|
|
#include <QtMath>
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2023-07-15 12:47:14 +02:00
|
|
|
#include "../vmisc/defglobal.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2023-07-17 15:33:57 +02:00
|
|
|
// Header <ciso646> is removed in C++20.
|
|
|
|
#if __cplusplus <= 201703L
|
|
|
|
#include <ciso646> // and, not, or
|
|
|
|
#endif
|
|
|
|
|
2023-10-07 17:56:39 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
|
|
|
|
#include "../vmisc/compatibility.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
class QPaintDevice;
|
|
|
|
class QPixmap;
|
|
|
|
class QPoint;
|
|
|
|
class QPointF;
|
|
|
|
class QPolygonF;
|
|
|
|
class QRectF;
|
2015-10-08 19:07:48 +02:00
|
|
|
|
2014-12-12 15:08:31 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
static inline auto svgEngineFeatures() -> QPaintEngine::PaintEngineFeatures
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
QT_WARNING_PUSH
|
|
|
|
QT_WARNING_DISABLE_CLANG("-Wsign-conversion")
|
|
|
|
// cppcheck-suppress unknownMacro
|
|
|
|
QT_WARNING_DISABLE_INTEL(68)
|
|
|
|
QT_WARNING_DISABLE_INTEL(2022)
|
|
|
|
|
2023-07-15 14:12:17 +02:00
|
|
|
return {QPaintEngine::AllFeatures & ~QPaintEngine::PatternBrush & ~QPaintEngine::PerspectiveTransform &
|
|
|
|
~QPaintEngine::ConicalGradientFill & ~QPaintEngine::PorterDuff};
|
2023-07-15 12:47:14 +02:00
|
|
|
|
|
|
|
QT_WARNING_POP
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VObjEngine::VObjEngine()
|
2023-07-15 12:47:14 +02:00
|
|
|
: QPaintEngine(svgEngineFeatures()),
|
|
|
|
stream(),
|
|
|
|
globalPointsCount(0),
|
|
|
|
outputDevice(),
|
|
|
|
planeCount(0),
|
|
|
|
size(),
|
|
|
|
resolution(96),
|
|
|
|
matrix()
|
2014-12-21 21:33:31 +01:00
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
for (int i = 0; i < MAX_POINTS; i++)
|
2014-12-21 21:33:31 +01:00
|
|
|
{
|
|
|
|
points[i].x = 0;
|
|
|
|
points[i].y = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 15:08:31 +01:00
|
|
|
|
2015-10-20 16:32:01 +02:00
|
|
|
#if defined(Q_CC_INTEL)
|
2023-07-15 12:47:14 +02:00
|
|
|
#pragma warning(pop)
|
2015-10-20 16:32:01 +02:00
|
|
|
#endif
|
|
|
|
|
2014-12-12 15:08:31 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::begin(QPaintDevice *pdev) -> bool
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(pdev)
|
2015-10-20 16:32:01 +02:00
|
|
|
if (outputDevice.isNull())
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
qWarning("VObjEngine::begin(), no output device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (outputDevice->isOpen() == false)
|
|
|
|
{
|
|
|
|
if (outputDevice->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate) == false)
|
|
|
|
{
|
|
|
|
qWarning("VObjEngine::begin(), could not open output device: '%s'",
|
|
|
|
qPrintable(outputDevice->errorString()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (outputDevice->isWritable() == false)
|
|
|
|
{
|
|
|
|
qWarning("VObjEngine::begin(), could not write to read-only output device: '%s'",
|
|
|
|
qPrintable(outputDevice->errorString()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size.isValid() == false)
|
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
qWarning() << "VObjEngine::begin(), size is not valid";
|
2014-12-12 15:08:31 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-20 16:32:01 +02:00
|
|
|
stream = QSharedPointer<QTextStream>(new QTextStream(outputDevice.data()));
|
2023-07-15 12:47:14 +02:00
|
|
|
*stream << "# Valentina OBJ File" << Qt::endl;
|
|
|
|
*stream << "# smart-pattern.com.ua/" << Qt::endl;
|
2014-12-12 15:08:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::end() -> bool
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2015-10-20 16:32:01 +02:00
|
|
|
stream.reset();
|
2014-12-12 15:08:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-12-12 15:08:31 +01:00
|
|
|
void VObjEngine::updateState(const QPaintEngineState &state)
|
|
|
|
{
|
|
|
|
QPaintEngine::DirtyFlags flags = state.state();
|
|
|
|
|
|
|
|
// always stream full gstate, which is not required, but...
|
|
|
|
flags |= QPaintEngine::AllDirty;
|
|
|
|
|
|
|
|
if (flags & QPaintEngine::DirtyTransform)
|
|
|
|
{
|
2020-07-07 09:51:00 +02:00
|
|
|
matrix = state.transform(); // Save new matrix for moving paths
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VObjEngine::drawPath(const QPainterPath &path)
|
|
|
|
{
|
|
|
|
QPolygonF polygon = path.toFillPolygon(matrix);
|
2023-07-15 12:47:14 +02:00
|
|
|
polygon = MakePointsUnique(polygon); // Points must be unique
|
2014-12-12 15:08:31 +01:00
|
|
|
if (polygon.size() < 3)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-19 17:09:56 +01:00
|
|
|
qint64 const sq = Square(polygon);
|
2014-12-12 15:08:31 +01:00
|
|
|
|
|
|
|
++planeCount;
|
2023-10-07 17:56:39 +02:00
|
|
|
*stream << "o Plane." << u"%1"_s.arg(planeCount, 3, 10, '0'_L1) << Qt::endl;
|
2014-12-12 15:08:31 +01:00
|
|
|
|
2015-10-26 09:36:07 +01:00
|
|
|
quint32 num_points = 0;
|
2014-12-12 15:08:31 +01:00
|
|
|
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto &p : polygon)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
if (num_points < MAX_POINTS)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
points[num_points].x = p.x();
|
|
|
|
points[num_points].y = p.y();
|
2014-12-12 15:08:31 +01:00
|
|
|
num_points++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = 0;
|
2023-07-15 12:47:14 +02:00
|
|
|
delaunay2d_t *res = delaunay2d_from(points, num_points); // Calculate faces
|
2014-12-12 15:08:31 +01:00
|
|
|
|
2015-03-02 18:11:43 +01:00
|
|
|
QPointF pf[MAX_POINTS];
|
2022-08-08 14:25:14 +02:00
|
|
|
// cppcheck-suppress unreadVariable
|
2023-07-15 12:47:14 +02:00
|
|
|
bool skipFace = false; // Need skip first face
|
2014-12-12 15:08:31 +01:00
|
|
|
|
2023-07-15 12:47:14 +02:00
|
|
|
for (quint32 i = 0; i < res->num_faces; i++)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
if (offset == 0)
|
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
skipFace = true;
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
skipFace = false;
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
2024-02-19 17:09:56 +01:00
|
|
|
int const num_verts = static_cast<int>(res->faces[offset]);
|
2014-12-12 15:08:31 +01:00
|
|
|
offset++;
|
2023-07-15 12:47:14 +02:00
|
|
|
for (int j = 0; j < num_verts; j++)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2024-02-19 17:09:56 +01:00
|
|
|
int const p0 = static_cast<int>(res->faces[offset + j]);
|
2014-12-12 15:08:31 +01:00
|
|
|
pf[j] = QPointF(points[p0].x, points[p0].y);
|
|
|
|
}
|
2023-07-15 12:47:14 +02:00
|
|
|
if (skipFace == false)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
QPolygonF face;
|
2024-04-17 12:02:25 +02:00
|
|
|
face.reserve(num_verts);
|
2023-07-15 12:47:14 +02:00
|
|
|
for (int ind = 0; ind < num_verts; ind++)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2022-08-08 14:25:14 +02:00
|
|
|
face << QPointF(pf[ind]);
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
2024-02-19 17:09:56 +01:00
|
|
|
QPolygonF const united = polygon.united(face);
|
|
|
|
qint64 const sqUnited = Square(united);
|
2015-03-02 18:11:43 +01:00
|
|
|
if (sqUnited <= sq)
|
2023-07-15 12:47:14 +02:00
|
|
|
{ // This face incide our base polygon.
|
2014-12-12 15:08:31 +01:00
|
|
|
drawPolygon(pf, num_verts, QPaintEngine::OddEvenMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset += num_verts;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:47:14 +02:00
|
|
|
delaunay2d_release(res); // Don't forget release data
|
2020-07-07 09:51:00 +02:00
|
|
|
*stream << "s off" << Qt::endl;
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VObjEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
|
|
|
|
{
|
|
|
|
Q_UNUSED(mode)
|
|
|
|
|
|
|
|
drawPoints(points, pointCount);
|
|
|
|
*stream << "f";
|
|
|
|
|
|
|
|
for (int i = 0; i < pointCount; ++i)
|
|
|
|
{
|
2023-10-07 17:56:39 +02:00
|
|
|
*stream << u" %1"_s.arg(static_cast<int>(globalPointsCount) - pointCount + i + 1);
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
2020-07-07 09:51:00 +02:00
|
|
|
*stream << Qt::endl;
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::type() const -> QPaintEngine::Type
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
return QPaintEngine::User;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VObjEngine::drawPoints(const QPointF *points, int pointCount)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < pointCount; ++i)
|
|
|
|
{
|
2024-02-19 17:09:56 +01:00
|
|
|
qreal const x = ((points[i].x() - 0) / qFloor(size.width() / 2.0)) - 1.0;
|
|
|
|
qreal const y = (((points[i].y() - 0) / qFloor(size.width() / 2.0)) - 1.0) * -1;
|
2014-12-12 15:08:31 +01:00
|
|
|
|
2023-07-15 12:47:14 +02:00
|
|
|
*stream << "v"
|
|
|
|
<< " " << QString::number(x, 'f', 6) << " " << QString::number(y, 'f', 6) << " "
|
2020-07-07 09:51:00 +02:00
|
|
|
<< "0.000000" << Qt::endl;
|
2014-12-12 15:08:31 +01:00
|
|
|
++globalPointsCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 11:56:31 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2015-04-15 14:44:57 +02:00
|
|
|
// cppcheck-suppress unusedFunction
|
2014-12-12 15:08:31 +01:00
|
|
|
void VObjEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
|
|
|
|
{
|
|
|
|
Q_UNUSED(r)
|
|
|
|
Q_UNUSED(pm)
|
|
|
|
Q_UNUSED(sr)
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::getSize() const -> QSize
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VObjEngine::setSize(const QSize &value)
|
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT(not isActive());
|
2014-12-12 15:08:31 +01:00
|
|
|
size = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::getOutputDevice() const -> QIODevice *
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2015-10-20 16:32:01 +02:00
|
|
|
return outputDevice.data();
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VObjEngine::setOutputDevice(QIODevice *value)
|
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT(not isActive());
|
2015-10-20 16:32:01 +02:00
|
|
|
outputDevice.reset(value);
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::getResolution() const -> int
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
return resolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VObjEngine::setResolution(int value)
|
|
|
|
{
|
2015-10-28 15:22:36 +01:00
|
|
|
Q_ASSERT(not isActive());
|
2014-12-12 15:08:31 +01:00
|
|
|
resolution = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::MakePointsUnique(const QPolygonF &polygon) const -> QPolygonF
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
QVector<QPointF> set;
|
|
|
|
QPolygonF uniquePolygon;
|
2018-04-03 13:36:38 +02:00
|
|
|
for (auto p : polygon)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
if (set.contains(p) == false)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2018-04-03 13:36:38 +02:00
|
|
|
set.append(p);
|
|
|
|
uniquePolygon.append(p);
|
2014-12-12 15:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return uniquePolygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-03 13:07:02 +02:00
|
|
|
auto VObjEngine::Square(const QPolygonF &poly) const -> qint64
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2024-02-19 17:09:56 +01:00
|
|
|
vsizetype const n = poly.count();
|
2014-12-12 15:08:31 +01:00
|
|
|
qreal s, res = 0;
|
|
|
|
qint64 sq = 0;
|
|
|
|
|
2024-04-17 12:02:25 +02:00
|
|
|
QVector<qreal> x;
|
|
|
|
QVector<qreal> y;
|
|
|
|
|
|
|
|
x.reserve(n);
|
|
|
|
y.reserve(n);
|
|
|
|
|
2023-07-15 12:47:14 +02:00
|
|
|
for (int i = 0; i < n; i++)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
|
|
|
x.append(poly.at(i).x());
|
|
|
|
y.append(poly.at(i).y());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculation a polygon area through the sum of the areas of trapezoids
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
s = x.at(i) * (y.at(n - 1) - y.at(i + 1)); // if i == 0, then y[i-1] replace on y[n-1]
|
2014-12-12 15:08:31 +01:00
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
if (i == n - 1)
|
2014-12-12 15:08:31 +01:00
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
s = x.at(i) * (y.at(i - 1) - y.at(0)); // if i == n-1, then y[i+1] replace on y[0]
|
2014-12-12 15:08:31 +01:00
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-15 12:47:14 +02:00
|
|
|
s = x.at(i) * (y.at(i - 1) - y.at(i + 1));
|
2014-12-12 15:08:31 +01:00
|
|
|
res += s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-15 12:47:14 +02:00
|
|
|
sq = qFloor(qAbs(res / 2.0));
|
2014-12-12 15:08:31 +01:00
|
|
|
return sq;
|
|
|
|
}
|