SDC C-Project CF Review 프로그램
LYW
2021-09-27 b9b6752e83c701cc67241923d2b74dc3a963d243
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// GlassAlign.h: interface for the CGlassAlign class.
//
//////////////////////////////////////////////////////////////////////
 
#if !defined(AFX_GLASSALIGN_H__A4EDE408_69C7_440F_9842_CCDFAA98048A__INCLUDED_)
#define AFX_GLASSALIGN_H__A4EDE408_69C7_440F_9842_CCDFAA98048A__INCLUDED_
 
#include "Math.h"
 
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
 
 
 
// ¿øÁ¡ÀÇ À§Ä¡ ¿Í xÃàÀÇ ¹æÇâ¿¡ ´ëÇÑ Á¤ÀÇ.
// ¹Ý ½Ã°è¹æÇâ ¼³Á¤, ¼øÂ÷ÀûÀ¸·Î ¾Õ¼ø¼­ÀÇ µÞÀÚ¸¦ ½ÃÀÛ ±ÛÀڷΠÁöÁ¤.
enum OriginPos{OP_LEFT_TOP, OP_LEFT_BOTTOM, OP_RIGHT_BOTTOM, OP_RIGHT_TOP, OP_COUNT};
enum XDirection{XD_HORIZONTAL= 0, XD_VERTICAL= 1, XD_COUNT};
 
 
 
template <typename TTT>
class CCoordSystem
{
public:
    OriginPos    m_OriginPos;
    XDirection    m_XDirection;
    TTT            m_Width;
    TTT            m_Height;
 
public:
    CCoordSystem()
    {
        m_OriginPos= OP_LEFT_TOP;
        m_XDirection= XD_HORIZONTAL;
        m_Width= 0;
        m_Height= 0;
    }
    CCoordSystem(OriginPos pos, XDirection xDir, TTT width, TTT height)
    {
        SetSystem(pos, xDir, width, height);
    }
    void SetSystem(OriginPos pos, XDirection xDir, TTT width, TTT height)
    {
        m_OriginPos        = pos;
        m_XDirection    = xDir;
        m_Width            = width;
        m_Height        = height;
    }
 
    BOOL operator ==(CCoordSystem<TTT> &comp)
    {
        return m_OriginPos == comp.m_OriginPos && m_XDirection == comp.m_XDirection;
    }
};
 
// x¿Í yÀÇ ºÎÈ£ º¯È¯,, x¿Í yÀǠġȯÀº xÃàÀÇ ¹æÇâ º¸°í °áÁ¤.
static int transx[4][4][2]= {     {{1, 1},    {1, -1},    {-1, -1},    {-1, 1}} // 1 : (1~4) = Src(1) : Target(1~4)
,{{1, -1},    {1, 1},        {-1, 1},    {-1, -1}} // 2 : (1~4)
,{{-1, -1},    {-1, 1},    {1, 1},        {1, -1}} // 3 : (1~4)
,{{-1, 1},    {-1, -1},    {1, -1},    {1, 1}} // 4 : (1~4)
};
 
 
// [pox, xdir]ÀÇ °öÀ¸·Î ÁÂÇ¥°è º¯È¯ÀÇ °á°ú¸¦ ¹Ý¿µÇÑ´Ù.
// [LEFT_BOTTOM, XD_HORIZONTAL]=1, ¿¡¼­ [RIGHT_BOTTOM, XD_VERTICAL]= 1 À̸é 1*1 = 1 À̵ǹǷΠTHETAÀÇ ÇÕÀÌ ÃÖÁ¾ THETA°ªÀÌ µÈ´Ù.
static int thetasign[4][2]= {{-1, 1}, {1, -1}, {-1, 1}, {1, -1} };
 
 
#define TRANS_PT(trans, pt, targetPt) trans.TransToTarget(pt.x, pt.y, targetPt.x, targetPt.y);
#define ROTATE_PT(trans, pt, targetPt) trans.RotateToTarget(pt.x, pt.y, targetPt.x, targetPt.y);
 
template <typename TTT>
class CCoordTrans
{
    CCoordSystem<TTT>    m_SrcSystem;
    CCoordSystem<TTT>    m_TargetSystem;
 
 
    int a, b, width, height;// ¹æÇâ Àüȯ¿ë flag
 
public:
    BOOL IsNeedeToTrans()
    {
        if(m_SrcSystem == m_TargetSystem)
            return FALSE;// ÁÂÇ¥°è°¡ °°À¸¸é º¯È¯ Çʿ䠾øÀ½.
        return TRUE;
    }
 
