Blame view

stim/visualization/cylinder.h 18.3 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>
e21d1051   David Mayerich   cylinder can buil...
6
  #include <stim/visualization/obj.h>
5b6c317a   Pavel Govyadinov   implemented the c...
7
  
5b6c317a   Pavel Govyadinov   implemented the c...
8
9
10
11
  
  namespace stim
  {
  template<typename T>
e21d1051   David Mayerich   cylinder can buil...
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  class cylinder : public centerline<T> {
  protected:
  	//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::vec3<T> > U;					//stores the array of U vectors defining the Frenet frame
  	std::vector< std::vector<T> > M;				//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;
  	using stim::centerline<T>::size;
  	using stim::centerline<T>::at;
  	using stim::centerline<T>::findIdx;
26aee9ed   Pavel Govyadinov   changed circle cl...
28
  	
e21d1051   David Mayerich   cylinder can buil...
29
30
31
32
33
34
35
36
37
38
  	//calculates the U values for each point to initialize the frenet frame
  	//	this function assumes that the centerline has already been set
  	void init() {
  		U.resize(size());								//allocate space for the frenet frame vectors
  	
  		stim::circle<T> c;								//create a circle
  		stim::vec3<T> d0, d1;
  		for (size_t i = 0; i < size() - 1; i++) {		//for each line segment in the centerline
  			c.rotate(d(i));								//rotate the circle to match that normal
  			U[i] = c.U;									//save the U vector from the circle
5b6c317a   Pavel Govyadinov   implemented the c...
39
  		}
e21d1051   David Mayerich   cylinder can buil...
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
  		U[size() - 1] = c.U;							//for the last point, duplicate the final frenet frame vector
  	}
  
  	///adds a magnitude to each point in the cylinder
  	void add_mag(T val = 0) {
  		if (M.size() == 0) M.resize(size());	//if the magnitude vector isn't initialized, resize it to match the centerline
  		for (size_t i = 0; i < size(); i++)		//for each point
  			M[i].push_back(val);				//add this value to the magnitude vector at each point
  	}
  
  	//adds a magnitude based on a list of magnitudes for each point
  	void add_mag(std::vector<T> val) {
  		if (M.size() == 0) M.resize(size());	//if the magnitude vector isn't initialized, resize it to match the centerline
  		for (size_t i = 0; i < size(); i++)		//for each point
  			M[i].push_back(val[i]);				//add this value to the magnitude vector at each point
  	}
  
  public:
  	std::string str() {
  		std::stringstream ss;
  		size_t N = std::vector< stim::vec3<T> >::size();
  		ss << "---------[" << N << "]---------" << std::endl;
  		for (size_t i = 0; i < N; i++)
  			ss << std::vector< stim::vec3<T> >::at(i) << "   r = "<< M[i][0]<<"   u = "<<U[i]<<std::endl;
  		ss << "--------------------" << std::endl;
  
  		return ss.str();
  	}
  
  	cylinder(centerline& c, T r = 0) : centerline(c) {
  		init();
  		add_mag(r);
  	}
  
  	//initialize a cylinder with a list of points and magnitude values
  	cylinder(centerline& c, std::vector<T> r) : centerline(c){
  		init();
  		add_mag(r);
  	}
  
  	///Returns magnitude i 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 m(T l, int idx, size_t i = 0) {
  		T a = (l - L[idx]) / (L[idx + 1] - L[idx]);
  		T v2 = (M[idx][i] + (M[idx + 1][i] - M[idx][i])*a);
  		
  		return v2;
  	}
  
  	///Returns the ith magnitude 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 m(T pvalue, size_t i = 0) {
  		if (pvalue <= 0.0) return M[0][i];
  		if (pvalue >= 1.0) return M[size() - 1][i];
  
  		T l = pvalue*L[L.size() - 1];
  		int idx = findIdx(l);
  		return m(l, idx, i);
  	}
  
  	///Returns a circle representing the cylinder cross section at point i
  	stim::circle<T> circ(size_t i, size_t m = 0) {
  		return stim::circle<T>(at(i), M[i][m], d(i), U[i]);
  	}
  
  	///Returns an OBJ object representing the cylinder with a radial tesselation value of N using magnitude m
  	stim::obj<T> OBJ(size_t N, size_t m = 0) {
  		stim::obj<T> out;								//create an OBJ object
  		stim::circle<T> c0, c1;
  		std::vector< stim::vec3<T> > p0, p1;			//allocate space for the point sets representing the circles bounding each cylinder segment
  		T u0, u1, v0, v1;											//allocate variables to store running texture coordinates
  		for (size_t i = 1; i < size(); i++) {			//for each line segment in the cylinder
  			c0 = circ(i - 1);							//get the two circles bounding the line segment
  			c1 = circ(i);
  
  			p0 = c0.points(N);							//get t points for each of the end caps
  			p1 = c1.points(N);
  
  			u0 = L[i - 1] / length();						//calculate the texture coordinate (u, v) where u runs along the cylinder length
  			u1 = L[i] / length();
  				
  			for (size_t n = 1; n < N; n++) {				//for each point in the circle
  				v0 = (double)(n-1) / (double)(N - 1);			//v texture coordinate runs around the cylinder
  				v1 = (double)(n) / (double)(N - 1);
  				out.Begin(OBJ_FACE);						//start a face (quad)
  					out.TexCoord(u0, v0);
  					out.Vertex(p0[n - 1][0], p0[n - 1][1], p0[n - 1][2]);	//output the points composing a strip of quads wrapping the cylinder segment
  					out.TexCoord(u1, v0);
  					out.Vertex(p1[n - 1][0], p1[n - 1][1], p1[n - 1][2]);
  				
  					out.TexCoord(u0, v1);
  					out.Vertex(p1[n][0], p1[n][1], p1[n][2]);
  					out.TexCoord(u1, v1);
  					out.Vertex(p0[n][0], p0[n][1], p0[n][2]);
  				out.End();
0311ab81   Pavel Govyadinov   fixed some issues...
138
  			}
e21d1051   David Mayerich   cylinder can buil...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  			v0 = (double)(N - 2) / (double)(N - 1);			//v texture coordinate runs around the cylinder
  			v1 = 1.0;
  			out.Begin(OBJ_FACE);
  				out.TexCoord(u0, v0);
  				out.Vertex(p0[N - 1][0], p0[N - 1][1], p0[N - 1][2]);	//output the points composing a strip of quads wrapping the cylinder segment
  				out.TexCoord(u1, v0);
  				out.Vertex(p1[N - 1][0], p1[N - 1][1], p1[N - 1][2]);
  
  				out.TexCoord(u0, v1);
  				out.Vertex(p1[0][0], p1[0][1], p1[0][2]);
  				out.TexCoord(u1, v1);
  				out.Vertex(p0[0][0], p0[0][1], p0[0][2]);
  			out.End();
  		}
  		return out;
  	}
