[Bf-blender-cvs] [47ce2d7] master: OpenNL: significantly simplify code using Eigen / STL.
Brecht Van Lommel
noreply at git.blender.org
Sun Nov 22 22:54:40 CET 2015
Commit: 47ce2d7bef32a7f5de34ac3e0cfb8300a4e63cd9
Author: Brecht Van Lommel
Date: Sun Nov 22 05:25:32 2015 +0100
Branches: master
https://developer.blender.org/rB47ce2d7bef32a7f5de34ac3e0cfb8300a4e63cd9
OpenNL: significantly simplify code using Eigen / STL.
===================================================================
M intern/opennl/extern/ONL_opennl.h
M intern/opennl/intern/opennl.cpp
M source/blender/bmesh/operators/bmo_smooth_laplacian.c
M source/blender/editors/armature/meshlaplacian.c
M source/blender/editors/armature/reeb.c
M source/blender/editors/uvedit/uvedit_parametrizer.c
M source/blender/modifiers/intern/MOD_laplaciandeform.c
M source/blender/modifiers/intern/MOD_laplaciansmooth.c
===================================================================
diff --git a/intern/opennl/extern/ONL_opennl.h b/intern/opennl/extern/ONL_opennl.h
index d550a63..61a11fa 100644
--- a/intern/opennl/extern/ONL_opennl.h
+++ b/intern/opennl/extern/ONL_opennl.h
@@ -37,11 +37,6 @@
* the Software into proprietary programs.
*/
-/*
-#define NL_DEBUG
-#define NL_PARANOID
-*/
-
#ifndef nlOPENNL_H
#define nlOPENNL_H
@@ -49,8 +44,6 @@
extern "C" {
#endif
-#define NL_VERSION_0_0 1
-
/* Datatypes */
typedef unsigned int NLenum;
@@ -59,7 +52,7 @@ typedef int NLint; /* 4-byte signed */
typedef unsigned int NLuint; /* 4-byte unsigned */
typedef double NLdouble; /* double precision float */
-typedef void* NLContext;
+typedef struct NLContext NLContext;
/* Constants */
@@ -76,17 +69,16 @@ typedef void* NLContext;
#define NL_SOLVER 0x100
#define NL_NB_VARIABLES 0x101
#define NL_LEAST_SQUARES 0x102
-#define NL_SYMMETRIC 0x106
#define NL_ERROR 0x108
#define NL_NB_ROWS 0x110
#define NL_NB_RIGHT_HAND_SIDES 0x112 /* 4 max */
/* Contexts */
-NLContext nlNewContext(void);
-void nlDeleteContext(NLContext context);
-void nlMakeCurrent(NLContext context);
-NLContext nlGetCurrent(void);
+NLContext *nlNewContext(void);
+void nlDeleteContext(NLContext *context);
+void nlMakeCurrent(NLContext *context);
+NLContext *nlGetCurrent(void);
/* State get/set */
@@ -113,8 +105,7 @@ void nlRightHandSideSet(NLuint rhsindex, NLuint index, NLdouble value);
/* Solve */
void nlPrintMatrix(void);
-NLboolean nlSolve(void);
-NLboolean nlSolveAdvanced(NLint *permutation, NLboolean solveAgain);
+NLboolean nlSolve(NLboolean solveAgain);
#ifdef __cplusplus
}
diff --git a/intern/opennl/intern/opennl.cpp b/intern/opennl/intern/opennl.cpp
index 085375b..de12805 100644
--- a/intern/opennl/intern/opennl.cpp
+++ b/intern/opennl/intern/opennl.cpp
@@ -40,688 +40,232 @@
#include "ONL_opennl.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
-
-#ifdef NL_PARANOID
-#ifndef NL_DEBUG
-#define NL_DEBUG
-#endif
-#endif
-
#include <Eigen/Sparse>
+
+#include <cassert>
+#include <cstdlib>
#include <iostream>
+#include <vector>
+
+/* Eigen data structures */
typedef Eigen::SparseMatrix<double, Eigen::ColMajor> EigenSparseMatrix;
typedef Eigen::SparseLU<EigenSparseMatrix> EigenSparseSolver;
+typedef Eigen::VectorXd EigenVectorX;
+typedef Eigen::Triplet<double> EigenTriplet;
-/************************************************************************************/
-/* Assertions */
-
-
-static void __nl_assertion_failed(const char* cond, const char* file, int line) {
- fprintf(
- stderr,
- "OpenNL assertion failed: %s, file:%s, line:%d\n",
- cond,file,line
- );
- abort();
-}
-
-static void __nl_range_assertion_failed(
- double x, double min_val, double max_val, const char* file, int line
-) {
- fprintf(
- stderr,
- "OpenNL range assertion failed: %f in [ %f ... %f ], file:%s, line:%d\n",
- x, min_val, max_val, file,line
- );
- abort();
-}
-
-static void __nl_should_not_have_reached(const char* file, int line) {
- fprintf(
- stderr,
- "OpenNL should not have reached this point: file:%s, line:%d\n",
- file,line
- );
- abort();
-}
-
-
-#define __nl_assert(x) { \
- if(!(x)) { \
- __nl_assertion_failed(#x,__FILE__, __LINE__); \
- } \
-}
-
-#define __nl_range_assert(x,max_val) { \
- if(((x) > (max_val))) { \
- __nl_range_assertion_failed(x, 0.0, max_val, \
- __FILE__, __LINE__ \
- ); \
- } \
-}
-
-#define __nl_assert_not_reached { \
- __nl_should_not_have_reached(__FILE__, __LINE__); \
-}
-
-#ifdef NL_DEBUG
-#define __nl_debug_assert(x) __nl_assert(x)
-#define __nl_debug_range_assert(x,min_val,max_val) __nl_range_assert(x,min_val,max_val)
-#else
-#define __nl_debug_assert(x)
-#define __nl_debug_range_assert(x,min_val,max_val)
-#endif
-
-#ifdef NL_PARANOID
-#define __nl_parano_assert(x) __nl_assert(x)
-#define __nl_parano_range_assert(x,min_val,max_val) __nl_range_assert(x,min_val,max_val)
-#else
-#define __nl_parano_assert(x)
-#define __nl_parano_range_assert(x,min_val,max_val)
-#endif
-
-/************************************************************************************/
-/* classic macros */
-
-#ifndef MIN
-#define MIN(x,y) (((x) < (y)) ? (x) : (y))
-#endif
-
-#ifndef MAX
-#define MAX(x,y) (((x) > (y)) ? (x) : (y))
-#endif
-
-/************************************************************************************/
-/* memory management */
-
-#define __NL_NEW(T) (T*)(calloc(1, sizeof(T)))
-#define __NL_NEW_ARRAY(T,NB) (T*)(calloc(MAX(NB, 1),sizeof(T)))
-#define __NL_RENEW_ARRAY(T,x,NB) (T*)(realloc(x,(NB)*sizeof(T)))
-#define __NL_DELETE(x) if(x) free(x); x = NULL
-#define __NL_DELETE_ARRAY(x) if(x) free(x); x = NULL
-
-#define __NL_CLEAR(T, x) memset(x, 0, sizeof(T))
-#define __NL_CLEAR_ARRAY(T,x,NB) if(NB) memset(x, 0, (NB)*sizeof(T))
-
-/************************************************************************************/
-/* Dynamic arrays for sparse row/columns */
+/* NLContext data structure */
typedef struct {
- NLuint index;
+ NLuint index;
NLdouble value;
-} __NLCoeff;
+} NLCoeff;
typedef struct {
- NLuint size;
- NLuint capacity;
- __NLCoeff* coeff;
-} __NLRowColumn;
-
-static void __nlRowColumnConstruct(__NLRowColumn* c) {
- c->size = 0;
- c->capacity = 0;
- c->coeff = NULL;
-}
-
-static void __nlRowColumnDestroy(__NLRowColumn* c) {
- __NL_DELETE_ARRAY(c->coeff);
-#ifdef NL_PARANOID
- __NL_CLEAR(__NLRowColumn, c);
-#endif
-}
-
-static void __nlRowColumnGrow(__NLRowColumn* c) {
- if(c->capacity != 0) {
- c->capacity = 2 * c->capacity;
- c->coeff = __NL_RENEW_ARRAY(__NLCoeff, c->coeff, c->capacity);
- } else {
- c->capacity = 4;
- c->coeff = __NL_NEW_ARRAY(__NLCoeff, c->capacity);
- }
-}
-
-static void __nlRowColumnAdd(__NLRowColumn* c, NLint index, NLdouble value) {
- NLuint i;
- for(i=0; i<c->size; i++) {
- if(c->coeff[i].index == (NLuint)index) {
- c->coeff[i].value += value;
- return;
- }
- }
- if(c->size == c->capacity) {
- __nlRowColumnGrow(c);
- }
- c->coeff[c->size].index = index;
- c->coeff[c->size].value = value;
- c->size++;
-}
-
-/* Does not check whether the index already exists */
-static void __nlRowColumnAppend(__NLRowColumn* c, NLint index, NLdouble value) {
- if(c->size == c->capacity) {
- __nlRowColumnGrow(c);
- }
- c->coeff[c->size].index = index;
- c->coeff[c->size].value = value;
- c->size++;
-}
-
-static void __nlRowColumnClear(__NLRowColumn* c) {
- c->size = 0;
- c->capacity = 0;
- __NL_DELETE_ARRAY(c->coeff);
-}
+ NLdouble value[4];
+ NLboolean locked;
+ NLuint index;
+ std::vector<NLCoeff> a;
+} NLVariable;
-/************************************************************************************/
-/* SparseMatrix data structure */
+#define NL_STATE_INITIAL 0
+#define NL_STATE_SYSTEM 1
+#define NL_STATE_MATRIX 2
+#define NL_STATE_MATRIX_CONSTRUCTED 3
+#define NL_STATE_SYSTEM_CONSTRUCTED 4
+#define NL_STATE_SYSTEM_SOLVED 5
-#define __NL_ROWS 1
-#define __NL_COLUMNS 2
-#define __NL_SYMMETRIC 4
+struct NLContext {
+ NLenum state;
-typedef struct {
- NLuint m;
NLuint n;
- NLuint diag_size;
- NLenum storage;
- __NLRowColumn* row;
- __NLRowColumn* column;
- NLdouble* diag;
-} __NLSparseMatrix;
-
-
-static void __nlSparseMatrixConstruct(
- __NLSparseMatrix* M, NLuint m, NLuint n, NLenum storage
-) {
- NLuint i;
- M->m = m;
- M->n = n;
- M->storage = storage;
- if(storage & __NL_ROWS) {
- M->row = __NL_NEW_ARRAY(__NLRowColumn, m);
- for(i=0; i<m; i++) {
- __nlRowColumnConstruct(&(M->row[i]));
- }
- } else {
- M->row = NULL;
- }
-
- if(storage & __NL_COLUMNS) {
- M->column = __NL_NEW_ARRAY(__NLRowColumn, n);
- for(i=0; i<n; i++) {
- __nlRowColumnConstruct(&(M->column[i]));
- }
- } else {
- M->column = NULL;
- }
-
- M->diag_size = MIN(m,n);
- M->diag = __NL_NEW_ARRAY(NLdouble, M->diag_size);
-}
-
-static void __nlSparseMatrixDestroy(__NLSparseMatrix* M) {
- NLuint i;
- __NL_DELETE_ARRAY(M->diag);
- if(M->storage & __NL_ROWS) {
- for(i=0; i<M->m; i++) {
- __nlRowColumnDestroy(&(M->row[i]));
- }
- __NL_DELETE_ARRAY(M->row);
- }
- if(M->storage & __NL_COLUMNS) {
- for(i=0; i<M->n; i++) {
- __nlRowColumnDestroy(&(M->column[i]));
- }
- __NL_DELETE_ARRAY(M->column);
- }
-#ifdef NL_PARANOID
- __NL_CLEAR(__NLSparseMatrix,M);
-#endif
-}
-
-static void __nlSparseMatrixAdd(
- __NLSparseMatrix* M, NLuint i, NLuint j, NLdouble value
-) {
- __nl_parano_range_assert(i, 0, M->m - 1);
- __nl_parano_range_assert(j, 0, M->n - 1);
- if((M->storage & __NL_SYMMETRIC) && (j > i)) {
- return;
- }
- if(i == j) {
- M->diag[i] += value;
- }
- if(M->storage & __NL_ROWS) {
- __nlRowColumnAdd(&(M->row[i]), j, value);
- }
- if(M->storage & __NL_COLUMNS) {
- __nlRowColumnAdd(&(M->column[j]), i, value);
- }
-}
-
-static void __nlSparseMatrixClear( __NLSparseMatrix* M) {
- NLuint i;
- if(M->storage & __NL_ROWS) {
- for(i=0; i<M->m; i++) {
- __nlRowColumnClear(&(M->row[i]));
- }
- }
- if(M->storage & __NL_COLUMNS) {
- for(i=0; i<M->n; i++) {
- __nlRowColumnClear(&(M->column[i]));
- }
- }
- __NL_CLEAR_ARRAY(NLdouble, M->diag, M->diag_size);
-}
-
-/************************************************************************************/
-/* SparseMatrix x Vector routines, internal helper routines */
-
-static void __nlSparseMatrix_mult_rows_symmetric(
- __NLSparseMatrix* A, NLdouble* x, NLdouble* y
-) {
- NLuint m = A->m;
- NLuint i,ij;
- __NLRowColumn* Ri = NULL;
- __NLCoeff* c = NULL;
- for(i=0; i<m; i++) {
- y[i] = 0;
- Ri = &(A->row[i]);
- for(ij=0; ij<Ri->size; ij++) {
- c = &(Ri->coeff[ij]);
- y[i] += c->value * x[c->index];
- if(i != c->index) {
- y[c->index] += c->value * x[i];
- }
- }
- }
-}
-
-static void __nlSparseMatrix_mult_rows(
- __NLSparseMatrix* A, NLdouble* x, NLdouble* y
-) {
- NLuint m = A->m;
- NLuint i,ij;
- __NLRowColumn* Ri = NULL;
- __NLCoeff* c = NULL;
- for(i=0; i<m; i++) {
- y[i] = 0;
- Ri = &(A->row[i]);
- for(ij=0; ij<Ri->size; ij++) {
- c = &(Ri->coeff[ij]);
- y[i] += c->value * x[c->index];
-
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list