DFNgen  2.0
DFN Model Generator
insertUserRectsByCoord.cpp
Go to the documentation of this file.
1 #include <cmath>
2 #include <iostream>
3 #include "insertShape.h"
4 #include "vectorFunctions.h"
5 #include "mathFunctions.h"
7 #include "structures.h"
8 #include "input.h"
9 #include "domain.h"
10 
11 /*************************************************************/
12 /*********** Insert User Rects By Coord ********************/
21 void insertUserRectsByCoord(std::vector<Poly>& acceptedPoly, std::vector<IntPoints> &intpts, struct Stats &pstats, std::vector<Point> &triplePoints) {
22 
23  std::cout << "\n" << nRectByCoord << " User Rectangles By Coordinates Defined\n\n";
24 
25  for (unsigned int i = 0; i < nRectByCoord; i++) {
26 
27  Poly newPoly;
28  newPoly.familyNum = -2; // Using -2 for all user specified rectangles
29 
30  newPoly.vertices = new double[12]; // 4 * {x,y,z}
31 
32  // Set number of nodes - needed for rotations
33  newPoly.numberOfNodes = 4;
34 
35  int polyVertIdx = i*12; // Each polygon has 4 vertices (12 elements, 4*{x,y,z}))
36 
37  // Initialize vertices
38  for (int j = 0; j < 4; j++) {
39  int vIdx = j*3;
40  newPoly.vertices[vIdx] = userRectCoordVertices[polyVertIdx+vIdx];
41  newPoly.vertices[vIdx+1] = userRectCoordVertices[polyVertIdx+1+vIdx];
42  newPoly.vertices[vIdx+2] = userRectCoordVertices[polyVertIdx+2+vIdx];
43  }
44 
45 
46  // Check that rectangle lays one a single plane:
47  // let xProd1 = cross Product vector (1st node to 2nd node) with vector(1st node to 3rd node)
48  // and xProd2 = cross product vector (1st node to 3th node) with vector (1st node to 4th node)
49  // Then, cross product xProd1 and xProd2, if this produces zero vector, all coords are on the same plane
50  // v1 is vector from first vertice to third vertice
51 
52  // Vector from fist node to 3rd node (vector through middle of sqare)
53  double v1[3] = {newPoly.vertices[6]-newPoly.vertices[0],
54  newPoly.vertices[7]-newPoly.vertices[1],
55  newPoly.vertices[8]-newPoly.vertices[2]};
56  // Vector from first node to 2nd node
57  double v2[3] = {newPoly.vertices[3]-newPoly.vertices[0],
58  newPoly.vertices[4]-newPoly.vertices[1],
59  newPoly.vertices[5]-newPoly.vertices[2]};
60  double *xProd1 = crossProduct(v2, v1);
61 
62  // Vector from fist node to 4th node
63  double v3[3] = {newPoly.vertices[9]-newPoly.vertices[0], newPoly.vertices[10]-newPoly.vertices[1], newPoly.vertices[11]-newPoly.vertices[2]};
64  double *xProd2 = crossProduct(v3, v1);
65 
66  double *xProd3 = crossProduct(xProd1, xProd2); //will be zero vector if all vertices are on the same plane
67 
68 
69  //TODO: Error check below is too sensitive. Adjust it.
70  // Error check for points not on the same plane
71 // if (std::abs(magnitude(xProd3[0],xProd3[1],xProd3[2])) > eps) { //points do not lay on the same plane. reject poly else meshing will fail
72 /* if (!(std::abs(xProd3[0]) < eps && std::abs(xProd3[1]) < eps && std::abs(xProd3[2]) < eps)) {
73  delete[] newPoly.vertices;
74  pstats.rejectedPolyCount++;
75  std::cout << "\nUser Rectangle (defined by coordinates) " << i+1 << " was rejected. The defined vertices are not co-planar.\n";
76  std::cout << "Please check user defined coordinates for rectanle " << i+1 << " in input file\n";
77  delete[] xProd1;
78  delete[] xProd2;
79  delete[] xProd3;
80  continue; //go to next poly
81  } */
82 
83  // Set normal vector
84  newPoly.normal[0] = xProd1[0]; //x
85  newPoly.normal[1] = xProd1[1]; //y
86  newPoly.normal[2] = xProd1[2]; //z
87 
88  normalize(newPoly.normal);
89 
90  delete[] xProd1;
91  delete[] xProd2;
92  delete[] xProd3;
93 
94  // Set radius (x and y radii might be switched based on order of users coordinates)
95  newPoly.xradius = .5 * magnitude(v2[0],v2[1],v2[2]);
96  newPoly.yradius = .5 * magnitude(v3[0],v3[1],v3[2]);
97 
98  newPoly.aspectRatio = newPoly.yradius/newPoly.xradius;
99 
100  // Estimate translation
101  // Use midpoint between 1st and 3rd vertices
102  // Note: For polygons defined by coordinates, the coordinates
103  // themselves provide the translation. We are just filling the
104  // translation array for completeness even though the translation
105  // array might not be used
106  newPoly.translation[0] = .5*(newPoly.vertices[0]+newPoly.vertices[6]);
107  newPoly.translation[1] = .5*(newPoly.vertices[1]+newPoly.vertices[7]);
108  newPoly.translation[2] = .5*(newPoly.vertices[2]+newPoly.vertices[8]);
109 
110  if (domainTruncation(newPoly, domainSize) == 1) {
111  // Poly completely outside domain
112  delete[] newPoly.vertices;
113  pstats.rejectionReasons.outside++;
114  pstats.rejectedPolyCount++;
115  std::cout << "\nUser Rectangle (defined by coordinates) " << i+1 << " was rejected for being outside the defined domain.\n";
116  continue; // Go to next poly (go to next iteration of for loop)
117  }
118 
119  createBoundingBox(newPoly);
120 
121  // Line of intersection and FRAM
122  int rejectCode = intersectionChecking(newPoly, acceptedPoly, intpts, pstats, triplePoints);
123  if(rejectCode == 0) {
124  // If intersection is ok (FRAM passed all tests)
125 
126  if (newPoly.truncated == 1) {
127  pstats.truncated++;
128  }
129 
130  // Incriment counter of accepted polys
131  pstats.acceptedPolyCount++;
132 
133  // Calculate poly's area
134  newPoly.area = getArea(newPoly);
135 
136  // Add new rejectsPerAttempt counter
137  pstats.rejectsPerAttempt.push_back(0);
138 
139  std::cout << "\nUser Defined Rectangular Fracture (Defined By Coordinates) " << (i+1) << " Accepted\n";
140  acceptedPoly.push_back(newPoly); // Save newPoly to accepted polys list
141  }
142  else {
143  delete[] newPoly.vertices; // Need to delete manually, created with new[]
144  pstats.rejectsPerAttempt[pstats.acceptedPolyCount]++;
145  pstats.rejectedPolyCount++;
146  std::cout << "\nRejected User Defined Rectangular Fracture (Defined By Coordinates) " << i+1 << "\n";
147  printRejectReason(rejectCode, newPoly);
148  }
149  } // End loop
150 
151  delete[] userRectCoordVertices;
152 }
153 
double * userRectCoordVertices
Definition: readInput.cpp:393
T magnitude(T x, T y, T z)
void normalize(T *vec)
bool truncated
Definition: structures.h:95
int numberOfNodes
Definition: structures.h:14
unsigned int nRectByCoord
Definition: readInput.cpp:383
double getArea(struct Poly &poly)
unsigned int truncated
Definition: structures.h:311
struct RejectionReasons rejectionReasons
Definition: structures.h:339
float aspectRatio
Definition: structures.h:49
unsigned long long int outside
Definition: structures.h:262
std::vector< unsigned int > rejectsPerAttempt
Definition: structures.h:371
void insertUserRectsByCoord(std::vector< Poly > &acceptedPoly, std::vector< IntPoints > &intpts, struct Stats &pstats, std::vector< Point > &triplePoints)
double * vertices
Definition: structures.h:70
double yradius
Definition: structures.h:46
void createBoundingBox(struct Poly &newPoly)
bool domainTruncation(Poly &newPoly, double *domainSize)
Definition: domain.cpp:19
double xradius
Definition: structures.h:40
double normal[3]
Definition: structures.h:55
float area
Definition: structures.h:33
int intersectionChecking(struct Poly &newPoly, std::vector< Poly > &acceptedPoly, std::vector< IntPoints > &intPtsList, struct Stats &pstats, std::vector< Point > &triplePoints)
void printRejectReason(int rejectCode, struct Poly newPoly)
double domainSize[3]
Definition: readInput.cpp:18
int familyNum
Definition: structures.h:22
T * crossProduct(const T *v1, const T *v2)
unsigned int acceptedPolyCount
Definition: structures.h:300
double translation[3]
Definition: structures.h:52
unsigned long long int rejectedPolyCount
Definition: structures.h:304