#
source:
foam/trunk/vision/src/Vector.h
@
97

Revision 97, 6.4 KB checked in by dave, 11 years ago (diff) |
---|

Line | |
---|---|

1 | // Copyright (C) 2009 foam |

2 | // |

3 | // This program is free software; you can redistribute it and/or modify |

4 | // it under the terms of the GNU General Public License as published by |

5 | // the Free Software Foundation; either version 2 of the License, or |

6 | // (at your option) any later version. |

7 | // |

8 | // This program is distributed in the hope that it will be useful, |

9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |

10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |

11 | // GNU General Public License for more details. |

12 | // |

13 | // You should have received a copy of the GNU General Public License |

14 | // along with this program; if not, write to the Free Software |

15 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |

16 | |

17 | #include <assert.h> |

18 | #include <math.h> |

19 | #include <iostream> |

20 | |

21 | #ifndef FOAM_VECTOR |

22 | #define FOAM_VECTOR |

23 | |

24 | template<class T> |

25 | bool feq(T a, T b, T t=0.001) |

26 | { |

27 | return fabs(a-b)<t; |

28 | } |

29 | |

30 | template<class T> |

31 | class Vector |

32 | { |

33 | public: |

34 | Vector(); |

35 | Vector(unsigned int s); |

36 | ~Vector(); |

37 | Vector(const Vector &other); |

38 | |

39 | T &operator[](unsigned int i) |

40 | { |

41 | assert(i<m_Size); |

42 | return m_Data[i]; |

43 | } |

44 | |

45 | const T &operator[](unsigned int i) const |

46 | { |

47 | assert(i<m_Size); |

48 | return m_Data[i]; |

49 | } |

50 | |

51 | unsigned int Size() const { return m_Size; } |

52 | T *GetRawData() { return m_Data; } |

53 | const T *GetRawDataConst() const { return m_Data; } |

54 | |

55 | void Print() const; |

56 | void SetAll(T s); |

57 | void Zero() { SetAll(0); } |

58 | bool IsInf(); |

59 | T Mean(); |

60 | T DistanceFrom(const Vector &other) const; |

61 | T Magnitude() const; |

62 | Vector Normalised() const; |

63 | |

64 | Vector &operator=(const Vector &other); |

65 | Vector operator+(const Vector &other) const; |

66 | Vector operator-(const Vector &other) const; |

67 | Vector operator+(T v) const; |

68 | Vector operator-(T v) const; |

69 | Vector operator*(T v) const; |

70 | Vector operator/(T v) const; |

71 | Vector &operator+=(const Vector &other); |

72 | Vector &operator-=(const Vector &other); |

73 | Vector &operator+=(T v); |

74 | Vector &operator-=(T v); |

75 | Vector &operator*=(T v); |

76 | Vector &operator/=(T v); |

77 | |

78 | void Save(FILE *f) const; |

79 | void Load(FILE *f); |

80 | |

81 | static void RunTests(); |

82 | |

83 | private: |

84 | |

85 | unsigned int m_Size; |

86 | T *m_Data; |

87 | }; |

88 | |

89 | template<class T> |

90 | Vector<T>::Vector() : |

91 | m_Size(0) |

92 | { |

93 | m_Data=NULL; |

94 | } |

95 | |

96 | template<class T> |

97 | Vector<T>::Vector(unsigned int s) : |

98 | m_Size(s) |

99 | { |

100 | m_Data=new T[s]; |

101 | } |

102 | |

103 | template<class T> |

104 | Vector<T>::~Vector() |

105 | { |

106 | delete[] m_Data; |

107 | } |

108 | |

109 | template<class T> |

110 | Vector<T>::Vector(const Vector &other) |

111 | { |

112 | m_Size = other.m_Size; |

113 | m_Data=new T[m_Size]; |

114 | memcpy(m_Data,other.m_Data,m_Size*sizeof(T)); |

115 | } |

116 | |

117 | template<class T> |

118 | Vector<T> &Vector<T>::operator=(const Vector &other) |

