Blame view

stim/visualization/cylinder.h 13.4 KB
5b6c317a   Pavel Govyadinov   implemented the c...
1
2
3
4
  #ifndef STIM_CYLINDER_H
  #define STIM_CYLINDER_H
  #include <iostream>
  #include <stim/math/circle.h>
0311ab81   Pavel Govyadinov   fixed some issues...
5
  #include <stim/biomodels/centerline.h>
5b6c317a   Pavel Govyadinov   implemented the c...
6
7
8
9
10
11
  
  
  namespace stim
  {
  template<typename T>
  class cylinder
0311ab81   Pavel Govyadinov   fixed some issues...
12
   : public centerline<T>
5b6c317a   Pavel Govyadinov   implemented the c...
13
14
  {
  	private:
8495a970   Pavel Govyadinov   added comments to...
15
  		stim::circle<T> s;			//an arbitrary circle
0311ab81   Pavel Govyadinov   fixed some issues...
16
17
18
19
  		std::vector<stim::circle<T> > e;	//an array of circles that store the centerline
  
  		std::vector<stim::vec3<T> > norms;
  		std::vector<stim::vec<T> > Us;
3e5d3ad3   Pavel Govyadinov   merged the change...
20
  		std::vector<stim::vec<T> > mags;
8495a970   Pavel Govyadinov   added comments to...
21
  		std::vector< T > L;			//length of the cylinder at each position.
0311ab81   Pavel Govyadinov   fixed some issues...
22
23
24
25
  
  
  		using stim::centerline<T>::c;
  		using stim::centerline<T>::N;
26aee9ed   Pavel Govyadinov   changed circle cl...
26
27
  	
  		///default init	
5b6c317a   Pavel Govyadinov   implemented the c...
28
  		void
26aee9ed   Pavel Govyadinov   changed circle cl...
29
30
  		init()
  		{
10654a1f   Pavel Govyadinov   added the necessa...
31
  
5b6c317a   Pavel Govyadinov   implemented the c...
32
33
  		}
  
0311ab81   Pavel Govyadinov   fixed some issues...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  		///Calculate the physical length of the fiber at each point in the fiber.
  		void
  		calculateL()
  		{
  /*			L.resize(inP.size());
  			T temp = (T)0;
  			L[0] = 0;
  			for(size_t i = 1; i < L.size(); i++)
  			{
  				temp += (inP[i-1] - inP[i]).len();
  				L[i] = temp;
  			}
  */		}
  
7d3162a2   Pavel Govyadinov   fixed majority of...
48
  		///inits the cylinder from a list of points (std::vector of stim::vec3 --inP) and radii (inM)
5b6c317a   Pavel Govyadinov   implemented the c...
49
  		void
308a743c   David Mayerich   fixed class compa...
50
  		init(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
26aee9ed   Pavel Govyadinov   changed circle cl...
51
  		{
5b6c317a   Pavel Govyadinov   implemented the c...
52
  			mags = inM;
308a743c   David Mayerich   fixed class compa...
53
54
  			stim::vec3<float> v1;
  			stim::vec3<float> v2;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
55
  			e.resize(inP.size());
0311ab81   Pavel Govyadinov   fixed some issues...
56
57
58
59
  
  			norms.resize(inP.size());
  			Us.resize(inP.size());
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
60
61
  			if(inP.size() < 2)
  				return;
8495a970   Pavel Govyadinov   added comments to...
62
63
  
  			//calculate each L.
8c4f5d84   Pavel Govyadinov   fixed the issue w...
64
  			L.resize(inP.size());
bf23ee36   Pavel Govyadinov   more minor bug fi...
65
  			T temp = (T)0;
26aee9ed   Pavel Govyadinov   changed circle cl...
66
  			L[0] = 0;
308a743c   David Mayerich   fixed class compa...
67
  			for(size_t i = 1; i < L.size(); i++)
10654a1f   Pavel Govyadinov   added the necessa...
68
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
69
  				temp += (inP[i-1] - inP[i]).len();
bf23ee36   Pavel Govyadinov   more minor bug fi...
70
  				L[i] = temp;
10654a1f   Pavel Govyadinov   added the necessa...
71
  			}
9c97e126   David Mayerich   added an axis-ali...
72
  
308a743c   David Mayerich   fixed class compa...
73
74
  			stim::vec3<T> dr = (inP[1] - inP[0]).norm();
  			s = stim::circle<T>(inP[0], inM[0][0], dr, stim::vec3<T>(1,0,0));
0311ab81   Pavel Govyadinov   fixed some issues...
75
  			norms[0] = s.N;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
76
  			e[0] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
77
  			Us[0] = e[0].U;
308a743c   David Mayerich   fixed class compa...
78
  			for(size_t i = 1; i < inP.size()-1; i++)
8c4f5d84   Pavel Govyadinov   fixed the issue w...
79
80
81
82
83
84
  			{
  				s.center(inP[i]);
  				v1 = (inP[i] - inP[i-1]).norm();
  				v2 = (inP[i+1] - inP[i]).norm();
  				dr = (v1+v2).norm();
  				s.normal(dr);
0311ab81   Pavel Govyadinov   fixed some issues...
85
86
87
  
  				norms[i] = s.N;
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
88
89
  				s.scale(inM[i][0]/inM[i-1][0]);
  				e[i] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
90
  				Us[i] = e[i].U;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
91
92
93
94
95
96
  			}
  			
  			int j = inP.size()-1;
  			s.center(inP[j]);
  			dr = (inP[j] - inP[j-1]).norm();
  			s.normal(dr);
0311ab81   Pavel Govyadinov   fixed some issues...
97
98
99
  
  			norms[j] = s.N;
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
100
101
  			s.scale(inM[j][0]/inM[j-1][0]);
  			e[j] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
102
  			Us[j] = e[j].U;
26aee9ed   Pavel Govyadinov   changed circle cl...
103
104
  		}
  		
8495a970   Pavel Govyadinov   added comments to...
105
  		///returns the direction vector at point idx.
308a743c   David Mayerich   fixed class compa...
106
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
107
108
109
110
  		d(int idx)
  		{
  			if(idx == 0)
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
111
112
113
114
115
116
117
  				stim::vec3<T> temp(
  						c[idx+1][0]-c[idx][0],	
  						c[idx+1][1]-c[idx][1],	
  						c[idx+1][2]-c[idx][2]
  						);	
  //				return (e[idx+1].P - e[idx].P).norm();
  				return (temp.norm());
26aee9ed   Pavel Govyadinov   changed circle cl...
118
  			}
0311ab81   Pavel Govyadinov   fixed some issues...
119
  			else if(idx == N-1)
26aee9ed   Pavel Govyadinov   changed circle cl...
120
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
121
122
123
124
125
126
127
  				stim::vec3<T> temp(
  						c[idx][0]-c[idx+1][0],	
  						c[idx][1]-c[idx+1][1],	
  						c[idx][2]-c[idx+1][2]
  						);	
  			//	return (e[idx].P - e[idx-1].P).norm();
  				return (temp.norm());
26aee9ed   Pavel Govyadinov   changed circle cl...
128
129
130
  			}
  			else
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
131
  //				return (e[idx+1].P - e[idx].P).norm();
0311ab81   Pavel Govyadinov   fixed some issues...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  //				stim::vec3<float> v1 = (e[idx].P-e[idx-1].P).norm();
  				stim::vec3<T> v1(
  						c[idx][0]-c[idx-1][0],	
  						c[idx][1]-c[idx-1][1],	
  						c[idx][2]-c[idx-1][2]
  						);	
  						
  //				stim::vec3<float> v2 = (e[idx+1].P-e[idx].P).norm();
  				stim::vec3<T> v2(
  						c[idx+1][0]-c[idx][0],	
  						c[idx+1][1]-c[idx][1],	
  						c[idx+1][2]-c[idx][2]
  						);
  					
  				return (v1.norm()+v2.norm()).norm();			
8c4f5d84   Pavel Govyadinov   fixed the issue w...
147
148
149
  			} 
  	//		return e[idx].N;	
  
