| 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 |  | 
|---|
| 63 | typedef QGLSceneNode *MeshList[GLMoleculeObject::DETAILTYPES_MAX]; | 
|---|
| 64 |  | 
|---|
| 65 | QGLSceneNode *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 | QGeometryData geo; | 
|---|
| 83 | // we need at least three points for tesselation | 
|---|
| 84 | if (points.size() >= 3) { | 
|---|
| 85 | // Fill the points into a tesselate-able container. | 
|---|
| 86 | TesselPointSTLList Corners; | 
|---|
| 87 | for (size_t i=0;i<points.size();++i){ | 
|---|
| 88 | TesselPoint *Walker = new TesselPoint; | 
|---|
| 89 | Walker->setPosition(points[i]); | 
|---|
| 90 | Walker->setName(toString(i)); | 
|---|
| 91 | Walker->setNr(i); | 
|---|
| 92 | Corners.push_back(Walker); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | // Tesselate the points. | 
|---|
| 96 | Tesselation T; | 
|---|
| 97 | PointCloudAdaptor<TesselPointSTLList> cloud(&Corners, "TesselPointSTLList"); | 
|---|
| 98 | T(cloud, minradius); | 
|---|
| 99 |  | 
|---|
| 100 | // Fill the points into a Qt geometry. | 
|---|
| 101 | LinkedCell_deprecated LinkedList(cloud, minradius); | 
|---|
| 102 | std::vector<Vector> normals; | 
|---|
| 103 | normals.resize(points.size(), zeroVec); | 
|---|
| 104 | for(size_t i=0;i<points.size();++i){ | 
|---|
| 105 | // add data to the primitive | 
|---|
| 106 | geo.appendVertex(QVector3D(points[i][0], points[i][1], points[i][2])); | 
|---|
| 107 | if (ShapeFactory::getInstance().isSimpleShape(shape.getType())) | 
|---|
| 108 | normals[i] = shape.getNormal(points[i]); | 
|---|
| 109 | else | 
|---|
| 110 | normals[i] = T.getNormal(points[i], &LinkedList); | 
|---|
| 111 | geo.appendNormal(QVector3D(normals[i][0], normals[i][1], normals[i][2])); | 
|---|
| 112 | geo.appendColor(QColor(1, 1, 1, 1)); | 
|---|
| 113 | geo.appendTexCoord(QVector2D(0, 0)); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | // Fill the tesselated triangles into the geometry. | 
|---|
| 117 | for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin(); runner != T.TrianglesOnBoundary.end(); runner++) { | 
|---|
| 118 | int v[3]; | 
|---|
| 119 | for (size_t i=0; i<3; ++i) | 
|---|
| 120 | v[i] = runner->second->endpoints[i]->node->getNr(); | 
|---|
| 121 |  | 
|---|
| 122 | // Sort the vertices so the triangle is clockwise (relative to the normal vector). | 
|---|
| 123 | Vector cross = points[v[1]] - points[v[0]]; | 
|---|
| 124 | cross.VectorProduct(points[v[2]] - points[v[0]]); | 
|---|
| 125 | if (cross.ScalarProduct(normals[v[0]] + normals[v[1]] + normals[v[2]]) > 0) | 
|---|
| 126 | geo.appendIndices(v[0], v[1], v[2]); | 
|---|
| 127 | else | 
|---|
| 128 | geo.appendIndices(v[0], v[2], v[1]); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | // Build a mesh from the geometry. | 
|---|
| 133 | QGLBuilder builder; | 
|---|
| 134 | builder.addTriangles(geo); | 
|---|
| 135 | QGLSceneNode *mesh = builder.finalizedSceneNode(); | 
|---|
| 136 | return mesh; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | GLMoleculeObject_shape::GLMoleculeObject_shape(Shape &shape, QObject *parent) : | 
|---|
| 140 | GLMoleculeObject(createShapeMesh(shape, parent), parent), | 
|---|
| 141 | m_shape(shape) | 
|---|
| 142 | { | 
|---|
| 143 | // Create the material. | 
|---|
| 144 | QGLMaterial *material = new QGLMaterial(NULL); | 
|---|
| 145 | material->setAmbientColor( QColor(50, 60, 100, 255) ); | 
|---|
| 146 | material->setDiffuseColor( QColor(150, 160, 200, 180) ); | 
|---|
| 147 | material->setSpecularColor( QColor(60, 60, 60) ); | 
|---|
| 148 | material->setShininess( 128 ); | 
|---|
| 149 | setMaterial(material); | 
|---|
| 150 |  | 
|---|
| 151 | m_enabled = false; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | GLMoleculeObject_shape::~GLMoleculeObject_shape() | 
|---|
| 155 | { | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void GLMoleculeObject_shape::enable(bool enabled) | 
|---|
| 159 | { | 
|---|
| 160 | m_enabled = enabled; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | void GLMoleculeObject_shape::draw(QGLPainter *painter, const QVector4D &cameraPlane) | 
|---|
| 165 | { | 
|---|
| 166 | if (!m_enabled) | 
|---|
| 167 | return; | 
|---|
| 168 | painter->modelViewMatrix().push(); | 
|---|
| 169 |  | 
|---|
| 170 | painter->setColor(m_material->diffuseColor()); | 
|---|
| 171 | painter->setFaceMaterial(QGL::AllFaces, m_material); | 
|---|
| 172 |  | 
|---|
| 173 | // Draw the transparent cube. | 
|---|
| 174 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 175 | glCullFace(GL_BACK); | 
|---|
| 176 | glEnable(GL_CULL_FACE); | 
|---|
| 177 | glEnable(GL_BLEND); | 
|---|
| 178 | glDepthMask(0); | 
|---|
| 179 | glDisable(GL_DEPTH_TEST); | 
|---|
| 180 | m_mesh[0]->draw(painter); | 
|---|
| 181 | glEnable(GL_DEPTH_TEST); | 
|---|
| 182 | glDepthMask(1); | 
|---|
| 183 | glDisable(GL_BLEND); | 
|---|
| 184 | glDisable(GL_CULL_FACE); | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | painter->modelViewMatrix().pop(); | 
|---|
| 188 | } | 
|---|