source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_shape.cpp@ 87d6bd

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 87d6bd was 691318, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: GLMoleculeObject_shape did tesselate with fixed radius.

  • this radius is too small for large shapes (e.g. sphere with radius 20).
  • we now approximate the radius estimated from the surface area of the shape.
  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[afd3d2]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * GLMoleculeObject_shape.cpp
25 *
26 * Created on: Aug 3, 2012
27 * Author: ankele
28 */
29
30
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "GLMoleculeObject_shape.hpp"
38
39#include <Qt3D/qglview.h>
40#include <Qt3D/qglscenenode.h>
41#include <Qt3D/qglpainter.h>
42#include <Qt3D/qglmaterial.h>
43#include <Qt3D/qglbuilder.h>
44#include <Qt3D/qglsphere.h>
45
46#include "CodePatterns/MemDebug.hpp"
47
48#include "CodePatterns/Assert.hpp"
49#include "CodePatterns/Log.hpp"
50
51#include "Shapes/Shape.hpp"
52#include "LinearAlgebra/Vector.hpp"
53#include "LinkedCell/PointCloudAdaptor.hpp"
54#include "LinkedCell/linkedcell.hpp"
55#include "Tesselation/tesselation.hpp"
56#include "Tesselation/BoundaryLineSet.hpp"
57#include "Tesselation/BoundaryTriangleSet.hpp"
58#include "Tesselation/CandidateForTesselation.hpp"
59#include "Atom/TesselPoint.hpp"
60
61typedef QGLSceneNode *MeshList[GLMoleculeObject::DETAILTYPES_MAX];
62
[85c36d]63QGLSceneNode *createShapeMesh(Shape &shape, QObject *parent)
[afd3d2]64{
[691318]65 size_t NumPointsonSurface = 200;
66 // guess the surface area which divided by 200 determines rolling sphere's size
67 const double surfacearea = shape.getSurfaceArea();
68 const size_t EstimatedNumberTriangles = NumPointsonSurface*1;
69 const double MeanAreaPerTriangle = surfacearea/((double)EstimatedNumberTriangles);
70 // assume two triangles to form a square and take its diameter as the radius
71 double minradius = sqrt(2. * 2.*MeanAreaPerTriangle);
72 LOG(3, "DEBUG: Surface area is " << surfacearea << ", minradius is " << minradius << ".");
73 if (minradius < 1.)
74 minradius = 1.;
75 LOG(2, "DEBUG: Tesselating shape " << shape.getName() << " with sphere of radius " << minradius << ".");
76
[afd3d2]77 // Create some points on our shape.
[691318]78 std::vector<Vector> points = shape.getHomogeneousPointsOnSurface(NumPointsonSurface);
[afd3d2]79
80 // Fill the points into a tesselate-able container.
81 TesselPointSTLList Corners;
[691318]82 for (size_t i=0;i<points.size();++i){
[afd3d2]83 TesselPoint *Walker = new TesselPoint;
84 Walker->setPosition(points[i]);
85 Walker->setName(toString(i));
86 Walker->setNr(i);
87 Corners.push_back(Walker);
88 }
89
90 // Fill the points into a Qt geometry.
91 QGeometryData geo;
[691318]92 for(size_t i=0;i<points.size();++i){
[afd3d2]93 // add data to the primitive
94 geo.appendVertex(QVector3D(points[i][0], points[i][1], points[i][2]));
95 Vector n = shape.getNormal(points[i]);
96 geo.appendNormal(QVector3D(n[0], n[1], n[2]));
97 geo.appendColor(QColor(1, 1, 1, 1));
98 geo.appendTexCoord(QVector2D(0, 0));
99 }
100
101 // Tesselate the points.
102 Tesselation *T = new Tesselation;
103 PointCloudAdaptor<TesselPointSTLList> cloud(&Corners, "TesselPointSTLList");
[691318]104 (*T)(cloud, minradius);
[afd3d2]105
106 // Fill the tesselated triangles into the geometry.
107 for (TriangleMap::const_iterator runner = T->TrianglesOnBoundary.begin(); runner != T->TrianglesOnBoundary.end(); runner++) {
[b3b228]108 int v[3];
[691318]109 for (size_t i=0; i<3; ++i)
[b3b228]110 v[i] = runner->second->endpoints[i]->node->getNr();
111
112 // Sort the vertices so the triangle is clockwise (relative to the normal vector).
113 Vector cross = points[v[1]] - points[v[0]];
114 cross.VectorProduct(points[v[2]] - points[v[0]]);
[66f712]115 if (cross.ScalarProduct(shape.getNormal(points[v[0]]) + shape.getNormal(points[v[1]]) + shape.getNormal(points[v[2]])) > 0)
[b3b228]116 geo.appendIndices(v[0], v[1], v[2]);
117 else
118 geo.appendIndices(v[0], v[2], v[1]);
[afd3d2]119 }
120
121 // Build a mesh from the geometry.
122 QGLBuilder builder;
123 builder.addTriangles(geo);
124 QGLSceneNode *mesh = builder.finalizedSceneNode();
125 return mesh;
126}
127
[85c36d]128GLMoleculeObject_shape::GLMoleculeObject_shape(Shape &shape, QObject *parent) :
[afd3d2]129 GLMoleculeObject(createShapeMesh(shape, parent), parent),
130 m_shape(shape)
131{
132 // Create the material.
133 QGLMaterial *material = new QGLMaterial(NULL);
134 material->setAmbientColor( QColor(50, 60, 100, 255) );
135 material->setDiffuseColor( QColor(150, 160, 200, 180) );
136 material->setSpecularColor( QColor(60, 60, 60) );
137 material->setShininess( 128 );
138 setMaterial(material);
[85c36d]139
140 m_enabled = false;
[afd3d2]141}
142
143GLMoleculeObject_shape::~GLMoleculeObject_shape()
144{
145}
146
[85c36d]147void GLMoleculeObject_shape::enable(bool enabled)
148{
149 m_enabled = enabled;
150}
151
[afd3d2]152
153void GLMoleculeObject_shape::draw(QGLPainter *painter, const QVector4D &cameraPlane)
154{
[85c36d]155 if (!m_enabled)
156 return;
[afd3d2]157 painter->modelViewMatrix().push();
158
159 painter->setColor(m_material->diffuseColor());
160 painter->setFaceMaterial(QGL::AllFaces, m_material);
161
162 // Draw the transparent cube.
[b3b228]163 painter->setStandardEffect(QGL::LitMaterial);
[afd3d2]164 glCullFace(GL_BACK);
[b3b228]165 glEnable(GL_CULL_FACE);
[afd3d2]166 glEnable(GL_BLEND);
167 glDepthMask(0);
[b3b228]168 glDisable(GL_DEPTH_TEST);
[afd3d2]169 m_mesh[0]->draw(painter);
[b3b228]170 glEnable(GL_DEPTH_TEST);
[afd3d2]171 glDepthMask(1);
172 glDisable(GL_BLEND);
[b3b228]173 glDisable(GL_CULL_FACE);
[afd3d2]174
175
176 painter->modelViewMatrix().pop();
177}
Note: See TracBrowser for help on using the repository browser.