Blame view

stim/visualization/cylinder.h 21.6 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
  class cylinder : public centerline<T> {
  protected:
ee17b90b   Jiaming Guo   make it compatibl...
14
15
  	
  	using stim::centerline<T>::d;
e21d1051   David Mayerich   cylinder can buil...
16
  
e21d1051   David Mayerich   cylinder can buil...
17
  	std::vector< stim::vec3<T> > U;					//stores the array of U vectors defining the Frenet frame
7115e973   David Mayerich   removed the magni...
18
  	std::vector< T > R;				//stores a list of magnitudes for each point in the centerline (assuming mags[0] is the radius)
e21d1051   David Mayerich   cylinder can buil...
19
  
e21d1051   David Mayerich   cylinder can buil...
20
  	using stim::centerline<T>::findIdx;
49061f1d   Pavel Govyadinov   Modified centerli...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  
  	void init() {
  		U.resize(size());			//allocate space for the frenet frame vectors
  //		if (R.size() != 0)
  		R.resize(size());
  
  		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
  		}
  		U[size() - 1] = c.U;							//for the last point, duplicate the final frenet frame vector
  	}
26aee9ed   Pavel Govyadinov   changed circle cl...
35
  	
e21d1051   David Mayerich   cylinder can buil...
36
37
  	//calculates the U values for each point to initialize the frenet frame
  	//	this function assumes that the centerline has already been set
e21d1051   David Mayerich   cylinder can buil...
38
  
e21d1051   David Mayerich   cylinder can buil...
39
  public:
e21d1051   David Mayerich   cylinder can buil...
40
  
eb554b48   David Mayerich   bug fixes for Net...
41
42
  	using stim::centerline<T>::size;
  	using stim::centerline<T>::at;
ee17b90b   Jiaming Guo   make it compatibl...
43
44
  	using stim::centerline<T>::L;
  	using stim::centerline<T>::length;
eb554b48   David Mayerich   bug fixes for Net...
45
  
ee17b90b   Jiaming Guo   make it compatibl...
46
  	cylinder() : centerline<T>(){}
e21d1051   David Mayerich   cylinder can buil...
47
  
ee17b90b   Jiaming Guo   make it compatibl...
48
  	cylinder(centerline<T>c) : centerline<T>(c) {
e21d1051   David Mayerich   cylinder can buil...
49
  		init();
e21d1051   David Mayerich   cylinder can buil...
50
  	}
49061f1d   Pavel Govyadinov   Modified centerli...
51
52
53
54
55
56
  
  	cylinder(std::vector<stim::vec3<T> > p, std::vector<T> s)
  		: centerline<T>(p)
  	{
  		R = s;
  		init();
ac278135   Jiaming Guo   new stuff here
57
  	}
49061f1d   Pavel Govyadinov   Modified centerli...
58
  
ac278135   Jiaming Guo   new stuff here
59
  /*
49061f1d   Pavel Govyadinov   Modified centerli...
60
61
  	cylinder(stim::centerline<T> p, std::vector<T> s)
  	{
ac278135   Jiaming Guo   new stuff here
62
  		d = p;
49061f1d   Pavel Govyadinov   Modified centerli...
63
  		init();
ac278135   Jiaming Guo   new stuff here
64
65
  	}
  */
3b14b0f5   Pavel Govyadinov   added operators f...
66
  	
ee17b90b   Jiaming Guo   make it compatibl...
67
68
69
70
71
  	//cylinder(centerline<T>c, T r) : centerline(c) {
  	//	init();
  	//	//add_mag(r);
  	//}
  
e21d1051   David Mayerich   cylinder can buil...
72
  	//initialize a cylinder with a list of points and magnitude values
ee17b90b   Jiaming Guo   make it compatibl...
73
74
75
76
  	//cylinder(centerline<T>c, std::vector<T> r) : centerline(c){
  	//	init();
  	//	//add_mag(r);
  	//}
e21d1051   David Mayerich   cylinder can buil...
77
  
