Home | History | Annotate | Download | only in algos
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef MAT_H_
     18 
     19 #define MAT_H_
     20 
     21 #include "vec.h"
     22 #include <stdint.h>
     23 #include <sys/types.h>
     24 
     25 #ifdef __cplusplus
     26 extern "C" {
     27 #endif
     28 
     29 struct Mat33 {
     30     float elem[3][3];
     31 
     32 };
     33 
     34 struct Size3 {
     35     uint32_t elem[3];
     36 
     37 };
     38 
     39 struct Mat44 {
     40     float elem[4][4];
     41 
     42 };
     43 
     44 struct Size4 {
     45     uint32_t elem[4];
     46 
     47 };
     48 
     49 void initZeroMatrix(struct Mat33 *A);
     50 void initDiagonalMatrix(struct Mat33 *A, float x);
     51 
     52 void initMatrixColumns(
     53         struct Mat33 *A, const struct Vec3 *v1, const struct Vec3 *v2, const struct Vec3 *v3);
     54 
     55 void mat33Apply(struct Vec3 *out, const struct Mat33 *A, const struct Vec3 *v);
     56 void mat33Multiply(struct Mat33 *out, const struct Mat33 *A, const struct Mat33 *B);
     57 void mat33ScalarMul(struct Mat33 *A, float c);
     58 
     59 void mat33Add(struct Mat33 *out, const struct Mat33 *A);
     60 void mat33Sub(struct Mat33 *out, const struct Mat33 *A);
     61 
     62 int mat33IsPositiveSemidefinite(const struct Mat33 *A, float tolerance);
     63 
     64 // out = A^(-1)
     65 void mat33Invert(struct Mat33 *out, const struct Mat33 *A);
     66 
     67 // out = A^T B
     68 void mat33MultiplyTransposed(
     69         struct Mat33 *out, const struct Mat33 *A, const struct Mat33 *B);
     70 
     71 // out = A B^T
     72 void mat33MultiplyTransposed2(
     73         struct Mat33 *out, const struct Mat33 *A, const struct Mat33 *B);
     74 
     75 // out = A^T
     76 void mat33Transpose(struct Mat33 *out, const struct Mat33 *A);
     77 
     78 void mat33DecomposeLup(struct Mat33 *LU, struct Size3 *pivot);
     79 
     80 void mat33SwapRows(struct Mat33 *A, const uint32_t i, const uint32_t j);
     81 
     82 void mat33Solve(const struct Mat33 *A, struct Vec3 *x, const struct Vec3 *b, const struct Size3 *pivot);
     83 
     84 void mat33GetEigenbasis(struct Mat33 *S, struct Vec3 *eigenvals, struct Mat33 *eigenvecs);
     85 
     86 uint32_t mat33Maxind(const struct Mat33 *A, uint32_t k);
     87 
     88 void mat33Rotate(struct Mat33 *A, float c, float s, uint32_t k, uint32_t l, uint32_t i, uint32_t j);
     89 
     90 void mat44Apply(struct Vec4 *out, const struct Mat44 *A, const struct Vec4 *v);
     91 
     92 void mat44DecomposeLup(struct Mat44 *LU, struct Size4 *pivot);
     93 
     94 void mat44SwapRows(struct Mat44 *A, const uint32_t i, const uint32_t j);
     95 
     96 void mat44Solve(const struct Mat44 *A, struct Vec4 *x, const struct Vec4 *b, const struct Size4 *pivot);
     97 
     98 #ifdef __cplusplus
     99 }
    100 #endif
    101 
    102 #endif  // MAT_H_
    103