ACT-CV - Machine Vision for Cognitive Modeling
Pos.h
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4; coding: iso-8859-1; -*-
2 
3 /*
4 ACT-CV - Machine Vision for Cognitive Modeling
5 Copyright (c) 2008 Marc Halbruegge
6 
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
22 
23 #ifndef __POS_H
24 #define __POS_H
25 
32 #include <iostream>
33 #define _USE_MATH_DEFINES
34 #include <cmath>
35 
36 #include "DMatrix.h"
37 
42 template<unsigned int dims, class _t = int>
43 class Pos {
44  _t pos[dims];
45 public:
46 
47  Pos() {
48  for (unsigned int i=0;i<dims;i++) pos[i]=0;
49  }
50 
51  explicit Pos(_t x0, _t x1, _t x2) {
52  pos[0]=x0;
53  pos[1]=x1;
54  pos[2]=x2;
55  }
56  explicit Pos(_t x0, _t x1) {
57  pos[0]=x0;
58  pos[1]=x1;
59  }
60  explicit Pos(_t x0) {
61  pos[0]=x0;
62  }
63 
64  Pos(const Pos<dims,_t> &p) {
65  for (unsigned int i=0;i<dims;i++) pos[i]=p.pos[i];
66  }
67 
68  explicit Pos(const DMatrix<dims, 1> &d) {
69  for (unsigned int i=0;i<dims;i++) pos[i]=d[i][0];
70  }
71 
73  void Zero() {
74  for (unsigned int i=0;i<dims;i++) {
75  pos[i]=static_cast<_t>(0);
76  }
77  }
78 
79  _t& operator[] (unsigned int i) {
80  return pos[i];
81  }
82 
83  _t operator[] (unsigned int i) const {
84  return pos[i];
85  }
86 
88  for (unsigned int i=0;i<dims;i++) pos[i]=p.pos[i];
89  return *this;
90  }
91 
93  for (unsigned int i=0;i<dims;i++) pos[i]+=p.pos[i];
94  return *this;
95  }
96 
98  for (unsigned int i=0;i<dims;i++) pos[i]-=p.pos[i];
99  return *this;
100  }
101 
102  Pos& operator /= (const _t &skalar) {
103  for (unsigned int i=0;i<dims;i++) pos[i]/=skalar;
104  return *this;
105  }
106 
107  Pos& operator *= (const _t &skalar) {
108  for (unsigned int i=0;i<dims;i++) pos[i]*=skalar;
109  return *this;
110  }
111 
112  Pos operator + (const Pos<dims,_t> &p) const {
113  Pos result;
114  for (unsigned int i=0;i<dims;i++) result[i]=pos[i]+p.pos[i];
115  return result;
116  }
117 
118  Pos operator - (const Pos<dims,_t> &p) const {
119  Pos result;
120  for (unsigned int i=0;i<dims;i++) result[i]=pos[i]-p.pos[i];
121  return result;
122  }
123 
124  Pos operator * (const _t &skalar) const {
125  Pos result;
126  for (unsigned int i=0;i<dims;i++) result[i]=pos[i]*skalar;
127  return result;
128  }
129 
130  Pos operator / (const _t &skalar) const {
131  Pos result;
132  for (unsigned int i=0;i<dims;i++) result[i]=pos[i]/skalar;
133  return result;
134  }
135 
136  _t ScalProd (const Pos<dims,_t> &p) const {
137  _t result=0;
138  for (unsigned int i=0;i<dims;i++) result+=p[i]*pos[i];
139  return result;
140  }
141 
142  operator Pos<dims, double> () const {
143  Pos<dims, double> result;
144  for (unsigned int i=0;i<dims;i++) result[i]=pos[i];
145  return result;
146  }
147 
148  operator DMatrix<dims, 1> () const {
149  DMatrix<dims, 1> result;
150  for (unsigned int i=0;i<dims;i++) result[i][0]=pos[i];
151  return result;
152  }
153 
155  for (unsigned int i=0;i<dims;i++) pos[i]=d[i][0];
156  return *this;
157  }
158 };
159 
160 
161 
162 template<unsigned int dims, class _t>
163 bool operator == (const Pos<dims, _t> &a, const Pos<dims, _t> &b) {
164  for (unsigned int i=0;i<dims;i++) if (a[i]!=b[i]) return false;
165  return true;
166 }
167 
168 template<unsigned int dims>
170  DMatrix<dims,1> b2=b;
171  return a * b2;
172 }
173 
174 
175 template<unsigned int dims, class _t>
176 std::ostream& operator << (std::ostream &o, const Pos<dims, _t> &p) {
177  for (unsigned int i=0;i<dims;i++) {
178  o << p[i];
179  if (i+1<dims) o << " ";
180  }
181  return o;
182 }
183 
184 template<unsigned int dims, class _t>
185 std::istream& operator >> (std::istream &in, Pos<dims, _t> &p) {
186  for (unsigned int i=0;i<dims;i++) {
187  in >> p[i];
188  }
189  return in;
190 }
191 
192 template<unsigned int dims>
193 double Len(const Pos<dims, double> &p) {
194  double result=0;
195  for (unsigned int i=0;i<dims;i++) {
196  result+=p[i]*p[i];
197  }
198 #if MSC_VER<700
199  return sqrt(result);
200 #else
201  return std::sqrt(result);
202 #endif
203 }
204 
205 template<unsigned int dims>
206 float Len(const Pos<dims, float> &p) {
207  float result=0;
208  for (unsigned int i=0;i<dims;i++) {
209  result+=p[i]*p[i];
210  }
211 #if MSC_VER<700
212  return sqrt(result);
213 #else
214  return std::sqrt(result);
215 #endif
216 }
217 
218 template<unsigned int dims>
219 double Len(const Pos<dims, int> &p) {
220  double result=0;
221  for (unsigned int i=0;i<dims;i++) {
222  result+=p[i]*p[i];
223  }
224  return std::sqrt(result);
225 }
226 
227 template<unsigned int dims, typename _t>
228 _t ScalProd(const Pos<dims, _t> &p1, const Pos<dims, _t> &p2) {
229  _t result=0;
230  for (unsigned int i=0;i<dims;i++) {
231  result+=p1[i]*p2[i];
232  }
233  return result;
234 }
235 
236 
238 template<class _t>
239 bool Innerhalb(const Pos<4,_t> &rgn, const Pos<2,_t> &pos) {
240  return pos[0]>=rgn[0] && pos[0]<=rgn[2] &&
241  pos[1]>=rgn[1] && pos[1]<=rgn[3];
242 }
243 
245 template<unsigned int dims, class _t>
247  x.Zero();
248 };
249 
250 #endif


ACT-CV - Machine Vision for Cognitive Modeling
© 2015 Marc Halbruegge (actcvlibrary@googlemail.com)