#ifndef __RV_DEFS_H_INCLUDED_
#define __RV_DEFS_H_INCLUDED_


#include <math.h>
#include "rvtypes.h"
 



//rvb class name 
#define RVB_CLS_MATRIX     "rvb_class_matrix"
#define RVB_CLS_IMAGE      "rvb_class_image"
#define RVB_CLS_MASK       "rvb_class_mask"
#define RVB_CLS_BLOB       "rvb_class_blob"
#define RVB_CLS_READING_BANK    "rvb_class_reading_bank"
#define RVB_CLS_SEQUENCE   "rvb_class_sequence"
#define RVB_CLS_LIST       "rvb_class_list"
#define RVB_CLS_CLUSTER    "rvb_class_cluster"
#define RVB_CLS_GRAPH      "rvb_class_graph"
#define RVB_CLS_TREE       "rvb_class_tree"
#define RVB_CLS_CONTOUR    "rvb_class_contour"
#define RVB_CLS_REGION     "rvb_class_region"
#define RVB_CLS_CURVE      "rvb_class_curve"

#define RVB_CLS_COMPOSITE  "rvb_class_composite"
#define RVB_CLS_COMP_DESCRIPTOR  "rvb_class_composite_descriptor"

#define RV_IS_FLAG_ON(flags, id)    ((flags & id) == id)
#define RV_SET_FLAG_ON(flags, id)   (flags |= id)
#define RV_SET_FLAG_OFF(flags, id)  (flags &= ~id)



/* RV_FUNCNAME macro defines icvFuncName constant which is used by RV_ERROR macro */
#ifdef RV_NO_FUNC_NAMES
    #define RV_FUNCNAME( name )
    #define __RV_FUNC_NAME__   _T("")
#else    
    #define RV_FUNCNAME( name )  \
    static char __RV_FUNC_NAME__[] = name
#endif


//#ifndef NDEBUG
//     #define RV_ASSERT( condition )   assert(condition) 
//#else
//     #define RV_ASSERT( condition )   
//#endif

#ifndef NDEBUG
     #define RV_NULL(ptr)  ptr = NULL
#else
     #define RV_NULL(ptr)  
#endif

#define RV_RELEASE_OBJ(fnRelease, obj) {  fnRelease(obj); obj=NULL;  }

//************************************************************//
//
#define DSM_MEM_DELTA(size)  (((1 << 7) + ((size)-1)) / (size) )

//逐步放弃支持多数据类型
#define RV_COLLECT_FIELDS()     \
    int       cid;             /* Miscellaneous flags.     */      \
    int       total;             /* Total number of elements.            */    \
	int       tag;                                                             \
	int       dataType;         \
    int       elemSize;    /* Size of sequence element in bytes.   */     \
	RvMemoryStore nodeStore; \
    RvMemoryStore dataStore; \
	RvDestroyFunc      fnDestroyElem;  \
	RvCompareFunc      fnCompareElem; \
	void*              pCompareUserData;     \
	RvConditionFunc    fnConditionElem; \
	void*              pCriterionUserData; 
	
//新的不需要数据类型和数据大小，数据结构仅支持void*
#define RV_COLLECT_FIELDS_E1()     \
    int       cid;             /* Miscellaneous flags.     */      \
    int       total;             /* Total number of elements.            */    \
	int       tag;                                                             \
	RvMemoryStore nodeStore; \
    /*RvMemoryStore dataStore;*/ \
	RvDestroyFunc      fnDestroyElem;  \
	RvCompareFunc      fnCompareElem; \
	void*              pCompareUserData;     \
	RvConditionFunc    fnConditionElem; \
	void*              pCriterionUserData; 



typedef struct _rv_seq_reader
{
 
	//int  elemSize;
	RvCollectItem*  current;  
}RvSeqReader;

