71 lines
1.6 KiB
C++
Executable File
71 lines
1.6 KiB
C++
Executable File
/*
|
|
* Ruler.cpp
|
|
*
|
|
* Created on: May 8, 2018
|
|
* Author: yunya
|
|
*/
|
|
|
|
#include "Ruler.h"
|
|
#include <sstream>
|
|
|
|
Ruler::Ruler(UI *ui, int *lastKey) {
|
|
Ruler::ui = ui;
|
|
Ruler::lastKey = lastKey;
|
|
focalLength = estimateFocalLength();
|
|
}
|
|
|
|
void Ruler::update(cv::Rect rect) {
|
|
focalLength = estimateFocalLength();
|
|
distance = estimateDistance(rect);
|
|
inputUpdate();
|
|
updateUI();
|
|
}
|
|
|
|
float Ruler::estimateFocalLength(int HFOV) {
|
|
return (ui->getWidth()/2.0)*(1/(std::tan(HFOV/2.0)));
|
|
}
|
|
|
|
void Ruler::updateUI() {
|
|
if (inputtingVal) {
|
|
cv::putText(*(ui->drawnFrame()), ("Given width: " << widthInCM), confTextPos, cv::FONT_HERSHEY_DUPLEX, 0.7, textFontColor + 50);
|
|
} else {
|
|
cv::putText(*(ui->drawnFrame()), ("Given width: " << widthInCM), confTextPos, cv::FONT_HERSHEY_DUPLEX, 0.7, textFontColor);
|
|
}
|
|
cv::putText(*(ui->drawnFrame()), ("distance: " << distance << "cm"), distanceTextPos, cv::FONT_HERSHEY_DUPLEX, 0.7, textFontColor);
|
|
|
|
}
|
|
|
|
void Ruler::toggleInput() {
|
|
inputtingVal = !inputtingVal;
|
|
if (!inputtingVal) {
|
|
numBuild.clear();
|
|
}
|
|
}
|
|
|
|
int Ruler::estimateDistance(cv::Rect rect) {
|
|
return (widthInCM*focalLength) / rect.width;
|
|
}
|
|
|
|
void Ruler::inputUpdate() {
|
|
if (*lastKey == 13) {
|
|
toggleInput();
|
|
}
|
|
if (inputtingVal) {
|
|
if (*lastKey >= 48 && *lastKey <= 57) {
|
|
char val = '0' + (*lastKey - 48);
|
|
numBuild.push_back(val);
|
|
}
|
|
std::string numString{numBuild.begin(), numBuild.end()};
|
|
numString >> widthInCM;
|
|
}
|
|
}
|
|
|
|
void Ruler::componentSetup() {
|
|
|
|
}
|
|
|
|
Ruler::~Ruler() {
|
|
// TODO Auto-generated destructor stub
|
|
}
|
|
|