    BOOL SetTransSystem(OriginPos srcPos, XDirection srcXDir, TTT width, TTT height, OriginPos targetPos, XDirection targetXDir)
    {
        m_SrcSystem.SetSystem(srcPos, srcXDir, width, height);
        m_TargetSystem.SetSystem(targetPos, targetXDir, width, height);
        SetTransSystem(m_SrcSystem, m_TargetSystem);
 
        return IsNeedeToTrans();
    }
 
    BOOL SetTransSystem(CCoordSystem<TTT> &src, CCoordSystem<TTT> &target)
    {
        m_SrcSystem= src;
        m_TargetSystem= target;
 
 
        // x= a*x+ width;
        // y= b*y+ height;
 
 
        if(m_SrcSystem.m_XDirection == XD_HORIZONTAL)// HORIZONTALÀ» ±âÁØÀ¸·Î Å×ÀÌºí ¸¸µé¾îÁü.
        {
            a= transx[m_SrcSystem.m_OriginPos][m_TargetSystem.m_OriginPos][0];
            b= transx[m_SrcSystem.m_OriginPos][m_TargetSystem.m_OriginPos][1];
        }else
        {
            b= transx[m_SrcSystem.m_OriginPos][m_TargetSystem.m_OriginPos][0];
            a= transx[m_SrcSystem.m_OriginPos][m_TargetSystem.m_OriginPos][1];
        }
 
 
        if(a < 0)
            width= m_SrcSystem.m_Width;
        else
            width= 0;
 
        if(b < 0)
            height= m_SrcSystem.m_Height;
        else
            height= 0;
 
        if(m_SrcSystem.m_XDirection == m_TargetSystem.m_XDirection)
        {
            target.m_Width= m_TargetSystem.m_Width= m_SrcSystem.m_Width;
            target.m_Height= m_TargetSystem.m_Height= m_SrcSystem.m_Height;
        }else
        {
            target.m_Width= m_TargetSystem.m_Width= m_SrcSystem.m_Height;
            target.m_Height= m_TargetSystem.m_Height= m_SrcSystem.m_Width;
        }
 
        return IsNeedeToTrans();
    }
    void TransToTarget(TTT x, TTT y, TTT &tx, TTT &ty)
    {
        // Ãà ¹æÇâ Àüȯ.(¹«º¯È­, x2= width- x, y2= height- y)
        x= x*a+ width;
        y= y*b+ height;
 
        // Ãà°£ Àüȯ. (¹«º¯È­, x <-> y)
        if(m_SrcSystem.m_XDirection == m_TargetSystem.m_XDirection)
        {
            tx= x;
            ty= y;
        }else
        {
            tx= y;
            ty= x;
        }
    }
    void RotateToTarget(TTT x, TTT y, TTT &tx, TTT &ty)
    {
        // Ãà ¹æÇâ Àüȯ.(¹«º¯È­, x2= width- x, y2= height- y)
        x= x*a;
        y= y*b;
 
        // Ãà°£ Àüȯ. (¹«º¯È­, x <-> y)
        if(m_SrcSystem.m_XDirection == m_TargetSystem.m_XDirection)
        {
            tx= x;
            ty= y;
        }else
        {
            tx= y;
            ty= x;
        }
    }
    // 1 or -1, theta°ªÀÇ ºÎÈ£¸¦ °áÁ¤ÇÑ´Ù.
    int    GetThetaSign()
    {
        int srcDir= thetasign[m_SrcSystem.m_OriginPos][m_SrcSystem.m_XDirection];
        int tarDir= thetasign[m_TargetSystem.m_OriginPos][m_TargetSystem.m_XDirection];
        return srcDir*tarDir;
    }
};
 
 
// ¾ó¶óÀΠ¸¶Å©¸¦ ÀÌ¿ëÇÑ ÁÂÇ¥ º¸Á¤.
// µÎ°³ÀÇ ¾ó¶óÀΠ¸¶Å©Áß ÇѰ³ÀÇ ¾ó¶óÀΠ¸¶Å©¸¦ ±âÁØÁ¡À¸·Î theta »ç¿ë.
// Á¤º¸ : ¾ó¶óÀθ¶Å©(¾ó¶óÀΠ±âÁØÁ¡)ÀÇ x, y, dx, dy(shiftÀ§Ä¡), theta(Ʋ¾îÁø °¢)
// ¾ó¶óÀΠ¸¶Å©±âÁØ¿¡ ÀÇÇÑ º¸Á¤°ú, ¿øÁ¡º¸Á¤À» ÅëÇÑ º¸Á¤ µÎ°¡Áö ¹æ¹ý »ç¿ë °¡´É.
// 1. SetAlignData
// 2. If Istilted(), then Correct()..
 