0311ab81   Pavel Govyadinov   fixed some issues...
155
  
e21d1051   David Mayerich   cylinder can buil...
156
157
158
  
  		/*
  		///inits the cylinder from a list of points (std::vector of stim::vec3 --inP) and magnitudes (inM)
5b6c317a   Pavel Govyadinov   implemented the c...
159
  		void
e21d1051   David Mayerich   cylinder can buil...
160
161
162
  		init(centerline inP, std::vector< std::vector<T> > inM) {
  			M = inM;									//the magnitude vector can be copied directly
  			(*this) = inP;								//the centerline can be copied to this class directly
308a743c   David Mayerich   fixed class compa...
163
164
  			stim::vec3<float> v1;
  			stim::vec3<float> v2;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
165
  			e.resize(inP.size());
0311ab81   Pavel Govyadinov   fixed some issues...
166
167
168
169
  
  			norms.resize(inP.size());
  			Us.resize(inP.size());
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
170
171
  			if(inP.size() < 2)
  				return;
8495a970   Pavel Govyadinov   added comments to...
172
173
  
  			//calculate each L.
1b2858a2   David Mayerich   fixed some commen...
174
175
176
  			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;
308a743c   David Mayerich   fixed class compa...
177
  			for(size_t i = 1; i < L.size(); i++)
10654a1f   Pavel Govyadinov   added the necessa...
178
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
179
  				temp += (inP[i-1] - inP[i]).len();
bf23ee36   Pavel Govyadinov   more minor bug fi...
180
  				L[i] = temp;
10654a1f   Pavel Govyadinov   added the necessa...
181
  			}
9c97e126   David Mayerich   added an axis-ali...
182
  
308a743c   David Mayerich   fixed class compa...
183
184
  			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...
185
  			norms[0] = s.N;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
186
  			e[0] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
187
  			Us[0] = e[0].U;
308a743c   David Mayerich   fixed class compa...
188
  			for(size_t i = 1; i < inP.size()-1; i++)
8c4f5d84   Pavel Govyadinov   fixed the issue w...
189
190
191
192
193
194
  			{
  				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...
195
196
197
  
  				norms[i] = s.N;
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
198
199
  				s.scale(inM[i][0]/inM[i-1][0]);
  				e[i] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
200
  				Us[i] = e[i].U;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
201
202
203
204
205
206
  			}
  			
  			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...
207
208
209
  
  			norms[j] = s.N;
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
210
211
  			s.scale(inM[j][0]/inM[j-1][0]);
  			e[j] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
212
  			Us[j] = e[j].U;
26aee9ed   Pavel Govyadinov   changed circle cl...
213
214
  		}
  		
8495a970   Pavel Govyadinov   added comments to...
215
  		///returns the direction vector at point idx.
308a743c   David Mayerich   fixed class compa...
216
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
217
218
219
220
  		d(int idx)
  		{
  			if(idx == 0)
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
221
222
223
224
225
226
227
  				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...
228
  			}
0311ab81   Pavel Govyadinov   fixed some issues...
229
  			else if(idx == N-1)
26aee9ed   Pavel Govyadinov   changed circle cl...
230
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
231
232
233
234
235
236
237
  				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...
238
239
240
  			}
  			else
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
241
  //				return (e[idx+1].P - e[idx].P).norm();
0311ab81   Pavel Govyadinov   fixed some issues...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
  //				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...
257
258
259
  			} 
  	//		return e[idx].N;	
  
10654a1f   Pavel Govyadinov   added the necessa...
260
261
  		}
  
308a743c   David Mayerich   fixed class compa...
262
  		stim::vec3<T>
8c4f5d84   Pavel Govyadinov   fixed the issue w...
263
  		d(T l, int idx)
26aee9ed   Pavel Govyadinov   changed circle cl...
264
  		{
0311ab81   Pavel Govyadinov   fixed some issues...
265
  			if(idx == 0 || idx == N-1)
10654a1f   Pavel Govyadinov   added the necessa...
266
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
267
268
  				return norms[idx];
  //				return e[idx].N;
10654a1f   Pavel Govyadinov   added the necessa...
269
  			}
8c4f5d84   Pavel Govyadinov   fixed the issue w...
270
271
  			else
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
272
  				
8c4f5d84   Pavel Govyadinov   fixed the issue w...
273
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
274
275
  				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...
276
  			} 
10654a1f   Pavel Govyadinov   added the necessa...
277
278
  		}
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
279
  
8495a970   Pavel Govyadinov   added comments to...
280
281
  		///finds the index of the point closest to the length l on the lower bound.
  		///binary search.
10654a1f   Pavel Govyadinov   added the necessa...
282
  		int
26aee9ed   Pavel Govyadinov   changed circle cl...
283
284
285
286
287
  		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...
288
  			while(i > 0 && i < L.size()-1)
10654a1f   Pavel Govyadinov   added the necessa...
289
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
290
291
  //				std::cerr << "Trying " << i << std::endl;
  //				std::cerr << "l is " << l << ", L[" << i << "]" << L[i] << std::endl;
26aee9ed   Pavel Govyadinov   changed circle cl...
292
  				if(l < L[i])
10654a1f   Pavel Govyadinov   added the necessa...
293
  				{
26aee9ed   Pavel Govyadinov   changed circle cl...
294
  					max = i;
26aee9ed   Pavel Govyadinov   changed circle cl...
295
  					i = min+(max-min)/2;
10654a1f   Pavel Govyadinov   added the necessa...
296
  				}
26aee9ed   Pavel Govyadinov   changed circle cl...
297
  				else if(L[i] <= l && L[i+1] >= l)
10654a1f   Pavel Govyadinov   added the necessa...
298
299
300
301
302
  				{
  					break;
  				}
  				else
  				{
26aee9ed   Pavel Govyadinov   changed circle cl...
303
  					min = i;
26aee9ed   Pavel Govyadinov   changed circle cl...
304
  					i = min+(max-min)/2;
10654a1f   Pavel Govyadinov   added the necessa...
305
306
307
308
  				}
  			}
  			return i;
  		}
5b6c317a   Pavel Govyadinov   implemented the c...
309
  
9c97e126   David Mayerich   added an axis-ali...
310
311
  	public:
  		///default constructor
26aee9ed   Pavel Govyadinov   changed circle cl...
312
  		cylinder()
0311ab81   Pavel Govyadinov   fixed some issues...
313
  		// : centerline<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
314
315
316
  		{
  
  		}
9c97e126   David Mayerich   added an axis-ali...
317
  
5b6c317a   Pavel Govyadinov   implemented the c...
318
  		///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...
319
  		///@param inP:  Vector of stim vec3 composing the points of the centerline.
5b6c317a   Pavel Govyadinov   implemented the c...
320
  		///@param inM:  Vector of stim vecs composing the radii of the centerline.
0311ab81   Pavel Govyadinov   fixed some issues...
321
322
323
  		cylinder(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
  			: centerline<T>(inP)
  		{
5b6c317a   Pavel Govyadinov   implemented the c...
324
325
326
  			init(inP, inM);
  		}
  
09049866   Pavel Govyadinov   fixed a drawing b...
327
328
329
330
331
332
333
  		///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...
334
  			stim::vec<T> zero(0.0,0.0);
09049866   Pavel Govyadinov   fixed a drawing b...
335
  			temp.resize(inM.size(), zero);
47fe6300   Pavel Govyadinov   fixed an accident...
336
337
  			for(int i = 0; i < inM.size(); i++)
  				temp[i][0] = inM[i];
09049866   Pavel Govyadinov   fixed a drawing b...
338
339
340
  			init(inP, temp);
  		}
  
7d3162a2   Pavel Govyadinov   fixed majority of...
341
  
58ee2b21   David Mayerich   incorporated stim...
342
  		///Constructor defines a cylinder with centerline inP and magnitudes of zero
7d3162a2   Pavel Govyadinov   fixed majority of...
343
  		///@param inP: Vector of stim vec3 composing the points of the centerline
0311ab81   Pavel Govyadinov   fixed some issues...
344
345
346
  		cylinder(std::vector< stim::vec3<T> > inP)
  			: centerline<T>(inP)
  		{
96e8ab1f   David Mayerich   fixed stim::cylin...
347
  			std::vector< stim::vec<T> > inM;						//create an array of arbitrary magnitudes
9c97e126   David Mayerich   added an axis-ali...
348
349
350
351
352
  
  			stim::vec<T> zero;
  			zero.push_back(0);
  
  			inM.resize(inP.size(), zero);								//initialize the magnitude values to zero
58ee2b21   David Mayerich   incorporated stim...
353
354
355
  			init(inP, inM);
  		}
  
e21d1051   David Mayerich   cylinder can buil...
356
357
358
359
360
  		//assignment operator creates a cylinder from a centerline (default radius is zero)
  		cylinder& operator=(stim::centerline<T> c) {
  			init(c);
  		}
  
58ee2b21   David Mayerich   incorporated stim...
361
362
363
  		///Returns the number of points on the cylinder centerline
  
  		unsigned int size(){
0311ab81   Pavel Govyadinov   fixed some issues...
364
  			return N;
58ee2b21   David Mayerich   incorporated stim...
365
366
  		}
  
e21d1051   David Mayerich   cylinder can buil...
367
  		
10654a1f   Pavel Govyadinov   added the necessa...
368
  		///Returns a position vector at the given p-value (p value ranges from 0 to 1).
8495a970   Pavel Govyadinov   added comments to...
369
370
  		///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...
371
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
372
373
  		p(T pvalue)
  		{
bf23ee36   Pavel Govyadinov   more minor bug fi...
374
  			if(pvalue < 0.0 || pvalue > 1.0)
26aee9ed   Pavel Govyadinov   changed circle cl...
375
  			{
308a743c   David Mayerich   fixed class compa...
376
  				return stim::vec3<float>(-1,-1,-1);
26aee9ed   Pavel Govyadinov   changed circle cl...
377
  			}
10654a1f   Pavel Govyadinov   added the necessa...
378
379
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
0311ab81   Pavel Govyadinov   fixed some issues...
380
  			return (p(l,idx));
e21d1051   David Mayerich   cylinder can buil...
381
  		}
10654a1f   Pavel Govyadinov   added the necessa...
382
  
8495a970   Pavel Govyadinov   added comments to...
383
384
385
386
  		///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...
387
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
388
389
390
  		p(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
391
392
393
394
395
396
397
398
399
400
401
402
403
  				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...
404
405
  //			return(
  //			return (pos[idx] + (pos[idx+1]-pos[idx])*((l-L[idx])/(L[idx+1]- L[idx])));
10654a1f   Pavel Govyadinov   added the necessa...
406
407
408
  		}
  
  		///Returns a radius at the given p-value (p value ranges from 0 to 1).
8495a970   Pavel Govyadinov   added comments to...
409
410
  		///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...
411
  		T
26aee9ed   Pavel Govyadinov   changed circle cl...
412
413
  		r(T pvalue)
  		{
98eecaa9   David Mayerich   VS and win32 updates
414
415
416
417
  			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...
418
419
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
0311ab81   Pavel Govyadinov   fixed some issues...
420
  			return (r(l,idx));
e21d1051   David Mayerich   cylinder can buil...
421
  		}
10654a1f   Pavel Govyadinov   added the necessa...
422
  
8495a970   Pavel Govyadinov   added comments to...
423
424
425
426
  		///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...
427
  		T
26aee9ed   Pavel Govyadinov   changed circle cl...
428
429
430
  		r(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
431
432
433
434
435
436
437
438
  			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...
439
  				std::cout << Us[idx].str() << std::endl;
0311ab81   Pavel Govyadinov   fixed some issues...
440
441
442
443
444
  				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...
445
  			return(v2);
0311ab81   Pavel Govyadinov   fixed some issues...
446
447
  //			return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  	//	(
66fe63f3   Pavel Govyadinov   fixed minor typo
448
  		}
10654a1f   Pavel Govyadinov   added the necessa...
449
  
9c97e126   David Mayerich   added an axis-ali...
450
451
452
453
454
455
456
457
458
459
  		///	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...
460
  			for(unsigned int p = 0; p < N; p++)
9c97e126   David Mayerich   added an axis-ali...
461
462
463
464
465
466
467
468
469
470
471
  				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...
472
473
474
475
476
  		/// Returns the number of magnitude values at each point
  		unsigned nmags(){
  			return mags[0].size();
  		}
  
10654a1f   Pavel Govyadinov   added the necessa...
477
  		///returns the position of the point with a given pvalue and theta on the surface
8495a970   Pavel Govyadinov   added comments to...
478
479
480
  		///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...
481
  		stim::vec3<T>
10654a1f   Pavel Govyadinov   added the necessa...
482
483
  		surf(T pvalue, T theta)
  		{
bf23ee36   Pavel Govyadinov   more minor bug fi...
484
  			if(pvalue < 0.0 || pvalue > 1.0)
26aee9ed   Pavel Govyadinov   changed circle cl...
485
  			{
308a743c   David Mayerich   fixed class compa...
486
  				return stim::vec3<float>(-1,-1,-1);
26aee9ed   Pavel Govyadinov   changed circle cl...
487
  			} else {
10654a1f   Pavel Govyadinov   added the necessa...
488
489
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
308a743c   David Mayerich   fixed class compa...
490
  			stim::vec3<T> ps = p(l, idx); 
10654a1f   Pavel Govyadinov   added the necessa...
491
  			T m = r(l, idx);
8c4f5d84   Pavel Govyadinov   fixed the issue w...
492
493
494
495
  			s = e[idx];
  			s.center(ps);
  			s.normal(d(l, idx));
  			s.scale(m/e[idx].U.len());
10654a1f   Pavel Govyadinov   added the necessa...
496
  			return(s.p(theta));
26aee9ed   Pavel Govyadinov   changed circle cl...
497
  			}
10654a1f   Pavel Govyadinov   added the necessa...
498
499
  		}
  
8495a970   Pavel Govyadinov   added comments to...
500
  		///returns a vector of points necessary to create a circle at every position in the fiber.
26aee9ed   Pavel Govyadinov   changed circle cl...
501
  		///@param sides: the number of sides of each circle.	
308a743c   David Mayerich   fixed class compa...
502
  		std::vector<std::vector<vec3<T> > >
5b6c317a   Pavel Govyadinov   implemented the c...
503
504
  		getPoints(int sides)
  		{
308a743c   David Mayerich   fixed class compa...
505
  			std::vector<std::vector <vec3<T> > > points;
0311ab81   Pavel Govyadinov   fixed some issues...
506
507
  			points.resize(N);
  			for(int i = 0; i < N; i++)
5b6c317a   Pavel Govyadinov   implemented the c...
508
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
509
  				points[i] = e[i].getPoints(sides);
5b6c317a   Pavel Govyadinov   implemented the c...
510
  			}
8c4f5d84   Pavel Govyadinov   fixed the issue w...
511
  			return points;
5b6c317a   Pavel Govyadinov   implemented the c...
512
  		}
58ee2b21   David Mayerich   incorporated stim...
513
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
514
515
516
517
518
519
  		///returns the total length of the line at index j.
  		T
  		getl(int j)
  		{
  			return (L[j]);
  		}
58ee2b21   David Mayerich   incorporated stim...
520
521
  		/// Allows a point on the centerline to be accessed using bracket notation
  
308a743c   David Mayerich   fixed class compa...
522
  		vec3<T> operator[](unsigned int i){
3e5d3ad3   Pavel Govyadinov   merged the change...
523
  			return e[i].P;
58ee2b21   David Mayerich   incorporated stim...
524
525
526
527
528
529
530
  		}
  
  		/// Returns the total length of the cylinder centerline
  		T length(){
  			return L.back();
  		}
  
9c97e126   David Mayerich   added an axis-ali...
531
532
533
534
535
536
537
  		/// 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...
538
  			//vec3<T> p0, p1;					//allocate space for both points in a single segment
9c97e126   David Mayerich   added an axis-ali...
539
540
541
542
543
544
545
  
  			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...
546
  			for(unsigned p = 1; p < N; p++){
9c97e126   David Mayerich   added an axis-ali...
547
548
549
550
551
552
553
  
  				//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...
554
  				M += (m0 + m1)/(T)2.0 * len;
9c97e126   David Mayerich   added an axis-ali...
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  
  				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...
569
570
571
572
573
  		/// 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...
574
  			std::vector< vec3<T> > result;
9c97e126   David Mayerich   added an axis-ali...
575
  
1b2858a2   David Mayerich   fixed some commen...
576
  			vec3<T> p0 = e[0].P;									//initialize p0 to the first point on the centerline
308a743c   David Mayerich   fixed class compa...
577
  			vec3<T> p1;
1b2858a2   David Mayerich   fixed some commen...
578
  			unsigned N = size();									//number of points in the current centerline
58ee2b21   David Mayerich   incorporated stim...
579
580
581
  
  			//for each line segment on the centerline
  			for(unsigned int i = 1; i < N; i++){
1b2858a2   David Mayerich   fixed some commen...
582
  				p1 = e[i].P;										//get the second point in the line segment
58ee2b21   David Mayerich   incorporated stim...
583
  
1b2858a2   David Mayerich   fixed some commen...
584
585
  				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)
58ee2b21   David Mayerich   incorporated stim...
586
  
308a743c   David Mayerich   fixed class compa...
587
  				size_t nsteps = (size_t)std::ceil(d / spacing);		//calculate the number of steps to take along the segment to meet the spacing criteria
1b2858a2   David Mayerich   fixed some commen...
588
  				T stepsize = (T)1.0 / nsteps;						//calculate the parametric step size between new centerline points
58ee2b21   David Mayerich   incorporated stim...
589
590
591
  
  				//for each step along the line segment
  				for(unsigned s = 0; s < nsteps; s++){
1b2858a2   David Mayerich   fixed some commen...
592
593
  					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
58ee2b21   David Mayerich   incorporated stim...
594
595
  				}
  
1b2858a2   David Mayerich   fixed some commen...
596
  				p0 = p1;											//shift the points to move to the next line segment
58ee2b21   David Mayerich   incorporated stim...
597
598
  			}
  
1b2858a2   David Mayerich   fixed some commen...
599
  			result.push_back(e[size() - 1].P);						//push the last point in the centerline
9c97e126   David Mayerich   added an axis-ali...
600
  
58ee2b21   David Mayerich   incorporated stim...
601
602
  			return cylinder<T>(result);
  
e21d1051   David Mayerich   cylinder can buil...
603
  		}*/
9c97e126   David Mayerich   added an axis-ali...
604
  
26aee9ed   Pavel Govyadinov   changed circle cl...
605
  		
5b6c317a   Pavel Govyadinov   implemented the c...
606
607
608
609
  };
  
  }
  #endif