VC++ (main.cpp):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | #include "stdafx.h" #include <iostream> #include <regex> #include <fstream> #include "gd/gd.h" #include "gd/gdfontg.h" #include "gd/gdfontt.h" #include "gd/gdfonts.h" #include "gd/gdfontmb.h" #include "gd/gdfontl.h" #pragma comment(lib, "lib/bgd.lib") using namespace std; class pos { public : float x; float y; float z; }; class ERR { private : int kind; public : ERR( int k) : kind(k){}; //建構函式 char * message() { switch (kind) { case 0: return "Parameter Error!" ; case 1: return "Path Error!" ; case 2: return "File Error!" ; case 3: return "Array Size is too small." ; case 4: return "STL Format Error!" ; } } }; void draw(gdImagePtr im, pos vArr[], int v, int color) { int i = 0; for (i=0;i<v;i+=3) { gdImageLine(im, ( int )vArr[i].x, ( int )vArr[i].y, ( int )vArr[i+1].x, ( int )vArr[i+1].y, color); gdImageLine(im, ( int )vArr[i+1].x, ( int )vArr[i+1].y, ( int )vArr[i+2].x, ( int )vArr[i+2].y, color); gdImageLine(im, ( int )vArr[i].x, ( int )vArr[i].y, ( int )vArr[i+2].x, ( int )vArr[i+2].y, color); } } int main( int argc, char * argv[]) { const float zoom = 5; const int arrSize = 3000; const int width = 500; const int height = 500; int i = 0, normals = 0, vertexs = 0; char pathChar[1024]; char stlName[1024]; string lines = "" ; pos *normalArr; pos *vertexArr; regex pathRegex( "([a-zA-Z]\:\\\\(?:[^\\\\]+\\\\)*)(.*)" ); //"C:\\temp\\hello.txt" regex solid( " *solid.*" ); regex facetNormal( " *facet normal ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)" ); regex outerLoop( " *outer loop" ); regex vertex( " *vertex ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)" ); regex endLoop( " *endloop" ); regex endfacet( " *endfacet" ); regex endsolid( " *endsolid" ); cmatch pathMatch; smatch normalMatch; smatch vertexMatch; try { if (argc<2) throw ERR(1); //Check Parameter strcpy (pathChar, argv[1]); //Get STL Path if (!regex_match(pathChar, pathMatch, pathRegex)) throw ERR(1); //Check STL Path strcpy (stlName, pathMatch[2].str().c_str()); //Get STL Name ifstream File(pathChar); if (!File.is_open()) throw ERR(2); //Check File normalArr = new pos[arrSize]; //宣告Facet normal動態陣列大小 vertexArr = new pos[arrSize*3]; //宣告Vertex動態陣列大小 cout << "Reading Now..." << endl; //solid if (!File.good()) throw ERR(4); getline(File, lines); if (!regex_match(lines, solid)) throw ERR(4); while (File.good()) { if (normals >= arrSize) throw ERR(3); //Check Array Size //facet normal getline(File, lines); if (regex_match(lines, normalMatch, facetNormal)) { normalArr[normals].x = atof (normalMatch[1].str().c_str()) + width/2; normalArr[normals].y = atof (normalMatch[2].str().c_str()) + height/2; normalArr[normals].z = atof (normalMatch[3].str().c_str()); normals++; } else if (regex_match(lines, endsolid)) //end solid { break ; } else { throw ERR(4); } //outer loop getline(File, lines); if (!regex_match(lines, outerLoop)) throw ERR(4); //vertex for (i=0;i<3;i++) { getline(File, lines); if (regex_match(lines, vertexMatch, vertex)) { vertexArr[vertexs].x = atof (vertexMatch[1].str().c_str())*zoom + width/2; vertexArr[vertexs].y = atof (vertexMatch[2].str().c_str())*zoom + height/2; vertexArr[vertexs].z = atof (vertexMatch[3].str().c_str())*zoom; vertexs++; } else { throw ERR(4); } } //end loop getline(File, lines); if (!regex_match(lines, endLoop)) throw ERR(4); //end facet getline(File, lines); if (!regex_match(lines, endfacet)) throw ERR(4); } File.close(); cout << "Facet Normal: " << normals << endl; cout << "Vertex: " << vertexs << endl; cout << "Done." << endl; // --- draw --- cout << "Drawing Now..." << endl; FILE *out; int size; char *data; gdImagePtr im = gdImageCreate(width, height); int white = gdImageColorAllocate(im, 255, 255, 255), //Background black = gdImageColorAllocate(im, 0, 0, 0), blue = gdImageColorAllocate(im, 0, 0, 255), cyan = gdImageColorAllocate(im, 0, 255, 255), green = gdImageColorAllocate(im, 128, 255, 0), red = gdImageColorAllocate(im, 255, 0, 0), purple = gdImageColorAllocate(im, 255, 0, 255), orange = gdImageColorAllocate(im, 255, 128, 0), yellow = gdImageColorAllocate(im, 255, 255, 0); strcpy (pathChar, argv[0]); //Get Execution Path if (!regex_match(pathChar, pathMatch, pathRegex)) throw ERR(1); //Check Execution Path strcpy (pathChar, pathMatch[1].str().c_str()); //Get Folder Path strcat (pathChar, stlName); //Connections Folder Path and STL Name strcat (pathChar, ".png" ); //Connections Path and Extension out = fopen (pathChar, "wb" ); if (!out) throw ERR(1); draw(im, vertexArr, vertexs, black); data = ( char *) gdImagePngPtr(im, &size); if (!data) throw ERR(2); if ( fwrite (data, 1, size, out) != size) throw ERR(3); if ( fclose (out) != 0) throw ERR(4); gdFree(data); cout << "Done." << endl; } catch (ERR e) { cout << e.message() << endl; } system ( "pause" ); return 0; } |
說明:
首先拖曳一個STL模型檔案至執行檔以進行讀取,一邊進行檔案比對一邊將點資料存入類別陣列內,再使用GD Library匯出png圖片檔。
沒有留言:
張貼留言