OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LED.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #include "LED.h"
26 
27 namespace osvr {
28 namespace vbtracker {
29 
30  Led::Led(LedIdentifier *identifier, LedMeasurement const &meas)
31  : m_id(SENTINEL_NO_IDENTIFIER_OBJECT_OR_INSUFFICIENT_DATA),
32  m_identifier(identifier) {
35  addMeasurement(meas, false);
36  }
37 
38  void Led::addMeasurement(LedMeasurement const &meas, bool blobsKeepId) {
39  m_latestMeasurement = meas;
40  m_brightnessHistory.push_back(meas.brightness);
41 
42  // If we don't have an identifier, then our ID is unknown.
43  // Otherwise, try and find it.
44  if (!m_identifier) {
45  m_id = SENTINEL_NO_IDENTIFIER_OBJECT_OR_INSUFFICIENT_DATA;
46  } else {
47  auto oldId = m_id;
48  m_id = m_identifier->getId(m_id, m_brightnessHistory, m_lastBright,
49  blobsKeepId);
50 #if 0
51  m_newlyRecognized = oldId < 0 && m_id >= 0;
52  auto lostRecognition = m_id < 0 && oldId >= 0;
53  if (!m_newlyRecognized && !lostRecognition && oldId != m_id) {
54  std::cout << "Identity theft detected!" << std::endl;
55  }
56 #endif
57 
61 
64  if (oldId != m_id) {
66  m_novelty = MAX_NOVELTY;
67  } else if (m_novelty != 0) {
69  m_novelty--;
70  }
71  }
72  }
73 
74  KeyPointIterator Led::nearest(KeyPointList &keypoints,
75  double threshold) const {
76  // If we have no elements in the vector, return the end().
77  if (keypoints.empty()) {
78  return end(keypoints);
79  }
80 
81  // Squaring the threshold to avoid doing a square-root in a tight loop.
82  auto thresholdSquared = threshold * threshold;
83  auto location = getLocation();
84 
85  auto computeDistSquared = [location](KeyPointIterator it) {
86  auto diff = (location - it->pt);
87  return diff.dot(diff);
88  };
89 
90  // Find the distance to the first point and record it as the
91  // current minimum distance;
92  auto ret = begin(keypoints);
93  auto minDistSq = computeDistSquared(ret);
94 
95  // Search the rest of the elements to see if we can find a
96  // better one.
97  for (auto it = begin(keypoints), e = end(keypoints); it != e; ++it) {
98  auto distSq = computeDistSquared(it);
99  if (distSq < minDistSq) {
100  minDistSq = distSq;
101  ret = it;
102  }
103  }
104 
105  // If the closest is within the threshold, return it. Otherwise,
106  // return the end.
107  if (minDistSq <= thresholdSquared) {
108  return ret;
109  }
110  return end(keypoints);
111  }
112 
113  LedMeasurementIterator Led::nearest(LedMeasurementList &meas,
114  double threshold) const {
115  // If we have no elements in the vector, return the end().
116  if (meas.empty()) {
117  return end(meas);
118  }
119 
120  // Squaring the threshold to avoid doing a square-root in a tight loop.
121  auto thresholdSquared = threshold * threshold;
122  auto location = getLocation();
123 
124  auto computeDistSquared = [location](LedMeasurementIterator it) {
125  auto diff = (location - it->loc);
126  return diff.dot(diff);
127  };
128 
129  // Find the distance to the first point and record it as the
130  // current minimum distance;
131  auto ret = begin(meas);
132  auto minDistSq = computeDistSquared(ret);
133 
134  // Search the rest of the elements to see if we can find a
135  // better one.
136  for (auto it = begin(meas), e = end(meas); it != e; ++it) {
137  auto distSq = computeDistSquared(it);
138  if (distSq < minDistSq) {
139  minDistSq = distSq;
140  ret = it;
141  }
142  }
143 
144  // If the closest is within the threshold, return it. Otherwise,
145  // return the end.
146  if (minDistSq <= thresholdSquared) {
147  return ret;
148  }
149  return end(meas);
150  }
151 
152  void Led::markMisidentified() {
153  m_id = SENTINEL_NO_IDENTIFIER_OBJECT_OR_INSUFFICIENT_DATA;
154  if (!m_brightnessHistory.empty()) {
155  m_brightnessHistory.clear();
156  m_brightnessHistory.push_back(getMeasurement().brightness);
157  }
158  }
159 
160 } // End namespace vbtracker
161 } // End namespace osvr
cv::Point2f getLocation() const
Reports the most-recently-added position.
Definition: LED.h:107
KeyPointIterator nearest(KeyPointList &keypoints, double threshold) const
Find the nearest KeyPoint from a container of points to me, if there is one within the specified thre...
Definition: LED.cpp:74
Led(LedIdentifier *identifier, LedMeasurement const &meas)
Definition: LED.cpp:30
Header file for class that tracks and identifies LEDs.
void addMeasurement(LedMeasurement const &meas, bool blobsKeepId)
Add a new measurement for this LED, which must be for a frame that is just following the previous mea...
Definition: LED.cpp:37
void markMisidentified()
Definition: LED.cpp:76
virtual ZeroBasedBeaconId getId(ZeroBasedBeaconId currentId, BrightnessList &brightnesses, bool &lastBright, bool blobsKeepId) const =0
Determine the identity of the LED whose brightness pattern is passed in. Truncates the passed-in list...