aboutsummaryrefslogtreecommitdiff
path: root/src/epnp
diff options
context:
space:
mode:
authorJustin Berger <j.david.berger@gmail.com>2018-03-15 16:26:11 -0600
committerJustin Berger <j.david.berger@gmail.com>2018-03-15 16:26:11 -0600
commita3faba1727a741fa43d4ad2a7b43cf93dcc1cadf (patch)
tree4913696519efa49c1bccc68a9df685d403507361 /src/epnp
parent49bade6bbcb351900f627f066b14a6826f6d7984 (diff)
downloadlibsurvive-a3faba1727a741fa43d4ad2a7b43cf93dcc1cadf.tar.gz
libsurvive-a3faba1727a741fa43d4ad2a7b43cf93dcc1cadf.tar.bz2
Cleaned up memory leaks
Diffstat (limited to 'src/epnp')
-rw-r--r--src/epnp/epnp.h1
-rw-r--r--src/epnp/opencv_shim.c53
2 files changed, 17 insertions, 37 deletions
diff --git a/src/epnp/epnp.h b/src/epnp/epnp.h
index 9ca3a2e..0a42b04 100644
--- a/src/epnp/epnp.h
+++ b/src/epnp/epnp.h
@@ -46,6 +46,7 @@ typedef struct {
double cws_determinant;
} epnp;
+void epnp_dtor(epnp *self);
void epnp_set_internal_parameters(epnp *self, double uc, double vc, double fu, double fv);
void epnp_set_maximum_number_of_correspondences(epnp *self, int n);
void epnp_reset_correspondences(epnp *self);
diff --git a/src/epnp/opencv_shim.c b/src/epnp/opencv_shim.c
index d7886d0..df2765b 100644
--- a/src/epnp/opencv_shim.c
+++ b/src/epnp/opencv_shim.c
@@ -28,29 +28,7 @@ const int CV_64F = 0;
typedef double doublereal;
#define F77_FUNC(func) func##_
-/*
-extern int F77_FUNC(dgetrs)(char *trans, int *n, int *nrhs, double *a, int *lda, int *ipiv, double *b, int *ldb, int
-*info);
-
-extern int F77_FUNC(dgetri)(int *n, double *a, int *lda, int *ipiv, double *work, int *lwork, int *info);
-extern int F77_FUNC(dgetrf)(int *m, int *n, double *a, int *lda, int *ipiv, int *info); /* blocked LU
-
-extern int F77_FUNC(dgesvd)(char *jobu, char *jobvt,
- int *m, int *n,
- double *a, int *lda, double *s, double *u, int *ldu,
- double *vt, int *ldvt, double *work, int *lwork,
- int *info);
-
-extern int F77_FUNC(dgesdd)(char *jobz,
- int *m, int *n, double *a, int *lda,
- double *s, double *u, int *ldu, double *vt, int *ldvt,
- double *work, int *lwork, int *iwork, int *info);
-
-extern int dgemm_(char *transa, char *transb, lapack_lapack_int *m, lapack_lapack_int *
- n, lapack_lapack_int *k, double *alpha, double *a, lapack_lapack_int *lda,
- double *b, lapack_lapack_int *ldb, double *beta, double *c, lapack_lapack_int
- *ldc);
-*/
+
void cvGEMM(const CvMat *src1, const CvMat *src2, double alpha, const CvMat *src3, double beta, CvMat *dst, int tABC) {
lapack_int rows1 = src1->rows;
lapack_int cols1 = src1->cols;
@@ -85,7 +63,7 @@ void cvMulTransposed(const CvMat *src, CvMat *dst, int order, const CvMat *delta
lapack_int drows = dst->rows;
assert(drows == cols);
assert(order == 1 ? (dst->cols == src->cols) : (dst->cols == src->rows));
- assert(delta == 0); // THIS ISN'T IMPLEMENTED YET
+ assert(delta == 0 && "This isn't implemented yet");
double beta = 0;
bool isAT = order == 1;
@@ -93,16 +71,12 @@ void cvMulTransposed(const CvMat *src, CvMat *dst, int order, const CvMat *delta
lapack_int dstCols = dst->cols;
- cblas_dgemm(CblasRowMajor, isAT ? CblasTrans : CblasNoTrans, isBT ? CblasTrans : CblasNoTrans,
- cols, // isAT ? cols : rows,
- dstCols,
- rows, // isAT ? rows : cols,
+ cblas_dgemm(CblasRowMajor, isAT ? CblasTrans : CblasNoTrans, isBT ? CblasTrans : CblasNoTrans, cols, dstCols, rows,
scale,
src->data.db, cols, src->data.db, cols, beta,
dst->data.db, dstCols);
- // const CvMat* delta, double scale
}
void *cvAlloc(size_t size) { return malloc(size); }
@@ -242,14 +216,15 @@ double cvInvert(const CvMat *srcarr, CvMat *dstarr, int method) {
free(ipiv);
} else if (method == DECOMP_SVD) {
-
+ // TODO: There is no way this needs this many allocations,
+ // but in my defense I was very tired when I wrote this code
CvMat *w = cvCreateMat(1, MIN(dstarr->rows, dstarr->cols), dstarr->type);
CvMat *u = cvCreateMat(dstarr->cols, dstarr->cols, dstarr->type);
CvMat *v = cvCreateMat(dstarr->rows, dstarr->rows, dstarr->type);
- cvSVD(dstarr, w, u, v, 0);
-
CvMat *um = cvCreateMat(w->cols, w->cols, w->type);
+ cvSVD(dstarr, w, u, v, 0);
+
cvSetZero(um);
for (int i = 0; i < w->cols; i++) {
cvmSet(um, i, i, 1. / w->data.db[i]);
@@ -258,6 +233,12 @@ double cvInvert(const CvMat *srcarr, CvMat *dstarr, int method) {
CvMat *tmp = cvCreateMat(dstarr->cols, dstarr->rows, dstarr->type);
cvGEMM(v, um, 1, 0, 0, tmp, GEMM_1_T);
cvGEMM(tmp, u, 1, 0, 0, dstarr, GEMM_2_T);
+
+ cvReleaseMat(&tmp);
+ cvReleaseMat(&w);
+ cvReleaseMat(&u);
+ cvReleaseMat(&v);
+ cvReleaseMat(&um);
}
return 0;
}
@@ -333,11 +314,9 @@ int cvSolve(const CvMat *Aarr, const CvMat *xarr, CvMat *Barr, int method) {
assert(Barr->cols == xCpy->cols);
xCpy->rows = acols;
cvCopyTo(xCpy, Barr);
-/*
-Barr->data = xCpy->data;
-Barr->rows = acols;
-Barr->cols = xCpy->cols;
-*/
+
+ cvReleaseMat(&aCpy);
+ cvReleaseMat(&xCpy);
#ifdef DEBUG_PRINT
print_mat(Barr);
#endif