struct stAlignPoint
{
    int    x, y;
    stAlignPoint()            {ResetPoint();}
    void    ResetPoint()    {x= -1; y= -1;}// À½¼ö¸¦ °¡Áú ¼ö ¾ø´Ù.
    BOOL    IsPointSet()    {return x != -1 && y!= -1;}
    void    SetPoint(int xx, int yy)    {x= xx; y= yy;}
 
    stAlignPoint operator +(stAlignPoint& pt)
    {
        stAlignPoint npt;
        npt.x= x+ pt.x;
        npt.y= y+ pt.y;
        return npt;
    }
    stAlignPoint operator -(stAlignPoint& pt)
    {
        stAlignPoint npt;
        npt.x= x- pt.x;
        npt.y= y- pt.y;
        return npt;
    }
    void    Scale(double rx, double ry)
    {
        x= (int)(x*rx);
        y= (int)(y*ry);
    }
    stAlignPoint operator-()
    {
        stAlignPoint pt;
        pt.x= -x;
        pt.y= -y;
        return pt;
    }
};
 
struct stAlignInfo
{
    BOOL            m_bAlignFound;
 
    int                m_xAlign, m_yAlign;        // Align Position(x, y)
    int                m_dxAlign, m_dyAlign;    // Align Shift.
    double            m_Theta;                // Angle of Glass Tilt == Align Tilt.
    double            m_BasicTheta;
    double            m_ExtraTheta;
 
public:
    void    ResetAlignInfo()    {m_bAlignFound= FALSE;    m_Theta= 0;    m_dxAlign= 0; m_dyAlign= 0; m_xAlign= m_yAlign= 0;}
 
    void    SetAlignFound()        {m_bAlignFound= TRUE;}
    BOOL    IsAlignFound()        {return m_bAlignFound;}
 
    BOOL    IsTilted()            {return    m_Theta != 0 || m_dxAlign != 0 || m_dyAlign != 0;}
 
public:
    stAlignInfo&    SelfAlignInfo()        {return *this;}
};
 
 
 
 
class AFX_EXT_CLASS CAlignFind
{
public:
 
    // 1. ±Û¶ó½º »ó¿¡¼­ÀÇ ¾ó¶óÀθ¶Å© À§Ä¡¿Í Á¤º¸.
    CCoordSystem<int>    m_csOperator;        // ±âÁØ ÁÂÇ¥°è (OP_LEFT_TOP, XD_HORIZONTAL : ÀÛ¾÷ÀÚ ½Ã°¢, ºÒº¯), SetAlignInGlass ¿¡¼­ ¼³Á¤µÈ´Ù.
 
    stAlignPoint        m_FirstInGlass;            // um ´ÜÀ§
    stAlignPoint        m_SecondInGlass;        // um ´ÜÀ§
    stAlignPoint        m_FirstFindInGlass;
    stAlignPoint        m_SecondFindInGlass;
 
    double                m_BasicTheta;            // ¼³Á¤ ¾ó¶óÀΠ¸¶Å© À§Ä¡°¡ °¡Áø °íÀ¯ tilt
    double                m_ExtraTheta;
    double                m_TotalTheta;
    stAlignPoint        m_AlignShift;
    stAlignPoint        m_AlignDelta;
 
 
    // 2. debugÀ» À§Çؼ­ ÀúÀ常 ÇصдÙ. Ä«¸Þ¶ó¿¡¼­ÀÇ ¸¶Å© Á¤º¸. ¿ÜºÎ¿¡¼­ ¼³Á¤µÇ´Â ÀԷ°ªµéÀÌ´Ù. 
protected:
    stAlignPoint        m_FirstAlign;            // ±âÁØ ¾ó¶óÀΠ¸¶Å© À§Ä¡. um ´ÜÀ§
    stAlignPoint        m_FirstFind;            // ±âÁØ ¾ó¶óÀΠ¸¶Å© Ã£Àº °÷. um ´ÜÀ§
    CCoordSystem<int>    m_csFirst;
 
