halcon基礎數據類型詳解


#if defined(__CHAR_UNSIGNED__) || defined(__sgi) 
#define  INT1      signed char       /* integer, signed 1 Byte         */
#define  INT1_MIN  SCHAR_MIN
#define  INT1_MAX  SCHAR_MAX
#else
#define  INT1      char              /* integer, signed 1 Byte         */
#define  INT1_MIN  CHAR_MIN
#define  INT1_MAX  CHAR_MAX
#endif
#define  UINT1     unsigned char     /* integer, unsigned 1 Byte       */
#define  UINT1_MIN 0
#define  UINT1_MAX UCHAR_MAX

#define LONG_FORMAT _INT64_FORMAT typedef INT4_8 Hlong; typedef UINT4_8 Hulong; 

 

  看粗體部分,可以看到 Hlong型在32位的機器上其實就是long型 代表4個字節 32位,在64位機器上有另一種定義

 

  再來看看halcon中最重要的數據類型HTuple,在C++里面,halcon將HTuple類型封裝了類,其始祖類HRootObject,這個類相當於MFC里面的CObject,halcon從HRootObject派生了HBaseArray,當然這兩個類是虛基類,有一些方法需要我HTuple自己實現,當然也有一些方法可以直接用的。這兩個類在HCPPUtil里,可以看看。

  HTuple類就是從HBaseArray派生,元組基類,相當於數組,具有如下的構造函數:

  

  HTuple(int l);
  HTuple(float f);
  HTuple(double d);
  HTuple(const char *s);
  HTuple(const HCtrlVal &c);
  HTuple(const HTuple &in):HBaseArray() {CopyTuple(in);}
  HTuple(Hlong length, const HTuple &value);
  HTuple(const HTuple &length, const HTuple &value);
  HTuple(SpecialTuple d);

  

  HTuple對各種操作符進行了重載:

  operator     HCtrlVal(void) const;
  HTuple       operator () (Hlong min, Hlong max) const;
  HTuple       operator () (const HTuple &min, const HTuple &max) const;
  HCtrlVal    &operator [] (Hlong index);
  HCtrlVal     operator [] (Hlong index) const;
  HCtrlVal    &operator [] (const HTuple &index);
  HCtrlVal     operator [] (const HTuple &index) const;
  HTuple      &operator ++ (void); // nur fuer double und Hlong
  HBool        operator !  (void) const;
  HTuple       operator ~  (void) const;
  HTuple       operator << (const HTuple &val) const;
  HTuple       operator << (Hlong val) const;
  HTuple       operator >> (const HTuple &val) const;
  HTuple       operator >> (Hlong val) const;
  HTuple       operator +  (const HTuple &val) const;
  HTuple       operator +  (double val) const;
  HTuple       operator +  (int val) const;

 

  在講解halcon是如何維護這樣一個HTuple中各種數據之前 ,先來看看這樣一個類:

  

class LIntExport HCtrlVal  {
  friend class HTuple;
public:
  HCtrlVal(void)      {val.type  = UndefVal;  val.par.l = 0;}
#if !defined(_TMS320C6X)
  HCtrlVal(Hlong l)   {val.type  = LongVal;   val.par.l = l;}
#endif
  HCtrlVal(int l)     {val.type  = LongVal;   val.par.l = l;}
  HCtrlVal(double d)  {val.type  = DoubleVal; val.par.f = d;}
  HCtrlVal(const char *s);
  HCtrlVal(const HCtrlVal &v) {CopyCtrlVal(v);}
  ~HCtrlVal(void)             {ClearCtrlVal();}
  HCtrlVal& operator = (const HCtrlVal &v);
 
