├── Application ├── CMakeLists.txt ├── PerseusLibTest.cpp └── Utils │ └── Timer.h ├── CMakeLists.txt ├── CMakeModules ├── FindASSIMP.cmake ├── FindEigen3.cmake ├── FindFREEIMAGE.cmake ├── FindFreeImage.cmake ├── FindOpenCV2.cmake ├── FindPackageHandleStandardArgs.cmake ├── FindTinyXML2.cmake └── SetPlatformVars.cmake ├── Files ├── CameraCalibration │ ├── 900nc.cal │ └── Kinect.cal ├── Images │ ├── 248-LiveRGB.png │ └── Red.png ├── Masks │ ├── 248-ID-1-LiveImage.png │ ├── 248-ID-3-LiveImage.png │ ├── 248-LiveRGB.png │ ├── 480p_All_VideoMask.png │ ├── Red_Mask.png │ └── Red_Source.png ├── Models │ └── Renderer │ │ └── long.obj ├── Others │ └── heaviside.txt └── Results │ ├── OriginalResult │ ├── result0000.png │ ├── result0001.png │ ├── result0002.png │ └── result0003.png │ ├── posteriors.png │ ├── result0000.png │ ├── result0001.png │ ├── result0002.png │ └── result0003.png ├── PerseusLib ├── CMakeLists.txt ├── CUDA │ ├── CUDAConvolution.cu │ ├── CUDAConvolution.h │ ├── CUDADT.cu │ ├── CUDADT.h │ ├── CUDAData.h │ ├── CUDADefines.h │ ├── CUDAEF.cu │ ├── CUDAEF.h │ ├── CUDAEngine.cu │ ├── CUDAEngine.h │ ├── CUDARenderer.cu │ ├── CUDARenderer.h │ ├── CUDAScharr.cu │ ├── CUDAScharr.h │ ├── CUDAUtils.cu │ └── CUDAUtils.h ├── Objects │ ├── HistogramVarBin.h │ ├── ImageRender.h │ ├── IterationConfiguration.h │ ├── Object3D.h │ ├── Object3DParams.h │ ├── Pose3D.h │ ├── StepSize3D.h │ ├── View3D.h │ └── View3DParams.h ├── Optimiser │ ├── EFs │ │ ├── EFStandard.cpp │ │ ├── EFStandard.h │ │ └── IEnergyFunction.h │ └── Engine │ │ ├── OptimisationEngine.cpp │ │ └── OptimisationEngine.h ├── Others │ └── PerseusLibDefines.h ├── PWP3DConfig.cmake.in ├── PWP3DConfigVersion.cmake.in ├── PerseusLib.h ├── Primitives │ ├── ImagePerseus.h │ ├── PixelUCHAR4.h │ ├── Vector2D.h │ ├── Vector3D.h │ └── Vector4D.h ├── Renderer │ ├── Engine │ │ ├── DrawingEngine.cpp │ │ ├── DrawingEngine.h │ │ ├── DrawingPrimitives.cpp │ │ └── DrawingPrimitives.h │ ├── Model │ │ ├── Model.cpp │ │ ├── Model.h │ │ ├── ModelFace.h │ │ ├── ModelGroup.cpp │ │ ├── ModelGroup.h │ │ ├── ModelH.cpp │ │ ├── ModelH.h │ │ ├── ModelVertex.h │ │ └── ModelVertexH.h │ ├── Objects │ │ ├── Renderer3DObject.h │ │ └── Renderer3DView.h │ ├── Primitives │ │ ├── Camera3D.h │ │ ├── Quaternion.cpp │ │ └── Quaternion.h │ └── Transforms │ │ ├── CameraCoordinateTransform.cpp │ │ ├── CameraCoordinateTransform.h │ │ ├── CoordinateTransform.cpp │ │ ├── CoordinateTransform.h │ │ └── ObjectCoordinateTransform.h ├── Utils │ ├── Debug.h │ ├── FileUtils.cpp │ ├── FileUtils.h │ ├── Heaviside.h │ ├── HistogramEngine.cpp │ ├── HistogramEngine.h │ ├── ImageUtils.cpp │ ├── ImageUtils.h │ ├── MathUtils.cpp │ ├── MathUtils.h │ ├── VisualisationEngine.cpp │ └── VisualisationEngine.h └── config.h.in └── README.md /Application/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.8 ) 2 | 3 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra ") 4 | if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") 6 | endif() 7 | 8 | find_package( PWP3D REQUIRED) 9 | include_directories( ${PWP3D_INCLUDE_DIRS}) 10 | add_executable( PWP3DAPP PerseusLibTest.cpp ) 11 | target_link_libraries(PWP3DAPP ${PWP3D_LIBRARIES}) 12 | 13 | 14 | -------------------------------------------------------------------------------- /Application/PerseusLibTest.cpp: -------------------------------------------------------------------------------- 1 | #include "../PerseusLib/PerseusLib.h" 2 | 3 | #include "Utils/Timer.h" 4 | #include 5 | 6 | using namespace Perseus::Utils; 7 | 8 | int main(void) 9 | { 10 | // std::string sModelPath = "/Users/luma/Code/Luma/PWP3D/Files/Models/Renderer/long.obj"; 11 | // std::string sSrcImage = "/Users/luma/Code/Luma/PWP3D/Files/Images/Red.png"; 12 | // std::string sCameraMatrix = "/Users/luma/Code/Luma/PWP3D/Files/CameraCalibration/900nc.cal"; 13 | // std::string sTargetMask = "/Users/luma/Code/Luma/PWP3D/Files/Masks/480p_All_VideoMask.png"; 14 | // std::string sHistSrc = "/Users/luma/Code/Luma/PWP3D/Files/Masks/Red_Source.png"; 15 | // std::string sHistMask = "/Users/luma/Code/Luma/PWP3D/Files/Masks/Red_Mask.png"; 16 | 17 | 18 | // blue car demo 19 | // std::string sModelPath = "/Users/luma/Code/DataSet/Mesh/BlueCar.obj"; 20 | // std::string sSrcImage = "/Users/luma/Code/Luma/PWP3D/Files/Images/248-LiveRGB.png"; 21 | // std::string sCameraMatrix = "/Users/luma/Code/Luma/PWP3D/Files/CameraCalibration/Kinect.cal"; 22 | // std::string sTargetMask = "/Users/luma/Code/Luma/PWP3D/Files/Masks/480p_All_VideoMask.png"; 23 | // std::string sHistSrc = "/Users/luma/Code/Luma/PWP3D/Files/Images/248-LiveRGB.png"; 24 | // std::string sHistMask = "/Users/luma/Code/Luma/PWP3D/Files/Masks/248-ID-3-LiveImage.png"; 25 | 26 | // red can demo 27 | std::string sModelPath = "/Users/luma/Code/DataSet/Mesh/RedCan.obj"; 28 | std::string sSrcImage = "/Users/luma/Code/Luma/PWP3D/Files/Images/248-LiveRGB.png"; 29 | std::string sCameraMatrix = "/Users/luma/Code/Luma/PWP3D/Files/CameraCalibration/Kinect.cal"; 30 | std::string sTargetMask = "/Users/luma/Code/Luma/PWP3D/Files/Masks/480p_All_VideoMask.png"; 31 | std::string sHistSrc = "/Users/luma/Code/Luma/PWP3D/Files/Images/248-LiveRGB.png"; 32 | std::string sHistMask = "/Users/luma/Code/Luma/PWP3D/Files/Masks/248-ID-1-LiveImage.png"; 33 | 34 | // --------------------------------------------------------------------------- 35 | char str[100]; 36 | int i; 37 | 38 | int width = 640, height = 480; 39 | int viewCount = 1, objectCount = 1; 40 | int objectId = 0, viewIdx = 0, objectIdx = 0; 41 | 42 | Timer t; 43 | 44 | //result visualisation 45 | ImageUChar4* ResultImage = new ImageUChar4(width, height); 46 | 47 | // --------------------------------------------------------------------------- 48 | //input image 49 | //camera = 24 bit colour rgb 50 | ImageUChar4* camera = new ImageUChar4(width, height); 51 | ImageUtils::Instance()->LoadImageFromFile(camera, (char*)sSrcImage.c_str()); 52 | 53 | //objects allocation + initialisation: 3d model in obj required 54 | Object3D **objects = new Object3D*[objectCount]; 55 | 56 | std::cout<<"\n==[APP] Init Model =="<LoadImageFromFile(views[viewIdx]->videoMask, 73 | (char*)sTargetMask.c_str()); 74 | 75 | ImageUtils::Instance()->LoadImageFromFile(objects[objectIdx]->histSources[viewIdx], 76 | (char*)sHistSrc.c_str()); 77 | 78 | ImageUtils::Instance()->LoadImageFromFile(objects[objectIdx]->histMasks[viewIdx], 79 | (char*)sHistMask.c_str(), objectIdx+1); 80 | 81 | HistogramEngine::Instance()->UpdateVarBinHistogram( 82 | objects[objectIdx], views[viewIdx], objects[objectIdx]->histSources[viewIdx], 83 | objects[objectIdx]->histMasks[viewIdx], views[viewIdx]->videoMask); 84 | 85 | 86 | // --------------------------------------------------------------------------- 87 | //iteration configuration for one object 88 | IterationConfiguration *iterConfig = new IterationConfiguration(); 89 | iterConfig->width = width; iterConfig->height = height; 90 | iterConfig->iterViewIds[viewIdx] = 0; 91 | iterConfig->iterObjectCount[viewIdx] = 1; 92 | iterConfig->levelSetBandSize = 8; 93 | iterConfig->iterObjectIds[viewIdx][objectIdx] = 0; 94 | iterConfig->iterViewCount = 1; 95 | iterConfig->iterCount = 1; 96 | 97 | //step size per object and view 98 | objects[objectIdx]->stepSize[viewIdx] = new StepSize3D(0.2f, 0.5f, 0.5f, 10.0f); 99 | 100 | //initial pose per object and view 101 | // Notice the input pose here is angle, not radians for the rotation part 102 | // objects[objectIdx]->initialPose[viewIdx]->SetFrom( 103 | // -1.98f, -2.90f, 37.47f, -40.90f, -207.77f, 27.48f); 104 | 105 | // for blue car demo 106 | // objects[objectIdx]->initialPose[viewIdx]->SetFrom( -3.0f,-4.5f,28.f, -220.90f, -207.77f, 87.48f); 107 | 108 | // for red can demo 109 | objects[objectIdx]->initialPose[viewIdx]->SetFrom( 110 | 1.0f, 3.0f, 30.f, 180.f, 80.f, 60.f); 111 | 112 | //primary initilisation 113 | OptimisationEngine::Instance()->Initialise(width, height); 114 | 115 | //register camera image with main engine 116 | OptimisationEngine::Instance()->RegisterViewImage(views[viewIdx], camera); 117 | 118 | // --------------------------------------------------------------------------- 119 | std::cout<<"\n==[APP] Rendering object initial pose.. =="<GetImage( 121 | ResultImage, GETIMAGE_PROXIMITY, 122 | objects[objectIdx], views[viewIdx], 123 | objects[objectIdx]->initialPose[viewIdx]); 124 | 125 | cv::Mat ResultMat(height,width,CV_8UC4, ResultImage->pixels); 126 | cv::imshow("initial pose", ResultMat); 127 | cv::waitKey(1000); 128 | 129 | std::cout<<"[App] Finish Rendered object initial pose."<useCUDAEF = true; 137 | iterConfig->useCUDARender = true; 138 | break; 139 | case 1: 140 | iterConfig->useCUDAEF = false; 141 | iterConfig->useCUDARender = true; 142 | break; 143 | case 2: 144 | iterConfig->useCUDAEF = true; 145 | iterConfig->useCUDARender = false; 146 | break; 147 | case 3: 148 | iterConfig->useCUDAEF = false; 149 | iterConfig->useCUDARender = false; 150 | break; 151 | } 152 | 153 | printf("======= mode: useCUDAAEF: %d, use CUDARender %d ========;\n", 154 | iterConfig->useCUDAEF, iterConfig->useCUDARender); 155 | 156 | sprintf(str, "/Users/luma/Code/Luma/PWP3D/Files/Results/result%04d.png", i); 157 | 158 | //main processing 159 | t.restart(); 160 | OptimisationEngine::Instance()->Minimise(objects, views, iterConfig); 161 | t.check("Iteration"); 162 | 163 | //result plot 164 | VisualisationEngine::Instance()->GetImage( 165 | ResultImage, GETIMAGE_PROXIMITY, 166 | objects[objectIdx], views[viewIdx], objects[objectIdx]->pose[viewIdx]); 167 | 168 | //result save to file 169 | // ImageUtils::Instance()->SaveImageToFile(result, str); 170 | cv::Mat ResultMat(height,width,CV_8UC4, ResultImage->pixels); 171 | cv::imshow("result", ResultMat); 172 | cv::waitKey(2000); 173 | 174 | printf("final pose result %f %f %f %f %f %f %f\n\n", 175 | objects[objectIdx]->pose[viewIdx]->translation->x, 176 | objects[objectIdx]->pose[viewIdx]->translation->y, 177 | objects[objectIdx]->pose[viewIdx]->translation->z, 178 | objects[objectIdx]->pose[viewIdx]->rotation->vector4d.x, 179 | objects[objectIdx]->pose[viewIdx]->rotation->vector4d.y, 180 | objects[objectIdx]->pose[viewIdx]->rotation->vector4d.z, 181 | objects[objectIdx]->pose[viewIdx]->rotation->vector4d.w); 182 | } 183 | 184 | //posteriors plot 185 | sprintf(str, "/Users/luma/Code/Luma/PWP3D/Files/Results/posteriors.png"); 186 | VisualisationEngine::Instance()->GetImage( 187 | ResultImage, GETIMAGE_POSTERIORS, 188 | objects[objectIdx], views[viewIdx], objects[objectIdx]->pose[viewIdx]); 189 | 190 | ImageUtils::Instance()->SaveImageToFile(ResultImage, str); 191 | 192 | //primary engine destructor 193 | OptimisationEngine::Instance()->Shutdown(); 194 | 195 | for (i = 0; i 5 | #include 6 | #include 7 | #include 8 | 9 | #ifndef MSGOUT 10 | #define MSGOUT std::cout// dbg::out() 11 | #endif 12 | 13 | namespace Perseus 14 | { 15 | namespace Utils 16 | { 17 | class Timer 18 | { 19 | friend std::ostream& operator<<(std::ostream& os, Timer& t); 20 | 21 | private: 22 | bool running; 23 | clock_t start_clock; 24 | time_t start_time; 25 | double acc_time; 26 | 27 | public: 28 | // 'running' is initially false. A Timer needs to be explicitly started 29 | // using 'start' or 'restart' 30 | Timer() : running(false), start_clock(0), start_time(0), acc_time(0) { } 31 | 32 | void start(const char* msg = 0); 33 | void restart(const char* msg = 0); 34 | void stop(const char* msg = 0); 35 | void check(const char* msg = 0); 36 | void check(const char* msg, int msg_count); 37 | 38 | float elapsed_time(); 39 | }; // class Timer 40 | 41 | //=========================================================================== 42 | // Return the total time that the Timer has been in the "running" 43 | // state since it was first "started" or last "restarted". For 44 | // "short" time periods (less than an hour), the actual cpu time 45 | // used is reported instead of the elapsed time. 46 | 47 | inline float Timer::elapsed_time() 48 | { 49 | time_t acc_sec = time(0) - start_time; 50 | if (acc_sec < 3600) 51 | return (clock() - start_clock) / (1.0f * CLOCKS_PER_SEC); 52 | else 53 | return (1.0f * acc_sec); 54 | 55 | } // Timer::elapsed_time 56 | 57 | //=========================================================================== 58 | // Start a Timer. If it is already running, let it continue running. 59 | // Print an optional message. 60 | 61 | inline void Timer::start(const char* msg) 62 | { 63 | // Print an optional message, something like "Starting Timer t"; 64 | if (msg) MSGOUT << msg << std::endl; 65 | 66 | // Return immediately if the Timer is already running 67 | if (running) return; 68 | 69 | // Set Timer status to running and set the start time 70 | running = true; 71 | start_clock = clock(); 72 | start_time = time(0); 73 | 74 | } // Timer::start 75 | 76 | //=========================================================================== 77 | // Turn the Timer off and start it again from 0. Print an optional message. 78 | 79 | inline void Timer::restart(const char* msg) 80 | { 81 | // Print an optional message, something like "Restarting Timer t"; 82 | if (msg) MSGOUT << msg << std::endl; 83 | 84 | // Set Timer status to running, reset accumulated time, and set start time 85 | running = true; 86 | acc_time = 0; 87 | start_clock = clock(); 88 | start_time = time(0); 89 | 90 | } // Timer::restart 91 | 92 | //=========================================================================== 93 | // Stop the Timer and print an optional message. 94 | 95 | inline void Timer::stop(const char* msg) 96 | { 97 | // Print an optional message, something like "Stopping Timer t"; 98 | if (msg) MSGOUT << msg << std::endl; 99 | 100 | // Compute accumulated running time and set Timer status to not running 101 | if (running) acc_time += elapsed_time(); 102 | running = false; 103 | 104 | } // Timer::stop 105 | 106 | //=========================================================================== 107 | // Print out an optional message followed by the current Timer timing. 108 | 109 | inline void Timer::check(const char* msg) 110 | { 111 | std::string s; 112 | // Print an optional message, something like "Checking Timer t"; 113 | if (msg) MSGOUT << msg << " : "; 114 | 115 | MSGOUT << "Time [" << std::setiosflags(std::ios::fixed) 116 | << std::setprecision(4) 117 | << acc_time + (running ? elapsed_time() : 0) << "] seconds\n"; 118 | } // Timer::check 119 | 120 | inline void Timer::check(const char* msg, int msg_count) 121 | { 122 | std::string s; 123 | // Print an optional message, something like "Checking Timer t"; 124 | if (msg) MSGOUT << msg << ":"; 125 | 126 | MSGOUT << msg_count << ": " << "Time [" << std::setiosflags(std::ios::fixed) 127 | << std::setprecision(4) 128 | << acc_time + (running ? elapsed_time() : 0) << "] seconds\n"; 129 | } // Timer::check 130 | 131 | //=========================================================================== 132 | // Allow Timers to be printed to ostreams using the syntax 'os << t' 133 | // for an ostream 'os' and a Timer 't'. For example, "cout << t" will 134 | // print out the total amount of time 't' has been "running". 135 | 136 | inline std::ostream& operator<<(std::ostream& os, Timer& t) 137 | { 138 | os << std::setprecision(4) << std::setiosflags(std::ios::fixed) 139 | << t.acc_time + (t.running ? t.elapsed_time() : 0); 140 | return os; 141 | } 142 | } 143 | } 144 | 145 | //=========================================================================== 146 | 147 | #endif // Timer_H 148 | 149 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.8 ) 2 | 3 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/") 4 | 5 | # Build Configures 6 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall ") 7 | if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang") 8 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") 9 | endif() 10 | 11 | # Platform configuration vars 12 | include(SetPlatformVars) 13 | 14 | # Overide with cmake -DCMAKE_BUILD_TYPE=Debug {dir} 15 | if( NOT CMAKE_BUILD_TYPE ) 16 | set( CMAKE_BUILD_TYPE Release ) 17 | endif() 18 | 19 | option(BUILD_SHARED_LIBS "Build Shared Library" ON) 20 | add_subdirectory(PerseusLib) 21 | 22 | option(BUILD_PWP3D_APP "Build PWP3D App" OFF) 23 | if(BUILD_PWP3D_APP) 24 | add_subdirectory(Application) 25 | endif() 26 | 27 | -------------------------------------------------------------------------------- /CMakeModules/FindASSIMP.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find Assimp 2 | # Once done, this will define 3 | # 4 | # ASSIMP_FOUND - system has Assimp 5 | # ASSIMP_INCLUDE_DIR - the Assimp include directories 6 | # ASSIMP_LIBRARIES - link these to use Assimp 7 | 8 | FIND_PATH( ASSIMP_INCLUDE_DIR assimp/mesh.h 9 | /usr/include 10 | /usr/local/include 11 | /opt/local/include 12 | ) 13 | 14 | FIND_LIBRARY( ASSIMP_LIBRARY assimp 15 | /usr/lib64 16 | /usr/lib 17 | /usr/local/lib 18 | /opt/local/lib 19 | ) 20 | 21 | IF(ASSIMP_INCLUDE_DIR AND ASSIMP_LIBRARY) 22 | SET( ASSIMP_FOUND TRUE ) 23 | SET( ASSIMP_LIBRARIES ${ASSIMP_LIBRARY} ) 24 | ENDIF(ASSIMP_INCLUDE_DIR AND ASSIMP_LIBRARY) 25 | 26 | IF(ASSIMP_FOUND) 27 | IF(NOT ASSIMP_FIND_QUIETLY) 28 | MESSAGE(STATUS "Found ASSIMP: ${ASSIMP_LIBRARY}") 29 | ENDIF(NOT ASSIMP_FIND_QUIETLY) 30 | ELSE(ASSIMP_FOUND) 31 | IF(ASSIMP_FIND_REQUIRED) 32 | MESSAGE(FATAL_ERROR "Could not find libASSIMP") 33 | ENDIF(ASSIMP_FIND_REQUIRED) 34 | ENDIF(ASSIMP_FOUND) 35 | -------------------------------------------------------------------------------- /CMakeModules/FindEigen3.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find Eigen3 lib 2 | # 3 | # This module supports requiring a minimum version, e.g. you can do 4 | # find_package(Eigen3 3.1.2) 5 | # to require version 3.1.2 or newer of Eigen3. 6 | # 7 | # Once done this will define 8 | # 9 | # EIGEN3_FOUND - system has eigen lib with correct version 10 | # EIGEN3_INCLUDE_DIR - the eigen include directory 11 | # EIGEN3_VERSION - eigen version 12 | 13 | # Copyright (c) 2006, 2007 Montel Laurent, 14 | # Copyright (c) 2008, 2009 Gael Guennebaud, 15 | # Copyright (c) 2009 Benoit Jacob 16 | # Redistribution and use is allowed according to the terms of the 2-clause BSD license. 17 | 18 | if(NOT Eigen3_FIND_VERSION) 19 | if(NOT Eigen3_FIND_VERSION_MAJOR) 20 | set(Eigen3_FIND_VERSION_MAJOR 2) 21 | endif(NOT Eigen3_FIND_VERSION_MAJOR) 22 | if(NOT Eigen3_FIND_VERSION_MINOR) 23 | set(Eigen3_FIND_VERSION_MINOR 91) 24 | endif(NOT Eigen3_FIND_VERSION_MINOR) 25 | if(NOT Eigen3_FIND_VERSION_PATCH) 26 | set(Eigen3_FIND_VERSION_PATCH 0) 27 | endif(NOT Eigen3_FIND_VERSION_PATCH) 28 | 29 | set(Eigen3_FIND_VERSION "${Eigen3_FIND_VERSION_MAJOR}.${Eigen3_FIND_VERSION_MINOR}.${Eigen3_FIND_VERSION_PATCH}") 30 | endif(NOT Eigen3_FIND_VERSION) 31 | 32 | macro(_eigen3_check_version) 33 | file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header) 34 | 35 | string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match "${_eigen3_version_header}") 36 | set(EIGEN3_WORLD_VERSION "${CMAKE_MATCH_1}") 37 | string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen3_major_version_match "${_eigen3_version_header}") 38 | set(EIGEN3_MAJOR_VERSION "${CMAKE_MATCH_1}") 39 | string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen3_minor_version_match "${_eigen3_version_header}") 40 | set(EIGEN3_MINOR_VERSION "${CMAKE_MATCH_1}") 41 | 42 | set(EIGEN3_VERSION ${EIGEN3_WORLD_VERSION}.${EIGEN3_MAJOR_VERSION}.${EIGEN3_MINOR_VERSION}) 43 | if(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION}) 44 | set(EIGEN3_VERSION_OK FALSE) 45 | else(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION}) 46 | set(EIGEN3_VERSION_OK TRUE) 47 | endif(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION}) 48 | 49 | if(NOT EIGEN3_VERSION_OK) 50 | 51 | message(STATUS "Eigen3 version ${EIGEN3_VERSION} found in ${EIGEN3_INCLUDE_DIR}, " 52 | "but at least version ${Eigen3_FIND_VERSION} is required") 53 | endif(NOT EIGEN3_VERSION_OK) 54 | endmacro(_eigen3_check_version) 55 | 56 | if (EIGEN3_INCLUDE_DIR) 57 | 58 | # in cache already 59 | _eigen3_check_version() 60 | set(EIGEN3_FOUND ${EIGEN3_VERSION_OK}) 61 | 62 | else (EIGEN3_INCLUDE_DIR) 63 | 64 | find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library 65 | PATHS 66 | ${CMAKE_INSTALL_PREFIX}/include 67 | ${KDE4_INCLUDE_DIR} 68 | PATH_SUFFIXES eigen3 eigen 69 | ) 70 | 71 | if(EIGEN3_INCLUDE_DIR) 72 | _eigen3_check_version() 73 | endif(EIGEN3_INCLUDE_DIR) 74 | 75 | include(FindPackageHandleStandardArgs) 76 | find_package_handle_standard_args(Eigen3 DEFAULT_MSG EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK) 77 | 78 | mark_as_advanced(EIGEN3_INCLUDE_DIR) 79 | 80 | endif(EIGEN3_INCLUDE_DIR) 81 | 82 | message(STATUS "EIGEN3_INCLUDE_DIR: ${EIGEN3_INCLUDE_DIR}") 83 | message(STATUS "EIGEN3_LIBRARIES: ${EIGEN3_LIBRARIES}") 84 | -------------------------------------------------------------------------------- /CMakeModules/FindFREEIMAGE.cmake: -------------------------------------------------------------------------------- 1 | # Find the FreeImage library. 2 | # 3 | # This module defines 4 | # FREEIMAGE_FOUND - True if FREEIMAGE was found. 5 | # FREEIMAGE_INCLUDE_DIRS - Include directories for FREEIMAGE headers. 6 | # FREEIMAGE_LIBRARIES - Libraries for FREEIMAGE. 7 | # 8 | # To specify an additional directory to search, set FREEIMAGE_ROOT. 9 | # 10 | # Copyright (c) 2010, Ewen Cheslack-Postava 11 | # Based on FindSQLite3.cmake by: 12 | # Copyright (c) 2006, Jaroslaw Staniek, 13 | # Extended by Siddhartha Chaudhuri, 2008. 14 | # 15 | # Redistribution and use is allowed according to the terms of the BSD license. 16 | # 17 | 18 | SET(FREEIMAGE_FOUND FALSE) 19 | SET(FREEIMAGE_INCLUDE_DIRS) 20 | SET(FREEIMAGE_LIBRARIES) 21 | 22 | SET(SEARCH_PATHS 23 | $ENV{ProgramFiles}/freeimage/include 24 | $ENV{SystemDrive}/freeimage/include 25 | $ENV{ProgramFiles}/freeimage 26 | $ENV{SystemDrive}/freeimage 27 | ) 28 | IF(FREEIMAGE_ROOT) 29 | SET(SEARCH_PATHS 30 | ${FREEIMAGE_ROOT} 31 | ${FREEIMAGE_ROOT}/include 32 | ${SEARCH_PATHS} 33 | ) 34 | ENDIF() 35 | 36 | FIND_PATH(FREEIMAGE_INCLUDE_DIRS 37 | NAMES FreeImage.h 38 | PATHS ${SEARCH_PATHS} 39 | NO_DEFAULT_PATH) 40 | IF(NOT FREEIMAGE_INCLUDE_DIRS) # now look in system locations 41 | FIND_PATH(FREEIMAGE_INCLUDE_DIRS NAMES FreeImage.h) 42 | ENDIF(NOT FREEIMAGE_INCLUDE_DIRS) 43 | 44 | SET(FREEIMAGE_LIBRARY_DIRS) 45 | IF(FREEIMAGE_ROOT) 46 | SET(FREEIMAGE_LIBRARY_DIRS ${FREEIMAGE_ROOT}) 47 | IF(EXISTS "${FREEIMAGE_ROOT}/lib") 48 | SET(FREEIMAGE_LIBRARY_DIRS ${FREEIMAGE_LIBRARY_DIRS} ${FREEIMAGE_ROOT}/lib) 49 | ENDIF() 50 | IF(EXISTS "${FREEIMAGE_ROOT}/lib/static") 51 | SET(FREEIMAGE_LIBRARY_DIRS ${FREEIMAGE_LIBRARY_DIRS} ${FREEIMAGE_ROOT}/lib/static) 52 | ENDIF() 53 | ENDIF() 54 | 55 | # FREEIMAGE 56 | # Without system dirs 57 | FIND_LIBRARY(FREEIMAGE_LIBRARY 58 | NAMES freeimage 59 | PATHS ${FREEIMAGE_LIBRARY_DIRS} 60 | NO_DEFAULT_PATH 61 | ) 62 | IF(NOT FREEIMAGE_LIBRARY) # now look in system locations 63 | FIND_LIBRARY(FREEIMAGE_LIBRARY NAMES freeimage) 64 | ENDIF(NOT FREEIMAGE_LIBRARY) 65 | 66 | SET(FREEIMAGE_LIBRARIES) 67 | IF(FREEIMAGE_LIBRARY) 68 | SET(FREEIMAGE_LIBRARIES ${FREEIMAGE_LIBRARY}) 69 | ENDIF() 70 | 71 | IF(FREEIMAGE_INCLUDE_DIRS AND FREEIMAGE_LIBRARIES) 72 | SET(FREEIMAGE_FOUND TRUE) 73 | IF(NOT FREEIMAGE_FIND_QUIETLY) 74 | MESSAGE(STATUS "Found FreeImage: headers at ${FREEIMAGE_INCLUDE_DIRS}, libraries at ${FREEIMAGE_LIBRARY_DIRS} :: ${FREEIMAGE_LIBRARIES}") 75 | ENDIF(NOT FREEIMAGE_FIND_QUIETLY) 76 | ELSE(FREEIMAGE_INCLUDE_DIRS AND FREEIMAGE_LIBRARIES) 77 | SET(FREEIMAGE_FOUND FALSE) 78 | IF(FREEIMAGE_FIND_REQUIRED) 79 | MESSAGE(STATUS "FreeImage not found") 80 | ENDIF(FREEIMAGE_FIND_REQUIRED) 81 | ENDIF(FREEIMAGE_INCLUDE_DIRS AND FREEIMAGE_LIBRARIES) 82 | 83 | MARK_AS_ADVANCED(FREEIMAGE_INCLUDE_DIRS FREEIMAGE_LIBRARIES) 84 | -------------------------------------------------------------------------------- /CMakeModules/FindFreeImage.cmake: -------------------------------------------------------------------------------- 1 | # Find the FreeImage library. 2 | # 3 | # This module defines 4 | # FREEIMAGE_FOUND - True if FREEIMAGE was found. 5 | # FREEIMAGE_INCLUDE_DIRS - Include directories for FREEIMAGE headers. 6 | # FREEIMAGE_LIBRARIES - Libraries for FREEIMAGE. 7 | # 8 | # To specify an additional directory to search, set FREEIMAGE_ROOT. 9 | # 10 | # Copyright (c) 2010, Ewen Cheslack-Postava 11 | # Based on FindSQLite3.cmake by: 12 | # Copyright (c) 2006, Jaroslaw Staniek, 13 | # Extended by Siddhartha Chaudhuri, 2008. 14 | # 15 | # Redistribution and use is allowed according to the terms of the BSD license. 16 | # 17 | 18 | SET(FREEIMAGE_FOUND FALSE) 19 | SET(FREEIMAGE_INCLUDE_DIRS) 20 | SET(FREEIMAGE_LIBRARIES) 21 | 22 | SET(SEARCH_PATHS 23 | $ENV{ProgramFiles}/freeimage/include 24 | $ENV{SystemDrive}/freeimage/include 25 | $ENV{ProgramFiles}/freeimage 26 | $ENV{SystemDrive}/freeimage 27 | ) 28 | IF(FREEIMAGE_ROOT) 29 | SET(SEARCH_PATHS 30 | ${FREEIMAGE_ROOT} 31 | ${FREEIMAGE_ROOT}/include 32 | ${SEARCH_PATHS} 33 | ) 34 | ENDIF() 35 | 36 | FIND_PATH(FREEIMAGE_INCLUDE_DIRS 37 | NAMES FreeImage.h 38 | PATHS ${SEARCH_PATHS} 39 | NO_DEFAULT_PATH) 40 | IF(NOT FREEIMAGE_INCLUDE_DIRS) # now look in system locations 41 | FIND_PATH(FREEIMAGE_INCLUDE_DIRS NAMES FreeImage.h) 42 | ENDIF(NOT FREEIMAGE_INCLUDE_DIRS) 43 | 44 | SET(FREEIMAGE_LIBRARY_DIRS) 45 | IF(FREEIMAGE_ROOT) 46 | SET(FREEIMAGE_LIBRARY_DIRS ${FREEIMAGE_ROOT}) 47 | IF(EXISTS "${FREEIMAGE_ROOT}/lib") 48 | SET(FREEIMAGE_LIBRARY_DIRS ${FREEIMAGE_LIBRARY_DIRS} ${FREEIMAGE_ROOT}/lib) 49 | ENDIF() 50 | IF(EXISTS "${FREEIMAGE_ROOT}/lib/static") 51 | SET(FREEIMAGE_LIBRARY_DIRS ${FREEIMAGE_LIBRARY_DIRS} ${FREEIMAGE_ROOT}/lib/static) 52 | ENDIF() 53 | ENDIF() 54 | 55 | # FREEIMAGE 56 | # Without system dirs 57 | FIND_LIBRARY(FREEIMAGE_LIBRARY 58 | NAMES freeimage 59 | PATHS ${FREEIMAGE_LIBRARY_DIRS} 60 | NO_DEFAULT_PATH 61 | ) 62 | IF(NOT FREEIMAGE_LIBRARY) # now look in system locations 63 | FIND_LIBRARY(FREEIMAGE_LIBRARY NAMES freeimage) 64 | ENDIF(NOT FREEIMAGE_LIBRARY) 65 | 66 | SET(FREEIMAGE_LIBRARIES) 67 | IF(FREEIMAGE_LIBRARY) 68 | SET(FREEIMAGE_LIBRARIES ${FREEIMAGE_LIBRARY}) 69 | ENDIF() 70 | 71 | IF(FREEIMAGE_INCLUDE_DIRS AND FREEIMAGE_LIBRARIES) 72 | SET(FREEIMAGE_FOUND TRUE) 73 | IF(NOT FREEIMAGE_FIND_QUIETLY) 74 | MESSAGE(STATUS "Found FreeImage: headers at ${FREEIMAGE_INCLUDE_DIRS}, libraries at ${FREEIMAGE_LIBRARY_DIRS} :: ${FREEIMAGE_LIBRARIES}") 75 | ENDIF(NOT FREEIMAGE_FIND_QUIETLY) 76 | ELSE(FREEIMAGE_INCLUDE_DIRS AND FREEIMAGE_LIBRARIES) 77 | SET(FREEIMAGE_FOUND FALSE) 78 | IF(FREEIMAGE_FIND_REQUIRED) 79 | MESSAGE(STATUS "FreeImage not found") 80 | ENDIF(FREEIMAGE_FIND_REQUIRED) 81 | ENDIF(FREEIMAGE_INCLUDE_DIRS AND FREEIMAGE_LIBRARIES) 82 | 83 | MARK_AS_ADVANCED(FREEIMAGE_INCLUDE_DIRS FREEIMAGE_LIBRARIES) 84 | -------------------------------------------------------------------------------- /CMakeModules/FindPackageHandleStandardArgs.cmake: -------------------------------------------------------------------------------- 1 | # FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME (DEFAULT_MSG|"Custom failure message") VAR1 ... ) 2 | # This macro is intended to be used in FindXXX.cmake modules files. 3 | # It handles the REQUIRED and QUIET argument to FIND_PACKAGE() and 4 | # it also sets the _FOUND variable. 5 | # The package is found if all variables listed are TRUE. 6 | # Example: 7 | # 8 | # FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2 DEFAULT_MSG LIBXML2_LIBRARIES LIBXML2_INCLUDE_DIR) 9 | # 10 | # LibXml2 is considered to be found, if both LIBXML2_LIBRARIES and 11 | # LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to TRUE. 12 | # If it is not found and REQUIRED was used, it fails with FATAL_ERROR, 13 | # independent whether QUIET was used or not. 14 | # If it is found, the location is reported using the VAR1 argument, so 15 | # here a message "Found LibXml2: /usr/lib/libxml2.so" will be printed out. 16 | # If the second argument is DEFAULT_MSG, the message in the failure case will 17 | # be "Could NOT find LibXml2", if you don't like this message you can specify 18 | # your own custom failure message there. 19 | 20 | MACRO(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FAIL_MSG _VAR1 ) 21 | 22 | IF("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") 23 | IF (${_NAME}_FIND_REQUIRED) 24 | SET(_FAIL_MESSAGE "Could not find REQUIRED package ${_NAME}") 25 | ELSE (${_NAME}_FIND_REQUIRED) 26 | SET(_FAIL_MESSAGE "Could not find OPTIONAL package ${_NAME}") 27 | ENDIF (${_NAME}_FIND_REQUIRED) 28 | ELSE("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") 29 | SET(_FAIL_MESSAGE "${_FAIL_MSG}") 30 | ENDIF("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") 31 | 32 | STRING(TOUPPER ${_NAME} _NAME_UPPER) 33 | 34 | SET(${_NAME_UPPER}_FOUND TRUE) 35 | IF(NOT ${_VAR1}) 36 | SET(${_NAME_UPPER}_FOUND FALSE) 37 | ENDIF(NOT ${_VAR1}) 38 | 39 | FOREACH(_CURRENT_VAR ${ARGN}) 40 | IF(NOT ${_CURRENT_VAR}) 41 | SET(${_NAME_UPPER}_FOUND FALSE) 42 | ENDIF(NOT ${_CURRENT_VAR}) 43 | ENDFOREACH(_CURRENT_VAR) 44 | 45 | IF (${_NAME_UPPER}_FOUND) 46 | IF (NOT ${_NAME}_FIND_QUIETLY) 47 | MESSAGE(STATUS "Found ${_NAME}: ${${_VAR1}}") 48 | ENDIF (NOT ${_NAME}_FIND_QUIETLY) 49 | ELSE (${_NAME_UPPER}_FOUND) 50 | IF (${_NAME}_FIND_REQUIRED) 51 | MESSAGE(FATAL_ERROR "${_FAIL_MESSAGE}") 52 | ELSE (${_NAME}_FIND_REQUIRED) 53 | IF (NOT ${_NAME}_FIND_QUIETLY) 54 | MESSAGE(STATUS "${_FAIL_MESSAGE}") 55 | ENDIF (NOT ${_NAME}_FIND_QUIETLY) 56 | ENDIF (${_NAME}_FIND_REQUIRED) 57 | ENDIF (${_NAME_UPPER}_FOUND) 58 | ENDMACRO(FIND_PACKAGE_HANDLE_STANDARD_ARGS) 59 | -------------------------------------------------------------------------------- /CMakeModules/FindTinyXML2.cmake: -------------------------------------------------------------------------------- 1 | # FractalImages 2 | # Copyright (C) 2012 Sven Hertle 3 | # 4 | # This program is free software; you can redistribute it and/or modify it under 5 | # the terms of the GNU General Public License as published by the Free Software 6 | # Foundation; either version 3 of the License, or (at your option) any later 7 | # version. 8 | # 9 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 11 | # PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 | # 13 | # You should have received a copy of the GNU General Public License along with 14 | # this program; if not, see . 15 | 16 | # Find TinyXML2 17 | # 18 | # TinyXML2_FOUND True if TinyXML2 was found 19 | # TinyXML2_INCLUDE_DIR Directory with headers 20 | # TinyXML2_LIBRARIES List of libraries 21 | # 22 | 23 | find_path(TinyXML2_INCLUDE_DIR "tinyxml2.h") 24 | 25 | find_library(TinyXML2_LIBRARIES NAMES "tinyxml2") 26 | 27 | include(FindPackageHandleStandardArgs) 28 | find_package_handle_standard_args("TinyXML2" DEFAULT_MSG TinyXML2_INCLUDE_DIR TinyXML2_LIBRARIES) 29 | 30 | mark_as_advanced(TinyXML2_INCLUDE_DIR TinyXML2_LIBRARIES) 31 | 32 | #message(STATUS "TinyXML2_INCLUDE_DIR: ${TinyXML2_INCLUDE_DIR}") 33 | #message(STATUS "TinyXML2_LIBRARIES: ${TinyXML2_LIBRARIES}") 34 | -------------------------------------------------------------------------------- /CMakeModules/SetPlatformVars.cmake: -------------------------------------------------------------------------------- 1 | ## Compiler configuration 2 | IF(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_GNUCC) 3 | SET(_GCC_ 1) 4 | ENDIF() 5 | 6 | IF(MSVC) 7 | SET(_MSVC_ 1) 8 | ENDIF() 9 | 10 | ## Platform configuration 11 | IF(WIN32 OR WIN64) 12 | SET(_WIN_ 1) 13 | ENDIF() 14 | 15 | IF(UNIX) 16 | SET(_UNIX_ 1) 17 | ENDIF() 18 | 19 | IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 20 | SET(_OSX_ 1) 21 | ENDIF() 22 | 23 | IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") 24 | SET(_LINUX_ 1) 25 | ENDIF() 26 | 27 | IF(ANDROID) 28 | SET(_ANDROID_ 1) 29 | ENDIF() 30 | -------------------------------------------------------------------------------- /Files/CameraCalibration/900nc.cal: -------------------------------------------------------------------------------- 1 | Perseus_CalFile 2 | 640 480 3 | 827.03599 821.06273 4 | 329.14773 186.90243 -------------------------------------------------------------------------------- /Files/CameraCalibration/Kinect.cal: -------------------------------------------------------------------------------- 1 | Perseus_CalFile 2 | 640 480 3 | 536.7323 535.8335 4 | 314.1642 235.7941 -------------------------------------------------------------------------------- /Files/Images/248-LiveRGB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Images/248-LiveRGB.png -------------------------------------------------------------------------------- /Files/Images/Red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Images/Red.png -------------------------------------------------------------------------------- /Files/Masks/248-ID-1-LiveImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Masks/248-ID-1-LiveImage.png -------------------------------------------------------------------------------- /Files/Masks/248-ID-3-LiveImage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Masks/248-ID-3-LiveImage.png -------------------------------------------------------------------------------- /Files/Masks/248-LiveRGB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Masks/248-LiveRGB.png -------------------------------------------------------------------------------- /Files/Masks/480p_All_VideoMask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Masks/480p_All_VideoMask.png -------------------------------------------------------------------------------- /Files/Masks/Red_Mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Masks/Red_Mask.png -------------------------------------------------------------------------------- /Files/Masks/Red_Source.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Masks/Red_Source.png -------------------------------------------------------------------------------- /Files/Models/Renderer/long.obj: -------------------------------------------------------------------------------- 1 | # Blender3D v248 OBJ File: long.blend 2 | # www.blender3d.org 3 | g Cube.007 4 | v 2.016505 -3.960833 -1.000000 5 | v 2.016505 -3.960833 1.000000 6 | v -2.016505 -3.960833 1.000000 7 | v -2.016504 -3.960833 -1.000000 8 | usemtl Material 9 | s off 10 | f 1 2 3 11 | f 1 3 4 12 | g Cube.006 13 | v 2.016505 -3.960833 -1.000000 14 | v 2.016505 -3.960833 1.000000 15 | v 2.016506 3.960833 -1.000000 16 | v 2.016503 3.960833 1.000000 17 | usemtl Material 18 | s off 19 | f 5 7 6 20 | f 7 8 6 21 | g Cube.005 22 | v 2.016505 -3.960833 -1.000000 23 | v -2.016504 -3.960833 -1.000000 24 | v 2.016506 3.960833 -1.000000 25 | v -2.016505 3.960833 -1.000000 26 | usemtl Material 27 | s off 28 | f 11 9 10 29 | f 11 10 12 30 | g Cube.004 31 | v 2.016505 -3.960833 1.000000 32 | v -2.016505 -3.960833 1.000000 33 | v 2.016503 3.960833 1.000000 34 | v -2.016505 3.960833 0.999999 35 | usemtl Material 36 | s off 37 | f 13 15 14 38 | f 15 16 14 39 | g Cube.003 40 | v 2.016505 -3.960833 -1.000000 41 | v -2.016504 -3.960833 -1.000000 42 | v 2.016506 3.960833 -1.000000 43 | v -2.016505 3.960833 -1.000000 44 | usemtl Material 45 | s off 46 | f 19 17 18 47 | f 19 18 20 48 | g Cube.002 49 | v -2.016505 -3.960833 1.000000 50 | v -2.016504 -3.960833 -1.000000 51 | v -2.016505 3.960833 0.999999 52 | v -2.016505 3.960833 -1.000000 53 | usemtl Material 54 | s off 55 | f 21 23 24 56 | f 21 24 22 57 | g Cube.001 58 | v 2.016506 3.960833 -1.000000 59 | v 2.016503 3.960833 1.000000 60 | v -2.016505 3.960833 0.999999 61 | v -2.016505 3.960833 -1.000000 62 | usemtl Material 63 | s off 64 | f 25 28 26 65 | f 28 27 26 66 | -------------------------------------------------------------------------------- /Files/Results/OriginalResult/result0000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/OriginalResult/result0000.png -------------------------------------------------------------------------------- /Files/Results/OriginalResult/result0001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/OriginalResult/result0001.png -------------------------------------------------------------------------------- /Files/Results/OriginalResult/result0002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/OriginalResult/result0002.png -------------------------------------------------------------------------------- /Files/Results/OriginalResult/result0003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/OriginalResult/result0003.png -------------------------------------------------------------------------------- /Files/Results/posteriors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/posteriors.png -------------------------------------------------------------------------------- /Files/Results/result0000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/result0000.png -------------------------------------------------------------------------------- /Files/Results/result0001.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/result0001.png -------------------------------------------------------------------------------- /Files/Results/result0002.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/result0002.png -------------------------------------------------------------------------------- /Files/Results/result0003.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumark/PWP3D/0de2fdeb07983f0404809c6466a08e8f9217a5ab/Files/Results/result0003.png -------------------------------------------------------------------------------- /PerseusLib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required( VERSION 2.8 ) 2 | project( PWP3D ) 3 | 4 | set(VERSION_MAJOR 0) 5 | set(VERSION_MINOR 1) 6 | set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}) 7 | string( TOLOWER ${PROJECT_NAME} LIBRARY_NAME ) 8 | 9 | ################################################################################ 10 | 11 | list(APPEND SRC_H 12 | CUDA/CUDAConvolution.h 13 | CUDA/CUDAData.h 14 | CUDA/CUDADT.h 15 | CUDA/CUDAEF.h 16 | CUDA/CUDAEngine.h 17 | CUDA/CUDARenderer.h 18 | CUDA/CUDAScharr.h 19 | CUDA/CUDAUtils.h 20 | 21 | Objects/HistogramVarBin.h 22 | Objects/ImageRender.h 23 | Objects/IterationConfiguration.h 24 | Objects/Object3D.h 25 | Objects/Object3DParams.h 26 | Objects/Pose3D.h 27 | Objects/StepSize3D.h 28 | Objects/View3D.h 29 | Objects/View3DParams.h 30 | 31 | Optimiser/EFs/EFStandard.h 32 | Optimiser/EFs/IEnergyFunction.h 33 | Optimiser/Engine/OptimisationEngine.h 34 | 35 | Others/PerseusLibDefines.h 36 | 37 | PerseusLib.h 38 | 39 | Primitives/ImagePerseus.h 40 | Primitives/PixelUCHAR4.h 41 | Primitives/Vector2D.h 42 | Primitives/Vector3D.h 43 | Primitives/Vector4D.h 44 | 45 | Renderer/Engine/DrawingEngine.h 46 | Renderer/Engine/DrawingPrimitives.h 47 | 48 | Renderer/Model/Model.h 49 | Renderer/Model/ModelFace.h 50 | Renderer/Model/ModelGroup.h 51 | Renderer/Model/ModelH.h 52 | Renderer/Model/ModelVertex.h 53 | Renderer/Objects/Renderer3DObject.h 54 | Renderer/Objects/Renderer3DView.h 55 | Renderer/Primitives/Camera3D.h 56 | Renderer/Primitives/Quaternion.h 57 | Renderer/Transforms/CameraCoordinateTransform.h 58 | Renderer/Transforms/CoordinateTransform.h 59 | Renderer/Transforms/ObjectCoordinateTransform.h 60 | 61 | Utils/Debug.h 62 | Utils/FileUtils.h 63 | Utils/HistogramEngine.h 64 | Utils/ImageUtils.h 65 | Utils/MathUtils.h 66 | Utils/VisualisationEngine.h 67 | ) 68 | 69 | 70 | list(APPEND SRC_CU 71 | CUDA/CUDAConvolution.cu 72 | CUDA/CUDADT.cu 73 | CUDA/CUDAEF.cu 74 | CUDA/CUDAEngine.cu 75 | CUDA/CUDARenderer.cu 76 | CUDA/CUDAScharr.cu 77 | CUDA/CUDAUtils.cu 78 | 79 | Optimiser/EFs/EFStandard.cpp 80 | Optimiser/Engine/OptimisationEngine.cpp 81 | 82 | Renderer/Engine/DrawingEngine.cpp 83 | Renderer/Engine/DrawingPrimitives.cpp 84 | Renderer/Model/Model.cpp 85 | Renderer/Model/ModelGroup.cpp 86 | Renderer/Model/ModelH.cpp 87 | Renderer/Primitives/Quaternion.cpp 88 | Renderer/Transforms/CameraCoordinateTransform.cpp 89 | Renderer/Transforms/CoordinateTransform.cpp 90 | 91 | Utils/FileUtils.cpp 92 | Utils/HistogramEngine.cpp 93 | Utils/ImageUtils.cpp 94 | Utils/MathUtils.cpp 95 | Utils/VisualisationEngine.cpp 96 | ) 97 | 98 | 99 | ################################################################################ 100 | # Find required dependencies 101 | 102 | # Find CUDA toolkit. Thrust and NPP are bundled with CUDA toolkit. 103 | find_package(CUDA REQUIRED) 104 | list( APPEND USER_INC ${CUDA_INCLUDE_DIRS} ) 105 | set(HAVE_THRUST 1) 106 | set(HAVE_NPP 1) 107 | 108 | # Dont propogate host flags into CUDA environment. 109 | set(CUDA_PROPAGATE_HOST_FLAGS off) 110 | 111 | # Generate code for 2.0 and 3.0 CUDA compute architectures (TODO: Verify this works) 112 | set(CUDA_NVCC_FLAGS "-use_fast_math -gencode arch=compute_30,code=sm_30 -gencode arch=compute_20,code=sm_20" ) 113 | 114 | # Make sure that NPP is found 115 | if(NOT CUDA_npp_LIBRARY) 116 | # TODO: Fix FindCUDA or make this more robust 117 | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 118 | set(CUDA_npp_LIBRARY "${CUDA_TOOLKIT_ROOT_DIR}/lib/libnppc.dylib;${CUDA_TOOLKIT_ROOT_DIR}/lib/libnppi.dylib;${CUDA_TOOLKIT_ROOT_DIR}/lib/libnpps.dylib") 119 | elseif(WIN32) 120 | set(CUDA_npp_LIBRARY "${CUDA_TOOLKIT_ROOT_DIR}/lib/Win32/nppc.lib;${CUDA_TOOLKIT_ROOT_DIR}/lib/Win32/nppi.lib;${CUDA_TOOLKIT_ROOT_DIR}/lib/Win32/npps.lib") 121 | elseif(WIN64) 122 | set(CUDA_npp_LIBRARY "${CUDA_TOOLKIT_ROOT_DIR}/lib/x64/nppc.lib;${CUDA_TOOLKIT_ROOT_DIR}/lib/x64/nppi.lib;${CUDA_TOOLKIT_ROOT_DIR}/lib/x64/npps.lib") 123 | else() 124 | set(CUDA_npp_LIBRARY "${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnpps.so;${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnppi.so;${CUDA_TOOLKIT_ROOT_DIR}/lib64/libnpps.so") 125 | endif() 126 | endif() 127 | list(APPEND LINK_LIBS ${CUDA_npp_LIBRARY} ) 128 | 129 | find_package( Eigen3 QUIET ) 130 | if(EIGEN3_FOUND) 131 | set(HAVE_EIGEN 1) 132 | list( APPEND USER_INC ${EIGEN3_INCLUDE_DIR} ) 133 | endif() 134 | 135 | 136 | find_package( FREEIMAGE REQUIRED ) 137 | if(FREEIMAGE_FOUND) 138 | set(HAVE_FREEIMAGE 1) 139 | list(APPEND USER_INC ${FREEIMAGE_INCLUDE_DIRS} ) 140 | list(APPEND LINK_LIBS ${FREEIMAGE_LIBRARIES} ) 141 | endif() 142 | 143 | find_package( ASSIMP REQUIRED ) 144 | if(ASSIMP_FOUND) 145 | set(HAVE_ASSIMP 1) 146 | list(APPEND USER_INC ${ASSIMP_INCLUDE_DIR}) 147 | list(APPEND LINK_LIBS ${ASSIMP_LIBRARIES} ) 148 | endif() 149 | 150 | find_package( OpenCV REQUIRED ) 151 | if(FREEIMAGE_FOUND) 152 | set(HAVE_OpenCV 1) 153 | list(APPEND USER_INC ${OpenCV_INCLUDE_DIRS} ) 154 | list(APPEND LINK_LIBS ${OpenCV_LIBRARIES} ) 155 | endif() 156 | 157 | # So that we can find config.h 158 | list( APPEND LIB_INC_DIR "${CMAKE_CURRENT_BINARY_DIR}/..;${CMAKE_CURRENT_SOURCE_DIR}/.." ) 159 | 160 | ####################################################### 161 | ## Create configure file for inclusion in library 162 | 163 | CONFIGURE_FILE( 164 | "${CMAKE_CURRENT_SOURCE_DIR}/config.h.in" 165 | "${CMAKE_CURRENT_BINARY_DIR}/config.h" 166 | ) 167 | 168 | ################################################################################ 169 | # Include collected includes / libraries 170 | include_directories( ${USER_INC} ) 171 | include_directories( ${LIB_INC_DIR} ) 172 | include_directories( ${INTERNAL_INC} ) 173 | cuda_add_library( ${LIBRARY_NAME} ${SRC_H} ${SRC_CU} ) 174 | target_link_libraries(${LIBRARY_NAME} ${LINK_LIBS}) 175 | 176 | ## Generate symbol export helper header on MSVC 177 | if(MSVC) 178 | string(TOUPPER ${LIBRARY_NAME} LIBRARY_NAME_CAPS) 179 | include(GenerateExportHeader) 180 | generate_export_header( ${LIBRARY_NAME} 181 | BASE_NAME ${LIBRARY_NAME_CAPS} 182 | EXPORT_MACRO_NAME ${LIBRARY_NAME_CAPS}_EXPORT 183 | EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${LIBRARY_NAME}_export.h" 184 | STATIC_DEFINE ${LIBRARY_NAME_CAPS}_BUILT_AS_STATIC 185 | ) 186 | endif() 187 | 188 | ####################################################### 189 | 190 | # This relative path allows installed files to be relocatable. 191 | set( CMAKECONFIG_INSTALL_DIR "lib/cmake/${PROJECT_NAME}" ) 192 | file( RELATIVE_PATH REL_INCLUDE_DIR 193 | "${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR}" 194 | "${CMAKE_INSTALL_PREFIX}/include" ) 195 | 196 | # Export library for easy inclusion from other cmake projects. 197 | export( TARGETS ${LIBRARY_NAME} 198 | FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake" ) 199 | 200 | # Version information 201 | configure_file("${PROJECT_NAME}ConfigVersion.cmake.in" 202 | "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" @ONLY) 203 | 204 | # Build tree config 205 | set( EXPORT_LIB_INC_DIR "${LIB_INC_DIR}" ) 206 | CONFIGURE_FILE( "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in" 207 | "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY IMMEDIATE ) 208 | 209 | # Install tree config 210 | set( EXPORT_LIB_INC_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/${REL_INCLUDE_DIR}" ) 211 | configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in" 212 | "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake" @ONLY ) 213 | 214 | # Add package to CMake package registery for use from the build tree 215 | export( PACKAGE ${PROJECT_NAME} ) 216 | 217 | ####################################################### 218 | ## Install headers / targets 219 | 220 | install(FILES "${CMAKE_CURRENT_BINARY_DIR}/config.h" 221 | DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME} 222 | ) 223 | install(FILES ${SRC_H} 224 | DESTINATION ${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME} 225 | ) 226 | install(TARGETS ${LIBRARY_NAME} 227 | EXPORT "${PROJECT_NAME}Targets" 228 | RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin 229 | LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 230 | ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib 231 | ) 232 | 233 | ####################################################### 234 | ## Install CMake config 235 | 236 | INSTALL( 237 | FILES "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake" 238 | "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" 239 | DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) 240 | 241 | install( EXPORT "${PROJECT_NAME}Targets" DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) 242 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAConvolution.cu: -------------------------------------------------------------------------------- 1 | #include "CUDAConvolution.h" 2 | #include "CUDAUtils.h" 3 | #include "CUDAData.h" 4 | 5 | extern CUDAData* cudaData; 6 | 7 | __device__ __constant__ float dKernelConvolution[KERNEL_WIDTH]; 8 | const int KERNEL_SIZE = KERNEL_WIDTH * sizeof(float); 9 | 10 | // Loop unrolling templates, needed for best performance 11 | template __device__ float convolutionRowT(float *data){return data[KERNEL_RADIUS-i]*dKernelConvolution[i]+convolutionRowT(data);} 12 | template<> __device__ float convolutionRowT<-1>(float *data){return 0;} 13 | template __device__ float convolutionColT(float *data){return data[(KERNEL_RADIUS-i)*COLUMN_TILE_WIDTH]*dKernelConvolution[i]+convolutionColT(data);} 14 | template<> __device__ float convolutionColT<-1>(float *data){return 0;} 15 | 16 | __global__ void convolutionRow(float *d_Result, float *d_Data, int dataW, int dataH) 17 | { 18 | const int rowStart = IMUL(blockIdx.y, dataW); 19 | 20 | __shared__ float data[KERNEL_RADIUS + ROW_TILE_WIDTH + KERNEL_RADIUS]; 21 | 22 | const int tileStart = IMUL(blockIdx.x, ROW_TILE_WIDTH); 23 | const int tileEnd = tileStart + ROW_TILE_WIDTH - 1; 24 | const int apronStart = tileStart - KERNEL_RADIUS; 25 | const int apronEnd = tileEnd + KERNEL_RADIUS; 26 | 27 | const int tileEndClamped = min(tileEnd, dataW - 1); 28 | const int apronStartClamped = max(apronStart, 0); 29 | const int apronEndClamped = min(apronEnd, dataW - 1); 30 | 31 | const int apronStartAligned = tileStart - KERNEL_RADIUS_ALIGNED; 32 | 33 | const int loadPos = apronStartAligned + threadIdx.x; 34 | 35 | if(loadPos >= apronStart) 36 | { 37 | const int smemPos = loadPos - apronStart; 38 | data[smemPos] = ((loadPos >= apronStartClamped) && (loadPos <= apronEndClamped)) ? d_Data[rowStart + loadPos] : 0; 39 | } 40 | 41 | __syncthreads(); 42 | const int writePos = tileStart + threadIdx.x; 43 | 44 | if(writePos <= tileEndClamped) 45 | { 46 | const int smemPos = writePos - apronStart; 47 | float sum = 0; 48 | sum = convolutionRowT<2 * KERNEL_RADIUS>(data + smemPos); 49 | d_Result[rowStart + writePos] = sum; 50 | } 51 | } 52 | 53 | __global__ void convolutionColumn(float *d_Result, float *d_Data, int dataW, int dataH, int smemStride, int gmemStride) 54 | { 55 | const int columnStart = IMUL(blockIdx.x, COLUMN_TILE_WIDTH) + threadIdx.x; 56 | 57 | __shared__ float data[COLUMN_TILE_WIDTH * (KERNEL_RADIUS + COLUMN_TILE_HEIGHT + KERNEL_RADIUS)]; 58 | 59 | const int tileStart = IMUL(blockIdx.y, COLUMN_TILE_HEIGHT); 60 | const int tileEnd = tileStart + COLUMN_TILE_HEIGHT - 1; 61 | const int apronStart = tileStart - KERNEL_RADIUS; 62 | const int apronEnd = tileEnd + KERNEL_RADIUS; 63 | 64 | const int tileEndClamped = min(tileEnd, dataH - 1); 65 | const int apronStartClamped = max(apronStart, 0); 66 | const int apronEndClamped = min(apronEnd, dataH - 1); 67 | 68 | int smemPos = IMUL(threadIdx.y, COLUMN_TILE_WIDTH) + threadIdx.x; 69 | int gmemPos = IMUL(apronStart + threadIdx.y, dataW) + columnStart; 70 | 71 | for(int y = apronStart + threadIdx.y; y <= apronEnd; y += blockDim.y) 72 | { 73 | data[smemPos] = ((y >= apronStartClamped) && (y <= apronEndClamped)) ? d_Data[gmemPos] : 0; 74 | smemPos += smemStride; 75 | gmemPos += gmemStride; 76 | } 77 | 78 | __syncthreads(); 79 | 80 | smemPos = IMUL(threadIdx.y + KERNEL_RADIUS, COLUMN_TILE_WIDTH) + threadIdx.x; 81 | gmemPos = IMUL(tileStart + threadIdx.y , dataW) + columnStart; 82 | 83 | for(int y = tileStart + threadIdx.y; y <= tileEndClamped; y += blockDim.y) 84 | { 85 | float sum = 0; 86 | sum = convolutionColT<2 * KERNEL_RADIUS>(data + smemPos); 87 | d_Result[gmemPos] = sum; 88 | smemPos += smemStride; 89 | gmemPos += gmemStride; 90 | } 91 | } 92 | 93 | __host__ void initialiseConvolution(int width, int height) 94 | { 95 | cudaData->hKernelConvolution = (float *)malloc(KERNEL_SIZE); 96 | cudaData->hKernelConvolution[0] = 0.5f; 97 | cudaData->hKernelConvolution[1] = 0; 98 | cudaData->hKernelConvolution[2] = -0.5f; 99 | 100 | perseusSafeCall(cudaMemcpyToSymbol(dKernelConvolution, cudaData->hKernelConvolution, KERNEL_SIZE)); 101 | } 102 | __host__ void shutdownConvolution() 103 | { 104 | free(cudaData->hKernelConvolution); 105 | } 106 | __host__ void computeDerivativeXY(float* function, float* derivativeX, float* derivativeY, int width, int height) 107 | { 108 | dim3 blockGridRows = dim3(iDivUp(width, ROW_TILE_WIDTH), height); 109 | dim3 blockGridColumns = dim3(iDivUp(width, COLUMN_TILE_WIDTH), iDivUp(height, COLUMN_TILE_HEIGHT)); 110 | dim3 threadBlockRows = dim3(KERNEL_RADIUS_ALIGNED + ROW_TILE_WIDTH + KERNEL_RADIUS); 111 | dim3 threadBlockColumns = dim3(COLUMN_TILE_WIDTH, 8); 112 | 113 | convolutionRow<<>> (derivativeX, function, width, height); 114 | convolutionColumn<<>>( derivativeY, function, width, height, 115 | COLUMN_TILE_WIDTH * threadBlockColumns.y, width * threadBlockColumns.y); 116 | } 117 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAConvolution.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "CUDADefines.h" 10 | 11 | __host__ void initialiseConvolution(int width, int height); 12 | __host__ void shutdownConvolution(); 13 | 14 | __host__ void computeDerivativeXY(float* function, float* derivativeX, float* derivativeY, int width, int height); 15 | 16 | __global__ void convolutionRow(float *d_Result, float *d_Data, int dataW, int dataH); 17 | __global__ void convolutionColumn(float *d_Result, float *d_Data, int dataW, int dataH, int smemStride, int gmemStride); 18 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDADT.cu: -------------------------------------------------------------------------------- 1 | #include "CUDADT.h" 2 | #include "CUDAUtils.h" 3 | #include "CUDAData.h" 4 | 5 | extern CUDAData* cudaData; 6 | 7 | __host__ void initialiseDT(int width, int height) 8 | { 9 | //TODO MAX WIDTH ROI 10 | int maxWidthROI = width, maxHeightROI = height; 11 | 12 | perseusSafeCall(cudaMalloc((void**)&cudaData->dtImageT1, maxWidthROI * maxHeightROI * sizeof(float))); 13 | perseusSafeCall(cudaMalloc((void**)&cudaData->dtZImage, (maxWidthROI+1) * maxHeightROI * sizeof(float))); 14 | perseusSafeCall(cudaMalloc((void**)&cudaData->dtVImage, maxWidthROI * maxHeightROI * sizeof(int))); 15 | perseusSafeCall(cudaMalloc((void**)&cudaData->dtImagePosYT1, maxWidthROI * maxHeightROI * sizeof(int))); 16 | } 17 | 18 | __host__ void shutdownDT() 19 | { 20 | perseusSafeCall(cudaFree(cudaData->dtImageT1)); 21 | perseusSafeCall(cudaFree(cudaData->dtZImage)); 22 | perseusSafeCall(cudaFree(cudaData->dtVImage)); 23 | perseusSafeCall(cudaFree(cudaData->dtImagePosYT1)); 24 | } 25 | 26 | __global__ void dtToImage(float* dtImage, unsigned char* grayImage, int dtWidth, int dtHeight) 27 | { 28 | int i; 29 | float* currentDTRow = dtImage + threadIdx.x * dtWidth; 30 | unsigned char* currentRow = grayImage + threadIdx.x * dtWidth; 31 | 32 | for (i=0;i>>(imageTransform, image, widthFull, heightFull); 38 | } 39 | 40 | __host__ void processDT(float *dt, int *dtPosX, int *dtPosY, unsigned char *grayImage, unsigned char* signMask, int *roi, int bandSize) 41 | { 42 | int widthROI, heightROI; 43 | widthROI = roi[4]; heightROI = roi[5]; 44 | 45 | dim3 blocks(8); 46 | dim3 threads_in_block_t1(iDivUp(widthROI, 8)); 47 | dim3 threads_in_block_t2(iDivUp(heightROI, 8)); 48 | 49 | perseusSafeCall(cudaMemset(cudaData->dtImageT1, 0, (widthROI+16) * (heightROI+16) * sizeof(float))); 50 | perseusSafeCall(cudaMemset(dt, 0, (widthROI+16) * (heightROI+16) * sizeof(float))); 51 | perseusSafeCall(cudaMemset(dtPosX, -1, (widthROI+16) * (heightROI+16) * sizeof(int))); 52 | perseusSafeCall(cudaMemset(dtPosY, -1, (widthROI+16) * (heightROI+16) * sizeof(int))); 53 | 54 | processDTT1<<>>(grayImage, cudaData->dtImageT1, dtPosX, cudaData->dtImagePosYT1, 55 | widthROI, heightROI, widthROI, roi[0], roi[1], roi[2], roi[3], bandSize); 56 | 57 | processDTT2<<>>(dt, widthROI, heightROI, cudaData->dtImageT1, 58 | cudaData->dtZImage, cudaData->dtVImage, signMask, dtPosX, cudaData->dtImagePosYT1, dtPosY, 59 | widthROI, heightROI, roi[0], roi[1], roi[2], roi[3], bandSize); 60 | } 61 | 62 | __global__ void processDTT1(unsigned char* grayImage, float* dtImageT1, int* dtImagePosX, int* dtImagePosYT1, int dtWidth, 63 | int dtHeight, int dtWidthFull, int minxB, int minyB, int maxxB, int maxyB, int bandSize) 64 | { 65 | int j, iOriginal; 66 | int prevZero = minyB; 67 | 68 | int columnIndexI, columnIndexJ, rowIndex, dtHeightOriginal, columnIndexMin; 69 | 70 | int offsetX = minxB + threadIdx.x + blockIdx.x * blockDim.x; 71 | 72 | if (offsetX >= maxxB) 73 | return; 74 | 75 | rowIndex = offsetX; 76 | dtHeightOriginal = dtHeight + minyB; 77 | columnIndexMin = COLUMN(rowIndex, minyB, dtWidthFull); 78 | 79 | for (iOriginal = minyB; iOriginal= prevZero; j--) 90 | { 91 | columnIndexJ = COLUMN(rowIndex, j, dtWidthFull); 92 | dtImageT1[columnIndexJ] = SQRi(iOriginal - j); 93 | dtImagePosYT1[columnIndexJ] = iOriginal; 94 | } 95 | prevZero = iOriginal; 96 | 97 | continue; 98 | } 99 | 100 | if (iOriginal == dtHeightOriginal - 1 && prevZero == minyB && grayImage[columnIndexMin] != 0) 101 | { 102 | for (j = iOriginal; j >= prevZero; j--) 103 | { 104 | columnIndexJ = COLUMN(rowIndex, j, dtWidthFull); 105 | dtImageT1[columnIndexJ] = SQRi(iOriginal - j); 106 | dtImagePosYT1[columnIndexJ] = iOriginal; 107 | } 108 | prevZero = iOriginal; 109 | 110 | continue; 111 | } 112 | 113 | if (iOriginal == dtHeightOriginal - 1 && grayImage[columnIndexI] == 0 && prevZero != minyB) 114 | { 115 | for (j = iOriginal; j >= prevZero; j--) 116 | { 117 | columnIndexJ = COLUMN(rowIndex, j, dtWidthFull); 118 | dtImageT1[columnIndexJ] = SQRi(j - prevZero); 119 | dtImagePosYT1[columnIndexJ] = prevZero; 120 | } 121 | prevZero = iOriginal; 122 | 123 | continue; 124 | } 125 | 126 | if (((iOriginal == dtHeightOriginal - 1) || (prevZero == minyB && iOriginal == minyB )) 127 | && grayImage[columnIndexI] == 0) 128 | { 129 | continue; 130 | } 131 | 132 | for (j = iOriginal; j >= iOriginal - (iOriginal - prevZero)/2; j--) 133 | { 134 | columnIndexJ = COLUMN(rowIndex, j, dtWidthFull); 135 | dtImageT1[columnIndexJ] = SQRi(iOriginal - j); 136 | dtImagePosYT1[columnIndexJ] = iOriginal; 137 | } 138 | 139 | for (j = prevZero; j < iOriginal - (iOriginal - prevZero)/2; j++) 140 | { 141 | columnIndexJ = COLUMN(rowIndex, j, dtWidthFull); 142 | dtImageT1[columnIndexJ] = SQRi(j - prevZero); 143 | dtImagePosYT1[columnIndexJ] = prevZero; 144 | } 145 | prevZero = iOriginal; 146 | } 147 | } 148 | } 149 | 150 | __global__ void processDTT2(float* dtImageT2, int dtWidthFull, int dtHeightFull, float* dtImageT1, float* zImage, int* vImage, 151 | unsigned char *signMask, int* dtImagePosX, int* dtImagePosYT1, int* dtImagePosYT2, int dtWidth, int dtHeight, 152 | int minxB, int minyB, int maxxB, int maxyB, int bandSize) 153 | { 154 | int offsetX = minxB; 155 | int offsetY = threadIdx.x + (blockIdx.x * blockDim.x) + minyB; 156 | 157 | if (offsetX >= maxxB || offsetY >= maxyB) 158 | return; 159 | 160 | int offset = offsetX + offsetY * dtWidthFull; 161 | 162 | float* f = dtImageT1 + offset; 163 | 164 | float* d = dtImageT2 + offset; 165 | float* z = zImage + offset; 166 | int* v = vImage + offset; 167 | 168 | int* fPos = dtImagePosYT1 + offset; 169 | int* posXRow = dtImagePosX + offset; 170 | int* posYRow = dtImagePosYT2 + offset; 171 | 172 | unsigned char* signMaskRow = signMask + offset; 173 | 174 | int k = 0, q, dq; 175 | int dist, bandSizeSquared = IMUL(bandSize, bandSize); 176 | 177 | float fqpqq, s; 178 | v[0] = 0; 179 | z[0] = -INF_INT; 180 | z[1] = +INF_INT; 181 | 182 | for (q = 0; q < dtWidth; q++) 183 | { 184 | if (f[q] != +INF_INT) 185 | { 186 | fqpqq = f[q] + SQRi(q); 187 | dq = 2 * q; 188 | 189 | s = (q == 0) ? (fqpqq - (f[v[k]] + SQRi(v[k]))) : __fdividef((fqpqq - (f[v[k]] + SQRi(v[k]))),(dq - IMUL(2, v[k]))); 190 | 191 | while (s <= z[k]) 192 | { 193 | k--; 194 | if (k < 0) break; 195 | s = (q == 0) ? (fqpqq - (f[v[k]] + SQRi(v[k]))) : __fdividef((fqpqq - (f[v[k]] + SQRi(v[k]))),(dq - IMUL(2, v[k]))); 196 | } 197 | k++; 198 | v[k] = q; 199 | z[k] = s; 200 | } 201 | else 202 | { 203 | s = (f[v[k]] == +INF_INT) ? 0 : +INF_INT; 204 | 205 | k++; 206 | v[k] = q; 207 | z[k] = s; 208 | z[k+1] = +INF_INT; 209 | } 210 | } 211 | 212 | k = 0; 213 | for (q = 0; q < dtWidth; q++) 214 | { 215 | while (z[k+1] < q && (k+1) < dtWidth) k++; 216 | 217 | if (v[k] < dtWidth) 218 | { 219 | if (f[v[k]] < bandSizeSquared) 220 | { 221 | dist = SQRi(q - v[k]) + f[v[k]]; 222 | if (dist < bandSizeSquared) 223 | { 224 | posXRow[q] = v[k] + minxB; 225 | posYRow[q] = fPos[v[k]]; 226 | d[q] = (signMaskRow[q] == signMask[posXRow[q] + posYRow[q] * dtWidth]) ? sqrtf(dist) : -sqrtf(dist); //was != 0 227 | } 228 | } 229 | } 230 | } 231 | } 232 | 233 | __host__ void getDT(float* dtROI, float *dt, int *roi, int widthFull, int heightFull) 234 | { 235 | memset(dt, 0, widthFull * heightFull * sizeof(float)); 236 | 237 | perseusSafeCall(cudaMemcpy2D(dt + roi[0] + roi[1] * widthFull, widthFull * sizeof(float), 238 | dtROI, roi[4] * sizeof(float), roi[4] * sizeof(float), roi[5], cudaMemcpyDeviceToDevice)); 239 | } 240 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDADT.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "CUDADefines.h" 10 | 11 | __host__ void initialiseDT(int width, int height); 12 | __host__ void shutdownDT(); 13 | 14 | __host__ void convertDTToImage(unsigned char* image, float* imageTransform, int widthFull, int heightFull); 15 | __host__ void processDT(float *dt, int *dtPosX, int *dtPosY, unsigned char *grayImage, unsigned char* signMask, int *roi, int bandSize); 16 | __host__ void getDT(float* dtROI, float *dt, int *roi, int widthFull, int heightFull); 17 | 18 | __global__ void processDTT1(unsigned char* grayImage, float* dtImageT1, int* dtImagePosX, int* dtImagePosYT1, int dtWidth, 19 | int dtHeight, int dtWidthFull, int minxB, int minyB, int maxxB, int maxyB, int bandSize); 20 | __global__ void processDTT2(float* dtImageT2, int dtWidthFull, int dtHeightFull, float* dtImageT1, float* zImage, int* vImage, 21 | unsigned char *signMask, int* dtImagePosX, int* dtImagePosYT1, int* dtImagePosYT2, int dtWidth, int dtHeight, 22 | int minxB, int minyB, int maxxB, int maxyB, int bandSize); 23 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAData.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "CUDADefines.h" 4 | 5 | struct CUDAData 6 | { 7 | //int widthFull, heightFull; 8 | //int widthROI, heightROI; 9 | int bufferSize; 10 | 11 | int viewCount, objectCount; 12 | 13 | cudaArray *arrayScharr; 14 | 15 | int *dtVImage; 16 | float *dtZImage; 17 | int *dtImagePosYT1; 18 | float *dtImageT1; 19 | 20 | float *hKernelConvolution; 21 | 22 | cudaArray *arrayHeaviside; 23 | 24 | int histogramSize; 25 | float dpose[7]; 26 | float2 *histograms; 27 | float3 *dfxTranslation, *dfxResultTranslation; 28 | float4 *dfxRotation, *dfxResultRotation; 29 | 30 | cudaChannelFormatDesc descRendererVertices; 31 | 32 | unsigned char *fill; 33 | unsigned char *objects; 34 | unsigned int *zbuffer; 35 | unsigned int *zbufferInverse; 36 | 37 | unsigned char *fillAll; 38 | unsigned char *objectsAll; 39 | unsigned int *zbufferAll; 40 | unsigned int *zbufferInverseAll; 41 | 42 | int roiGenerated[6]; 43 | int roiGeneratedAll[6]; 44 | 45 | int4 *d_rois, *d_roisAll, *h_rois, *h_roisAll; 46 | 47 | int roisSize; 48 | }; 49 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDADefines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #ifdef _WIN32 8 | # define WINDOWS_LEAN_AND_MEAN 9 | # include 10 | #endif 11 | 12 | #ifndef perseusSafeCall 13 | #define perseusSafeCall(err) __perseusSafeCall(err, __FILE__, __LINE__) 14 | 15 | inline void __perseusSafeCall( cudaError err, const char *file, const int line ) 16 | { 17 | if( cudaSuccess != err) { 18 | printf("%s(%i) : cudaSafeCall() Runtime API error : %s.\n", 19 | file, line, cudaGetErrorString(err) ); 20 | exit(-1); 21 | } 22 | } 23 | 24 | #endif 25 | 26 | #ifndef HISTOGRAM_NO_BINS 27 | #define HISTOGRAM_NO_BINS 32 28 | #endif 29 | 30 | #ifndef EXECUTYIN512THREADS 31 | #define EXECUTYIN512THREADS(counter, startPoint, func, params) \ 32 | startPoint = 0;\ 33 | if (counter / 512 > 0) \ 34 | { \ 35 | while (counter / 512 > 0) \ 36 | { \ 37 | func<<<1, 512>>> ## params; \ 38 | startPoint += 512; \ 39 | counter -= 512; \ 40 | } \ 41 | if (counter != 0) \ 42 | func<<<1, counter>>> ## params; \ 43 | } \ 44 | else \ 45 | func<<<1, counter>>> ## params; 46 | #endif 47 | 48 | #ifndef WARP_SIZE 49 | #define WARP_SIZE 32 50 | #endif 51 | 52 | #ifndef MAX_BLOCKS_PER_DIM 53 | #define MAX_BLOCKS_PER_DIM 65536 54 | #endif 55 | 56 | #ifndef IMUL 57 | #define IMUL(a, b) __mul24(a, b) 58 | #endif 59 | 60 | #ifndef SQR 61 | #define SQR(a) (a)*(a) 62 | #endif 63 | 64 | #ifndef SQRi 65 | #define SQRi(a) __mul24((a),(a)) 66 | #endif 67 | 68 | #ifndef KERNEL_RADIUS 69 | #define KERNEL_RADIUS 1 70 | #endif 71 | 72 | #ifndef KERNEL_WIDTH 73 | #define KERNEL_WIDTH (2 * KERNEL_RADIUS + 1) 74 | #endif 75 | 76 | #ifndef ROW_TILE_WIDTH 77 | #define ROW_TILE_WIDTH 128 78 | #endif 79 | 80 | #ifndef KERNEL_RADIUS_ALIGNED 81 | #define KERNEL_RADIUS_ALIGNED 16 82 | #endif 83 | 84 | #ifndef COLUMN_TILE_WIDTH 85 | #define COLUMN_TILE_WIDTH 16 86 | #endif 87 | 88 | #ifndef COLUMN_TILE_HEIGHT 89 | #define COLUMN_TILE_HEIGHT 48 90 | #endif 91 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAEF.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "CUDADefines.h" 10 | 11 | __host__ void initialiseEF(int width, int height, float* heavisideFunction, int heavisideFunctionSize); 12 | __host__ void shutdownEF(); 13 | 14 | __host__ void registerViewGeometricData(float *invP_EF, float *projectionParams_EF, int *viewTransform_EF); 15 | __host__ void registerObjectGeometricData(float* rotationQuaternion_EF, float* invPM_EF); 16 | 17 | void processEFD1(float* dpose, int *roiNormalised, int *roiGenerated, float2* histogram, uchar4 *imageRegistered, unsigned char *imageObjects, 18 | bool isMultiobject, unsigned int *imageZBuffer, unsigned int *imageZBufferInverse, 19 | float *dt, int *dtPosX, int *dtPosY, float *dtDX, float *dtDY, int objectId); 20 | 21 | __global__ void processEFD1_global(float3 *dfxTranslation, float4 *dfxRotation, float2 *histogram, uchar4 *imageRegistered, unsigned char *imageObjects, 22 | bool isMultiobject, unsigned int *imageZBuffer, unsigned int *imageZBufferInverse, 23 | float *dt, int *dtPosX, int *dtPosY, float *dtDX, float *dtDY, 24 | int minX, int minY, int widthROI, int heightROI, int objectId); 25 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAEngine.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace PerseusLib::Objects; 12 | 13 | #include "CUDADefines.h" 14 | 15 | void initialiseCUDA(int width, int height, float* heavisideFunction, int heavisideFunctionSize); 16 | void shutdownCUDA(); 17 | 18 | void registerObjectImage(Object3D* object, View3D* view, bool renderingFromGPU, bool isMultiobject); 19 | void registerObjectAndViewGeometricData(Object3D* object, View3D* view); 20 | 21 | void processDTSihluetteLSDXDY(Object3D* object, View3D* view, int bandSize); 22 | void processAndGetEFFirstDerivatives(Object3D* object, View3D* view, bool isMultiobject); 23 | 24 | void getProcessedDataDTSihluetteLSDXDY(Object3D* object, View3D* view); 25 | void getProcessedDataEFFirstDerivatives(Object3D* object, View3D* view); 26 | void getProcessedDataRendering(Object3D* object, View3D* view); 27 | void getProcessedDataRenderingAll(View3D* view); 28 | 29 | void renderObjectCUDA(Object3D *object, View3D *view); 30 | void renderObjectAllCUDA(Object3D **objects, int objectCount, View3D *view); 31 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDARenderer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "CUDADefines.h" 10 | 11 | __host__ void initialiseRenderer(int width, int height); 12 | __host__ void shutdownRenderer(); 13 | 14 | __host__ void renderObjectCUDA_one_EF(float4 *vertices, int faceCount, int objectId, float *h_pmMatrix, int *h_viewTransform, int widthRender, int heightRender); 15 | __host__ void renderObjectCUDA_all_EF(float4 *vertices, int faceCount, int objectId, float *h_pmMatrix, int *h_viewTransform, int widthRender, int heightRender, bool clearMap); 16 | 17 | __global__ void renderObjectCUDA_EF_global(unsigned char *fill, unsigned char *objects, unsigned int *zbuffer, unsigned int *zbufferInverse, 18 | int4 *rois, int faceCount, int objectId, int widthRender, int heightRender); 19 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAScharr.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "CUDADefines.h" 10 | 11 | __global__ void scharrTex(unsigned char*, unsigned int, int, int, float); 12 | __global__ void sihluetteTex(unsigned char *, unsigned int, int, int, float); 13 | 14 | __device__ unsigned char computeScharrPP(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, 15 | unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, float); 16 | __device__ unsigned char computeSihluettePP(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, 17 | unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, float); 18 | 19 | __host__ void initialiseScharr(int, int); 20 | __host__ void shutdownScharr(); 21 | __host__ void computeSihluette(unsigned char *originalImage, unsigned char *scharrImage, int w, int h, float fScale); 22 | __host__ void computeScharr(unsigned char *originalImage, unsigned char *sihlutteImage, int w, int h, float fScale); 23 | -------------------------------------------------------------------------------- /PerseusLib/CUDA/CUDAUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "CUDADefines.h" 10 | 11 | //struct PixelRGB { CUDA_PIXEL r,b,g; }; 12 | 13 | __host__ int iDivUp(int a, int b); 14 | __host__ int iDivDown(int a, int b); 15 | __host__ int iAlignUp(int a, int b); 16 | __host__ int iAlignDown(int a, int b); 17 | 18 | //__host__ void MakeGrayScale(int*, CUDA_PIXEL*, int, int); 19 | //__host__ void GrayToOutput(CUDA_PIXEL*, int*, int, int); 20 | //__host__ void GetNormalizedRoi(int*, int*, int &, int &); 21 | //__host__ void GetNormalizedImageParamatersCenter(int*, int*, int&, int&); 22 | //__host__ void GetNormalizedImageParamaters(int*, int*, int *, int*, int&, int&); 23 | //__host__ void GetCenteredRoi(int*, int, int, int*); 24 | //__host__ void NormalizeWithRoi(CUDA_PIXEL*, int*, int, int, int*, int, int, CUDA_PIXEL*); 25 | //__host__ void Add(CUDA_PIXEL* image1, CUDA_PIXEL* image2, CUDA_PIXEL *imageSum, int width, int height); 26 | //__host__ void Sub(CUDA_PIXEL* image1, CUDA_PIXEL* image2, CUDA_PIXEL *imageDiff, int width, int height); 27 | //__host__ void AddDT(CUDA_FLOAT* image1, CUDA_FLOAT* image2, CUDA_FLOAT* imageSum, int width, int height); 28 | //__host__ void SubDT(CUDA_FLOAT* image1, CUDA_FLOAT* image2, CUDA_FLOAT* imageDiff, int width, int height); 29 | //__host__ void CopyToOutputImageCentered(CUDA_PIXEL*, CUDA_PIXEL*, int, int, int, int); 30 | //__host__ void CopyToOutputImageOriginal(CUDA_PIXEL*, CUDA_PIXEL*, int*, int*, int, int, int, int); 31 | //__host__ void CombineRenderedWithRegistered3(CUDA_PIXEL*, CUDA_PIXEL*, int, int); 32 | // 33 | //__global__ void makeGrayScale(int*, CUDA_PIXEL*, int); 34 | //__global__ void grayToOutput(CUDA_PIXEL*, int*, int); 35 | //__global__ void normalizeWithRoi(CUDA_PIXEL*,int, int, int, int, int, CUDA_PIXEL*, int, int, int, int); 36 | //__global__ void add(CUDA_PIXEL*, CUDA_PIXEL*, CUDA_PIXEL*, int, int); 37 | //__global__ void sub(CUDA_PIXEL*, CUDA_PIXEL*, CUDA_PIXEL*, int, int); 38 | //__global__ void addDT(CUDA_FLOAT*, CUDA_FLOAT*, CUDA_FLOAT*, int, int); 39 | //__global__ void subDT(CUDA_FLOAT*, CUDA_FLOAT*, CUDA_FLOAT*, int, int); 40 | //__global__ void combineRenderedWithRegistered3sm(CUDA_PIXEL*, CUDA_PIXEL*, int, int); 41 | // 42 | //__device__ inline void atomicFloatAdd(float *address, float val); 43 | -------------------------------------------------------------------------------- /PerseusLib/Objects/HistogramVarBin.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | namespace PerseusLib 8 | { 9 | namespace Objects 10 | { 11 | class HistogramVarBin 12 | { 13 | private: 14 | public: 15 | int fullHistSize; 16 | int *histOffsets; 17 | 18 | float mergeAlphaForeground; 19 | float mergeAlphaBackground; 20 | 21 | float2* normalised; 22 | float2* notnormalised; 23 | 24 | float2* normalisedGPU; 25 | 26 | int *noBins, *factor, noHistograms; 27 | bool isAllocated, alreadyInitialised; 28 | 29 | float totalForegroundPixels, totalBackgroundPixels; 30 | float etaF, etaB; 31 | 32 | HistogramVarBin() 33 | { 34 | isAllocated = false; 35 | } 36 | 37 | void Set(int noHistograms, int *noBins) 38 | { 39 | if(noHistograms <= 0) 40 | { 41 | printf("fatal error! noHistograms must >0\n"); 42 | exit(-1); 43 | } 44 | 45 | if (!isAllocated) 46 | { 47 | this->isAllocated = true; 48 | this->noHistograms = noHistograms; 49 | 50 | fullHistSize = 0; 51 | this->noBins = new int[noHistograms]; 52 | this->histOffsets = new int[noHistograms]; 53 | 54 | for (int i=0; inoBins[i] = noBins[i]; 57 | histOffsets[i] = fullHistSize; 58 | fullHistSize += noBins[i] * noBins[i] * noBins[i]; 59 | } 60 | 61 | normalised = new float2[fullHistSize]; 62 | notnormalised = new float2[fullHistSize]; 63 | 64 | cudaMalloc((void**)&normalisedGPU, fullHistSize * sizeof(float2)); 65 | 66 | // TODO MAKE NICER 67 | factor = new int[noHistograms]; 68 | factor[0] = 5; 69 | factor[1] = 4; 70 | factor[2] = 3; 71 | factor[3] = 2; 72 | 73 | this->Clear(); 74 | } 75 | else this->Clear(); 76 | } 77 | 78 | void GetValue(float* foreground, float *background, int r, int g, int b, int x, int y) 79 | { 80 | int ru, gu, bu, pidx, currentHistogram; 81 | 82 | int greyVal = int(float(r) * 0.3f + float(g) * 0.59f + float(b) * 0.11f); 83 | 84 | currentHistogram = 0; 85 | if (greyVal < 128) currentHistogram = 3; 86 | else if (greyVal < 192) currentHistogram = 2; 87 | else if (greyVal < 224) currentHistogram = 1; 88 | 89 | //currentHistogram = 2; 90 | 91 | ru = (r >> factor[currentHistogram]) & (noBins[currentHistogram] - 1); 92 | gu = (g >> factor[currentHistogram]) & (noBins[currentHistogram] - 1); 93 | bu = (b >> factor[currentHistogram]) & (noBins[currentHistogram] - 1); 94 | pidx = (ru + gu * noBins[currentHistogram]) * noBins[currentHistogram] + bu; 95 | 96 | *foreground = normalised[histOffsets[currentHistogram] + pidx].x; 97 | *background = normalised[histOffsets[currentHistogram] + pidx].y; 98 | } 99 | 100 | void AddPoint(float foreground, float background, int r, int g, int b, int x, int y) 101 | { 102 | int i, ru, gu, bu, pidx; 103 | 104 | for (i=0; i> factor[i]) & (noBins[i] - 1); 107 | gu = (g >> factor[i]) & (noBins[i] - 1); 108 | bu = (b >> factor[i]) & (noBins[i] - 1); 109 | pidx = (ru + gu * noBins[i]) * noBins[i] + bu; 110 | 111 | notnormalised[histOffsets[i] + pidx].x += foreground; 112 | notnormalised[histOffsets[i] + pidx].y += background; 113 | } 114 | 115 | totalForegroundPixels += foreground; 116 | totalBackgroundPixels += background; 117 | 118 | if (!alreadyInitialised) 119 | { 120 | etaF += foreground; 121 | etaB += background; 122 | } 123 | } 124 | 125 | void Clear() 126 | { 127 | totalForegroundPixels = 0; 128 | totalBackgroundPixels = 0; 129 | etaF = 0; 130 | etaB = 0; 131 | 132 | memset(normalised, 0, fullHistSize * 2 * sizeof(float)); 133 | memset(notnormalised, 0, fullHistSize * 2 * sizeof(float)); 134 | 135 | alreadyInitialised = false; 136 | } 137 | 138 | void ClearNormalised() 139 | { 140 | memset(normalised, 0, fullHistSize * 2 * sizeof(float)); 141 | alreadyInitialised = false; 142 | 143 | totalForegroundPixels = 0; 144 | totalBackgroundPixels = 0; 145 | etaF = 0; 146 | etaB = 0; 147 | } 148 | 149 | void ClearNotNormalised() 150 | { 151 | memset(notnormalised, 0, fullHistSize * 2 * sizeof(float)); 152 | 153 | totalForegroundPixels = 0; 154 | totalBackgroundPixels = 0; 155 | etaF = 0; 156 | etaB = 0; 157 | } 158 | 159 | void ClearNotNormalisedPartial() 160 | { 161 | memset(notnormalised, 0, fullHistSize * 2 * sizeof(float)); 162 | 163 | totalForegroundPixels = 0; 164 | totalBackgroundPixels = 0; 165 | } 166 | 167 | void UpdateGPUFromCPU() 168 | { 169 | cudaMemcpy(normalisedGPU, normalised, sizeof(float2) * fullHistSize, cudaMemcpyHostToDevice); 170 | } 171 | 172 | void Free() 173 | { 174 | if (this->isAllocated) 175 | { 176 | delete normalised; 177 | delete notnormalised; 178 | 179 | cudaFree(normalisedGPU); 180 | } 181 | 182 | this->isAllocated = false; 183 | } 184 | 185 | ~HistogramVarBin() { this->Free(); } 186 | }; 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /PerseusLib/Objects/ImageRender.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | using namespace PerseusLib::Utils; 7 | 8 | namespace PerseusLib 9 | { 10 | namespace Objects 11 | { 12 | class ImageRender 13 | { 14 | public: 15 | bool isAllocated; 16 | 17 | int width, height; 18 | 19 | ImageUChar *imageFill; 20 | ImageUInt *imageZBuffer; 21 | ImageUInt *imageZBufferInverse; 22 | ImageUChar *imageObjects; 23 | 24 | void Clear() 25 | { 26 | imageFill->Clear(); 27 | } 28 | 29 | void ClearZBuffer() 30 | { 31 | imageObjects->Clear(); 32 | imageZBuffer->Clear(MAX_INT); 33 | imageZBufferInverse->Clear(); 34 | } 35 | 36 | ImageRender(int width, int height, bool useCudaAlloc) 37 | { 38 | this->width = width; 39 | this->height = height; 40 | 41 | this->imageFill = new ImageUChar(width, height, useCudaAlloc); 42 | this->imageZBuffer = new ImageUInt(width, height, useCudaAlloc); 43 | this->imageZBufferInverse = new ImageUInt(width, height, useCudaAlloc); 44 | this->imageObjects = new ImageUChar(width, height, useCudaAlloc); 45 | 46 | isAllocated = true; 47 | } 48 | 49 | void Free() 50 | { 51 | if (isAllocated) 52 | { 53 | imageFill->Free(); 54 | imageZBuffer->Free(); 55 | imageZBufferInverse->Free(); 56 | imageObjects->Free(); 57 | } 58 | 59 | isAllocated = false; 60 | } 61 | 62 | ~ImageRender() { this->Free(); } 63 | }; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /PerseusLib/Objects/IterationConfiguration.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace PerseusLib::Objects; 8 | 9 | #ifndef PERSEUS_MAX_OBJECT_COUNT 10 | #define PERSEUS_MAX_OBJECT_COUNT 20 11 | #endif 12 | 13 | #ifndef PERSEUS_MAX_VIEW_COUNT 14 | #define PERSEUS_MAX_VIEW_COUNT 10 15 | #endif 16 | 17 | 18 | #ifndef PERSEUS_MAX_ITER_COUNT 19 | #define PERSEUS_MAX_ITER_COUNT 160 20 | #endif 21 | 22 | namespace PerseusLib 23 | { 24 | namespace Objects 25 | { 26 | class IterationConfiguration 27 | { 28 | public: 29 | int iterCount; 30 | 31 | int width; 32 | int height; 33 | 34 | int levelSetBandSize; 35 | 36 | int iterObjectCount[PERSEUS_MAX_VIEW_COUNT]; 37 | int iterViewCount; 38 | 39 | int iterObjectIds[PERSEUS_MAX_VIEW_COUNT][PERSEUS_MAX_OBJECT_COUNT]; 40 | int iterViewIds[PERSEUS_MAX_VIEW_COUNT]; 41 | 42 | IterationTarget iterTarget[PERSEUS_MAX_ITER_COUNT]; 43 | 44 | bool useCUDARender; 45 | bool useCUDAEF; 46 | 47 | IterationConfiguration(void) { 48 | int i; 49 | iterViewCount = 0; iterCount = 1; 50 | for (i=0; i 4 | 5 | namespace PerseusLib 6 | { 7 | namespace Objects 8 | { 9 | class Object3DParams 10 | { 11 | public: 12 | int numberOfOptimizedVariables; 13 | int noVarBinHistograms; // max 8 14 | int noVarBinHistogramBins[4]; 15 | 16 | Object3DParams() { 17 | noVarBinHistogramBins[0] = 8; 18 | noVarBinHistogramBins[1] = 16; 19 | noVarBinHistogramBins[2] = 32; 20 | noVarBinHistogramBins[3] = 64; 21 | noVarBinHistograms = 4; 22 | numberOfOptimizedVariables = 7; 23 | // printf("Finish init Object3DParams, noVarBinHistograms %d \n", noVarBinHistogramBins); 24 | } 25 | 26 | ~Object3DParams(void) 27 | { 28 | 29 | } 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PerseusLib/Objects/Pose3D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | using namespace PerseusLib::Primitives; 7 | using namespace Renderer::Primitives; 8 | 9 | namespace PerseusLib 10 | { 11 | namespace Objects 12 | { 13 | class Pose3D 14 | { 15 | public: 16 | Quaternion *rotation; 17 | VECTOR3DA *translation; 18 | 19 | void SetFrom(float *pose, int poseSize) 20 | { 21 | if (poseSize == 4) this->SetFrom(pose[0], pose[1], pose[2], pose[3], 0.0f, 0.0f); 22 | if (poseSize == 6) this->SetFrom(pose[0], pose[1], pose[2], pose[3], pose[4], pose[5]); 23 | if (poseSize == 7) this->SetFrom(pose[0], pose[1], pose[2], pose[3], pose[4], pose[5], pose[6]); 24 | } 25 | 26 | void SetFrom(Pose3D *pose) { 27 | this->translation->x = pose->translation->x; 28 | this->translation->y = pose->translation->y; 29 | this->translation->z = pose->translation->z; 30 | this->rotation->vector4d.x = pose->rotation->vector4d.x; 31 | this->rotation->vector4d.y = pose->rotation->vector4d.y; 32 | this->rotation->vector4d.z = pose->rotation->vector4d.z; 33 | this->rotation->vector4d.w = pose->rotation->vector4d.w; 34 | } 35 | 36 | void SetFrom(VECTOR3DA* translation, Quaternion* rotation){ 37 | // printf("[Pose3D/SetFrom] 3 \n"); 38 | 39 | this->rotation->Set(rotation); 40 | this->translation->x = translation->x; 41 | this->translation->y = translation->y; 42 | this->translation->z = translation->z; 43 | } 44 | 45 | void SetFrom(VFLOAT tX, VFLOAT tY, VFLOAT tZ, VFLOAT rX, VFLOAT rY, VFLOAT rZ) { 46 | // printf("[Pose3D/SetFrom] 4 \n"); 47 | 48 | this->translation->x = tX; 49 | this->translation->y = tY; 50 | this->translation->z = tZ; 51 | this->rotation->SetFromEuler(rX,rY,rZ); 52 | } 53 | 54 | void SetFrom(VFLOAT tX, VFLOAT tY, VFLOAT tZ, VFLOAT rX, VFLOAT rY, VFLOAT rZ, VFLOAT rW) { 55 | // printf("[Pose3D/SetFrom] 5 \n"); 56 | 57 | this->translation->x = tX; 58 | this->translation->y = tY; 59 | this->translation->z = tZ; 60 | this->rotation->vector4d.x = rX; 61 | this->rotation->vector4d.y = rY; 62 | this->rotation->vector4d.z = rZ; 63 | this->rotation->vector4d.w = rW; 64 | } 65 | 66 | void GetModelViewMatrix(float *returnMatrix) 67 | { 68 | // printf("[Pose3D/GetModelViewMatrix] \n"); 69 | 70 | VFLOAT matrixFromSource[16]; 71 | rotation->GetMatrix(matrixFromSource); 72 | 73 | returnMatrix[0] = matrixFromSource[0]; 74 | returnMatrix[1] = matrixFromSource[1]; 75 | returnMatrix[2] = matrixFromSource[2]; 76 | returnMatrix[3] = 0.0f; 77 | 78 | returnMatrix[4] = matrixFromSource[4]; 79 | returnMatrix[5] = matrixFromSource[5]; 80 | returnMatrix[6] = matrixFromSource[6]; 81 | returnMatrix[7] = 0.0f; 82 | 83 | returnMatrix[8] = matrixFromSource[8]; 84 | returnMatrix[9] = matrixFromSource[9]; 85 | returnMatrix[10] = matrixFromSource[10]; 86 | returnMatrix[11] = 0.0f; 87 | 88 | returnMatrix[12] = translation->x; 89 | returnMatrix[13] = translation->y; 90 | returnMatrix[14] = translation->z; 91 | returnMatrix[15] = 1.0f; 92 | 93 | } 94 | 95 | void CopyInto(Pose3D *targetPose) { 96 | // printf("[Pose3D/CopyInto] \n"); 97 | 98 | targetPose->translation->x = this->translation->x; 99 | targetPose->translation->y = this->translation->y; 100 | targetPose->translation->z = this->translation->z; 101 | targetPose->rotation->vector4d.x = this->rotation->vector4d.x; 102 | targetPose->rotation->vector4d.y = this->rotation->vector4d.y; 103 | targetPose->rotation->vector4d.z = this->rotation->vector4d.z; 104 | targetPose->rotation->vector4d.w = this->rotation->vector4d.w; 105 | } 106 | 107 | void Clear() 108 | { 109 | this->translation->x = 0; this->translation->y = 0; this->translation->z = 0; 110 | this->rotation->vector4d.x = 0; this->rotation->vector4d.y = 0; this->rotation->vector4d.z = 0; this->rotation->vector4d.w = 0; 111 | } 112 | 113 | Pose3D(VFLOAT tX, VFLOAT tY, VFLOAT tZ, VFLOAT rX, VFLOAT rY, VFLOAT rZ) { 114 | rotation = new Quaternion(); translation = new VECTOR3DA(); 115 | this->SetFrom(tX, tY, tZ, rX, rY, rZ); 116 | } 117 | 118 | Pose3D(float *pose, int poseSize) { 119 | rotation = new Quaternion(); translation = new VECTOR3DA(); 120 | this->SetFrom(pose, poseSize); 121 | } 122 | 123 | Pose3D(void) { rotation = new Quaternion(); translation = new VECTOR3DA(); } 124 | ~Pose3D(void) { if (rotation) delete rotation; if (translation) delete translation; } 125 | }; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /PerseusLib/Objects/StepSize3D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace PerseusLib 4 | { 5 | namespace Objects 6 | { 7 | class StepSize3D 8 | { 9 | public: 10 | float tX, tY, tZ, r; 11 | 12 | void SetFrom(float r, float tX, float tY, float tZ) 13 | { 14 | this->r = r; this->tX = tX; this->tY = tY; this->tZ = tZ; 15 | } 16 | 17 | void SetFrom(float *stepSize) 18 | { 19 | this->r = stepSize[0]; this->tX = stepSize[1]; 20 | this->tY = stepSize[2]; this->tZ = stepSize[3]; 21 | } 22 | 23 | StepSize3D(void) { 24 | this->SetFrom(0.0f, 0.0f, 0.0f, 0.0f); 25 | } 26 | 27 | StepSize3D(float r, float tX, float tY, float tZ) { 28 | this->SetFrom(r, tX, tY, tZ); 29 | } 30 | 31 | ~StepSize3D(void) { } 32 | }; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /PerseusLib/Objects/View3D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | using namespace PerseusLib::Objects; 18 | 19 | using namespace Renderer::Primitives; 20 | using namespace Renderer::Transforms; 21 | using namespace Renderer::Objects; 22 | 23 | using namespace PerseusLib::Utils; 24 | 25 | namespace PerseusLib 26 | { 27 | namespace Objects 28 | { 29 | class View3D 30 | { 31 | public: 32 | int viewId; 33 | 34 | float zBufferOffset; 35 | 36 | VUINT roiGeneratedAll[6]; 37 | 38 | Renderer3DView* renderView; 39 | 40 | ImageRender *imageRenderAll; 41 | 42 | ImageRender *imageHistogramMaskAll; 43 | ImageUChar *imageWireframeAll; 44 | 45 | ImageUChar *imagePosteriors; 46 | ImageUChar4 *imageRegistered; 47 | ImageUChar4 *imageRegisteredPrev; 48 | ImageUChar4 *imageProximity; 49 | 50 | ImageUChar *videoMask; 51 | 52 | View3D(int viewIdx, char* cameraCalibFileName, int width, int height, View3DParams* params = NULL) { 53 | if (params == NULL) 54 | { 55 | params = new View3DParams(); 56 | } 57 | 58 | if(params->zFar <0 || params->zNear<0) 59 | { 60 | printf("error! invalid zFar value. \n"); 61 | } 62 | 63 | this->viewId = viewIdx; 64 | this->zBufferOffset = params->zBufferOffset; 65 | 66 | renderView = new Renderer3DView(width, height, cameraCalibFileName, params->zNear, params->zFar, viewIdx); 67 | 68 | imageRenderAll = new ImageRender(width, height, true); 69 | 70 | imageHistogramMaskAll = new ImageRender(width, height, false); 71 | imageWireframeAll = new ImageUChar(width, height, false); 72 | 73 | imagePosteriors = new ImageUChar(width, height, false); 74 | 75 | imageRegistered = new ImageUChar4(width, height, true); 76 | imageRegisteredPrev = new ImageUChar4(width, height, false); 77 | imageProximity = new ImageUChar4(width, height, false); 78 | 79 | videoMask = new ImageUChar(width, height, false); 80 | } 81 | 82 | View3D(int viewIdx, float fSizeX, float fSizeY, 83 | float fFocalLengthX, float fFocalLengthY, 84 | float fCenterPointX, float fCenterPointY, 85 | int width, int height, float fzNear, float fzFar, View3DParams* params = NULL) 86 | { 87 | if (params == NULL) 88 | { 89 | params = new View3DParams(fzFar, fzNear); 90 | } 91 | 92 | if(params->zFar <0 || params->zNear<0) 93 | { 94 | printf("error! invalid zFar value. \n"); 95 | } 96 | 97 | this->viewId = viewIdx; 98 | this->zBufferOffset = params->zBufferOffset; 99 | 100 | renderView = new Renderer3DView(width, height, fSizeX, fSizeY, 101 | fFocalLengthX, fFocalLengthY, 102 | fCenterPointX, fCenterPointY, 103 | params->zNear, params->zFar, viewIdx); 104 | 105 | imageRenderAll = new ImageRender(width, height, true); 106 | 107 | imageHistogramMaskAll = new ImageRender(width, height, false); 108 | imageWireframeAll = new ImageUChar(width, height, false); 109 | 110 | imagePosteriors = new ImageUChar(width, height, false); 111 | 112 | imageRegistered = new ImageUChar4(width, height, true); 113 | imageRegisteredPrev = new ImageUChar4(width, height, false); 114 | imageProximity = new ImageUChar4(width, height, false); 115 | 116 | videoMask = new ImageUChar(width, height, false); 117 | } 118 | 119 | 120 | ~View3D() { 121 | delete imageRenderAll; 122 | 123 | delete imageHistogramMaskAll; 124 | delete imageWireframeAll; 125 | 126 | delete imagePosteriors; 127 | 128 | delete imageRegistered; 129 | delete imageRegisteredPrev; 130 | delete imageProximity; 131 | 132 | delete videoMask; 133 | 134 | delete renderView; 135 | } 136 | }; 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /PerseusLib/Objects/View3DParams.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace PerseusLib 4 | { 5 | namespace Objects 6 | { 7 | class View3DParams 8 | { 9 | public: 10 | float zNear, zFar; 11 | float zBufferOffset; 12 | 13 | View3DParams(void) { 14 | zBufferOffset = 0.0001f; 15 | zFar = 50.0f; 16 | zNear = 0.01f; 17 | } 18 | 19 | 20 | View3DParams(float f_zFar, float f_zNear) { 21 | zBufferOffset = 0.0001f; 22 | zFar = f_zFar; 23 | zNear = f_zNear; 24 | } 25 | 26 | ~View3DParams(void) {} 27 | }; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /PerseusLib/Optimiser/EFs/EFStandard.cpp: -------------------------------------------------------------------------------- 1 | #include "EFStandard.h" 2 | 3 | using namespace PerseusLib::Optimiser; 4 | 5 | #include 6 | 7 | #include 8 | using namespace PerseusLib::Utils; 9 | 10 | 11 | EFStandard::EFStandard(void) 12 | { 13 | } 14 | 15 | EFStandard::~EFStandard(void) 16 | { 17 | } 18 | 19 | void EFStandard::PrepareIteration(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig) 20 | { 21 | Object3D* object; View3D* view; int objectIdx, viewIdx; 22 | 23 | for (viewIdx = 0; viewIdx 1) DrawingEngine::Instance()->DrawAllInView(objects[viewIdx], objectCount[viewIdx], view, iterConfig->useCUDARender, true); 29 | 30 | for (objectIdx = 0; objectIdx < objectCount[viewIdx]; objectIdx++) 31 | { 32 | object = objects[viewIdx][objectIdx]; 33 | 34 | // render object 35 | DrawingEngine::Instance()->Draw(object, view, iterConfig->useCUDARender, !iterConfig->useCUDAEF); 36 | DrawingEngine::Instance()->ChangeROIWithBand(object, view, iterConfig->levelSetBandSize, iterConfig->width, iterConfig->height); 37 | 38 | registerObjectImage(object, view, iterConfig->useCUDARender, (objectCount[viewIdx] > 1)); 39 | 40 | processDTSihluetteLSDXDY(object, view, iterConfig->levelSetBandSize); 41 | } 42 | } 43 | } 44 | 45 | void EFStandard::GetFirstDerivativeValues(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig) 46 | { 47 | int objectIdx, viewIdx; 48 | Object3D* object; View3D* view; 49 | 50 | if (iterConfig->useCUDAEF) 51 | { 52 | for (viewIdx = 0; viewIdx < viewCount; viewIdx++) for (objectIdx = 0; objectIdx < objectCount[viewIdx]; objectIdx++) 53 | { 54 | object = objects[viewIdx][objectIdx]; view = views[viewIdx]; 55 | 56 | registerObjectAndViewGeometricData(object, view); 57 | 58 | processAndGetEFFirstDerivatives(object, view, (objectCount[viewIdx] > 1)); 59 | } 60 | return; 61 | } 62 | 63 | this->GetFirstDerivativeValues_CPU_6DoF(objects, objectCount, views, viewCount, iterConfig); 64 | } 65 | 66 | void EFStandard::GetFirstDerivativeValues_CPU_6DoF(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig) 67 | { 68 | int objectIdx, viewIdx, objectId, viewId; 69 | Object3D* object; View3D* view; 70 | 71 | int width = iterConfig->width, height = iterConfig->height; 72 | 73 | int i, j, k, idx, icX, icY, icZ, nhidx; 74 | float pYB, pYF, dtIdx, dfPPGeneric, dirac, heaviside; 75 | unsigned char r, b, g; 76 | int *dtPosX, *dtPosY; 77 | float *dt, *dtDX, *dtDY; 78 | float xProjected[4], xUnprojected[4], xUnrotated[4], dfPP[7], dpose[7], otherInfo[2]; 79 | 80 | for (viewIdx = 0; viewIdxviewId; objectId = object->objectId; 85 | 86 | dt = object->dt[viewId]->pixels; 87 | dtPosX = object->dtPosX[viewId]->pixels; dtPosY = object->dtPosY[viewId]->pixels; 88 | dtDX = object->dtDX[viewId]->pixels; dtDY = object->dtDY[viewId]->pixels; 89 | 90 | getProcessedDataDTSihluetteLSDXDY(object, view); 91 | 92 | // init dpose to 0 93 | for (i=0; i<7; i++) dpose[i] = 0; 94 | 95 | // for each pixel in the image 96 | for (j=0, idx=0; j= 0)// && view->videoMask->pixels[idx] > 128) 99 | { 100 | dtIdx = dt[idx]; 101 | 102 | icX = i; icY = j; 103 | if (dtIdx < 0) { 104 | icX = dtPosX[idx] + object->roiGenerated[viewId][0]; 105 | icY = dtPosY[idx] + object->roiGenerated[viewId][1]; 106 | } 107 | icZ = icX + icY * width; 108 | 109 | if (objectCount[viewIdx] > 1) 110 | if (((view->imageRenderAll->imageObjects->pixels[icZ]-1) != objectId) || 111 | ((view->imageRenderAll->imageObjects->pixels[i + j * width] - 1) != objectId && (view->imageRenderAll->imageObjects->pixels[i + j * width] - 1) != -1 )) 112 | continue; 113 | 114 | nhidx = int(4096 + 512 * dtIdx); 115 | 116 | // for a vaild pixel 117 | if (nhidx >= 0 && nhidx < MathUtils::Instance()->heavisideSize) 118 | { 119 | heaviside = MathUtils::Instance()->heavisideFunction[nhidx]; 120 | 121 | r = view->imageRegistered->pixels[idx].x; g = view->imageRegistered->pixels[idx].y; b = view->imageRegistered->pixels[idx].z; 122 | 123 | object->histogramVarBin[viewId]->GetValue(&pYF, &pYB, r, g, b, i, j); 124 | 125 | pYF += 0.0000001f; pYB += 0.0000001f; 126 | 127 | dirac = (1.0f / float(PI)) * (1.0f / (dtIdx * dtIdx + 1.0f) + float(1e-3)); 128 | 129 | // Get the derivate of PWP3D. this is for update 130 | dfPPGeneric = dirac * (pYF - pYB) / (heaviside * (pYF - pYB) + pYB); 131 | 132 | /// ------- run 1 133 | xProjected[0] = (float) 2 * (icX - view->renderView->view[0]) / view->renderView->view[2] - 1.0f; 134 | xProjected[1] = (float) 2 * (icY - view->renderView->view[1]) / view->renderView->view[3] - 1.0f; 135 | xProjected[2] = (float) 2 * ((float)object->imageRender[viewId]->imageZBuffer->pixels[icZ] / (float)MAX_INT) - 1.0f; 136 | xProjected[3] = 1.0f; 137 | 138 | MathUtils::Instance()->MatrixVectorProduct4(view->renderView->invP, xProjected, xUnprojected); 139 | MathUtils::Instance()->MatrixVectorProduct4(object->invPMMatrix[viewId], xProjected, xUnrotated); 140 | 141 | otherInfo[0] = view->renderView->projectionParams.A * dtDX[idx]; 142 | otherInfo[1] = view->renderView->projectionParams.B * dtDY[idx]; 143 | 144 | dfPP[0] = -otherInfo[0] / xUnprojected[2]; 145 | dfPP[1] = -otherInfo[1] / xUnprojected[2]; 146 | dfPP[2] = (otherInfo[0] * xUnprojected[0] + otherInfo[1] * xUnprojected[1]) / (xUnprojected[2] * xUnprojected[2]); 147 | 148 | object->renderObject->objectCoordinateTransform[viewId]->rotation->GetDerivatives(dfPP + 3, xUnprojected, xUnrotated, 149 | view->renderView->projectionParams.all, otherInfo); 150 | 151 | for (k=0; k<7; k++) { 152 | dfPP[k] *= dfPPGeneric; 153 | dpose[k] += dfPP[k]; 154 | } 155 | 156 | /// -------- run 2 157 | xProjected[0] = (float) 2 * (icX - view->renderView->view[0]) / view->renderView->view[2] - 1.0f; 158 | xProjected[1] = (float) 2 * (icY - view->renderView->view[1]) / view->renderView->view[3] - 1.0f; 159 | xProjected[2] = (float) 2 * ((float)object->imageRender[viewId]->imageZBufferInverse->pixels[icZ] / (float)MAX_INT) - 1.0f; 160 | xProjected[3] = 1.0f; 161 | 162 | MathUtils::Instance()->MatrixVectorProduct4(view->renderView->invP, xProjected, xUnprojected); 163 | MathUtils::Instance()->MatrixVectorProduct4(object->invPMMatrix[viewId], xProjected, xUnrotated); 164 | 165 | otherInfo[0] = view->renderView->projectionParams.A * dtDX[idx]; 166 | otherInfo[1] = view->renderView->projectionParams.B * dtDY[idx]; 167 | 168 | dfPP[0] = -otherInfo[0] / xUnprojected[2]; 169 | dfPP[1] = -otherInfo[1] / xUnprojected[2]; 170 | dfPP[2] = (otherInfo[0] * xUnprojected[0] + otherInfo[1] * xUnprojected[1]) / (xUnprojected[2] * xUnprojected[2]); 171 | 172 | object->renderObject->objectCoordinateTransform[viewId]->rotation->GetDerivatives(dfPP + 3, xUnprojected, xUnrotated, 173 | view->renderView->projectionParams.all, otherInfo); 174 | 175 | for (k=0; k<7; k++) { 176 | dfPP[k] *= dfPPGeneric; 177 | dpose[k] += dfPP[k]; 178 | } 179 | 180 | } 181 | } 182 | } 183 | 184 | object->dpose[viewId]->SetFrom(dpose, 7); 185 | 186 | //char rez[200]; 187 | //sprintf(rez, "%4.5f %4.5f %4.5f %4.5f %4.5f %4.5f %4.5f", object->dpose[viewId]->translation->x, object->dpose[viewId]->translation->y, 188 | // object->dpose[viewId]->translation->z, object->dpose[viewId]->rotation->vector4d.x, object->dpose[viewId]->rotation->vector4d.y, 189 | // object->dpose[viewId]->rotation->vector4d.z, object->dpose[viewId]->rotation->vector4d.w); 190 | 191 | //DEBUGBREAK; 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /PerseusLib/Optimiser/EFs/EFStandard.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace PerseusLib 6 | { 7 | namespace Optimiser 8 | { 9 | class EFStandard: public IEnergyFunction 10 | { 11 | private: 12 | void GetFirstDerivativeValues_CPU_6DoF(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig); 13 | 14 | public: 15 | void PrepareIteration(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig); 16 | void GetFirstDerivativeValues(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig); 17 | 18 | EFStandard(void); 19 | ~EFStandard(void); 20 | }; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PerseusLib/Optimiser/EFs/IEnergyFunction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | using namespace PerseusLib::Objects; 10 | using namespace Renderer::Engine; 11 | 12 | namespace PerseusLib 13 | { 14 | namespace Optimiser 15 | { 16 | class IEnergyFunction 17 | { 18 | public: 19 | virtual void GetFirstDerivativeValues(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig) = 0; 20 | virtual void PrepareIteration(Object3D ***objects, int *objectCount, View3D** views, int viewCount, IterationConfiguration* iterConfig) = 0; 21 | 22 | IEnergyFunction(void) { } 23 | virtual ~IEnergyFunction(void) { } 24 | }; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /PerseusLib/Optimiser/Engine/OptimisationEngine.cpp: -------------------------------------------------------------------------------- 1 | #include "OptimisationEngine.h" 2 | 3 | #include 4 | 5 | using namespace PerseusLib::Optimiser; 6 | 7 | OptimisationEngine* OptimisationEngine::instance; 8 | 9 | OptimisationEngine::OptimisationEngine(void) { } 10 | OptimisationEngine::~OptimisationEngine(void) { } 11 | 12 | void OptimisationEngine::Initialise(int width, int height) 13 | { 14 | int i; 15 | 16 | objects = new Object3D**[PERSEUS_MAX_VIEW_COUNT]; 17 | views = new View3D*[PERSEUS_MAX_VIEW_COUNT]; 18 | for (i=0; iSetPresetStepSizes(); 28 | 29 | MathUtils::Instance()->ReadAndAllocateHeaviside(8192, "/Users/luma/Code/Luma/PWP3D/Files/Others/heaviside.txt"); 30 | 31 | initialiseCUDA(width, height, MathUtils::Instance()->heavisideFunction, MathUtils::Instance()->heavisideSize); 32 | 33 | } 34 | 35 | void OptimisationEngine::Shutdown() 36 | { 37 | int i; 38 | 39 | shutdownCUDA(); 40 | 41 | for (i=0; i<8; i++) delete stepSizes[i]; 42 | 43 | delete objectCount; 44 | 45 | delete stepSizes; 46 | delete energyFunction_standard; 47 | 48 | MathUtils::Instance()->DeallocateHeaviside(); 49 | 50 | delete instance; 51 | } 52 | 53 | void OptimisationEngine::SetPresetStepSizes() 54 | { 55 | stepSizes[0]->tX = -0.005f; stepSizes[0]->tY = -0.005f; stepSizes[0]->tZ = -0.005f; stepSizes[0]->r = -0.0008f; 56 | 57 | stepSizes[1]->tX = -0.003f; stepSizes[1]->tY = -0.003f; stepSizes[1]->tZ = -0.003f; stepSizes[1]->r = -0.0003f; 58 | stepSizes[2]->tX = -0.003f; stepSizes[2]->tY = -0.003f; stepSizes[2]->tZ = -0.003f; stepSizes[2]->r = -0.0003f; 59 | 60 | stepSizes[3]->tX = -0.002f; stepSizes[3]->tY = -0.002f; stepSizes[3]->tZ = -0.003f; stepSizes[3]->r = -0.0003f; 61 | stepSizes[4]->tX = -0.002f; stepSizes[4]->tY = -0.002f; stepSizes[4]->tZ = -0.003f; stepSizes[4]->r = -0.0003f; 62 | stepSizes[5]->tX = -0.002f; stepSizes[5]->tY = -0.002f; stepSizes[5]->tZ = -0.003f; stepSizes[5]->r = -0.0003f; 63 | 64 | stepSizes[6]->tX = -0.001f; stepSizes[6]->tY = -0.001f; stepSizes[6]->tZ = -0.002f; stepSizes[6]->r = -0.0002f; 65 | stepSizes[7]->tX = -0.001f; stepSizes[7]->tY = -0.001f; stepSizes[7]->tZ = -0.002f; stepSizes[7]->r = -0.0002f; 66 | } 67 | 68 | void OptimisationEngine::RegisterViewImage(View3D *view, ImageUChar4* image) 69 | { 70 | ImageUtils::Instance()->Copy(image, view->imageRegistered); 71 | view->imageRegistered->UpdateGPUFromCPU(); 72 | } 73 | 74 | void OptimisationEngine::Minimise(Object3D **objects, View3D **views, IterationConfiguration *iterConfig) 75 | { 76 | int objectIdx, viewIdx, iterIdx; 77 | 78 | this->iterConfig = iterConfig; 79 | 80 | viewCount = iterConfig->iterViewCount; 81 | for (viewIdx=0; viewIdxviews[viewIdx] = views[iterConfig->iterViewIds[viewIdx]]; 84 | this->objectCount[viewIdx] = iterConfig->iterObjectCount[viewIdx]; 85 | } 86 | 87 | for (viewIdx=0; viewIdxobjects[viewIdx][objectIdx] = objects[iterConfig->iterObjectIds[viewIdx][objectIdx]]; 90 | this->objects[viewIdx][objectIdx]->initialPose[viewIdx]->CopyInto(this->objects[viewIdx][objectIdx]->pose[viewIdx]); 91 | this->objects[viewIdx][objectIdx]->UpdateRendererFromPose(views[viewIdx]); 92 | } 93 | 94 | energyFunction = energyFunction_standard; 95 | 96 | for (iterIdx=0; iterIdx< iterConfig->iterCount; iterIdx++) 97 | { 98 | this->RunOneMultiIteration(iterConfig); 99 | } 100 | } 101 | 102 | 103 | void OptimisationEngine::MinimiseSingle(Object3D **objects, View3D **views, IterationConfiguration *iterConfig, int nIterNum) 104 | { 105 | int objectIdx, viewIdx, iterIdx; 106 | 107 | this->iterConfig = iterConfig; 108 | 109 | viewCount = iterConfig->iterViewCount; 110 | for (viewIdx=0; viewIdxviews[viewIdx] = views[iterConfig->iterViewIds[viewIdx]]; 113 | this->objectCount[viewIdx] = iterConfig->iterObjectCount[viewIdx]; 114 | } 115 | 116 | for (viewIdx=0; viewIdxobjects[viewIdx][objectIdx] = objects[iterConfig->iterObjectIds[viewIdx][objectIdx]]; 119 | this->objects[viewIdx][objectIdx]->initialPose[viewIdx]->CopyInto(this->objects[viewIdx][objectIdx]->pose[viewIdx]); 120 | this->objects[viewIdx][objectIdx]->UpdateRendererFromPose(views[viewIdx]); 121 | } 122 | 123 | energyFunction = energyFunction_standard; 124 | 125 | for (iterIdx=0; iterIdx< iterConfig->iterCount; iterIdx++) 126 | { 127 | if(nIterNum >7 || nIterNum<0){ 128 | std::cout<<"fatal error! nIterNum must be from 0 to 7"<RunOneSingleIteration(stepSizes[nIterNum], iterConfig); if (this->HasConverged()) return; 133 | } 134 | } 135 | 136 | void OptimisationEngine::RunOneMultiIteration(IterationConfiguration* iterConfig) 137 | { 138 | this->RunOneSingleIteration(stepSizes[0], iterConfig); if (this->HasConverged()) return; 139 | this->RunOneSingleIteration(stepSizes[1], iterConfig); if (this->HasConverged()) return; 140 | this->RunOneSingleIteration(stepSizes[2], iterConfig); if (this->HasConverged()) return; 141 | this->RunOneSingleIteration(stepSizes[3], iterConfig); if (this->HasConverged()) return; 142 | this->RunOneSingleIteration(stepSizes[4], iterConfig); if (this->HasConverged()) return; 143 | this->RunOneSingleIteration(stepSizes[5], iterConfig); if (this->HasConverged()) return; 144 | this->RunOneSingleIteration(stepSizes[6], iterConfig); if (this->HasConverged()) return; 145 | this->RunOneSingleIteration(stepSizes[7], iterConfig); if (this->HasConverged()) return; 146 | 147 | this->NormaliseRotation(); 148 | } 149 | 150 | void OptimisationEngine::RunOneSingleIteration(StepSize3D* presetStepSize, IterationConfiguration* iterConfig) 151 | { 152 | energyFunction->PrepareIteration(objects, objectCount, views, viewCount, iterConfig); 153 | 154 | // update the pose of the object 155 | energyFunction->GetFirstDerivativeValues(objects, objectCount, views, viewCount, iterConfig); 156 | 157 | this->DescendWithGradient(presetStepSize, iterConfig); 158 | } 159 | 160 | void OptimisationEngine::DescendWithGradient(StepSize3D *presetStepSize, IterationConfiguration *iterConfig) 161 | { 162 | int objectIdx, viewIdx; 163 | 164 | StepSize3D actualStepSize; 165 | 166 | for (viewIdx = 0; viewIdx < viewCount; viewIdx++) for (objectIdx = 0; objectIdx < objectCount[viewIdx]; objectIdx++) 167 | { 168 | actualStepSize.r = presetStepSize->r * objects[viewIdx][objectIdx]->stepSize[viewIdx]->r; 169 | actualStepSize.tX = presetStepSize->tX * objects[viewIdx][objectIdx]->stepSize[viewIdx]->tX; 170 | actualStepSize.tY = presetStepSize->tY * objects[viewIdx][objectIdx]->stepSize[viewIdx]->tY; 171 | actualStepSize.tZ = presetStepSize->tZ * objects[viewIdx][objectIdx]->stepSize[viewIdx]->tZ; 172 | 173 | switch (iterConfig->iterTarget[0]) 174 | { 175 | case ITERATIONTARGET_BOTH: 176 | AdvanceTranslation(objects[viewIdx][objectIdx], views[viewIdx], &actualStepSize); 177 | 178 | AdvanceRotation(objects[viewIdx][objectIdx], views[viewIdx], &actualStepSize); 179 | 180 | break; 181 | case ITERATIONTARGET_TRANSLATION: 182 | AdvanceTranslation(objects[viewIdx][objectIdx], views[viewIdx], &actualStepSize); 183 | break; 184 | case ITERATIONTARGET_ROTATION: 185 | AdvanceRotation(objects[viewIdx][objectIdx], views[viewIdx], &actualStepSize); 186 | break; 187 | } 188 | 189 | objects[viewIdx][objectIdx]->UpdateRendererFromPose(views[viewIdx]); 190 | } 191 | } 192 | void OptimisationEngine::AdvanceTranslation(Object3D* object, View3D* view, StepSize3D* stepSize) 193 | { 194 | object->pose[view->viewId]->translation->x -= stepSize->tX * object->dpose[view->viewId]->translation->x; 195 | object->pose[view->viewId]->translation->y -= stepSize->tY * object->dpose[view->viewId]->translation->y; 196 | object->pose[view->viewId]->translation->z -= stepSize->tZ * object->dpose[view->viewId]->translation->z; 197 | } 198 | void OptimisationEngine::AdvanceRotation(Object3D* object, View3D* view, StepSize3D* stepSize) 199 | { 200 | object->pose[view->viewId]->rotation->vector4d.x -= stepSize->r * object->dpose[view->viewId]->rotation->vector4d.x; 201 | object->pose[view->viewId]->rotation->vector4d.y -= stepSize->r * object->dpose[view->viewId]->rotation->vector4d.y; 202 | object->pose[view->viewId]->rotation->vector4d.z -= stepSize->r * object->dpose[view->viewId]->rotation->vector4d.z; 203 | object->pose[view->viewId]->rotation->vector4d.w -= stepSize->r * object->dpose[view->viewId]->rotation->vector4d.w; 204 | } 205 | 206 | void OptimisationEngine::NormaliseRotation() 207 | { 208 | int objectIdx, viewIdx; 209 | for (viewIdx = 0; viewIdx < viewCount; viewIdx++) for (objectIdx = 0; objectIdx < objectCount[viewIdx]; objectIdx++) 210 | { 211 | objects[viewIdx][objectIdx]->pose[viewIdx]->rotation->Normalize(); 212 | objects[viewIdx][objectIdx]->UpdateRendererFromPose(views[viewIdx]); 213 | } 214 | } 215 | 216 | bool OptimisationEngine::HasConverged() 217 | { 218 | return false; 219 | } 220 | -------------------------------------------------------------------------------- /PerseusLib/Optimiser/Engine/OptimisationEngine.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace PerseusLib::Primitives; 13 | using namespace PerseusLib::Objects; 14 | using namespace PerseusLib::Utils; 15 | 16 | namespace PerseusLib 17 | { 18 | namespace Optimiser 19 | { 20 | class OptimisationEngine 21 | { 22 | private: 23 | static OptimisationEngine* instance; 24 | 25 | IEnergyFunction *energyFunction; 26 | IEnergyFunction *energyFunction_standard; 27 | 28 | IterationConfiguration *iterConfig; 29 | 30 | int viewCount; 31 | int *objectCount; 32 | Object3D*** objects; 33 | View3D** views; 34 | 35 | StepSize3D **stepSizes; 36 | 37 | bool HasConverged(); 38 | 39 | void SetPresetStepSizes(); 40 | 41 | void NormaliseRotation(); 42 | 43 | void DescendWithGradient(StepSize3D *stepSize, IterationConfiguration *iterConfig); 44 | void AdvanceTranslation(Object3D* object, View3D* view, StepSize3D* stepSize); 45 | void AdvanceRotation(Object3D* object, View3D* view, StepSize3D* stepSize); 46 | 47 | void RunOneMultiIteration(IterationConfiguration* iterConfig); 48 | void RunOneSingleIteration(StepSize3D* stepSize, IterationConfiguration* iterConfig); 49 | 50 | public: 51 | static OptimisationEngine* Instance(void) { 52 | if (instance == NULL) instance = new OptimisationEngine(); 53 | return instance; 54 | } 55 | 56 | void Initialise(int width, int heigh); 57 | void Shutdown(); 58 | 59 | void RegisterViewImage(View3D *view, ImageUChar4* image); 60 | void Minimise(Object3D **objects, View3D **views, IterationConfiguration *iterConfig); 61 | void MinimiseSingle(Object3D **objects, View3D **views, IterationConfiguration *iterConfig, int nIterNum); 62 | 63 | OptimisationEngine(void); 64 | ~OptimisationEngine(void); 65 | }; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /PerseusLib/Others/PerseusLibDefines.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef SUPPORTED_GETIMAGE_TYPES 4 | #define SUPPORTED_GETIMAGE_TYPES 5 | enum GetImageType 6 | { 7 | GETIMAGE_WIREFRAME, 8 | GETIMAGE_FILL, 9 | GETIMAGE_ORIGINAL, 10 | GETIMAGE_POSTERIORS, 11 | GETIMAGE_OBJECTS, 12 | GETIMAGE_SIHLUETTE, 13 | GETIMAGE_DT, 14 | GETIMAGE_PROXIMITY 15 | }; 16 | #endif 17 | 18 | #ifndef COLUMN 19 | #define COLUMN(columnId, index, width) ((columnId) + ((width) * (index))) 20 | #endif 21 | 22 | #ifndef ROW 23 | #define ROW(rowId, index, width) (((rowId) * (width)) + (index)) 24 | #endif 25 | 26 | #ifndef PIXELMAT 27 | #define PIXELMAT(image, i, j, stride) image[(int)(i)+(int)(j)*(int)(stride)] 28 | #endif 29 | 30 | #ifndef PIXELMATINDEX 31 | #define PIXELMATINDEX(i, j, stride) (int)(i)+(int)(j)*(int)(stride) 32 | #endif 33 | 34 | #ifndef PIXELMONOATF 35 | #define PIXELMONOATF(image, i, j, stride) *((image) + ((int)j)*((int)stride)*sizeof(VFLOAT) + ((int)i)) 36 | #endif 37 | 38 | #ifndef GETPIXEL 39 | #define GETPIXEL(image, x, y) PIXELMAT(image->pixels, x, y, image->width) 40 | #endif 41 | 42 | #ifndef GETZBUFFER 43 | #define GETZBUFFER(image, x, y) PIXELMAT(image->zbuffer, x, y, image->width) 44 | #endif 45 | 46 | //math 47 | #ifndef MIN 48 | #define MIN(a,b) ((a < b) ? a : b) 49 | #endif 50 | 51 | #ifndef MAX 52 | #define MAX(a,b) ((a < b) ? b : a) 53 | #endif 54 | 55 | #ifndef ABS 56 | #define ABS(a) ((a < 0) ? -a : a) 57 | #endif 58 | 59 | #ifndef CLAMP 60 | #define CLAMP(x,a,b) MAX((a), MIN((b), (x))) 61 | #endif 62 | 63 | #ifndef PI 64 | #define PI 3.1415926535897932384626433832795 65 | #endif 66 | 67 | #ifndef DEGTORAD 68 | #define DEGTORAD 0.017453292519943295769236907684886 69 | #endif 70 | 71 | #ifndef INF 72 | #define INF 0xFFFF 73 | #endif 74 | 75 | #ifndef INF_FLOAT 76 | #define INF_FLOAT 0xFFFFFFFF 77 | #endif 78 | 79 | #ifndef INF_INT 80 | #define INF_INT 0xFFFF 81 | #endif 82 | 83 | //conversions 84 | #ifndef RGBTOINT 85 | #define RGBTOINT(r,g,b,out) out = (int(b)<<16) | (int(g)<<8) | int(r); 86 | #endif 87 | 88 | #ifndef INTTORGB 89 | #define INTTORGB(r,g,b,in) r = in&0xff; g = (in>>8)&0xff; b = (in>>16)&0xff; 90 | #endif 91 | 92 | #ifndef PIXELTOINT 93 | #define PIXELTOINT(pixel,out) RGBTOINT(pixel.r, pixel.g, pixel.b, out); 94 | #endif 95 | 96 | #ifndef INTTOPIXEL 97 | #define INTTOPIXEL(pixel,in) INTTORGB(pixel.r, pixel.g, pixel.b, in); 98 | #endif 99 | 100 | #ifndef MAX_INT 101 | #define MAX_INT 4294967295 102 | #endif 103 | 104 | //debug 105 | #ifndef DEBUGBREAK 106 | #define DEBUGBREAK \ 107 | { \ 108 | int ryifrklaeybfcklarybckyar=0; \ 109 | ryifrklaeybfcklarybckyar++; \ 110 | } 111 | #endif 112 | 113 | //types 114 | 115 | #ifndef VFLOAT 116 | #define VFLOAT float 117 | #endif 118 | 119 | #ifndef VFLOAT2_def 120 | #define VFLOAT2_def 121 | struct VFLOAT2 122 | { 123 | VFLOAT x; 124 | VFLOAT y; 125 | }; 126 | #endif 127 | 128 | #ifndef VINT 129 | #define VINT int 130 | #endif 131 | 132 | #ifndef VUINT 133 | #define VUINT unsigned int 134 | #endif 135 | 136 | #ifndef VBYTE 137 | #define VBYTE unsigned char 138 | #endif 139 | 140 | #ifndef VBOOL 141 | #define VBOOL bool 142 | #endif 143 | 144 | #ifndef NULL 145 | #define NULL 0 146 | #endif 147 | 148 | #ifndef TRUE 149 | #define TRUE 1 150 | #endif 151 | 152 | #ifndef FALSE 153 | #define FALSE 0 154 | #endif 155 | 156 | #ifndef VECTOR3DA 157 | #define VECTOR3DA Vector3D 158 | #endif 159 | 160 | #ifndef VECTOR3DI 161 | #define VECTOR3DI Vector3D 162 | #endif 163 | 164 | #ifndef VECTOR2DA 165 | #define VECTOR2DA Vector2D 166 | #endif 167 | 168 | #ifndef VECTOR4DA 169 | #define VECTOR4DA Vector4D 170 | #endif 171 | 172 | #ifndef RENDERER_ZBUFFER_CONDITION 173 | #define RENDERER_ZBUFFER_CONDITION Sz < imageFill->zbuffer[index] 174 | #endif 175 | 176 | #ifndef RENDERER_ZBUFFERINVERSE_CONDITION 177 | #define RENDERER_ZBUFFERINVERSE_CONDITION Sz > imageFill->zbufferInverse[index] 178 | #endif 179 | 180 | #ifndef DRAWLINE 181 | #define DRAWLINE(image,x1,y1,x2,y2,color) DrawingPrimitives::Instance()->DrawLine(image, (VINT) x1, (VINT) y1, (VINT) x2, (VINT) y2, color) 182 | #endif 183 | 184 | #ifndef DRAWLINEZ 185 | #define DRAWLINEZ(image,x1,y1,z1,x2,y2,z2,meshid,color) DrawingPrimitives::Instance()->DrawLineZ(image, x1, y1, z1, x2, y2, z2, meshid, color) 186 | #endif 187 | 188 | #ifndef ITERATION_TARGET 189 | #define ITERATION_TARGET 190 | enum IterationTarget { ITERATIONTARGET_TRANSLATION, ITERATIONTARGET_ROTATION, ITERATIONTARGET_BOTH }; 191 | #endif 192 | 193 | #ifndef ImageFloat 194 | #define ImageFloat ImagePerseus 195 | #endif 196 | 197 | #ifndef ImageUInt 198 | #define ImageUInt ImagePerseus 199 | #endif 200 | 201 | #ifndef ImageInt 202 | #define ImageInt ImagePerseus 203 | #endif 204 | 205 | #ifndef ImageUChar 206 | #define ImageUChar ImagePerseus 207 | #endif 208 | 209 | #ifndef ImageUChar4 210 | #define ImageUChar4 ImagePerseus 211 | #endif 212 | 213 | #ifndef ImageBool 214 | #define ImageBool ImagePerseus 215 | #endif -------------------------------------------------------------------------------- /PerseusLib/PWP3DConfig.cmake.in: -------------------------------------------------------------------------------- 1 | # Compute paths 2 | get_filename_component( PROJECT_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH ) 3 | SET( @PROJECT_NAME@_INCLUDE_DIRS "@EXPORT_LIB_INC_DIR@;@USER_INC@" ) 4 | 5 | # Library dependencies (contains definitions for IMPORTED targets) 6 | if( NOT TARGET @LIBRARY_NAME@ AND NOT @PROJECT_NAME@_BINARY_DIR ) 7 | include( "${PROJECT_CMAKE_DIR}/@PROJECT_NAME@Targets.cmake" ) 8 | endif() 9 | 10 | SET( @PROJECT_NAME@_LIBRARIES "@LIBRARY_NAME@" ) 11 | -------------------------------------------------------------------------------- /PerseusLib/PWP3DConfigVersion.cmake.in: -------------------------------------------------------------------------------- 1 | set(PACKAGE_VERSION "@VERSION@") 2 | 3 | # Check build type is valid 4 | # Check whether the requested PACKAGE_FIND_VERSION is compatible 5 | if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") 6 | set(PACKAGE_VERSION_COMPATIBLE FALSE) 7 | else() 8 | set(PACKAGE_VERSION_COMPATIBLE TRUE) 9 | if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") 10 | set(PACKAGE_VERSION_EXACT TRUE) 11 | endif() 12 | endif() 13 | -------------------------------------------------------------------------------- /PerseusLib/PerseusLib.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Objects/Object3D.h" 4 | #include "Objects/View3D.h" 5 | #include "Optimiser/Engine/OptimisationEngine.h" 6 | #include "Utils/HistogramEngine.h" 7 | #include "Utils/ImageUtils.h" 8 | #include "Utils/VisualisationEngine.h" 9 | 10 | using namespace PerseusLib::Objects; 11 | using namespace PerseusLib::Optimiser; 12 | using namespace PerseusLib::Utils; -------------------------------------------------------------------------------- /PerseusLib/Primitives/ImagePerseus.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | namespace PerseusLib 12 | { 13 | namespace Primitives 14 | { 15 | template 16 | class ImagePerseus 17 | { 18 | private: 19 | size_t totalSize, totalSizeGPU; 20 | 21 | public: 22 | bool useCudaAlloc; 23 | 24 | int isAllocated; 25 | int width, height; 26 | 27 | T* pixels; 28 | T* pixelsGPU; 29 | 30 | ImagePerseus(int width, int height, bool useCudaAlloc = false, int allocWidthGPU = 0, int allocHeightGPU = 0) { 31 | this->width = width; 32 | this->height = height; 33 | this->useCudaAlloc = useCudaAlloc; 34 | 35 | totalSize = width * height * sizeof(T); 36 | if (allocWidthGPU != 0 && allocHeightGPU != 0) totalSizeGPU = allocWidthGPU * allocHeightGPU * sizeof(T); 37 | else totalSizeGPU = width * height * sizeof(T); 38 | 39 | if (useCudaAlloc) 40 | { 41 | perseusSafeCall(cudaMallocHost((void**)&pixels, totalSize)); 42 | perseusSafeCall(cudaMalloc((void**)&pixelsGPU, totalSizeGPU)); 43 | // printf("use device alloc\n"); 44 | } 45 | else 46 | { pixels = new T[width * height]; } 47 | 48 | this->Clear(); 49 | 50 | isAllocated = true; 51 | } 52 | 53 | T &operator[](unsigned subscript) { return pixels[subscript]; } 54 | T operator[](unsigned subscript) const { return pixels[subscript]; } 55 | 56 | void Clear(T defaultValue = T(0)) 57 | { 58 | memset(pixels, (int)defaultValue, totalSize); 59 | if (useCudaAlloc) perseusSafeCall(cudaMemset(pixelsGPU, (int)defaultValue, totalSizeGPU)); 60 | } 61 | 62 | void UpdateGPUFromCPU() { if (useCudaAlloc) perseusSafeCall(cudaMemcpy(pixelsGPU, pixels, totalSizeGPU, cudaMemcpyHostToDevice)); } 63 | void UpdateCPUFromGPU() { if (useCudaAlloc) perseusSafeCall(cudaMemcpy(pixels, pixelsGPU, totalSizeGPU, cudaMemcpyDeviceToHost)); } 64 | 65 | void Free() 66 | { 67 | if (useCudaAlloc) { 68 | perseusSafeCall(cudaFree(pixelsGPU)); 69 | perseusSafeCall(cudaFreeHost(pixels)); 70 | } 71 | else delete pixels; 72 | 73 | this->isAllocated = false; 74 | } 75 | 76 | ~ImagePerseus() { if (isAllocated) this->Free(); } 77 | }; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /PerseusLib/Primitives/PixelUCHAR4.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace PerseusLib 7 | { 8 | namespace Primitives 9 | { 10 | class PixelUCHAR4 11 | { 12 | public: 13 | unsigned char x, y, z, w; 14 | 15 | void SetFrom(unsigned char x, unsigned char y, unsigned char z, unsigned char w) 16 | { this->x = x; this->y = y; this->z = z; this->w = w; } 17 | 18 | void SetFrom(unsigned char x, unsigned char y, unsigned char z) 19 | { this->SetFrom(x, y, z, 255); } 20 | 21 | void SetFrom(unsigned char *xyzw, int size) 22 | { if (size == 4) this->SetFrom(xyzw[0], xyzw[1], xyzw[2], xyzw[3]); else this->SetFrom(xyzw[0], xyzw[1], xyzw[2]); } 23 | 24 | //this is an ugly hack used to get the memset working :( 25 | operator int() {return x;} 26 | 27 | PixelUCHAR4(unsigned char value) { this->SetFrom(value,value,value,value); } 28 | PixelUCHAR4(unsigned char x, unsigned char y, unsigned char z, unsigned char w) { this->SetFrom(x,y,z,w); } 29 | PixelUCHAR4() { this->SetFrom(0,0,0,0); } 30 | }; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PerseusLib/Primitives/Vector2D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace PerseusLib 4 | { 5 | namespace Primitives 6 | { 7 | template 8 | class Vector2D 9 | { 10 | public: 11 | T x, y; 12 | 13 | const Vector2D& operator+= (Vector2D &p){ 14 | x += p.x; y += p.y; 15 | return *this; 16 | } 17 | 18 | const Vector2D& operator-= (Vector2D &p){ 19 | x -= p.x; y -= p.y; 20 | return *this; 21 | } 22 | 23 | Vector2D operator* (T f) const{ 24 | Vector2D r(x * f, y * f); 25 | return r; 26 | } 27 | 28 | Vector2D& operator*= (T f){ 29 | x *= f; y *= f; 30 | return *this; 31 | } 32 | 33 | friend Vector2D operator+ (const Vector2D &lp, const Vector2D &rp){ 34 | Vector2D r = Vector2D(lp.x + rp.x, lp.y + rp.y); 35 | return r; 36 | } 37 | 38 | friend Vector2D operator- (const Vector2D &lp, const Vector2D &rp){ 39 | Vector2D r = Vector2D(lp.x - rp.x, lp.y - rp.y); 40 | return r; 41 | } 42 | 43 | friend Vector2D operator& (const Vector2D &a, const Vector2D &b){ 44 | //cross product 45 | Vector2D r = Vector2D(a.y * b.x, a.x * b.y); 46 | return r; 47 | } 48 | 49 | friend T operator| (const Vector2D &a, const Vector2D &b){ 50 | //dot product 51 | return (T) (a.x*b.x + a.y*b.y); 52 | } 53 | 54 | friend bool operator== (const Vector2D &lp, const Vector2D &rp){ 55 | return ((lp.x == rp.x) && (lp.y == rp.y)); 56 | } 57 | 58 | //double* ToDoubleArray() { double* a = new double[2]; a[0] = x; a[1] = y; return a; } 59 | //void FromDoubleArray(double *a) { x = a[0]; y = a[1]; } 60 | 61 | Vector2D(long double x, long double y) { this->x = x; this->y = y; } 62 | Vector2D(double x, double y) { this->x = x; this->y = y; } 63 | Vector2D(float x, float y) { this->x = x; this->y = y; } 64 | Vector2D(int x, int y) { this->x = x; this->y = y; } 65 | 66 | Vector2D(double* d) { this->x = d[0]; this->y = d[0]; } 67 | Vector2D(float *f) { this->x = f[0]; this->y = f[0]; } 68 | Vector2D(int *i) { this->x = i[0]; this->y = i[0]; } 69 | 70 | Vector2D(Vector2D *v) { this->x = v->x; this->y = v->y; } 71 | Vector2D(void) {x = 0; y = 0; } 72 | 73 | ~Vector2D(void) {} 74 | }; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /PerseusLib/Primitives/Vector3D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace PerseusLib 4 | { 5 | namespace Primitives 6 | { 7 | template 8 | class Vector3D 9 | { 10 | public: 11 | T x, y, z; 12 | 13 | const Vector3D& operator+= (Vector3D &p){ 14 | x += p.x; y += p.y; z += p.z; 15 | return *this; 16 | } 17 | 18 | const Vector3D& operator-= (Vector3D &p){ 19 | x -= p.x; y -= p.y; z -= p.z; 20 | return *this; 21 | } 22 | 23 | Vector3D operator* (T f) const{ 24 | Vector3D r(x * f, y * f, z * f); 25 | return r; 26 | } 27 | 28 | Vector3D& operator*= (T f){ 29 | x *= f; y *= f; z *= f; 30 | return *this; 31 | } 32 | 33 | friend Vector3D operator+ (const Vector3D &lp, const Vector3D &rp){ 34 | Vector3D r = Vector3D(lp.x + rp.x, lp.y + rp.y, lp.z + rp.z); 35 | return r; 36 | } 37 | 38 | friend bool operator== (const Vector3D &lp, const Vector3D &rp){ 39 | return ((lp.x == rp.x) && (lp.y == rp.y) && (lp.z == rp.z)); 40 | } 41 | 42 | friend Vector3D operator- (const Vector3D &lp, const Vector3D &rp){ 43 | Vector3D r = Vector3D(lp.x - rp.x, lp.y - rp.y, lp.z - rp.z); 44 | return r; 45 | } 46 | 47 | friend Vector3D operator& (const Vector3D &a, const Vector3D &b){ 48 | //cross product 49 | Vector3D r = Vector3D(a.y*b.z - a.z*b.y, a.z*b.z - a.x*b.z, a.x*b.y - a.y*b.z); 50 | return r; 51 | } 52 | 53 | friend T operator| (const Vector3D &a, const Vector3D &b){ 54 | //dot product 55 | return (T) (a.x*b.x + a.y*b.y + a.z*b.z); 56 | } 57 | 58 | //double* ToDoubleArray() { double* a = new double[3]; a[0] = x; a[1] = y; a[2] = z; return a; } 59 | //void FromDoubleArray(double *a) { x = a[0]; y = a[1]; z = a[2]; } 60 | 61 | T norm() { return sqrt(x*x + y*y + z*z); } 62 | 63 | Vector3D(float *f){ x = (T) f[0]; y = (T) f[1]; z = (T) f[2]; } 64 | Vector3D(double *d) { x = (T) d[0]; y = (T) d[1]; z = (T) d[2]; } 65 | Vector3D(long double *d) { x = (T) d[0]; y = (T) d[1]; z = (T) d[2]; } 66 | Vector3D(int *i) { x = (T) i[0]; y = (T) i[1]; z = (T) i[2]; } 67 | 68 | Vector3D(float f0, float f1, float f2) { x = (T)f0; y = (T)f1; z = (T)f2; } 69 | Vector3D(double f0, double f1, double f2){ x = (T)f0; y = (T)f1; z = (T)f2; } 70 | Vector3D(long double f0, long double f1, long double f2){ x = (T)f0; y = (T)f1; z = (T)f2; } 71 | Vector3D(int i0, int i1, int i2) { x = (T)i0; y = (T)i1; z = (T)i2; } 72 | 73 | Vector3D(void) { x = 0; y = 0; z = 0; } 74 | 75 | void CopyInto(Vector3D dest) { 76 | dest.x = this->x; dest.y = this->y; dest.z = this->z; 77 | } 78 | void CopyInto(Vector3D *dest) { 79 | dest->x = this->x; dest->y = this->y; dest->z = this->z; 80 | } 81 | ~Vector3D(void) {} 82 | }; 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /PerseusLib/Primitives/Vector4D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace PerseusLib 4 | { 5 | namespace Primitives 6 | { 7 | template 8 | class Vector4D 9 | { 10 | public: 11 | T x, y, z, w; 12 | 13 | const Vector4D& operator+= (Vector4D &p){ 14 | x += p.x; y += p.y; z += p.z; z += p.w; 15 | return *this; 16 | } 17 | 18 | const Vector4D& operator-= (Vector4D &p){ 19 | x -= p.x; y -= p.y; z -= p.z; z += p.w; 20 | return *this; 21 | } 22 | 23 | Vector4D operator* (T f) const{ 24 | Vector4D r(x * f, y * f, z * f, w * f); 25 | return r; 26 | } 27 | 28 | Vector4D& operator*= (T f){ 29 | x *= f; y *= f; z *= f; z *= w; 30 | return *this; 31 | } 32 | 33 | friend Vector4D operator+ (const Vector4D &lp, const Vector4D &rp){ 34 | Vector4D r = Vector4D(lp.x + rp.x, lp.y + rp.y, lp.z + rp.z, lp.w + rp.w); 35 | return r; 36 | } 37 | 38 | friend Vector4D operator- (const Vector4D &lp, const Vector4D &rp){ 39 | Vector4D r = Vector4D(lp.x - rp.x, lp.y - rp.y, lp.z - rp.z, lp.w + rp.w); 40 | return r; 41 | } 42 | 43 | //friend Vector4D operator& (const Vector4D &a, const Vector4D &b){ 44 | // //cross product 45 | // Vector4D r = Vector4D( 46 | // a.y*(b.z*cw - cz*b.w) - a.z*(b.y*cw - cy*b.w) + a.w*(b.y*cz - cy*b.z), 47 | // -a.x*(b.z*cw - cz*b.w) + a.z*(b.x*cw - cx*b.w) - a.w*(b.x*cz - cx*b.z), 48 | // a.x*(b.y*cw - cy*b.w) - a.y*(b.x*cw - cx*b.w) + a.w*(b.x*cy - cx*b.y), 49 | // -a.x*(b.y*cz - cy*b.z) + a.y*(b.x*cz - cx*b.z) - a.z*(b.x*cy - cx*b.y) 50 | // ); 51 | // return r; 52 | //} 53 | 54 | friend T operator| (const Vector4D &a, const Vector4D &b){ 55 | //dot product 56 | return (T) (a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w); 57 | } 58 | 59 | friend bool operator== (const Vector4D &lp, const Vector4D &rp){ 60 | return ((lp.x == rp.x) && (lp.y == rp.y) && (lp.z == rp.z) && (lp.w == rp.w)); 61 | } 62 | 63 | Vector4D(float *f){ x = (T) f[0]; y = (T) f[1]; z = (T) f[2]; w = (T) f[3]; } 64 | Vector4D(double *d) { x = (T) d[0]; y = (T) d[1]; z = (T) d[2]; w = (T) d[3]; } 65 | Vector4D(long double *d) { x = (T) d[0]; y = (T) d[1]; z = (T) d[2]; w = (T) d[3]; } 66 | Vector4D(int *i) { x = (T) i[0]; y = (T) i[1]; z = (T) i[2]; w = (T) i[3]; } 67 | 68 | Vector4D(float v0, float v1, float v2, float v3) { x = (T)v0; y = (T)v1; z = (T)v2; w = (T)v3; } 69 | Vector4D(double v0, double v1, double v2, double v3){ x = (T) v0; y = (T) v1; z = (T) v2; w = (T) v3; } 70 | Vector4D(long double v0, long double v1, long double v2, long double v3) 71 | { x = (T) v0; y = (T) v1; z = (T) v2; w = (T) v3; } 72 | Vector4D(int v0, int v1, int v2, int v3) { x = (T) v0; y = (T) v1; z = (T) v2; w = (T) v3; } 73 | 74 | Vector4D(void) { x = 0; y = 0; z = 0; w = 0; } 75 | 76 | ~Vector4D(void) {} 77 | }; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Engine/DrawingEngine.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | #include 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | #include 20 | 21 | using namespace PerseusLib::Primitives; 22 | using namespace PerseusLib::Objects; 23 | using namespace PerseusLib::Utils; 24 | 25 | using namespace Renderer::Model3D; 26 | using namespace Renderer::Primitives; 27 | using namespace Renderer::Objects; 28 | using namespace Renderer::Transforms; 29 | 30 | #include 31 | 32 | namespace Renderer 33 | { 34 | namespace Engine 35 | { 36 | class DrawingEngine 37 | { 38 | private: 39 | VECTOR3DA f; 40 | VFLOAT projectionMatrix[16], modelViewMatrix[16], pmMatrix[16], buffer[4]; 41 | 42 | void applyCoordinateTransform(Renderer3DView* view, Renderer3DObject* object, float *pmMatrix); 43 | 44 | void drawFaceEdges(ImageUChar *imageRender, ModelFace* currentFace, ModelH* drawingModel, VBYTE color, int* roiGenerated); 45 | void drawFaceFilled(ImageUChar *imageRender, ModelFace* currentFace, ModelH* drawingModel, int objectId, VBYTE color, VINT meshId); 46 | void drawFaceFilled(ImageRender *imageRender, ModelFace* currentFace, ModelH* drawingModel, int objectId, VBYTE color, int meshId); 47 | 48 | void drawWireframe(ImageUChar* imageWireframe, ModelH* drawingModel, int* roiGenerated); 49 | void drawFilled(ImageRender* imageFill, ModelH* drawingModel, int objectId); 50 | void drawFilled(ImageUChar* imageFill, ModelH* drawingModel, int objectId); 51 | 52 | static DrawingEngine* instance; 53 | public: 54 | enum RenderingType {RENDERING_FILL, RENDERING_WIREFRAME}; 55 | 56 | static DrawingEngine* Instance(void) { 57 | if (instance == NULL) instance = new DrawingEngine(); 58 | return instance; 59 | } 60 | 61 | void DrawAllInView(Object3D** objects, int objectCount, View3D* view, bool useCUDA, bool getBackData); 62 | 63 | void Draw(Object3D* object, View3D *view, bool useCUDA, bool getBackData); 64 | void Draw(Object3D* object, View3D* view, Pose3D *pose, ImageUChar *pImageRender, RenderingType renderingType, bool clearImage = true); 65 | void Draw(Object3D* object, View3D* view, Pose3D *pose, ImageRender *imageRender, bool clearImage = true); 66 | 67 | void ChangeROIWithBand(Object3D* object, View3D *view, int bandSize, int width, int height); 68 | void ChangeROIWithBand(View3D* view, int bandSize, int width, int height); 69 | 70 | void GetPMMatrices(Object3D *object, View3D *view, Pose3D* pose, float *projectionMatrix, float *modelViewMatrix, float *pmMatrix); 71 | void ComputeAndSetPMMatrices(Object3D* object, View3D* view, Pose3D* pose = NULL); 72 | 73 | DrawingEngine(void); 74 | ~DrawingEngine(void); 75 | }; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Engine/DrawingPrimitives.cpp: -------------------------------------------------------------------------------- 1 | // Extremely Fast Line Algorithm Var E (Addition Fixed Point PreCalc) 2 | // Copyright 2001-2, By Po-Han Lin 3 | 4 | 5 | // Freely useable in non-commercial applications as long as credits 6 | // to Po-Han Lin and link to http://www.edepot.com is provided in 7 | // source code and can been seen in compiled executable. 8 | // Commercial applications please inquire about licensing the algorithms. 9 | // 10 | // Lastest version at http://www.edepot.com/phl.html 11 | // This version is for standard displays (up to 65536x65536) 12 | // For small display version (256x256) visit http://www.edepot.com/lineex.html 13 | 14 | #include "DrawingPrimitives.h" 15 | #include 16 | 17 | using namespace Renderer::Engine; 18 | 19 | DrawingPrimitives* DrawingPrimitives::instance; 20 | 21 | DrawingPrimitives::DrawingPrimitives(void) 22 | { 23 | } 24 | 25 | DrawingPrimitives::~DrawingPrimitives(void) 26 | { 27 | } 28 | 29 | void DrawingPrimitives::DrawLine(ImageUChar *image, int x, int y, int x2, int y2, VBYTE color) 30 | { 31 | int sx, sy; 32 | VBOOL yLonger=false; 33 | int shortLen=y2-y; 34 | int longLen=x2-x; 35 | if (abs(shortLen)>abs(longLen)) 36 | { 37 | int swap=shortLen; 38 | shortLen=longLen; 39 | longLen=swap; 40 | yLonger=true; 41 | } 42 | int decInc; 43 | if (longLen==0) decInc=0; 44 | else decInc = (shortLen << 16) / longLen; 45 | 46 | if (yLonger) 47 | { 48 | if (longLen>0) 49 | { 50 | longLen+=y; 51 | for (int j=0x8000+(x<<16);y<=longLen;++y) 52 | { 53 | sx = CLAMP(j >> 16, 0, image->width-1); 54 | sy = CLAMP(y, 0, image->height-1); 55 | 56 | GETPIXEL(image,sx,sy) = color; 57 | j+=decInc; 58 | } 59 | return; 60 | } 61 | longLen+=y; 62 | for (int j=0x8000+(x<<16);y>=longLen;--y) 63 | { 64 | sx = CLAMP(j >> 16, 0, image->width-1); 65 | sy = CLAMP(y, 0, image->height-1); 66 | 67 | GETPIXEL(image,sx,sy) = color; 68 | j-=decInc; 69 | } 70 | return; 71 | } 72 | 73 | if (longLen>0) 74 | { 75 | longLen+=x; 76 | for (int j=0x8000+(y<<16);x<=longLen;++x) 77 | { 78 | sx = CLAMP(x, 0, image->width-1); 79 | sy = CLAMP(j>>16, 0, image->height-1); 80 | 81 | GETPIXEL(image,sx,sy) = color; 82 | j+=decInc; 83 | } 84 | return; 85 | } 86 | longLen+=x; 87 | for (int j=0x8000+(y<<16);x>=longLen;--x) 88 | { 89 | sx = CLAMP(x, 0, image->width-1); 90 | sy = CLAMP(j>>16, 0, image->height-1); 91 | 92 | GETPIXEL(image,sx,sy) = color; 93 | j-=decInc; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Engine/DrawingPrimitives.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | 9 | using namespace PerseusLib::Primitives; 10 | using namespace PerseusLib::Utils; 11 | 12 | namespace Renderer 13 | { 14 | namespace Engine 15 | { 16 | class DrawingPrimitives 17 | { 18 | static DrawingPrimitives* instance; 19 | public: 20 | static DrawingPrimitives* Instance(void) { 21 | if (instance == NULL) instance = new DrawingPrimitives(); 22 | return instance; 23 | } 24 | 25 | int sgn(int num) { if (num > 0) return(1); else if (num < 0) return(-1); else return(0); } 26 | 27 | void DrawLine(ImageUChar *image, int x1, int y1, int x2, int y2, VBYTE color); 28 | 29 | DrawingPrimitives(void); 30 | ~DrawingPrimitives(void); 31 | }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/Model.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace Renderer::Model3D; 13 | 14 | namespace Renderer 15 | { 16 | namespace Model3D 17 | { 18 | 19 | class Model 20 | { 21 | private: 22 | int createFromFile(FILE *file); 23 | int createFromMesh(aiMesh* pMesh); 24 | 25 | public: 26 | std::vector groups; 27 | std::vector vertices; 28 | 29 | VFLOAT* verticesVector; 30 | 31 | VFLOAT minZ; 32 | 33 | int faceCount; 34 | 35 | Model(void); 36 | Model(char* fileName); 37 | Model(std::string fileName); 38 | Model(aiMesh* pMesh); 39 | Model(FILE* f); 40 | 41 | Model* Clone(); 42 | 43 | void ToModelH(ModelH* newModel); 44 | void ToModelHInit(ModelH* newModel); 45 | 46 | ~Model(void); 47 | }; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelFace.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace Renderer 9 | { 10 | namespace Model3D 11 | { 12 | class ModelFace 13 | { 14 | public: 15 | std::vector vertices; 16 | int *verticesVector; 17 | size_t verticesVectorCount; 18 | size_t edgeListStartIndex; 19 | size_t edgeListStopIndex; 20 | int edgeCount; 21 | 22 | VBOOL isInvisible; 23 | VBOOL isVisible; 24 | 25 | ModelFace(void) {} 26 | ~ModelFace(void) { delete verticesVector; } 27 | }; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelGroup.cpp: -------------------------------------------------------------------------------- 1 | #include "ModelGroup.h" 2 | #include 3 | 4 | using namespace Renderer::Model3D; 5 | 6 | ModelGroup::ModelGroup(void) 7 | { 8 | } 9 | 10 | ModelGroup::~ModelGroup(void) 11 | { 12 | size_t i; 13 | for (i=0;igroupName = strdup(groupName); 21 | } 22 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelGroup.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | //#include 8 | 9 | namespace Renderer 10 | { 11 | namespace Model3D 12 | { 13 | class ModelGroup 14 | { 15 | public: 16 | 17 | std::vector faces; 18 | char* groupName; 19 | 20 | ModelGroup(char* groupName); 21 | ModelGroup(void); 22 | ~ModelGroup(void); 23 | }; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelH.cpp: -------------------------------------------------------------------------------- 1 | #include "ModelH.h" 2 | 3 | #include 4 | 5 | using namespace Renderer::Model3D; 6 | 7 | ModelH::ModelH(void) 8 | { 9 | isAllocated = false; 10 | } 11 | 12 | ModelH::~ModelH(void) 13 | { 14 | if (isAllocated) 15 | { 16 | delete verticesVector; 17 | delete verticesVectorPreP; 18 | delete verticesGPUBuff; 19 | 20 | perseusSafeCall(cudaFree(verticesGPU)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelH.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | namespace Renderer 11 | { 12 | namespace Model3D 13 | { 14 | class ModelH 15 | { 16 | public: 17 | bool isAllocated; 18 | std::vector* groups; 19 | 20 | VFLOAT *verticesVector; 21 | VFLOAT *verticesVectorPreP; 22 | VFLOAT *originalVerticesVector; 23 | size_t verticesVectorSize; 24 | 25 | float* verticesGPU, *verticesGPUBuff; 26 | 27 | VFLOAT minZ; 28 | 29 | int faceCount; 30 | 31 | ModelH(void); 32 | ~ModelH(void); 33 | }; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelVertex.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace PerseusLib::Primitives; 8 | 9 | namespace Renderer 10 | { 11 | namespace Model3D 12 | { 13 | class ModelVertex 14 | { 15 | public: 16 | VECTOR3DA vector3d; 17 | 18 | ModelVertex* Clone() { return new ModelVertex(vector3d); } 19 | 20 | ModelVertexH* ToModelVertexH() { return new ModelVertexH(vector3d.x, vector3d.y, vector3d.z, (VFLOAT) 1); } 21 | 22 | ModelVertex(VECTOR3DA v) { vector3d = VECTOR3DA(v.x, v.y, v.z); } 23 | ModelVertex(ModelVertexH* m) { vector3d.x = m->vector4d.x/m->vector4d.w; vector3d.y = m->vector4d.y/m->vector4d.w;; vector3d.z = m->vector4d.z/m->vector4d.w; } 24 | 25 | ModelVertex(float *v) { vector3d = VECTOR3DA(v); } 26 | ModelVertex(double *v) { vector3d = VECTOR3DA(v); } 27 | ModelVertex(long double *v) { vector3d = VECTOR3DA(v); } 28 | ModelVertex(int *v) { vector3d = VECTOR3DA(v); } 29 | 30 | ModelVertex(float v0, float v1, float v2) { vector3d = VECTOR3DA(v0, v1, v2); } 31 | ModelVertex(double v0, double v1, double v2) { vector3d = VECTOR3DA(v0, v1, v2); } 32 | ModelVertex(long double v0, long double v1, long double v2) { vector3d = VECTOR3DA(v0, v1, v2); } 33 | ModelVertex(int v0, int v1, int v2) { vector3d = VECTOR3DA(v0, v1, v2); } 34 | ModelVertex(void) { vector3d = VECTOR3DA(); } 35 | 36 | void FromModelVertexH(ModelVertexH* m) { vector3d.x = m->vector4d.x/m->vector4d.w; vector3d.y = m->vector4d.y/m->vector4d.w;; vector3d.z = m->vector4d.z/m->vector4d.w; } 37 | 38 | ~ModelVertex(void){ } 39 | }; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Model/ModelVertexH.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | using namespace PerseusLib::Primitives; 8 | 9 | namespace Renderer 10 | { 11 | namespace Model3D 12 | { 13 | class ModelVertexH 14 | { 15 | public: 16 | VECTOR4DA vector4d; 17 | 18 | ModelVertexH* Clone() { return new ModelVertexH(vector4d); } 19 | 20 | ModelVertexH(VECTOR4DA v) { vector4d = VECTOR4DA(v.x, v.y, v.z, v.w); } 21 | 22 | ModelVertexH(float *v) { vector4d = VECTOR4DA(v); } 23 | ModelVertexH(double *v) { vector4d = VECTOR4DA(v); } 24 | ModelVertexH(int *v) { vector4d = VECTOR4DA(v); } 25 | ModelVertexH(long double *v) { vector4d = VECTOR4DA(v); } 26 | 27 | ModelVertexH(float v0, float v1, float v2, float v3) { vector4d = VECTOR4DA(v0, v1, v2, v3); } 28 | ModelVertexH(double v0, double v1, double v2, double v3) { vector4d = VECTOR4DA(v0, v1, v2, v3); } 29 | ModelVertexH(long double v0, long double v1, long double v2, long double v3) { vector4d = VECTOR4DA(v0, v1, v2, v3); } 30 | ModelVertexH(int v0, int v1, int v2, int v3) { vector4d = VECTOR4DA(v0, v1, v2, v3); } 31 | ModelVertexH(void) { vector4d = VECTOR4DA(); } 32 | 33 | ~ModelVertexH(void){ } 34 | }; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Objects/Renderer3DObject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace Renderer::Model3D; 10 | using namespace Renderer::Transforms; 11 | using namespace Renderer::Primitives; 12 | 13 | #include 14 | 15 | namespace Renderer 16 | { 17 | namespace Objects 18 | { 19 | class Renderer3DObject 20 | { 21 | public: 22 | //contains R,t for object for each view 23 | ObjectCoordinateTransform** objectCoordinateTransform; 24 | 25 | int objectId; 26 | int viewCount; 27 | 28 | Model* model; 29 | ModelH** drawingModel; 30 | 31 | Renderer3DObject(std::string fileName, int viewCount, int objectId) 32 | { 33 | this->viewCount = viewCount; 34 | this->objectId = objectId; 35 | 36 | objectCoordinateTransform = new ObjectCoordinateTransform*[viewCount]; 37 | 38 | for (int i=0; iToModelHInit(drawingModel[i]); 47 | } 48 | } 49 | 50 | Renderer3DObject(aiMesh* pMesh, int viewCount, int objectId) 51 | { 52 | this->viewCount = viewCount; 53 | this->objectId = objectId; 54 | 55 | objectCoordinateTransform = new ObjectCoordinateTransform*[viewCount]; 56 | 57 | for (int i=0; iToModelHInit(drawingModel[i]); 66 | } 67 | } 68 | 69 | void GetModelViewMatrix(float* modelViewMatrix, int viewId) 70 | { 71 | this->objectCoordinateTransform[viewId]->GetModelViewMatrix(modelViewMatrix); 72 | } 73 | 74 | ~Renderer3DObject(void) { 75 | delete model; 76 | 77 | for (int i=0; i 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace PerseusLib::Primitives; 9 | using namespace PerseusLib::Utils; 10 | 11 | using namespace Renderer::Transforms; 12 | using namespace Renderer::Primitives; 13 | 14 | namespace Renderer 15 | { 16 | namespace Objects 17 | { 18 | class Renderer3DView 19 | { 20 | public: 21 | Camera3D* camera3D; 22 | CameraCoordinateTransform* cameraCoordinateTransform; 23 | CameraCoordinateTransform::ProjectionParams projectionParams; 24 | 25 | int viewId; 26 | int view[4]; 27 | int roiGenerated[6]; 28 | 29 | VFLOAT invP[16]; 30 | 31 | void SetViewPort(int x, int y, int width, int height) { 32 | view[0] = x; 33 | view[1] = y; 34 | view[2] = width; 35 | view[3] = height; 36 | } 37 | 38 | Renderer3DView(int width, int height, Camera3D* camera, VFLOAT zNear, 39 | VFLOAT zFar, int viewId) 40 | { 41 | this->camera3D = camera; 42 | cameraCoordinateTransform = new CameraCoordinateTransform(); 43 | printf("[Renderer3DView] Set zNear %f, zFar %f; \n", zNear, zFar); 44 | 45 | cameraCoordinateTransform->SetProjectionMatrix(camera, zNear, zFar); 46 | 47 | this->viewId = viewId; 48 | 49 | this->SetViewPort(0, 0, width, height); 50 | } 51 | 52 | Renderer3DView(int width, int height, char* cameraCalibrationFile, 53 | VFLOAT zNear, VFLOAT zFar, int viewId) 54 | { 55 | camera3D = new Camera3D(cameraCalibrationFile); 56 | 57 | cameraCoordinateTransform = new CameraCoordinateTransform(); 58 | printf("[Renderer3DView] Set zNear %f, zFar %f; \n", zNear, zFar); 59 | 60 | cameraCoordinateTransform->SetProjectionMatrix(cameraCalibrationFile, zNear, zFar); 61 | cameraCoordinateTransform->GetProjectionParameters(&projectionParams); 62 | 63 | //setup invP matrix (inverse of projection matrix ... needed in energy function) 64 | cameraCoordinateTransform->GetInvPMatrix(invP); 65 | 66 | this->viewId = viewId; 67 | 68 | this->SetViewPort(0, 0, width, height); 69 | } 70 | 71 | Renderer3DView(int width, int height, float fSizeX, float fSizeY, 72 | float fFocalLengthX, float fFocalLengthY, 73 | float fCenterPointX, float fCenterPointY, 74 | VFLOAT zNear, VFLOAT zFar, int viewId) 75 | { 76 | camera3D = new Camera3D(fSizeX, fSizeY, fFocalLengthX, fFocalLengthY, 77 | fCenterPointX, fCenterPointY); 78 | 79 | cameraCoordinateTransform = new CameraCoordinateTransform(); 80 | printf("[Renderer3DView] Set zNear %f, zFar %f; \n", zNear, zFar); 81 | 82 | cameraCoordinateTransform->SetProjectionMatrix(camera3D, zNear, zFar); 83 | cameraCoordinateTransform->GetProjectionParameters(&projectionParams); 84 | 85 | //setup invP matrix (inverse of projection matrix ... needed in energy function) 86 | cameraCoordinateTransform->GetInvPMatrix(invP); 87 | 88 | this->viewId = viewId; 89 | 90 | this->SetViewPort(0, 0, width, height); 91 | } 92 | 93 | 94 | ~Renderer3DView(void) { 95 | delete cameraCoordinateTransform; 96 | } 97 | }; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Primitives/Camera3D.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace Renderer 11 | { 12 | namespace Primitives 13 | { 14 | class Camera3D 15 | { 16 | public: 17 | VFLOAT cameraParameters[5]; 18 | 19 | VFLOAT invFocal[2]; 20 | VFLOAT d2Tan, dOneOver2Tan, dWinv, dLargestRadius, dMaxR; 21 | VFLOAT dLastR, dLastFactor, dLastDistR; 22 | 23 | VFLOAT focalLength[2]; 24 | VFLOAT centerPoint[2]; 25 | VFLOAT distortionW; 26 | 27 | std::vector vLastIm; 28 | std::vector vLastCam; 29 | std::vector vLastDistCam; 30 | 31 | float invrtrans(float r) 32 | { 33 | if (distortionW == 0.0f) return r; 34 | return (tanf(r * distortionW) * dOneOver2Tan); 35 | } 36 | 37 | float rtrans_factor(float r) 38 | { 39 | if ( r <0.001f || distortionW == 0.0f ) return 1.0f; 40 | return (dWinv * atanf(r * d2Tan) / r); 41 | } 42 | 43 | std::vector makeVector(float val1, float val2) { 44 | std::vector val(2); 45 | val[0] = val1; 46 | val[1] = val2; 47 | return val; 48 | } 49 | 50 | std::vector Project(const std::vector &vCam) 51 | { 52 | float dLastR = sqrtf(vCam[0] * vCam[0] + vCam[1] * vCam[1]); 53 | float dLastFactor = rtrans_factor(dLastR); 54 | 55 | vLastIm[0] = centerPoint[0] + focalLength[0] * dLastFactor * vCam[0]; 56 | vLastIm[1] = centerPoint[1] + focalLength[1] * dLastFactor * vCam[1]; 57 | 58 | return vLastIm; 59 | } 60 | 61 | void Project(float* vInput, float *vOutput) 62 | { 63 | float dLastR = sqrtf(vInput[0] * vInput[0] + vInput[1] * vInput[1]); 64 | float dLastFactor = rtrans_factor(dLastR); 65 | 66 | vOutput[0] = centerPoint[0] + focalLength[0] * dLastFactor * vInput[0]; 67 | vOutput[1] = centerPoint[1] + focalLength[1] * dLastFactor * vInput[1]; 68 | } 69 | 70 | std::vector UnProject(const std::vector &v2Im) 71 | { 72 | vLastDistCam[0] = (v2Im[0] - centerPoint[0]) * invFocal[0]; 73 | vLastDistCam[1] = (v2Im[1] - centerPoint[1]) * invFocal[1]; 74 | dLastDistR = sqrtf(vLastDistCam[0] * vLastDistCam[0] + vLastDistCam[1] * vLastDistCam[1]); 75 | dLastR = invrtrans(dLastDistR); 76 | 77 | float dFactor; 78 | if (dLastDistR > 0.01f) dFactor = dLastR / dLastDistR; 79 | else dFactor = 1.0f; 80 | 81 | dLastFactor = 1.0f / dFactor; 82 | vLastCam[0] = dFactor * vLastDistCam[0]; 83 | vLastCam[1] = dFactor * vLastDistCam[1]; 84 | 85 | return vLastCam; 86 | } 87 | 88 | public: 89 | VFLOAT SizeX; 90 | VFLOAT SizeY; 91 | VFLOAT K[3][4], KGL[3][4]; 92 | VFLOAT vImplaneTL[2], vImplaneBR[2]; 93 | 94 | std::string CameraName; 95 | 96 | Camera3D(char *fileName) 97 | { 98 | vLastIm = std::vector(2); 99 | vLastCam = std::vector(2); 100 | vLastDistCam = std::vector(2); 101 | 102 | VBYTE cameraName[100]; 103 | int i, j; 104 | FILE* f = fopen(fileName, "r"); 105 | fscanf(f, "%s", cameraName); 106 | 107 | fscanf(f, "%f %f", &SizeX, &SizeY); 108 | fscanf(f, "%f %f", &focalLength[0], &focalLength[1]); 109 | fscanf(f, "%f %f", ¢erPoint[0], ¢erPoint[1]); 110 | fclose(f); 111 | 112 | for (i=0; i<3; i++) for (j=0; j<4; j++) K[i][j] = 0; 113 | for (i=0; i<3; i++) for (j=0; j<4; j++) KGL[i][j] = 0; 114 | 115 | K[0][0] = focalLength[0]; K[1][1] = focalLength[1]; 116 | K[0][2] = centerPoint[0]; K[1][2] = centerPoint[1]; 117 | K[2][2] = 1; 118 | 119 | KGL[0][0] = focalLength[0]; KGL[1][1] = focalLength[1]; 120 | KGL[0][2] = -centerPoint[0]; KGL[1][2] = -centerPoint[1]; 121 | KGL[2][2] = -1; 122 | 123 | for (i = 0; i < 4; i++) K[1][i] = (SizeY - 1) * (K[2][i]) - K[1][i]; 124 | for (i = 0; i < 4; i++) KGL[1][i] = (SizeY - 1) * (KGL[2][i]) - KGL[1][i]; 125 | } 126 | 127 | Camera3D(float fSizeX, float fSizeY, float fFocalLengthX, 128 | float fFocalLengthY, float fCenterPointX, float fCenterPointY) 129 | { 130 | vLastIm = std::vector(2); 131 | vLastCam = std::vector(2); 132 | vLastDistCam = std::vector(2); 133 | 134 | int i, j; 135 | 136 | SizeX = fSizeX; 137 | SizeY = fSizeY; 138 | focalLength[0] = fFocalLengthX; 139 | focalLength[1] = fFocalLengthY; 140 | centerPoint[0] = fCenterPointX; 141 | centerPoint[1] = fCenterPointY; 142 | 143 | for (i=0; i<3; i++) for (j=0; j<4; j++) K[i][j] = 0; 144 | for (i=0; i<3; i++) for (j=0; j<4; j++) KGL[i][j] = 0; 145 | 146 | K[0][0] = focalLength[0]; K[1][1] = focalLength[1]; 147 | K[0][2] = centerPoint[0]; K[1][2] = centerPoint[1]; 148 | K[2][2] = 1; 149 | 150 | KGL[0][0] = focalLength[0]; KGL[1][1] = focalLength[1]; 151 | KGL[0][2] = -centerPoint[0]; KGL[1][2] = -centerPoint[1]; 152 | KGL[2][2] = -1; 153 | 154 | for (i = 0; i < 4; i++) K[1][i] = (SizeY - 1) * (K[2][i]) - K[1][i]; 155 | for (i = 0; i < 4; i++) KGL[1][i] = (SizeY - 1) * (KGL[2][i]) - KGL[1][i]; 156 | } 157 | 158 | 159 | ~Camera3D(void) { } 160 | }; 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Primitives/Quaternion.cpp: -------------------------------------------------------------------------------- 1 | #include "Quaternion.h" 2 | #include 3 | 4 | using namespace Renderer::Primitives; 5 | 6 | void Quaternion::FromEuler(VFLOAT rotationX, VFLOAT rotationY, VFLOAT rotationZ) 7 | { 8 | rotationX = rotationX * (VFLOAT) DEGTORAD; 9 | rotationY = rotationY * (VFLOAT) DEGTORAD; 10 | rotationZ = rotationZ * (VFLOAT) DEGTORAD; 11 | 12 | VFLOAT c1 = cos(rotationY / 2.f); 13 | VFLOAT c2 = cos(rotationZ / 2.f); 14 | VFLOAT c3 = cos(rotationX / 2.f); 15 | 16 | VFLOAT s1 = sin(rotationY / 2.f); 17 | VFLOAT s2 = sin(rotationZ / 2.f); 18 | VFLOAT s3 = sin(rotationX / 2.f); 19 | 20 | VFLOAT c1c2 = c1*c2; 21 | VFLOAT s1s2 = s1*s2; 22 | this->vector4d.w = c1c2*c3 - s1s2*s3; 23 | this->vector4d.x = c1c2*s3 + s1s2*c3; 24 | this->vector4d.y = s1*c2*c3 + c1*s2*s3; 25 | this->vector4d.z = c1*s2*c3 - s1*c2*s3; 26 | 27 | VFLOAT norm = 1 / sqrtf(vector4d.x * vector4d.x + vector4d.y * vector4d.y + vector4d.z * vector4d.z + vector4d.w * vector4d.w); 28 | vector4d = VECTOR4DA(vector4d.x * norm, vector4d.y * norm, vector4d.z * norm, vector4d.w * norm); 29 | } 30 | 31 | void Quaternion::FromMatrix(VFLOAT* rotMatrix) 32 | { 33 | float matrix[3][3]; 34 | for (int i=0; i<3; i++) for (int j=0; j<3; j++) matrix[j][i] = rotMatrix[i + j * 3]; //was [j][i] 35 | 36 | float trace = matrix[0][0] + matrix[1][1] + matrix[2][2]; 37 | if( trace > 0 ) 38 | { 39 | float s = 0.5f / sqrtf(trace + 1.0f); 40 | vector4d.w = 0.25f / s; 41 | vector4d.x = ( matrix[2][1] - matrix[1][2] ) * s; 42 | vector4d.y = ( matrix[0][2] - matrix[2][0] ) * s; 43 | vector4d.z = ( matrix[1][0] - matrix[0][1] ) * s; 44 | } else { 45 | if ( matrix[0][0] > matrix[1][1] && matrix[0][0] > matrix[2][2] ) { 46 | float s = 2.0f * sqrtf( 1.0f + matrix[0][0] - matrix[1][1] - matrix[2][2]); 47 | vector4d.w = (matrix[2][1] - matrix[1][2] ) / s; 48 | vector4d.x = 0.25f * s; 49 | vector4d.y = (matrix[0][1] + matrix[1][0] ) / s; 50 | vector4d.z = (matrix[0][2] + matrix[2][0] ) / s; 51 | } else if (matrix[1][1] > matrix[2][2]) { 52 | float s = 2.0f * sqrtf( 1.0f + matrix[1][1] - matrix[0][0] - matrix[2][2]); 53 | vector4d.w = (matrix[0][2] - matrix[2][0] ) / s; 54 | vector4d.x = (matrix[0][1] + matrix[1][0] ) / s; 55 | vector4d.y = 0.25f * s; 56 | vector4d.z = (matrix[1][2] + matrix[2][1] ) / s; 57 | } else { 58 | float s = 2.0f * sqrtf( 1.0f + matrix[2][2] - matrix[0][0] - matrix[1][1] ); 59 | vector4d.w = (matrix[1][0] - matrix[0][1] ) / s; 60 | vector4d.x = (matrix[0][2] + matrix[2][0] ) / s; 61 | vector4d.y = (matrix[1][2] + matrix[2][1] ) / s; 62 | vector4d.z = 0.25f * s; 63 | } 64 | } 65 | } 66 | 67 | void Quaternion::ToOpenGLMatrix(VFLOAT* M) 68 | { 69 | VFLOAT m[4][4]; 70 | 71 | toOpenGLMatrix(m); 72 | 73 | m[0][3] = 0; 74 | m[1][3] = 0; 75 | m[2][3] = 0; 76 | 77 | m[3][0] = 0; 78 | m[3][1] = 0; 79 | m[3][2] = 0; 80 | 81 | m[3][3] = 1; 82 | 83 | matrixToOpenGLMatrix(m, M); 84 | 85 | DEBUGBREAK; 86 | } 87 | 88 | void Quaternion::toOpenGLMatrix(VFLOAT m[][4]) 89 | { 90 | VFLOAT sqw = vector4d.w * vector4d.w; 91 | VFLOAT sqx = vector4d.x * vector4d.x; 92 | VFLOAT sqy = vector4d.y * vector4d.y; 93 | VFLOAT sqz = vector4d.z * vector4d.z; 94 | 95 | VFLOAT invs = 1 / (sqx + sqy + sqz + sqw); 96 | m[0][0] = ( sqx - sqy - sqz + sqw)*invs ; 97 | m[1][1] = (-sqx + sqy - sqz + sqw)*invs ; 98 | m[2][2] = (-sqx - sqy + sqz + sqw)*invs ; 99 | 100 | VFLOAT tmp1 = vector4d.x*vector4d.y; 101 | VFLOAT tmp2 = vector4d.z*vector4d.w; 102 | m[1][0] = (VFLOAT) 2.0 * (tmp1 + tmp2)*invs ; 103 | m[0][1] = (VFLOAT) 2.0 * (tmp1 - tmp2)*invs ; 104 | 105 | tmp1 = vector4d.x*vector4d.z; 106 | tmp2 = vector4d.y*vector4d.w; 107 | m[2][0] = (VFLOAT) 2.0 * (tmp1 - tmp2)*invs ; 108 | m[0][2] = (VFLOAT) 2.0 * (tmp1 + tmp2)*invs ; 109 | tmp1 = vector4d.y*vector4d.z; 110 | tmp2 = vector4d.x*vector4d.w; 111 | m[2][1] = (VFLOAT) 2.0 * (tmp1 + tmp2)*invs ; 112 | m[1][2] = (VFLOAT) 2.0 * (tmp1 - tmp2)*invs ; 113 | } 114 | 115 | void Quaternion::matrixToOpenGLMatrix(VFLOAT m[][4], VFLOAT* M) 116 | { 117 | int i, j, k; 118 | k=0; 119 | for (i=0;i<4;i++) 120 | { 121 | for (j=0;j<4;j++) 122 | { 123 | M[k] = m[j][i]; 124 | k++; 125 | } 126 | } 127 | } 128 | 129 | void Quaternion::GetDerivatives(VFLOAT* derivatives, VFLOAT* xUnprojected, 130 | VFLOAT* xSource, VFLOAT* projectionParams, 131 | VFLOAT* otherInfo) 132 | { 133 | VFLOAT qx2, qy2, qz2, qw2; 134 | VFLOAT precalcX, precalcY, precalcXY; 135 | VFLOAT d0x, d0y, d0z; 136 | VFLOAT d1x, d1y, d1z; 137 | VFLOAT d2x, d2y, d2z; 138 | VFLOAT d3x, d3y, d3z; 139 | 140 | qx2 = 2 * vector4d.x; qy2 = 2 * vector4d.y; qz2 = 2 * vector4d.z; qw2 = 2 * vector4d.w; 141 | 142 | d0x = qy2*xSource[1] + qz2*xSource[2]; 143 | d0y = qy2*xSource[0] - 2*qx2*xSource[1] - qw2*xSource[2]; 144 | d0z = qz2*xSource[0] + qw2*xSource[1] - 2*qx2*xSource[2]; 145 | 146 | d1x = qx2*xSource[1] - 2*qy2*xSource[0] + qw2*xSource[2]; 147 | d1y = qx2*xSource[0] + qz2*xSource[2]; 148 | d1z = qz2*xSource[1] - qw2*xSource[0] - 2*qy2*xSource[2]; 149 | 150 | d2x = qx2*xSource[2] - qw2*xSource[1] - 2*qz2*xSource[0]; 151 | d2y = qw2*xSource[0] - 2*qz2*xSource[1] + qy2*xSource[2]; 152 | d2z = qx2*xSource[0] + qy2*xSource[1]; 153 | 154 | d3x = qy2*xSource[2] - qz2*xSource[1]; 155 | d3y = qz2*xSource[0] - qx2*xSource[2]; 156 | d3z = qx2*xSource[1] - qy2*xSource[0]; 157 | 158 | precalcXY = xUnprojected[2] * xUnprojected[2]; 159 | precalcX = -otherInfo[0] / precalcXY; 160 | precalcY = -otherInfo[1] / precalcXY; 161 | 162 | derivatives[0] = precalcX * (xUnprojected[2] * d0x - xUnprojected[0] * d0z) + precalcY * (xUnprojected[2] * d0y - xUnprojected[1] * d0z); 163 | derivatives[1] = precalcX * (xUnprojected[2] * d1x - xUnprojected[0] * d1z) + precalcY * (xUnprojected[2] * d1y - xUnprojected[1] * d1z); 164 | derivatives[2] = precalcX * (xUnprojected[2] * d2x - xUnprojected[0] * d2z) + precalcY * (xUnprojected[2] * d2y - xUnprojected[1] * d2z); 165 | derivatives[3] = precalcX * (xUnprojected[2] * d3x - xUnprojected[0] * d3z) + precalcY * (xUnprojected[2] * d3y - xUnprojected[1] * d3z); 166 | } 167 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Transforms/CameraCoordinateTransform.cpp: -------------------------------------------------------------------------------- 1 | #include "CameraCoordinateTransform.h" 2 | 3 | using namespace Renderer::Transforms; 4 | 5 | CameraCoordinateTransform::CameraCoordinateTransform(void) 6 | { 7 | projectionMatrix = new VFLOAT[16]; 8 | projectionMatrixGL = new VFLOAT[16]; 9 | 10 | for (int i=0; i<16; i++) 11 | { 12 | projectionMatrix[i] = 0; 13 | projectionMatrixGL[i] = 0; 14 | } 15 | } 16 | 17 | CameraCoordinateTransform::~CameraCoordinateTransform(void) 18 | { 19 | delete projectionMatrix; 20 | delete projectionMatrixGL; 21 | } 22 | 23 | void CameraCoordinateTransform::SetProjectionMatrix(VFLOAT *projectionMatrix) 24 | { 25 | this->projectionMatrix[0] = projectionMatrix[0]; 26 | this->projectionMatrix[5] = projectionMatrix[5]; 27 | this->projectionMatrix[8] = projectionMatrix[8]; 28 | this->projectionMatrix[9] = projectionMatrix[9]; 29 | this->projectionMatrix[10] = projectionMatrix[10]; 30 | this->projectionMatrix[14] = projectionMatrix[14]; 31 | } 32 | 33 | void CameraCoordinateTransform::SetProjectionMatrix() 34 | { 35 | this->projectionMatrix[0] = 1; 36 | this->projectionMatrix[5] = 1; 37 | this->projectionMatrix[10] = 1; 38 | this->projectionMatrix[15] = 1; 39 | } 40 | 41 | void CameraCoordinateTransform::GetProjectionParameters(ProjectionParams *params) 42 | { 43 | params->A = projectionMatrix[0]; 44 | params->B = projectionMatrix[5]; 45 | params->C = projectionMatrix[8]; 46 | params->D = projectionMatrix[9]; 47 | params->E = projectionMatrix[10]; 48 | params->F = projectionMatrix[14]; 49 | 50 | params->all[0] = params->A; 51 | params->all[1] = params->B; 52 | params->all[2] = params->C; 53 | params->all[3] = params->D; 54 | params->all[4] = params->E; 55 | params->all[5] = params->F; 56 | } 57 | 58 | void CameraCoordinateTransform::decompKMatrix(VFLOAT source[3][4], VFLOAT cpara[3][4], VFLOAT trans[3][4]) 59 | { 60 | int r, c; 61 | VFLOAT Cpara[3][4]; 62 | VFLOAT rem1, rem2, rem3; 63 | 64 | if( source[2][3] >= 0 ) 65 | { 66 | for( r = 0; r < 3; r++ ) 67 | for( c = 0; c < 4; c++ ) 68 | Cpara[r][c] = source[r][c]; 69 | } 70 | else 71 | { 72 | for( r = 0; r < 3; r++ ) 73 | for( c = 0; c < 4; c++ ) 74 | Cpara[r][c] = -(source[r][c]); 75 | } 76 | 77 | for( r = 0; r < 3; r++ ) 78 | for( c = 0; c < 4; c++ ) 79 | cpara[r][c] = 0.0; 80 | 81 | cpara[2][2] = norm( Cpara[2][0], Cpara[2][1], Cpara[2][2] ); 82 | trans[2][0] = Cpara[2][0] / cpara[2][2]; 83 | trans[2][1] = Cpara[2][1] / cpara[2][2]; 84 | trans[2][2] = Cpara[2][2] / cpara[2][2]; 85 | trans[2][3] = Cpara[2][3] / cpara[2][2]; 86 | 87 | cpara[1][2] = dot( trans[2][0], trans[2][1], trans[2][2], 88 | Cpara[1][0], Cpara[1][1], Cpara[1][2] ); 89 | rem1 = Cpara[1][0] - cpara[1][2] * trans[2][0]; 90 | rem2 = Cpara[1][1] - cpara[1][2] * trans[2][1]; 91 | rem3 = Cpara[1][2] - cpara[1][2] * trans[2][2]; 92 | cpara[1][1] = norm( rem1, rem2, rem3 ); 93 | trans[1][0] = rem1 / cpara[1][1]; 94 | trans[1][1] = rem2 / cpara[1][1]; 95 | trans[1][2] = rem3 / cpara[1][1]; 96 | 97 | cpara[0][2] = dot( trans[2][0], trans[2][1], trans[2][2], 98 | Cpara[0][0], Cpara[0][1], Cpara[0][2] ); 99 | cpara[0][1] = dot( trans[1][0], trans[1][1], trans[1][2], 100 | Cpara[0][0], Cpara[0][1], Cpara[0][2] ); 101 | rem1 = Cpara[0][0] - cpara[0][1]*trans[1][0] - cpara[0][2]*trans[2][0]; 102 | rem2 = Cpara[0][1] - cpara[0][1]*trans[1][1] - cpara[0][2]*trans[2][1]; 103 | rem3 = Cpara[0][2] - cpara[0][1]*trans[1][2] - cpara[0][2]*trans[2][2]; 104 | cpara[0][0] = norm( rem1, rem2, rem3 ); 105 | trans[0][0] = rem1 / cpara[0][0]; 106 | trans[0][1] = rem2 / cpara[0][0]; 107 | trans[0][2] = rem3 / cpara[0][0]; 108 | 109 | trans[1][3] = (Cpara[1][3] - cpara[1][2]*trans[2][3]) / cpara[1][1]; 110 | trans[0][3] = (Cpara[0][3] - cpara[0][1]*trans[1][3] 111 | - cpara[0][2]*trans[2][3]) / cpara[0][0]; 112 | 113 | for( r = 0; r < 3; r++ ) 114 | for( c = 0; c < 3; c++ ) 115 | cpara[r][c] /= cpara[2][2]; 116 | } 117 | 118 | void CameraCoordinateTransform::SetProjectionMatrix(char *cameraCalibrationFile, VFLOAT zNear, VFLOAT zFar) 119 | { 120 | Camera3D* cam3D = new Camera3D(cameraCalibrationFile); 121 | 122 | this->zFar = zFar; 123 | this->zNear = zNear; 124 | 125 | this->SetProjectionMatrix(cam3D, zNear, zFar); 126 | 127 | delete cam3D; 128 | } 129 | 130 | void CameraCoordinateTransform::SetProjectionMatrix(Camera3D* camera, VFLOAT zNear, VFLOAT zFar) 131 | { 132 | int i,j; 133 | 134 | if(zNear<=0 || zFar<=0) 135 | { 136 | printf("Set projection matrix to: zNear:%f, zFar:%f \n", zNear, zFar); 137 | printf("Error! Invalid projectionMatrix.\n"); 138 | exit(-1); 139 | } 140 | 141 | VFLOAT icpara[3][4]; 142 | VFLOAT trans[3][4]; 143 | VFLOAT p[3][3]; 144 | VFLOAT q[4][4], qGL[4][4]; 145 | 146 | decompKMatrix(camera->K, icpara, trans); 147 | for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) p[i][j] = icpara[i][j] / icpara[2][2]; 148 | 149 | q[0][0] = (2.0f * p[0][0] / camera->SizeX); 150 | q[0][1] = (2.0f * p[0][1] / camera->SizeX); 151 | q[0][2] = ((2.0f * p[0][2] / camera->SizeX) - 1.0f); 152 | q[0][3] = 0.0f; 153 | 154 | q[1][0] = 0.0f; 155 | q[1][1] = -(2.0f * p[1][1] / camera->SizeY); 156 | q[1][2] = ((2.0f * p[1][2] / camera->SizeY) - 1.0f); 157 | q[1][3] = 0.0f; 158 | 159 | q[2][0] = 0.0f; 160 | q[2][1] = 0.0f; 161 | q[2][2] = (zFar + zNear) / (zFar - zNear); 162 | q[2][3] = -2.0f * zFar * zNear / (zFar - zNear); 163 | 164 | q[3][0] = 0.0f; 165 | q[3][1] = 0.0f; 166 | q[3][2] = 1.0f; 167 | q[3][3] = 0.0f; 168 | 169 | for (i = 0; i < 4; i++) 170 | { 171 | for (j = 0; j < 3; j++) 172 | { projectionMatrix[i + j * 4] = q[i][0] * trans[0][j] + q[i][1] * trans[1][j] + q[i][2] * trans[2][j]; } 173 | projectionMatrix[i + 3 * 4] = q[i][0] * trans[0][3] + q[i][1] * trans[1][3] + q[i][2] * trans[2][3] + q[i][3]; 174 | } 175 | 176 | decompKMatrix(camera->KGL, icpara, trans); 177 | for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { p[i][j] = icpara[i][j] / icpara[2][2]; } 178 | 179 | qGL[0][0] = (2.0f * p[0][0] / camera->SizeX); 180 | qGL[0][1] = (2.0f * p[0][1] / camera->SizeX); 181 | qGL[0][2] = 1.0f - (2.0f * p[0][2] / camera->SizeX); 182 | qGL[0][3] = 0.0f; 183 | 184 | qGL[1][0] = 0.0f; 185 | qGL[1][1] = (2.0f * p[1][1] / camera->SizeY); 186 | qGL[1][2] = ((2.0f * p[1][2] / camera->SizeY) - 1.0f); 187 | qGL[1][3] = 0.0f; 188 | 189 | qGL[2][0] = 0.0f; 190 | qGL[2][1] = 0.0f; 191 | qGL[2][2] = (zFar + zNear) / (zNear - zFar); 192 | qGL[2][3] = 2.0f * zFar * zNear / (zNear - zFar); 193 | 194 | qGL[3][0] = 0.0f; 195 | qGL[3][1] = 0.0f; 196 | qGL[3][2] = -1.0f; 197 | qGL[3][3] = 0.0f; 198 | 199 | for (i = 0; i < 4; i++) 200 | { 201 | for (j = 0; j < 3; j++) 202 | { projectionMatrixGL[i + j * 4] = qGL[i][0] * trans[0][j] + qGL[i][1] * trans[1][j] + qGL[i][2] * trans[2][j]; } 203 | projectionMatrixGL[i + 3 * 4] = qGL[i][0] * trans[0][3] + qGL[i][1] * trans[1][3] + qGL[i][2] * trans[2][3] + qGL[i][3]; 204 | } 205 | } 206 | 207 | void CameraCoordinateTransform::SetProjectionMatrix(VFLOAT fovy, VFLOAT aspect, VFLOAT zNear, VFLOAT zFar) 208 | { 209 | this->zFar = zFar; 210 | this->zNear = zNear; 211 | this->fovy = fovy; 212 | 213 | memset(projectionMatrix, 0, 16 * sizeof(float)); 214 | VFLOAT f = (VFLOAT) 1.0 / (VFLOAT) tan(PI/180 * fovy/2); 215 | projectionMatrix[0] = f/aspect; 216 | projectionMatrix[5] = f; 217 | projectionMatrix[10] = -1 * (zFar + zNear)/(zFar - zNear); 218 | projectionMatrix[11] = -1; 219 | projectionMatrix[14] = -2 * (zFar * zNear)/(zFar - zNear); 220 | } 221 | 222 | void CameraCoordinateTransform::GetInvPMatrix(VFLOAT* prod) 223 | { 224 | MathUtils::Instance()->InvertMatrix4(prod, projectionMatrix); 225 | } 226 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Transforms/CameraCoordinateTransform.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace PerseusLib::Utils; 11 | using namespace Renderer::Primitives; 12 | 13 | 14 | namespace Renderer 15 | { 16 | namespace Transforms 17 | { 18 | class CameraCoordinateTransform 19 | { 20 | private: 21 | void decompKMatrix(VFLOAT source[3][4], VFLOAT cpara[3][4], VFLOAT trans[3][4]); 22 | 23 | VFLOAT norm(VFLOAT a, VFLOAT b, VFLOAT c) { 24 | return ((VFLOAT)sqrtf(a * a + b * b + c * c)); 25 | } 26 | VFLOAT dot(VFLOAT a1, VFLOAT a2, VFLOAT a3, VFLOAT b1, VFLOAT b2, VFLOAT b3) { 27 | return (a1 * b1 + a2 * b2 + a3 * b3); 28 | } 29 | 30 | public: 31 | struct ProjectionParams 32 | { 33 | VFLOAT all[6]; 34 | VFLOAT A,B,C,D,E,F; 35 | 36 | }projectionParams; 37 | 38 | VFLOAT *projectionMatrix, *projectionMatrixGL; 39 | 40 | VFLOAT zFar, zNear, fovy; 41 | 42 | void SetProjectionMatrix(VFLOAT *projectionMatrix); 43 | void SetProjectionMatrix(); 44 | void SetProjectionMatrix(VFLOAT fovy, VFLOAT aspect, VFLOAT zNear, VFLOAT zFar); 45 | void SetProjectionMatrix(Camera3D* camera, VFLOAT zNear, VFLOAT zFar); 46 | void SetProjectionMatrix(char* cameraCalibrationFile, VFLOAT zNear, VFLOAT zFar); 47 | 48 | void GetProjectionMatrix(VFLOAT *pmatrix) { for (int i=0; i< 16; i++) pmatrix[i] = projectionMatrix[i]; } 49 | void GetProjectionMatrixGL(VFLOAT *pmatrix) { for (int i=0; i< 16; i++) pmatrix[i] = projectionMatrixGL[i]; } 50 | void GetProjectionParameters(ProjectionParams *params); 51 | 52 | void GetInvPMatrix(VFLOAT* prod); 53 | 54 | CameraCoordinateTransform(void); 55 | ~CameraCoordinateTransform(void); 56 | }; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Transforms/CoordinateTransform.cpp: -------------------------------------------------------------------------------- 1 | #include "CoordinateTransform.h" 2 | 3 | #include 4 | 5 | using namespace Renderer::Primitives; 6 | using namespace Renderer::Transforms; 7 | 8 | CoordinateTransform::CoordinateTransform(void) 9 | { 10 | int i; 11 | 12 | modelViewMatrix = new VFLOAT[16]; 13 | projectionMatrix = new VFLOAT[16]; 14 | translation = new VFLOAT[3]; 15 | view = new int[4]; 16 | 17 | for (i=0;i<16;i++) 18 | { 19 | modelViewMatrix[i] = 0; 20 | projectionMatrix[i] = 0; 21 | } 22 | 23 | translation = VECTOR3DA(0,0,0); 24 | 25 | qrotation = Quaternion(0,0,0,0); 26 | } 27 | 28 | CoordinateTransform::~CoordinateTransform(void) 29 | { 30 | delete modelViewMatrix; 31 | delete projectionMatrix; 32 | delete view; 33 | } 34 | 35 | void CoordinateTransform::SetProjectionMatrix(VFLOAT *projectionMatrix) 36 | { 37 | this->projectionMatrix[0] = projectionMatrix[0]; 38 | this->projectionMatrix[5] = projectionMatrix[5]; 39 | this->projectionMatrix[10] = projectionMatrix[10]; 40 | this->projectionMatrix[11] = projectionMatrix[11]; 41 | this->projectionMatrix[14] = projectionMatrix[14]; 42 | } 43 | 44 | void CoordinateTransform::SetProjectionMatrix() 45 | { 46 | this->projectionMatrix[0] = 1; 47 | this->projectionMatrix[5] = 1; 48 | this->projectionMatrix[10] = 1; 49 | this->projectionMatrix[15] = 1; 50 | } 51 | 52 | void CoordinateTransform::GetProjectionParameters(ProjectionParams *params) 53 | { 54 | params->A = projectionMatrix[0]; 55 | params->B = projectionMatrix[5]; 56 | params->C = projectionMatrix[8]; 57 | params->D = projectionMatrix[9]; 58 | params->E = projectionMatrix[10]; 59 | params->F = projectionMatrix[14]; 60 | 61 | params->all[0] = params->A; 62 | params->all[1] = params->B; 63 | params->all[2] = params->C; 64 | params->all[3] = params->D; 65 | params->all[4] = params->E; 66 | params->all[5] = params->F; 67 | } 68 | 69 | void CoordinateTransform::decompKMatrix(VFLOAT source[3][4], VFLOAT cpara[3][4], VFLOAT trans[3][4]) 70 | { 71 | int r, c; 72 | VFLOAT Cpara[3][4]; 73 | VFLOAT rem1, rem2, rem3; 74 | 75 | if( source[2][3] >= 0 ) 76 | { 77 | for( r = 0; r < 3; r++ ) 78 | for( c = 0; c < 4; c++ ) 79 | Cpara[r][c] = source[r][c]; 80 | } 81 | else 82 | { 83 | for( r = 0; r < 3; r++ ) 84 | for( c = 0; c < 4; c++ ) 85 | Cpara[r][c] = -(source[r][c]); 86 | } 87 | 88 | for( r = 0; r < 3; r++ ) 89 | for( c = 0; c < 4; c++ ) 90 | cpara[r][c] = 0.0; 91 | 92 | cpara[2][2] = norm( Cpara[2][0], Cpara[2][1], Cpara[2][2] ); 93 | trans[2][0] = Cpara[2][0] / cpara[2][2]; 94 | trans[2][1] = Cpara[2][1] / cpara[2][2]; 95 | trans[2][2] = Cpara[2][2] / cpara[2][2]; 96 | trans[2][3] = Cpara[2][3] / cpara[2][2]; 97 | 98 | cpara[1][2] = dot( trans[2][0], trans[2][1], trans[2][2], 99 | Cpara[1][0], Cpara[1][1], Cpara[1][2] ); 100 | rem1 = Cpara[1][0] - cpara[1][2] * trans[2][0]; 101 | rem2 = Cpara[1][1] - cpara[1][2] * trans[2][1]; 102 | rem3 = Cpara[1][2] - cpara[1][2] * trans[2][2]; 103 | cpara[1][1] = norm( rem1, rem2, rem3 ); 104 | trans[1][0] = rem1 / cpara[1][1]; 105 | trans[1][1] = rem2 / cpara[1][1]; 106 | trans[1][2] = rem3 / cpara[1][1]; 107 | 108 | cpara[0][2] = dot( trans[2][0], trans[2][1], trans[2][2], 109 | Cpara[0][0], Cpara[0][1], Cpara[0][2] ); 110 | cpara[0][1] = dot( trans[1][0], trans[1][1], trans[1][2], 111 | Cpara[0][0], Cpara[0][1], Cpara[0][2] ); 112 | rem1 = Cpara[0][0] - cpara[0][1]*trans[1][0] - cpara[0][2]*trans[2][0]; 113 | rem2 = Cpara[0][1] - cpara[0][1]*trans[1][1] - cpara[0][2]*trans[2][1]; 114 | rem3 = Cpara[0][2] - cpara[0][1]*trans[1][2] - cpara[0][2]*trans[2][2]; 115 | cpara[0][0] = norm( rem1, rem2, rem3 ); 116 | trans[0][0] = rem1 / cpara[0][0]; 117 | trans[0][1] = rem2 / cpara[0][0]; 118 | trans[0][2] = rem3 / cpara[0][0]; 119 | 120 | trans[1][3] = (Cpara[1][3] - cpara[1][2]*trans[2][3]) / cpara[1][1]; 121 | trans[0][3] = (Cpara[0][3] - cpara[0][1]*trans[1][3] 122 | - cpara[0][2]*trans[2][3]) / cpara[0][0]; 123 | 124 | for( r = 0; r < 3; r++ ) 125 | for( c = 0; c < 3; c++ ) 126 | cpara[r][c] /= cpara[2][2]; 127 | } 128 | 129 | void CoordinateTransform::SetProjectionMatrix(Camera3D* camera, VFLOAT zNear, VFLOAT zFar) 130 | { 131 | int i,j; 132 | 133 | VFLOAT icpara[3][4]; 134 | VFLOAT trans[3][4]; 135 | VFLOAT p[3][3]; 136 | VFLOAT q[4][4]; 137 | 138 | decompKMatrix(camera->K, icpara, trans); 139 | for (i = 0; i < 3; i++) 140 | { 141 | for (j = 0; j < 3; j++) 142 | { 143 | p[i][j] = icpara[i][j] / icpara[2][2]; 144 | } 145 | } 146 | q[0][0] = (2.0f * p[0][0] / camera->SizeX); 147 | q[0][1] = (2.0f * p[0][1] / camera->SizeX); 148 | q[0][2] = (1.0f - (2.0f * p[0][2] / camera->SizeX)); 149 | q[0][3] = 0.0f; 150 | 151 | q[1][0] = 0.0f; 152 | q[1][1] = -(2.0f * p[1][1] / camera->SizeY); 153 | q[1][2] = (1.0f - (2.0f * p[1][2] / camera->SizeY)); 154 | q[1][3] = 0.0f; 155 | 156 | q[2][0] = 0.0f; 157 | q[2][1] = 0.0f; 158 | q[2][2] = -(zFar + zNear) / (zNear - zFar); 159 | q[2][3] = -2 * (zFar * zNear) / (zNear - zFar); 160 | 161 | q[3][0] = 0.0f; 162 | q[3][1] = 0.0f; 163 | q[3][2] = 1.0f; 164 | q[3][3] = 0.0f; 165 | 166 | for (i = 0; i < 4; i++) 167 | { 168 | for (j = 0; j < 3; j++) 169 | { 170 | projectionMatrix[i + j * 4] = q[i][0] * trans[0][j] 171 | + q[i][1] * trans[1][j] 172 | + q[i][2] * trans[2][j]; 173 | } 174 | projectionMatrix[i + 3 * 4] = q[i][0] * trans[0][3] 175 | + q[i][1] * trans[1][3] 176 | + q[i][2] * trans[2][3] 177 | + q[i][3]; 178 | } 179 | } 180 | 181 | void CoordinateTransform::SetProjectionMatrix(VFLOAT fovy, VFLOAT aspect, VFLOAT zNear, VFLOAT zFar) 182 | { 183 | this->zFar = zFar; 184 | this->zNear = zNear; 185 | this->fovy = fovy; 186 | 187 | memset(projectionMatrix, 0, 16 * sizeof(float)); 188 | VFLOAT f = (VFLOAT) 1.0 / (VFLOAT) tan(PI/180 * fovy/2); 189 | projectionMatrix[0] = f/aspect; 190 | projectionMatrix[5] = f; 191 | projectionMatrix[10] = -1 * (zFar + zNear)/(zFar - zNear); 192 | projectionMatrix[11] = -1; 193 | projectionMatrix[14] = -2 * (zFar * zNear)/(zFar - zNear); 194 | } 195 | 196 | void CoordinateTransform::GetModelViewMatrix(VFLOAT *returnMatrix) 197 | { 198 | int i; 199 | 200 | returnMatrix[0] = 1; 201 | returnMatrix[5] = 1; 202 | returnMatrix[10] = 1; 203 | returnMatrix[15] = 1; 204 | returnMatrix[3] = 0; 205 | returnMatrix[7] = 0; 206 | returnMatrix[11] = 0; 207 | 208 | returnMatrix[12] = this->translation.x; 209 | returnMatrix[13] = this->translation.y; 210 | returnMatrix[14] = this->translation.z; 211 | 212 | VFLOAT matrixFromSource[16]; 213 | rotation->GetMatrix(matrixFromSource); 214 | 215 | returnMatrix[0] = matrixFromSource[0]; 216 | returnMatrix[1] = matrixFromSource[1]; 217 | returnMatrix[2] = matrixFromSource[2]; 218 | returnMatrix[4] = matrixFromSource[4]; 219 | returnMatrix[5] = matrixFromSource[5]; 220 | returnMatrix[6] = matrixFromSource[6]; 221 | returnMatrix[8] = matrixFromSource[8]; 222 | returnMatrix[9] = matrixFromSource[9]; 223 | returnMatrix[10] = matrixFromSource[10]; 224 | 225 | VFLOAT norm = 1/returnMatrix[15]; 226 | for (i=0;i<16;i++) returnMatrix[i] *= norm; 227 | } 228 | 229 | void CoordinateTransform::GetPMMatrix(VFLOAT* prod) 230 | { 231 | VFLOAT m[16]; 232 | this->GetModelViewMatrix(m); 233 | 234 | MathUtils::Instance()->SquareMatrixProduct(prod, projectionMatrix, m, 4); 235 | } 236 | 237 | void CoordinateTransform::GetInvPMMatrix(VFLOAT* prod) 238 | { 239 | VFLOAT m[16],pm[16]; 240 | this->GetModelViewMatrix(m); 241 | 242 | MathUtils::Instance()->SquareMatrixProduct(pm, projectionMatrix, m, 4); 243 | MathUtils::Instance()->InvertMatrix4(prod, pm); 244 | } 245 | 246 | void CoordinateTransform::GetInvPMatrix(VFLOAT* prod) 247 | { 248 | MathUtils::Instance()->InvertMatrix4(prod, projectionMatrix); 249 | } 250 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Transforms/CoordinateTransform.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | using namespace PerseusLib::Utils; 15 | using namespace PerseusLib::Primitives; 16 | 17 | using namespace Renderer::Primitives; 18 | 19 | namespace Renderer 20 | { 21 | namespace Transforms 22 | { 23 | class CoordinateTransform 24 | { 25 | std::vector cameraTransforms; 26 | std::vector objectTransforms; 27 | 28 | void decompKMatrix(VFLOAT source[3][4], VFLOAT cpara[3][4], VFLOAT trans[3][4]); 29 | 30 | VFLOAT norm(VFLOAT a, VFLOAT b, VFLOAT c) { 31 | return ((VFLOAT)sqrtf(a * a + b * b + c * c)); 32 | } 33 | VFLOAT dot(VFLOAT a1, VFLOAT a2, VFLOAT a3, VFLOAT b1, VFLOAT b2, VFLOAT b3) { 34 | return (a1 * b1 + a2 * b2 + a3 * b3); 35 | } 36 | 37 | public: 38 | struct ProjectionParams 39 | { 40 | VFLOAT all[6]; 41 | VFLOAT A,B,C,D,E,F; 42 | 43 | }projectionParams; 44 | 45 | Quaternion *rotation; 46 | 47 | VFLOAT *modelViewMatrix; 48 | VFLOAT *projectionMatrix; 49 | VFLOAT *pmMatrix; 50 | 51 | int* view; 52 | 53 | VFLOAT zFar, zNear, fovy; 54 | 55 | Quaternion qrotation; 56 | VECTOR3DA translation; 57 | 58 | void SetTranslation(VFLOAT* translation) { 59 | this->translation.x = translation[0]; this->translation.y = translation[1]; this->translation.z = translation[2]; 60 | } 61 | void SetTranslation(VECTOR3DA translation) { this->translation = translation; } 62 | void SetTranslation(VECTOR3DA* translation) { this->translation = *translation; } 63 | 64 | void AddTranslation(VFLOAT *translation) { 65 | this->translation.x += translation[0]; this->translation.y += translation[1]; this->translation.z += translation[2]; 66 | } 67 | void AddTranslation(VECTOR3DA translation) { 68 | this->translation.x += translation.x; this->translation.y += translation.y; this->translation.z += translation.z; 69 | } 70 | void AddTranslation(VECTOR3DA *translation) { 71 | this->translation.x += translation->x; this->translation.y += translation->y; this->translation.z += translation->z; 72 | } 73 | 74 | void SetRotation(Quaternion* rotation) { this->rotation = rotation; } 75 | 76 | void SetProjectionMatrix(VFLOAT *projectionMatrix); 77 | void SetProjectionMatrix(); 78 | void SetProjectionMatrix(VFLOAT fovy, VFLOAT aspect, VFLOAT zNear, VFLOAT zFar); 79 | void SetProjectionMatrix(Camera3D* camera, VFLOAT zNear, VFLOAT zFar); 80 | 81 | void SetViewPort(int x, int y, int width, int height) { view[0] = x; view[1] = y; view[2] = width; view[3] = height; } 82 | 83 | void GetProjectionMatrix(VFLOAT *pmatrix) { for (int i=0; i< 16; i++) pmatrix[i] = projectionMatrix[i]; } 84 | void GetModelViewMatrix(VFLOAT* ); 85 | void GetPMMatrix(VFLOAT*); 86 | void GetProjectionParameters(ProjectionParams *params); 87 | 88 | void GetInvPMMatrix(VFLOAT*); 89 | void GetInvPMatrix(VFLOAT* prod); 90 | 91 | //void GetViewPort(int* mview) { mview = view; } 92 | 93 | CoordinateTransform(void); 94 | ~CoordinateTransform(void); 95 | }; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /PerseusLib/Renderer/Transforms/ObjectCoordinateTransform.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace Renderer::Primitives; 8 | 9 | namespace Renderer 10 | { 11 | namespace Transforms 12 | { 13 | class ObjectCoordinateTransform 14 | { 15 | public: 16 | public: 17 | Quaternion *rotation; 18 | VECTOR3DA *translation; 19 | 20 | 21 | void SetFrom(float *pose) { this->SetFrom(pose[0], pose[1], pose[2], pose[3], pose[4], pose[5], pose[6]); } 22 | 23 | void SetFrom(ObjectCoordinateTransform *pose) { 24 | this->translation->x = pose->translation->x; 25 | this->translation->y = pose->translation->y; 26 | this->translation->z = pose->translation->z; 27 | this->rotation->vector4d.x = pose->rotation->vector4d.x; 28 | this->rotation->vector4d.y = pose->rotation->vector4d.y; 29 | this->rotation->vector4d.z = pose->rotation->vector4d.z; 30 | this->rotation->vector4d.w = pose->rotation->vector4d.w; 31 | } 32 | 33 | void SetFrom(VECTOR3DA* translation, Quaternion* rotation){ 34 | // printf("[SetFrom/1] \n"); 35 | 36 | this->rotation->Set(rotation); 37 | this->translation->x = translation->x; 38 | this->translation->y = translation->y; 39 | this->translation->z = translation->z; 40 | } 41 | 42 | void SetFrom(VFLOAT tX, VFLOAT tY, VFLOAT tZ, VFLOAT rX, VFLOAT rY, VFLOAT rZ) { 43 | this->translation->x = tX; 44 | this->translation->y = tY; 45 | this->translation->z = tZ; 46 | this->rotation->SetFromEuler(rX,rY,rZ); 47 | } 48 | 49 | void SetFrom(VFLOAT tX, VFLOAT tY, VFLOAT tZ, VFLOAT rX, VFLOAT rY, VFLOAT rZ, VFLOAT rW) { 50 | this->translation->x = tX; 51 | this->translation->y = tY; 52 | this->translation->z = tZ; 53 | this->rotation->vector4d.x = rX; 54 | this->rotation->vector4d.y = rY; 55 | this->rotation->vector4d.z = rZ; 56 | this->rotation->vector4d.w = rW; 57 | } 58 | 59 | void GetModelViewMatrix(float *returnMatrix) 60 | { 61 | VFLOAT matrixFromSource[16]; 62 | rotation->GetMatrix(matrixFromSource); 63 | 64 | // printf("[etModelViewMatrix] rotation is %f,%f,%f,%f\n",rotation->vector4d.x,rotation->vector4d.y, rotation->vector4d.z,rotation->vector4d.w); 65 | 66 | returnMatrix[0] = matrixFromSource[0]; 67 | returnMatrix[1] = matrixFromSource[1]; 68 | returnMatrix[2] = matrixFromSource[2]; 69 | returnMatrix[3] = 0.0f; 70 | 71 | returnMatrix[4] = matrixFromSource[4]; 72 | returnMatrix[5] = matrixFromSource[5]; 73 | returnMatrix[6] = matrixFromSource[6]; 74 | returnMatrix[7] = 0.0f; 75 | 76 | returnMatrix[8] = matrixFromSource[8]; 77 | returnMatrix[9] = matrixFromSource[9]; 78 | returnMatrix[10] = matrixFromSource[10]; 79 | returnMatrix[11] = 0.0f; 80 | 81 | returnMatrix[12] = translation->x; 82 | returnMatrix[13] = translation->y; 83 | returnMatrix[14] = translation->z; 84 | returnMatrix[15] = 1.0f; 85 | } 86 | 87 | void CopyInto(ObjectCoordinateTransform *transform) { 88 | transform->translation->x = this->translation->x; 89 | transform->translation->y = this->translation->y; 90 | transform->translation->z = this->translation->z; 91 | transform->rotation->vector4d.x = this->rotation->vector4d.x; 92 | transform->rotation->vector4d.y = this->rotation->vector4d.y; 93 | transform->rotation->vector4d.z = this->rotation->vector4d.z; 94 | transform->rotation->vector4d.w = this->rotation->vector4d.w; 95 | } 96 | 97 | void Clear() 98 | { 99 | this->translation->x = 0; this->translation->y = 0; this->translation->z = 0; 100 | this->rotation->vector4d.x = 0; this->rotation->vector4d.y = 0; this->rotation->vector4d.z = 0; this->rotation->vector4d.w = 0; 101 | } 102 | 103 | ObjectCoordinateTransform(VFLOAT tX, VFLOAT tY, VFLOAT tZ, VFLOAT rX, VFLOAT rY, VFLOAT rZ) { 104 | rotation = new Quaternion(); translation = new VECTOR3DA(); 105 | this->SetFrom(tX, tY, tZ, rX, rY, rZ); 106 | } 107 | 108 | ObjectCoordinateTransform(void) { rotation = new Quaternion(); translation = new VECTOR3DA(); } 109 | ~ObjectCoordinateTransform(void) { if (rotation) delete rotation; if (translation) delete translation; } 110 | }; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /PerseusLib/Utils/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // disable non-sense warning 4 | #pragma warning(disable: 4100) //unreferenced formal parameter 5 | #pragma warning(disable: 4996) //nonsense "CRT deprecated" MS initiative 6 | #pragma warning(disable: 4512) //unassignable object are by design 7 | #pragma warning(disable: 4345) //POD default initialization 8 | 9 | // inliend globals 10 | #define SELECTANY _declspec(selectany) ///< use for inline global data 11 | 12 | // Debug-only expressions 13 | #ifdef _DEBUG 14 | #define DBG(x) x 15 | #else 16 | #define DBG(x) 17 | #endif 18 | #define COMMA , ///< use in macro parameters, where no separator is required 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #include 25 | #include 26 | 27 | #ifndef MSGOUT 28 | #define MSGOUT std::cout// dbg::out() 29 | #endif 30 | 31 | namespace PerseusLib 32 | { 33 | namespace Utils 34 | { 35 | namespace dbg{ 36 | // \namespace GE_::dbg contains debug helpers 37 | 38 | typedef std::basic_ostream ostream_t; ///< type of stream 39 | 40 | /// inner details 41 | namespace report_h{ 42 | 43 | /// contains globals data and related functions 44 | class globals 45 | { 46 | public: 47 | ostream_t stream; ///< the debug stream 48 | /// stream buffer 49 | class streambuff: 50 | public std::basic_streambuf 51 | { 52 | static const size_t BUFFSZ = 1; 53 | TCHAR buffer[BUFFSZ]; 54 | public: 55 | /// imitialize the debug library 56 | streambuff() 57 | { 58 | _CrtSetDbgFlag( 59 | _CRTDBG_LEAK_CHECK_DF| 60 | _CRTDBG_ALLOC_MEM_DF| 61 | _CRTDBG_CHECK_ALWAYS_DF); 62 | OutputDebugStringA("creating debug stream buffer for "); 63 | OutputDebugStringA(typeid(char_type).name()); 64 | OutputDebugStringA("\n"); 65 | pubsetbuf(buffer,BUFFSZ-1); 66 | } 67 | /// finalize the debug 68 | ~streambuff() 69 | { 70 | OutputDebugStringA("deleting debug stream buffer for "); 71 | OutputDebugStringA(typeid(char_type).name()); 72 | OutputDebugStringA("\n"); 73 | } 74 | /// makes the streambuff working on \c buffer 75 | virtual basic_streambuf* setbuf(TCHAR* _Buffer, std::streamsize _Count) 76 | { 77 | setp(buffer,buffer,buffer+BUFFSZ-1); 78 | return this; 79 | } 80 | 81 | /// sync and rewind when full 82 | virtual int_type overflow(int_type i = traits_type::eof()) 83 | { 84 | sync(); 85 | *pptr() = traits_type::to_char_type(i); 86 | pbump(1); 87 | return traits_type::not_eof(i); 88 | } 89 | /// spit the buffer to the debugger 90 | virtual int sync() 91 | { 92 | char_type* p = pbase(); 93 | int sz = (int)(pptr()-p); 94 | p[sz] = 0; 95 | OutputDebugString(p); 96 | pbump(-sz); 97 | return 0; 98 | } 99 | }; 100 | unsigned depth; ///< the curred recursion detpth 101 | private: 102 | streambuff buff; ///< the streambuff instance 103 | public: 104 | /// initialize the stream 105 | globals() 106 | : stream(&buff), depth(1) 107 | {} 108 | }; 109 | 110 | /// the \c globals global instance 111 | inline globals& global() { static globals g; return g; } 112 | 113 | } 114 | 115 | /// getting the global debugging stream 116 | inline ostream_t& out() { return report_h::global().stream; } 117 | 118 | /// RAII enter/exit tracer 119 | /** \details 120 | increments / decrements the depth 121 | **/ 122 | class trace 123 | { 124 | const char* name; 125 | void* addr; 126 | unsigned& depth() { return report_h::global().depth; } 127 | public: 128 | trace(const char* name_, void * addr_=0) : name(name_), addr(addr_) 129 | { 130 | if(name) 131 | { 132 | out() << std::setw(depth()) << '>' << name; 133 | if(addr) out() << '[' << addr << ']'; 134 | out() << std::endl; 135 | } 136 | ++depth(); 137 | } 138 | ~trace() 139 | { 140 | --depth(); 141 | if(name) 142 | { 143 | out() << std::setw(depth()) << '<' << name; 144 | if(addr) out() << '[' << addr << ']'; 145 | out() << std::endl; 146 | } 147 | } 148 | template 149 | ostream_t& operator<<(const T& t) { out() << std::setw(depth()) << '.' << t; return out(); } 150 | }; 151 | 152 | 153 | inline ostream_t& depth(ostream_t& s) 154 | { s << std::setw(report_h::global().depth) << '.'; return s; } 155 | 156 | /// RAII ctor/dtor tracker 157 | /** \details 158 | won't increment / decrement the depth 159 | **/ 160 | class track 161 | { 162 | protected: 163 | const char* name; 164 | void* addr; 165 | unsigned& depth() { return report_h::global().depth; } 166 | public: 167 | track(const char* name_, void * addr_=0) : name(name_), addr(addr_) 168 | { 169 | out() << std::setw(depth()) << "C:" << name; 170 | if(addr) out() << '[' << addr << ']'; 171 | out() << std::endl; 172 | } 173 | ~track() 174 | { 175 | out() << std::setw(depth()) << "D:" << name; 176 | if(addr) out() << '[' << addr << ']'; 177 | out() << std::endl; 178 | } 179 | template 180 | ostream_t& operator<<(const T& t) { out() << std::setw(depth()) << '.' << t; return out(); } 181 | }; 182 | 183 | template 184 | class trackobj: public track 185 | { 186 | public: 187 | trackobj() : track(typeid(T).name(),&name) 188 | {} 189 | }; 190 | 191 | 192 | inline void init_debug_h() { out(); } 193 | } 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /PerseusLib/Utils/FileUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | //#include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace PerseusLib 15 | { 16 | namespace Utils 17 | { 18 | class FileUtils 19 | { 20 | private: 21 | static FileUtils* instance; 22 | public: 23 | static FileUtils* Instance(void); 24 | 25 | char* TextFileRead(char *fn); 26 | int TextFileWrite(char *fn, char *s); 27 | 28 | void WriteToFile(double *matrix, int m, int n, char *fileName); 29 | void WriteToFile(double *matrix, int m, int n, double* vector, int v, char *fileName); 30 | void WriteToFile(std::vector> matrix, std::string name, std::string fileName); 31 | void WriteToFile(double *matrix, int m, int n, std::string name, char *fileName); 32 | void WriteToFile(double *vector, int n, std::string name, char *fileName); 33 | 34 | void WriteToFile(float *matrix, int m, int n, char *fileName); 35 | void WriteToFile(float *matrix, int m, int n, float* vector, int v, char *fileName); 36 | void WriteToFile(float *matrix, int m, int n, std::string name, char *fileName); 37 | void WriteToFile(std::vector> matrix, std::string name, std::string fileName); 38 | void WriteToFile(float *vector, int n, std::string name, char *fileName); 39 | 40 | void WriteToFile(float **matrix, int m, int n, char* objectName, char* fileName); 41 | 42 | void WriteToFile(int *vector, int n, std::string name, char *fileName); 43 | void WriteToFile(unsigned int *vector, int n, std::string name, char *fileName); 44 | void WriteToFile(unsigned char *vector, int n, std::string name, char *fileName); 45 | 46 | void WriteToFile(std::vector> matrix, std::string name, std::string fileName); 47 | 48 | void ReadFromFile(double *matrix, int &m, int &n, char *fileName); 49 | void ReadFromFile(double *matrix, int &m, int &n, double* vector, int &v, char *fileName); 50 | 51 | void ReadFromFile(float *matrix, int &m, int &n, char *fileName); 52 | void ReadFromFile(float *matrix, int &m, int &n, float* vector, int &v, char *fileName); 53 | 54 | void ReadFromFile(float *vector, int m, std::string fileName); 55 | 56 | FileUtils(void); 57 | ~FileUtils(void); 58 | }; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /PerseusLib/Utils/HistogramEngine.cpp: -------------------------------------------------------------------------------- 1 | #include "HistogramEngine.h" 2 | #include "ImageUtils.h" 3 | 4 | #include 5 | 6 | using namespace PerseusLib::Utils; 7 | 8 | HistogramEngine* HistogramEngine::instance; 9 | 10 | HistogramEngine::HistogramEngine(void) { } 11 | 12 | HistogramEngine::~HistogramEngine(void) { delete instance; } 13 | 14 | void HistogramEngine::NormaliseHistogramVarBin(HistogramVarBin *histogram) 15 | { 16 | int i, j, k, histIndex, histOffset, histNo; 17 | float sumHistogramForeground, sumHistogramBackground; 18 | 19 | sumHistogramForeground = histogram->totalForegroundPixels; 20 | sumHistogramBackground = histogram->totalBackgroundPixels; 21 | 22 | sumHistogramForeground = (sumHistogramForeground != 0) ? 1.0f / sumHistogramForeground : 0; 23 | sumHistogramBackground = (sumHistogramBackground != 0) ? 1.0f / sumHistogramBackground : 0; 24 | 25 | for (histNo = 0; histNonoHistograms; histNo++) 26 | { 27 | histOffset = histogram->histOffsets[histNo]; 28 | 29 | for (i=0; inoBins[histNo]; i++) for (j=0; jnoBins[histNo]; j++) for (k=0; knoBins[histNo]; k++) 30 | { 31 | histIndex = (i + j * histogram->noBins[histNo]) * histogram->noBins[histNo] + k; 32 | if (histogram->alreadyInitialised) 33 | { 34 | histogram->normalised[histOffset + histIndex].x = 35 | histogram->normalised[histOffset + histIndex].x * (1.0f - histogram->mergeAlphaForeground) + 36 | histogram->notnormalised[histOffset + histIndex].x * sumHistogramForeground * histogram->mergeAlphaForeground; 37 | 38 | histogram->normalised[histOffset + histIndex].y = 39 | histogram->normalised[histOffset + histIndex].y * (1.0f - histogram->mergeAlphaBackground) + 40 | histogram->notnormalised[histOffset + histIndex].y * sumHistogramBackground * histogram->mergeAlphaBackground; 41 | } 42 | else 43 | { 44 | histogram->normalised[histOffset + histIndex].x = histogram->notnormalised[histOffset + histIndex].x * sumHistogramForeground; 45 | histogram->normalised[histOffset + histIndex].y = histogram->notnormalised[histOffset + histIndex].y * sumHistogramBackground; 46 | } 47 | } 48 | } 49 | 50 | if (!histogram->alreadyInitialised) histogram->alreadyInitialised = true; 51 | } 52 | 53 | 54 | void HistogramEngine::BuildHistogramVarBin(HistogramVarBin *histogram, ImageUChar *mask, ImageUChar4* image, int objectId) 55 | { 56 | int i, j, idx; 57 | PixelUCHAR4 pixel; 58 | 59 | for (j = 0; j < image->height; j++) for (i = 0; i < image->width; i++) 60 | { 61 | idx = i + j * mask->width; 62 | 63 | pixel = image->pixels[idx]; 64 | 65 | if (mask->pixels[idx] != 0) 66 | { 67 | if ((mask->pixels[idx] - 1) == objectId) 68 | histogram->AddPoint(1, 0, pixel.x, pixel.y, pixel.z, i, j); 69 | } 70 | else 71 | histogram->AddPoint(0, 1, pixel.x, pixel.y, pixel.z, i, j); 72 | } 73 | printf("BuildHistogramVarBin success.\n"); 74 | } 75 | 76 | void HistogramEngine::BuildHistogramVarBin(HistogramVarBin *histogram, ImageUChar *mask, ImageUChar *videoMask, ImageUChar4* image, int objectId) 77 | { 78 | int i, j, idx; 79 | PixelUCHAR4 pixel; 80 | 81 | for (j = 0; j < image->height; j++) for (i = 0; i < image->width; i++) 82 | { 83 | idx = i + j * mask->width; 84 | 85 | pixel = image->pixels[idx]; 86 | 87 | if (videoMask->pixels[idx] > 128) 88 | { 89 | if (mask->pixels[idx] != 0 && (mask->pixels[idx] - 1) == objectId) 90 | histogram->AddPoint(1, 0, pixel.x, pixel.y, pixel.z, i, j); 91 | else 92 | histogram->AddPoint(0, 1, pixel.x, pixel.y, pixel.z, i, j); 93 | } 94 | } 95 | } 96 | 97 | void HistogramEngine::UpdateVarBinHistogram(Object3D* object, View3D* view, ImageUChar4* originalImage, ImageUChar* mask) 98 | { 99 | ImageUtils::Instance()->Copy(mask, object->imageHistogramMask[view->viewId]); 100 | this->BuildHistogramVarBin(object->histogramVarBin[view->viewId], mask, originalImage, object->objectId); 101 | this->NormaliseHistogramVarBin(object->histogramVarBin[view->viewId]); 102 | object->histogramVarBin[view->viewId]->UpdateGPUFromCPU(); 103 | } 104 | 105 | void HistogramEngine::UpdateVarBinHistogram(Object3D* object, View3D* view, ImageUChar4* originalImage, ImageUChar* mask, ImageUChar* videoMask) 106 | { 107 | ImageUtils::Instance()->Copy(mask, object->imageHistogramMask[view->viewId]); 108 | this->BuildHistogramVarBin(object->histogramVarBin[view->viewId], mask, videoMask, originalImage, object->objectId); 109 | this->NormaliseHistogramVarBin(object->histogramVarBin[view->viewId]); 110 | object->histogramVarBin[view->viewId]->UpdateGPUFromCPU(); 111 | } 112 | 113 | void HistogramEngine::UpdateVarBinHistogram(Object3D* object, View3D* view, ImageUChar4* originalImage, Pose3D* pose) 114 | { 115 | DrawingEngine::Instance()->Draw(object, view, pose, object->imageHistogramMask[view->viewId], DrawingEngine::RENDERING_FILL); 116 | this->BuildHistogramVarBin(object->histogramVarBin[view->viewId], object->imageHistogramMask[view->viewId], originalImage, object->objectId); 117 | this->NormaliseHistogramVarBin(object->histogramVarBin[view->viewId]); 118 | object->histogramVarBin[view->viewId]->UpdateGPUFromCPU(); 119 | } 120 | 121 | void HistogramEngine::SetVarBinHistogram(Object3D* object, View3D* view, float2 *normalised) 122 | { 123 | memcpy(object->histogramVarBin[view->viewId]->normalised, normalised, sizeof(float2) * object->histogramVarBin[view->viewId]->fullHistSize); 124 | object->histogramVarBin[view->viewId]->UpdateGPUFromCPU(); 125 | } 126 | 127 | void HistogramEngine::MergeHistograms(Object3D** objects, int objectCount, View3D** views, int viewCount, float mergeAlphaForeground, float mergeAlphaBackground) 128 | { 129 | int viewIdx, objectIdx; 130 | 131 | for (viewIdx = 0; viewIdx < viewCount; viewIdx++) 132 | { 133 | for (objectIdx = 0; objectIdx < objectCount; objectIdx++) 134 | { 135 | objects[objectIdx]->histogramVarBin[views[viewIdx]->viewId]->mergeAlphaForeground = mergeAlphaForeground; 136 | objects[objectIdx]->histogramVarBin[views[viewIdx]->viewId]->mergeAlphaBackground = mergeAlphaBackground; 137 | objects[objectIdx]->histogramVarBin[views[viewIdx]->viewId]->ClearNotNormalisedPartial(); 138 | 139 | DrawingEngine::Instance()->Draw(objects[objectIdx], views[viewIdx], objects[objectIdx]->initialPose[views[viewIdx]->viewId], 140 | views[viewIdx]->imageHistogramMaskAll, (objectIdx == 0)); 141 | } 142 | 143 | for (objectIdx = 0; objectIdx < objectCount; objectIdx++) 144 | { this->UpdateVarBinHistogram(objects[objectIdx], views[viewIdx], views[viewIdx]->imageRegistered, views[viewIdx]->imageHistogramMaskAll->imageObjects); } 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /PerseusLib/Utils/HistogramEngine.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | using namespace PerseusLib::Primitives; 13 | using namespace PerseusLib::Objects; 14 | using namespace PerseusLib::Utils; 15 | 16 | using namespace Renderer::Engine; 17 | 18 | namespace PerseusLib 19 | { 20 | namespace Utils 21 | { 22 | class HistogramEngine 23 | { 24 | private: 25 | static HistogramEngine* instance; 26 | 27 | void NormaliseHistogramVarBin(HistogramVarBin *histogram); 28 | 29 | void BuildHistogramVarBin(HistogramVarBin *histogram, ImageUChar *mask, ImageUChar4 *image, int objectId); 30 | void BuildHistogramVarBin(HistogramVarBin *histogram, ImageUChar *mask, ImageUChar *videoMask, ImageUChar4* image, int objectId); 31 | public: 32 | static HistogramEngine* Instance(void) { 33 | if (instance == NULL) instance = new HistogramEngine(); 34 | return instance; 35 | } 36 | 37 | void UpdateVarBinHistogram(Object3D* object, View3D* view, ImageUChar4* originalImage, ImageUChar* mask); 38 | void UpdateVarBinHistogram(Object3D* object, View3D* view, ImageUChar4* originalImage, ImageUChar* mask, ImageUChar* videoMask); 39 | void UpdateVarBinHistogram(Object3D* object, View3D* view, ImageUChar4* originalImage, Pose3D* pose); 40 | 41 | void SetVarBinHistogram(Object3D* object, View3D* view, float2 *normalised); 42 | 43 | void MergeHistograms(Object3D** objects, int objectCount, View3D** views, int viewCount, float mergeAlphaForeground, float mergeAlphaBackground); 44 | 45 | HistogramEngine(void); 46 | ~HistogramEngine(void); 47 | }; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /PerseusLib/Utils/ImageUtils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | using namespace PerseusLib::Utils; 7 | 8 | ImageUtils* ImageUtils::instance; 9 | 10 | ImageUtils::ImageUtils(void) 11 | { 12 | } 13 | 14 | ImageUtils::~ImageUtils(void) 15 | { 16 | } 17 | 18 | void ImageUtils::SaveImageToFile(ImageUChar4* image, char* fileName) 19 | { 20 | ImageUChar4* newImage = new ImageUChar4(image->width, image->height, false); 21 | this->Copy(image, newImage); 22 | 23 | FIBITMAP *bmpO = FreeImage_ConvertFromRawBits((unsigned char*)newImage->pixels, newImage->width, newImage->height, newImage->width * 4, 32, 0, 0, 0, false); 24 | 25 | FreeImage_FlipVertical(bmpO); 26 | 27 | FIBITMAP *bmp = FreeImage_ConvertTo24Bits(bmpO); 28 | 29 | FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; 30 | fif = FreeImage_GetFileType(fileName, 0); 31 | if(fif == FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(fileName); } 32 | if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) { FreeImage_Save(fif, bmp, fileName, 0); } 33 | 34 | FreeImage_Unload(bmp); 35 | FreeImage_Unload(bmpO); 36 | delete newImage; 37 | } 38 | 39 | void ImageUtils::FlipColours(ImageUChar4 *image) 40 | { 41 | int i; 42 | 43 | PixelUCHAR4 pixel; 44 | 45 | for (i=0; iwidth * image->height; i++) 46 | { 47 | pixel = image->pixels[i]; 48 | image->pixels[i].x = pixel.z; 49 | image->pixels[i].y = pixel.y; 50 | image->pixels[i].z = pixel.x; 51 | } 52 | } 53 | 54 | void ImageUtils::LoadImageFromFile(ImageUChar4* image, char* fileName) 55 | { 56 | bool bLoaded = false; 57 | int bpp; 58 | FIBITMAP *bmp = 0; 59 | FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; 60 | fif = FreeImage_GetFileType(fileName); 61 | 62 | if (fif == FIF_UNKNOWN) { fif = FreeImage_GetFIFFromFilename(fileName); } 63 | 64 | if (fif != FIF_UNKNOWN && FreeImage_FIFSupportsReading(fif)) 65 | { 66 | bmp = FreeImage_Load(fif, fileName, 0); 67 | bLoaded = true; if (bmp == NULL) bLoaded = false; 68 | } 69 | 70 | if (bLoaded) 71 | { 72 | FreeImage_FlipVertical(bmp); 73 | 74 | bpp = FreeImage_GetBPP(bmp); 75 | switch (bpp) 76 | { 77 | case 32: 78 | break; 79 | default: 80 | FIBITMAP *bmpTemp = FreeImage_ConvertTo32Bits(bmp); 81 | if (bmp != NULL) FreeImage_Unload(bmp); 82 | bmp = bmpTemp; 83 | bpp = FreeImage_GetBPP(bmp); 84 | break; 85 | } 86 | 87 | memcpy(image->pixels, FreeImage_GetBits(bmp), sizeof(unsigned char) * 4 * image->width * image->height); 88 | 89 | FreeImage_Unload(bmp); 90 | } 91 | } 92 | 93 | void ImageUtils::LoadImageFromFile(ImageUChar* image, char* fileName, int fixedValue) 94 | { 95 | ImageUChar4* TempImage = new ImageUChar4(image->width, image->height, false); 96 | this->LoadImageFromFile(TempImage, fileName); 97 | this->Copy(TempImage, image, fixedValue); 98 | delete TempImage; 99 | } 100 | 101 | void ImageUtils::LoadImageFromCVMat(ImageUChar4* image, cv::Mat& rMat8UC4) 102 | { 103 | ImageUChar4* TempImage = new ImageUChar4(image->width, image->height, false); 104 | memcpy(TempImage->pixels, rMat8UC4.data, sizeof(unsigned char) * 4 * image->width * image->height); 105 | this->Copy(TempImage, image); 106 | delete TempImage; 107 | } 108 | 109 | void ImageUtils::LoadImageFromCVMat(ImageUChar* image, cv::Mat& rMat8U) 110 | { 111 | ImageUChar* TempImage = new ImageUChar(image->width, image->height, false); 112 | memcpy(TempImage->pixels, rMat8U.data, sizeof(unsigned char) * image->width * image->height); 113 | this->Copy(TempImage, image); 114 | delete TempImage; 115 | } 116 | 117 | void ImageUtils::Copy(ImageUChar4 *src, ImageUChar4* dst) { memcpy(dst->pixels, src->pixels, src->width * src->height * sizeof(PixelUCHAR4)); } 118 | 119 | void ImageUtils::Copy(ImageUChar *src, ImageUChar4* dst) 120 | { 121 | for (int i=0; iwidth*src->height; i++) 122 | { dst->pixels[i].x = src->pixels[i]; dst->pixels[i].y = src->pixels[i]; dst->pixels[i].z = src->pixels[i]; dst->pixels[i].w = 255; } 123 | } 124 | void ImageUtils::Copy(ImageUChar *src, ImageUChar* dst) { memcpy(dst->pixels, src->pixels, src->width * src->height * sizeof(unsigned char)); } 125 | void ImageUtils::Copy(ImageUChar4 *src, ImageUChar* dst, int fixedValue) 126 | { 127 | if (fixedValue > 0) 128 | { 129 | for (int i=0; iwidth*src->height; i++) 130 | { dst->pixels[i] = (src->pixels[i].x + src->pixels[i].y + src->pixels[i].x) > 0 ? fixedValue : 0; } 131 | } 132 | else 133 | { 134 | for (int i=0; iwidth*src->height; i++) 135 | { dst->pixels[i] = (unsigned char) (0.2989f * float(src->pixels[i].x) + 0.5870f * float(src->pixels[i].y) + 0.1140f * float(src->pixels[i].z)); } 136 | } 137 | } 138 | 139 | 140 | void ImageUtils::Overlay(ImageUChar* srcGrey, ImageUChar4 *destRGB, int destB, int destG, int destR) 141 | { 142 | int idx; 143 | 144 | for (idx = 0; idx < srcGrey->width * srcGrey->height; idx++) 145 | { 146 | if (srcGrey->pixels[idx] > 0) 147 | { 148 | destRGB->pixels[idx].x = (unsigned char)(float(destR) * (float(srcGrey->pixels[idx]) / 255.0f)); 149 | destRGB->pixels[idx].y = (unsigned char)(float(destG) * (float(srcGrey->pixels[idx]) / 255.0f)); 150 | destRGB->pixels[idx].z = (unsigned char)(float(destB) * (float(srcGrey->pixels[idx]) / 255.0f)); 151 | destRGB->pixels[idx].w = 255; 152 | } 153 | } 154 | } 155 | 156 | void ImageUtils::ScaleToGray(ImageFloat *src, ImageUChar4* dest) 157 | { 158 | int idx; 159 | 160 | float *source = src->pixels; 161 | 162 | dest->Clear(); 163 | 164 | float lims[2], out_lims[2], scale; 165 | 166 | lims[0] = 100000.0f; lims[1]= -100000.0f; out_lims[0] = 0.0f; out_lims[1] = 1.0f; 167 | 168 | //for (idx = 0; idx < dest->width * dest->height; idx++) if (source[idx] < 0) source[idx] = 0; 169 | 170 | for (idx = 0; idx < dest->width * dest->height; idx++) 171 | { 172 | if (source[idx] < lims[0]) lims[0] = source[idx]; 173 | if (source[idx] > lims[1]) lims[1] = source[idx]; 174 | } 175 | 176 | scale = (out_lims[1] - out_lims[0]) / (lims[1] - lims[0]); 177 | 178 | if (lims[0] == lims[1] || out_lims[0] == out_lims[1]) 179 | { 180 | for (idx = 0; idx < dest->width * dest->height; idx++) source[idx] = 0; 181 | return; 182 | } 183 | 184 | if (lims[0] != 0) for (idx = 0; idx < dest->width * dest->height; idx++) source[idx] = source[idx] - lims[0]; 185 | if (scale != 1.0f) for (idx = 0; idx < dest->width * dest->height; idx++) source[idx] = source[idx] * scale; 186 | if (out_lims[0] != 0) for (idx = 0; idx < dest->width * dest->height; idx++) source[idx] = source[idx] + out_lims[0]; 187 | 188 | for (idx = 0; idx < dest->width * dest->height; idx++) 189 | { 190 | dest->pixels[idx].x = (unsigned char) (source[idx] * 255.0f); 191 | dest->pixels[idx].y = (unsigned char) (source[idx] * 255.0f); 192 | dest->pixels[idx].z = (unsigned char) (source[idx] * 255.0f); 193 | dest->pixels[idx].w = 255; 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /PerseusLib/Utils/ImageUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace PerseusLib::Primitives; 9 | 10 | namespace PerseusLib 11 | { 12 | namespace Utils 13 | { 14 | class ImageUtils 15 | { 16 | private: 17 | static ImageUtils* instance; 18 | public: 19 | static ImageUtils* Instance(void) { 20 | if (instance == NULL) instance = new ImageUtils(); 21 | return instance; 22 | } 23 | 24 | void ScaleToGray(ImageFloat *source, ImageUChar4* dest); 25 | void Overlay(ImageUChar* srcGrey, ImageUChar4 *destRGB, int destR = 255, int destG = 0, int destB = 0); 26 | 27 | void FlipColours(ImageUChar4 *image); 28 | 29 | void SaveImageToFile(ImageUChar4* image, char* fileName); 30 | 31 | void LoadImageFromFile(ImageUChar4* image, char* fileName); 32 | void LoadImageFromFile(ImageUChar* image, char* fileName, int fixedValue = -1); 33 | 34 | void LoadImageFromCVMat(ImageUChar4* image, cv::Mat& rMat8UC4); 35 | void LoadImageFromCVMat(ImageUChar* image, cv::Mat& rMat8U); 36 | 37 | void Copy(ImageUChar4 *src, ImageUChar4* dst); 38 | void Copy(ImageUChar *src, ImageUChar* dst); 39 | void Copy(ImageUChar *src, ImageUChar4* dst); 40 | void Copy(ImageUChar4 *src, ImageUChar* dst, int fixedValue = -1); 41 | 42 | ImageUtils(void); 43 | ~ImageUtils(void); 44 | }; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /PerseusLib/Utils/MathUtils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define MATH_USE_OPENMP 1 7 | //#define MATH_USE_DIRECTX 2 8 | //#define MATH_USE_VNL 3 9 | 10 | namespace PerseusLib 11 | { 12 | namespace Utils 13 | { 14 | class MathUtils 15 | { 16 | private: 17 | static MathUtils* instance; 18 | public: 19 | 20 | int heavisideSize; 21 | float *heavisideFunction; 22 | 23 | static MathUtils* Instance(void) { 24 | if (instance == NULL) instance = new MathUtils(); 25 | return instance; 26 | } 27 | 28 | void ReadAndAllocateHeaviside(int heavisideSize, char* fileName); 29 | void DeallocateHeaviside(); 30 | 31 | void MatrixVectorProduct(float **matrix, float *vector, float* output, int vectorSize); 32 | 33 | void SquareMatrixProduct(double *c, const double *a, const double *b, int dim); 34 | void SquareMatrixProduct(float *c, const float *a, const float *b, int dim); 35 | void SquareMatrixProduct(long double *c, const long double *a, const long double *b, int dim); 36 | 37 | void MatrixVectorProduct4Inplace(double *matrix, double *vector); 38 | void MatrixVectorProduct4Inplace(float *matrix, float *vector); 39 | void MatrixVectorProduct4Inplace(long double *matrix, long double *vector); 40 | 41 | void InvertMatrix4Pose(float *out, float *in); 42 | void TransposeSquareMatrix(float *in, float *out, int size); 43 | 44 | void MatrixVectorProduct4(double *matrix, double *vector, double *vectorOutput); 45 | void MatrixVectorProduct4(float *matrix, float *vector, float *vectorOutput) { 46 | vectorOutput[0] = matrix[0] * vector[0] + matrix[4] * vector[1] + matrix[8] * vector[2] + matrix[12] * vector[3]; 47 | vectorOutput[1] = matrix[1] * vector[0] + matrix[5] * vector[1] + matrix[9] * vector[2] + matrix[13] * vector[3]; 48 | vectorOutput[2] = matrix[2] * vector[0] + matrix[6] * vector[1] + matrix[10] * vector[2] + matrix[14] * vector[3]; 49 | vectorOutput[3] = matrix[3] * vector[0] + matrix[7] * vector[1] + matrix[11] * vector[2] + matrix[15] * vector[3]; 50 | 51 | float norm = 1.0f/vectorOutput[3]; 52 | for (int i = 0; i < 4; i++) vectorOutput[i] *= norm; 53 | } 54 | void MatrixVectorProduct4(long double *matrix, long double *vector, long double *vectorOutput); 55 | 56 | void MatrixVectorProduct4(float *matrix, double *vector, float *vectorOutput); 57 | void MatrixVectorProduct4(float *matrix, float *vector, double *vectorOutput); 58 | 59 | void InvertMatrix4(float *dst, float *mat); 60 | void InvertMatrix4(double *dst, double *mat); 61 | void InvertMatrix4(long double *dst, long double *mat); 62 | 63 | int GetMinor(float **src, float **dest, int row, int col, int order); 64 | double CalcDeterminant( float **mat, int order); 65 | void InvertMatrix(float **Y, int order, float **A); 66 | 67 | void ConvertArray(double *dst, float* src, int dim); 68 | void ConvertArray(float *dst, double* src, int dim); 69 | 70 | float MatrixDeterminant3(float a[3][3]); 71 | void InvertMatrix3(float in[3][3], float a[3][3]); 72 | 73 | MathUtils(void); 74 | ~MathUtils(void); 75 | }; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /PerseusLib/Utils/VisualisationEngine.cpp: -------------------------------------------------------------------------------- 1 | #include "VisualisationEngine.h" 2 | 3 | using namespace PerseusLib::Utils; 4 | 5 | VisualisationEngine* VisualisationEngine::instance; 6 | 7 | 8 | void VisualisationEngine::Initialise(int width, int height) 9 | { 10 | 11 | } 12 | 13 | void VisualisationEngine::Shutdown() 14 | { 15 | 16 | } 17 | 18 | void VisualisationEngine::GetImage(ImageUChar4* image, GetImageType getImageType, Object3D* object, View3D* view, Pose3D *pose) 19 | { 20 | switch (getImageType) 21 | { 22 | case GETIMAGE_WIREFRAME: 23 | DrawingEngine::Instance()->Draw(object, view, pose, object->imageWireframe[view->viewId], DrawingEngine::RENDERING_WIREFRAME); 24 | ImageUtils::Instance()->Copy(object->imageWireframe[view->viewId], image); 25 | break; 26 | case GETIMAGE_FILL: 27 | ImageUtils::Instance()->Copy(view->imageRegistered, image); 28 | DrawingEngine::Instance()->Draw(object, view, pose, object->imageRender[view->viewId]->imageFill, DrawingEngine::RENDERING_FILL); 29 | ImageUtils::Instance()->Overlay(object->imageRender[view->viewId]->imageFill, image); 30 | break; 31 | case GETIMAGE_ORIGINAL: 32 | ImageUtils::Instance()->Copy(view->imageRegistered, image); 33 | break; 34 | case GETIMAGE_POSTERIORS: 35 | this->computePosteriors(object, view); 36 | ImageUtils::Instance()->ScaleToGray(object->imagePosteriorsPFPB[view->viewId], image); 37 | break; 38 | case GETIMAGE_SIHLUETTE: 39 | ImageUtils::Instance()->Copy(view->imageRegistered, image); 40 | getProcessedDataDTSihluetteLSDXDY(object, view); 41 | ImageUtils::Instance()->Overlay(object->imageSihluette[view->viewId], image); 42 | break; 43 | case GETIMAGE_DT: 44 | ImageUtils::Instance()->Copy(view->imageRegistered, image); 45 | getProcessedDataDTSihluetteLSDXDY(object, view); 46 | ImageUtils::Instance()->ScaleToGray(object->dt[view->viewId], image); 47 | break; 48 | case GETIMAGE_OBJECTS: 49 | //DrawingEngine::Instance()->Draw(object, view, pose, object->imageWireframe[view->viewId], DrawingEngine::RENDERING_FILL); 50 | break; 51 | case GETIMAGE_PROXIMITY: 52 | ImageUtils::Instance()->Copy(view->imageRegistered, image); 53 | DrawingEngine::Instance()->Draw(object, view, pose, object->imageWireframe[view->viewId], DrawingEngine::RENDERING_WIREFRAME); 54 | ImageUtils::Instance()->Overlay(object->imageWireframe[view->viewId], image); 55 | break; 56 | } 57 | } 58 | 59 | void VisualisationEngine::computePosteriors(Object3D* object, View3D* view) 60 | { 61 | int i, j, idx; 62 | unsigned char r, b, g; 63 | 64 | float pYB, pYF, pF, pB; 65 | float etaF, etaB; 66 | 67 | etaF = object->histogramVarBin[view->viewId]->etaF; etaB = object->histogramVarBin[view->viewId]->etaB; 68 | 69 | for (j=0, idx=0; jimageRegistered->height; j++) for (i=0; iimageRegistered->width; idx++, i++) 70 | { 71 | r = view->imageRegistered->pixels[idx].x; g = view->imageRegistered->pixels[idx].y; b = view->imageRegistered->pixels[idx].z; 72 | 73 | object->histogramVarBin[view->viewId]->GetValue(&pYF, &pYB, r, g, b, i, j); 74 | pYF += 0.0000001f; pYB += 0.0000001f; 75 | pF = pYF / (etaF * pYF + etaB * pYB); pB = pYB / (etaF * pYF + etaB * pYB); 76 | object->imagePosteriorsPFPB[view->viewId]->pixels[idx] = pF - pB; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /PerseusLib/Utils/VisualisationEngine.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace PerseusLib::Primitives; 10 | using namespace PerseusLib::Objects; 11 | using namespace PerseusLib::Utils; 12 | using namespace Renderer::Engine; 13 | 14 | namespace PerseusLib 15 | { 16 | namespace Utils 17 | { 18 | class VisualisationEngine 19 | { 20 | private: 21 | static VisualisationEngine* instance; 22 | 23 | ImageUChar *bufferGrey; 24 | 25 | void computePosteriors(Object3D* object, View3D* view); 26 | 27 | public: 28 | static VisualisationEngine* Instance(void) { 29 | if (instance == NULL) instance = new VisualisationEngine(); 30 | return instance; 31 | } 32 | 33 | void Initialise(int width, int height); 34 | void Shutdown(); 35 | 36 | void GetImage(ImageUChar4* image, GetImageType getImageType, Object3D* object, View3D* view, Pose3D *pose); 37 | 38 | VisualisationEngine(void) { } 39 | ~VisualisationEngine(void) { } 40 | }; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /PerseusLib/config.h.in: -------------------------------------------------------------------------------- 1 | #ifndef DDTR_CONFIG_H 2 | #define DDTR_CONFIG_H 3 | 4 | /// Platform 5 | #cmakedefine _UNIX_ 6 | #cmakedefine _WIN_ 7 | #cmakedefine _OSX_ 8 | #cmakedefine _LINUX_ 9 | #cmakedefine _ANDROID_ 10 | 11 | /// Configured libraries 12 | #cmakedefine HAVE_PANGOLIN 13 | #cmakedefine HAVE_SCENEGRAPH 14 | #cmakedefine HAVE_KANGAROO 15 | #cmakedefine HAVE_TINYXML2 16 | #cmakedefine HAVE_OPENCV 17 | #cmakedefine HAVE_CVARS 18 | #cmakedefine HAVE_SOPHUS 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PWP3D 2 | ===== 3 | 4 | This is a cmake version of the PWP3D. The original VS version is availabled at: 5 | 6 | http://www.robots.ox.ac.uk/~victor/code.html 7 | 8 | For more detail of the paper, see: 9 | 10 | See original paper from: 11 | PWP3D: Real-time Segmentation and Tracking of 3D Objects 12 | Victor Adrian Prisacariu, Ian Reid 13 | --------------------------------------------------------------------------------