Move the sources to trunk
[opencv] / cvaux / src / vs / blobtrackpostproclist.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //
12 // Copyright (C) 2000, Intel Corporation, all rights reserved.
13 // Third party copyrights are property of their respective owners.
14 //
15 // Redistribution and use in source and binary forms, with or without modification,
16 // are permitted provided that the following conditions are met:
17 //
18 //   * Redistribution's of source code must retain the above copyright notice,
19 //     this list of conditions and the following disclaimer.
20 //
21 //   * Redistribution's in binary form must reproduce the above copyright notice,
22 //     this list of conditions and the following disclaimer in the documentation
23 //     and/or other materials provided with the distribution.
24 //
25 //   * The name of Intel Corporation may not be used to endorse or promote products
26 //     derived from this software without specific prior written permission.
27 //
28 // This software is provided by the copyright holders and contributors "as is" and
29 // any express or implied warranties, including, but not limited to, the implied
30 // warranties of merchantability and fitness for a particular purpose are disclaimed.
31 // In no event shall the Intel Corporation or contributors be liable for any direct,
32 // indirect, incidental, special, exemplary, or consequential damages
33 // (including, but not limited to, procurement of substitute goods or services;
34 // loss of use, data, or profits; or business interruption) however caused
35 // and on any theory of liability, whether in contract, strict liability,
36 // or tort (including negligence or otherwise) arising in any way out of
37 // the use of this software, even if advised of the possibility of such damage.
38 //
39 //M*/
40
41 #include "_cvaux.h"
42
43 /*======================= FILTER LIST SHELL =====================*/
44 typedef struct DefBlobFilter
45 {
46     CvBlob                  blob;
47     CvBlobTrackPostProcOne* pFilter;
48     int                     m_LastFrame;
49 } DefBlobFilter;
50
51 class CvBlobTrackPostProcList : public CvBlobTrackPostProc
52 {
53 protected:
54     CvBlobTrackPostProcOne* (*m_CreatePostProc)();
55     CvBlobSeq               m_BlobFilterList;
56     int                     m_Frame;
57 public:
58     CvBlobTrackPostProcList(CvBlobTrackPostProcOne* (*create)()):m_BlobFilterList(sizeof(DefBlobFilter))
59     {
60         m_Frame = 0;
61         m_CreatePostProc = create;
62         CvBlobTrackPostProcOne* pM = create();
63         TransferParamsFromChild(pM,NULL);
64         pM->Release();
65     }
66     ~CvBlobTrackPostProcList()
67     {
68         int i;
69         for(i=m_BlobFilterList.GetBlobNum();i>0;--i)
70         {
71             DefBlobFilter* pF = (DefBlobFilter*)m_BlobFilterList.GetBlob(i-1);
72             pF->pFilter->Release();
73         }
74     };
75     virtual void    AddBlob(CvBlob* pBlob)
76     {
77         DefBlobFilter* pF = (DefBlobFilter*)m_BlobFilterList.GetBlobByID(CV_BLOB_ID(pBlob));
78         if(pF == NULL)
79         { /* create new filter */
80             DefBlobFilter F;
81             F.blob = pBlob[0];
82             F.m_LastFrame = m_Frame;
83             F.pFilter = m_CreatePostProc();
84             TransferParamsToChild(F.pFilter,NULL);
85             m_BlobFilterList.AddBlob((CvBlob*)&F);
86             pF = (DefBlobFilter*)m_BlobFilterList.GetBlobByID(CV_BLOB_ID(pBlob));
87         }
88
89         assert(pF);
90         pF->blob = pBlob[0];
91         pF->m_LastFrame = m_Frame;
92     };
93     virtual void    Process()
94     {
95         int i;
96         for(i=m_BlobFilterList.GetBlobNum();i>0;--i)
97         {
98             DefBlobFilter* pF = (DefBlobFilter*)m_BlobFilterList.GetBlob(i-1);
99             if(pF->m_LastFrame == m_Frame)
100             {/* process */
101                 int ID = CV_BLOB_ID(pF);
102                 pF->blob = *(pF->pFilter->Process(&(pF->blob)));
103                 CV_BLOB_ID(pF) = ID;
104             }
105             else
106             {/* delete blob filter */
107                 pF->pFilter->Release();
108                 m_BlobFilterList.DelBlob(i-1);
109             }
110         }/* next blob */
111         m_Frame++;
112     };
113     int     GetBlobNum(){return m_BlobFilterList.GetBlobNum();};
114     CvBlob* GetBlob(int index){return m_BlobFilterList.GetBlob(index);};
115     void    Release(){delete this;};
116
117     /* additional functionality */
118     CvBlob* GetBlobByID(int BlobID){return m_BlobFilterList.GetBlobByID(BlobID);}
119 }; /* CvBlobTrackPostProcList */
120
121 CvBlobTrackPostProc* cvCreateBlobTrackPostProcList(CvBlobTrackPostProcOne* (*create)())
122 {
123     return (CvBlobTrackPostProc*) new CvBlobTrackPostProcList(create);
124 }
125 /*======================= FILTER LIST SHELL =====================*/