/
ARCube.cpp
344 lines (309 loc) · 9.3 KB
/
ARCube.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "pch_RulrNodes.h"
#include "ARCube.h"
#include "ofxRulr/Nodes/Item/Camera.h"
#include "ofxRulr/Nodes/Item/AbstractBoard.h"
#include "ofxCvMin.h"
#include "ofxCvGui/Widgets/LiveValue.h"
#include "ofxCvGui/Widgets/Toggle.h"
#include "ofxCvGui/Widgets/Indicator.h"
#include "ofxCvGui/Widgets/Title.h"
#include "ofxCvGui/Widgets/MultipleChoice.h"
#include "ofxCvGui/Widgets/Slider.h"
using namespace ofxCvGui;
using namespace ofxCv;
namespace ofxRulr {
namespace Nodes {
namespace Test {
//----------
ARCube::ARCube() {
RULR_NODE_INIT_LISTENER;
}
//----------
string ARCube::getTypeName() const {
return "Test::ARCube";
}
//----------
void ARCube::init() {
RULR_NODE_UPDATE_LISTENER;
RULR_NODE_DRAW_WORLD_LISTENER;
RULR_NODE_INSPECTOR_LISTENER;
RULR_NODE_SERIALIZATION_LISTENERS;
this->addInput<Item::Camera>();
this->addInput<Item::AbstractBoard>();
this->view = make_shared<Panels::Draws>(this->fbo);
this->view->onDraw += [this](DrawArguments & args) {
if (!this->getRunFinderEnabled()) {
ofxCvGui::Utils::drawText("Select this node and connect active camera.", args.localBounds);
}
};
//remove complaints from log about not allocated
this->fbo.allocate(1, 1, OF_IMAGE_COLOR_ALPHA);
//--
//Setup box
//--
//
//colour vertices
{
//set dimensions to 1.0f and move it forwards so sitting on z=0
this->cube.set(1.0f);
this->cube.move(ofVec3f(0, 0, -0.5f));
//setup the colours
auto & cubeMesh = this->cube.getMesh();
auto & colours = cubeMesh.getColors();
const auto & vertices = cubeMesh.getVertices();
auto numVertices = vertices.size();
colours.resize(numVertices);
for (int i = 0; i < numVertices; i++) {
for (int j = 0; j < 3; j++) {
colours[i][j] = ofMap(vertices[i][j], -0.5f, 0.5f, 0.3f, 1.0f);
}
colours[i].a = 1.0f;
}
}
//
//--
this->foundBoard = false;
}
//----------
void ARCube::update() {
auto camera = this->getInput<Item::Camera>();
if (camera) {
auto grabber = camera->getGrabber();
if (grabber->isFrameNew()) {
//if we're not using freerun, then we presume we're using single shot (since there's a frame available)
if (this->getRunFinderEnabled()) {
this->updateTracking();
}
}
}
}
//----------
void ARCube::updateTracking() {
auto camera = this->getInput<Item::Camera>();
if (camera) {
auto grabber = camera->getGrabber();
//allocate the undistorted image and fbo when required
auto distorted = grabber->getPixels();
auto & undistorted = this->undistorted.getPixels();
if (undistorted.getWidth() != distorted.getWidth() || undistorted.getHeight() != distorted.getHeight() || undistorted.getNumChannels() != distorted.getNumChannels()) {
//if undistorted isn't the right shape/format, then reallocate undistorted and the fbo
undistorted = distorted;
ofFbo::Settings fboSettings;
fboSettings.width = undistorted.getWidth();
fboSettings.height = undistorted.getHeight();
fboSettings.useDepth = true;
fboSettings.depthStencilInternalFormat = GL_DEPTH_COMPONENT24;
fboSettings.minFilter = GL_NEAREST;
fboSettings.maxFilter = GL_NEAREST;
this->fbo.allocate(fboSettings);
}
//perform computer vision tasks
try {
auto cameraMatrix = camera->getCameraMatrix();
auto distortionCoefficients = camera->getDistortionCoefficients();
//make the undistorted preview image
cv::undistort(toCv(distorted), toCv(undistorted), cameraMatrix, distortionCoefficients);
this->undistorted.update();
//find the board
auto board = this->getInput<Item::AbstractBoard>();
if (board) {
//find board in distorted image
vector<cv::Point2f> imagePoints;
vector<cv::Point3f> objectPoints;
auto success = board->findBoard(toCv(distorted)
, imagePoints
, objectPoints
, this->parameters.findBoardMode
, camera->getCameraMatrix()
, camera->getDistortionCoefficients());
if (success) {
//use solvePnP to resolve transform
Mat rotation, translation;
cv::solvePnP(objectPoints
, imagePoints
, cameraMatrix
, distortionCoefficients
, rotation
, translation);
this->boardTransform = makeMatrix(rotation, translation);
this->foundBoard = true;
}
else {
this->foundBoard = false;
}
}
}
RULR_CATCH_ALL_TO_ERROR
//update the fbo
this->fbo.begin();
{
//draw the undistorted image
this->undistorted.draw(0, 0);
//draw the 3D world on top
const auto & view = camera->getViewInWorldSpace();
view.beginAsCamera(true);
{
glEnable(GL_DEPTH_TEST);
{
glClear(GL_DEPTH_BUFFER_BIT);
this->drawWorldStage();
}
glDisable(GL_DEPTH_TEST);
}
view.endAsCamera();
//emulate alpha by drawing undistorted image again on top
if (this->parameters.alpha < 1.0f) {
ofEnableAlphaBlending();
{
ofSetColor(255, 255.0f * (1.0f - this->parameters.alpha));
//draw the undistorted image
this->undistorted.draw(0, 0);
}
ofDisableAlphaBlending();
}
}
this->fbo.end();
}
}
//----------
void ARCube::populateInspector(InspectArguments & inspectArguments) {
auto inspector = inspectArguments.inspector;
inspector->addParameterGroup(this->parameters);
inspector->add(new Widgets::Indicator("Found board", [this]() {
if (this->getInputPin<Item::AbstractBoard>()->isConnected() && this->getInputPin<Item::Camera>()->isConnected()) {
if (this->foundBoard) {
return Widgets::Indicator::Status::Good;
}
else {
return Widgets::Indicator::Status::Clear;
}
}
else {
return Widgets::Indicator::Status::Error;
}
}));
inspector->add(new Widgets::LiveValue<string>("Board position", [this]() {
if (this->foundBoard) {
stringstream ss;
ss << (ofVec3f() * this->boardTransform);
return ss.str();
}
else {
return string("");
}
}));
inspector->addButton("Force update", [this]() {
this->updateTracking();
}, ' ');
}
//----------
void ARCube::serialize(Json::Value & json) {
Utils::Serializable::serialize(json, this->parameters);
}
//----------
void ARCube::deserialize(const Json::Value & json) {
Utils::Serializable::deserialize(json, this->parameters);
}
//----------
void ARCube::drawWorldStage() {
auto camera = this->getInput<Item::Camera>();
auto board = this->getInput<Item::AbstractBoard>();
if (board && camera && this->foundBoard) {
ofPushStyle();
{
ofPushMatrix();
{
auto spacing = board->getSpacing();
ofMultMatrix(camera->getTransform());
ofMultMatrix(this->boardTransform);
switch (this->parameters.drawStyle.get()) {
case DrawStyle::Axes:
{
ofDrawAxis(spacing);
auto position = ofVec3f() * this->boardTransform;
ofPushStyle();
{
ofDrawBitmapString(ofToString(position), ofVec3f());
}
ofPopStyle();
break;
}
case DrawStyle::Cube:
{
ofPushMatrix();
{
auto scale = board->getSpacing() * 4.0f;
ofScale(scale, scale, scale);
ofPushStyle();
{
if (this->parameters.fillMode.get() == FillMode::Fill) {
ofFill();
}
else {
ofNoFill();
}
this->cube.draw();
}
ofPopStyle();
}
ofPopMatrix();
break;
}
case DrawStyle::Board:
{
if (this->parameters.fillMode.get() == FillMode::Fill) {
ofFill();
}
else {
ofNoFill();
}
board->drawObject();
break;
}
default:
break;
}
}
ofPopMatrix();
}
ofPopStyle();
}
}
//----------
bool ARCube::getRunFinderEnabled() const {
auto camera = this->getInput<Item::Camera>();
if(!camera) {
return false;
}
auto grabber = camera->getGrabber();
if(!grabber->getIsDeviceOpen()) {
return false;
}
switch (this->parameters.activewhen.get()) {
case ActiveWhen::Always:
return true;
case ActiveWhen::Selected:
if (this->isBeingInspected()) {
return true;
}
break;
default:
break;
}
if (camera) {
auto grabber = camera->getGrabber();
if (!grabber->getDeviceSpecification().supports(ofxMachineVision::CaptureSequenceType::Continuous)) {
//find when the camera is a single shot camera
return true;
}
}
//if nothing was good, then return false
return false;
}
//----------
ofxCvGui::PanelPtr ARCube::getPanel() {
return this->view;
}
}
}
}