1
/***************************************************************************
2
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
32
#include <Inventor/SoPickedPoint.h>
33
#include <Inventor/details/SoFaceDetail.h>
34
#include <Inventor/actions/SoSearchAction.h>
35
#include <Inventor/events/SoLocation2Event.h>
36
#include <Inventor/events/SoMouseButtonEvent.h>
37
#include <Inventor/nodes/SoDrawStyle.h>
38
#include <Inventor/nodes/SoIndexedFaceSet.h>
39
#include <Inventor/nodes/SoMaterial.h>
40
#include <Inventor/nodes/SoShapeHints.h>
41
#include <Inventor/sensors/SoIdleSensor.h>
44
#include <boost/range/adaptors.hpp>
46
#include <App/Annotation.h>
47
#include <App/Document.h>
48
#include <App/DocumentObjectGroup.h>
49
#include <Base/Console.h>
50
#include <Gui/Application.h>
51
#include <Gui/BitmapFactory.h>
52
#include <Gui/Document.h>
53
#include <Gui/MainWindow.h>
54
#include <Gui/Selection.h>
55
#include <Gui/SoFCColorBar.h>
56
#include <Gui/SoFCSelection.h>
57
#include <Gui/View3DInventorViewer.h>
58
#include <Gui/Widgets.h>
60
#include <Mod/Mesh/App/FeatureMeshCurvature.h>
61
#include <Mod/Mesh/App/MeshFeature.h>
63
#include "ViewProviderCurvature.h"
67
using namespace MeshGui;
70
bool ViewProviderMeshCurvature::addflag = false;
72
PROPERTY_SOURCE(MeshGui::ViewProviderMeshCurvature, Gui::ViewProviderDocumentObject)
74
ViewProviderMeshCurvature::ViewProviderMeshCurvature()
77
pcColorRoot = new SoSeparator();
79
pcColorMat = new SoMaterial;
81
pcColorStyle = new SoDrawStyle();
82
pcColorRoot->addChild(pcColorStyle);
84
pcColorBar = new Gui::SoFCColorBar;
85
pcColorBar->Attach(this);
87
pcColorBar->setRange(-0.5f, 0.5f, 3);
88
pcLinkRoot = new SoGroup;
93
const SbColor* cols {};
94
if (pcColorMat->ambientColor.getNum() == 1) {
95
cols = pcColorMat->ambientColor.getValues(0);
96
mat.ambientColor.setPackedValue(cols[0].getPackedValue());
98
if (pcColorMat->diffuseColor.getNum() == 1) {
99
cols = pcColorMat->diffuseColor.getValues(0);
100
mat.diffuseColor.setPackedValue(cols[0].getPackedValue());
102
if (pcColorMat->emissiveColor.getNum() == 1) {
103
cols = pcColorMat->emissiveColor.getValues(0);
104
mat.emissiveColor.setPackedValue(cols[0].getPackedValue());
106
if (pcColorMat->specularColor.getNum() == 1) {
107
cols = pcColorMat->specularColor.getValues(0);
108
mat.specularColor.setPackedValue(cols[0].getPackedValue());
110
if (pcColorMat->shininess.getNum() == 1) {
111
const float* shiny = pcColorMat->shininess.getValues(0);
112
mat.shininess = shiny[0];
114
if (pcColorMat->transparency.getNum() == 1) {
115
const float* trans = pcColorMat->transparency.getValues(0);
116
mat.transparency = trans[0];
119
ADD_PROPERTY(TextureMaterial, (mat));
120
SelectionStyle.setValue(1); // BBOX
123
ViewProviderMeshCurvature::~ViewProviderMeshCurvature()
125
pcColorRoot->unref();
127
pcColorBar->Detach(this);
132
void ViewProviderMeshCurvature::onChanged(const App::Property* prop)
134
if (prop == &TextureMaterial) {
135
const App::Material& Mat = TextureMaterial.getValue();
136
pcColorMat->ambientColor.setValue(Mat.ambientColor.r,
139
pcColorMat->specularColor.setValue(Mat.specularColor.r,
141
Mat.specularColor.b);
142
pcColorMat->emissiveColor.setValue(Mat.emissiveColor.r,
144
Mat.emissiveColor.b);
145
pcColorMat->shininess.setValue(Mat.shininess);
146
pcColorMat->transparency.setValue(Mat.transparency);
149
ViewProviderDocumentObject::onChanged(prop);
152
void ViewProviderMeshCurvature::hide()
155
pcColorStyle->style = SoDrawStyle::INVISIBLE;
158
void ViewProviderMeshCurvature::show()
161
pcColorStyle->style = SoDrawStyle::FILLED;
164
void ViewProviderMeshCurvature::init(const Mesh::PropertyCurvatureList* pCurvInfo)
166
std::vector<float> aMinValues, aMaxValues;
167
const std::vector<Mesh::CurvatureInfo>& fCurvInfo = pCurvInfo->getValues();
168
aMinValues.reserve(fCurvInfo.size());
169
aMaxValues.reserve(fCurvInfo.size());
171
for (const auto& jt : fCurvInfo) {
172
aMinValues.push_back(jt.fMinCurvature);
173
aMaxValues.push_back(jt.fMaxCurvature);
176
if (aMinValues.empty() || aMaxValues.empty()) {
177
return; // no values inside
180
float fMin = *std::min_element(aMinValues.begin(), aMinValues.end());
181
float fMax = *std::max_element(aMinValues.begin(), aMinValues.end());
183
// histogram over all values
184
std::map<int, int> aHistogram;
185
for (float aMinValue : aMinValues) {
186
int grp = (int)(10.0F * (aMinValue - fMin) / (fMax - fMin));
191
for (const auto& mIt : aHistogram) {
192
if ((float)mIt.second / (float)aMinValues.size() > 0.15F) {
193
fRMin = mIt.first * (fMax - fMin) / 10.0F + fMin;
198
fMin = *std::min_element(aMaxValues.begin(), aMaxValues.end());
199
fMax = *std::max_element(aMaxValues.begin(), aMaxValues.end());
201
// histogram over all values
203
for (float aMaxValue : aMaxValues) {
204
int grp = (int)(10.0F * (aMaxValue - fMin) / (fMax - fMin));
209
for (std::map<int, int>::reverse_iterator rIt2 = aHistogram.rbegin(); rIt2 != aHistogram.rend();
211
if ((float)rIt2->second / (float)aMaxValues.size() > 0.15F) {
212
fRMax = rIt2->first * (fMax - fMin) / 10.0F + fMin;
217
float fAbs = std::max<float>(fabs(fRMin), fabs(fRMax));
222
pcColorBar->setRange(fMin, fMax, 3);
225
void ViewProviderMeshCurvature::slotChangedObject(const App::DocumentObject& Obj,
226
const App::Property& Prop)
228
// we get this for any object for that a property has changed. Thus, we must regard that object
229
// which is linked by our link property
230
App::DocumentObject* object = static_cast<Mesh::Curvature*>(pcObject)->Source.getValue();
231
if (object == &Obj) {
232
const Mesh::PropertyMeshKernel& mesh = static_cast<Mesh::Feature*>(object)->Mesh;
233
if ((&mesh) == (&Prop)) {
234
const Mesh::MeshObject& kernel = mesh.getValue();
235
pcColorMat->diffuseColor.setNum((int)kernel.countPoints());
236
pcColorMat->transparency.setNum((int)kernel.countPoints());
237
static_cast<Mesh::Curvature*>(pcObject)
238
->Source.touch(); // make sure to recompute the feature
243
void ViewProviderMeshCurvature::attach(App::DocumentObject* pcFeat)
245
// creates the standard viewing modes
246
inherited::attach(pcFeat);
247
attachDocument(pcFeat->getDocument());
249
SoShapeHints* flathints = new SoShapeHints;
250
flathints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
251
flathints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
253
SoGroup* pcColorShadedRoot = new SoGroup();
254
pcColorShadedRoot->addChild(flathints);
257
SoDrawStyle* pcFlatStyle = new SoDrawStyle();
258
pcFlatStyle->style = SoDrawStyle::FILLED;
259
pcColorShadedRoot->addChild(pcFlatStyle);
261
SoMaterialBinding* pcMatBinding = new SoMaterialBinding;
262
pcMatBinding->value = SoMaterialBinding::PER_VERTEX_INDEXED;
263
pcColorShadedRoot->addChild(pcColorMat);
264
pcColorShadedRoot->addChild(pcMatBinding);
265
pcColorShadedRoot->addChild(pcLinkRoot);
267
addDisplayMaskMode(pcColorShadedRoot, "ColorShaded");
269
// Check for an already existing color bar
270
Gui::SoFCColorBar* pcBar =
271
((Gui::SoFCColorBar*)findFrontRootOfType(Gui::SoFCColorBar::getClassTypeId()));
273
float fMin = pcColorBar->getMinValue();
274
float fMax = pcColorBar->getMaxValue();
276
// Attach to the foreign color bar and delete our own bar
279
pcBar->setRange(fMin, fMax, 3);
281
pcColorBar->Detach(this);
286
pcColorRoot->addChild(pcColorBar);
289
void ViewProviderMeshCurvature::updateData(const App::Property* prop)
291
// set to the expected size
292
if (prop->isDerivedFrom<App::PropertyLink>()) {
293
Mesh::Feature* object =
294
static_cast<const App::PropertyLink*>(prop)->getValue<Mesh::Feature*>();
295
Gui::coinRemoveAllChildren(this->pcLinkRoot);
297
const Mesh::MeshObject& kernel = object->Mesh.getValue();
298
pcColorMat->diffuseColor.setNum((int)kernel.countPoints());
299
pcColorMat->transparency.setNum((int)kernel.countPoints());
301
// get the view provider of the associated mesh feature
302
App::Document* rDoc = pcObject->getDocument();
303
Gui::Document* pDoc = Gui::Application::Instance->getDocument(rDoc);
304
ViewProviderMesh* view = static_cast<ViewProviderMesh*>(pDoc->getViewProvider(object));
305
this->pcLinkRoot->addChild(view->getHighlightNode());
308
static_cast<Mesh::Feature*>(view->getObject())->Placement.getValue();
309
ViewProviderMesh::updateTransform(p, pcTransform);
312
else if (prop->is<Mesh::PropertyCurvatureList>()) {
313
const Mesh::PropertyCurvatureList* curv =
314
static_cast<const Mesh::PropertyCurvatureList*>(prop);
315
if (curv->getSize() < 3) { // invalid array
318
#if 0 // FIXME: Do not always change the range
319
init(curv); // init color bar
325
SoSeparator* ViewProviderMeshCurvature::getFrontRoot() const
330
void ViewProviderMeshCurvature::setVertexCurvatureMode(int mode)
332
using PropertyMap = std::map<std::string, App::Property*>;
333
Mesh::PropertyCurvatureList* pCurvInfo = nullptr;
335
pcObject->getPropertyMap(Map);
337
auto it = std::find_if(Map.begin(), Map.end(), [](const PropertyMap::value_type& v) {
338
Base::Type type = v.second->getTypeId();
339
return (type == Mesh::PropertyCurvatureList::getClassTypeId());
342
if (it == Map.end()) {
343
return; // cannot display this feature type due to missing curvature property
346
pCurvInfo = static_cast<Mesh::PropertyCurvatureList*>(it->second);
349
std::vector<float> fValues = pCurvInfo->getCurvature(mode);
350
pcColorMat->diffuseColor.setNum(fValues.size());
351
pcColorMat->transparency.setNum(fValues.size());
353
SbColor* diffcol = pcColorMat->diffuseColor.startEditing();
354
float* transp = pcColorMat->transparency.startEditing();
356
for (auto const& value : fValues | boost::adaptors::indexed(0)) {
357
App::Color c = pcColorBar->getColor(value.value());
358
diffcol[value.index()].setValue(c.r, c.g, c.b);
359
transp[value.index()] = c.a;
362
pcColorMat->diffuseColor.finishEditing();
363
pcColorMat->transparency.finishEditing();
365
// In order to apply the transparency changes the IndexFaceSet node must be touched
369
void ViewProviderMeshCurvature::touchShapeNode()
371
SoSearchAction searchAction;
372
searchAction.setType(SoIndexedFaceSet::getClassTypeId());
373
searchAction.setInterest(SoSearchAction::FIRST);
374
searchAction.apply(pcLinkRoot);
375
SoPath* selectionPath = searchAction.getPath();
377
selectionPath->getTail()->touch();
381
QIcon ViewProviderMeshCurvature::getIcon() const
383
static QPixmap px = Gui::BitmapFactory().pixmap(":/icons/Mesh_Tree_Curvature_Plot.svg");
387
void ViewProviderMeshCurvature::setDisplayMode(const char* ModeName)
389
if (strcmp("Mean curvature", ModeName) == 0) {
390
setVertexCurvatureMode(Mesh::PropertyCurvatureList::MeanCurvature);
391
setDisplayMaskMode("ColorShaded");
393
else if (strcmp("Gaussian curvature", ModeName) == 0) {
394
setVertexCurvatureMode(Mesh::PropertyCurvatureList::GaussCurvature);
395
setDisplayMaskMode("ColorShaded");
397
else if (strcmp("Maximum curvature", ModeName) == 0) {
398
setVertexCurvatureMode(Mesh::PropertyCurvatureList::MaxCurvature);
399
setDisplayMaskMode("ColorShaded");
401
else if (strcmp("Minimum curvature", ModeName) == 0) {
402
setVertexCurvatureMode(Mesh::PropertyCurvatureList::MinCurvature);
403
setDisplayMaskMode("ColorShaded");
405
else if (strcmp("Absolute curvature", ModeName) == 0) {
406
setVertexCurvatureMode(Mesh::PropertyCurvatureList::AbsCurvature);
407
setDisplayMaskMode("ColorShaded");
410
inherited::setDisplayMode(ModeName);
413
const char* ViewProviderMeshCurvature::getDefaultDisplayMode() const
415
return "Absolute curvature";
418
std::vector<std::string> ViewProviderMeshCurvature::getDisplayModes() const
420
std::vector<std::string> StrList = inherited::getDisplayModes();
423
StrList.emplace_back("Absolute curvature");
424
StrList.emplace_back("Mean curvature");
425
StrList.emplace_back("Gaussian curvature");
426
StrList.emplace_back("Maximum curvature");
427
StrList.emplace_back("Minimum curvature");
432
void ViewProviderMeshCurvature::OnChange(Base::Subject<int>& /*rCaller*/, int /*rcReason*/)
443
Annotation(Gui::ViewProviderDocumentObject* vp,
453
static void run(void* data, SoSensor* sensor)
455
Annotation* self = static_cast<Annotation*>(data);
463
App::Document* doc = vp->getObject()->getDocument();
465
std::vector<App::DocumentObject*> groups =
466
doc->getObjectsOfType(App::DocumentObjectGroup::getClassTypeId());
467
App::DocumentObjectGroup* group = nullptr;
468
std::string internalname = "CurvatureGroup";
469
for (const auto& it : groups) {
470
if (internalname == it->getNameInDocument()) {
471
group = static_cast<App::DocumentObjectGroup*>(it);
476
group = static_cast<App::DocumentObjectGroup*>(
477
doc->addObject("App::DocumentObjectGroup", internalname.c_str()));
480
App::AnnotationLabel* anno = static_cast<App::AnnotationLabel*>(
481
group->addObject("App::AnnotationLabel", internalname.c_str()));
482
QStringList lines = s.split(QLatin1String("\n"));
483
std::vector<std::string> text;
484
for (const auto& line : lines) {
485
text.emplace_back((const char*)line.toLatin1());
487
anno->LabelText.setValues(text);
488
std::stringstream str;
489
str << "Curvature info (" << group->Group.getSize() << ")";
490
anno->Label.setValue(str.str());
491
anno->BasePosition.setValue(p[0], p[1], p[2]);
492
anno->TextPosition.setValue(n[0], n[1], n[2]);
496
Gui::ViewProviderDocumentObject* vp;
502
} // namespace MeshGui
504
void ViewProviderMeshCurvature::curvatureInfoCallback(void* ud, SoEventCallback* n)
506
Gui::View3DInventorViewer* view = static_cast<Gui::View3DInventorViewer*>(n->getUserData());
507
const SoEvent* ev = n->getEvent();
508
if (ev->getTypeId() == SoMouseButtonEvent::getClassTypeId()) {
509
const SoMouseButtonEvent* mbe = static_cast<const SoMouseButtonEvent*>(ev);
511
// Mark all incoming mouse button events as handled, especially, to deactivate the selection
513
n->getAction()->setHandled();
514
if (mbe->getButton() == SoMouseButtonEvent::BUTTON2
515
&& mbe->getState() == SoButtonEvent::UP) {
519
QAction* fl = menu.addAction(QObject::tr("Annotation"));
520
fl->setCheckable(true);
521
fl->setChecked(addflag);
522
QAction* cl = menu.addAction(QObject::tr("Leave info mode"));
523
QAction* id = menu.exec(QCursor::pos());
525
addflag = fl->isChecked();
528
view->setEditing(false);
529
view->getWidget()->setCursor(QCursor(Qt::ArrowCursor));
530
view->setRedirectToSceneGraph(false);
531
view->setSelectionEnabled(true);
532
view->removeEventCallback(SoEvent::getClassTypeId(), curvatureInfoCallback, ud);
535
else if (mbe->getButton() == SoMouseButtonEvent::BUTTON1
536
&& mbe->getState() == SoButtonEvent::UP) {
537
const SoPickedPoint* point = n->getPickedPoint();
539
Base::Console().Message("No facet picked.\n");
545
// By specifying the indexed mesh node 'pcFaceSet' we make sure that the picked point is
546
// really from the mesh we render and not from any other geometry
547
Gui::ViewProvider* vp = view->getViewProviderByPathFromTail(point->getPath());
548
if (!vp || !vp->isDerivedFrom<ViewProviderMeshCurvature>()) {
551
ViewProviderMeshCurvature* self = static_cast<ViewProviderMeshCurvature*>(vp);
552
const SoDetail* detail = point->getDetail(point->getPath()->getTail());
553
if (detail && detail->getTypeId() == SoFaceDetail::getClassTypeId()) {
554
const SoFaceDetail* facedetail = static_cast<const SoFaceDetail*>(detail);
555
// get the curvature info of the three points of the picked facet
556
int index1 = facedetail->getPoint(0)->getCoordinateIndex();
557
int index2 = facedetail->getPoint(1)->getCoordinateIndex();
558
int index3 = facedetail->getPoint(2)->getCoordinateIndex();
559
std::string info = self->curvatureInfo(true, index1, index2, index3);
560
QString text = QString::fromLatin1(info.c_str());
562
SbVec3f pt = point->getPoint();
563
SbVec3f nl = point->getNormal();
564
Annotation* anno = new Annotation(self, text, pt, nl);
565
SoIdleSensor* sensor = new SoIdleSensor(Annotation::run, anno);
569
Gui::ToolTip::showText(QCursor::pos(), text);
574
else if (ev->getTypeId().isDerivedFrom(SoLocation2Event::getClassTypeId())) {
575
const SoPickedPoint* point = n->getPickedPoint();
581
// By specifying the indexed mesh node 'pcFaceSet' we make sure that the picked point is
582
// really from the mesh we render and not from any other geometry
583
Gui::ViewProvider* vp = view->getViewProviderByPathFromTail(point->getPath());
584
if (!vp || !vp->isDerivedFrom<ViewProviderMeshCurvature>()) {
587
ViewProviderMeshCurvature* that = static_cast<ViewProviderMeshCurvature*>(vp);
588
const SoDetail* detail = point->getDetail(point->getPath()->getTail());
589
if (detail && detail->getTypeId() == SoFaceDetail::getClassTypeId()) {
590
const SoFaceDetail* facedetail = static_cast<const SoFaceDetail*>(detail);
591
// get the curvature info of the three points of the picked facet
592
int index1 = facedetail->getPoint(0)->getCoordinateIndex();
593
int index2 = facedetail->getPoint(1)->getCoordinateIndex();
594
int index3 = facedetail->getPoint(2)->getCoordinateIndex();
595
std::string info = that->curvatureInfo(false, index1, index2, index3);
596
Gui::getMainWindow()->setPaneText(1, QString::fromLatin1(info.c_str()));
602
ViewProviderMeshCurvature::curvatureInfo(bool detail, int index1, int index2, int index3) const
604
// get the curvature info of the three points of the picked facet
605
App::Property* prop = pcObject->getPropertyByName("CurvInfo");
606
std::stringstream str;
607
if (prop && prop->is<Mesh::PropertyCurvatureList>()) {
608
Mesh::PropertyCurvatureList* curv = static_cast<Mesh::PropertyCurvatureList*>(prop);
609
const Mesh::CurvatureInfo& cVal1 = (*curv)[index1];
610
const Mesh::CurvatureInfo& cVal2 = (*curv)[index2];
611
const Mesh::CurvatureInfo& cVal3 = (*curv)[index3];
617
std::string mode = getActiveDisplayMode();
618
if (mode == "Minimum curvature") {
619
fVal1 = cVal1.fMinCurvature;
620
fVal2 = cVal2.fMinCurvature;
621
fVal3 = cVal3.fMinCurvature;
623
else if (mode == "Maximum curvature") {
624
fVal1 = cVal1.fMaxCurvature;
625
fVal2 = cVal2.fMaxCurvature;
626
fVal3 = cVal3.fMaxCurvature;
628
else if (mode == "Gaussian curvature") {
629
fVal1 = cVal1.fMaxCurvature * cVal1.fMinCurvature;
630
fVal2 = cVal2.fMaxCurvature * cVal2.fMinCurvature;
631
fVal3 = cVal3.fMaxCurvature * cVal3.fMinCurvature;
633
else if (mode == "Mean curvature") {
634
fVal1 = 0.5F * (cVal1.fMaxCurvature + cVal1.fMinCurvature);
635
fVal2 = 0.5F * (cVal2.fMaxCurvature + cVal2.fMinCurvature);
636
fVal3 = 0.5F * (cVal3.fMaxCurvature + cVal3.fMinCurvature);
638
else if (mode == "Absolute curvature") {
639
fVal1 = fabs(cVal1.fMaxCurvature) > fabs(cVal1.fMinCurvature) ? cVal1.fMaxCurvature
640
: cVal1.fMinCurvature;
641
fVal2 = fabs(cVal2.fMaxCurvature) > fabs(cVal2.fMinCurvature) ? cVal2.fMaxCurvature
642
: cVal2.fMinCurvature;
643
fVal3 = fabs(cVal3.fMaxCurvature) > fabs(cVal3.fMinCurvature) ? cVal3.fMaxCurvature
644
: cVal3.fMinCurvature;
652
str << mode << ": <" << fVal1 << ", " << fVal2 << ", " << fVal3 << ">";
655
str.setf(std::ios::fixed | std::ios::showpoint);
657
str << mode << std::endl
658
<< "v1: " << std::setw(5) << fVal1 << std::endl
659
<< "v2: " << std::setw(5) << fVal2 << std::endl
660
<< "v3: " << std::setw(5) << fVal3;
664
str << "No curvature mode set";