typedef struct _rv_seq_writer{
	int            count;
	//int            elemSize;
    RvMemoryStore  nodeStore;    /* Where the seq is stored.             */  
	//RvMemoryStore  dataStore;    /* Where the data is stored.             */   
	RvCollectItem*     pTail  ;    
	RvCollectItem*     pHead ; 
}RvSeqWriter;


typedef struct _rv_cluster_reader
{
	int  count;
	int  index;
	int  blockIndex;
 	int  delta;
	int  elemSize;
	RvCollectItem*  current;
}RvClusterReader;

typedef struct _rv_cluster_writer{
	int              count;
	int              blockCount;
	int              elemSize;
	int              index;
	int              delta;
	RvCollectItem*   current; 
}RvClusterWriter;

/*********************************** Sequence *******************************************/
typedef struct __rv_seq_item_ 
{
    struct __rv_seq_item_*  prev; /* Previous sequence Item.                   */
    struct __rv_seq_item_*  next; /* Next sequence Item.                       */
    void*    data;               /* Pointer to the first element of the Item. */
}RvSeqItem ;


typedef struct __rv_list_item_ 
{
    struct __rv_list_item_*  prev; /* Previous sequence Item.                   */
    struct __rv_list_item_*  next; /* Next sequence Item.                       */
    RV_DATA_CVT    data;        /* Pointer to the first element of the Item. */
} RvListItem ;

typedef struct __rv_dynamic_
{
	RV_COLLECT_FIELDS()
}RvDynamic_t;
  
typedef struct _rv_seq_
{
    RV_COLLECT_FIELDS_E1() 
 	RvSeqItem*    first;        /* Pointer to the first sequence block. */    \
 	RvSeqItem*    last;        /* Pointer to the last sequence Item. */    
}RvSeq;


typedef struct __rv_clust_block_ 
{
    struct __rv_clust_block_ *  prev; /* Previous sequence Item.                   */
    struct __rv_clust_block_ *  next; /* Next sequence Item.                       */
    RV_DATA_CVT  data;        /* Pointer to the first element of the Item. */
}RvClustBlock, RvClustItem;


/******************************** RvClust ***********************************************/
typedef struct __rv_clust_
{
    RV_COLLECT_FIELDS()      
    int             delta;          //increment each time
	int             blockCount;
 	RvClustBlock*   startBlock;     /* Pointer to the first sequence block. */    
 	RvClustBlock*   endBlock;      /* Pointer to the last sequence block. */    
}RvClust;


/********************************flexible list(thread-safe)*********************************************/
typedef struct __rv_flex_list_
{    
	RV_COLLECT_FIELDS()
    CRITICAL_SECTION  cs;
	RvListItem* pHead;
	RvListItem* pTail;
	RvListItem* pCur;
}RvList_t;

 
typedef struct __rv_tree_item_
{
	DWORD_PTR               flags;
    struct __rv_tree_item_* left;  /*  left sibling node.     */      \
    struct __rv_tree_item_* right; /* right sibling node.     */      \
    struct __rv_tree_item_* parent;    /*    parent node.   */          \
    struct __rv_tree_item_* child;  /*   child node .       */        \
	RV_DATA_CVT  data;        /* Pointer to the first element of the Item. */   \
}RvTreeItem;

/******************************** Tree *********************************************/
typedef struct __rv_tree_
{    
	RV_COLLECT_FIELDS()
	RvTreeItem* root;
}RvTree_t;

//next version UINT should be added into the matrix, and char
#define RV_MTX_DESC   int   cid; \
					int                dataType; \
					size_t           size; \
					int* pHeap;   \
                    int  headerRefCount;  \
                                            \
					union       \
					{           \
						void*           ptr;  \
						char*           ch;  \
						unsigned char*  bt;   \
						short*          s16;    \
						unsigned short* u16;    \
						int*            s32;   \
						float*          fl;   \
						double*         db;   \
					} data;            \
													  \
					union{                         \
						int rows;       \
						int height;       \
					};            \
				    union{                    \
						int cols;        \
						int width;        \
					};             \
					int  pitch;         \
					BOOL bVirtual;        



 