58674036   Jiaming Guo   fix minor error f...
78
79
80
81
82
83
  	//copy the original radius
  	void copy_r(std::vector<T> radius) {
  		for (unsigned i = 0; i < radius.size(); i++)
  			R[i] = radius[i];
  	}
  
e21d1051   David Mayerich   cylinder can buil...
84
85
86
87
  	///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.
7115e973   David Mayerich   removed the magni...
88
  	T r(T l, int idx) {
e21d1051   David Mayerich   cylinder can buil...
89
  		T a = (l - L[idx]) / (L[idx + 1] - L[idx]);
7115e973   David Mayerich   removed the magni...
90
  		T v2 = (R[idx] + (R[idx + 1] - R[idx])*a);
e21d1051   David Mayerich   cylinder can buil...
91
92
93
94
95
96
97
  		
  		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).
720240c8   Jiaming Guo   everything now is...
98
  	T rl(T pvalue) {
7115e973   David Mayerich   removed the magni...
99
100
  		if (pvalue <= 0.0) return R[0];
  		if (pvalue >= 1.0) return R[size() - 1];
e21d1051   David Mayerich   cylinder can buil...
101
102
103
  
  		T l = pvalue*L[L.size() - 1];
  		int idx = findIdx(l);
0d0ef1a9   David Mayerich   Adapted classes t...
104
  		return r(l, idx);
e21d1051   David Mayerich   cylinder can buil...
105
106
  	}
  
eb554b48   David Mayerich   bug fixes for Net...
107
108
  	///	Returns the magnitude at the given index
  	///	@param i is the index of the desired point
7115e973   David Mayerich   removed the magni...
109
  	/// @param r is the index of the magnitude value
720240c8   Jiaming Guo   everything now is...
110
111
112
  	T r(unsigned i) {
  		return R[i];
  	}
eb554b48   David Mayerich   bug fixes for Net...
113
  
0e0feff0   Jiaming Guo   fixed bugs in mer...
114
  
eb554b48   David Mayerich   bug fixes for Net...
115
  	///adds a magnitude to each point in the cylinder
7115e973   David Mayerich   removed the magni...
116
  	/*void add_mag(V val = 0) {
eb554b48   David Mayerich   bug fixes for Net...
117
118
  		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
7115e973   David Mayerich   removed the magni...
119
120
  			R[i].push_back(val);				//add this value to the magnitude vector at each point
  	}*/
eb554b48   David Mayerich   bug fixes for Net...
121
122
  
  	//adds a magnitude based on a list of magnitudes for each point
7115e973   David Mayerich   removed the magni...
123
  	/*void add_mag(std::vector<T> val) {
eb554b48   David Mayerich   bug fixes for Net...
124
125
  		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
7115e973   David Mayerich   removed the magni...
126
127
  			R[i].push_back(val[i]);				//add this value to the magnitude vector at each point
  	}*/
eb554b48   David Mayerich   bug fixes for Net...
128
129
  
  	//sets the value of magnitude m at point i
7115e973   David Mayerich   removed the magni...
130
131
  	void set_r(size_t i, T r) {
  		R[i] = r;
eb554b48   David Mayerich   bug fixes for Net...
132
133
  	}
  
7115e973   David Mayerich   removed the magni...
134
  	/*size_t nmags() {
eb554b48   David Mayerich   bug fixes for Net...
135
  		if (M.size() == 0) return 0;
7115e973   David Mayerich   removed the magni...
136
137
  		else return R[0].size();
  	}*/
eb554b48   David Mayerich   bug fixes for Net...
138
  
e21d1051   David Mayerich   cylinder can buil...
139
  	///Returns a circle representing the cylinder cross section at point i
7115e973   David Mayerich   removed the magni...
140
141
  	stim::circle<T> circ(size_t i) {
  		return stim::circle<T>(at(i), R[i], d(i), U[i]);
e21d1051   David Mayerich   cylinder can buil...
142
143
144
  	}
  
  	///Returns an OBJ object representing the cylinder with a radial tesselation value of N using magnitude m
7115e973   David Mayerich   removed the magni...
145
  	stim::obj<T> OBJ(size_t N) {
e21d1051   David Mayerich   cylinder can buil...
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
  		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...
174
  			}
