forked from yujiali/pygco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgco.cpp
185 lines (160 loc) · 4.77 KB
/
cgco.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Many error checks are not performed because it seems hard to pass these
* error message back to python.
*
* So be careful when using this wrapper, and make sure the parameters in
* function calls are valid.
*
* This wrapper now only supports general graph. The grid graph is not
* implemented yet.
*
* The return value of all functions are integers, which can be potentially
* used for error signals. If it is required to return something, the address
* of the required return value is passed as an argument to the function and
* it will be filled with the expected return value after the call.
*
* Yujia Li, 08/2013
*
*/
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include "gco_source/GCoptimization.h"
typedef GCoptimization::LabelID LabelID;
typedef GCoptimization::SiteID SiteID;
typedef GCoptimization::EnergyType EnergyType;
typedef GCoptimization::EnergyTermType EnergyTermType;
typedef std::map<int, GCoptimization*> GcoInstanceMap;
static GcoInstanceMap _gcoInstanceMap;
static int _gcoNextInstanceId = 12345;
GcoInstanceMap::mapped_type& findInstance(int handle)
{
GcoInstanceMap::iterator it = _gcoInstanceMap.find(handle);
if (it != _gcoInstanceMap.end())
return it->second;
else
{
fprintf(stderr, "Invalid instance handle %d\n", handle);
exit(EXIT_FAILURE);
}
}
void removeInstance(int handle)
{
delete findInstance(handle);
_gcoInstanceMap.erase(handle);
}
extern "C" int gcoCreateGeneralGraph(SiteID numSites, LabelID numLabels, int *handle)
{
GCoptimization *gco = new GCoptimizationGeneralGraph(numSites, numLabels);
_gcoInstanceMap[_gcoNextInstanceId] = gco;
*handle = _gcoNextInstanceId;
_gcoNextInstanceId++;
return 0;
}
extern "C" int gcoDestroyGraph(int handle)
{
removeInstance(handle);
return 0;
}
extern "C" int gcoSetDataCost(int handle, EnergyTermType *unary)
{
GCoptimization *gco = findInstance(handle);
gco->setDataCost(unary);
return 0;
}
extern "C" int gcoSetSiteDataCost(int handle, SiteID site, LabelID label, EnergyTermType e)
{
GCoptimization *gco = findInstance(handle);
gco->setDataCost(site, label, e);
return 0;
}
extern "C" int gcoSetNeighborPair(int handle, SiteID s1, SiteID s2, EnergyTermType e)
{
GCoptimizationGeneralGraph *gco = (GCoptimizationGeneralGraph*)findInstance(handle);
if (s1 < s2)
gco->setNeighbors(s1, s2, e);
return 0;
}
extern "C" int gcoSetAllNeighbors(int handle, SiteID *s1, SiteID *s2, EnergyTermType *e, int nPairs)
{
GCoptimizationGeneralGraph *gco = (GCoptimizationGeneralGraph*)findInstance(handle);
for (int i = 0; i < nPairs; i++)
if (s1[i] < s2[i])
gco->setNeighbors(s1[i], s2[i], e[i]);
return 0;
}
extern "C" int gcoSetSmoothCost(int handle, EnergyTermType *e)
{
GCoptimization *gco = findInstance(handle);
gco->setSmoothCost(e);
return 0;
}
extern "C" int gcoSetPairSmoothCost(int handle, LabelID l1, LabelID l2, EnergyTermType e)
{
GCoptimization *gco = findInstance(handle);
gco->setSmoothCost(l1, l2, e);
return 0;
}
extern "C" int gcoExpansion(int handle, int maxNumIters, EnergyType *e)
{
GCoptimization *gco = findInstance(handle);
*e = gco->expansion(maxNumIters);
return 0;
}
extern "C" int gcoExpansionOnAlpha(int handle, LabelID label, int *success)
{
GCoptimization *gco = findInstance(handle);
if (gco->alpha_expansion(label))
*success = 1;
else
*success = 0;
return 0;
}
extern "C" int gcoSwap(int handle, int maxNumIters, EnergyType *e)
{
GCoptimization *gco = findInstance(handle);
*e = gco->swap(maxNumIters);
return 0;
}
extern "C" int gcoAlphaBetaSwap(int handle, LabelID l1, LabelID l2)
{
GCoptimization *gco = findInstance(handle);
gco->alpha_beta_swap(l1, l2);
return 0;
}
extern "C" int gcoComputeEnergy(int handle, EnergyType *e)
{
GCoptimization *gco = findInstance(handle);
*e = gco->compute_energy();
return 0;
}
extern "C" int gcoComputeDataEnergy(int handle, EnergyType *e)
{
GCoptimization *gco = findInstance(handle);
*e = gco->giveDataEnergy();
return 0;
}
extern "C" int gcoComputeSmoothEnergy(int handle, EnergyType *e)
{
GCoptimization *gco = findInstance(handle);
*e = gco->giveSmoothEnergy();
return 0;
}
extern "C" int gcoGetLabelAtSite(int handle, SiteID site, LabelID *label)
{
GCoptimization *gco = findInstance(handle);
*label = gco->whatLabel(site);
return 0;
}
extern "C" int gcoGetLabels(int handle, LabelID *labels)
{
GCoptimization *gco = findInstance(handle);
gco->whatLabel(0, gco->numSites(), labels);
return 0;
}
extern "C" int gcoInitLabelAtSite(int handle, SiteID site, LabelID label)
{
GCoptimization *gco = findInstance(handle);
gco->setLabel(site, label);
return 0;
}