File: jonvec.h

package info (click to toggle)
sisl 4.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,560 kB
  • sloc: ansic: 84,814; cpp: 4,717; makefile: 7
file content (481 lines) | stat: -rwxr-xr-x 12,791 bytes parent folder | download | duplicates (3)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
 * Applied Mathematics, Norway.
 *
 * Contact information: E-mail: tor.dokken@sintef.no                      
 * SINTEF ICT, Department of Applied Mathematics,                         
 * P.O. Box 124 Blindern,                                                 
 * 0314 Oslo, Norway.                                                     
 *
 * This file is part of SISL.
 *
 * SISL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. 
 *
 * SISL is distributed in the hope that it will be useful,        
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with SISL. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public
 * License, a covered work must retain the producer line in every data
 * file that is created or manipulated using SISL.
 *
 * Other Usage
 * You can be released from the requirements of the license by purchasing
 * a commercial license. Buying such a license is mandatory as soon as you
 * develop commercial activities involving the SISL library without
 * disclosing the source code of your own applications.
 *
 * This file may be used in accordance with the terms contained in a
 * written agreement between you and SINTEF ICT. 
 */

#ifndef JONVEC_H_INCLUDED
#define JONVEC_H_INCLUDED

#include <stdio.h>
#include <math.h>
#include <vector>

#include "aux2.h"


/*!

  \page filters_mainpage The filter routines main page
  \anchor filters_mainpage
  
  \section filters_intro Introduction

  This is a collection of routines for handling various (3x3x3) 3d
  filters.<p>

  020114: This REALLY REALLY must be cleaned up...
  021231: Not yet, but soon, it will only be a question of removing commented
          out stuff... Hopefully...
  050120: Changed return types for boolean functions from int to bool.
          Hope this doesn't break anything.

*/






template<typename T>
class vector3t
{
public:
  T coo[3];

  vector3t()
    {
      coo[0]=0.0;
      coo[1]=0.0;
      coo[2]=0.0;
    };
  vector3t(const T a, const T b, const T c)
    {
      coo[0]=a;
      coo[1]=b;
      coo[2]=c;
    }
  vector3t(const float *a)
    {
      coo[0]=a[0];
      coo[1]=a[1];
      coo[2]=a[2];
    }
  vector3t(const double *a)
    {
      coo[0]=a[0];
      coo[1]=a[1];
      coo[2]=a[2];
    }
  // 030819: Disse to delegger et eller annet. Fordi de erstatter en default (og bitwise) copy constructor, uten at de samtidig er gode nok til  gjre det??? Skjnner ikke helt hva som skjer... M sjekkes opp... @@@
//    vector3t(vector3t<float> &v0) // 030819
//      {
//        coo[0]=v0.x();
//        coo[1]=v0.y();
//        coo[2]=v0.z();
//      }
//    vector3t(vector3t<double> &v0) // 030819
//      {
//        coo[0]=v0.x();
//        coo[1]=v0.y();
//        coo[2]=v0.z();
//      }
  ~vector3t(void) {}

//  vector rotate2d(T,int);
//  vector transform(coo_system &);

  //
  // 010122: For fast access used in OpenGL:
  //
  inline const T *raw(void) const
    {
      return coo;
    }

  //
  // 010125
  // 021024: Added the reference-versions.
  //
  inline T x(void) const { return coo[0]; }
  inline T y(void) const { return coo[1]; }
  inline T z(void) const { return coo[2]; }
  inline T &x(void) { return coo[0]; }
  inline T &y(void) { return coo[1]; }
  inline T &z(void) { return coo[2]; }

  //
  // 010207
  //
  inline void setx(const T a) { coo[0]=a; }
  inline void sety(const T a) { coo[1]=a; }
  inline void setz(const T a) { coo[2]=a; }

  //
  // Check out this on gcc... Why does only SGI CC complain?
  //
  inline bool operator==(const vector3t &v) const
    {
      return ((coo[0]==v.coo[0]) && (coo[1]==v.coo[1]) && (coo[2]==v.coo[2]));
    }

  //
  // 050120: Adding this... Would also be nice with one "and"'ing the
  //         results on each component... No, not necessary...
  //         Remember that deMorgan gives that a<&b <=> !(a>|b).
  //
  inline bool operator<(const vector3t &v) const
    {
      return ((coo[0]<v.coo[0]) || (coo[1]<v.coo[1]) || (coo[2]<v.coo[2]));
    }
  inline bool operator>(const vector3t &v) const
    {
      return ((coo[0]>v.coo[0]) || (coo[1]>v.coo[1]) || (coo[2]>v.coo[2]));
    }

  //
  // Note: Prefer a!=b to !(a==b) since the former is probably faster!
  //
  inline bool operator!=(const vector3t &v) const
    {
      return ((coo[0]!=v.coo[0]) || (coo[1]!=v.coo[1]) || (coo[2]!=v.coo[2]));
    }

  //
  // Ok, the strange warning produced when actually using this operator,
  // stemmed from a missing 'const' at the end of the declaration of
  // this operator. (Then the function could have altered *this, and
  // that would have discarded any const'ness...)
  //
  inline const vector3t operator+(const vector3t &v) const
    {
      return vector3t(coo[0]+v.coo[0], coo[1]+v.coo[1], coo[2]+v.coo[2]);
    }

  inline vector3t &operator+=(const vector3t &v)
    {
      coo[0]+=v.coo[0]; coo[1]+=v.coo[1]; coo[2]+=v.coo[2];
      return *this;
    }

  // 030819
//    inline vector3t<float> &operator+=(const vector3t<double> &v)
//      {
//        coo[0]+=v.coo[0]; coo[1]+=v.coo[1]; coo[2]+=v.coo[2];
//        return *this;
//      }

  inline vector3t &operator-=(const vector3t &v)
    {
      coo[0]-=v.coo[0]; coo[1]-=v.coo[1]; coo[2]-=v.coo[2];
      return *this;
    }

  // 030513
  inline vector3t &operator-=(const T &d)
    {
      coo[0]-=d; coo[1]-=d; coo[2]-=d;
      return *this;
    }

  // 030626
  inline vector3t &operator+=(const T &d)
    {
      coo[0]+=d; coo[1]+=d; coo[2]+=d;
      return *this;
    }

  // 030709
  inline vector3t &operator*=(const double x)
    {
      coo[0]*=x; coo[1]*=x; coo[2]*=x;
      return *this;
    }

  // 030709
  inline vector3t &operator/=(const double x)
    {
      coo[0]/=x; coo[1]/=x; coo[2]/=x;
      return *this;
    }

  inline const vector3t operator-(const vector3t &v) const
    {
      return vector3t(coo[0]-v.coo[0], coo[1]-v.coo[1], coo[2]-v.coo[2]);
    }

  // 000310
  inline const vector3t operator-(const T &d) const
    {
      return vector3t(coo[0]-d, coo[1]-d, coo[2]-d);
    }

  inline const vector3t operator-(void) const
    {
      return vector3t(-coo[0], -coo[1], -coo[2]);
    }

  //
  // Why can't a non-member operator like this be 'const'?
  //
  inline const friend vector3t operator*(const T &a, const vector3t &v)
    {
      return vector3t(a*v.coo[0], a*v.coo[1], a*v.coo[2]);
    }

  // 030120:
  inline const friend vector3t operator+(const double &a, const vector3t &v)
    {
      return vector3t(a+v.coo[0], a+v.coo[1], a+v.coo[2]);
    }
  inline const friend vector3t operator+(const float &a, const vector3t &v)
    {
      return vector3t(a+v.coo[0], a+v.coo[1], a+v.coo[2]);
    }

  inline T operator*(const vector3t &v) const
    {
      return (coo[0]*v.coo[0] + coo[1]*v.coo[1] + coo[2]*v.coo[2]);
    }

  inline vector3t operator/(const vector3t &v) const
    {
      return (vector3t(coo[1]*v.coo[2]-coo[2]*v.coo[1],
		      coo[2]*v.coo[0]-coo[0]*v.coo[2],
		      coo[0]*v.coo[1]-coo[1]*v.coo[0]));
    }

  //
  // Why can't I make this a const function?
  //
  inline friend T cosangle(const vector3t &v0, const vector3t &v1)
    {
      return ((v0*v1)/sqrt((v0*v0)*(v1*v1)));
    }

  inline const vector3t &clamp(const vector3t &v0, const vector3t &v1)
    {
      coo[0]=std::max(v0.coo[0], std::min(v1.coo[0], coo[0]));
      coo[1]=std::max(v0.coo[1], std::min(v1.coo[1], coo[1]));
      coo[2]=std::max(v0.coo[2], std::min(v1.coo[2], coo[2]));
      return *this;
    }

  //
  // Added 021010:
  // (Hmm... What is the sensible thing to do in such a case,
  // return a new object, like for 'clamp' above, or to just modify
  // *this, like in this definition??)
  //
#ifdef MICROSOFT
  //
  // 030208: I simply don't know where these get defined as macros...
  //         (But I suspect some Microsoft .h-file does it...)
  //
#  undef min
#  undef max
#endif
  inline void min(const vector3t &v)
    {
      coo[0]=std::min(v.x(), x());
      coo[1]=std::min(v.y(), y());
      coo[2]=std::min(v.z(), z());
    }
  inline void max(const vector3t &v)
    {
      coo[0]=std::max(v.x(), x());
      coo[1]=std::max(v.y(), y());
      coo[2]=std::max(v.z(), z());
    }


  //
  // When I do this, how come that I'm allowed to do something like this:
  //
  // vector3t x, y, z, w;
  // z=x.conv(y);
  // z+=w;
  //
  // Aha. Because it's the z I modify, not the return value of conv.
  // This shouldn't work:
  //
  // (x.conv(y)).conv(w)
  //
  // And it does not. Therefore, 'inline vec...' instead of
  // 'inline const vec...'.
  // 
  inline vector3t &conv(const vector3t &v)
    {
      coo[0]*=v.coo[0];
      coo[1]*=v.coo[1];
      coo[2]*=v.coo[2];
      return *this;
    }
  // 050122: Added this variation.
  inline friend vector3t conv2(const vector3t &v1, const vector3t &v2)
    {
      return vector3t(v1.x()*v2.x(), v1.y()*v2.y(), v1.z()*v2.z());
    }

  // 050122: Added this... Note, no checking on components being zero!
  //         So, to scale V to the box with corners A and B:
  //         conv2((B-A).reciprocal(), V-A);
  inline vector3t reciprocal(void) const
    {
      return vector3t(1.0/coo[0], 1.0/coo[1], 1.0/coo[2]);
    }

  // 050122: Might as well add this, then, for better clarity in calling code.
  //         Hmm... 'transform' or something would have been a better name.
  inline vector3t &rescale(const vector3t &mi, const vector3t &ma,
			   const vector3t &new_mi, const vector3t &new_ma)
    {
      *this=conv2(conv2((ma-mi).reciprocal(), *this-mi), new_ma-new_mi)+new_mi;
      return *this;
    }

  inline T length_squared(void) const
    {
      return (coo[0]*coo[0]+coo[1]*coo[1]+coo[2]*coo[2]);
    }

  inline T length(void) const
    {
      return (sqrt(length_squared()));
      //return (sqrt(coo[0]*coo[0]+coo[1]*coo[1]+coo[2]*coo[2]));
    }

  inline void normalize(void)
    {
      const T tmp=1.0/length();
      coo[0]*=tmp;
      coo[1]*=tmp;
      coo[2]*=tmp;
    }

  inline vector3t normalized(void) const
    {
      return (1.0/length())*(*this);
    }

  void print(void) const
    {
      printf("vector3t(%f %f %f)\n",
	     (double)coo[0], (double)coo[1], (double)coo[2]);
    }

  // 041212: Added version which does not print a newline.
  void print2(void) const
    {
      printf("vector3t(%f %f %f)",
	     (double)coo[0], (double)coo[1], (double)coo[2]);
    }

  // 050214: Added version which does not print a newline, fixed size.
  void print3(void) const
    {
      printf("(%7.3f %7.3f %7.3f)",
	     (double)coo[0], (double)coo[1], (double)coo[2]);
    }

  inline T min_coo(void) const
    {
      return std::min(coo[0], std::min(coo[1], coo[2]));
    }

  inline T max_coo(void) const
    {
      return std::max(coo[0], std::max(coo[1], coo[2]));
    }
  
  inline bool dequal(const vector3t<T> &v) const
    {
      return (DEQUAL(x(), v.x()) && DEQUAL(y(), v.y()) && DEQUAL(z(), v.z()));
    }

  inline bool dequal2(const vector3t<T> &v) const
    {
      return (DEQUAL2(x(), v.x()) &&
	      DEQUAL2(y(), v.y()) &&
	      DEQUAL2(z(), v.z()));
    }

  // 030711: For floats
  inline bool dequal3(const vector3t<T> &v) const
    {
      return (DEQUAL3(x(), v.x()) &&
	      DEQUAL3(y(), v.y()) &&
	      DEQUAL3(z(), v.z()));
    }

  // 030209: Useful because we avoid casting to a vector2t.
  inline bool dequal_2d(const vector3t<T> &v) const
    {
      return (DEQUAL(x(), v.x()) && DEQUAL(y(), v.y()));
    }

  // 030709: Rotation in the xy-plane, i.e., around the z-axis.
  inline void rotate_xy(const double cosa, const double sina)
    {
      double oldx=coo[0], oldy=coo[1];
      coo[0] =  cosa*oldx + sina*oldy;
      coo[1] = -sina*oldx + cosa*oldy;
    }

  // 030709: Rotation in the xz-plane, i.e., around the y-axis, but note the
  //         "opposite" orientation...
  inline void rotate_xz(const double cosa, const double sina)
    {
      double oldx=coo[0], oldz=coo[2];
      coo[0] =  cosa*oldx + sina*oldz;
      coo[2] = -sina*oldx + cosa*oldz;
    }

  // 030819: Rotation in the yz-plane, i.e., around the x-axis.
  inline void rotate_yz(const double cosa, const double sina)
    {
      double oldy=coo[1], oldz=coo[2];
      coo[1] =  cosa*oldy + sina*oldz;
      coo[2] = -sina*oldy + cosa*oldz;
    }
};






#endif