|
Slicer 4.2
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
|
00001 /*========================================================================= 00002 00003 Program: Extract Skeleton 00004 Module: $HeadURL: http://svn.slicer.org/Slicer4/trunk/Modules/CLI/ExtractSkeleton/coordTypes.h $ 00005 Language: C++ 00006 Date: $Date: 2011-12-06 15:49:19 -0500 (Tue, 06 Dec 2011) $ 00007 Version: $Revision: 18864 $ 00008 00009 Copyright (c) Brigham and Women's Hospital (BWH) All Rights Reserved. 00010 00011 See License.txt or http://www.slicer.org/copyright/copyright.txt for details. 00012 00013 ==========================================================================*/ 00014 /***************************************************************************************************** 00015 // 00016 // Title: coordTypes.h 00017 // 00018 *******************************************************************************************************/ 00019 00020 #ifndef COORD_TYPES_H 00021 #define COORD_TYPES_H 00022 00023 #include "math.h" 00024 00025 #include "misc.h" 00026 00027 typedef struct point_struct 00028 { 00029 int x; 00030 int y; 00031 int z; 00032 } point; 00033 00034 class Coord3i 00035 { 00036 int v[3]; 00037 public: 00038 inline int & operator[](int i) 00039 { 00040 return v[i]; 00041 } 00042 inline void conv(double * i) 00043 { 00044 i[0] = v[0]; i[1] = v[1]; i[2] = v[2]; 00045 }; 00046 }; 00047 00048 class Coord3f 00049 { 00050 float v[3]; 00051 public: 00052 inline float & operator[](int i) 00053 { 00054 return v[i]; 00055 } 00056 inline void conv(float * i) 00057 { 00058 i[0] = v[0]; i[1] = v[1]; i[2] = v[2]; 00059 }; 00060 inline void conv(double * i) 00061 { 00062 i[0] = v[0]; i[1] = v[1]; i[2] = v[2]; 00063 }; 00064 }; 00065 00066 class Coord3d 00067 { 00068 double v[3]; 00069 public: 00070 inline double & operator[](int i) 00071 { 00072 return v[i]; 00073 }; 00074 inline void conv(int * i) 00075 { 00076 i[0] = (int) v[0]; i[1] = (int) v[1]; i[2] = (int) v[2]; 00077 }; 00078 inline void conv(float * i) 00079 { 00080 i[0] = static_cast<float>(v[0]); i[1] = static_cast<float>(v[1]); i[2] = static_cast<float>(v[2]); 00081 }; 00082 inline void conv(double * i) 00083 { 00084 i[0] = v[0]; i[1] = v[1]; i[2] = v[2]; 00085 }; 00086 }; 00087 00088 inline void normcrossprod(double *v1, double *v2, double *norm) 00089 // calculate normalized crossproduct 00090 { 00091 double absval; 00092 00093 norm[0] = v1[1] * v2[2] - v1[2] * v2[1]; 00094 norm[1] = v1[2] * v2[0] - v1[0] * v2[2]; 00095 norm[2] = v1[0] * v2[1] - v1[1] * v2[0]; 00096 00097 absval = sqrt(norm[0] * norm[0] + norm[1] * norm[1] + norm[2] * norm[2]); 00098 norm[0] /= absval; 00099 norm[1] /= absval; 00100 norm[2] /= absval; 00101 } 00102 00103 inline double vectorangle(double *v1, double *v2) 00104 // calculate angle between two vectors (0..M_PI), you might want to adjust to 0..M_PI/2 00105 // range after call via 'if (angle > M_PI/2) angle = M_PI-angle;' 00106 { 00107 double prod = 0, length1 = 0, length2 = 0; 00108 00109 for( int k = 0; k < 3; k++ ) 00110 { 00111 prod += v1[k] * v2[k]; 00112 length1 += v1[k] * v1[k]; 00113 length2 += v2[k] * v2[k]; 00114 } 00115 return acos(prod / sqrt(length1 * length2) ); 00116 } 00117 00118 inline double vectorangle(Coord3d v1, Coord3d v2) 00119 // calculate angle between two vectors (0..M_PI), you might want to adjust to 0..M_PI/2 00120 // range after call via 'if (angle > M_PI/2) angle = M_PI-angle;' 00121 { 00122 double prod = 0, length1 = 0, length2 = 0; 00123 00124 for( int k = 0; k < 3; k++ ) 00125 { 00126 prod += v1[k] * v2[k]; 00127 length1 += v1[k] * v1[k]; 00128 length2 += v2[k] * v2[k]; 00129 } 00130 return acos(prod / sqrt(length1 * length2) ); 00131 } 00132 00133 inline double vec_length(Coord3d x) 00134 { 00135 return sqrt(sqr(x[0]) + sqr(x[1]) + sqr(x[2]) ); 00136 } 00137 00138 inline double vec_length(double *x) 00139 { 00140 return sqrt(sqr(x[0]) + sqr(x[1]) + sqr(x[2]) ); 00141 } 00142 00143 inline double vec_length(double *x, double *y) 00144 { 00145 return sqrt(sqr(x[0] - y[0]) + sqr(x[1] - y[1]) + sqr(x[2] - y[2]) ); 00146 } 00147 00148 inline int transWorldToImage(Coord3d loc_world, int *loc_img, 00149 double *origin, int *dims, double voxelsize) 00150 // transform and check index, returns 0 on success and 1 if corrected location 00151 { 00152 int adjust = 0; 00153 00154 for( int i = 0; i < 3; i++ ) 00155 { 00156 loc_img[i] = (int) ( (loc_world[i] - origin[i]) / voxelsize); 00157 if( loc_img[i] < 0 ) 00158 { 00159 adjust = 1; loc_img[i] = 0; 00160 } 00161 if( loc_img[i] >= dims[i] ) 00162 { 00163 loc_img[i] = dims[i] - 1; adjust = 1; 00164 } 00165 } 00166 00167 return adjust; 00168 } 00169 00170 inline int transWorldToImage(double *loc_world, int *loc_img, 00171 double *origin, int *dims, double voxelsize) 00172 // transform and check index, returns 0 on success and 1 if corrected location 00173 { 00174 int adjust = 0; 00175 00176 for( int i = 0; i < 3; i++ ) 00177 { 00178 loc_img[i] = (int) ( (loc_world[i] - origin[i]) / voxelsize); 00179 if( loc_img[i] < 0 ) 00180 { 00181 adjust = 1; loc_img[i] = 0; 00182 } 00183 if( loc_img[i] >= dims[i] ) 00184 { 00185 loc_img[i] = dims[i] - 1; adjust = 1; 00186 } 00187 } 00188 00189 return adjust; 00190 } 00191 00192 #endif
1.7.4