Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OcdFileImport: Revise path coord processing #2224

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 43 additions & 33 deletions src/fileformats/ocd_file_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2127,43 +2127,53 @@ Object* OcdFileImport::importRectangleObject(const Ocd::OcdPoint32* ocd_points,
return border_path;
}

void OcdFileImport::setPathHolePoint(OcdImportedPathObject *object, quint32 pos)
{
// Look for curve start points before the current point and apply hole point only if no such point is there.
// This prevents hole points in the middle of a curve caused by incorrect map objects.
if (pos >= 1 && object->coords[pos].isCurveStart())
; //object->coords[i-1].setHolePoint(true);
else if (pos >= 2 && object->coords[pos-1].isCurveStart())
; //object->coords[i-2].setHolePoint(true);
else if (pos >= 3 && object->coords[pos-2].isCurveStart())
; //object->coords[i-3].setHolePoint(true);
else if (pos > 0) // Don't start with hole point.
object->coords[pos].setHolePoint(true);
}

void OcdFileImport::setPointFlags(OcdImportedPathObject* object, quint32 pos, bool is_area, const Ocd::OcdPoint32& ocd_point)
{
// We can support CurveStart, HolePoint, DashPoint.
// CurveStart needs to be applied to the main point though, not the control point, and
// hole points need to bet set as the last point of a part of an area object instead of the first point of the next part
if (ocd_point.x & Ocd::OcdPoint32::FlagCtl1 && pos > 0)
object->coords[pos-1].setCurveStart(true);
if ((ocd_point.y & Ocd::OcdPoint32::FlagDash) || (ocd_point.y & Ocd::OcdPoint32::FlagCorner))
object->coords[pos].setDashPoint(true);
if (ocd_point.y & Ocd::OcdPoint32::FlagHole && pos > 1 && is_area)
setPathHolePoint(object, pos - 1);
}

/** Translates the OC*D path given in the last two arguments into an Object.
*/
void OcdFileImport::fillPathCoords(OcdImportedPathObject *object, bool is_area, quint32 num_points, const Ocd::OcdPoint32* ocd_points)
void OcdFileImport::fillPathCoords(OcdImportedPathObject *object, bool is_area, quint32 num_points, const Ocd::OcdPoint32* ocd_points) const
{
object->coords.resize(num_points);
for (auto i = 0u; i < num_points; i++)
{
object->coords[i] = convertOcdPoint(ocd_points[i]);
setPointFlags(object, i, is_area, ocd_points[i]);
}

auto const out_first = object->coords.begin();
auto out_coord = out_first;
int ignore_flag_hole = 2;
std::for_each(ocd_points, ocd_points + num_points, [&](auto& ocd_point) {
*out_coord = convertOcdPoint(ocd_point);
if ((ocd_point.y & Ocd::OcdPoint32::FlagDash) || (ocd_point.y & Ocd::OcdPoint32::FlagCorner))
{
out_coord->setDashPoint(true);
}
if ((ocd_point.x & Ocd::OcdPoint32::FlagCtl1) && out_coord != out_first)
{
// CurveStart needs to be applied to the start point
(out_coord-1)->setCurveStart(true);
ignore_flag_hole = 2; // 2nd control point + end point
}
else if (is_area)
{
if (ignore_flag_hole)
{
--ignore_flag_hole;
}
else if (ocd_point.y & Ocd::OcdPoint32::FlagHole)
{
Q_ASSERT(std::distance(out_first, out_coord) >= 2); // implied by initialization of ignore_flag_hole
// HolePoint needs to be applied to the last point of a part
if ((out_coord-2)->isHolePoint())
{
// overwrite current part start (i.e. drop last point from input)
*(out_coord-1) = *out_coord;
--out_coord;
}
else
{
// HolePoint needs to be applied to the last point of a part
(out_coord-1)->setHolePoint(true);
}
}
}
++out_coord;
});
object->coords.resize(std::distance(out_first, out_coord));

// For path objects, create closed parts where the position of the last point is equal to that of the first point
if (object->getType() == Object::Path)
Expand Down
6 changes: 1 addition & 5 deletions src/fileformats/ocd_file_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,7 @@ class OcdFileImport : public Importer

// Some helper functions that are used in multiple places

void setPointFlags(OcdImportedPathObject* object, quint32 pos, bool is_area, const Ocd::OcdPoint32& ocd_point);

void setPathHolePoint(OcdFileImport::OcdImportedPathObject* object, quint32 pos);

void fillPathCoords(OcdFileImport::OcdImportedPathObject* object, bool is_area, quint32 num_points, const Ocd::OcdPoint32* ocd_points);
void fillPathCoords(OcdFileImport::OcdImportedPathObject* object, bool is_area, quint32 num_points, const Ocd::OcdPoint32* ocd_points) const;

bool fillTextPathCoords(TextObject* object, TextSymbol* symbol, quint32 npts, const Ocd::OcdPoint32* ocd_points);

Expand Down
3 changes: 0 additions & 3 deletions test/file_format_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,6 @@ void FileFormatTest::ocdPathImportTest_data()
0,
0,
MapCoord::ClosePoint | MapCoord::HolePoint,
MapCoord::ClosePoint | MapCoord::HolePoint,
0,
0,
MapCoord::ClosePoint
Expand All @@ -1479,8 +1478,6 @@ void FileFormatTest::ocdPathImportTest_data()
0,
0,
MapCoord::ClosePoint | MapCoord::HolePoint,
MapCoord::ClosePoint | MapCoord::HolePoint,
MapCoord::ClosePoint | MapCoord::HolePoint,
0,
0,
MapCoord::ClosePoint
Expand Down