00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "GUI/ViewPort3D.H"
00037 #include "Util/log.H"
00038 #include "Util/WorkThreadServer.H"
00039 #include "Util/JobWithSemaphore.H"
00040 #include "Component/ModelManager.H"
00041 #include "Raster/GenericFrame.H"
00042 #include "Image/Layout.H"
00043 #include "Image/MatrixOps.H"
00044 #include "Image/DrawOps.H"
00045 #include "Image/FilterOps.H"
00046 #include "Image/MathOps.H"
00047 #include "Image/PixelsTypes.H"
00048 #include "Image/ColorOps.H"
00049 #include "Media/FrameSeries.H"
00050 #include "Transport/FrameInfo.H"
00051 #include "GUI/XWinManaged.H"
00052 #include "GUI/ImageDisplayStream.H"
00053
00054 #include <stdlib.h>
00055 #include <math.h>
00056 #include <fcntl.h>
00057
00058 void drawAir (void) {
00059
00060
00061
00062
00063
00064
00065
00066
00067 glBegin (GL_POLYGON);
00068 glEdgeFlag (1);
00069 glVertex3f (0,-1, 10);
00070 glEdgeFlag (0);
00071 glVertex3f (2, 0, 4);
00072 glEdgeFlag (1);
00073 glVertex3f (-2, 0, 4);
00074 glEnd ();
00075
00076 glEdgeFlag (1);
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 }
00268
00269 void tridisplay()
00270
00271
00272
00273
00274
00275
00276
00277 {
00278 glClear(GL_COLOR_BUFFER_BIT);
00279
00280 glBegin(GL_TRIANGLES);
00281
00282 glColor3f(0.3,0.3,0.3);
00283 glVertex2f(-0.5,0.0);
00284 glColor3f(0.0,0.0,1.0);
00285 glVertex2f(0.0,-1.0);
00286 glEdgeFlag(GL_FALSE);
00287 glVertex2f(0.5,0.0);
00288
00289 glEdgeFlag(GL_TRUE);
00290 glVertex2f(0.5,0.0);
00291 glColor3f(0.0,1.0,0.0);
00292 glVertex2f(0.497,0.052);
00293 glEdgeFlag(GL_FALSE);
00294 glVertex2f(0.489,0.104);
00295
00296 glEdgeFlag(GL_TRUE);
00297 glVertex2f(0.489,0.104);
00298 glVertex2f(0.476,0.155);
00299 glEdgeFlag(GL_FALSE);
00300 glVertex2f(0.457,0.203);
00301
00302 glEdgeFlag(GL_TRUE);
00303 glVertex2f(0.457,0.203);
00304 glVertex2f(0.433,0.25);
00305 glEdgeFlag(GL_FALSE);
00306 glVertex2f(0.405,0.294);
00307
00308 glEdgeFlag(GL_TRUE);
00309 glVertex2f(0.405,0.294);
00310 glVertex2f(0.372,0.335);
00311 glColor3f(1.0,0.0,0.0);
00312 glEdgeFlag(GL_FALSE);
00313 glVertex2f(0.335,0.372);
00314
00315 glEdgeFlag(GL_TRUE);
00316 glVertex2f(0.335,0.372);
00317 glVertex2f(0.294,0.405);
00318 glEdgeFlag(GL_FALSE);
00319 glVertex2f(0.25,0.433);
00320
00321 glEdgeFlag(GL_TRUE);
00322 glVertex2f(0.25,0.433);
00323 glVertex2f(0.203,0.457);
00324 glEdgeFlag(GL_FALSE);
00325 glVertex2f(0.155,0.476);
00326
00327 glEdgeFlag(GL_TRUE);
00328 glVertex2f(0.155,0.476);
00329 glVertex2f(0.104,0.489);
00330 glEdgeFlag(GL_FALSE);
00331 glVertex2f(0.052,0.497);
00332
00333 glEdgeFlag(GL_TRUE);
00334 glVertex2f(0.052,0.497);
00335 glVertex2f(0.0,0.5);
00336 glColor3f(0.5,0.0,0.5);
00337 glEdgeFlag(GL_FALSE);
00338 glVertex2f(-0.052,0.497);
00339
00340 glEdgeFlag(GL_TRUE);
00341 glVertex2f(-0.052,0.497);
00342 glVertex2f(-0.104,0.489);
00343 glEdgeFlag(GL_FALSE);
00344 glVertex2f(-0.155,0.476);
00345
00346 glEdgeFlag(GL_TRUE);
00347 glVertex2f(-0.155,0.476);
00348 glVertex2f(-0.203,0.457);
00349 glEdgeFlag(GL_FALSE);
00350 glVertex2f(-0.25,0.433);
00351
00352 glEdgeFlag(GL_TRUE);
00353 glVertex2f(-0.25,0.433);
00354 glVertex2f(-0.294,0.405);
00355 glEdgeFlag(GL_FALSE);
00356 glVertex2f(-0.335,0.372);
00357
00358 glEdgeFlag(GL_TRUE);
00359 glVertex2f(-0.335,0.372);
00360 glVertex2f(-0.372,0.335);
00361 glEdgeFlag(GL_FALSE);
00362 glVertex2f(-0.404,0.294);
00363
00364 glEdgeFlag(GL_TRUE);
00365 glVertex2f(-0.404,0.294);
00366 glVertex2f(-0.433,0.25);
00367 glEdgeFlag(GL_FALSE);
00368 glVertex2f(-0.457,0.203);
00369
00370 glEdgeFlag(GL_TRUE);
00371 glVertex2f(-0.457,0.203);
00372 glVertex2f(-0.476,0.155);
00373 glEdgeFlag(GL_FALSE);
00374 glVertex2f(-0.489,0.104);
00375
00376 glEdgeFlag(GL_TRUE);
00377 glVertex2f(-0.489,0.104);
00378 glVertex2f(-0.497,0.052);
00379 glEdgeFlag(GL_FALSE);
00380 glVertex2f(-0.5,0.0);
00381 glEdgeFlag(GL_TRUE);
00382
00383 glEnd();
00384
00385 glFlush();
00386 }
00387
00388
00389 Point2D<int> getMouseClick(nub::ref<OutputFrameSeries> &ofs)
00390 {
00391 const nub::soft_ref<ImageDisplayStream> ids =
00392 ofs->findFrameDestType<ImageDisplayStream>();
00393
00394 const rutz::shared_ptr<XWinManaged> uiwin =
00395 ids.is_valid()
00396 ? ids->getWindow("InputEdges")
00397 : rutz::shared_ptr<XWinManaged>();
00398
00399 if (uiwin.is_valid())
00400 return uiwin->getLastMouseClick();
00401 else
00402 return Point2D<int>(-1,-1);
00403 }
00404
00405 int getKey(nub::ref<OutputFrameSeries> &ofs)
00406 {
00407 const nub::soft_ref<ImageDisplayStream> ids =
00408 ofs->findFrameDestType<ImageDisplayStream>();
00409
00410 const rutz::shared_ptr<XWinManaged> uiwin =
00411 ids.is_valid()
00412 ? ids->getWindow("InputEdges")
00413 : rutz::shared_ptr<XWinManaged>();
00414 if (uiwin.is_valid())
00415 return uiwin->getLastKeyPress();
00416 else
00417 return -1;
00418 }
00419
00420 void saveToDisk(const std::vector<Point2D<float> >& contour, const char* filename)
00421 {
00422 int fd;
00423
00424 if ((fd = creat(filename, 0644)) == -1)
00425 LFATAL("Can not open %s for saving\n", filename);
00426
00427 int ret;
00428 size_t size = contour.size();
00429 ret = write(fd, (char *) &size, sizeof(size_t));
00430
00431 for(uint i=0; i<contour.size(); i++)
00432 ret = write(fd, (char *) &contour[i], sizeof(Point2D<float>));
00433
00434 close(fd);
00435 }
00436
00437 std::vector<Point2D<float> > readFromDisk(const char* filename)
00438 {
00439
00440 int fd;
00441 if ((fd = open(filename, 0, 0644)) == -1)
00442 return std::vector<Point2D<float> >();
00443
00444 int ret;
00445 printf("Reading from %s\n", filename);
00446
00447
00448 size_t size;
00449 ret = read(fd, (char *) &size, sizeof(size));
00450
00451 std::vector<Point2D<float> > contour(size);
00452 for(uint i=0; i<size; i++)
00453 ret = read(fd, (char *) &contour[i], sizeof(Point2D<float>));
00454
00455 close(fd);
00456
00457 return contour;
00458 }
00459
00460
00461
00462
00463
00464 int main(int argc, char *argv[]){
00465
00466 ModelManager manager("Test Viewport");
00467
00468 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00469 manager.addSubComponent(ofs);
00470
00471 nub::ref<InputFrameSeries> ifs(new InputFrameSeries(manager));
00472 manager.addSubComponent(ifs);
00473
00474
00475
00476 if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00477
00478 manager.start();
00479
00480 ifs->startStream();
00481
00482 ViewPort3D vp(320,240);
00483
00484
00485
00486
00487 std::vector<Point2D<int> > polygon;
00488 std::vector<Point2D<float> > contour;
00489 std::vector<Point2D<float> > bodyContour = readFromDisk("body.dat");
00490 std::vector<Point2D<float> > roll1Contour = readFromDisk("roll1.dat");
00491 std::vector<Point2D<float> > roll2Contour = readFromDisk("roll2.dat");
00492
00493
00494
00495
00496
00497
00498
00499 float x = -130;
00500 while(1)
00501 {
00502 vp.setCamera(Point3D<float>(0,0,0), Point3D<float>(0,0,0));
00503 Image< PixRGB<byte> > inputImg;
00504 const FrameState is = ifs->updateNext();
00505 if (is == FRAME_COMPLETE)
00506 break;
00507 GenericFrame input = ifs->readFrame();
00508 if (!input.initialized())
00509 break;
00510 inputImg = input.asRgb();
00511
00512 int key = getKey(ofs);
00513 if (key != -1)
00514 {
00515 LINFO("key %i", key);
00516 switch(key)
00517 {
00518 case 98:
00519 x += 1;
00520 break;
00521 case 104:
00522 x -= 1;
00523 break;
00524 case 39:
00525 saveToDisk(contour, "body.dat");
00526 break;
00527 }
00528 LINFO("X=%f\n", x);
00529 }
00530
00531 vp.initFrame();
00532
00533
00534
00535
00536 std::vector<Point2D<float> > triangles = vp.triangulate(contour);
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 glTranslatef(0.0F, 0,-130-25.0F);
00554 glRotatef(x, 0,1,0);
00555 if (true)
00556 {
00557 vp.drawExtrudedContour(bodyContour,
00558 Point3D<float>(0,0,0),
00559 Point3D<float>(0,0,0),
00560 50.0F,
00561 PixRGB<byte>(256,256,0));
00562
00563 }
00564
00565 Point2D<int> loc = getMouseClick(ofs);
00566 if (loc.isValid())
00567 {
00568 polygon.push_back(loc);
00569
00570 float x = loc.i - (inputImg.getWidth()/2);
00571 float y = -1*(loc.j - (inputImg.getHeight()/2));
00572 LINFO("Click at %i %i (%f,%f)\n", loc.i, loc.j, x, y);
00573 contour.push_back(Point2D<float>(x/2.72289,y/2.72289));
00574 }
00575
00576
00577 Image<PixRGB<byte> > img = flipVertic(vp.getFrame()) + inputImg;
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 Image<PixRGB<byte> > renderImg = flipVertic(vp.getFrame());
00589
00590 for(uint i=0; i<img.size(); i++)
00591 if (renderImg[i] != PixRGB<byte>(0,0,0))
00592 img[i] = renderImg[i];
00593
00594 ofs->writeRGB(img, "ViewPort3D", FrameInfo("ViewPort3D", SRC_POS));
00595 ofs->writeRGB(inputImg, "Input", FrameInfo("Input", SRC_POS));
00596
00597 Image<float> mag, ori, lum;
00598 lum = luminance(renderImg);
00599 gradientSobel(lum, mag, ori);
00600 inplaceNormalize(mag, 0.0F, 255.0F);
00601 ofs->writeRGB(toRGB(mag), "RenderEdges", FrameInfo("RenderEdges", SRC_POS));
00602
00603 lum = luminance(inputImg);
00604 gradientSobel(lum, mag, ori);
00605 inplaceNormalize(mag, 0.0F, 255.0F);
00606 ofs->writeRGB(toRGB(mag), "InputEdges", FrameInfo("InputEdges", SRC_POS));
00607
00608
00609 usleep(10000);
00610 }
00611
00612
00613 exit(0);
00614
00615 }
00616
00617
00618