e21d1051   David Mayerich   cylinder can buil...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  			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...
191
  
eb554b48   David Mayerich   bug fixes for Net...
192
193
194
195
196
  	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++)
7115e973   David Mayerich   removed the magni...
197
  			ss << std::vector< stim::vec3<T> >::at(i) << "   r = " << R[i] << "   u = " << U[i] << std::endl;
eb554b48   David Mayerich   bug fixes for Net...
198
199
200
201
202
203
204
  		ss << "--------------------" << std::endl;
  
  		return ss.str();
  	}
  
  	/// Integrates a magnitude value along the cylinder.
  	/// @param m is the magnitude value to be integrated (this is usually the radius)
7115e973   David Mayerich   removed the magni...
205
  	T integrate() {
58674036   Jiaming Guo   fix minor error f...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  		T sum = 0;							//initialize the integral to zero
  		if (L.size() == 1)
  			return sum;
  		else {					
  			T m0, m1;						//allocate space for both magnitudes in a single segment
  			m0 = R[0];					//initialize the first point and magnitude to the first point in the cylinder
  			T len = L[1];					//allocate space for the segment length
  
  
  			for (unsigned p = 1; p < size(); p++) {				//for every consecutive point in the cylinder
  				m1 = R[p];
  				if (p > 1) len = (L[p] - L[p - 1]);		//calculate the segment length using the L array
  				sum += (m0 + m1) / (T)2.0 * len;				//add the average magnitude, weighted by the segment length
  				m0 = m1;									//move to the next segment by shifting points
  			}
  			return sum;			//return the integral
eb554b48   David Mayerich   bug fixes for Net...
222
  		}
eb554b48   David Mayerich   bug fixes for Net...
223
224
225
226
227
228
229
230
  	}
  
  	/// 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) {
  		cylinder<T> c = stim::centerline<T>::resample(spacing);			//resample the centerline and use it to create a new cylinder
  
7115e973   David Mayerich   removed the magni...
231
232
233
234
235
  		//size_t nm = nmags();											//get the number of magnitude values in the current cylinder
  		//if (nm > 0) {													//if there are magnitude values
  		//	std::vector<T> magvec(nm, 0);							//create a magnitude vector for a single point
  		//	c.M.resize(c.size(), magvec);									//allocate space for a magnitude vector at each point of the new cylinder
  		//}
eb554b48   David Mayerich   bug fixes for Net...
236
237
238
239
240
  
  		T l, t;
  		for (size_t i = 0; i < c.size(); i++) {							//for each point in the new cylinder
  			l = c.L[i];													//get the length along the new cylinder
  			t = l / length();										//calculate the parameter value along the new cylinder
7115e973   David Mayerich   removed the magni...
241
242
243
  			//for (size_t mag = 0; mag < nm; mag++) {							//for each magnitude value
  			c.R[i] = r(t);								//retrieve the interpolated magnitude from the current cylinder and store it in the new one
  			//}
eb554b48   David Mayerich   bug fixes for Net...
244
  		}
0d0ef1a9   David Mayerich   Adapted classes t...
245
246
  		return c;
  	}
eb554b48   David Mayerich   bug fixes for Net...
247
  
0d0ef1a9   David Mayerich   Adapted classes t...
248
249
250
251
252
253
254
255
256
257
  	std::vector< stim::cylinder<T> > split(unsigned int idx) {
  
  		unsigned N = size();
  		std::vector< stim::centerline<T> > LL;
  		LL.resize(2);
  		LL = (*this).centerline<T>::split(idx);
  		std::vector< stim::cylinder<T> > C(LL.size());
  		unsigned i = 0;
  		C[0] = LL[0];
  		//C[0].R.resize(idx);
da89bce9   Jiaming Guo   fixed splitting bugs
258
  		for (; i < idx + 1; i++) {
0d0ef1a9   David Mayerich   Adapted classes t...
259
260
261
262
263
264
265
  			//for(unsigned d = 0; d < 3; d++)
  			//C[0][i][d] = LL[0].c[i][d];
  			C[0].R[i] = R[i];
  			//C[0].R[i].resize(1);
  		}
  		if (C.size() == 2) {
  			C[1] = LL[1];
da89bce9   Jiaming Guo   fixed splitting bugs
266
  			i--;
0d0ef1a9   David Mayerich   Adapted classes t...
267
268
269
270
271
272
273
274
  			//C[1].M.resize(N - idx);
  			for (; i < N; i++) {
  				//for(unsigned d = 0; d < 3; d++)
  				//C[1][i - idx][d] = LL[1].c[i - idx][d];
  				C[1].R[i - idx] = R[i];
  				//C[1].M[i - idx].resize(1);
  			}
  		}
eb554b48   David Mayerich   bug fixes for Net...
275
  
0d0ef1a9   David Mayerich   Adapted classes t...
276
  		return C;
eb554b48   David Mayerich   bug fixes for Net...
277
278
  	}
  