  // Type conversion
  int         ValType() const          {return val.type;}
  operator    int(void) const          {return I();}
#if !defined(_TMS320C6X)
  operator    Hlong(void) const        {return L();}
#endif
  operator    double(void) const       {return D();}
  operator    const char*(void) const  {return S();}
  operator    const Hcpar&(void)const  {return HCPAR();}
  // Access contents
  double          D() const;
  Hlong           L() const;
  int             I() const;
  const char *    S() const;
  const Hcpar&    HCPAR()const;
  // Arithmetics
  HCtrlVal     operator + (const HCtrlVal &val) const;
  HTuple       operator + (const HTuple &val) const;
  HCtrlVal     operator - (const HCtrlVal &val) const;
  HTuple       operator - (const HTuple &val) const;
  HCtrlVal     operator * (const HCtrlVal &val) const;
  HTuple       operator * (const HTuple &val) const;
  HCtrlVal     operator / (const HCtrlVal &val) const;
  HTuple       operator / (const HTuple &val) const;
  HCtrlVal     operator % (const HCtrlVal &val) const;
  HTuple       operator % (const HTuple &val) const;
  HBool        operator != (const HCtrlVal &val) const;
  HBool        operator != (const HTuple &val) const;
  HBool        operator == (const HCtrlVal &val) const;
  HBool        operator == (const HTuple &val) const;
  HBool        operator >= (const HCtrlVal &val) const;
  HBool        operator >= (const HTuple &val) const;
  HBool        operator <= (const HCtrlVal &val) const;
  HBool        operator <= (const HTuple &val) const;
  HBool        operator > (const HCtrlVal &val) const;
  HBool        operator > (const HTuple &val) const;
  HBool        operator < (const HCtrlVal &val) const;
  HBool        operator < (const HTuple &val) const;

  const char *ClassName(void) const { return "HCtrlVal"; }
  int Version(void) const;
  int Revision(void) const;
  const char *Creation(void) const;
  
private:
  // Data
  Hcpar val;        // Value: one of the three types and type specifyer
  // Support operationen
  void ClearCtrlVal();
  void CopyCtrlVal(const HCtrlVal& source);
};
typedef struct
{
  Hpar   par;             /* values                                          */
  INT1   type;            /* type flag                                       */
} Hcpar; /* parameter passing for the C interface           */
typedef union 
{
  INT4_8  l;              /* 4/8 byte integer                       (input)  */
  double  f;              /* 8 byte real                            (input)  */
  char    *s;             /* pointer to strings                     (input)  */
} Hpar;                   /* parameter passing for the C interface           */
typedef union 
{
  INT4_8  *l;             /* 4/8 byte integer                       (output) */
  double  *f;             /* 8 byte real                            (output) */
  char    *s;             /* pointer to strings                     (output) */
  VOIDP   p;              /* pointer to var. of any type (e.g. tuple)(output)*/
} Hvar;                   /* parameter passing for the C interface           */

 

  仔細看我用紅色粗體並加大的部分,這四段代碼可以說是halcon維護HTuple這種數據類型的精髓了。下面我們來講解一下:

  首先HTuple類中有私有成員變量:

private:
  HCtrlVal    *tuple;       // values (array of Hlong/float/string)

  halcon給的注釋寫的很清楚,tuple是一群值,指向一個數組,數組里面有long型,浮點型及字符串型數據。這是一個指針,這個類就是維護這樣一個指針,具體此指針的內容,我們往下看HCtrlVal: (這里說一下這幾個單詞的意義吧:H->Halcon   Ctrl->Control   Val->Values  表示Halcon的控制變量,當然還有圖形變量,以后再講吧。)

private:
  // Data
  Hcpar       val;        // Value: one of the three types and type specifyer

  HCtrlVal類就維護了這樣一個成員變量,halcon給的注釋是說 val 代表數據的三種類型中的一個,並指向一個值。那么HTuple中的tuple指針就是維護了val組成的鏈表,這樣HTuple就可以維護多種不同類型的數據。

  HTuple用起來的確很方便,halcon對其進行了大量的運算符重載包括像強制類型轉換,都不需要我們手動去做,只需要在前面加個數據類型就行了。

  好了,由於本人水平有限,文中可能會有紕漏,敬請指出!

  added by xiejl

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM