Advertisement
Guest User

Untitled

a guest
May 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. #include "tetgen.h"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <fstream>
  6.  
  7. #define NUMBER_TRIALS 1
  8. #define TETLIBRARY
  9. #include "tetgen.h"
  10.  
  11.  
  12. double volume = 0;
  13. static int run_test();
  14. using namespace std;
  15.  
  16. int main(int argc, char **argv)
  17. {
  18. int ntrial = argc > 1 ? atoi(argv[1]) : 1;
  19.  
  20. for (int n = 0; n < ntrial; ++n) {
  21. run_test();
  22. }
  23.  
  24. system("pause");
  25. return 0;
  26. }
  27.  
  28. struct Point
  29. {
  30. double x;
  31. double y;
  32. double z;
  33. };
  34.  
  35. struct Tetrahedron
  36. {
  37. Point p1;
  38. Point p2;
  39. Point p3;
  40. Point p4;
  41. };
  42.  
  43. struct VolumeArray {
  44. double array[5][5];
  45. };
  46.  
  47. struct PointsArray {
  48. double array[8];
  49. int sizeOfArray() {
  50. return sizeof(array);
  51. }
  52. };
  53.  
  54. double distanceBetweenPoints(Point p1, Point p2) {
  55. return sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2) + pow((p1.z - p2.z), 2));
  56. }
  57.  
  58. VolumeArray fillArray(Tetrahedron tet) {
  59.  
  60. double volumeArray[5][5] = {
  61. { 0, pow(distanceBetweenPoints(tet.p1 , tet.p2),2), pow(distanceBetweenPoints(tet.p1 , tet.p3),2), pow(distanceBetweenPoints(tet.p1 , tet.p4),2), 1 },
  62. { pow(distanceBetweenPoints(tet.p1 , tet.p2),2), 0, pow(distanceBetweenPoints(tet.p2 , tet.p3),2), pow(distanceBetweenPoints(tet.p2 , tet.p4),2), 1 },
  63. { pow(distanceBetweenPoints(tet.p1 , tet.p3),2), pow(distanceBetweenPoints(tet.p2 , tet.p3),2), 0, pow(distanceBetweenPoints(tet.p3 , tet.p4),2), 1 },
  64. { pow(distanceBetweenPoints(tet.p1 , tet.p4),2), pow(distanceBetweenPoints(tet.p2 , tet.p4),2), pow(distanceBetweenPoints(tet.p3 , tet.p4),2), 0, 1 },
  65. { 1,1,1,1,0 }
  66. };
  67.  
  68. VolumeArray singleArray;
  69. for (int i = 0; i < 5; i++) {
  70. for (int j = 0; j < 5; j++) {
  71. singleArray.array[i][j] = volumeArray[i][j];
  72. }
  73. }
  74. return singleArray;
  75. }
  76.  
  77. double determinant(VolumeArray volumeArray, int n) {
  78. double a[5][5];
  79. for (int d = 0; d < 5; d++) {
  80. for (int e = 0; e < 5; e++) {
  81. a[d][e] = volumeArray.array[d][e];
  82. }
  83. }
  84. int p, h, k, i, j;
  85. VolumeArray temp;
  86. double det = 0;
  87. if (n == 1) {
  88. return a[0][0];
  89. }
  90. else if (n == 2) {
  91. det = (a[0][0] * a[1][1] - a[0][1] * a[1][0]);
  92. return det;
  93. }
  94. else {
  95. for (p = 0; p<n; p++) {
  96. h = 0;
  97. k = 0;
  98. for (i = 1; i<n; i++) {
  99. for (j = 0; j<n; j++) {
  100. if (j == p) {
  101. continue;
  102. }
  103. temp.array[h][k] = a[i][j];
  104. k++;
  105. if (k == n - 1) {
  106. h++;
  107. k = 0;
  108. }
  109. }
  110. }
  111. det = det + a[0][p] * pow(-1, p)*determinant(temp, n - 1);
  112. }
  113. return det;
  114. }
  115. }
  116.  
  117. double calculateVolume(double determinant) {
  118.  
  119. double fraction = determinant / 288;
  120. return sqrt(abs(fraction));
  121. }
  122.  
  123.  
  124. void saveToFile(string path, vector <Point> pointsArray, int howManyInputs, int numberOfPoints, int howManyFaces) {
  125. ofstream NowyPlik;
  126. NowyPlik.open(path);
  127.  
  128.  
  129. NowyPlik << "ply" << endl;
  130. NowyPlik << "format ascii 1.0" << endl;
  131. NowyPlik << "element vertex " << numberOfPoints << endl;
  132. NowyPlik << "property float32 x" << endl;
  133. NowyPlik << "property float32 y" << endl;
  134. NowyPlik << "property float32 z" << endl;
  135. NowyPlik << "element face " << howManyFaces << endl;
  136. NowyPlik << "property list uint8 int32 vertex_indices" << endl;
  137. NowyPlik << "end_header" << endl;
  138. for (int i = 0; i < howManyInputs; i++) {
  139. if (i < numberOfPoints) {
  140. NowyPlik << pointsArray[i].x << " " << pointsArray[i].y << " " << pointsArray[i].z << endl;
  141. }
  142. else {
  143. NowyPlik << "3 " << pointsArray[i].x << " " << pointsArray[i].y << " " << pointsArray[i].z << endl;
  144. }
  145. }
  146. NowyPlik.close();
  147. }
  148.  
  149. vector<Point> getPointsFromFile(string filePath) {
  150.  
  151. vector<Point> vectorOfPoints;
  152. double a, b, c;
  153. string element, vertex;
  154. int numberOfPoints;
  155. Point point;
  156. int tmp = 0;
  157. int i = 0;
  158. ifstream points(filePath);
  159.  
  160. if (!points)
  161. {
  162. cout << "Nie mozna otworzyc pliku";
  163. getchar();
  164. return vectorOfPoints;
  165. }
  166.  
  167. while (!points.eof()) {
  168. points >> a >> b >> c;
  169. point.x = a;
  170. point.y = b;
  171. point.z = c;
  172. vectorOfPoints.push_back(point);
  173. tmp++;
  174. }
  175.  
  176. points.close();
  177.  
  178. return vectorOfPoints;
  179.  
  180. }
  181.  
  182. int run_test()
  183. {
  184. int n;
  185. tetgenio inp, out;
  186.  
  187. inp.initialize();
  188. out.initialize();
  189. /*
  190. Point pkt1 = { 0.2,0.0,0.1 };
  191. Point pkt2 = { 0.5,0.1,0.0 };
  192. Point pkt3 = { 0.8,0.2,0.0 };
  193. Point pkt4 = { 1.7,0.5,0.2 };
  194. Point pkt5 = { 2.2,0.4,0.0 };
  195. Point pkt6 = { 0.7,0.6,0.3 };
  196. Point pkt7 = { 0.9,1.0,0.2 };
  197. Point pkt8 = { 2.0,0.8,0.3 };
  198. Point pkt9 = { 0.3,1.4,0.3 };
  199. Point pkt10 = { 0.6,1.3,0.3 };
  200. Point pkt11 = { 1.5,1.7,0.5 };
  201. Point pkt12 = { 0.4,1.8,0.5 };
  202. Point pkt13 = { 1.0,2.0,0.8 };
  203. */
  204.  
  205. int indexesOfEdgePoint[10];
  206. indexesOfEdgePoint[0] = 0;
  207. indexesOfEdgePoint[1] = 1;
  208. indexesOfEdgePoint[2] = 2;
  209. indexesOfEdgePoint[3] = 3;
  210. indexesOfEdgePoint[4] = 4;
  211. indexesOfEdgePoint[5] = 7;
  212. indexesOfEdgePoint[6] = 10;
  213. indexesOfEdgePoint[7] = 12;
  214. indexesOfEdgePoint[8] = 11;
  215. indexesOfEdgePoint[9] = 8;
  216.  
  217. vector <Point> pointsVector = getPointsFromFile("F:\\INF\\semestr5i6\\IP\\Hexagon\\tetgen1\\output_2_ASCII.ply");
  218.  
  219. //pointsVector.push_back(pkt1);
  220. //pointsVector.push_back(pkt2);
  221. //pointsVector.push_back(pkt3);
  222. //pointsVector.push_back(pkt4);
  223. //pointsVector.push_back(pkt5);
  224. //pointsVector.push_back(pkt6);
  225. //pointsVector.push_back(pkt7);
  226. //pointsVector.push_back(pkt8);
  227. //pointsVector.push_back(pkt9);
  228. //pointsVector.push_back(pkt10);
  229. //pointsVector.push_back(pkt11);
  230. //pointsVector.push_back(pkt12);
  231. //pointsVector.push_back(pkt13);
  232.  
  233. ///DODANIE ZRZUTOWANYCH PUNKTOW KRANCOWYCH
  234.  
  235. /*
  236. for (int i = 0; i < (sizeof(indexesOfEdgePoint) / sizeof(*indexesOfEdgePoint)); i++) {
  237. Point point;
  238. int j = indexesOfEdgePoint[i];
  239. point.x = pointsVector[j].x;
  240. point.y = 0;
  241. point.z = pointsVector[j].z;
  242. pointsVector.push_back(point);
  243. }
  244. */
  245.  
  246.  
  247. inp.numberofpoints = pointsVector.size();
  248. inp.pointlist = new REAL[inp.numberofpoints * 3];
  249.  
  250. for (int n = 0; n < pointsVector.size(); n++) {
  251.  
  252. inp.pointlist[3 * n + 0] = pointsVector[n].x;
  253. inp.pointlist[3 * n + 1] = pointsVector[n].y;
  254. inp.pointlist[3 * n + 2] = pointsVector[n].z;
  255. }
  256.  
  257.  
  258. tetrahedralize("veeQ", &inp, &out);
  259.  
  260.  
  261. //cout << "faces:" << endl;
  262. for (int i = 0; i < out.numberoftrifaces; i++) {
  263. //cout << "face:" << endl;
  264. //cout << out.trifacelist[i * 3 + 0] << " ";
  265. //cout << out.trifacelist[i * 3 + 1] << " ";
  266. //cout << out.trifacelist[i * 3 + 2] << endl;
  267. }
  268.  
  269. vector <Tetrahedron> tetrahedronsVector;
  270.  
  271. //cout << "tetra:" << endl;
  272. for (int i = 0; i < out.numberoftetrahedra; i++) {
  273.  
  274. Tetrahedron tetrahedron;
  275. //cout << out.tetrahedronlist[i * 4 + 0] << " ";
  276. //cout << out.tetrahedronlist[i * 4 + 1] << " ";
  277. //cout << out.tetrahedronlist[i * 4 + 2] << " ";
  278. //cout << out.tetrahedronlist[i * 4 + 4] << " " << endl << endl;
  279.  
  280. //DODANIE CZWOROSCIANOW DO TABLICY
  281.  
  282. tetrahedron.p1 = pointsVector[out.tetrahedronlist[i * 4 + 0]];
  283. tetrahedron.p2 = pointsVector[out.tetrahedronlist[i * 4 + 1]];
  284. tetrahedron.p3 = pointsVector[out.tetrahedronlist[i * 4 + 2]];
  285. tetrahedron.p4 = pointsVector[out.tetrahedronlist[i * 4 + 3]];
  286.  
  287. tetrahedronsVector.push_back(tetrahedron);
  288. }
  289.  
  290.  
  291. //WYPELNIENIE MACIERZY DO LICZENIA WYZNACZNIKOW, WSZYSTKIE MACIERZE ZNAJDUJA SIE W VECTORZE tetrahedronsArrayVector
  292.  
  293. vector <VolumeArray> tetrahedronsArrayVector;
  294.  
  295. for (int i = 0; i < tetrahedronsVector.size(); i++) {
  296.  
  297. tetrahedronsArrayVector.push_back(fillArray(tetrahedronsVector[i]));
  298. }
  299.  
  300. cout << "ile czworoscianow w tablicy?" << tetrahedronsVector.size();
  301.  
  302. cout << "Delaunay information:" << endl;
  303. cout << "numberofpoints: " << out.numberofpoints << endl;
  304. cout << "numberofedges: " << out.numberofedges << endl;
  305. cout << "numberoftrifaces: " << out.numberoftrifaces << endl;
  306. cout << "numberoftetrahedra: " << out.numberoftetrahedra << endl;
  307. cout << "numberofcorners: " << out.numberofcorners << endl;
  308.  
  309. determinant(tetrahedronsArrayVector.at(1), 5);
  310.  
  311. fstream plik("plik.txt", ios::out);
  312. if (plik.good()){
  313. for (int i = 0; i < tetrahedronsVector.size(); i++) {
  314.  
  315. plik << "Volume of " << i + 1 << " tetrahedron = " << calculateVolume(determinant(tetrahedronsArrayVector.at(i), 5)) << endl;
  316. plik.flush();
  317.  
  318.  
  319. //cout << "Volume of " << i + 1 << " tetrahedron = " << calculateVolume(determinant(tetrahedronsArrayVector.at(i), 5)) << endl;
  320. volume += calculateVolume(determinant(tetrahedronsArrayVector.at(i), 5));
  321. }
  322. plik.close();
  323. }
  324. cout << "Volume of surface = " << volume << endl;
  325.  
  326.  
  327.  
  328. for (int n = 0; n<out.numberoftrifaces; ++n) {
  329. int n0 = out.trifacelist[3 * n + 0];
  330. int n1 = out.trifacelist[3 * n + 1];
  331. int n2 = out.trifacelist[3 * n + 2];
  332.  
  333. REAL *u = &out.pointlist[3 * n0];
  334. REAL *v = &out.pointlist[3 * n1];
  335. REAL *y = &out.pointlist[3 * n2];
  336. //printf("(%f %f %f) -> (%f %f %f) -> (%f %f %f)\n", u[0], u[1], u[2], v[0], v[1], v[2], y[0], y[1], y[2]);
  337. }
  338.  
  339. vector<Point> pointsArray;
  340. for (int n = 0; n < out.numberofpoints; n++) {
  341. REAL *u = &out.pointlist[3 * n];
  342. //printf("(%f %f %f)\n", u[0], u[1], u[2]);
  343.  
  344. Point point;
  345. point.x = u[0];
  346. point.y = u[1];
  347. point.z = u[2];
  348. pointsArray.push_back(point);
  349.  
  350. }
  351.  
  352. for (int i = 0; i < out.numberoftrifaces; i++) {
  353. Point point;
  354. point.x = out.trifacelist[i * 3 + 0];
  355. point.y = out.trifacelist[i * 3 + 1];
  356. point.z = out.trifacelist[i * 3 + 2];
  357. pointsArray.push_back(point);
  358. }
  359.  
  360. saveToFile("test.ply", pointsArray, pointsArray.size(), out.numberofpoints, out.numberoftrifaces);
  361.  
  362.  
  363. out.save_edges("esgesFile");
  364. out.save_elements("elementsFile");
  365. out.save_poly("polyFule");
  366. out.save_faces2smesh("faces2smesh");
  367.  
  368. return 0;
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement