Slicer  4.8
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
coordTypes.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Extract Skeleton
4  Module: $HeadURL$
5  Language: C++
6  Date: $Date$
7  Version: $Revision$
8 
9  Copyright (c) Brigham and Women's Hospital (BWH) All Rights Reserved.
10 
11  See License.txt or http://www.slicer.org/copyright/copyright.txt for details.
12 
13 ==========================================================================*/
14 /*****************************************************************************************************
15 //
16 // Title: coordTypes.h
17 //
18 *******************************************************************************************************/
19 
20 #ifndef COORD_TYPES_H
21 #define COORD_TYPES_H
22 
23 #include "math.h"
24 
25 #include "misc.h"
26 
27 typedef struct point_struct
28  {
29  int x;
30  int y;
31  int z;
32  } point;
33 
34 class Coord3i
35 {
36  int v[3];
37 public:
38  inline int & operator[](int i)
39  {
40  return v[i];
41  }
42  inline void conv(double * i)
43  {
44  i[0] = v[0]; i[1] = v[1]; i[2] = v[2];
45  };
46 };
47 
48 class Coord3f
49 {
50  float v[3];
51 public:
52  inline float & operator[](int i)
53  {
54  return v[i];
55  }
56  inline void conv(float * i)
57  {
58  i[0] = v[0]; i[1] = v[1]; i[2] = v[2];
59  };
60  inline void conv(double * i)
61  {
62  i[0] = v[0]; i[1] = v[1]; i[2] = v[2];
63  };
64 };
65 
66 class Coord3d
67 {
68  double v[3];
69 public:
70  inline double & operator[](int i)
71  {
72  return v[i];
73  };
74  inline void conv(int * i)
75  {
76  i[0] = (int) v[0]; i[1] = (int) v[1]; i[2] = (int) v[2];
77  };
78  inline void conv(float * i)
79  {
80  i[0] = static_cast<float>(v[0]); i[1] = static_cast<float>(v[1]); i[2] = static_cast<float>(v[2]);
81  };
82  inline void conv(double * i)
83  {
84  i[0] = v[0]; i[1] = v[1]; i[2] = v[2];
85  };
86 };
87 
88 inline void normcrossprod(double *v1, double *v2, double *norm)
89 // calculate normalized crossproduct
90 {
91  double absval;
92 
93  norm[0] = v1[1] * v2[2] - v1[2] * v2[1];
94  norm[1] = v1[2] * v2[0] - v1[0] * v2[2];
95  norm[2] = v1[0] * v2[1] - v1[1] * v2[0];
96 
97  absval = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]);
98  norm[0] /= absval;
99  norm[1] /= absval;
100  norm[2] /= absval;
101 }
102 
103 inline double vectorangle(double *v1, double *v2)
104 // calculate angle between two vectors (0..M_PI), you might want to adjust to 0..M_PI/2
105 // range after call via 'if (angle > M_PI/2) angle = M_PI-angle;'
106 {
107  double prod = 0, length1 = 0, length2 = 0;
108 
109  for( int k = 0; k < 3; k++ )
110  {
111  prod += v1[k] * v2[k];
112  length1 += v1[k] * v1[k];
113  length2 += v2[k] * v2[k];
114  }
115  return acos(prod / sqrt(length1 * length2) );
116 }
117 
118 inline double vectorangle(Coord3d v1, Coord3d v2)
119 // calculate angle between two vectors (0..M_PI), you might want to adjust to 0..M_PI/2
120 // range after call via 'if (angle > M_PI/2) angle = M_PI-angle;'
121 {
122  double prod = 0, length1 = 0, length2 = 0;
123 
124  for( int k = 0; k < 3; k++ )
125  {
126  prod += v1[k] * v2[k];
127  length1 += v1[k] * v1[k];
128  length2 += v2[k] * v2[k];
129  }
130  return acos(prod / sqrt(length1 * length2) );
131 }
132 
133 inline double vec_length(Coord3d x)
134 {
135  return sqrt(sqr(x[0]) + sqr(x[1]) + sqr(x[2]) );
136 }
137 
138 inline double vec_length(double *x)
139 {
140  return sqrt(sqr(x[0]) + sqr(x[1]) + sqr(x[2]) );
141 }
142 
143 inline double vec_length(double *x, double *y)
144 {
145  return sqrt(sqr(x[0] - y[0]) + sqr(x[1] - y[1]) + sqr(x[2] - y[2]) );
146 }
147 
148 inline int transWorldToImage(Coord3d loc_world, int *loc_img,
149  double *origin, int *dims, double voxelsize)
150 // transform and check index, returns 0 on success and 1 if corrected location
151 {
152  int adjust = 0;
153 
154  for( int i = 0; i < 3; i++ )
155  {
156  loc_img[i] = (int) ( (loc_world[i] - origin[i]) / voxelsize);
157  if( loc_img[i] < 0 )
158  {
159  adjust = 1; loc_img[i] = 0;
160  }
161  if( loc_img[i] >= dims[i] )
162  {
163  loc_img[i] = dims[i] - 1; adjust = 1;
164  }
165  }
166 
167  return adjust;
168 }
169 
170 inline int transWorldToImage(double *loc_world, int *loc_img,
171  double *origin, int *dims, double voxelsize)
172 // transform and check index, returns 0 on success and 1 if corrected location
173 {
174  int adjust = 0;
175 
176  for( int i = 0; i < 3; i++ )
177  {
178  loc_img[i] = (int) ( (loc_world[i] - origin[i]) / voxelsize);
179  if( loc_img[i] < 0 )
180  {
181  adjust = 1; loc_img[i] = 0;
182  }
183  if( loc_img[i] >= dims[i] )
184  {
185  loc_img[i] = dims[i] - 1; adjust = 1;
186  }
187  }
188 
189  return adjust;
190 }
191 
192 #endif
void conv(double *i)
Definition: coordTypes.h:42
void conv(float *i)
Definition: coordTypes.h:56
void conv(int *i)
Definition: coordTypes.h:74
double vectorangle(double *v1, double *v2)
Definition: coordTypes.h:103
double vec_length(Coord3d x)
Definition: coordTypes.h:133
void conv(double *i)
Definition: coordTypes.h:82
double & operator[](int i)
Definition: coordTypes.h:70
void conv(double *i)
Definition: coordTypes.h:60
void normcrossprod(double *v1, double *v2, double *norm)
Definition: coordTypes.h:88
int & operator[](int i)
Definition: coordTypes.h:38
struct point_struct point
int transWorldToImage(Coord3d loc_world, int *loc_img, double *origin, int *dims, double voxelsize)
Definition: coordTypes.h:148
void conv(float *i)
Definition: coordTypes.h:78
float & operator[](int i)
Definition: coordTypes.h:52
T sqr(T x)
Definition: misc.h:64