119 | { |

120 | if (m_Data!=NULL) |

121 | { |

122 | delete[] m_Data; |

123 | } |

124 | |

125 | m_Size = other.m_Size; |

126 | m_Data=new T[m_Size]; |

127 | memcpy(m_Data,other.m_Data,m_Size*sizeof(T)); |

128 | |

129 | return *this; |

130 | } |

131 | |

132 | template<class T> |

133 | void Vector<T>::Print() const |

134 | { |

135 | for (unsigned int i=0; i<m_Size; i++) |

136 | { |

137 | std::cerr<<(*this)[i]<<" "; |

138 | } |

139 | std::cerr<<std::endl; |

140 | } |

141 | |

142 | template<class T> |

143 | void Vector<T>::SetAll(T s) |

144 | { |

145 | for (unsigned int i=0; i<m_Size; i++) |

146 | { |

147 | (*this)[i]=s; |

148 | } |

149 | } |

150 | |

151 | template<class T> |

152 | bool Vector<T>::IsInf() |

153 | { |

154 | for (unsigned int i=0; i<m_Size; i++) |

155 | { |

156 | if (isinf((*this)[i])) return true; |

157 | if (isnan((*this)[i])) return true; |

158 | } |

159 | return false; |

160 | } |

161 | |

162 | template<class T> |

163 | T Vector<T>::DistanceFrom(const Vector &other) const |

164 | { |

165 | assert(m_Size==other.m_Size); |

166 | |

167 | float acc=0; |

168 | for (unsigned int i=0; i<m_Size; i++) |

169 | { |

170 | acc+=(other[i]-(*this)[i]) * (other[i]-(*this)[i]); |

171 | } |

172 | |

173 | return sqrt(acc); |

174 | } |

175 | |

176 | template<class T> |

177 | T Vector<T>::Magnitude() const |

178 | { |

179 | float acc=0; |

180 | for (unsigned int i=0; i<m_Size; i++) |

181 | { |

182 | acc+=(*this)[i] * (*this)[i]; |

183 | } |

184 | |

185 | return sqrt(acc); |

186 | } |

187 | |

188 | template<class T> |

189 | Vector<T> Vector<T>::Normalised() const |

190 | { |

191 | Vector<T> ret(*this); |

192 | ret/=ret.Magnitude(); |

193 | return ret; |

194 | } |

195 | |

196 | template<class T> |

197 | Vector<T> Vector<T>::operator+(const Vector &other) const |

198 | { |

199 | assert(m_Size==other.m_Size); |

200 | |

201 | Vector<T> ret(m_Size); |

202 | for (unsigned int i=0; i<m_Size; i++) |

203 | { |

204 | ret[i]=(*this)[i]+other[i]; |

205 | } |

206 | return ret; |

207 | } |

208 | |

209 | template<class T> |

210 | Vector<T> Vector<T>::operator-(const Vector &other) const |

211 | { |

212 | assert(m_Size==other.m_Size); |

213 | |

214 | Vector<T> ret(m_Size); |

215 | for (unsigned int i=0; i<m_Size; i++) |

216 | { |

217 | ret[i]=(*this)[i]-other[i]; |

218 | } |

219 | return ret; |

220 | } |

221 | |

222 | template<class T> |

223 | Vector<T> Vector<T>::operator+(T v) const |

224 | { |

225 | Vector<T> ret(m_Size); |

226 | for (unsigned int i=0; i<m_Size; i++) |

227 | { |

228 | ret[i]=(*this)[i]+v; |

229 | } |

230 | return ret; |

231 | } |

232 | |

233 | template<class T> |

234 | Vector<T> Vector<T>::operator-(T v) const |

235 | { |

236 | Vector<T> ret(m_Size); |

237 | for (unsigned int i=0; i<m_Size; i++) |

238 | { |

239 | ret[i]=(*this)[i]-v; |

240 | } |

241 | return ret; |

242 | } |

243 | |

244 | template<class T> |

245 | Vector<T> Vector<T>::operator*(T v) const |

246 | { |

247 | Vector<T> ret(m_Size); |

248 | for (unsigned int i=0; i<m_Size; i++) |

249 | { |

250 | ret[i]=(*this)[i]*v; |

251 | } |

252 | return ret; |

253 | } |

