1 ////////////////////////////////////////////////////////////////////////////
2 //  File:           pba.cpp
3 //  Author:         Changchang Wu
4 //  Description :   implementation of ParallelBA, which is a wrapper around
5 //                  the GPU-based and CPU-based implementations
6 //
7 //  Copyright (c) 2011  Changchang Wu (ccwu@cs.washington.edu)
8 //    and the University of Washington at Seattle
9 //
10 //  This library is free software; you can redistribute it and/or
11 //  modify it under the terms of the GNU General Public
12 //  License as published by the Free Software Foundation; either
13 //  Version 3 of the License, or (at your option) any later version.
14 //
15 //  This library is distributed in the hope that it will be useful,
16 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
17 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 //  General Public License for more details.
19 //
20 ////////////////////////////////////////////////////////////////////////////////
21 #include <stdlib.h>
22 #include <new>
23 #include "pba.h"
24 #include "SparseBundleCU.h"
25 #include "SparseBundleCPU.h"
26 
27 namespace pba {
28 
ParallelBA(DeviceT device,const int num_threads)29 ParallelBA::ParallelBA(DeviceT device, const int num_threads) {
30   // The wrapper intends to provide different implementations.
31 
32   if (device >= PBA_CUDA_DEVICE_DEFAULT)
33 #ifndef PBA_NO_GPU
34   {
35     SparseBundleCU* cuba = new SparseBundleCU(device - PBA_CUDA_DEVICE0);
36     if (cuba->GetMemCapacity() > 0) {
37       _optimizer = cuba;
38     } else {
39       device = PBA_CPU_FLOAT;
40       _optimizer = NewSparseBundleCPU(false, num_threads);
41       delete cuba;
42     }
43   } else
44 #else
45     device = PBA_CPU_FLOAT;
46 #endif
47       if (device == PBA_CPU_FLOAT)
48     _optimizer = NewSparseBundleCPU(false, num_threads);
49   else if (device == PBA_CPU_DOUBLE)
50     _optimizer = NewSparseBundleCPU(true, num_threads);
51   else
52     _optimizer = NULL;
53 }
54 
~ParallelBA()55 ParallelBA::~ParallelBA() {
56   if (_optimizer) delete _optimizer;
57 }
58 
ParseParam(int narg,char ** argv)59 void ParallelBA::ParseParam(int narg, char** argv) {
60   _optimizer->ParseParam(narg, argv);
61 }
62 
GetInternalConfig()63 ConfigBA* ParallelBA::GetInternalConfig() {
64   if (_optimizer)
65     return _optimizer->GetInternalConfig();
66   else
67     return NULL;
68 }
69 
SetFixedIntrinsics(bool fixed)70 void ParallelBA::SetFixedIntrinsics(bool fixed) {
71   _optimizer->SetFixedIntrinsics(fixed);
72 }
EnableRadialDistortion(DistortionT enabled)73 void ParallelBA::EnableRadialDistortion(DistortionT enabled) {
74   _optimizer->EnableRadialDistortion(enabled);
75 }
SetNextTimeBudget(int seconds)76 void ParallelBA::SetNextTimeBudget(int seconds) {
77   _optimizer->SetNextTimeBudget(seconds);
78 }
79 
SetNextBundleMode(BundleModeT mode)80 void ParallelBA::SetNextBundleMode(BundleModeT mode) {
81   _optimizer->SetNextBundleMode(mode);
82 }
83 
SetCameraData(size_t ncam,CameraT * cams)84 void ParallelBA::SetCameraData(size_t ncam, CameraT* cams) {
85   _optimizer->SetCameraData(ncam, cams);
86 }
87 
SetPointData(size_t npoint,Point3D * pts)88 void ParallelBA::SetPointData(size_t npoint, Point3D* pts) {
89   _optimizer->SetPointData(npoint, pts);
90 }
91 
SetProjection(size_t nproj,const Point2D * imgpts,const int * point_idx,const int * cam_idx)92 void ParallelBA::SetProjection(size_t nproj, const Point2D* imgpts,
93                                const int* point_idx, const int* cam_idx) {
94   _optimizer->SetProjection(nproj, imgpts, point_idx, cam_idx);
95 }
RunBundleAdjustment()96 int ParallelBA::RunBundleAdjustment() {
97   return _optimizer->RunBundleAdjustment();
98 }
99 
GetMeanSquaredError()100 float ParallelBA::GetMeanSquaredError() {
101   return _optimizer->GetMeanSquaredError();
102 }
103 
GetCurrentIteration()104 int ParallelBA::GetCurrentIteration() {
105   return _optimizer->GetCurrentIteration();
106 }
AbortBundleAdjustment()107 void ParallelBA::AbortBundleAdjustment() {
108   return _optimizer->AbortBundleAdjustment();
109 }
110 
ReserveStorage(size_t ncam,size_t npt,size_t nproj)111 void ParallelBA::ReserveStorage(size_t ncam, size_t npt, size_t nproj) {
112   if (_optimizer) _optimizer->ReserveStorage(ncam, npt, nproj);
113 }
114 
SetFocalMask(const int * fmask,float weight)115 void ParallelBA::SetFocalMask(const int* fmask, float weight) {
116   if (_optimizer && weight > 0) _optimizer->SetFocalMask(fmask, weight);
117 }
118 
operator new(size_t size)119 void* ParallelBA::operator new(size_t size) {
120   void* p = malloc(size);
121   if (p == 0) {
122     const std::bad_alloc ba;
123     throw ba;
124   }
125   return p;
126 }
127 
NewParallelBA(ParallelBA::DeviceT device)128 ParallelBA* NewParallelBA(ParallelBA::DeviceT device) {
129   return new ParallelBA(device);
130 }
131 
ParallelBA_GetVersion()132 int ParallelBA_GetVersion() { return 105; }
133 
134 }  // namespace pba
135