//矩阵定义
typedef struct __rv_matrix_
{
	RV_MTX_DESC
} RvMatrix_t;




#define RV_CONTOUR_FIELDS()  \
	int         cid;    \
	int         code;   \
	int         hier;   \
	int         flags;  
	

 

typedef struct _rv_contour_header_
{ 
	RV_CONTOUR_FIELDS()
}RvContourHeader_t;

typedef struct _contour_body_
{
	RV_CONTOUR_TYPE   type;   
	unsigned short    parent, child, prev, next; 

	int      count;
	RvPoint* pPtArr;

}RvContourBody_t;

#define __CNTR_RECT  1
#define __CNTR_BOX   (1<<1)
#define __CNTR_AREA  (1<<2)
#define __CNTR_PERI  (1<<3)
#define __CNTR_CIRC  (1<<4)
#define __CNTR_SLANT  (1<<5)




 
typedef struct _rv_contour_
{ 
	RV_CONTOUR_FIELDS()
	int     attribute;

//	BOOL    bRectValid;
//	BOOL    bBoxValid;

	RvRect  rect; 
	RvBox2D box; //bound
	double  area;
	double  perim;
	double circ;
	double slant;

	//轮廓数量,最小1，最多
	unsigned short   total;
	RvContourBody_t*  pData; 
	 
}RvContour_t;
 

//typedef struct _rv_contour_list_body_
//{ 
//	RV_CONTOUR_DATA()
//	struct _rv_contour_list_body_* prev;
//	struct _rv_contour_list_body_* next;
//}RvContourListBody_t;
//
//typedef struct _rv_contour_list_
//{        
//	RV_CONTOUR_FIELDS()
//	RvContourListBody_t* clb;
//}RvContourList_t;


//typedef struct _rv_contour_tree_body_
//{ 
//	RV_CONTOUR_DATA()
//	struct _rv_contour_tree_body_* prev;
//	struct _rv_contour_tree_body_* next;
//	struct _rv_contour_tree_body_* up;
//	struct _rv_contour_tree_body_* down;
//}RvContourTreeBody_t;

//typedef struct _rv_contour_tree_
//{ 
//	RV_CONTOUR_FIELDS()
//	RvContourTreeBody_t* ctb;
//}RvContourTree_t;
// 
//////////////////////////////////////////////////////////////////

//图像对象
typedef struct __rv_image_{
	RV_MTX_DESC
	int             imageType;
	int             channelFlags;  //RESERVED 2014.02.26
	int             alignBytes;    //对齐字节:1或4(默认) Modified by HJL on 2023/03/29
	//double          range[1+1];   //the lower and the upper.
} RvImage_t;







typedef struct _rv_blob_t
{
	int              cid;      //class id: blob
	int              tag;
	RV_BLOB_ENCODER  encoder;  
	RvPoint          leftTop;  //  原点坐标
	int              width;   //宽度
	int              height;  //高度 
	void*            pDataHandler;  //BLOB数据
}RvBlob_t;
 
// 
//typedef struct _rv_blob_t
//{
//	RV_BLOB_TYPE     type;
//	RV_BLOB_ENCODER  encoder;
//	int           tag;
//	RvPoint       leftTop;  //  原点坐标
//	int           width;   //宽度
//	int           height;  //高度
//	RvImage       base;   //原始图象,仅引用。
//	//RvSequence    sequence;  //点坐标序列， 或点坐标的组合的序列。
//
//	void*         pDataHandler;  //保留:用于ROI图(DEF)
//}RvBlob_t;

/*
typedef struct _rv_blob_ex_t{
	RV_BLOB_T
     RV_BLOB_ATTR
}RvBlobEx_t;
*/


typedef struct _rv_mask_t{  
    RV_MTX_DESC
    int   refX, refY;
	BOOL  bEnhanced;
}RvMask_t;
 
#define RV_PICK_FIELDS \
    int             cid;     \
	RV_PICK_TYPE    shape;\
	int             drawStyle;\
	RvFloat32       alignAngle;    \
	RvPoint_f       alignCenter;    \
	RvOffset_f      alignOffset;    \
	int         alignType;\
	int         left, top; \
	int         width, height;\
	RvImage_t*  pSubImage;\
	RvMask_t*   pMask;


typedef struct _rv_pick_t{
    RV_PICK_FIELDS
}RvPick_t;

 
typedef struct _rv_pick_rect_t
{
   RV_PICK_FIELDS
   RvPoint_f     leftTop;
   RvPoint_f     rightBottom;
   RvFloat32     angle;  //围绕自身中心旋转的角度
}RpRect_t;


typedef struct _rv_pick_circle_t
{
   RV_PICK_FIELDS
   RvPoint_f     center;
   RvFloat32        rx;  
   RvFloat32        ry;    
}RpCircle_t;

 


typedef struct _rv_pick_annulus_t
{
   RV_PICK_FIELDS
   RvPoint_f    center;
   RvFloat32     startAngle; // 
   RvFloat32     endAngle; // 
   RvFloat32       innerRadius;   //  
   RvFloat32       outerRadius; 
  // RvSeq*    pContourList;
}RpAnnulus_t;


typedef struct _rv_pick_torus_t
{
   RV_PICK_FIELDS
   RvPoint_f   center; // 
   RvFloat32      innerRadius;   //  inner radius
   RvFloat32      outerRadius;   //  outer radius
}RpTorus_t;
 

#define MAX_PICK_POLYGON_EDGE  256

typedef struct _rv_pick_polygon_t
{
   RV_PICK_FIELDS
   int        vertexCount;
   RvPoint_f*    pVertexCollect;  
}RpPolygon_t;

/*
typedef struct _rv_pick_adaptive_t
{
   RvRegion_t   base;
   GPoint     leftTop;
   GPoint     rightBottom;  
   int        vertexCount;
   GPoint     *vertexCollect;
}RpAdapt_t;
*/

typedef struct _rv_pick_custom_t
{
   RV_PICK_FIELDS
   RvPoint_f   leftTop;
   RvPoint_f   rightBottom;  
}RpCustom_t;

typedef struct _rv_pick_ellipse_t
{
   RV_PICK_FIELDS
   RvPoint_f   center;
   RvFloat32   rx;  
   RvFloat32   ry;  
   RvFloat32   angle;
}RpEllipse_t;

typedef struct _rv_pick_sector_t
{
   RV_PICK_FIELDS
   RvPoint_f    center;
   RvFloat32     startAngle; // 
   RvFloat32     endAngle; // 
   int       radius; 
}RpSector_t;



typedef struct __rv_path_t{
 	int         channel;
	BOOL        bAutoRelease;
    int         itemCount;
	RvPathItem* pItemArray;
}RvPath_t;

#define RV_CUT_FIELDS             \
	int             cid;          \
	RV_CUT_TYPE     type;         \
	RvRect_f64      bound;        \
	RvFloat32       sampler;      \
	RvFloat32       alignAngle;   \
	RvPoint_f       alignCenter;  \
	RvOffset_f      alignOffset;  \
	int             alignType;    \
 	RvSeq*          pPathMap;     

 


typedef struct _rv_cut_t{
    RV_CUT_FIELDS 
}RvCut_t;


typedef struct _rv_cut_point_t
{
    RV_CUT_FIELDS
    RvPoint_f64    pos;
}RcPoint_t;


typedef struct _rv_cut_line_t
{
   RV_CUT_FIELDS
   RvPoint_f64   p1;
   RvPoint_f64   p2;
}RcLine_t;


