/*
 * Copyright 2008 Nicolas Normand and Pierre Evenou.
 
 * This file is part of hLutChamfer3D.
 
 * hLutChamfer3D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 
 * hLutChamfer3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public License
 * along with hLutChamfer3D.  If not, see <http://www.gnu.org/licenses/>.
 */
/** \file hLutChamfer3D.h
 *
 */

// $Id: hLutChamfer3D.h,v 1.15 2008/10/31 06:12:30 cvs Exp $

#define DIMENSION	    3
#define MAX_WEIGHT_COUNT    200
#define MAX_CONE_COUNT	    99
#define MAX_HALFSPACE_COUNT MAX_CONE_COUNT
#define MAX_LUT_ENTRY	    100000

typedef enum {
    NOT_A_NORM = -1
} ReturnCodes;

typedef enum {
    ELAPSED_TIME_FILENO = 3,
    VISITED_POINTS_FILENO,
    MATRIX_FILENO,
    HPARAMETERS_FILENO,
    NORM_CHECK_PARTITION_FILENO
} Fileno;

typedef int LookUpTable[MAX_WEIGHT_COUNT][MAX_LUT_ENTRY + 1];

/*!
 * Vector nD
 */
typedef int Vector[DIMENSION];

/*!
 * Weighting.
 *
 * A weighting is the combination of a vector and a weight.
 */
typedef struct {
    Vector p;	    //!< Neighbor
    unsigned int w; //!< Weight
} Weighting;

/*!
 * Chamfer mask
 */
typedef struct {
    //! Number of weightings in this mask
    unsigned int ng;
    //! Array of ::Mask.ng weightings
    Weighting vg[MAX_WEIGHT_COUNT];
} Mask;

/*!
 * HPolytope Matrix
 */
typedef struct {
    unsigned int height;    /* half space count	    */
    unsigned int width;	    /* ambient dimension    */
    unsigned int elts[0];
} Matrix;

/*!
 * \brief Structure for propagation-based distance computation
 */
typedef struct {
    int allocatedSize;
    int size;
    Mask* mask;
    int* pixels;
} GeneratorImage;

/*!
 * Describes a set of
 * \f$\mathcal{H}\f$-polytopes
 * sharing the same matrix.
 * The i<sup>th</sup>
 * \f$\mathcal{H}\f$-polytope
 * is the set of points \f$(x,y)\f$ verifying:
 * \f$\mathrm{matrix}.\left(\begin{array}{c}x\\y\end{array}\right)\leq\mathrm{coeff}[i]\f$
 */
typedef struct {
    int halfspaceCount;
    int polytopeCount;
    Matrix *matrix;
    //unsigned int matrix[MAX_HALFSPACE_COUNT][DIMENSION];
    int coeff [MAX_LUT_ENTRY + 1][MAX_HALFSPACE_COUNT];
} HPolytopeTable;

typedef struct {
    int** coneDirs;
    int** hIncrements;
    int* next;
    int last;
    FILE* out;
} Cone;

struct SimplicialCone;
typedef struct SimplicialCone SimplicialCone;
struct SimplicialCone {
    int dimension;
    Vector vectorSum;
    int vectorSumHParams[MAX_HALFSPACE_COUNT];
    SimplicialCone*** faces;
    int vectorIndex[DIMENSION];
};

typedef struct {
    SimplicialCone** conesPtr[DIMENSION+1];
    int coneCount[DIMENSION+1];
} SimplicialConeDecomposition;

typedef int (*comparisonFunction)(const void* a, const void* b);

inline int max(int a, int b);
inline int min(int a, int b);
int decreasingOrder(const int* a, const int* b);

void symPointInGenerator(Vector p);
void nextVisiblePoint(Vector point, int dimension);

void hCoeffAdd(int *dst, int *src, int size);
void hCoeffSubtract(int *dst, int *src, int size);

GeneratorImage* imageCreate(Mask *mask);
void imageFree(GeneratorImage* image);
int imageGetPixelIndex(GeneratorImage* image, int p[]);
int imageGetPixel(GeneratorImage* image, int p[]);

Matrix* matrixCreate(int dimension);
Matrix* matrixAllocateRow(Matrix* matrix);
Matrix* matrixAddUniqueRowElts(Matrix* matrix, Vector elts);
void matrixPrint(FILE*out, Matrix* m);
unsigned int distanceFromOrigin(Vector p, Matrix *matrix);

Matrix* computeMatrix(Mask* mask, int dimension);

void vectorCopy(Vector dst, Vector src, int dimension);
void vectorAdd(Vector dst, Vector src, int dimension);
void vectorSubtract(Vector dst, Vector src, int dimension);
int vectorIsConstant(Vector vector, int dimension);
int vectorScalarProduct(Vector v1, Vector v2, int dimension);
int vectorIsInGenerator(Vector v, int dimension);
int vectorIsEqual(Vector v1, Vector v2, int dimension);
int vectorIsVisible(Vector v, int dimension);
void vectorPrint(FILE *out, Vector vector, int dimension);

Mask* maskCreate(int weightingCount, int dimension);
void maskAddWeighting(Mask* mask, Vector vector, int weight, int dimension);
void maskPrint(FILE* out, Mask* mask, int displayOrder[MAX_WEIGHT_COUNT], int apparitionRadius[MAX_WEIGHT_COUNT], int innerRadius[MAX_WEIGHT_COUNT]);

Cone* coneCreate(Mask* chamferMask, HPolytopeTable* hPolytopeTable, FILE* out);
void coneDestroy(Cone *cone);

SimplicialConeDecomposition* simplicialConeDecompositionCreate();
SimplicialCone* simplicialConeDecompositionAddCone(SimplicialConeDecomposition* decomposition, Mask* mask, int* combi, int dimension);

int triangulation(Mask* chamferMask, SimplicialConeDecomposition* decomposition, HPolytopeTable* hPolytopeTable);