10654a1f   Pavel Govyadinov   added the necessa...
150
151
  		}
  
308a743c   David Mayerich   fixed class compa...
152
  		stim::vec3<T>
8c4f5d84   Pavel Govyadinov   fixed the issue w...
153
  		d(T l, int idx)
26aee9ed   Pavel Govyadinov   changed circle cl...
154
  		{
0311ab81   Pavel Govyadinov   fixed some issues...
155
  			if(idx == 0 || idx == N-1)
10654a1f   Pavel Govyadinov   added the necessa...
156
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
157
158
  				return norms[idx];
  //				return e[idx].N;
10654a1f   Pavel Govyadinov   added the necessa...
159
  			}
8c4f5d84   Pavel Govyadinov   fixed the issue w...
160
161
  			else
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
162
  				
8c4f5d84   Pavel Govyadinov   fixed the issue w...
163
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
164
165
  				return(	norms[idx] + (norms[idx+1] - norms[idx])*rat);
  //				return(	e[idx].N + (e[idx+1].N - e[idx].N)*rat);
8c4f5d84   Pavel Govyadinov   fixed the issue w...
166
  			} 
10654a1f   Pavel Govyadinov   added the necessa...
167
168
  		}
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
169
  
8495a970   Pavel Govyadinov   added comments to...
170
171
  		///finds the index of the point closest to the length l on the lower bound.
  		///binary search.
10654a1f   Pavel Govyadinov   added the necessa...
172
  		int
26aee9ed   Pavel Govyadinov   changed circle cl...
173
174
175
176
177
  		findIdx(T l)
  		{
  			unsigned int i = L.size()/2;
  			unsigned int max = L.size()-1;
  			unsigned int min = 0;
26aee9ed   Pavel Govyadinov   changed circle cl...
178
  			while(i > 0 && i < L.size()-1)
10654a1f   Pavel Govyadinov   added the necessa...
179
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
180
181
  //				std::cerr << "Trying " << i << std::endl;
  //				std::cerr << "l is " << l << ", L[" << i << "]" << L[i] << std::endl;
26aee9ed   Pavel Govyadinov   changed circle cl...
182
  				if(l < L[i])
10654a1f   Pavel Govyadinov   added the necessa...
183
  				{
26aee9ed   Pavel Govyadinov   changed circle cl...
184
  					max = i;
26aee9ed   Pavel Govyadinov   changed circle cl...
185
  					i = min+(max-min)/2;
10654a1f   Pavel Govyadinov   added the necessa...
186
  				}
26aee9ed   Pavel Govyadinov   changed circle cl...
187
  				else if(L[i] <= l && L[i+1] >= l)
10654a1f   Pavel Govyadinov   added the necessa...
188
189
190
191
192
  				{
  					break;
  				}
  				else
  				{
26aee9ed   Pavel Govyadinov   changed circle cl...
193
  					min = i;
26aee9ed   Pavel Govyadinov   changed circle cl...
194
  					i = min+(max-min)/2;
10654a1f   Pavel Govyadinov   added the necessa...
195
196
197
198
  				}
  			}
  			return i;
  		}
5b6c317a   Pavel Govyadinov   implemented the c...
199
  
9c97e126   David Mayerich   added an axis-ali...
200
201
  	public:
  		///default constructor
26aee9ed   Pavel Govyadinov   changed circle cl...
202
  		cylinder()
0311ab81   Pavel Govyadinov   fixed some issues...
203
  		// : centerline<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
204
205
206
  		{
  
  		}
9c97e126   David Mayerich   added an axis-ali...
207
  
5b6c317a   Pavel Govyadinov   implemented the c...
208
  		///constructor to create a cylinder from a set of points, radii, and the number of sides for the cylinder.
7d3162a2   Pavel Govyadinov   fixed majority of...
209
  		///@param inP:  Vector of stim vec3 composing the points of the centerline.
5b6c317a   Pavel Govyadinov   implemented the c...
210
  		///@param inM:  Vector of stim vecs composing the radii of the centerline.
