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

Improve generation of polylines #8291

Merged
merged 2 commits into from
Jul 23, 2024
Merged
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
163 changes: 101 additions & 62 deletions Lab/demo/Lab/Plugins/PCA/Basic_generator_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,92 +732,130 @@ void Basic_generator_plugin::generateLines()
{
QString text = dock_widget->line_textEdit->toPlainText();
std::list<std::vector<Scene_polylines_item::Point_3> > polylines;
polylines.resize(polylines.size()+1);
std::vector<Scene_polylines_item::Point_3>& polyline = *(polylines.rbegin());
QStringList polylines_metadata;

QStringList list = text.split(QRegularExpression("\\s+"), CGAL_QT_SKIP_EMPTY_PARTS);
int counter = 0;
double coord[3];
bool ok = true;
if (list.isEmpty()) return;
if(!dock_widget->polygon_checkBox->isChecked() && list.size()%3!=0){
QMessageBox *msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("ERROR : Input should consists of triplets.");
msgBox->exec();
return;
}
else if(dock_widget->polygon_checkBox->isChecked()&& list.size()%2!=0){
QMessageBox *msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("ERROR : Input should consists of pairs.");
msgBox->exec();
return;
}
for(QString s : list)
auto read_polyline = [&polylines](const QStringList& list, bool is_2d, bool is_closed)
{
if(!s.isEmpty())
int counter = -1;
double coord[3];
bool ok = true;

if(!is_2d && list.size()%3!=0){
QMessageBox *msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("ERROR : Input should consists of triplets.");
msgBox->exec();
return false;
}
else if(is_2d && list.size()%2!=0){
QMessageBox *msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("ERROR : Input should consists of pairs.");
msgBox->exec();
return false;
}

polylines.back().reserve(list.size()+(is_closed?1:0));

for(QString s : list)
{
double res = s.toDouble(&ok);
if(!ok)
if(!s.isEmpty())
{
QMessageBox *msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("ERROR : Coordinates are invalid.");
msgBox->exec();
break;
double res = s.toDouble(&ok);
if(!ok)
{
QMessageBox *msgBox = new QMessageBox;
msgBox->setWindowTitle("Error");
msgBox->setText("ERROR : Coordinates are invalid.");
msgBox->exec();
return false;
}
else
{
coord[++counter] = res;
}
}
else
if(!is_2d && counter == 2)
{
coord[counter] = res;
counter++;
Scene_polylines_item::Point_3 p(coord[0], coord[1], coord[2]);
polylines.back().push_back(p);
counter=-1;
}
else if(is_2d && counter == 1)
{
Scene_polylines_item::Point_3 p(coord[0], coord[1], 0);
polylines.back().push_back(p);
counter=-1;
}
}
if(!dock_widget->polygon_checkBox->isChecked() && counter == 3)
if(is_closed)
{
Scene_polylines_item::Point_3 p(coord[0], coord[1], coord[2]);
polyline.push_back(p);
counter =0;
if (polylines.back().back()!=polylines.back().front())
polylines.back().push_back(polylines.back().front()); //close if not already closed
}
else if(dock_widget->polygon_checkBox->isChecked() && counter == 2)

return true;
};

const bool is_2d = dock_widget->dim2_checkBox->isChecked();
const bool is_closed = dock_widget->polygon_checkBox->isChecked();
const bool shall_fill = is_closed && dock_widget->fill_checkBox->isChecked();
const bool oneperline = dock_widget->oneperline_checkBox->isChecked();

bool ok = true;
if (oneperline)
{
QStringList poly_list = text.split(QRegularExpression("\\n"), CGAL_QT_SKIP_EMPTY_PARTS);
if (poly_list.empty()) return;

for(const QString& qs : poly_list)
{
Scene_polylines_item::Point_3 p(coord[0], coord[1], 0);
polyline.push_back(p);
counter = 0;
QStringList list = qs.split(QRegularExpression("\\s+"), CGAL_QT_SKIP_EMPTY_PARTS);
if (list.isEmpty()) continue;
polylines.emplace_back();
ok = read_polyline(list, is_2d, is_closed);
if (!ok) return;
}
}
if(dock_widget->polygon_checkBox->isChecked())
else
{
polyline.push_back(polyline.front()); //polygon_2 are not closed.
QStringList list = text.split(QRegularExpression("\\s+"), CGAL_QT_SKIP_EMPTY_PARTS);
if (list.isEmpty()) return;
polylines.emplace_back();
ok = read_polyline(list, is_2d, is_closed);
if (!ok) return;
}

if(ok)
{
dock_widget->line_textEdit->clear();
if(dock_widget->fill_checkBox->isChecked())
if(shall_fill)
{
CGAL::Three::Three::CursorScopeGuard guard(Qt::WaitCursor);
QApplication::processEvents();
if(polyline.front() != polyline.back()) {
polyline.push_back(polyline.front());
}
if(polyline.size() < 4) { // no triangle, skip it (needs at least 3 + 1 repeat)
QMessageBox::warning(mw, "Warning", "Needs at least 3 points to triangulate. Aborting.");
return;
}
std::vector<Face> patch;
CGAL::Polygon_mesh_processing::triangulate_hole_polyline(polyline,
std::back_inserter(patch),
CGAL::parameters::use_delaunay_triangulation(true));

if(patch.empty()) {
QMessageBox::warning(mw, "Warning", "Triangulation failed.");
SMesh* poly = new SMesh;
for (const auto& polyline : polylines)
{
if(polyline.size() < 4) { // no triangle, skip it (needs at least 3 + 1 repeat)
QMessageBox::warning(mw, "Warning", "Needs at least 3 points to triangulate. Aborting.");
delete poly;
return;
}

std::vector<Face> patch;
CGAL::Polygon_mesh_processing::triangulate_hole_polyline(polyline,
std::back_inserter(patch),
CGAL::parameters::use_delaunay_triangulation(true));

if(patch.empty()) {
QMessageBox::warning(mw, "Warning", "Triangulation failed.");
return;
}

CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(polyline,
patch,
*poly);
}
SMesh* poly = new SMesh;
CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(polyline,
patch,
*poly);

Scene_surface_mesh_item* poly_item = new Scene_surface_mesh_item(poly);
poly_item->setName(dock_widget->name_lineEdit->text());
Expand All @@ -831,6 +869,7 @@ void Basic_generator_plugin::generateLines()
item->invalidateOpenGLBuffers();
item->setName(dock_widget->name_lineEdit->text());
item->setColor(Qt::black);
QStringList polylines_metadata;
item->setProperty("polylines metadata", polylines_metadata);
Scene_interface::Item_id id = scene->addItem(item);
scene->setSelectedItem(id);
Expand Down
38 changes: 36 additions & 2 deletions Lab/demo/Lab/Plugins/PCA/Basic_generator_widget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -1101,20 +1101,37 @@ li.checked::marker { content: &quot;\2612&quot;; }
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_23">
<item>
<widget class="QCheckBox" name="dim2_checkBox">
<property name="text">
<string>2D</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="polygon_checkBox">
<property name="text">
<string>From Polygon_2</string>
<string>Polygon(s)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="fill_checkBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Triangulate</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="oneperline_checkBox">
<property name="text">
<string>One Per Line</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -1222,5 +1239,22 @@ li.checked::marker { content: &quot;\2612&quot;; }
<include location="../../CGALlab.qrc"/>
<include location="../../CGALlab.qrc"/>
</resources>
<connections/>
<connections>
<connection>
<sender>polygon_checkBox</sender>
<signal>stateChanged(int)</signal>
<receiver>fill_checkBox</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>190</x>
<y>83</y>
</hint>
<hint type="destinationlabel">
<x>534</x>
<y>83</y>
</hint>
</hints>
</connection>
</connections>
</ui>