File: Picking.cpp

package info (click to toggle)
pymol 2.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,312 kB
  • sloc: cpp: 480,106; python: 79,860; ansic: 28,343; javascript: 6,792; sh: 47; makefile: 30; csh: 8
file content (198 lines) | stat: -rw-r--r-- 5,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Color picking
 *
 * (C) Schrodinger, Inc.
 */

#include "Picking.h"

#include <algorithm>
#include <cassert>

constexpr auto MAX_BITS = 8u;

/**
 * Value to put in the "unused bits" part of a color channel.
 * May serve several purposes:
 * - validation (check bits)
 * - no-pick vs. through-pick
 * - rounding
 *
 * Disclaimer: I'm not sure if rounding is ever needed and if rounding and
 * validation should be mutually exclusive.
 */
constexpr unsigned make_check_value(unsigned bits)
{
  return 0x80u >> bits;
}

/**
 * Return true if this RGBA color looks valid, and false if it should
 * be ignored.
 */
bool PickColorConverter::validateCheckBits(const channel_t* rgba) const
{
  for (unsigned i = 0; i != 4; ++i) {
    assert(m_rgba_and_check_bits[i] >= m_rgba_bits[i]);

    // mask to stamp out the validation bits
    const channel_t check_mask = (0xFFu >> m_rgba_bits[i]) & //
                                 ~(0xFFu >> m_rgba_and_check_bits[i]);

    const channel_t check_value = make_check_value(m_rgba_bits[i]);

    if ((rgba[i] & check_mask) != (check_value & check_mask)) {
      // found antialiased bit (or cPickableNoPick/cPickableThrough which can
      // also be ignored)
      return false;
    }
  }

  return true;
}

/**
 * Set the number of bits per picking channel
 * @param rgba_bits Number of available bits per channel
 * @param max_check_bits Maximum number of bits (per channel) to use for
 * validation
 */
void PickColorConverter::setRgbaBits(const int* rgba_bits, int max_check_bits)
{
  for (unsigned i = 0; i != 4; ++i) {
    m_rgba_bits[i] = std::min<unsigned>(MAX_BITS, rgba_bits[i]);

    // Use at most half of the pixels for validation
    int const check_bits = std::min(m_rgba_bits[i] / 2, max_check_bits);

    m_rgba_and_check_bits[i] = m_rgba_bits[i];
    m_rgba_bits[i] = std::max(0, int(m_rgba_bits[i]) - check_bits);
  }

  // Use one bit to distinguish no-pick and through-pick.
  // This bit only has to reach the shader, it's not relevant for the
  // output buffer. This means it can be part of the check bits, but
  // doesn't have to be. It can also be beyond the color depth of the
  // output buffer (In theory, I have no suitable system to test this
  // assumption).
  m_rgba_bits[3] = std::min<unsigned>(MAX_BITS - 1, m_rgba_bits[3]);
}

/**
 * Get the picking index from color
 * @param rgba RGBA color
 * @return Picking index
 */
PickColorConverter::index_t PickColorConverter::indexFromColor(
    const channel_t* rgba) const
{
  if (!validateCheckBits(rgba)) {
    return 0;
  }

  unsigned idx = 0;
  unsigned bits = 0;
  for (unsigned i = 0; i != 4; ++i) {
    idx |= (unsigned(rgba[i]) >> (MAX_BITS - m_rgba_bits[i])) << bits;
    bits += m_rgba_bits[i];
  }
  return idx;
}

/**
 * Get color from picking index
 * @param[out] rgba RGBA color
 * @param idx Picking index
 */
void PickColorConverter::colorFromIndex(channel_t* rgba, index_t idx) const
{
  unsigned bits = 0;
  for (unsigned i = 0; i != 4; ++i) {
    rgba[i] = ((idx >> bits) & 0xFFu) << (MAX_BITS - m_rgba_bits[i]);

    rgba[i] |= make_check_value(m_rgba_bits[i]);

    bits += m_rgba_bits[i];
  }
}

/**
 * Get no-pick color
 * @param[out] rgba RGBA color
 */
void PickColorConverter::colorNoPick(channel_t* rgba) const
{
  rgba[0] = 0;
  rgba[1] = 0;
  rgba[2] = 0;
  rgba[3] = make_check_value(m_rgba_bits[3]);
  assert(rgba[3] != 0);
}

/**
 * Get pick-through color
 * @param[out] rgba RGBA color
 *
 * Note: This only works if the shader discards full-opaque pixels (e.g. may not
 * work in immediate mode)
 */
void PickColorConverter::colorPickThrough(channel_t* rgba) const
{
  rgba[0] = 0;
  rgba[1] = 0;
  rgba[2] = 0;
  rgba[3] = 0;
}

/**
 * Get the color for (context, index, bond, current picking pass).
 */
void PickColorManager::colorNext(unsigned char* color,
    const PickContext* context, unsigned int index, int bond)
{
  if (bond == cPickableNoPick) {
    colorNoPick(color);
    return;
  }

  if (bond == cPickableThrough) {
    // TODO has no effect on immediate mode (fragment not discarded)
    colorPickThrough(color);
    return;
  }

  const Picking p_new = {{index, bond}, *context};

  assert(m_count <= m_identifiers.size());

  // compare with previous pick color, increment if different
  if (m_count == 0 || m_identifiers[m_count - 1] != p_new) {
    ++m_count;
  }

  unsigned j = m_count;

  if (m_pass > 0) {
    assert(m_count <= m_identifiers.size());
    j >>= getTotalBits() * m_pass;
  } else if (m_count == m_identifiers.size() + 1) {
    m_identifiers.push_back(p_new);
  }

  // if this assertion fails, then invalidate() was not called, but should be
  assert(m_identifiers[m_count - 1] == p_new);

  colorFromIndex(color, j);
}

/**
 * Get identifier for the 1-based picking color index.
 * Returns NULL if the index is out of bounds.
 */
const Picking* PickColorManager::getIdentifier(unsigned index) const
{
  if (index > 0 && index <= m_identifiers.size()) {
    return m_identifiers.data() + index - 1;
  }
  return nullptr;
}