00001 #include "filter.h"
00002
00003 TrackingFilter::TrackingFilter(float angle, float shift)
00004 {
00005 this->angleFilter = angle;
00006 this->distFilter = shift;
00007 }
00008
00009 TrackingFilter::TFilterEntry * TrackingFilter::findEntry(Body::BodyType btype, int bid)
00010 {
00011 std::list<TFilterEntry>::iterator i;
00012 for (i = table.begin(); i!= table.end(); i++)
00013 {
00014 if (((*i).id == bid) && ((*i).type == btype)) return &(*i);
00015 }
00016 return NULL;
00017 }
00018 bool TrackingFilter::checkTranslation(MATRIX currMatrix, MATRIX prevMatrix, float shift)
00019 {
00020 if (absolute(currMatrix._m41 - prevMatrix._m41) >= shift
00021 || absolute(currMatrix._m42 - prevMatrix._m42) >= shift
00022 || absolute(currMatrix._m43 - prevMatrix._m43) >= shift)
00023 {
00024
00025 return true;
00026 }
00027 else
00028 {
00029
00030 return false;
00031 }
00032 }
00033 bool TrackingFilter::checkRotation(MATRIX currMatrix, MATRIX prevMatrix, float angle)
00034 {
00035 float cosA = cos(angle);
00036 float cy2 = cosf(asinf(currMatrix._m13));
00037 float cy1 = cosf(asinf(prevMatrix._m13));
00038 float cosX = (currMatrix._m33 / cy2)*(prevMatrix._m33 / cy1) + (currMatrix._m23 / (-1 * cy2)) * (prevMatrix._m23 / (-1 * cy1));
00039 float cosY = (cy2 * cy1) + (currMatrix._m13 * prevMatrix._m13);
00040 float cosZ = ((currMatrix._m11 / cy2) * (prevMatrix._m11 / cy1)) + ((currMatrix._m12 / (-1 * cy2)) * (prevMatrix._m12 / (-1 * cy1)));
00041 if (cosA >= cosX || cosA >= cosY || cosA >= cosZ)
00042 {
00043
00044 return true;
00045 }
00046 else
00047 {
00048
00049 return false;
00050 }
00051 }
00052 bool TrackingFilter::Filter(Body *body)
00053 {
00054 TrackingFilter::TFilterEntry * entry = findEntry(body->GetType(), body->GetID());
00055 if (entry == NULL)
00056 {
00057
00058 TrackingFilter::TFilterEntry newEntry;
00059 newEntry.id = body->GetID();
00060 newEntry.type = body->GetType();
00061 newEntry.prevMatrix = body->Matrix();
00062 newEntry.angleFilter = this->angleFilter;
00063 newEntry.distFilter = this->distFilter;
00064 newEntry.validMatrix = true;
00065 table.push_back(newEntry);
00066
00067 return false;
00068 }
00069 else if (entry->validMatrix)
00070 {
00071
00072
00073 if (body->GetQuality() != 1)
00074 {
00075
00076
00077
00078 body->SetMatrix(entry->prevMatrix);
00079 return false;
00080 }
00081 else
00082 {
00083 bool filt1 = false;
00084 MATRIX filterM;
00085 if (!checkRotation(body->Matrix(), entry->prevMatrix, entry->angleFilter))
00086 {
00087
00088
00089 filterM = entry->prevMatrix.ExtractRotation();
00090 filt1 = true;
00091 }
00092 else
00093 {
00094
00096 filterM = body->Matrix().ExtractRotation();
00097 }
00098 if (!checkTranslation(body->Matrix(), entry->prevMatrix, entry->distFilter))
00099 {
00100
00101 filterM.Translate(entry->prevMatrix.ExtractTranslation());
00102 }
00103 else
00104 {
00105
00106 filterM.Translate(body->Matrix().ExtractTranslation());
00107 filt1 = false;
00108 }
00109 entry->prevMatrix = body->Matrix() = filterM;
00110 return filt1;
00111 }
00112 return true;
00113 }
00114 else
00115 {
00116
00117 entry->prevMatrix = body->Matrix();
00118 entry->validMatrix = true;
00119 return false;
00120 }
00121 }
00122
00123 bool TrackingFilter::UpdateFilter(Body::BodyType type, int id, float angle, float shift)
00124 {
00125 TrackingFilter::TFilterEntry * entry = this->findEntry(type, id);
00126 if (entry == NULL)
00127 {
00128 TFilterEntry new_entry;
00129 new_entry.angleFilter = angle;
00130 new_entry.distFilter = shift;
00131 new_entry.id = id;
00132 new_entry.type = type;
00133 new_entry.validMatrix = false;
00134 table.push_back(new_entry);
00135 return false;
00136 }
00137 else
00138 {
00139 entry->angleFilter = angle;
00140 entry->distFilter = shift;
00141 return true;
00142 }
00143 }
00144 wxString TrackingFilter::GetAsString(const char *lineSeparator, const char *atributeSeparator)
00145 {
00146 wxString dump = wxT("");
00147 size_t max = this->table.size();
00148 if (max ==0) return wxT("EMPTY");
00149 std::list<TFilterEntry>::iterator i, j;
00150 for (i =table.begin(); i != table.end(); i++)
00151 {
00152 if (i != table.begin()) dump += wxString::From8BitData(lineSeparator);
00153 TFilterEntry * entry = &(*i);
00154 dump += wxString::Format(wxT("%i%s%i%s%f%s%f"),
00155 entry->type, atributeSeparator,
00156 entry->id, atributeSeparator,
00157 entry->angleFilter, atributeSeparator,
00158 entry->distFilter);
00159 }
00160 return dump;
00161 }