Commit 964b489815c44baab10fe2f6b2d952cf7a6fe549
1 parent
cc7e4705
added function to resample stim vec of points on fiber
Showing
1 changed file
with
58 additions
and
15 deletions
Show diff stats
stim/biomodels/fiber.h
... | ... | @@ -18,11 +18,11 @@ protected: |
18 | 18 | double **c; //centerline (array of double pointers) |
19 | 19 | |
20 | 20 | T* r; // array of fiber radii |
21 | -// // fibers this fiber intersects. | |
22 | 21 | ANNkd_tree* kdt; //kd-tree stores all points in the fiber for fast searching |
23 | 22 | |
24 | 23 | /// Initialize an empty fiber |
25 | - void init(){ | |
24 | + void init() | |
25 | + { | |
26 | 26 | kdt = NULL; |
27 | 27 | c=NULL; |
28 | 28 | r=NULL; |
... | ... | @@ -30,7 +30,8 @@ protected: |
30 | 30 | } |
31 | 31 | |
32 | 32 | /// Initialize a fiber with N centerline points (all located at [0, 0, 0] with radius 0) |
33 | - void init(unsigned int n){ | |
33 | + void init(unsigned int n) | |
34 | + { | |
34 | 35 | |
35 | 36 | N = n; //set the number of points |
36 | 37 | kdt = NULL; |
... | ... | @@ -61,27 +62,25 @@ protected: |
61 | 62 | gen_kdtree(); //generate the kd tree for the new fiber |
62 | 63 | } |
63 | 64 | |
64 | - void gen_kdtree(){ | |
65 | - | |
66 | - //create an array of data points | |
67 | - int n_data = N; | |
68 | - | |
65 | + /// generate a KD tree for points on fiber | |
66 | + void gen_kdtree() | |
67 | + { | |
68 | + int n_data = N; //create an array of data points | |
69 | 69 | ANNpointArray pts = (ANNpointArray)c; //cast the centerline list to an ANNpointArray |
70 | - | |
71 | 70 | kdt = new ANNkd_tree(pts, n_data, 3); //build a KD tree |
72 | 71 | } |
73 | 72 | |
74 | - | |
73 | + /// find distance between two points | |
75 | 74 | double dist(double* p0, double* p1){ |
76 | 75 | |
77 | - double sum = 0; | |
76 | + double sum = 0; // initialize variables | |
78 | 77 | float v; |
79 | - for(unsigned int d = 0; d < 3; d++){ | |
78 | + for(unsigned int d = 0; d < 3; d++) | |
79 | + { | |
80 | 80 | v = p1[d] - p0[d]; |
81 | 81 | sum +=v * v; |
82 | 82 | |
83 | 83 | } |
84 | - | |
85 | 84 | return sqrt(sum); |
86 | 85 | } |
87 | 86 | |
... | ... | @@ -112,8 +111,6 @@ protected: |
112 | 111 | } |
113 | 112 | |
114 | 113 | |
115 | - | |
116 | - | |
117 | 114 | public: |
118 | 115 | |
119 | 116 | fiber(){ |
... | ... | @@ -490,6 +487,52 @@ public: |
490 | 487 | stim::vec<T> back(){ |
491 | 488 | return get_vec(N-1); |
492 | 489 | } |
490 | + ////resample a fiber in the network | |
491 | + std::vector<stim::vec<T> > Resample(std::vector< stim::vec<T> > fiberPositions, float spacing=25.0) | |
492 | + { | |
493 | + | |
494 | + std::vector<T> v(3); //v-direction vector of the segment | |
495 | + stim::vec<T> p(3); //- intermediate point to be added | |
496 | + std::vector<T> p1(3); // p1 - starting point of an segment on the fiber, | |
497 | + std::vector<T> p2(3); // p2 - ending point, | |
498 | + double sum=0; //distance summation | |
499 | + | |
500 | + std::vector<stim::vec<T> > newPointList; // initialize list of new resampled points on the fiber | |
501 | + // for each point on the centerline (skip if it is the last point on centerline) | |
502 | + unsigned int N = fiberPositions.size(); // number of points on the fiber | |
503 | + for(unsigned int f=0; f< N-1; f++) | |
504 | + { | |
505 | + for(unsigned int d = 0; d < 3; d++) | |
506 | + { | |
507 | + p1[d] = fiberPositions[f][d]; // starting point of the segment on the centerline | |
508 | + p2[d] = fiberPositions[f + 1][d]; // ending point of the segment on the centerline | |
509 | + v[d] = p2[d] - p1[d]; //direction vector | |
510 | + sum +=v[d] * v[d]; //length of segment-distance between starting and ending point | |
511 | + } | |
512 | + newPointList.push_back(fiberPositions[f]); //always push the starting point | |
513 | + | |
514 | + T lengthSegment = sqrt(sum); //find Length of the segment as distance between the starting and ending points of the segment | |
515 | + | |
516 | + if(lengthSegment >= spacing) // if length of the segment is greater than standard deviation resample | |
517 | + { | |
518 | + // repeat resampling until accumulated stepsize is equsl to length of the segment | |
519 | + for(T step=0.0; step<lengthSegment; step+=spacing) | |
520 | + { | |
521 | + // calculate the resampled point by travelling step size in the direction of normalized gradient vector | |
522 | + for(unsigned int i=0; i<3;i++) | |
523 | + { | |
524 | + p[i] = p1[i] + v[i]*(step/lengthSegment); | |
525 | + } //for each dimension | |
526 | + // add this resampled points to the new fiber list | |
527 | + newPointList.push_back(p); | |
528 | + } | |
529 | + } | |
530 | + else // length of the segment is now less than standard deviation, push the ending point of the segment and proceed to the next point in the fiber | |
531 | + newPointList.push_back(fiberPositions[f+1]); | |
532 | + } | |
533 | + newPointList.push_back(fiberPositions[N-1]); //add the last point on the fiber to the new fiber list | |
534 | + return newPointList; | |
535 | + } | |
493 | 536 | |
494 | 537 | }; |
495 | 538 | ... | ... |