Blame view

stim/visualization/cylinder_dep.h 13.4 KB
e21d1051   David Mayerich   cylinder can buil...
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
  #ifndef STIM_CYLINDER_H
  #define STIM_CYLINDER_H
  #include <iostream>
  #include <stim/math/circle.h>
  #include <stim/biomodels/centerline.h>
  
  
  namespace stim
  {
  template<typename T>
  class cylinder
   : public centerline<T>
  {
  	private:
  		stim::circle<T> s;							//an arbitrary circle
  		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;
  		std::vector<stim::vec<T> > mags;			//stores a list of magnitudes for each point in the centerline (assuming mags[0] is the radius)
  		std::vector< T > L;							//length of the cylinder at each position (pre-integration)
  
  
  		using stim::centerline<T>::c;
  		using stim::centerline<T>::N;
  	
  		///default init	
  		void
  		init()
  		{
  
  		}
  
  		///inits the cylinder from a list of points (std::vector of stim::vec3 --inP) and radii (inM)
  		void
  		init(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
  		{
  			mags = inM;
  			stim::vec3<float> v1;
  			stim::vec3<float> v2;
  			e.resize(inP.size());
  
  			norms.resize(inP.size());
  			Us.resize(inP.size());
  
  			if(inP.size() < 2)
  				return;
  
  			//calculate each L.
  			L.resize(inP.size());						//the number of precomputed lengths will equal the number of points
  			T temp = (T)0;								//length up to that point
  			L[0] = temp;
  			for(size_t i = 1; i < L.size(); i++)
  			{
  				temp += (inP[i-1] - inP[i]).len();
  				L[i] = temp;
  			}
  
  			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));
  			norms[0] = s.N;
  			e[0] = s;
  			Us[0] = e[0].U;
  			for(size_t i = 1; i < inP.size()-1; i++)
  			{
  				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);
  
  				norms[i] = s.N;
  
  				s.scale(inM[i][0]/inM[i-1][0]);
  				e[i] = s;
  				Us[i] = e[i].U;
  			}
  			
  			int j = inP.size()-1;
  			s.center(inP[j]);
  			dr = (inP[j] - inP[j-1]).norm();
  			s.normal(dr);
  
  			norms[j] = s.N;
  
  			s.scale(inM[j][0]/inM[j-1][0]);
  			e[j] = s;
  			Us[j] = e[j].U;
  		}
  		
  		///returns the direction vector at point idx.
  		stim::vec3<T>
  		d(int idx)
  		{
  			if(idx == 0)
  			{
  				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());
  			}
  			else if(idx == N-1)
  			{
  				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());
  			}
  			else
  			{
  //				return (e[idx+1].P - e[idx].P).norm();
  //				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();			
  			} 
  	//		return e[idx].N;	
  
  		}
  
  		stim::vec3<T>
  		d(T l, int idx)
  		{
  			if(idx == 0 || idx == N-1)
  			{
  				return norms[idx];
  //				return e[idx].N;
  			}
  			else
  			{
  				
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
  				return(	norms[idx] + (norms[idx+1] - norms[idx])*rat);
  //				return(	e[idx].N + (e[idx+1].N - e[idx].N)*rat);
  			} 
  		}
  
  
  		///finds the index of the point closest to the length l on the lower bound.
  		///binary search.
  		int
  		findIdx(T l)
  		{
  			unsigned int i = L.size()/2;
  			unsigned int max = L.size()-1;
  			unsigned int min = 0;
  			while(i > 0 && i < L.size()-1)
  			{
  //				std::cerr << "Trying " << i << std::endl;
  //				std::cerr << "l is " << l << ", L[" << i << "]" << L[i] << std::endl;
  				if(l < L[i])
  				{
  					max = i;
  					i = min+(max-min)/2;
  				}
  				else if(L[i] <= l && L[i+1] >= l)
  				{
  					break;
  				}
  				else
  				{
  					min = i;
  					i = min+(max-min)/2;
  				}
  			}
  			return i;
  		}
  
  	public:
  		///default constructor
  		cylinder()
  		// : centerline<T>()
  		{
  
  		}
  
  		///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<stim::vec<T> > inM)
  			: centerline<T>(inP)
  		{
  			init(inP, inM);
  		}
  
  		///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;
  			stim::vec<T> zero(0.0,0.0);
  			temp.resize(inM.size(), zero);
  			for(int i = 0; i < inM.size(); i++)
  				temp[i][0] = inM[i];
  			init(inP, temp);
  		}
  
  
  		///Constructor defines a cylinder with centerline inP and magnitudes of zero
  		///@param inP: Vector of stim vec3 composing the points of the centerline
  		cylinder(std::vector< stim::vec3<T> > inP)
  			: centerline<T>(inP)
  		{
  			std::vector< stim::vec<T> > inM;						//create an array of arbitrary magnitudes
  
  			stim::vec<T> zero;
  			zero.push_back(0);
  
  			inM.resize(inP.size(), zero);								//initialize the magnitude values to zero
  			init(inP, inM);
  		}
  
  		///Returns the number of points on the cylinder centerline
  
  		unsigned int size(){
  			return N;
  		}
  
  
  		///Returns a position vector at the given p-value (p value ranges from 0 to 1).
  		///interpolates the position along the line.
  		///@param pvalue: the location of the in the cylinder, from 0 (beginning to 1).
  		stim::vec3<T>
  		p(T pvalue)
  		{
  			if(pvalue < 0.0 || pvalue > 1.0)
  			{
  				return stim::vec3<float>(-1,-1,-1);
  			}
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
  			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);
  */		}
  
  		///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.
  		stim::vec3<T>
  		p(T l, int 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);
  //			return(
  //			return (pos[idx] + (pos[idx+1]-pos[idx])*((l-L[idx])/(L[idx+1]- L[idx])));
  		}
  
  		///Returns a radius at the given p-value (p value ranges from 0 to 1).
  		///interpolates the radius along the line.
  		///@param pvalue: the location of the in the cylinder, from 0 (beginning to 1).
  		T
  		r(T pvalue)
  		{
  			if(pvalue < 0.0 || pvalue > 1.0){
  				std::cerr<<"Error, value "<<pvalue<<" is outside of [0 1]."<<std::endl;
  				exit(1);
  			}
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
  			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);
  */		}
  
  		///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.
  		T
  		r(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
  			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;
  				std::cout << Us[idx].str() << std::endl;
  				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;
  			return(v2);
  //			return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  	//	(
  		}
  
  		///	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){
  			for(unsigned int p = 0; p < N; p++)
  				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;
  		}
  
  		/// Returns the number of magnitude values at each point
  		unsigned nmags(){
  			return mags[0].size();
  		}
  
  		///returns the position of the point with a given pvalue and theta on the surface
  		///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.
  		stim::vec3<T>
  		surf(T pvalue, T theta)
  		{
  			if(pvalue < 0.0 || pvalue > 1.0)
  			{
  				return stim::vec3<float>(-1,-1,-1);
  			} else {
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
  			stim::vec3<T> ps = p(l, idx); 
  			T m = r(l, idx);
  			s = e[idx];
  			s.center(ps);
  			s.normal(d(l, idx));
  			s.scale(m/e[idx].U.len());
  			return(s.p(theta));
  			}
  		}
  
  		///returns a vector of points necessary to create a circle at every position in the fiber.
  		///@param sides: the number of sides of each circle.	
  		std::vector<std::vector<vec3<T> > >
  		getPoints(int sides)
  		{
  			std::vector<std::vector <vec3<T> > > points;
  			points.resize(N);
  			for(int i = 0; i < N; i++)
  			{
  				points[i] = e[i].getPoints(sides);
  			}
  			return points;
  		}
  
  		///returns the total length of the line at index j.
  		T
  		getl(int j)
  		{
  			return (L[j]);
  		}
  		/// Allows a point on the centerline to be accessed using bracket notation
  
  		vec3<T> operator[](unsigned int i){
  			return e[i].P;
  		}
  
  		/// Returns the total length of the cylinder centerline
  		T length(){
  			return L.back();
  		}
  
  		/// 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
  
  			//vec3<T> p0, p1;					//allocate space for both points in a single segment
  
  			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
  			for(unsigned p = 1; p < N; p++){
  
  				//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
  				M += (m0 + m1)/(T)2.0 * len;
  
  				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();
  		}
  
  		/// 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){
  
  			std::vector< vec3<T> > result;
  
  			vec3<T> p0 = e[0].P;									//initialize p0 to the first point on the centerline
  			vec3<T> p1;
  			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++){
  				p1 = e[i].P;										//get the second point in the line segment
  
  				vec3<T> v = p1 - p0;								//calculate the vector between these two points
  				T d = v.len();										//calculate the distance between these two points (length of the line segment)
  
  				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
  
  				//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
  			}
  
  			result.push_back(e[size() - 1].P);						//push the last point in the centerline
  
  			return cylinder<T>(result);
  
  		}
  
  		
  };
  
  }
  #endif