FreeCAD

Форк
0
/
Segmentation.cpp 
169 строк · 6.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2012 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24
#ifndef _PreComp_
25
#include <sstream>
26
#endif
27

28
#include <App/Application.h>
29
#include <App/Document.h>
30
#include <App/DocumentObjectGroup.h>
31
#include <Mod/Mesh/App/Core/Curvature.h>
32
#include <Mod/Mesh/App/Core/Segmentation.h>
33
#include <Mod/Mesh/App/Core/Smoothing.h>
34
#include <Mod/Mesh/App/MeshFeature.h>
35

36
#include "Segmentation.h"
37
#include "ui_Segmentation.h"
38

39

40
using namespace MeshGui;
41

42
Segmentation::Segmentation(Mesh::Feature* mesh, QWidget* parent, Qt::WindowFlags fl)
43
    : QWidget(parent, fl)
44
    , ui(new Ui_Segmentation)
45
    , myMesh(mesh)
46
{
47
    ui->setupUi(this);
48
    ui->numPln->setRange(1, INT_MAX);
49
    ui->numPln->setValue(100);
50
    ui->crvCyl->setRange(0, INT_MAX);
51
    ui->numCyl->setRange(1, INT_MAX);
52
    ui->numCyl->setValue(100);
53
    ui->crvSph->setRange(0, INT_MAX);
54
    ui->numSph->setRange(1, INT_MAX);
55
    ui->numSph->setValue(100);
56
    ui->crv1Free->setRange(-INT_MAX, INT_MAX);
57
    ui->crv2Free->setRange(-INT_MAX, INT_MAX);
58
    ui->numFree->setRange(1, INT_MAX);
59
    ui->numFree->setValue(100);
60

61
    ui->checkBoxSmooth->setChecked(false);
62
}
63

64
Segmentation::~Segmentation()
65
{
66
    // no need to delete child widgets, Qt does it all for us
67
    delete ui;
68
}
69

70
void Segmentation::accept()
71
{
72
    const Mesh::MeshObject* mesh = myMesh->Mesh.getValuePtr();
73
    // make a copy because we might smooth the mesh before
74
    MeshCore::MeshKernel kernel = mesh->getKernel();
75

76
    if (ui->checkBoxSmooth->isChecked()) {
77
        MeshCore::LaplaceSmoothing smoother(kernel);
78
        smoother.Smooth(ui->smoothSteps->value());
79
    }
80

81
    MeshCore::MeshSegmentAlgorithm finder(kernel);
82
    MeshCore::MeshCurvature meshCurv(kernel);
83
    meshCurv.ComputePerVertex();
84

85
    std::vector<MeshCore::MeshSurfaceSegmentPtr> segm;
86
    if (ui->groupBoxFree->isChecked()) {
87
        segm.emplace_back(
88
            std::make_shared<MeshCore::MeshCurvatureFreeformSegment>(meshCurv.GetCurvature(),
89
                                                                     ui->numFree->value(),
90
                                                                     ui->tol1Free->value(),
91
                                                                     ui->tol2Free->value(),
92
                                                                     ui->crv1Free->value(),
93
                                                                     ui->crv2Free->value()));
94
    }
95
    if (ui->groupBoxCyl->isChecked()) {
96
        segm.emplace_back(
97
            std::make_shared<MeshCore::MeshCurvatureCylindricalSegment>(meshCurv.GetCurvature(),
98
                                                                        ui->numCyl->value(),
99
                                                                        ui->tol1Cyl->value(),
100
                                                                        ui->tol2Cyl->value(),
101
                                                                        ui->crvCyl->value()));
102
    }
103
    if (ui->groupBoxSph->isChecked()) {
104
        segm.emplace_back(
105
            std::make_shared<MeshCore::MeshCurvatureSphericalSegment>(meshCurv.GetCurvature(),
106
                                                                      ui->numSph->value(),
107
                                                                      ui->tolSph->value(),
108
                                                                      ui->crvSph->value()));
109
    }
110
    if (ui->groupBoxPln->isChecked()) {
111
        segm.emplace_back(
112
            std::make_shared<MeshCore::MeshCurvaturePlanarSegment>(meshCurv.GetCurvature(),
113
                                                                   ui->numPln->value(),
114
                                                                   ui->tolPln->value()));
115
    }
116
    finder.FindSegments(segm);
117

118
    App::Document* document = App::GetApplication().getActiveDocument();
119
    document->openTransaction("Segmentation");
120

121
    std::string internalname = "Segments_";
122
    internalname += myMesh->getNameInDocument();
123
    App::DocumentObjectGroup* group = static_cast<App::DocumentObjectGroup*>(
124
        document->addObject("App::DocumentObjectGroup", internalname.c_str()));
125
    std::string labelname = "Segments ";
126
    labelname += myMesh->Label.getValue();
127
    group->Label.setValue(labelname);
128
    for (const auto& it : segm) {
129
        const std::vector<MeshCore::MeshSegment>& data = it->GetSegments();
130
        for (const auto& jt : data) {
131
            Mesh::MeshObject* segment = mesh->meshFromSegment(jt);
132
            Mesh::Feature* feaSegm =
133
                static_cast<Mesh::Feature*>(group->addObject("Mesh::Feature", "Segment"));
134
            Mesh::MeshObject* feaMesh = feaSegm->Mesh.startEditing();
135
            feaMesh->swap(*segment);
136
            feaSegm->Mesh.finishEditing();
137
            delete segment;
138

139
            std::stringstream label;
140
            label << feaSegm->Label.getValue() << " (" << it->GetType() << ")";
141
            feaSegm->Label.setValue(label.str());
142
        }
143
    }
144
    document->commitTransaction();
145
}
146

147
void Segmentation::changeEvent(QEvent* e)
148
{
149
    if (e->type() == QEvent::LanguageChange) {
150
        ui->retranslateUi(this);
151
    }
152
    QWidget::changeEvent(e);
153
}
154

155
// ---------------------------------------
156

157
/* TRANSLATOR MeshGui::TaskRemoveComponents */
158

159
TaskSegmentation::TaskSegmentation(Mesh::Feature* mesh)
160
{
161
    widget = new Segmentation(mesh);  // NOLINT
162
    addTaskBox(widget, false);
163
}
164

165
bool TaskSegmentation::accept()
166
{
167
    widget->accept();
168
    return true;
169
}
170

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.