| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "cartoongeometry.h" |
| 7 |
|
| 8 |
#include <cmath> |
| 9 |
|
| 10 |
namespace Avogadro::Rendering { |
| 11 |
|
| 12 |
using Core::Residue; |
| 13 |
using std::vector; |
| 14 |
|
| 15 |
const float Cartoon::ELIPSE_RATIO = 0.75f; |
| 16 |
|
| 17 |
Cartoon::Cartoon() |
| 18 |
: BSplineGeometry(false), m_minRadius(-1.0f), m_maxRadius(-1.0f) |
| 19 |
{ |
| 20 |
} |
| 21 |
|
| 22 |
Cartoon::Cartoon(float minRadius, float maxRadius) |
| 23 |
: BSplineGeometry(false), m_minRadius(minRadius), m_maxRadius(maxRadius) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
vector<ColorNormalVertex> Cartoon::computeCirclePoints(const Eigen::Affine3f& a, |
| 28 |
const Eigen::Affine3f& b, |
| 29 |
bool flat) const |
| 30 |
{ |
| 31 |
unsigned int circleResolution = flat ? 2 : 20; |
| 32 |
const float resolutionRadians = |
| 33 |
2.0f * static_cast<float>(M_PI) / static_cast<float>(circleResolution); |
| 34 |
vector<ColorNormalVertex> result; |
| 35 |
float elipseA = flat ? 0.999f : ELIPSE_RATIO; |
| 36 |
float elipseB = 1.0f - elipseA; |
| 37 |
float e = std::sqrt(1.0f - ((elipseB * elipseB) / (elipseA * elipseA))); |
| 38 |
float c = elipseA * e; |
| 39 |
for (unsigned int i = 0; i < circleResolution; ++i) { |
| 40 |
float theta = resolutionRadians * i; |
| 41 |
float r = (elipseA * (1.0f - (e * e))) / (1.0f + e * std::cos(theta)); |
| 42 |
Vector3f elipse = |
| 43 |
Vector3f(r * std::sin(theta), 0.0f, c + r * std::cos(theta)); |
| 44 |
|
| 45 |
ColorNormalVertex vert1; |
| 46 |
vert1.normal = a.linear() * elipse; |
| 47 |
vert1.vertex = a * elipse; |
| 48 |
result.push_back(vert1); |
| 49 |
|
| 50 |
ColorNormalVertex vert2; |
| 51 |
vert2.normal = b.linear() * elipse; |
| 52 |
vert2.vertex = b * elipse; |
| 53 |
result.push_back(vert2); |
| 54 |
} |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
float arrowFunction(float t) |
| 59 |
{ |
| 60 |
float result; |
| 61 |
const float maxPoint = 0.7f; |
| 62 |
if (t < maxPoint) { |
| 63 |
// normalize t using max point and scale it so that adding will be between |
| 64 |
// [minimumRadius, 1] |
| 65 |
result = t / maxPoint; |
| 66 |
} else { |
| 67 |
// starting with 1 and go decreassing |
| 68 |
t = (t - maxPoint) / (1.0f - maxPoint); |
| 69 |
result = 1.0f - t; |
| 70 |
result = result < 0.3 ? 0.3 : result; |
| 71 |
} |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
float Cartoon::computeScale(size_t index, float p, float radius) const |
| 76 |
{ |
| 77 |
if (index > m_type.size()) |
| 78 |
return radius; |
| 79 |
float t = (m_type[index].second + p) / 0.80f; |
| 80 |
t = t > 1.0f ? 1.0f : t; |
| 81 |
switch (m_type[index].first) { |
| 82 |
default: |
| 83 |
case Undefined: |
| 84 |
return radius; |
| 85 |
case Body: |
| 86 |
return m_minRadius; |
| 87 |
case Arrow: |
| 88 |
if (m_type[index].second == 0) { |
| 89 |
return (arrowFunction(1.0f - t) * m_maxRadius) + m_minRadius; |
| 90 |
} else { |
| 91 |
return 0.3 * m_maxRadius + m_minRadius; |
| 92 |
} |
| 93 |
case Head: |
| 94 |
return ((1.0f - t) * (m_maxRadius - m_minRadius)) + (1.0f * m_minRadius); |
| 95 |
case Tail: |
| 96 |
return (t * (m_maxRadius - m_minRadius)) + (1.0f * m_minRadius); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
CartoonType secondaryToCartoonType(Residue::SecondaryStructure sec) |
| 101 |
{ |
| 102 |
switch (sec) { |
| 103 |
case Residue::SecondaryStructure::betaSheet: |
| 104 |
return Arrow; |
| 105 |
case Residue::SecondaryStructure::alphaHelix: |
| 106 |
return Tail; |
| 107 |
case Residue::SecondaryStructure::helix310: |
| 108 |
return Tail; |
| 109 |
case Residue::SecondaryStructure::piHelix: |
| 110 |
return Tail; |
| 111 |
default: |
| 112 |
return Body; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void Cartoon::addPoint(const Vector3f& pos, const Vector3ub& color, |
| 117 |
size_t group, size_t id, Residue::SecondaryStructure sec) |
| 118 |
{ |
| 119 |
CartoonType ct = secondaryToCartoonType(sec); |
| 120 |
size_t idCartoon = 0; |
| 121 |
if (m_type.size() > 0) { |
| 122 |
idCartoon = ct == m_type.back().first && m_type.size() > (SKIPPED + 1) |
| 123 |
? m_type.back().second + 1 |
| 124 |
: 0; |
| 125 |
if (Tail == m_type.back().first && ct == Body) { |
| 126 |
for (size_t i = m_type.size(), j = 0; |
| 127 |
i > 0 && j < std::ceil(m_type.back().second / 2.0f); --i, ++j) { |
| 128 |
m_type[i - 1].first = Head; |
| 129 |
m_type[i - 1].second = j; |
| 130 |
} |
| 131 |
} |
| 132 |
if (ct == Arrow && m_type.back().first == Arrow) { |
| 133 |
m_type.back().second = 1; |
| 134 |
idCartoon = 0; |
| 135 |
} |
| 136 |
} |
| 137 |
m_type.emplace_back(ct, idCartoon); |
| 138 |
BSplineGeometry::addPoint(pos, color, m_minRadius, group, id); |
| 139 |
} |
| 140 |
|
| 141 |
} // namespace Avogadro::Rendering |