0311ab81   Pavel Govyadinov   fixed some issues...
211
212
213
  		cylinder(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
  			: centerline<T>(inP)
  		{
5b6c317a   Pavel Govyadinov   implemented the c...
214
215
216
  			init(inP, inM);
  		}
  
09049866   Pavel Govyadinov   fixed a drawing b...
217
218
219
220
221
222
223
  		///constructor to create a cylinder from a set of points, radii, and the number of sides for the cylinder.
  		///@param inP:  Vector of stim vec3 composing the points of the centerline.
  		///@param inM:  Vector of stim vecs composing the radii of the centerline.
  		cylinder(std::vector<stim::vec3<T> > inP, std::vector< T > inM)
  			: centerline<T>(inP)
  		{
  			std::vector<stim::vec<T> > temp;
a36c3850   Pavel Govyadinov   fixed the bug tha...
224
  			stim::vec<T> zero(0.0,0.0);
09049866   Pavel Govyadinov   fixed a drawing b...
225
  			temp.resize(inM.size(), zero);
47fe6300   Pavel Govyadinov   fixed an accident...
226
227
  			for(int i = 0; i < inM.size(); i++)
  				temp[i][0] = inM[i];
09049866   Pavel Govyadinov   fixed a drawing b...
228
229
230
  			init(inP, temp);
  		}
  
7d3162a2   Pavel Govyadinov   fixed majority of...
231
  
58ee2b21   David Mayerich   incorporated stim...
232
  		///Constructor defines a cylinder with centerline inP and magnitudes of zero
7d3162a2   Pavel Govyadinov   fixed majority of...
233
  		///@param inP: Vector of stim vec3 composing the points of the centerline
0311ab81   Pavel Govyadinov   fixed some issues...
234
235
236
  		cylinder(std::vector< stim::vec3<T> > inP)
  			: centerline<T>(inP)
  		{
09049866   Pavel Govyadinov   fixed a drawing b...
237
  			std::vector< T > inM;						//create an array of arbitrary magnitudes
9c97e126   David Mayerich   added an axis-ali...
238
239
240
241
242
  
  			stim::vec<T> zero;
  			zero.push_back(0);
  
  			inM.resize(inP.size(), zero);								//initialize the magnitude values to zero
58ee2b21   David Mayerich   incorporated stim...
243
244
245
  			init(inP, inM);
  		}
  
58ee2b21   David Mayerich   incorporated stim...
246
247
248
  		///Returns the number of points on the cylinder centerline
  
  		unsigned int size(){
0311ab81   Pavel Govyadinov   fixed some issues...
249
  			return N;
58ee2b21   David Mayerich   incorporated stim...
250
251
  		}
  
10654a1f   Pavel Govyadinov   added the necessa...
252
253
  
  		///Returns a position vector at the given p-value (p value ranges from 0 to 1).
8495a970   Pavel Govyadinov   added comments to...
254
255
  		///interpolates the position along the line.
  		///@param pvalue: the location of the in the cylinder, from 0 (beginning to 1).
308a743c   David Mayerich   fixed class compa...
256
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
257
258
  		p(T pvalue)
  		{
bf23ee36   Pavel Govyadinov   more minor bug fi...
259
  			if(pvalue < 0.0 || pvalue > 1.0)
26aee9ed   Pavel Govyadinov   changed circle cl...
260
  			{
308a743c   David Mayerich   fixed class compa...
261
  				return stim::vec3<float>(-1,-1,-1);
26aee9ed   Pavel Govyadinov   changed circle cl...
262
  			}
10654a1f   Pavel Govyadinov   added the necessa...
263
264
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
0311ab81   Pavel Govyadinov   fixed some issues...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  			return (p(l,idx));
  /*				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
  				stim::vec3<T> v1(
  						c[idx][0],	
  						c[idx][1],	
  						c[idx][2]
  						);	
  						
  				stim::vec3<T> v2(
  						c[idx+1][0],	
  						c[idx+1][1],	
  						c[idx+1][2]
  						);
  
  //			return(	e[idx].P + (e[idx+1].P-e[idx].P)*rat);
  			return( v1 + (v2 - v1)*rat);
  */		}
10654a1f   Pavel Govyadinov   added the necessa...
282
  
8495a970   Pavel Govyadinov   added comments to...
283
284
285
286
  		///Returns a position vector at the given length into the fiber (based on the pvalue).
  		///Interpolates the radius along the line.
  		///@param l: the location of the in the cylinder.
  		///@param idx: integer location of the point closest to l but prior to it.
308a743c   David Mayerich   fixed class compa...
287
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
288
289
290
  		p(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
291
292
293
294
295
296
297
298
299
300
301
302
303
  				stim::vec3<T> v1(
  						c[idx][0],	
  						c[idx][1],	
  						c[idx][2]
  						);	
  						
  				stim::vec3<T> v2(
  						c[idx+1][0],	
  						c[idx+1][1],	
  						c[idx+1][2]
  						);
  //			return(	e[idx].P + (e[idx+1].P-e[idx].P)*rat);
  			return(	v1 + (v2-v1)*rat);
26aee9ed   Pavel Govyadinov   changed circle cl...
304
305
  //			return(
  //			return (pos[idx] + (pos[idx+1]-pos[idx])*((l-L[idx])/(L[idx+1]- L[idx])));
10654a1f   Pavel Govyadinov   added the necessa...
306
307
308
  		}
  
  		///Returns a radius at the given p-value (p value ranges from 0 to 1).
8495a970   Pavel Govyadinov   added comments to...
309
310
  		///interpolates the radius along the line.
  		///@param pvalue: the location of the in the cylinder, from 0 (beginning to 1).
10654a1f   Pavel Govyadinov   added the necessa...
311
  		T
26aee9ed   Pavel Govyadinov   changed circle cl...
312
313
  		r(T pvalue)
  		{
98eecaa9   David Mayerich   VS and win32 updates
314
315
316
317
  			if(pvalue < 0.0 || pvalue > 1.0){
  				std::cerr<<"Error, value "<<pvalue<<" is outside of [0 1]."<<std::endl;
  				exit(1);
  			}
10654a1f   Pavel Govyadinov   added the necessa...
318
319
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
0311ab81   Pavel Govyadinov   fixed some issues...
320
321
322
323
324
  			return (r(l,idx));
  /*			T rat = (l-L[idx])/(L[idx+1]-L[idx]);
  //			return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  			return (e[idx].U.len() + (e[idx+1].U.len() - e[idx].U.len())*rat);
  */		}
10654a1f   Pavel Govyadinov   added the necessa...
325
  
8495a970   Pavel Govyadinov   added comments to...
326
327
328
329
  		///Returns a radius at the given length into the fiber (based on the pvalue).
  		///Interpolates the position along the line.
  		///@param l: the location of the in the cylinder.
  		///@param idx: integer location of the point closest to l but prior to it.
10654a1f   Pavel Govyadinov   added the necessa...
330
  		T
26aee9ed   Pavel Govyadinov   changed circle cl...
331
332
333
  		r(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
334
335
336
337
338
339
340
341
  			T v1 = (e[idx].U.len() + (e[idx+1].U.len() - e[idx].U.len())*rat);
  			T v3 = (Us[idx].len() + (Us[idx+1].len() - Us[idx].len())*rat);
  			T v2 = (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  //			std::cout << (float)v1 = (float) v2 << std::endl;
  			if(abs(v3 - v1) >= 10e-6)
  			{
  				std::cout << "-------------------------" << std::endl;
  				std::cout << e[idx].str() << std::endl << std::endl;
b50c840e   David Mayerich   eliminated ANN fr...
342
  				std::cout << Us[idx].str() << std::endl;
0311ab81   Pavel Govyadinov   fixed some issues...
343
344
345
346
347
  				std::cout << (float)v1 - (float) v2 << std::endl;
  				std::cout << "failed" << std::endl;
  			}
  //			std::cout << e[idx].U.len() << " " << mags[idx][0] << std::endl;
  //			std::cout << v2 << std::endl;
24dee661   Pavel Govyadinov   finished the debu...
348
  			return(v2);
0311ab81   Pavel Govyadinov   fixed some issues...
349
350
  //			return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  	//	(
66fe63f3   Pavel Govyadinov   fixed minor typo
351
  		}
10654a1f   Pavel Govyadinov   added the necessa...
352
  
9c97e126   David Mayerich   added an axis-ali...
353
354
355
356
357
358
359
360
361
362
  		///	Returns the magnitude at the given index
  		///	@param i is the index of the desired point
  		/// @param m is the index of the magnitude value
  		T ri(unsigned i, unsigned m = 0){
  			return mags[i][m];
  		}
  
  		/// Adds a new magnitude value to all points
  		/// @param m is the starting value for the new magnitude
  		void add_mag(T m = 0){
0311ab81   Pavel Govyadinov   fixed some issues...
363
  			for(unsigned int p = 0; p < N; p++)
9c97e126   David Mayerich   added an axis-ali...
364
365
366
367
368
369
370
371
372
373
374
  				mags[p].push_back(m);
  		}
  
  		/// Sets a magnitude value
  		/// @param val is the new value for the magnitude
  		/// @param p is the point index for the magnitude to be set
  		/// @param m is the index for the magnitude
  		void set_mag(T val, unsigned p, unsigned m = 0){
  			mags[p][m] = val;
  		}
  
b3a38641   David Mayerich   added the ability...
375
376
377
378
379
  		/// Returns the number of magnitude values at each point
  		unsigned nmags(){
  			return mags[0].size();
  		}
  
10654a1f   Pavel Govyadinov   added the necessa...
380
  		///returns the position of the point with a given pvalue and theta on the surface
8495a970   Pavel Govyadinov   added comments to...
381
382
383
  		///in x, y, z coordinates. Theta is in degrees from 0 to 360.
  		///@param pvalue: the location of the in the cylinder, from 0 (beginning to 1).
  		///@param theta: the angle to the point of a circle.
308a743c   David Mayerich   fixed class compa...
384
  		stim::vec3<T>
10654a1f   Pavel Govyadinov   added the necessa...
385
386
  		surf(T pvalue, T theta)
  		{
bf23ee36   Pavel Govyadinov   more minor bug fi...
387
  			if(pvalue < 0.0 || pvalue > 1.0)
26aee9ed   Pavel Govyadinov   changed circle cl...
388
  			{
308a743c   David Mayerich   fixed class compa...
389
  				return stim::vec3<float>(-1,-1,-1);
26aee9ed   Pavel Govyadinov   changed circle cl...
390
  			} else {
10654a1f   Pavel Govyadinov   added the necessa...
391
392
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
308a743c   David Mayerich   fixed class compa...
393
  			stim::vec3<T> ps = p(l, idx); 
10654a1f   Pavel Govyadinov   added the necessa...
394
  			T m = r(l, idx);
8c4f5d84   Pavel Govyadinov   fixed the issue w...
395
396
397
398
  			s = e[idx];
  			s.center(ps);
  			s.normal(d(l, idx));
  			s.scale(m/e[idx].U.len());
10654a1f   Pavel Govyadinov   added the necessa...
399
  			return(s.p(theta));
26aee9ed   Pavel Govyadinov   changed circle cl...
400
  			}
10654a1f   Pavel Govyadinov   added the necessa...
401
402
  		}
  
8495a970   Pavel Govyadinov   added comments to...
403
  		///returns a vector of points necessary to create a circle at every position in the fiber.
26aee9ed   Pavel Govyadinov   changed circle cl...
404
  		///@param sides: the number of sides of each circle.	
308a743c   David Mayerich   fixed class compa...
405
  		std::vector<std::vector<vec3<T> > >
5b6c317a   Pavel Govyadinov   implemented the c...
406
407
  		getPoints(int sides)
  		{
308a743c   David Mayerich   fixed class compa...
408
  			std::vector<std::vector <vec3<T> > > points;
0311ab81   Pavel Govyadinov   fixed some issues...
409
410
  			points.resize(N);
  			for(int i = 0; i < N; i++)
5b6c317a   Pavel Govyadinov   implemented the c...
411
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
412
  				points[i] = e[i].getPoints(sides);
5b6c317a   Pavel Govyadinov   implemented the c...
413
  			}
8c4f5d84   Pavel Govyadinov   fixed the issue w...
414
  			return points;
5b6c317a   Pavel Govyadinov   implemented the c...
415
  		}
58ee2b21   David Mayerich   incorporated stim...
416
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
417
418
419
420
421
422
  		///returns the total length of the line at index j.
  		T
  		getl(int j)
  		{
  			return (L[j]);
  		}
58ee2b21   David Mayerich   incorporated stim...
423
424
  		/// Allows a point on the centerline to be accessed using bracket notation
  
308a743c   David Mayerich   fixed class compa...
425
  		vec3<T> operator[](unsigned int i){
3e5d3ad3   Pavel Govyadinov   merged the change...
426
  			return e[i].P;
58ee2b21   David Mayerich   incorporated stim...
427
428
429
430
431
432
433
  		}
  
  		/// Returns the total length of the cylinder centerline
  		T length(){
  			return L.back();
  		}
  
9c97e126   David Mayerich   added an axis-ali...
434
435
436
437
438
439
440
  		/// Integrates a magnitude value along the cylinder.
  		/// @param m is the magnitude value to be integrated (this is usually the radius)
  		T integrate(unsigned m = 0){
  
  			T M = 0;						//initialize the integral to zero
  			T m0, m1;						//allocate space for both magnitudes in a single segment
  
308a743c   David Mayerich   fixed class compa...
441
  			//vec3<T> p0, p1;					//allocate space for both points in a single segment
9c97e126   David Mayerich   added an axis-ali...
442
443
444
445
446
447
448
  
  			m0 = mags[0][m];				//initialize the first point and magnitude to the first point in the cylinder
  			//p0 = pos[0];
  
  			T len = L[0];						//allocate space for the segment length
  
  			//for every consecutive point in the cylinder
0311ab81   Pavel Govyadinov   fixed some issues...
449
  			for(unsigned p = 1; p < N; p++){
9c97e126   David Mayerich   added an axis-ali...
450
451
452
453
454
455
456
  
  				//p1 = pos[p];							//get the position and magnitude for the next point
  				m1 = mags[p][m];
  
  				if(p > 1) len = (L[p-1] - L[p-2]);		//calculate the segment length using the L array
  
  				//add the average magnitude, weighted by the segment length
308a743c   David Mayerich   fixed class compa...
457
  				M += (m0 + m1)/(T)2.0 * len;
9c97e126   David Mayerich   added an axis-ali...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  
  				m0 = m1;								//move to the next segment by shifting points
  			}
  			return M;			//return the integral
  		}
  
  		/// Averages a magnitude value across the cylinder
  		/// @param m is the magnitude value to be averaged (this is usually the radius)
  		T average(unsigned m = 0){			
  
  			//return the average magnitude
  			return integrate(m) / L.back();
  		}
  
58ee2b21   David Mayerich   incorporated stim...
472
473
474
475
476
  		/// Resamples the cylinder to provide a maximum distance of "spacing" between centerline points. All current
  		///		centerline points are guaranteed to exist in the new cylinder
  		/// @param spacing is the maximum spacing allowed between sample points
  		cylinder<T> resample(T spacing){
  
308a743c   David Mayerich   fixed class compa...
477
  			std::vector< vec3<T> > result;
9c97e126   David Mayerich   added an axis-ali...
478
  
308a743c   David Mayerich   fixed class compa...
479
480
  			vec3<T> p0 = e[0].P;								//initialize p0 to the first point on the centerline
  			vec3<T> p1;
58ee2b21   David Mayerich   incorporated stim...
481
482
483
484
  			unsigned N = size();							//number of points in the current centerline
  
  			//for each line segment on the centerline
  			for(unsigned int i = 1; i < N; i++){
3e5d3ad3   Pavel Govyadinov   merged the change...
485
  				p1 = e[i].P;								//get the second point in the line segment
58ee2b21   David Mayerich   incorporated stim...
486
  
308a743c   David Mayerich   fixed class compa...
487
  				vec3<T> v = p1 - p0;							//calculate the vector between these two points
58ee2b21   David Mayerich   incorporated stim...
488
489
  				T d = v.len();								//calculate the distance between these two points (length of the line segment)
  
308a743c   David Mayerich   fixed class compa...
490
491
  				size_t nsteps = (size_t)std::ceil(d / spacing);		//calculate the number of steps to take along the segment to meet the spacing criteria
  				T stepsize = (T)1.0 / nsteps;			//calculate the parametric step size between new centerline points
58ee2b21   David Mayerich   incorporated stim...
492
493
494
495
496
497
498
499
500
501
  
  				//for each step along the line segment
  				for(unsigned s = 0; s < nsteps; s++){
  					T alpha = stepsize * s;					//calculate the fraction of the distance along the line segment covered
  					result.push_back(p0 + alpha * v);	//push the point at alpha position along the line segment
  				}
  
  				p0 = p1;								//shift the points to move to the next line segment
  			}
  
3e5d3ad3   Pavel Govyadinov   merged the change...
502
  			result.push_back(e[size() - 1].P);			//push the last point in the centerline
9c97e126   David Mayerich   added an axis-ali...
503
  
58ee2b21   David Mayerich   incorporated stim...
504
505
506
  			return cylinder<T>(result);
  
  		}
9c97e126   David Mayerich   added an axis-ali...
507
  
26aee9ed   Pavel Govyadinov   changed circle cl...
508
  		
5b6c317a   Pavel Govyadinov   implemented the c...
509
510
511
512
  };
  
  }
  #endif