From fe9839c3b3a793e7f09c7c4a2b480447d56bf810 Mon Sep 17 00:00:00 2001 From: Maire Nicolas Date: Fri, 5 Aug 2011 11:37:49 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20des=20presets=20dans=20l'?= =?UTF-8?q?interface=20Qt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apps/Examples/clipping.cpp | 140 ++++++- Apps/Examples/clipping.h | 5 + Apps/Examples/clipping.ui | 662 ++++++++++++++++++-------------- include/Utils/clippingPresets.h | 4 +- src/Utils/clippingPresets.cpp | 60 +-- src/Utils/clippingShader.cpp | 32 +- 6 files changed, 567 insertions(+), 336 deletions(-) diff --git a/Apps/Examples/clipping.cpp b/Apps/Examples/clipping.cpp index 4f30657d..740502d7 100644 --- a/Apps/Examples/clipping.cpp +++ b/Apps/Examples/clipping.cpp @@ -25,6 +25,36 @@ #include "clipping.h" #include "Utils/static_assert.h" +/******************************************************************************* + * MISCELLANOUS + *******************************************************************************/ + +void computeBasisFromVector(const Geom::Vec3f& vec1, Geom::Vec3f& vec2, Geom::Vec3f& vec3) +{ + const float epsilon = 0.0001f; + + // Check if the given vector length is acceptable + if (vec1.norm() < epsilon) + return; + + // First take a non colinear second vector to cross product with vec1 + // (by default (0.0, 0.0, 1.0) + Geom::Vec3f tempVec (0.0, 0.0, 1.0); + + // Construct second vector, check other vectors non colinearity at the same time + vec2 = vec1 ^ tempVec; + float sinAngle = vec2.norm() / (vec1.norm() + tempVec.norm()); + if (sinAngle < epsilon) // f:x->sin(x) ~ f:x->x when x ~ 0 + { + tempVec = Geom::Vec3f (1.0, 0.0, 0.0); + vec2 = vec1 ^ tempVec; + } + + // Get third vector + vec3 = vec1 ^ vec2; + +} + /******************************************************************************* * SLOTS *******************************************************************************/ @@ -77,7 +107,7 @@ void Clipping::slot_explodTopoPhi3(double c) void Clipping::slot_pushButton_addPlane() { // Create clipping and pickable objects - int newPlaneId = m_shader->addClipPlane(); + unsigned int newPlaneId = m_shader->addClipPlane(); Utils::Pickable* pickable = new Utils::Pickable(m_planeDrawable, newPlaneId); m_pickablePlanes.push_back(pickable); @@ -115,7 +145,7 @@ void Clipping::slot_pushButton_changePlanesColor() void Clipping::slot_pushButton_addSphere() { // Create clipping and pickable objects - int newSphereId = m_shader->addClipSphere(); + unsigned int newSphereId = m_shader->addClipSphere(); Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, newSphereId); m_pickableSpheres.push_back(pickable); @@ -227,6 +257,106 @@ void Clipping::slot_pushButton_deleteSelectedObject() } } +void Clipping::slot_pushButton_applyClippingPreset() +{ + // Create and apply preset + Utils::ClippingPreset *preset = NULL; + switch (dock.comboBox_ClippingPresets->currentIndex()) + { + case 0 : // Dual planes + { + using namespace CGoGN::Utils::QT; + + double centerX = (double)m_bb.center()[0]; + double centerY = (double)m_bb.center()[1]; + double centerZ = (double)m_bb.center()[2]; + double size = (double)m_bb.maxSize()*0.75; + int axis = 0; + bool facing = false; + if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", + VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", + VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", + VarDbl(size - 100.0, size + 100.0, size, "Size", + VarSlider(0, 2, axis, "Axis", + VarBool(facing, "Facing" + )))))), "Preset Setup")) + preset = Utils::ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, axis, facing); + } + break; + + case 1 : // Cube + { + using namespace CGoGN::Utils::QT; + + double centerX = (double)m_bb.center()[0]; + double centerY = (double)m_bb.center()[1]; + double centerZ = (double)m_bb.center()[2]; + double size = (double)m_bb.maxSize()*0.75; + bool facing = false; + if (inputValues(VarDbl(centerX - 100.0, centerX + 100.0, centerX, "Center X", + VarDbl(centerY - 100.0, centerY + 100.0, centerY, "Center Y", + VarDbl(centerZ - 100.0, centerZ + 100.0, centerZ, "Center Z", + VarDbl(size - 100.0, size + 100.0, size, "Size", + VarBool(facing, "Facing" + ))))), "Preset Setup")) + preset = Utils::ClippingPreset::CreateCubePreset(Geom::Vec3f((float)centerX, (float)centerY, (float)centerZ), (float)size, facing); + } + break; + } + std::vector planesIds; + std::vector spheresIds; + preset->apply(m_shader, &planesIds, &spheresIds); + delete preset; + + // Cleanup of pickables before adding new ones + m_lastPickedObject = NULL; + for (size_t i = 0; i < m_pickablePlanes.size(); i++) + delete m_pickablePlanes[i]; + m_pickablePlanes.resize(0); + for (size_t i = 0; i < m_pickableSpheres.size(); i++) + delete m_pickableSpheres[i]; + m_pickableSpheres.resize(0); + + // Add new pickable objects + for (size_t i = 0; i < planesIds.size(); i++) + { + Utils::Pickable* pickable = new Utils::Pickable(m_planeDrawable, planesIds[i]); + pickable->translate(m_shader->getClipPlaneParamsOrigin(planesIds[i])); + Geom::Vec3f vec1, vec2, vec3; + vec1 = m_shader->getClipPlaneParamsNormal(planesIds[i]); + computeBasisFromVector(vec1, vec2, vec3); + glm::mat4& transfoMat = pickable->transfo(); + for (int i = 0; i < 3; i++) + { + transfoMat[0][i] = vec2[i]; + transfoMat[1][i] = vec3[i]; + transfoMat[2][i] = vec1[i]; + } + + m_pickablePlanes.push_back(pickable); + } + for (size_t i = 0; i < spheresIds.size(); i++) + { + Utils::Pickable* pickable = new Utils::Pickable(m_sphereDrawable, spheresIds[i]); + pickable->translate(m_shader->getClipSphereParamsCenter(spheresIds[i])); + pickable->scale(m_shader->getClipSphereParamsRadius(spheresIds[i])); + } + + // Update shader sources edits + dock.vertexEdit->setPlainText(QString(m_shader->getVertexShaderSrc())); + dock.fragmentEdit->setPlainText(QString(m_shader->getFragmentShaderSrc())); + + // Update clipping parameters in interface + Utils::ClippingShader::clippingMode clipMode = m_shader->getClipMode(); + if (clipMode == Utils::ClippingShader::CLIPPING_MODE_AND) + dock.radioButton_ClippingModeAnd->setChecked(true); + else if (clipMode == Utils::ClippingShader::CLIPPING_MODE_OR) + dock.radioButton_ClippingModeOr->setChecked(true); + + + updateGLMatrices(); +} + void Clipping::button_compile() { QString st1 = dynamic_cast(dockWidget())->vertexEdit->toPlainText(); @@ -317,6 +447,12 @@ void Clipping::initGUI() else if (colorAttMode == Utils::ClippingShader::COLOR_ATTENUATION_MODE_QUADRATIC) dock.radioButton_ColorAttenuationModeQuadratic->setChecked(true); + + setCallBack(dock.PushButton_ApplyClippingPreset, SIGNAL(clicked()), SLOT(slot_pushButton_applyClippingPreset())); + + dock.comboBox_ClippingPresets->addItem("Dual Planes"); + dock.comboBox_ClippingPresets->addItem("Cube"); + } void Clipping::cb_Open() diff --git a/Apps/Examples/clipping.h b/Apps/Examples/clipping.h index 8799498c..410b77ac 100644 --- a/Apps/Examples/clipping.h +++ b/Apps/Examples/clipping.h @@ -28,6 +28,7 @@ #include #include "Utils/qtSimple.h" +#include "Utils/qtInputs.h" #include "ui_clipping.h" // inclure qtui.h juste après le ui_xxx.h @@ -51,6 +52,8 @@ #include "Utils/frameManipulator.h" +#include "Utils/clippingPresets.h" + #include "Utils/cgognStream.h" #include "Utils/drawer.h" @@ -153,6 +156,8 @@ public slots: void slot_pushButton_deleteSelectedObject(); + void slot_pushButton_applyClippingPreset(); + void button_compile(); }; diff --git a/Apps/Examples/clipping.ui b/Apps/Examples/clipping.ui index c0e4c6cb..55f95a01 100644 --- a/Apps/Examples/clipping.ui +++ b/Apps/Examples/clipping.ui @@ -6,8 +6,8 @@ 0 0 - 352 - 778 + 374 + 783 @@ -32,314 +32,388 @@ - - - - - Vertices - - - - - - - Lines - - - true - - - - - - - Faces - - - true - - - - - - - true - - - Topo - - - true - - - false - - - - - - - - 1.000000000000000 - - - 0.100000000000000 - - - 0.900000000000000 - - - - - - - 1.000000000000000 - - - 0.100000000000000 - - - 0.900000000000000 - - - - - - - Faces - - - - - - - Volumes - - - - - - - - - - 1.000000000000000 - - - 0.100000000000000 - - - 0.900000000000000 - - - - - - - Edges - - - - - - - - - - + + + 0 + + + + Object + + - - - true - - - Clipping Planes - - - - - - Add - - - - - - - - - Res. - - - - - - - 200 - - - + + + + + Vertices + + + + + + + Lines + + + true + + + + + + + Faces + + + true + + + + + + + true + + + Topo + + + true + + + false + + - - - Color - - - - - - - Change Color - - + + + + + 1.000000000000000 + + + 0.100000000000000 + + + 0.900000000000000 + + + + + + + 1.000000000000000 + + + 0.100000000000000 + + + 0.900000000000000 + + + + + + + Faces + + + + + + + Volumes + + + + + + + + + + 1.000000000000000 + + + 0.100000000000000 + + + 0.900000000000000 + + + + + + + Edges + + + + - - - + + + - - - - - - - true + + + Qt::Vertical - - Clipping Spheres + + + 20 + 40 + - - - - - Add - - - - - - - - - Res. - - - - - - - 200 - - - - - - - Color - - - - - - - Change Color - - - - - - - + - - - + + + + Clipping + + - - - Clipping Mode - - - - - - AND - - - - - - - OR - - - - - + + + + + + + Clipping Mode + + + + + + AND + + + + + + + OR + + + + + + + + + + + + + + true + + + Clipping Spheres + + + + + + Add + + + + + + + + + Res. + + + + + + + 200 + + + + + + + Color + + + + + + + Change Color + + + + + + + + + + + + + + + + true + + + Clipping Planes + + + + + + Add + + + + + + + + + Res. + + + + + + + 200 + + + + + + + Color + + + + + + + Change Color + + + + + + + + + + + + + + + + Clipping Presets + + + + + + + + + Apply Preset + + + + + + + + + + + + + + Color Attenuation + + + + + + Color Att. Factor : + + + true + + + + + + + 4 + + + 1.000000000000000 + + + + + + + Color Att. Mode : + + + true + + + + + + + Linear + + + + + + + Quadratic + + + + + + + + + - - - - - - - Color Attenuation + + + Qt::Vertical - - - - - Color Att. Factor : - - - true - - - - - - - 4 - - - 1.000000000000000 - - - - - - - Color Att. Mode : - - - true - - - - - - - Linear - - - - - - - Quadratic - - - - - + + + 20 + 40 + + + - - + + diff --git a/include/Utils/clippingPresets.h b/include/Utils/clippingPresets.h index d710bf6f..ae086fff 100644 --- a/include/Utils/clippingPresets.h +++ b/include/Utils/clippingPresets.h @@ -57,7 +57,7 @@ public : * @param axis axis on which planes are aligned (0 for x, 1 for y, 2 for z) * @param facing true means having facing planes */ - static ClippingPreset* CreateDualPlanesPreset(Geom::Vec3f center, float distance, int axis, bool facing); + static ClippingPreset* CreateDualPlanesPreset(Geom::Vec3f center, float size, int axis, bool facing); /** * public static constructor @@ -65,7 +65,7 @@ public : * @param distance distance between planes * @param facing true means having facing planes */ - static ClippingPreset* CreateCubePreset(Geom::Vec3f center, float distance, bool facing); + static ClippingPreset* CreateCubePreset(Geom::Vec3f center, float size, bool facing); private : diff --git a/src/Utils/clippingPresets.cpp b/src/Utils/clippingPresets.cpp index ef160257..3384c5d7 100644 --- a/src/Utils/clippingPresets.cpp +++ b/src/Utils/clippingPresets.cpp @@ -45,50 +45,66 @@ ClippingPreset* ClippingPreset::CreateEmptyPreset() return preset; } -ClippingPreset* ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f center, float distance, int axis, bool facing) +ClippingPreset* ClippingPreset::CreateDualPlanesPreset(Geom::Vec3f center, float size, int axis, bool facing) { ClippingPreset *preset = new ClippingPreset; // Axis on which planes will be aligned if ((axis < 0) || (axis > 2)) axis = 0; - Geom::Vec3f positDir (0.0, 0.0, 0.0); - positDir[axis] = 1.0; - Geom::Vec3f negDir (0.0, 0.0, 0.0); - negDir[axis] = -1.0; + Geom::Vec3f positDir (0.0f, 0.0f, 0.0f); + positDir[axis] = 1.0f; + Geom::Vec3f negDir (0.0f, 0.0f, 0.0f); + negDir[axis] = -1.0f; + + // Facing of planes + float side = 1.0; + if (facing) + side = -1.0; // Add planes to preset - preset->addClipPlane(positDir, center + positDir*(distance / 2.0)); - preset->addClipPlane(negDir, center + negDir*(distance / 2.0)); + preset->addClipPlane(positDir, center + positDir*(size / 2.0f)*(side)); + preset->addClipPlane(negDir, center + negDir*(size / 2.0f)*(side)); // Set clipping mode - preset->setClippingMode(ClippingShader::CLIPPING_MODE_AND); + ClippingShader::clippingMode clipMode = ClippingShader::CLIPPING_MODE_AND; + if (facing) + clipMode = ClippingShader::CLIPPING_MODE_OR; + preset->setClippingMode(clipMode); return preset; } -ClippingPreset* ClippingPreset::CreateCubePreset(Geom::Vec3f center, float distance, bool facing) +ClippingPreset* ClippingPreset::CreateCubePreset(Geom::Vec3f center, float size, bool facing) { ClippingPreset *preset = new ClippingPreset; // Directions - Geom::Vec3f xAxisPos = Geom::Vec3f (1.0, 0.0, 0.0); - Geom::Vec3f xAxisNeg = Geom::Vec3f (-1.0, 0.0, 0.0); - Geom::Vec3f yAxisPos = Geom::Vec3f (0.0, 1.0, 0.0); - Geom::Vec3f yAxisNeg = Geom::Vec3f (0.0, -1.0, 0.0); - Geom::Vec3f zAxisPos = Geom::Vec3f (0.0, 0.0, 1.0); - Geom::Vec3f zAxisNeg = Geom::Vec3f (0.0, 0.0, -1.0); + Geom::Vec3f xAxisPos = Geom::Vec3f (1.0f, 0.0f, 0.0f); + Geom::Vec3f xAxisNeg = Geom::Vec3f (-1.0f, 0.0f, 0.0f); + Geom::Vec3f yAxisPos = Geom::Vec3f (0.0f, 1.0f, 0.0f); + Geom::Vec3f yAxisNeg = Geom::Vec3f (0.0f, -1.0f, 0.0f); + Geom::Vec3f zAxisPos = Geom::Vec3f (0.0f, 0.0f, 1.0f); + Geom::Vec3f zAxisNeg = Geom::Vec3f (0.0f, 0.0f, -1.0f); + + // Facing of planes + float side = 1.0; + if (facing) + side = -1.0; // Add planes to preset - preset->addClipPlane(xAxisPos, center + xAxisPos*(distance / 2.0)); - preset->addClipPlane(xAxisNeg, center + xAxisNeg*(distance / 2.0)); - preset->addClipPlane(yAxisPos, center + yAxisPos*(distance / 2.0)); - preset->addClipPlane(yAxisNeg, center + yAxisNeg*(distance / 2.0)); - preset->addClipPlane(zAxisPos, center + zAxisPos*(distance / 2.0)); - preset->addClipPlane(zAxisNeg, center + zAxisNeg*(distance / 2.0)); + preset->addClipPlane(xAxisPos, center + xAxisPos*(size / 2.0f)*(side)); + preset->addClipPlane(xAxisNeg, center + xAxisNeg*(size / 2.0f)*(side)); + preset->addClipPlane(yAxisPos, center + yAxisPos*(size / 2.0f)*(side)); + preset->addClipPlane(yAxisNeg, center + yAxisNeg*(size / 2.0f)*(side)); + preset->addClipPlane(zAxisPos, center + zAxisPos*(size / 2.0f)*(side)); + preset->addClipPlane(zAxisNeg, center + zAxisNeg*(size / 2.0f)*(side)); // Set clipping mode - preset->setClippingMode(ClippingShader::CLIPPING_MODE_AND); + ClippingShader::clippingMode clipMode = ClippingShader::CLIPPING_MODE_AND; + if (facing) + clipMode = ClippingShader::CLIPPING_MODE_OR; + preset->setClippingMode(clipMode); return preset; } diff --git a/src/Utils/clippingShader.cpp b/src/Utils/clippingShader.cpp index b017209d..ce96f401 100644 --- a/src/Utils/clippingShader.cpp +++ b/src/Utils/clippingShader.cpp @@ -45,7 +45,7 @@ ClippingShader::ClippingShader(): // Initialize default global clipping variables m_hasClippingCodeBeenInserted (false), - m_clipColorAttenuationFactor (1.0), + m_clipColorAttenuationFactor (1.0f), m_unif_clipColorAttenuationFactor (0), m_colorAttenuationMode (COLOR_ATTENUATION_MODE_LINEAR), m_clipMode (CLIPPING_MODE_AND) @@ -94,15 +94,15 @@ unsigned int ClippingShader::addClipPlane() m_clipPlanes.resize((size_t)(previousPlanesCount + 1)); if (newPlaneId >= m_clipPlanesIds.size()) m_clipPlanesIds.resize((size_t)(newPlaneId + 1)); - m_clipPlanesEquations.resize(4*(size_t)(previousPlanesCount + 1), 0.0); + m_clipPlanesEquations.resize(4*(size_t)(previousPlanesCount + 1), 0.0f); // Set new plane id m_clipPlanesIds[newPlaneId].used = true; m_clipPlanesIds[newPlaneId].index = previousPlanesCount; // Set default parameters values for the new plane - Geom::Vec3f defaultNormal (0.0, 0.0, 1.0); - Geom::Vec3f defaultOrigin (0.0, 0.0, 0.0); + Geom::Vec3f defaultNormal (0.0f, 0.0f, 1.0f); + Geom::Vec3f defaultOrigin (0.0f, 0.0f, 0.0f); setClipPlaneParamsAll(newPlaneId, defaultNormal, defaultOrigin); // Recompile shaders (automatically calls updateClippingUniforms) @@ -285,9 +285,9 @@ Geom::Vec3f ClippingShader::getClipPlaneParamsNormal(unsigned int id) { // Check if the given id is valid if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::getClipPlaneParamsFirstVec")) - return Geom::Vec3f(0.0, 0.0, 0.0); + return Geom::Vec3f(0.0f, 0.0f, 0.0f); if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::getClipPlaneParamsFirstVec")) - return Geom::Vec3f(0.0, 0.0, 0.0); + return Geom::Vec3f(0.0f, 0.0f, 0.0f); // Get the corresponding plane index int planeIndex = m_clipPlanesIds[id].index; @@ -300,9 +300,9 @@ Geom::Vec3f ClippingShader::getClipPlaneParamsOrigin(unsigned int id) { // Check if the given id is valid if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::getClipPlaneParamsOrigin")) - return Geom::Vec3f(0.0, 0.0, 0.0); + return Geom::Vec3f(0.0f, 0.0f, 0.0f); if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::getClipPlaneParamsOrigin")) - return Geom::Vec3f(0.0, 0.0, 0.0); + return Geom::Vec3f(0.0f, 0.0f, 0.0f); // Get the corresponding plane index int planeIndex = m_clipPlanesIds[id].index; @@ -388,15 +388,15 @@ unsigned int ClippingShader::addClipSphere() m_clipSpheres.resize((size_t)(previousSpheresCount + 1)); if (newSphereId >= m_clipSpheresIds.size()) m_clipSpheresIds.resize((size_t)(newSphereId + 1)); - m_clipSpheresCentersAndRadiuses.resize(4*(size_t)(previousSpheresCount + 1), 0.0); + m_clipSpheresCentersAndRadiuses.resize(4*(size_t)(previousSpheresCount + 1), 0.0f); // Set new sphere id m_clipSpheresIds[newSphereId].used = true; m_clipSpheresIds[newSphereId].index = previousSpheresCount; // Set default parameters values for the new sphere - Geom::Vec3f defaultCenter (0.0, 0.0, 0.0); - float defaultRadius = 10.0; + Geom::Vec3f defaultCenter (0.0f, 0.0f, 0.0f); + float defaultRadius = 10.0f; setClipSphereParamsAll(newSphereId, defaultCenter, defaultRadius); // Recompile shaders (automatically calls updateClippingUniforms) @@ -571,9 +571,9 @@ Geom::Vec3f ClippingShader::getClipSphereParamsCenter(unsigned int id) { // Check if the given id is valid if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::getClipSphereParamsCenter")) - return Geom::Vec3f(0.0, 0.0, 0.0); + return Geom::Vec3f(0.0f, 0.0f, 0.0f); if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::getClipSphereParamsCenter")) - return Geom::Vec3f(0.0, 0.0, 0.0); + return Geom::Vec3f(0.0f, 0.0f, 0.0f); // Get the corresponding sphere index int sphereIndex = m_clipSpheresIds[id].index; @@ -586,9 +586,9 @@ float ClippingShader::getClipSphereParamsRadius(unsigned int id) { // Check if the given id is valid if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::getClipSphereParamsRadius")) - return 0.0; + return 0.0f; if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::getClipSphereParamsRadius")) - return 0.0; + return 0.0f; // Get the corresponding sphere index int sphereIndex = m_clipSpheresIds[id].index; @@ -895,7 +895,7 @@ void ClippingShader::setClipColorAttenuationFactorRelative(float size, float fac { // Compute the relative color attenuation factor float colAttFact; - if (size != 0.0) + if (size != 0.0f) colAttFact = factor / size; else colAttFact = factor; -- GitLab