C++ 中的字符串数组
我正在尝试将我拥有的一个小型Python程序转换为C++,该程序打开一个OBJ文件并将必要的数据记录到FACES和VERTS类中。在 Python 程序中,它只是查看每一行并将它们分成由空格界定的标记。如果该行以“f”开头,则后续标记将是面部数据。对于“v”,顶点的位置。对于“vt”,UV 信息。 “vn” 顶点法线。
到目前为止我可以做开线的工作。但是遍历每一行然后将它们记录到字符串数组(字符数组)中是非常困难的。请提供一些帮助。
这是我必须开始的一个示例:
FILE * pFile;
char myString[100];
pFile = fopen(filename, "r");
while(fgets(myString, 100, pFile)!=NULL) {
char *sep;
int counter = 0;
int mode = 0;
sep = strtok(myString, " ");
while (sep != NULL) {
if (strncmp(sep,"f",1)==0) {
mode = 4;
} else {
if (strncmp(sep,"vn",2)==0) {
mode = 3;
} else {
if (strncmp(sep,"vt",2)==0) {
mode = 2;
} else {
if (strncmp(sep,"v",2)==0) {
mode = 1;
}else {
}
}
}
}
switch (mode) {
case 1 :{
// vertex position
break;
}
case 2 :{
cout << sep << " --> vertex normal" << endl;
break;
}
case 3 :{
cout << sep << " --> vertex UV" << endl;
break;
}
case 4 :{
cout << sep << " --> face " << endl;
break;
}
}
sep = strtok(NULL, " ");
counter++;
}
}
与其使用先前为“MODE”设置变量的“SWITCH”,不如使用像这样简单的东西:
def openFile(self, filename):
faceCount = 0
for line in open(filename, "r"):
vals = line.split()
if len(vals) > 0:
if vals[0] == "v":
v = map(float, vals[1:4])
self.verts.append(Point(v[0], v[1], v[2]))
if vals[0] == "vn":
n = map(float, vals[1:4])
self.norms.append(Normal(n[0], n[1], n[2]))
if vals[0] == "vt":
vt = map(float, vals[1:3])
self.text.append(UV(vt[0], vt[1]))
if vals[0] == "f":
vertsOut = []
normsOut = []
textOut = []
for f in vals[1:]:
w = f.split("/")
# OBJ Files are 1-indexed so we must subtract 1 below
try:
vertsOut.append(self.verts[int(w[0])-1])
except:
print "Issue with Position of Face %s " % faceCount
try:
textOut.append(self.text[int(w[1])-1])
except:
print "Issue with UV of Face %s " % faceCount
try:
normsOut.append(self.norms[int(w[2])-1])
except:
print "Issue with Normal of Face %s " % faceCount
self.verts[int(w[0])-1].addFace(faceCount)
self.faces[faceCount]= Face(vertsOut,normsOut,textOut)
faceCount += 1
但这是 PYTHON。那里容易多了。请帮忙。谢谢你!
I'm a trying to translate into C++ a small Python program I have that opens an OBJ file and records necessary data into Classes of FACES and VERTS. In the Python program, it simply looks at each line and splits them into tokens delineated by spaces. If the line starts with an "f" the succeeding tokens would be data for faces. For "v", position of verts. For "vt", UV info. "vn" normals for verts.
So far I could do the line for line opening. But to go through each line and then record them into an Array of Strings (char array), is so difficult. Some help please.
Here's a sample of what I have to start with:
FILE * pFile;
char myString[100];
pFile = fopen(filename, "r");
while(fgets(myString, 100, pFile)!=NULL) {
char *sep;
int counter = 0;
int mode = 0;
sep = strtok(myString, " ");
while (sep != NULL) {
if (strncmp(sep,"f",1)==0) {
mode = 4;
} else {
if (strncmp(sep,"vn",2)==0) {
mode = 3;
} else {
if (strncmp(sep,"vt",2)==0) {
mode = 2;
} else {
if (strncmp(sep,"v",2)==0) {
mode = 1;
}else {
}
}
}
}
switch (mode) {
case 1 :{
// vertex position
break;
}
case 2 :{
cout << sep << " --> vertex normal" << endl;
break;
}
case 3 :{
cout << sep << " --> vertex UV" << endl;
break;
}
case 4 :{
cout << sep << " --> face " << endl;
break;
}
}
sep = strtok(NULL, " ");
counter++;
}
}
Instead of having a "SWITCH" in which previously set variables for "MODE" I'd rather have something as simple as:
def openFile(self, filename):
faceCount = 0
for line in open(filename, "r"):
vals = line.split()
if len(vals) > 0:
if vals[0] == "v":
v = map(float, vals[1:4])
self.verts.append(Point(v[0], v[1], v[2]))
if vals[0] == "vn":
n = map(float, vals[1:4])
self.norms.append(Normal(n[0], n[1], n[2]))
if vals[0] == "vt":
vt = map(float, vals[1:3])
self.text.append(UV(vt[0], vt[1]))
if vals[0] == "f":
vertsOut = []
normsOut = []
textOut = []
for f in vals[1:]:
w = f.split("/")
# OBJ Files are 1-indexed so we must subtract 1 below
try:
vertsOut.append(self.verts[int(w[0])-1])
except:
print "Issue with Position of Face %s " % faceCount
try:
textOut.append(self.text[int(w[1])-1])
except:
print "Issue with UV of Face %s " % faceCount
try:
normsOut.append(self.norms[int(w[2])-1])
except:
print "Issue with Normal of Face %s " % faceCount
self.verts[int(w[0])-1].addFace(faceCount)
self.faces[faceCount]= Face(vertsOut,normsOut,textOut)
faceCount += 1
But that's PYTHON. So much easier there. Please help. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先声明一个字符串数组(字符数组的数组)。您可以声明静态以避免重新分配。
然后
First declare a Strings Array (array of char arrays). You can declare static in order to avoid reallocate.
Then
OBJ 解析可能有点痛苦;您应该查看
stringstream
对象。仅举一个满足您需求的具体使用的简短示例:
OBJ parsing can be kind of painful; you should look at
stringstream
objects.Just to give a short example of concrete use for your needs: