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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
|
// Copyright Contributors to the OpenVDB Project
// SPDX-License-Identifier: MPL-2.0
#include "Viewer.h"
#include "Camera.h"
#include "ClipBox.h"
#include "Font.h"
#include "RenderModules.h"
#include <openvdb/util/Formats.h> // for formattedInt()
#include <openvdb/util/logging.h>
#include <openvdb/points/PointDataGrid.h>
#include <openvdb/points/PointCount.h>
#include <openvdb/version.h> // for OPENVDB_LIBRARY_MAJOR_VERSION, etc.
#include <atomic>
#include <cmath> // for fabs()
#include <iomanip> // for std::setprecision()
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <vector>
#include <limits>
#include <thread>
#include <chrono>
#if defined(_WIN32)
#include <GL/glew.h>
#endif
#include <GLFW/glfw3.h>
namespace openvdb_viewer {
class ViewerImpl
{
public:
using CameraPtr = std::shared_ptr<Camera>;
using ClipBoxPtr = std::shared_ptr<ClipBox>;
using RenderModulePtr = std::shared_ptr<RenderModule>;
ViewerImpl();
void init(const std::string& progName);
std::string getVersionString() const;
bool isOpen() const;
bool open(int width = 900, int height = 800);
void view(const openvdb::GridCPtrVec&);
void handleEvents();
void close();
void resize(int width, int height);
void showPrevGrid();
void showNextGrid();
bool needsDisplay();
void setNeedsDisplay();
void toggleRenderModule(size_t n);
void toggleInfoText();
// Internal
void render();
void interrupt();
void setWindowTitle(double fps = 0.0);
void showNthGrid(size_t n);
void updateCutPlanes(int wheelPos);
void swapBuffers();
void keyCallback(int key, int action);
void mouseButtonCallback(int button, int action);
void mousePosCallback(int x, int y);
void mouseWheelCallback(int pos);
void windowSizeCallback(int width, int height);
void windowRefreshCallback();
static openvdb::BBoxd worldSpaceBBox(const openvdb::math::Transform&,
const openvdb::CoordBBox&);
static void sleep(double seconds);
private:
bool mDidInit;
CameraPtr mCamera;
ClipBoxPtr mClipBox;
RenderModulePtr mViewportModule;
std::vector<RenderModulePtr> mRenderModules;
openvdb::GridCPtrVec mGrids;
size_t mGridIdx, mUpdates;
std::string mGridName, mProgName, mGridInfo, mTransformInfo, mTreeInfo;
int mWheelPos;
bool mShiftIsDown, mCtrlIsDown, mShowInfo;
bool mInterrupt;
GLFWwindow* mWindow;
}; // class ViewerImpl
class ThreadManager
{
public:
ThreadManager();
void view(const openvdb::GridCPtrVec& gridList);
void close();
void resize(int width, int height);
private:
void doView();
static void* doViewTask(void* arg);
std::atomic<bool> mRedisplay;
bool mClose, mHasThread;
std::thread mThread;
openvdb::GridCPtrVec mGrids;
};
////////////////////////////////////////
namespace {
ViewerImpl* sViewer = nullptr;
ThreadManager* sThreadMgr = nullptr;
std::mutex sLock;
void
keyCB(GLFWwindow*, int key, int /*scancode*/, int action, int /*modifiers*/)
{
if (sViewer) sViewer->keyCallback(key, action);
}
void
mouseButtonCB(GLFWwindow*, int button, int action, int /*modifiers*/)
{
if (sViewer) sViewer->mouseButtonCallback(button, action);
}
void
mousePosCB(GLFWwindow*, double x, double y)
{
if (sViewer) sViewer->mousePosCallback(int(x), int(y));
}
void
mouseWheelCB(GLFWwindow*, double /*xoffset*/, double yoffset)
{
if (sViewer) sViewer->mouseWheelCallback(int(yoffset));
}
void
windowSizeCB(GLFWwindow*, int width, int height)
{
if (sViewer) sViewer->windowSizeCallback(width, height);
}
void
windowRefreshCB(GLFWwindow*)
{
if (sViewer) sViewer->windowRefreshCallback();
}
} // unnamed namespace
////////////////////////////////////////
Viewer
init(const std::string& progName, bool background)
{
if (sViewer == nullptr) {
std::lock_guard<std::mutex> lock(sLock);
if (sViewer == nullptr) {
OPENVDB_START_THREADSAFE_STATIC_WRITE
sViewer = new ViewerImpl;
OPENVDB_FINISH_THREADSAFE_STATIC_WRITE
}
}
sViewer->init(progName);
if (background) {
if (sThreadMgr == nullptr) {
std::lock_guard<std::mutex> lock(sLock);
if (sThreadMgr == nullptr) {
OPENVDB_START_THREADSAFE_STATIC_WRITE
sThreadMgr = new ThreadManager;
OPENVDB_FINISH_THREADSAFE_STATIC_WRITE
}
}
} else {
if (sThreadMgr != nullptr) {
std::lock_guard<std::mutex> lock(sLock);
delete sThreadMgr;
OPENVDB_START_THREADSAFE_STATIC_WRITE
sThreadMgr = nullptr;
OPENVDB_FINISH_THREADSAFE_STATIC_WRITE
}
}
return Viewer();
}
void
exit()
{
glfwTerminate();
}
////////////////////////////////////////
Viewer::Viewer()
{
OPENVDB_LOG_DEBUG_RUNTIME("constructed Viewer from thread " << std::this_thread::get_id());
}
void
Viewer::open(int width, int height)
{
if (sViewer) sViewer->open(width, height);
}
void
Viewer::view(const openvdb::GridCPtrVec& grids)
{
if (sThreadMgr) {
sThreadMgr->view(grids);
} else if (sViewer) {
sViewer->view(grids);
}
}
void
Viewer::handleEvents()
{
if (sViewer) sViewer->handleEvents();
}
void
Viewer::close()
{
if (sThreadMgr) sThreadMgr->close();
else if (sViewer) sViewer->close();
}
void
Viewer::resize(int width, int height)
{
if (sViewer) sViewer->resize(width, height);
}
std::string
Viewer::getVersionString() const
{
std::string version;
if (sViewer) version = sViewer->getVersionString();
return version;
}
////////////////////////////////////////
ThreadManager::ThreadManager()
: mClose(false)
, mHasThread(false)
{
mRedisplay = false;
}
void
ThreadManager::view(const openvdb::GridCPtrVec& gridList)
{
if (!sViewer) return;
mGrids = gridList;
mClose = false;
mRedisplay = true;
if (!mHasThread) {
mThread = std::thread(doViewTask, this);
mHasThread = true;
}
}
void
ThreadManager::close()
{
if (!sViewer) return;
// Tell the viewer thread to exit.
mRedisplay = false;
mClose = true;
// Tell the viewer to terminate its event loop.
sViewer->interrupt();
if (mHasThread) {
mThread.join();
mHasThread = false;
}
// Tell the viewer to close its window.
sViewer->close();
}
void
ThreadManager::doView()
{
// This function runs in its own thread.
// The mClose and mRedisplay flags are set from the main thread.
while (!mClose) {
// If mRedisplay was true, then set it to false
// and then, if sViewer, call view:
bool expected = true;
if (mRedisplay.compare_exchange_strong(expected, false)) {
if (sViewer) sViewer->view(mGrids);
}
sViewer->sleep(0.5/*sec*/);
}
}
//static
void*
ThreadManager::doViewTask(void* arg)
{
if (ThreadManager* self = static_cast<ThreadManager*>(arg)) {
self->doView();
}
return nullptr;
}
////////////////////////////////////////
ViewerImpl::ViewerImpl()
: mDidInit(false)
, mCamera(new Camera)
, mClipBox(new ClipBox)
, mGridIdx(0)
, mUpdates(0)
, mWheelPos(0)
, mShiftIsDown(false)
, mCtrlIsDown(false)
, mShowInfo(true)
, mInterrupt(false)
, mWindow(nullptr)
{
}
void
ViewerImpl::init(const std::string& progName)
{
mProgName = progName;
if (!mDidInit) {
struct Local {
static void errorCB(int error, const char* descr) {
OPENVDB_LOG_ERROR("GLFW Error " << error << ": " << descr);
}
};
glfwSetErrorCallback(Local::errorCB);
if (glfwInit() == GL_TRUE) {
OPENVDB_LOG_DEBUG_RUNTIME("initialized GLFW from thread "
<< std::this_thread::get_id());
mDidInit = true;
} else {
OPENVDB_LOG_ERROR("GLFW initialization failed");
}
}
mViewportModule.reset(new ViewportModule);
}
std::string
ViewerImpl::getVersionString() const
{
std::ostringstream ostr;
ostr << "OpenVDB: " <<
openvdb::OPENVDB_LIBRARY_MAJOR_VERSION << "." <<
openvdb::OPENVDB_LIBRARY_MINOR_VERSION << "." <<
openvdb::OPENVDB_LIBRARY_PATCH_VERSION;
int major, minor, rev;
glfwGetVersion(&major, &minor, &rev);
ostr << ", " << "GLFW: " << major << "." << minor << "." << rev;
if (mDidInit) {
ostr << ", " << "OpenGL: ";
std::shared_ptr<GLFWwindow> wPtr;
GLFWwindow* w = mWindow;
if (!w) {
wPtr.reset(glfwCreateWindow(100, 100, "", nullptr, nullptr), &glfwDestroyWindow);
w = wPtr.get();
}
if (w) {
ostr << glfwGetWindowAttrib(w, GLFW_CONTEXT_VERSION_MAJOR) << "."
<< glfwGetWindowAttrib(w, GLFW_CONTEXT_VERSION_MINOR) << "."
<< glfwGetWindowAttrib(w, GLFW_CONTEXT_REVISION);
}
}
return ostr.str();
}
bool
ViewerImpl::open(int width, int height)
{
if (mWindow == nullptr) {
glfwWindowHint(GLFW_RED_BITS, 8);
glfwWindowHint(GLFW_GREEN_BITS, 8);
glfwWindowHint(GLFW_BLUE_BITS, 8);
glfwWindowHint(GLFW_ALPHA_BITS, 8);
glfwWindowHint(GLFW_DEPTH_BITS, 32);
glfwWindowHint(GLFW_STENCIL_BITS, 0);
mWindow = glfwCreateWindow(
width, height, mProgName.c_str(), /*monitor=*/nullptr, /*share=*/nullptr);
OPENVDB_LOG_DEBUG_RUNTIME("created window " << std::hex << mWindow << std::dec
<< " from thread " << std::this_thread::get_id());
if (mWindow != nullptr) {
// Temporarily make the new window the current context, then create a font.
std::shared_ptr<GLFWwindow> curWindow(
glfwGetCurrentContext(), glfwMakeContextCurrent);
glfwMakeContextCurrent(mWindow);
BitmapFont13::initialize();
}
}
mCamera->setWindow(mWindow);
if (mWindow != nullptr) {
glfwSetKeyCallback(mWindow, keyCB);
glfwSetMouseButtonCallback(mWindow, mouseButtonCB);
glfwSetCursorPosCallback(mWindow, mousePosCB);
glfwSetScrollCallback(mWindow, mouseWheelCB);
glfwSetWindowSizeCallback(mWindow, windowSizeCB);
glfwSetWindowRefreshCallback(mWindow, windowRefreshCB);
}
return (mWindow != nullptr);
}
bool
ViewerImpl::isOpen() const
{
return (mWindow != nullptr);
}
// Set a flag so as to break out of the event loop on the next iteration.
// (Useful only if the event loop is running in a separate thread.)
void
ViewerImpl::interrupt()
{
mInterrupt = true;
if (mWindow) glfwSetWindowShouldClose(mWindow, true);
}
void
ViewerImpl::handleEvents()
{
glfwPollEvents();
}
void
ViewerImpl::close()
{
OPENVDB_LOG_DEBUG_RUNTIME("about to close window " << std::hex << mWindow << std::dec
<< " from thread " << std::this_thread::get_id());
mViewportModule.reset();
mRenderModules.clear();
mCamera->setWindow(nullptr);
GLFWwindow* win = mWindow;
mWindow = nullptr;
glfwDestroyWindow(win);
OPENVDB_LOG_DEBUG_RUNTIME("destroyed window " << std::hex << win << std::dec
<< " from thread " << std::this_thread::get_id());
}
////////////////////////////////////////
void
ViewerImpl::view(const openvdb::GridCPtrVec& gridList)
{
if (!isOpen()) return;
mGrids = gridList;
mGridIdx = size_t(-1);
mGridName.clear();
// Compute the combined bounding box of all the grids.
openvdb::BBoxd bbox(openvdb::Vec3d(0.0), openvdb::Vec3d(0.0));
if (!gridList.empty()) {
bbox = worldSpaceBBox(
gridList[0]->transform(), gridList[0]->evalActiveVoxelBoundingBox());
openvdb::Vec3d voxelSize = gridList[0]->voxelSize();
for (size_t n = 1; n < gridList.size(); ++n) {
bbox.expand(worldSpaceBBox(gridList[n]->transform(),
gridList[n]->evalActiveVoxelBoundingBox()));
voxelSize = minComponent(voxelSize, gridList[n]->voxelSize());
}
mClipBox->setStepSize(voxelSize);
}
mClipBox->setBBox(bbox);
// Prepare window for rendering.
glfwMakeContextCurrent(mWindow);
#if defined(_WIN32)
// This must come after glfwMakeContextCurrent
if (GLEW_OK != glewInit()) {
OPENVDB_LOG_ERROR("GLEW initialization failed");
}
#endif
{
// set up camera
openvdb::Vec3d extents = bbox.extents();
double maxExtent = std::max(extents[0], std::max(extents[1], extents[2]));
mCamera->setTarget(bbox.getCenter(), maxExtent);
mCamera->lookAtTarget();
mCamera->setSpeed();
}
swapBuffers();
setNeedsDisplay();
//////////
// Screen color
glClearColor(0.85f, 0.85f, 0.85f, 0.0f);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glPointSize(4);
glLineWidth(2);
//////////
// construct render modules
showNthGrid(/*n=*/0);
// main loop
size_t frame = 0;
double time = glfwGetTime();
glfwSwapInterval(1);
OPENVDB_LOG_DEBUG_RUNTIME("starting to render in window " << std::hex << mWindow << std::dec
<< " from thread " << std::this_thread::get_id());
mInterrupt = false;
for (bool stop = false; !stop; ) {
if (needsDisplay()) render();
// eval fps
++frame;
double elapsed = glfwGetTime() - time;
if (elapsed > 1.0) {
time = glfwGetTime();
setWindowTitle(/*fps=*/double(frame) / elapsed);
frame = 0;
}
// Swap front and back buffers
swapBuffers();
sleep(0.01/*sec*/);
// Exit if the Esc key is pressed or the window is closed.
handleEvents();
stop = (mInterrupt || glfwWindowShouldClose(mWindow));
}
if (glfwGetCurrentContext() == mWindow) { ///< @todo not thread-safe
// Detach this viewer's GL context.
glfwMakeContextCurrent(nullptr);
OPENVDB_LOG_DEBUG_RUNTIME("detached window " << std::hex << mWindow << std::dec
<< " from thread " << std::this_thread::get_id());
}
OPENVDB_LOG_DEBUG_RUNTIME("finished rendering in window " << std::hex << mWindow << std::dec
<< " from thread " << std::this_thread::get_id());
}
////////////////////////////////////////
void
ViewerImpl::resize(int width, int height)
{
if (mWindow) glfwSetWindowSize(mWindow, width, height);
}
////////////////////////////////////////
void
ViewerImpl::render()
{
if (mWindow == nullptr) return;
// Prepare window for rendering.
glfwMakeContextCurrent(mWindow);
mCamera->aim();
// draw scene
mViewportModule->render(); // ground plane.
mClipBox->render();
mClipBox->enableClipping();
for (size_t n = 0, N = mRenderModules.size(); n < N; ++n) {
mRenderModules[n]->render();
}
mClipBox->disableClipping();
// Render text
if (mShowInfo) {
BitmapFont13::enableFontRendering();
glColor3d(0.2, 0.2, 0.2);
int width, height;
glfwGetFramebufferSize(mWindow, &width, &height);
BitmapFont13::print(10, height - 13 - 10, mGridInfo);
BitmapFont13::print(10, height - 13 - 30, mTransformInfo);
BitmapFont13::print(10, height - 13 - 50, mTreeInfo);
// Indicate via their hotkeys which render modules are enabled.
std::string keys = "123";
for (auto n: {0, 1, 2}) { if (!mRenderModules[n]->visible()) keys[n] = ' '; }
BitmapFont13::print(width - 10 - 30, 10, keys);
glColor3d(0.75, 0.75, 0.75);
BitmapFont13::print(width - 10 - 30, 10, "123");
BitmapFont13::disableFontRendering();
}
}
////////////////////////////////////////
//static
void
ViewerImpl::sleep(double secs)
{
secs = fabs(secs);
int isecs = int(secs);
std::this_thread::sleep_for(std::chrono::seconds(isecs));
}
////////////////////////////////////////
//static
openvdb::BBoxd
ViewerImpl::worldSpaceBBox(const openvdb::math::Transform& xform, const openvdb::CoordBBox& bbox)
{
openvdb::Vec3d pMin = openvdb::Vec3d(std::numeric_limits<double>::max());
openvdb::Vec3d pMax = -pMin;
const openvdb::Coord& min = bbox.min();
const openvdb::Coord& max = bbox.max();
openvdb::Coord ijk;
// corner 1
openvdb::Vec3d ptn = xform.indexToWorld(min);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 2
ijk[0] = min.x();
ijk[1] = min.y();
ijk[2] = max.z();
ptn = xform.indexToWorld(ijk);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 3
ijk[0] = max.x();
ijk[1] = min.y();
ijk[2] = max.z();
ptn = xform.indexToWorld(ijk);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 4
ijk[0] = max.x();
ijk[1] = min.y();
ijk[2] = min.z();
ptn = xform.indexToWorld(ijk);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 5
ijk[0] = min.x();
ijk[1] = max.y();
ijk[2] = min.z();
ptn = xform.indexToWorld(ijk);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 6
ijk[0] = min.x();
ijk[1] = max.y();
ijk[2] = max.z();
ptn = xform.indexToWorld(ijk);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 7
ptn = xform.indexToWorld(max);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
// corner 8
ijk[0] = max.x();
ijk[1] = max.y();
ijk[2] = min.z();
ptn = xform.indexToWorld(ijk);
for (int i = 0; i < 3; ++i) {
if (ptn[i] < pMin[i]) pMin[i] = ptn[i];
if (ptn[i] > pMax[i]) pMax[i] = ptn[i];
}
return openvdb::BBoxd(pMin, pMax);
}
////////////////////////////////////////
void
ViewerImpl::updateCutPlanes(int wheelPos)
{
double speed = std::abs(mWheelPos - wheelPos);
if (mWheelPos < wheelPos) mClipBox->update(speed);
else mClipBox->update(-speed);
setNeedsDisplay();
}
////////////////////////////////////////
void
ViewerImpl::swapBuffers()
{
glfwSwapBuffers(mWindow);
}
////////////////////////////////////////
void
ViewerImpl::setWindowTitle(double fps)
{
std::ostringstream ss;
ss << mProgName << ": "
<< (mGridName.empty() ? std::string("OpenVDB") : mGridName)
<< " (" << (mGridIdx + 1) << " of " << mGrids.size() << ") @ "
<< std::setprecision(1) << std::fixed << fps << " fps";
if (mWindow) glfwSetWindowTitle(mWindow, ss.str().c_str());
}
////////////////////////////////////////
void
ViewerImpl::showPrevGrid()
{
if (const size_t numGrids = mGrids.size()) {
size_t idx = ((numGrids + mGridIdx) - 1) % numGrids;
showNthGrid(idx);
}
}
void
ViewerImpl::showNextGrid()
{
if (const size_t numGrids = mGrids.size()) {
size_t idx = (mGridIdx + 1) % numGrids;
showNthGrid(idx);
}
}
void
ViewerImpl::showNthGrid(size_t n)
{
if (mGrids.empty()) return;
n = n % mGrids.size();
if (n == mGridIdx) return;
mGridName = mGrids[n]->getName();
mGridIdx = n;
// save render settings
std::vector<bool> active(mRenderModules.size());
for (size_t i = 0, I = active.size(); i < I; ++i) {
active[i] = mRenderModules[i]->visible();
}
mRenderModules.clear();
mRenderModules.push_back(RenderModulePtr(new TreeTopologyModule(mGrids[n])));
mRenderModules.push_back(RenderModulePtr(new MeshModule(mGrids[n])));
mRenderModules.push_back(RenderModulePtr(new VoxelModule(mGrids[n])));
if (active.empty()) {
for (size_t i = 1, I = mRenderModules.size(); i < I; ++i) {
mRenderModules[i]->setVisible(false);
}
} else {
for (size_t i = 0, I = active.size(); i < I; ++i) {
mRenderModules[i]->setVisible(active[i]);
}
}
// Collect info
{
std::ostringstream ostrm;
std::string s = mGrids[n]->getName();
const openvdb::GridClass cls = mGrids[n]->getGridClass();
if (!s.empty()) ostrm << s << " / ";
ostrm << mGrids[n]->valueType() << " / ";
if (cls == openvdb::GRID_UNKNOWN) ostrm << " class unknown";
else ostrm << " " << openvdb::GridBase::gridClassToString(cls);
mGridInfo = ostrm.str();
}
{
openvdb::Coord dim = mGrids[n]->evalActiveVoxelDim();
std::ostringstream ostrm;
ostrm << dim[0] << " x " << dim[1] << " x " << dim[2]
<< " / voxel size " << std::setprecision(4) << mGrids[n]->voxelSize()[0]
<< " (" << mGrids[n]->transform().mapType() << ")";
mTransformInfo = ostrm.str();
}
{
std::ostringstream ostrm;
const openvdb::Index64 count = mGrids[n]->activeVoxelCount();
ostrm << openvdb::util::formattedInt(count)
<< " active voxel" << (count == 1 ? "" : "s");
mTreeInfo = ostrm.str();
}
{
if (mGrids[n]->isType<openvdb::points::PointDataGrid>()) {
const openvdb::points::PointDataGrid::ConstPtr points =
openvdb::gridConstPtrCast<openvdb::points::PointDataGrid>(mGrids[n]);
const openvdb::Index64 count = openvdb::points::pointCount(points->tree());
std::ostringstream ostrm;
ostrm << " / " << openvdb::util::formattedInt(count)
<< " point" << (count == 1 ? "" : "s");
mTreeInfo.append(ostrm.str());
}
}
setWindowTitle();
}
////////////////////////////////////////
void
ViewerImpl::keyCallback(int key, int action)
{
mCamera->keyCallback(key, action);
if (mWindow == nullptr) return;
const bool keyPress = (glfwGetKey(mWindow, key) == GLFW_PRESS);
/// @todo Should use "modifiers" argument to keyCB().
mShiftIsDown = glfwGetKey(mWindow, GLFW_KEY_LEFT_SHIFT);
mCtrlIsDown = glfwGetKey(mWindow, GLFW_KEY_LEFT_CONTROL);
if (keyPress) {
switch (key) {
case '1': case GLFW_KEY_KP_1:
toggleRenderModule(0);
break;
case '2': case GLFW_KEY_KP_2:
toggleRenderModule(1);
break;
case '3': case GLFW_KEY_KP_3:
toggleRenderModule(2);
break;
case 'c': case 'C':
mClipBox->reset();
break;
case 'h': case 'H': // center home
mCamera->lookAt(openvdb::Vec3d(0.0), 10.0);
break;
case 'g': case 'G': // center geometry
mCamera->lookAtTarget();
break;
case 'i': case 'I':
toggleInfoText();
break;
case GLFW_KEY_LEFT:
showPrevGrid();
break;
case GLFW_KEY_RIGHT:
showNextGrid();
break;
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(mWindow, true);
break;
}
}
switch (key) {
case 'x': case 'X':
mClipBox->activateXPlanes() = keyPress;
break;
case 'y': case 'Y':
mClipBox->activateYPlanes() = keyPress;
break;
case 'z': case 'Z':
mClipBox->activateZPlanes() = keyPress;
break;
}
mClipBox->shiftIsDown() = mShiftIsDown;
mClipBox->ctrlIsDown() = mCtrlIsDown;
setNeedsDisplay();
}
void
ViewerImpl::mouseButtonCallback(int button, int action)
{
mCamera->mouseButtonCallback(button, action);
mClipBox->mouseButtonCallback(button, action);
if (mCamera->needsDisplay()) setNeedsDisplay();
}
void
ViewerImpl::mousePosCallback(int x, int y)
{
bool handled = mClipBox->mousePosCallback(x, y);
if (!handled) mCamera->mousePosCallback(x, y);
if (mCamera->needsDisplay()) setNeedsDisplay();
}
void
ViewerImpl::mouseWheelCallback(int pos)
{
pos += mWheelPos;
if (mClipBox->isActive()) {
updateCutPlanes(pos);
} else {
mCamera->mouseWheelCallback(pos, mWheelPos);
if (mCamera->needsDisplay()) setNeedsDisplay();
}
mWheelPos = pos;
}
void
ViewerImpl::windowSizeCallback(int, int)
{
setNeedsDisplay();
}
void
ViewerImpl::windowRefreshCallback()
{
setNeedsDisplay();
}
////////////////////////////////////////
bool
ViewerImpl::needsDisplay()
{
if (mUpdates < 2) {
mUpdates += 1;
return true;
}
return false;
}
void
ViewerImpl::setNeedsDisplay()
{
mUpdates = 0;
}
void
ViewerImpl::toggleRenderModule(size_t n)
{
mRenderModules[n]->setVisible(!mRenderModules[n]->visible());
}
void
ViewerImpl::toggleInfoText()
{
mShowInfo = !mShowInfo;
}
} // namespace openvdb_viewer
|