source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_shape.cpp@ 426c84

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 426c84 was 27888f, checked in by Frederik Heber <heber@…>, 12 years ago

Using Normals from tesselated surface instead of from Shapes.

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