76 for (
int i = 0; i < (int)
m_Faces.size(); ++i)
81 double area = normal.
Len();
83 center += area * gravity3;
86 deta += gravity3 ^ normal;
90 for (
int j = 0; j < 3; ++j)
95 center /= sumArea * 3;
96 deta -= 3 * (center ^ sumNormal);
149 double maxEdgeLenOfBoundingBox = -1;
150 if (ptUp.
x - ptDown.
x > maxEdgeLenOfBoundingBox)
151 maxEdgeLenOfBoundingBox = ptUp.
x - ptDown.
x;
152 if (ptUp.
y - ptDown.
y > maxEdgeLenOfBoundingBox)
153 maxEdgeLenOfBoundingBox = ptUp.
y - ptDown.
y;
154 if (ptUp.
z - ptDown.
z > maxEdgeLenOfBoundingBox)
155 maxEdgeLenOfBoundingBox = ptUp.
z - ptDown.
z;
156 m_scale = maxEdgeLenOfBoundingBox / 2;
186 string str(
m_filename.substr(pos, pos2 - pos));
211 ifstream in(filename.c_str());
214 throw "fail to read file";
217 while (in.getline(buf,
sizeof buf))
219 istringstream line(buf);
231 else if (word ==
"f")
239 polygon.push_back(tmp);
241 line.getline(tmpBuf,
sizeof tmpBuf,
' ');
243 for (
int j = 1; j < (int)polygon.size() - 1; ++j)
245 face[0] = polygon[0] - 1;
246 face[1] = polygon[j] - 1;
247 face[2] = polygon[j + 1] - 1;
263 int nDot = (int)filename.rfind(
'.');
266 throw "File name doesn't contain a dot!";
268 string extension = filename.substr(nDot + 1);
270 if (extension ==
"obj")
274 else if (extension ==
"off")
278 else if (extension ==
"m")
284 throw "This format can't be handled!";
290 ifstream in(filename.c_str());
293 throw "fail to read file";
296 in.getline(buf,
sizeof buf);
297 int vertNum, faceNum, edgeNum;
298 in >> vertNum >> faceNum >> edgeNum;
300 for (
int i = 0; i < vertNum; ++i)
314 in >> first >> second;
316 for (
int i = 0; i < degree - 2; ++i)
333 ifstream in(filename.c_str());
336 throw "fail to read file";
339 while (in.getline(buf,
sizeof buf))
341 istringstream line(buf);
346 if (word ==
"Vertex")
357 else if (word ==
"Face")
366 polygon.push_back(tmp);
368 for (
int j = 1; j < (int)polygon.size() - 1; ++j)
370 face[0] = polygon[0] - 1;
371 face[1] = polygon[j] - 1;
372 face[2] = polygon[j + 1] - 1;
389 ofstream outFile(filename.c_str());
392 outFile <<
"Vertex " << i + 1 <<
" " <<
Vert(i).
x <<
" " <<
Vert(i).
y <<
" " <<
Vert(i).
z << endl;
399 outFile <<
"Face " << ++cnt <<
" " <<
Face(i)[0] + 1 <<
" " <<
Face(i)[1] + 1 <<
" " <<
Face(i)[2] + 1 << endl;
406 ofstream outFile(filename.c_str());
407 outFile <<
"OFF" << endl;
417 outFile << 3 <<
" " <<
Face(i)[0]<<
" " <<
Face(i)[1] <<
" " <<
Face(i)[2] << endl;
424 ofstream outFile(filename.c_str());
425 outFile <<
"g " << filename.substr(filename.rfind(
"\\") + 1, filename.rfind(
'.') - filename.rfind(
"\\") - 1) << endl;
428 outFile <<
"v " <<
Vert(i).
x <<
" " <<
Vert(i).
y <<
" " <<
Vert(i).
z << endl;
434 outFile <<
"f " <<
Face(i)[0] + 1 <<
" " <<
Face(i)[1] + 1<<
" " <<
Face(i)[2] + 1<< endl;
441 ofstream outFile(filename.c_str());
442 outFile <<
"g " << filename.substr(filename.rfind(
"\\") + 1, filename.rfind(
'.') - filename.rfind(
"\\") - 1) << endl;
443 outFile <<
"# maxDis: " << *max_element(vals.begin(), vals.end()) << endl;
446 outFile <<
"v " <<
Vert(i).
x <<
" " <<
Vert(i).
y <<
" " <<
Vert(i).
z << endl;
448 for (
int i = 0; i < (int)vals.size(); ++i)
450 outFile <<
"vt " << vals[i] <<
" " << 0 << endl;
456 outFile <<
"f " <<
Face(i)[0] + 1 <<
"/" <<
Face(i)[0] + 1
457 <<
" " <<
Face(i)[1] + 1 <<
"/" <<
Face(i)[1] + 1
458 <<
" " <<
Face(i)[2] + 1 <<
"/" <<
Face(i)[2] + 1 << endl;
465 ofstream outFile(filename.c_str());
466 outFile <<
"g " << filename.substr(filename.rfind(
"\\") + 1, filename.rfind(
'.') - filename.rfind(
"\\") - 1) << endl;
467 outFile << comments << endl;
470 outFile <<
"v " <<
Vert(i).
x <<
" " <<
Vert(i).
y <<
" " <<
Vert(i).
z << endl;
472 for (
int i = 0; i < (int)vals.size(); ++i)
474 outFile <<
"vt " << vals[i] <<
" " << 0 << endl;
480 outFile <<
"f " <<
Face(i)[0] + 1 <<
"/" <<
Face(i)[0] + 1
481 <<
" " <<
Face(i)[1] + 1 <<
"/" <<
Face(i)[1] + 1
482 <<
" " <<
Face(i)[2] + 1 <<
"/" <<
Face(i)[2] + 1 << endl;
489 ofstream outFile(filename.c_str());
490 outFile <<
"g " << filename.substr(filename.rfind(
"\\") + 1, filename.rfind(
'.') - filename.rfind(
"\\") - 1) << endl;
491 outFile <<
"# maxValue = " << *max_element(vals.begin(), vals.end()) / maxV << endl;
495 outFile <<
"v " <<
Vert(i).
x <<
" " <<
Vert(i).
y <<
" " <<
Vert(i).
z << endl;
497 for (
int i = 0; i < (int)vals.size(); ++i)
499 outFile <<
"vt " << vals[i] / maxV <<
" " << 0 << endl;
505 outFile <<
"f " <<
Face(i)[0] + 1 <<
"/" <<
Face(i)[0] + 1
506 <<
" " <<
Face(i)[1] + 1 <<
"/" <<
Face(i)[1] + 1
507 <<
" " <<
Face(i)[2] + 1 <<
"/" <<
Face(i)[2] + 1 << endl;
514 ofstream outFile(filename.c_str());
515 outFile <<
"g " << filename.substr(filename.rfind(
"\\") + 1, filename.rfind(
'.') - filename.rfind(
"\\") - 1) << endl;
518 outFile <<
"v " <<
Vert(i).
x <<
" " <<
Vert(i).
y <<
" " <<
Vert(i).
z << endl;
520 for (
int i = 0; i < (int)uvs.size(); ++i)
522 outFile <<
"vt " << uvs[i].first <<
" " << uvs[i].second << endl;
528 outFile <<
"f " <<
Face(i)[0] + 1 <<
"/" <<
Face(i)[0] + 1
529 <<
" " <<
Face(i)[1] + 1 <<
"/" <<
Face(i)[1] + 1
530 <<
" " <<
Face(i)[2] + 1 <<
"/" <<
Face(i)[2] + 1 << endl;
537 out <<
"Model info is as follows.\n";
541 out <<
"Scale = " <<
m_scale << endl;
556 double dis = DBL_MAX;
560 if ((
Vert(i) - pt).Len() < dis)
563 dis = (
Vert(
id)-pt).Len();
571 ifstream in(filename);
574 while (in.getline(buf,
sizeof buf))
588 vector<double> scalarField;
589 ifstream in(filename);
591 while (in.getline(buf,
sizeof buf))
593 istringstream line(buf);
600 scalarField.push_back(value);
void ReadOffFile(const string &filename)
static vector< double > GetScalarField(string filename)
string GetFileFullName() const
vector< CPoint3D > m_Verts
string GetFileShortName() const
void SaveObjFile(const string &filename) const
int GetNumOfFaces() const
CPoint3D VectorCross(const CPoint3D &pt1, const CPoint3D &pt2, const CPoint3D &pt3)
string GetFileShortNameWithoutExtension() const
void SaveOffFile(const string &filename) const
int GetVertexID(const CPoint3D &pt) const
void ReadMFile(const string &filename)
const CPoint3D & Normal(int vertIndex) const
static string GetComments(const char *filename)
int GetNumOfVerts() const
const vector< CBaseModel::CFace > & GetFaces() const
void SaveMFile(const string &filename) const
void SetFaces(const vector< CBaseModel::CFace > &faces)
const double RateOfNormalShift
const CPoint3D & Vert(int vertIndex) const
void ReadObjFile(const string &filename)
void SaveScalarFieldObjFile(const vector< double > &vals, const string &filename) const
vector< CPoint3D > m_NormalsToVerts
CPoint3D GetShiftVertex(int indexOfVert) const
void SavePamametrizationObjFile(const vector< pair< double, double >> &uvs, const string &filename) const
const CFace & Face(int faceIndex) const
set< int > m_UselessFaces
void ReadFile(const string &filename)
void ComputeScaleAndNormals()
CBaseModel(const string &filename)
virtual void PrintInfo(ostream &out) const