    stAlignPoint        m_SecondAlign;            // º¸Á¶ ¾ó¶óÀΠ¸¶Å© À§Ä¡. um ´ÜÀ§
    stAlignPoint        m_SecondFind;            // º¸Á¶ ¾ó¶óÀΠ¸¶Å© Ã£Àº °÷. um ´ÜÀ§
    CCoordSystem<int>    m_csSecond;
 
 
    // 3. debugÀ» À§Çؼ­ ÀúÀ常 ÇصдÙ. ±Û¶ó½º »ó ¾ó¶óÀΠ¸¶Å©ÀÇ À§Ä¡¿Í ÁÂÇ¥.
protected:
    int                    m_Dx, m_Dy;                    // ±Û¶ó½º ³¡¿¡¼­ ¾ó¶óÀΠ¸¶Å© ±îÁöÀÇ °Å¸®..
    OriginPos            m_FirstPos;                    // ¾ó¶óÀΠ¸¶Å© À§Ä¡.
    OriginPos            m_SecondPos;                // ¾ó¶óÀΠ¸¶Å© À§Ä¡.
public:
    int                    m_GlassWidth, m_GlassHeight;// ±Û¶ó½º Å©±â.
 
 
public:
    CAlignFind();
    CCoordSystem<int>    GetOperatorCoordSystem(){return m_csOperator;}
    void ResetAlignFind()    {    m_FirstFindInGlass.ResetPoint(); m_SecondFindInGlass.ResetPoint();}
    BOOL IsAlignFound()    {    return m_FirstFindInGlass.IsPointSet() && m_SecondFindInGlass.IsPointSet();}
 
 
    // 1. °Å¸® Á¤º¸·Î ±Û¶ó½º ¾ó¶óÀθ¶Å© À§Ã³ ±¸Çϱâ, ±Û¶ó½º ³¡´Ü(left, top)¿¡¼­ÀÇ ¾ó¶óÀθ¶Å© °Å¸® dx, dy¿Í glass Width, height¸¦ °¡Áö°í °¢ ¾ó¶óÀθ¶Å© À§Ä¡ ÁÂÇ¥ ±¸Çϱâ.
    void FillPos(int &x, int &y, OriginPos pos, int dx, int dy, int glassWidth, int glassHeight);
public:
    // 2. ±Û¶ó½ºÀÇ ¾ó¶óÀθ¶Å© Á¤º¸ ¼³Á¤. ´ÜÀ§ um, ÀÛ¾÷ÀÚ ¹æÇâ¿¡¼­ÀÇ left, topÀ» ¿øÁ¡À¸·Î xDirectionÀº HorizontalÀ» ±âÁØÀ¸·Î Çϴ µ¥ÀÌÅÍ´Ù.
    void SetAlignInGlass(int dx, int dy, int glassWidth, int glassHeight, OriginPos firstPos, OriginPos secondPos, double basicTheta);
 
    // 3. ¾ó¶óÀθ¶Å© Á¤º¸ ¼³Á¤.. ´ÜÀ§ um, Ä«¸Þ¶ó¿¡¼­ÀÇ À§Ä¡¿Í ÁÂÇ¥°èÁ¤º¸.
    void SetFirstAlign(stAlignPoint first, stAlignPoint find, double rx, double ry, CCoordSystem<int> cameraCoord);
    void SetSecondAlign(stAlignPoint second, stAlignPoint find, double rx, double ry, CCoordSystem<int> cameraCoord);
 
 
    // 4. ¾ó¶óÀΠº¸Á¤°ª ±¸Çϱâ.
    BOOL CalcAlignInfo(CCoordSystem<int> &targetCoord, stAlignInfo &alignInfo);// operatorÁÂÇ¥°è·Î ¼³Á¤µÈ ¾ó¶óÀθ¶Å©·Î theta°è»êÈÄ targetCoordÁÂÇ¥°è·Î °ªÀ» È¯»êÇØ ÁØ´Ù.
    double CalcAlignTheta_Operator(stAlignPoint delta, OriginPos opSrc, XDirection xdSrc);// operatorÁÂÇ¥°è¿¡¼­ÀÇ theta°ª ¸®ÅÏ.
    double CalcAlignTheta_Operator(stAlignPoint a1, stAlignPoint a2, OriginPos opSrc, XDirection xdSrc)
    {
        return CalcAlignTheta_Operator(a2- a1, opSrc, xdSrc);
    }
};
 
 
// ¾ó¶óÀΠ¸¶Å©ÀÇ shift¿Í theta ¸¦ ÀÌ¿ëÇØ ÁÂÇ¥ °è»êÇϱâ.
// ¾ó¶óÀθ¶Å©ÀÇ shift¸¦ ÀÌ¿ëÇØ ¿øÁ¡ÀÇ shift·®À» °è»êÇÑ´Ù. theta´Â µ¿ÀÏ. SetAlignData
// ¿øÁ¡¿¡¼­ÀÇ tilt(shift, theta)¸¦ ÀÌ¿ëÇØ ÀÓÀÇ ÁÂÇ¥ÀÇ tiltµÈ ÁÂÇ¥¸¦ °è»êÇØ³½´Ù.
// ÁÂÇ¥´Â umÀ» ±âº» ´ÜÀ§·Î »ç¿ëÇÑ´Ù.
class AFX_EXT_CLASS CBaseAligning : public stAlignInfo
{
protected:
    // AlignData¿¡¼­ 2Â÷ °¡°øµÇ´Â Á¤º¸.
    double            m_dCosine;                // Cosine(Theta)
    double            m_dSine;                // Sine(Theta)
    double            m_dTangent;
 
