1 ////////////////////////////////////////////////////////////////////////////
2 //  File:       pba.h
3 //  Author:       Changchang Wu (ccwu@cs.washington.edu)
4 //  Description :   interface of class ParallelBA, which has two
5 //implementations
6 //                  SparseBundleCU for CUDA-based version, and
7 //                  SparseBundleCPU<Float> for CPU multi-threading version
8 //
9 //  Copyright (c) 2011  Changchang Wu (ccwu@cs.washington.edu)
10 //    and the University of Washington at Seattle
11 //
12 //  This library is free software; you can redistribute it and/or
13 //  modify it under the terms of the GNU General Public
14 //  License as published by the Free Software Foundation; either
15 //  Version 3 of the License, or (at your option) any later version.
16 //
17 //  This library is distributed in the hope that it will be useful,
18 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 //  General Public License for more details.
21 //
22 ////////////////////////////////////////////////////////////////////////////////
23 
24 #ifndef PARALLEL_BA_H
25 #define PARALLEL_BA_H
26 
27 #if defined(_WIN32)
28 #ifdef PBA_DLL
29 #ifdef DLL_EXPORT
30 #define PBA_EXPORT __declspec(dllexport)
31 #else
32 #define PBA_EXPORT __declspec(dllimport)
33 #endif
34 #else
35 #define PBA_EXPORT
36 #endif
37 
38 #define PBA_EXPORT_EXTERN PBA_EXPORT
39 
40 #if _MSC_VER > 1000
41 #pragma once
42 #endif
43 #else
44 #define PBA_EXPORT
45 #define PBA_EXPORT_EXTERN extern "C"
46 #endif
47 
48 // filetype definitions for points and camera
49 #include "DataInterface.h"
50 #include "ConfigBA.h"
51 
52 namespace pba {
53 
54 class ParallelBA {
55  public:
56   enum StatusT {
57     STATUS_SUCCESS = 0,
58     STATUS_CAMERA_MISSING = 1,
59     STATUS_POINT_MISSING,
60     STATUS_PROJECTION_MISSING,
61     STATUS_MEASURMENT_MISSING,
62     STATUS_ALLOCATION_FAIL
63   };
64   enum DeviceT {
65     PBA_INVALID_DEVICE = -4,
66     PBA_CPU_DOUBLE = -3,
67     PBA_CPU_FLOAT = -2,
68     PBA_CUDA_DEVICE_DEFAULT = -1,
69     PBA_CUDA_DEVICE0 = 0
70   };
71   enum DistortionT {
72     PBA_MEASUREMENT_DISTORTION = -1,  // single parameter, apply to measurements
73     PBA_NO_DISTORTION = 0,  // no radial distortion
74     PBA_PROJECTION_DISTORTION = 1  // single parameter, apply to projectino
75   };
76   enum BundleModeT {
77     BUNDLE_FULL = 0,
78     BUNDLE_ONLY_MOTION = 1,
79     BUNDLE_ONLY_STRUCTURE = 2,
80   };
81 
82  private:
83   ParallelBA* _optimizer;
84 
85  public:
86   ////////////////////////////////////////////////////
87   // methods for changing bundle adjustment settings
88   PBA_EXPORT virtual void ParseParam(int narg, char** argv);  // indirect method
89   PBA_EXPORT virtual ConfigBA* GetInternalConfig();  // direct method
90   PBA_EXPORT virtual void SetFixedIntrinsics(
91       bool fixed);  // call this for calibrated system
92   PBA_EXPORT virtual void EnableRadialDistortion(
93       DistortionT type);  // call this to enable radial distortion
94   PBA_EXPORT virtual void SetNextTimeBudget(
95       int seconds);  //# of seconds for next run (0 = no limit)
96   PBA_EXPORT virtual void ReserveStorage(size_t ncam, size_t npt, size_t nproj);
97 
98  public:
99   // function name change; the old one is mapped as inline function
SetFocalLengthFixed(bool fixed)100   inline void SetFocalLengthFixed(bool fixed) { SetFixedIntrinsics(fixed); }
ResetBundleStorage()101   inline void ResetBundleStorage() {
102     ReserveStorage(0, 0, 0); /*Reset devide for CUDA*/
103   }
104 
105  public:
106   /////////////////////////////////////////////////////
107   // optimizer interface, input and run
108   PBA_EXPORT virtual void SetCameraData(size_t ncam,
109                                         CameraT* cams);  // set camera data
110   PBA_EXPORT virtual void SetPointData(size_t npoint,
111                                        Point3D* pts);  // set 3D point data
112   PBA_EXPORT virtual void SetProjection(size_t nproj, const Point2D* imgpts,
113                                         const int* point_idx,
114                                         const int* cam_idx);  // set projections
115   PBA_EXPORT virtual void SetNextBundleMode(
116       BundleModeT
117           mode = BUNDLE_FULL);  // mode of the next bundle adjustment call
118   PBA_EXPORT virtual int RunBundleAdjustment();  // start bundle adjustment,
119                                                  // return number of successful
120                                                  // LM iterations
121  public:
122   //////////////////////////////////////////////////
123   // Query optimzer runing status for Multi-threading
124   // Three functions below can be called from a differnt thread while bundle is
125   // running
126   PBA_EXPORT virtual float
127   GetMeanSquaredError();  // read back results during/after BA
128   PBA_EXPORT virtual void
129   AbortBundleAdjustment();  // tell bundle adjustment to abort ASAP
130   PBA_EXPORT virtual int
131   GetCurrentIteration();  // which iteration is it working on?
132  public:
133   PBA_EXPORT ParallelBA(DeviceT device = PBA_CUDA_DEVICE_DEFAULT,
134                         const int num_threads = -1);
135   PBA_EXPORT void* operator new(size_t size);
136   PBA_EXPORT virtual ~ParallelBA();
137 
138  public:
139   //////////////////////////////////////////////
140   // Future functions will be added to the end for compatiability with old
141   // version.
142   PBA_EXPORT virtual void SetFocalMask(const int* fmask, float weight = 1.0f);
143 };
144 
145 // function for dynamic loading of library
146 PBA_EXPORT_EXTERN ParallelBA* NewParallelBA(
147     ParallelBA::DeviceT device = ParallelBA::PBA_CUDA_DEVICE_DEFAULT);
148 typedef ParallelBA* (*NEWPARALLELBAPROC)(ParallelBA::DeviceT);
149 
150 ///////////////////////////////////////////////
151 // older versions do not have this function.
152 PBA_EXPORT_EXTERN int ParallelBA_GetVersion();
153 
154 }  // namespace pba
155 
156 #endif
157