254 | |

255 | template<class T> |

256 | Vector<T> Vector<T>::operator/(T v) const |

257 | { |

258 | Vector<T> ret(m_Size); |

259 | for (unsigned int i=0; i<m_Size; i++) |

260 | { |

261 | ret[i]=(*this)[i]/v; |

262 | } |

263 | return ret; |

264 | } |

265 | |

266 | template<class T> |

267 | Vector<T> &Vector<T>::operator+=(const Vector &other) |

268 | { |

269 | assert(m_Size==other.m_Size); |

270 | for (unsigned int i=0; i<m_Size; i++) |

271 | { |

272 | (*this)[i]+=other[i]; |

273 | } |

274 | return *this; |

275 | } |

276 | |

277 | template<class T> |

278 | Vector<T> &Vector<T>::operator-=(const Vector &other) |

279 | { |

280 | assert(m_Size==other.m_Size); |

281 | for (unsigned int i=0; i<m_Size; i++) |

282 | { |

283 | (*this)[i]-=other[i]; |

284 | } |

285 | return *this; |

286 | } |

287 | |

288 | template<class T> |

289 | Vector<T> &Vector<T>::operator+=(T v) |

290 | { |

291 | for (unsigned int i=0; i<m_Size; i++) |

292 | { |

293 | (*this)[i]+=v; |

294 | } |

295 | return *this; |

296 | } |

297 | |

298 | template<class T> |

299 | Vector<T> &Vector<T>::operator-=(T v) |

300 | { |

301 | for (unsigned int i=0; i<m_Size; i++) |

302 | { |

303 | (*this)[i]-=v; |

304 | } |

305 | return *this; |

306 | } |

307 | |

308 | template<class T> |

309 | Vector<T> &Vector<T>::operator*=(T v) |

310 | { |

311 | for (unsigned int i=0; i<m_Size; i++) |

312 | { |

313 | (*this)[i]*=v; |

314 | } |

315 | return *this; |

316 | } |

317 | |

318 | template<class T> |

319 | Vector<T> &Vector<T>::operator/=(T v) |

320 | { |

321 | for (unsigned int i=0; i<m_Size; i++) |

322 | { |

323 | (*this)[i]/=v; |

324 | } |

325 | return *this; |

326 | } |

327 | |

328 | template<class T> |

329 | T Vector<T>::Mean() |

330 | { |

331 | T acc=0; |

332 | for (unsigned int i=0; i<m_Size; i++) |

333 | { |

334 | acc+=(*this)[i]; |

335 | } |

336 | return acc/(float)m_Size; |

337 | } |

338 | |

339 | template<class T> |

340 | void Vector<T>::Save(FILE* f) const |

341 | { |

342 | int version = 1; |

343 | fwrite(&version,sizeof(version),1,f); |

344 | fwrite(&m_Size,sizeof(m_Size),1,f); |

345 | fwrite(m_Data,sizeof(T)*m_Size,1,f); |

346 | } |

347 | |

348 | template<class T> |

349 | void Vector<T>::Load(FILE* f) |

350 | { |

351 | int version; |

352 | fread(&version,sizeof(version),1,f); |

353 | fread(&m_Size,sizeof(m_Size),1,f); |

354 | m_Data=new T[m_Size]; |

355 | fread(m_Data,sizeof(T)*m_Size,1,f); |

356 | } |

357 | |

358 | template<class T> |

359 | void Vector<T>::RunTests() |

360 | { |

361 | Vector<T> m(10); |

362 | m.SetAll(0); |

363 | assert(m[0]==0); |

364 | m[5]=0.5; |

365 | assert(m[5]==0.5); |

366 | Vector<T> om(m); |

367 | assert(om[5]==0.5); |

368 | |

369 | assert(feq(m.Magnitude(),0.5f)); |

370 | Vector<T> a(10); |

371 | a.Zero(); |

372 | a[5]=-10; |

373 | assert(feq(a.DistanceFrom(m),10.5f)); |

374 | } |

375 | |

376 | #endif |

**Note:**See TracBrowser for help on using the repository browser.