    double            m_dCosineBack;                // Cosine(Theta)
    double            m_dSineBack;                // Sine(Theta)
 
    int                m_dxOrigin, m_dyOrigin;    // Shift of Glass Original Position
 
public:
    CBaseAligning();
 
 
    void    SetAlignInfo(stAlignInfo &alignData);
    void    SetAlignInfo(double theta, int xAlign, int yAlign, int dxAlign, int dyAlign);
 
    void    Correct_00(int x, int y, int &xPrime, int &yPrime);        // ÁÂÇ¥ ±âÁØÀ» ¾ó¶óÀθ¶Å© ±âÁØÀÌ ¾Æ´Ï¶ó ¿øÁ¡±âÁØÀ¸·Î º¯È¯ - ¿¬»ê°³¼ö°¡ ÁØ´Ù.
    void    Correct_00(int &x, int &y)    {Correct_00(x, y, x, y);}
 
    void    Correct(int x, int y, int &xPrime, int &yPrime, int bForwardScan, int nGlassHeight);        // x, y¸¦ ¾ó¶óÀθ¶Å© ±âÁØÀ¸·Î ÁÂÇ¥º¯È¯ÈĠƿƮ º¸Á¤ ¹× ±Û¶ó½º ÁÂÇ¥°è·Î º¹¿ø.
    void    Correct(int &x, int &y, int bForwardScan, int nGlassHeigh)        {Correct(x, y, x, y, bForwardScan, nGlassHeigh);}
};
 
 
 
 
// When use it(Correct_Scan()), first check if belows are called 
// 1. Reset(), 2. SetAlignData(), 3. SetScanData() 4. IsTilted()
class AFX_EXT_CLASS CScanAligning : public CBaseAligning
{
public:
    double        m_xResolution;            // Resolution of X Axis ( Inspector cam)
    double        m_yResolution;            // Resolution of Y Axis ( Inspector cam)
    double        m_xAccum;                // UM of Cam's Left First Pixel (Left Margin)
    double        m_xAccumOrigin;            // UM of Cam's Left Pixel (0)
    double        m_yAccum;                // UM of Cam's Left First Pixel (Left Margin)
    double        m_yAccumOrigin;            // UM of Cam's Left Pixel (0)
    int            m_LeftMargin;            // Left Margin of Each Cam.
    BOOL        m_bForwardScan;
 
    int            m_GlassHeight;            // Length of Glass Height
    int            m_TopMargin;            // Top Margin of Each Cam.
public:
    void    SetScanData(double xResolution, double yResolution, double xAccum, double yAccum, int leftMargin, int glassHeight, int glassStartLine, BOOL bForward);
    void    Correct_Pixel2UM(int x, int y, int &xPrime, int &yPrime);
    void    Correct_Pixel2Pixel(int x, int y, int &xPrime, int &yPrime);
    void    CorrectBack_Pixel2UM(int x, int y, int &xPrime, int &yPrime);
    void    CorrectBack_Pixel2Pixel(int x, int y, int &xPrime, int &yPrime);
 
    int        GetDx_Pixel(int x, int y);
    int        GetDy_Pixel(int x, int y);
 
};
 
// CCoordTrans ¿ë·Ê. ÁÂÇ¥°è º¯È¯¿¡ µû¸¥ ÁÂÇ¥ º¯È¯ ¿¹.
void Example_CoordTrans();
 
// ½ºÄµ¿¡¼­ ±Û¶ó½º·Î ÁÂÇ¥ º¯È¯°ªÀÌ ¿Ã¹Ù¸¥Áö Å×½ºÆ®Çغ¸ÀÚ.
void Test_MakeScanAlignData(CScanAligning &alining);
 
#endif // !defined(AFX_GLASSALIGN_H__A4EDE408_69C7_440F_9842_CCDFAA98048A__INCLUDED_)