e21d1051   David Mayerich   cylinder can buil...
279
280
281
  
  		/*
  		///inits the cylinder from a list of points (std::vector of stim::vec3 --inP) and magnitudes (inM)
5b6c317a   Pavel Govyadinov   implemented the c...
282
  		void
e21d1051   David Mayerich   cylinder can buil...
283
284
285
  		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...
286
287
  			stim::vec3<float> v1;
  			stim::vec3<float> v2;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
288
  			e.resize(inP.size());
0311ab81   Pavel Govyadinov   fixed some issues...
289
290
291
292
  
  			norms.resize(inP.size());
  			Us.resize(inP.size());
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
293
294
  			if(inP.size() < 2)
  				return;
8495a970   Pavel Govyadinov   added comments to...
295
296
  
  			//calculate each L.
1b2858a2   David Mayerich   fixed some commen...
297
298
299
  			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...
300
  			for(size_t i = 1; i < L.size(); i++)
10654a1f   Pavel Govyadinov   added the necessa...
301
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
302
  				temp += (inP[i-1] - inP[i]).len();
bf23ee36   Pavel Govyadinov   more minor bug fi...
303
  				L[i] = temp;
10654a1f   Pavel Govyadinov   added the necessa...
304
  			}
9c97e126   David Mayerich   added an axis-ali...
305
  
308a743c   David Mayerich   fixed class compa...
306
  			stim::vec3<T> dr = (inP[1] - inP[0]).norm();
7115e973   David Mayerich   removed the magni...
307
  			s = stim::circle<T>(inP[0], inR[0][0], dr, stim::vec3<T>(1,0,0));
0311ab81   Pavel Govyadinov   fixed some issues...
308
  			norms[0] = s.N;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
309
  			e[0] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
310
  			Us[0] = e[0].U;
308a743c   David Mayerich   fixed class compa...
311
  			for(size_t i = 1; i < inP.size()-1; i++)
8c4f5d84   Pavel Govyadinov   fixed the issue w...
312
313
314
315
316
317
  			{
  				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...
318
319
320
  
  				norms[i] = s.N;
  
7115e973   David Mayerich   removed the magni...
321
  				s.scale(inR[i][0]/inR[i-1][0]);
8c4f5d84   Pavel Govyadinov   fixed the issue w...
322
  				e[i] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
323
  				Us[i] = e[i].U;
8c4f5d84   Pavel Govyadinov   fixed the issue w...
324
325
326
327
328
329
  			}
  			
  			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...
330
331
332
  
  			norms[j] = s.N;
  
7115e973   David Mayerich   removed the magni...
333
  			s.scale(inR[j][0]/inR[j-1][0]);
8c4f5d84   Pavel Govyadinov   fixed the issue w...
334
  			e[j] = s;
0311ab81   Pavel Govyadinov   fixed some issues...
335
  			Us[j] = e[j].U;
26aee9ed   Pavel Govyadinov   changed circle cl...
336
337
  		}
  		
8495a970   Pavel Govyadinov   added comments to...
338
  		///returns the direction vector at point idx.
308a743c   David Mayerich   fixed class compa...
339
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
340
341
342
343
  		d(int idx)
  		{
  			if(idx == 0)
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
344
345
346
347
348
349
350
  				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...
351
  			}
0311ab81   Pavel Govyadinov   fixed some issues...
352
  			else if(idx == N-1)
26aee9ed   Pavel Govyadinov   changed circle cl...
353
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
354
355
356
357
358
359
360
  				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...
361
362
363
  			}
  			else
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
364
  //				return (e[idx+1].P - e[idx].P).norm();
0311ab81   Pavel Govyadinov   fixed some issues...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  //				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...
380
381
382
  			} 
  	//		return e[idx].N;	
  
10654a1f   Pavel Govyadinov   added the necessa...
383
384
  		}
  
308a743c   David Mayerich   fixed class compa...
385
  		stim::vec3<T>
8c4f5d84   Pavel Govyadinov   fixed the issue w...
386
  		d(T l, int idx)
26aee9ed   Pavel Govyadinov   changed circle cl...
387
  		{
0311ab81   Pavel Govyadinov   fixed some issues...
388
  			if(idx == 0 || idx == N-1)
10654a1f   Pavel Govyadinov   added the necessa...
389
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
390
391
  				return norms[idx];
  //				return e[idx].N;
10654a1f   Pavel Govyadinov   added the necessa...
392
  			}
8c4f5d84   Pavel Govyadinov   fixed the issue w...
393
394
  			else
  			{
0311ab81   Pavel Govyadinov   fixed some issues...
395
  				
8c4f5d84   Pavel Govyadinov   fixed the issue w...
396
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
397
398
  				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...
399
  			} 
10654a1f   Pavel Govyadinov   added the necessa...
400
401
  		}
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
402
  
8495a970   Pavel Govyadinov   added comments to...
403
404
  		///finds the index of the point closest to the length l on the lower bound.
  		///binary search.
10654a1f   Pavel Govyadinov   added the necessa...
405
  		int
26aee9ed   Pavel Govyadinov   changed circle cl...
406
407
408
409
410
  		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...
411
  			while(i > 0 && i < L.size()-1)
10654a1f   Pavel Govyadinov   added the necessa...
412
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
413
414
  //				std::cerr << "Trying " << i << std::endl;
  //				std::cerr << "l is " << l << ", L[" << i << "]" << L[i] << std::endl;
26aee9ed   Pavel Govyadinov   changed circle cl...
415
  				if(l < L[i])
10654a1f   Pavel Govyadinov   added the necessa...
416
  				{
26aee9ed   Pavel Govyadinov   changed circle cl...
417
  					max = i;
26aee9ed   Pavel Govyadinov   changed circle cl...
418
  					i = min+(max-min)/2;
10654a1f   Pavel Govyadinov   added the necessa...
419
  				}
26aee9ed   Pavel Govyadinov   changed circle cl...
420
  				else if(L[i] <= l && L[i+1] >= l)
10654a1f   Pavel Govyadinov   added the necessa...
421
422
423
424
425
  				{
  					break;
  				}
  				else
  				{
26aee9ed   Pavel Govyadinov   changed circle cl...
426
  					min = i;
26aee9ed   Pavel Govyadinov   changed circle cl...
427
  					i = min+(max-min)/2;
10654a1f   Pavel Govyadinov   added the necessa...
428
429
430
431
  				}
  			}
  			return i;
  		}
5b6c317a   Pavel Govyadinov   implemented the c...
432
  
9c97e126   David Mayerich   added an axis-ali...
433
434
  	public:
  		///default constructor
26aee9ed   Pavel Govyadinov   changed circle cl...
435
  		cylinder()
0311ab81   Pavel Govyadinov   fixed some issues...
436
  		// : centerline<T>()
26aee9ed   Pavel Govyadinov   changed circle cl...
437
438
439
  		{
  
  		}
9c97e126   David Mayerich   added an axis-ali...
440
  
5b6c317a   Pavel Govyadinov   implemented the c...
441
  		///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...
442
  		///@param inP:  Vector of stim vec3 composing the points of the centerline.
5b6c317a   Pavel Govyadinov   implemented the c...
443
  		///@param inM:  Vector of stim vecs composing the radii of the centerline.
0311ab81   Pavel Govyadinov   fixed some issues...
444
445
446
  		cylinder(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
  			: centerline<T>(inP)
  		{
5b6c317a   Pavel Govyadinov   implemented the c...
447
448
449
  			init(inP, inM);
  		}
  
09049866   Pavel Govyadinov   fixed a drawing b...
450
451
452
453
454
455
456
  		///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...
457
  			stim::vec<T> zero(0.0,0.0);
09049866   Pavel Govyadinov   fixed a drawing b...
458
  			temp.resize(inM.size(), zero);
47fe6300   Pavel Govyadinov   fixed an accident...
459
  			for(int i = 0; i < inM.size(); i++)
7115e973   David Mayerich   removed the magni...
460
  				temp[i][0] = inR[i];
09049866   Pavel Govyadinov   fixed a drawing b...
461
462
463
  			init(inP, temp);
  		}
  
7d3162a2   Pavel Govyadinov   fixed majority of...
464
  
58ee2b21   David Mayerich   incorporated stim...
465
  		///Constructor defines a cylinder with centerline inP and magnitudes of zero
7d3162a2   Pavel Govyadinov   fixed majority of...
466
  		///@param inP: Vector of stim vec3 composing the points of the centerline
0311ab81   Pavel Govyadinov   fixed some issues...
467
468
469
  		cylinder(std::vector< stim::vec3<T> > inP)
  			: centerline<T>(inP)
  		{
96e8ab1f   David Mayerich   fixed stim::cylin...
470
  			std::vector< stim::vec<T> > inM;						//create an array of arbitrary magnitudes
9c97e126   David Mayerich   added an axis-ali...
471
472
473
474
475
  
  			stim::vec<T> zero;
  			zero.push_back(0);
  
  			inM.resize(inP.size(), zero);								//initialize the magnitude values to zero
58ee2b21   David Mayerich   incorporated stim...
476
477
478
  			init(inP, inM);
  		}
  
e21d1051   David Mayerich   cylinder can buil...
479
480
481
482
483
  		//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...
484
485
486
  		///Returns the number of points on the cylinder centerline
  
  		unsigned int size(){
0311ab81   Pavel Govyadinov   fixed some issues...
487
  			return N;
58ee2b21   David Mayerich   incorporated stim...
488
489
  		}
  
e21d1051   David Mayerich   cylinder can buil...
490
  		
10654a1f   Pavel Govyadinov   added the necessa...
491
  		///Returns a position vector at the given p-value (p value ranges from 0 to 1).
8495a970   Pavel Govyadinov   added comments to...
492
493
  		///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...
494
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
495
496
  		p(T pvalue)
  		{
bf23ee36   Pavel Govyadinov   more minor bug fi...
497
  			if(pvalue < 0.0 || pvalue > 1.0)
26aee9ed   Pavel Govyadinov   changed circle cl...
498
  			{
308a743c   David Mayerich   fixed class compa...
499
  				return stim::vec3<float>(-1,-1,-1);
26aee9ed   Pavel Govyadinov   changed circle cl...
500
  			}
10654a1f   Pavel Govyadinov   added the necessa...
501
502
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
0311ab81   Pavel Govyadinov   fixed some issues...
503
  			return (p(l,idx));
e21d1051   David Mayerich   cylinder can buil...
504
  		}
10654a1f   Pavel Govyadinov   added the necessa...
505
  
8495a970   Pavel Govyadinov   added comments to...
506
507
508
509
  		///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...
510
  		stim::vec3<T>
26aee9ed   Pavel Govyadinov   changed circle cl...
511
512
513
  		p(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
514
515
516
517
518
519
520
521
522
523
524
525
526
  				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...
527
528
  //			return(
  //			return (pos[idx] + (pos[idx+1]-pos[idx])*((l-L[idx])/(L[idx+1]- L[idx])));
10654a1f   Pavel Govyadinov   added the necessa...
529
530
531
  		}
  
  		///Returns a radius at the given p-value (p value ranges from 0 to 1).
8495a970   Pavel Govyadinov   added comments to...
532
533
  		///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...
534
  		T
26aee9ed   Pavel Govyadinov   changed circle cl...
535
536
  		r(T pvalue)
  		{
98eecaa9   David Mayerich   VS and win32 updates
537
538
539
540
  			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...
541
542
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
0311ab81   Pavel Govyadinov   fixed some issues...
543
  			return (r(l,idx));
e21d1051   David Mayerich   cylinder can buil...
544
  		}
10654a1f   Pavel Govyadinov   added the necessa...
545
  
8495a970   Pavel Govyadinov   added comments to...
546
547
548
549
  		///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...
550
  		T
26aee9ed   Pavel Govyadinov   changed circle cl...
551
552
553
  		r(T l, int idx)
  		{
  				T rat = (l-L[idx])/(L[idx+1]-L[idx]);
0311ab81   Pavel Govyadinov   fixed some issues...
554
555
556
557
558
559
560
561
  			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...
562
  				std::cout << Us[idx].str() << std::endl;
0311ab81   Pavel Govyadinov   fixed some issues...
563
564
565
566
567
  				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...
568
  			return(v2);
0311ab81   Pavel Govyadinov   fixed some issues...
569
570
  //			return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  	//	(
66fe63f3   Pavel Govyadinov   fixed minor typo
571
  		}
10654a1f   Pavel Govyadinov   added the necessa...
572
  
9c97e126   David Mayerich   added an axis-ali...
573
574
575
576
577
578
579
580
581
582
  		///	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...
583
  			for(unsigned int p = 0; p < N; p++)
9c97e126   David Mayerich   added an axis-ali...
584
585
586
587
588
589
590
591
592
593
594
  				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...
595
596
597
598
599
  		/// Returns the number of magnitude values at each point
  		unsigned nmags(){
  			return mags[0].size();
  		}
  
10654a1f   Pavel Govyadinov   added the necessa...
600
  		///returns the position of the point with a given pvalue and theta on the surface
8495a970   Pavel Govyadinov   added comments to...
601
602
603
  		///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...
604
  		stim::vec3<T>
10654a1f   Pavel Govyadinov   added the necessa...
605
606
  		surf(T pvalue, T theta)
  		{
bf23ee36   Pavel Govyadinov   more minor bug fi...
607
  			if(pvalue < 0.0 || pvalue > 1.0)
26aee9ed   Pavel Govyadinov   changed circle cl...
608
  			{
308a743c   David Mayerich   fixed class compa...
609
  				return stim::vec3<float>(-1,-1,-1);
26aee9ed   Pavel Govyadinov   changed circle cl...
610
  			} else {
10654a1f   Pavel Govyadinov   added the necessa...
611
612
  			T l = pvalue*L[L.size()-1];
  			int idx = findIdx(l);
308a743c   David Mayerich   fixed class compa...
613
  			stim::vec3<T> ps = p(l, idx); 
10654a1f   Pavel Govyadinov   added the necessa...
614
  			T m = r(l, idx);
8c4f5d84   Pavel Govyadinov   fixed the issue w...
615
616
617
618
  			s = e[idx];
  			s.center(ps);
  			s.normal(d(l, idx));
  			s.scale(m/e[idx].U.len());
10654a1f   Pavel Govyadinov   added the necessa...
619
  			return(s.p(theta));
26aee9ed   Pavel Govyadinov   changed circle cl...
620
  			}
10654a1f   Pavel Govyadinov   added the necessa...
621
622
  		}
  
8495a970   Pavel Govyadinov   added comments to...
623
  		///returns a vector of points necessary to create a circle at every position in the fiber.
26aee9ed   Pavel Govyadinov   changed circle cl...
624
  		///@param sides: the number of sides of each circle.	
308a743c   David Mayerich   fixed class compa...
625
  		std::vector<std::vector<vec3<T> > >
5b6c317a   Pavel Govyadinov   implemented the c...
626
627
  		getPoints(int sides)
  		{
308a743c   David Mayerich   fixed class compa...
628
  			std::vector<std::vector <vec3<T> > > points;
0311ab81   Pavel Govyadinov   fixed some issues...
629
630
  			points.resize(N);
  			for(int i = 0; i < N; i++)
5b6c317a   Pavel Govyadinov   implemented the c...
631
  			{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
632
  				points[i] = e[i].getPoints(sides);
5b6c317a   Pavel Govyadinov   implemented the c...
633
  			}
8c4f5d84   Pavel Govyadinov   fixed the issue w...
634
  			return points;
5b6c317a   Pavel Govyadinov   implemented the c...
635
  		}
58ee2b21   David Mayerich   incorporated stim...
636
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
637
638
639
640
641
642
  		///returns the total length of the line at index j.
  		T
  		getl(int j)
  		{
  			return (L[j]);
  		}
58ee2b21   David Mayerich   incorporated stim...
643
644
  		/// Allows a point on the centerline to be accessed using bracket notation
  
308a743c   David Mayerich   fixed class compa...
645
  		vec3<T> operator[](unsigned int i){
3e5d3ad3   Pavel Govyadinov   merged the change...
646
  			return e[i].P;
58ee2b21   David Mayerich   incorporated stim...
647
648
649
650
651
652
653
  		}
  
  		/// Returns the total length of the cylinder centerline
  		T length(){
  			return L.back();
  		}
  
9c97e126   David Mayerich   added an axis-ali...
654
655
656
657
658
659
660
  		/// 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...
661
  			//vec3<T> p0, p1;					//allocate space for both points in a single segment
9c97e126   David Mayerich   added an axis-ali...
662
663
664
665
666
667
668
  
  			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...
669
  			for(unsigned p = 1; p < N; p++){
9c97e126   David Mayerich   added an axis-ali...
670
671
672
673
674
675
676
  
  				//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...
677
  				M += (m0 + m1)/(T)2.0 * len;
9c97e126   David Mayerich   added an axis-ali...
678
679
680
681
682
683
684
685
686
687
688
689
690
691
  
  				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...
692
693
694
695
696
  		/// 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...
697
  			std::vector< vec3<T> > result;
9c97e126   David Mayerich   added an axis-ali...
698
  
1b2858a2   David Mayerich   fixed some commen...
699
  			vec3<T> p0 = e[0].P;									//initialize p0 to the first point on the centerline
308a743c   David Mayerich   fixed class compa...
700
  			vec3<T> p1;
1b2858a2   David Mayerich   fixed some commen...
701
  			unsigned N = size();									//number of points in the current centerline
58ee2b21   David Mayerich   incorporated stim...
702
703
704
  
  			//for each line segment on the centerline
  			for(unsigned int i = 1; i < N; i++){
1b2858a2   David Mayerich   fixed some commen...
705
  				p1 = e[i].P;										//get the second point in the line segment
58ee2b21   David Mayerich   incorporated stim...
706
  
1b2858a2   David Mayerich   fixed some commen...
707
708
  				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...
709
  
308a743c   David Mayerich   fixed class compa...
710
  				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...
711
  				T stepsize = (T)1.0 / nsteps;						//calculate the parametric step size between new centerline points
58ee2b21   David Mayerich   incorporated stim...
712
713
714
  
  				//for each step along the line segment
  				for(unsigned s = 0; s < nsteps; s++){
1b2858a2   David Mayerich   fixed some commen...
715
716
  					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...
717
718
  				}
  
1b2858a2   David Mayerich   fixed some commen...
719
  				p0 = p1;											//shift the points to move to the next line segment
58ee2b21   David Mayerich   incorporated stim...
720
721
  			}
  
1b2858a2   David Mayerich   fixed some commen...
722
  			result.push_back(e[size() - 1].P);						//push the last point in the centerline
9c97e126   David Mayerich   added an axis-ali...
723
  
58ee2b21   David Mayerich   incorporated stim...
724
725
  			return cylinder<T>(result);
  
e21d1051   David Mayerich   cylinder can buil...
726
  		}*/
9c97e126   David Mayerich   added an axis-ali...
727
  
26aee9ed   Pavel Govyadinov   changed circle cl...
728
  		
5b6c317a   Pavel Govyadinov   implemented the c...
729
730
731
  };
  
  }
3b14b0f5   Pavel Govyadinov   added operators f...
732
  #endif