typedef struct _rv_cut_arc_t
{
   RV_CUT_FIELDS
   RvPoint_f64    center;
   RvFloat64         radius;        // 
   RvFloat64         startAngle, endAngle;   //角度单位0-360度
}RcArc_t;

typedef struct _rv_cut_circle_t
{
   RV_CUT_FIELDS
   RvPoint_f64   center;
   RvFloat64        radius;  
}RcCircle_t;

typedef struct _rv_cut_spoke_t
{
   RV_CUT_FIELDS
   RvPoint_f64    center;  
   RvFloat64      innerRadius;     
   RvFloat64      outerRadius;
   RvFloat64      angle; //角度
   int            segmentCount;
}RcSpoke_t;


typedef struct _rv_cut_bulleye_t
{
   RV_CUT_FIELDS
   RvPoint_f64    center; // 
   RvFloat64      innerRadius;
   RvFloat64      outerRadius;
   int            segmentCount;
}RcBullEye_t;


typedef struct _rv_cut_rake_t
{
   RV_CUT_FIELDS
   RvPoint_f64      leftTop; // 
   RvPoint_f64      rightBottom;   //  
   RvFloat64        angle; //角度
   int              segmentCount;

}RcRake_t;

typedef struct _rv_cut_polyline_t
{
   RV_CUT_FIELDS
   int          nVertexCount;
   RvPoint_f64* pVertexArray;
   //RvSeq*  pSegmentList;
}RcPolyline_t;
  
typedef struct _rv_cut_ellipse_t
{
   RV_CUT_FIELDS
   RvPoint_f64   center;
   RvFloat64        rx,ry;  
   RvFloat64        angle;
}RcEllipse_t;

typedef struct _rv_cut_any_two_t
{
   RV_CUT_FIELDS
   RvPoint_f64   p1,p2;
   RvPoint_f64   p3,p4;
}RcAnyTwo_t;

typedef struct _rv_cut_polygon_t
{
   RV_CUT_FIELDS
   int          nVertexCount;
   RvPoint_f64* pVertexArray;
}RcPolygon_t;

typedef struct _rv_cut_any_seg_t
{
   RV_CUT_FIELDS
   int          nVertexCount;
   RvPoint_f64* pVertexArray;
}RcAnySeg_t;



typedef struct _rv_cut_arch_t
{
   RV_CUT_FIELDS
   RvPoint_f64    center;
   RvFloat64      innerRadius;
   RvFloat64      outerRadius;
   RvFloat64      startAngle, endAngle;   //角度单位0-360度
   int            segmentCount;                //线段数
}RcArch_t;


typedef struct _rv_cut_square_t
{
   RV_CUT_FIELDS
   double        angle;    //旋转角度
   RvPoint_f64   leftTop, rightBottom;
}RcSquare_t;


#define RV_MALLOC_OBJECT(type)  ((type *) malloc(sizeof(type)))
#define RV_MALLOC_ARRAY(type,count)  ((type *)malloc((count)*sizeof(type)))


/* default image row align (in bytes) */
#define  RV_DEFAULT_IMAGE_ROW_ALIGN  4




#define RV_MAX_SCALER    1000


#define RV_INF_VAL       0xFFFFFFFF

#define RV_MAX_PICK_COUNT        128

#define RV_MAX_CLR_CHN                  3
#define RV_MAX_FILTER_SZE               32
#define RV_MIN_FILTER_SZE               3


#define RV_MAX_GRAY_LEVEL               0xFF
#define RV_GREY_LEVEL_COUNT             (RV_MAX_GRAY_LEVEL+1)

 
#define INF_VAL                 0xFFFFFFFF

#define  RV_DECIMAL_PRECISE   0.00000000001

#define PV_UNIT_RADIAN       (RV_PI/180.0)

#define  RV_EPS              2.220446049250313e-016

 
#define RV_MIN_INT32    0x7FFFFFFF
 






















#endif