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 "LinearAlgebra/Vector.hpp"
|
---|
54 | #include "LinkedCell/PointCloudAdaptor.hpp"
|
---|
55 | #include "LinkedCell/linkedcell.hpp"
|
---|
56 | #include "Tesselation/tesselation.hpp"
|
---|
57 | #include "Tesselation/BoundaryLineSet.hpp"
|
---|
58 | #include "Tesselation/BoundaryTriangleSet.hpp"
|
---|
59 | #include "Tesselation/CandidateForTesselation.hpp"
|
---|
60 | #include "Atom/TesselPoint.hpp"
|
---|
61 |
|
---|
62 | typedef QGLSceneNode *MeshList[GLMoleculeObject::DETAILTYPES_MAX];
|
---|
63 |
|
---|
64 | QGLSceneNode *createShapeMesh(Shape &shape, QObject *parent)
|
---|
65 | {
|
---|
66 | size_t NumPointsonSurface = 200;
|
---|
67 | // guess the surface area which divided by 200 determines rolling sphere's size
|
---|
68 | const double surfacearea = shape.getSurfaceArea();
|
---|
69 | const size_t EstimatedNumberTriangles = NumPointsonSurface*1;
|
---|
70 | const double MeanAreaPerTriangle = surfacearea/((double)EstimatedNumberTriangles);
|
---|
71 | // assume two triangles to form a square and take its diameter as the radius
|
---|
72 | double minradius = sqrt(2. * 2.*MeanAreaPerTriangle);
|
---|
73 | LOG(3, "DEBUG: Surface area is " << surfacearea << ", minradius is " << minradius << ".");
|
---|
74 | if (minradius < 1.)
|
---|
75 | minradius = 1.;
|
---|
76 | LOG(2, "DEBUG: Tesselating shape " << shape.getName() << " with sphere of radius " << minradius << ".");
|
---|
77 |
|
---|
78 | // Create some points on our shape.
|
---|
79 | std::vector<Vector> points = shape.getHomogeneousPointsOnSurface(NumPointsonSurface);
|
---|
80 |
|
---|
81 | // Fill the points into a tesselate-able container.
|
---|
82 | TesselPointSTLList Corners;
|
---|
83 | for (size_t i=0;i<points.size();++i){
|
---|
84 | TesselPoint *Walker = new TesselPoint;
|
---|
85 | Walker->setPosition(points[i]);
|
---|
86 | Walker->setName(toString(i));
|
---|
87 | Walker->setNr(i);
|
---|
88 | Corners.push_back(Walker);
|
---|
89 | }
|
---|
90 |
|
---|
91 | // Fill the points into a Qt geometry.
|
---|
92 | QGeometryData geo;
|
---|
93 | for(size_t i=0;i<points.size();++i){
|
---|
94 | // add data to the primitive
|
---|
95 | geo.appendVertex(QVector3D(points[i][0], points[i][1], points[i][2]));
|
---|
96 | Vector n = shape.getNormal(points[i]);
|
---|
97 | geo.appendNormal(QVector3D(n[0], n[1], n[2]));
|
---|
98 | geo.appendColor(QColor(1, 1, 1, 1));
|
---|
99 | geo.appendTexCoord(QVector2D(0, 0));
|
---|
100 | }
|
---|
101 |
|
---|
102 | // Tesselate the points.
|
---|
103 | Tesselation *T = new Tesselation;
|
---|
104 | PointCloudAdaptor<TesselPointSTLList> cloud(&Corners, "TesselPointSTLList");
|
---|
105 | (*T)(cloud, minradius);
|
---|
106 |
|
---|
107 | // Fill the tesselated triangles into the geometry.
|
---|
108 | for (TriangleMap::const_iterator runner = T->TrianglesOnBoundary.begin(); runner != T->TrianglesOnBoundary.end(); runner++) {
|
---|
109 | int v[3];
|
---|
110 | for (size_t i=0; i<3; ++i)
|
---|
111 | v[i] = runner->second->endpoints[i]->node->getNr();
|
---|
112 |
|
---|
113 | // Sort the vertices so the triangle is clockwise (relative to the normal vector).
|
---|
114 | Vector cross = points[v[1]] - points[v[0]];
|
---|
115 | cross.VectorProduct(points[v[2]] - points[v[0]]);
|
---|
116 | if (cross.ScalarProduct(shape.getNormal(points[v[0]]) + shape.getNormal(points[v[1]]) + shape.getNormal(points[v[2]])) > 0)
|
---|
117 | geo.appendIndices(v[0], v[1], v[2]);
|
---|
118 | else
|
---|
119 | geo.appendIndices(v[0], v[2], v[1]);
|
---|
120 | }
|
---|
121 |
|
---|
122 | // Build a mesh from the geometry.
|
---|
123 | QGLBuilder builder;
|
---|
124 | builder.addTriangles(geo);
|
---|
125 | QGLSceneNode *mesh = builder.finalizedSceneNode();
|
---|
126 | return mesh;
|
---|
127 | }
|
---|
128 |
|
---|
129 | GLMoleculeObject_shape::GLMoleculeObject_shape(Shape &shape, QObject *parent) :
|
---|
130 | GLMoleculeObject(createShapeMesh(shape, parent), parent),
|
---|
131 | m_shape(shape)
|
---|
132 | {
|
---|
133 | // Create the material.
|
---|
134 | QGLMaterial *material = new QGLMaterial(NULL);
|
---|
135 | material->setAmbientColor( QColor(50, 60, 100, 255) );
|
---|
136 | material->setDiffuseColor( QColor(150, 160, 200, 180) );
|
---|
137 | material->setSpecularColor( QColor(60, 60, 60) );
|
---|
138 | material->setShininess( 128 );
|
---|
139 | setMaterial(material);
|
---|
140 |
|
---|
141 | m_enabled = false;
|
---|
142 | }
|
---|
143 |
|
---|
144 | GLMoleculeObject_shape::~GLMoleculeObject_shape()
|
---|
145 | {
|
---|
146 | }
|
---|
147 |
|
---|
148 | void GLMoleculeObject_shape::enable(bool enabled)
|
---|
149 | {
|
---|
150 | m_enabled = enabled;
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | void GLMoleculeObject_shape::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
155 | {
|
---|
156 | if (!m_enabled)
|
---|
157 | return;
|
---|
158 | painter->modelViewMatrix().push();
|
---|
159 |
|
---|
160 | painter->setColor(m_material->diffuseColor());
|
---|
161 | painter->setFaceMaterial(QGL::AllFaces, m_material);
|
---|
162 |
|
---|
163 | // Draw the transparent cube.
|
---|
164 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
165 | glCullFace(GL_BACK);
|
---|
166 | glEnable(GL_CULL_FACE);
|
---|
167 | glEnable(GL_BLEND);
|
---|
168 | glDepthMask(0);
|
---|
169 | glDisable(GL_DEPTH_TEST);
|
---|
170 | m_mesh[0]->draw(painter);
|
---|
171 | glEnable(GL_DEPTH_TEST);
|
---|
172 | glDepthMask(1);
|
---|
173 | glDisable(GL_BLEND);
|
---|
174 | glDisable(GL_CULL_FACE);
|
---|
175 |
|
---|
176 |
|
---|
177 | painter->modelViewMatrix().pop();
|
---|
178 | }
|
---|