DFNgen  2.0
DFN Model Generator
generatingPoints.cpp
Go to the documentation of this file.
1 #include "generatingPoints.h"
2 #include <cmath>
3 #include <iostream>
4 #include <vector>
5 #include "structures.h"
6 #include "vectorFunctions.h"
8 #include <algorithm>
9 #include "insertShape.h"
10 
11 
12 /**************************************************************************/
13 /******************** Discretize Intersection ***************************/
18 std::vector<Point> discretizeLineOfIntersection(double *pt1, double *pt2, double dist){
19 
20  std::vector<Point> pointsList;
21 
22  // If reduced mesh, just save endpoints
23  if (visualizationMode == 1) {
24  Point pt;
25  pt.x = pt1[0];
26  pt.y = pt1[1];
27  pt.z = pt1[2];
28  pointsList.push_back(pt);
29  pt.x = pt2[0];
30  pt.y = pt2[1];
31  pt.z = pt2[2];
32  pointsList.push_back(pt);
33  return pointsList;
34  }
35 
36  double v[3] = {pt2[0]-pt1[0], pt2[1]-pt1[1], pt2[2]-pt1[2]}; // {x2-x1, y2-y1, z2-z1};
37  double p[3] = {pt1[0],pt1[1],pt1[2]}; // {x1, y1, z1};
38 
39 // double dist = magnitude((pt1[0]-pt2[0]), (pt1[1]-pt2[1]), (pt1[2]-pt2[2])); // (x1-x2), (y1-y2), (z1-z2));
40 
41  double nprime = std::ceil(2*dist/h);
42  double hprime = 1/nprime;
43  double *xx = new double[(int)nprime+1];
44  int i;
45  double temp = 0;
46 
47  for (i = 0; i < nprime; i++){ // Array from 0 - 1 with step = hprime
48  xx[i] = temp;
49  temp += hprime;
50  }
51 
52  xx[i] = 1; // Make sure last element is exactly 1
53 
54  pointsList.reserve((int) nprime+1);
55 
56  for (i = 0; i<nprime+1; i++){
57  pointsList.push_back((lineFunction3D(v, p, xx[i])));
58  }
59 
60  delete[] xx;
61  return pointsList;
62 }
63 
64 
65 /**************************************************************************/
66 /*********************** Parametric Line Function *************************/
72 Point lineFunction3D(double *v, double *point, double t){
73  Point pt;
74  pt.x = point[0] + v[0]*t;
75  pt.y = point[1] + v[1]*t;
76  pt.z = point[2] + v[2]*t;
77 
78  return pt;
79 }
80 
81 /**************************************************************************/
82 /****** Fisher Distributions for Generating polygons Normal Vectors *******/
93 double *fisherDistribution(double theta, double phi, double kappa, std::mt19937_64 &generator){
94 
95  double ck = (std::exp(kappa) - std::exp(-kappa))/kappa;
96  double v1[3];
97  v1[0] = sin(theta)*cos(phi);
98  v1[1] = sin(theta)*sin(phi);
99  v1[2] = cos(theta);
100 
101  double u[3] = {0,0,1};
102 
103  double *xProd = crossProduct(u,v1);
104  double R[9];
105 
106  // Get rotation matrix if normal vectors are not the same (if xProd is not zero vector)
107  if (!(std::abs(xProd[0]) <= eps && std::abs(xProd[1]) <= eps && std::abs(xProd[2]) <= eps )){
108  // Since vectors are normalized, sin = magnitude(AxB) and cos = A . B
109  double sin = sqrt(xProd[0]*xProd[0] + xProd[1]*xProd[1] + xProd[2]*xProd[2]);
110  double cos = dotProduct(u, v1);
111  double v[9] = {0, -xProd[2], xProd[1], xProd[2], 0, -xProd[0], -xProd[1], xProd[0], 0};
112  double scalar = (1.0f-cos)/(sin*sin);
113 
114  double vSquared[9];
115  vSquared[0] = (v[0]*v[0] + v[1]*v[3] + v[2]*v[6])*scalar;
116  vSquared[1] = (v[0]*v[1] + v[1]*v[4] + v[2]*v[7])*scalar;
117  vSquared[2] = (v[0]*v[2] + v[1]*v[5] + v[2]*v[8])*scalar;
118  vSquared[3] = (v[3]*v[0] + v[4]*v[3] + v[5]*v[6])*scalar;
119  vSquared[4] = (v[3]*v[1] + v[4]*v[4] + v[5]*v[7])*scalar;
120  vSquared[5] = (v[3]*v[2] + v[4]*v[5] + v[5]*v[8])*scalar;
121  vSquared[6] = (v[6]*v[0] + v[7]*v[3] + v[8]*v[6])*scalar;
122  vSquared[7] = (v[6]*v[1] + v[7]*v[4] + v[8]*v[7])*scalar;
123  vSquared[8] = (v[6]*v[2] + v[7]*v[5] + v[8]*v[8])*scalar;
124 
125  R[0] = 1 + v[0] + vSquared[0];
126  R[1] = 0 + v[1] + vSquared[1];
127  R[2] = 0 + v[2] + vSquared[2];
128  R[3] = 0 + v[3] + vSquared[3];
129  R[4] = 1 + v[4] + vSquared[4];
130  R[5] = 0 + v[5] + vSquared[5];
131  R[6] = 0 + v[6] + vSquared[6];
132  R[7] = 0 + v[7] + vSquared[7];
133  R[8] = 1 + v[8] + vSquared[8];
134  }
135  else {
136  // Identity Matrix
137  R[0]=1; R[1]=0; R[2]=0;
138  R[3]=0; R[4]=1; R[5]=0;
139  R[6]=0; R[7]=0; R[8]=1;
140  }
141 
142  delete[] xProd;
143  // Random number generator on [0,1]
144 
145  std::uniform_real_distribution<double> thetaDist(0.0,M_PI);
146  std::uniform_real_distribution<double> distribution(0.0,1.0);
147  double thetaRandom = thetaDist(generator);
148  double y = distribution(generator);
149  double V[2] = {std::cos(thetaRandom), std::sin(thetaRandom)};
150  double w = 1/kappa*std::log(std::exp(-kappa)+kappa*ck*y);
151  double temp = std::sqrt(1-(w*w));
152 
153  V[0] = temp*V[0];
154  V[1] = temp*V[1];
155 
156  // Matrix multiply with R
157  double *vec = new double[3];
158  vec[0] =V[0]*R[0] + V[1]*R[1] + w*R[2];
159  vec[1] =V[0]*R[3] + V[1]*R[4] + w*R[5];
160  vec[2] =V[0]*R[6] + V[1]*R[7] + w*R[8];
161 
162  return vec;
163 }
164 
165 
166 /**************************************************************************/
167 /******************* Returns random TRANSLATION ***************************/
177 double *randomTranslation(std::mt19937_64 &generator, float xMin, float xMax, float yMin, float yMax, float zMin, float zMax) {
178 
179  double *t = new double[3];
180 
181  // Setup for getting random x location
182  std::uniform_real_distribution<double> distributionX (xMin, xMax);
183  t[0] = distributionX(generator);
184 
185  // Setup for getting random y location
186  std::uniform_real_distribution<double> distributionY (yMin, yMax);
187  t[1] = distributionY(generator);
188 
189  // Setup for getting random z location
190  std::uniform_real_distribution<double> distributionZ (zMin, zMax);
191  t[2] = distributionZ(generator);
192 
193  return t;
194 }
195 
196 
197 /**************************************************************************/
198 /******************* Truncated Power-Law ********************************/
208 float truncatedPowerLaw(float randomNum, float min, float max, float alpha){
209 
210  float temp = 1-randomNum + (randomNum*std::pow((min/max), alpha));
211  temp = min * std::pow(temp,(-1/alpha));
212 
213  return temp;
214 }
215 
216 
217 /**************************************************************************/
218 /********** Generates Theta Array for Generating Ellipses ***************/
224 void generateTheta(float * &thetaArray, float aspectRatio, int nPoints){
225  // a = x radius
226  // b = y radius
227 
228  float a = 1;
229  float b = aspectRatio;
230 
231  double temp1 = ((a-b)/(a+b));
232  temp1 = temp1*temp1;
233  double c = M_PI *(a+b)*(1 + (3 * temp1)/(10 + std::sqrt(4-3 * temp1)));
234 
235  double del = c/nPoints;
236 
237  thetaArray = new float[nPoints];
238  thetaArray[0] = 0;
239 
240  for (int i = 1; i < nPoints; i++) {
241  double tmp;
242 
243  tmp = std::pow(b *std::cos(thetaArray[i-1]),2) + (std::pow((a * std::sin(thetaArray[i-1])),2));
244 
245  double f_tmp = del / std::sqrt(tmp);
246 
247  tmp = thetaArray[i-1] + f_tmp;
248 
249  thetaArray[i] = thetaArray[i-1] + 0.5*del*std::pow(std::sqrt(std::pow(b*std::cos(tmp),2) + std::pow(a*std::sin(tmp), 2)),(-1)) + 0.5*f_tmp;
250  }
251 }
252 
253 
254 /**************************************************************************/
255 /**************************************************************************/
263 bool greaterThan(float i, float j) { return (i > j); }
264 
265 
double eps
Definition: DFNmain.cpp:39
bool greaterThan(float i, float j)
double y
Definition: structures.h:113
float truncatedPowerLaw(float randomNum, float min, float max, float alpha)
double * randomTranslation(std::mt19937_64 &generator, float xMin, float xMax, float yMin, float yMax, float zMin, float zMax)
double x
Definition: structures.h:112
T dotProduct(const T *A, const T *B)
bool visualizationMode
Definition: readInput.cpp:36
double z
Definition: structures.h:114
double * fisherDistribution(double theta, double phi, double kappa, std::mt19937_64 &generator)
void generateTheta(float *&thetaArray, float aspectRatio, int nPoints)
Point lineFunction3D(double *v, double *point, double t)
double h
Definition: readInput.cpp:21
T * crossProduct(const T *v1, const T *v2)
std::vector< Point > discretizeLineOfIntersection(double *pt1, double *pt2, double dist)