!B (Sandbox) (CE-21795) Importing models with multisubmaterials via fbx switches...
[CRYENGINE.git] / Code / Sandbox / Plugins / LodGeneratorPlugin / Util / Solver.cpp
blob36bf220f3af7fe97c22bf6a729be4e11e2fbbd6c
1 #include "StdAfx.h"
2 #include "Solver.h"
3 //#include <CHOLMOD/cholmod.h>
5 namespace LODGenerator
7 CSolver::CSolver() {
10 CSolver::~CSolver() {
13 bool CSolver::solve(const CSparseMatrix& A_in, CVectorD& x_out, const CVectorD& b_in) {
14 // To support this function, a massive amount of third party code would have to be added to the project,
15 // which is not desirable. There is already one custom UV unwrapping function, which does an OK job ...
16 #if 0
17 assert(A_in.n() == A_in.m()) ;
18 assert(A_in.has_symmetric_storage()) ;
20 // Step 1: initialize CHOLMOD library
21 //----------------------------------------------------
22 cholmod_common c ;
23 cholmod_start(&c) ;
25 int N = A_in.n() ;
26 int NNZ = A_in.nnz() ;
28 // Step 2: translate sparse matrix into cholmod format
29 //---------------------------------------------------------------------------
30 cholmod_sparse* A = cholmod_allocate_sparse(N, N, NNZ, false, true, -1, CHOLMOD_REAL, &c);
32 int* colptr = static_cast<int*>(A->p) ;
33 int* rowind = static_cast<int*>(A->i) ;
34 double* a = static_cast<double*>(A->x) ;
36 // Convert Graphite Matrix into CHOLMOD Matrix
37 int count = 0 ;
38 for(int j=0; j<N; j++) {
39 const CSparseMatrix::Column& Cj = A_in.column(j) ;
40 colptr[j] = count ;
41 for(int ii=0; ii<Cj.nb_coeffs(); ii++) {
42 a[count] = Cj.coeff(ii).a ;
43 rowind[count] = Cj.coeff(ii).index ;
44 count++ ;
47 colptr[N] = NNZ ;
50 // Step 2: construct right-hand side
51 cholmod_dense* b = cholmod_allocate_dense(N, 1, N, CHOLMOD_REAL, &c) ;
52 memcpy(b->x, b_in.data(), N * sizeof(double)) ;
54 // Step 3: factorize
55 cholmod_factor* L = cholmod_analyze(A, &c) ;
56 cholmod_factorize(A, L, &c) ;
57 cholmod_dense* x = cholmod_solve(CHOLMOD_A, L, b, &c) ;
58 memcpy(x_out.data(), x->x, N * sizeof(double)) ;
60 // Step 5: cleanup
61 cholmod_free_factor(&L, &c) ;
62 cholmod_free_sparse(&A, &c) ;
63 cholmod_free_dense(&x, &c) ;
64 cholmod_free_dense(&b, &c) ;
65 cholmod_finish(&c) ;
66 return true;
67 #else
68 return false;
69 #endif
72 bool CSolver::needs_rows() const {
73 return false ;
76 bool CSolver::needs_columns() const {
77 return true ;
80 bool CSolver::supports_symmetric_storage() const {
81 return true ;