我正在尝试使用JavaScript解析本地文本文件(output2.txt)。该文件包含我需要用于自动三角测量的2D坐标。该项目的最终目标是从本地文件中读取2D坐标,并将它们包含在自动三角测量中,以便在浏览器中创建3D模型。
我一直在使用对alert方法的调用来查看代码是否通过某些相关部分运行。 chunk
是当前正在阅读的文本文件的一部分,但alert(chunk);
始终返回单个0
,而且文本文件中从不熟悉。为什么会发生这种情况?
当我按原样运行代码时,屏幕上不会显示任何数据点或面。警报方法输出意味着文本文件中没有任何内容,只有零。我怀疑因为我总是从文本文件中读取0
,所以我从未到达执行body_components.get(name).vertices.push(vertex);
的for循环的部分,因此从不推动任何顶点用于自动三角测量,导致什么都没有出现在屏幕上。
我的团队咨询this进行自动三角测量,this使用JavaScript访问本地文件。另外,搜索Stack Overflow的答案给了我们this,但我相信该线程的情况不同,因为我的文本文件不仅包含一个字符,而且因为我的团队代码在JavaScript而不是Java。
下面显示的是自动三角测量方法的代码,以及用于读取文件的代码。我还包含了一段output2.txt供参考。
提前感谢您的帮助。
代码:
for (var value in body_components.values()) {
var triangles = THREE.ShapeUtils.triangulateShape(value.vertices, holes);
for( var i = 0; i < triangles.length; i++ ){
value.faces.push(new THREE.Face3( triangles[i][0], triangles[i][1], triangles[i][2]));
}
var mesh = new THREE.Mesh(value, material);
scene.add(mesh);
}
function parse(){
alert("I am now inside the parse method!"); //works
$.ajax({
url:'http://localhost/human_output/output2.txt',
success: function (data){
//be wary of what a line in the output file contains, where it starts and ends.
alert("I have accessed output2!"); //works (yay~)
for (var entry in data.split('\n')) {
alert("I am in the for loop, looking at a line!");
//^works
var count = 0;
var name;
var layer_z_coordinate;
for (var chunk in entry.split('\s')) {
alert("I am looking at something after a space. Chunk:"); //works
alert(chunk);
if (/[0-9]+/.test(chunk)) {
//chunk is the layer number
alert("I am looking at a number."); //works
alert("Here is a chunk:");
alert(chunk);
layer_z_coordinate = parseInt(chunk) * (-1);
zString = layer_z_coordinate.toString();
alert("here is layer_z_coordinate made into a String:");
alert(zString); //works. this always gives me 0?
} else if (/[^0-9]+/.test(chunk)) {
//chunk is the name or a lone '|', so check if it exists, make it if necessary
alert("I am looking at a non-number 01.");
if (!body_components.has(chunk) && toString(chunk) != "|") {
name = chunk;
var geo = new THREE.Geometry();
alert(name + "hello?");
body_components.set(name, geo);
alert("I am looking a non-number 02.");
}
} else {
//chunk is the entire block of coordinates
alert("I am looking at neither a number nor a non-number?");
for (var coordinatePair in chunk.split(':')) {
alert("I just split a :");
if (count % 10 == 0) { //every 10th coordinate?
var xy = coordinatePair.split(',');
var vertex = new THREE.Vector3(parseInt(xy[0]), parseInt(xy[1]), layer_z_coordinate);
body_components.get(name).vertices.push(vertex);
//retrieves the corresponding geo(metry) for 'name'
alert("I just pushed the vertices!");
}
count++;
}
}
}
}
}
});
};
output2.txt示例:
1 |人类|男人|女人|人|智人| 309257:310257:309257:308258:309257:306258:307258:308258:305258:306258:360290:360291:360292:361293:361294:361295:361296:362297:362298:362299
1 |延髓(medulla oblongata) 121650:121650:121650
1 | Wirsung胰管| 374298:374296:374297:374298:374294:374295:374296:373294:374294:373293:336331:336332:337333:337334:338335:338336:339337:339337:339338:340339:422420:422420:423420:424420:425420: 425420:426420:426420:426419:426420:442386:443386:443386:443385:444384:444384:444383:444384:444383:444383:445382:445382:445381:445382:445380:445381:445378:445379:445380:445378: 414325:412324:413324:414325:412324:412322:412323:412324:411321:412322:377298:375298:376298:377298:375298:375298:375299:375299:374298:375299
答案 0 :(得分:0)
在代码中:
for (var chunk in entry.split('\s')) {
alert("I am looking at something after a space. Chunk:"); //works
alert(chunk);
您正在每次迭代时创建一个新数组。 chunk 将被赋予数组的属性名称,可能从0开始,因此alert(chunk)
总是返回0(并且可能进入无限循环)。
如果您正在尝试解析 output2.txt 示例,那么以下可能就是一个开始。它将文本拆分为换行符上的记录,然后通过拆分看起来是由&#39; |&#39;分隔的文本字段来处理每个记录。然后创建一个数组数组,这些数组看起来是由冒号分隔的坐标对,&#39;:&#39;字符。
任何进一步的处理都取决于您,希望这可以让您开始。
var text = '1 | human|man|woman|person|homo sapiens | 309,257:310,257:309,257:308,258:309,257:306,258:307,258:308,258:305,258:306,258:360,290:360,291:360,292:361,293:361,294:361,295:361,296:362,297:362,298:362,299' +
'\n' +
'\n1 | medulla oblongata | 121,650:121,650:121,650' +
'\n' +
'1 | pancreatic duct|duct of Wirsung | 374,298:374,296:374,297:374,298:374,294:374,295:374,296:373,294:374,294:373,293:336,331:336,332:337,333:337,334:338,335:338,336:339,337:339,337:339,338:340,339:422,420:422,420:423,420:424,420:425,420:425,420:426,420:426,420:426,419:426,420:442,386:443,386:443,386:443,385:444,384:444,384:444,383:444,384:444,383:444,383:445,382:445,382:445,381:445,382:445,380:445,381:445,378:445,379:445,380:445,378:414,325:412,324:413,324:414,325:412,324:412,322:412,323:412,324:411,321:412,322:377,298:375,298:376,298:377,298:375,298:375,298:375,299:375,299:374,298:375,299';
function parseText(s) {
// Split text on newlines to get records
var records = s.split(/\n/)
// Process each record, split fields from coords
var result = records.reduce(function(acc, rec) {
// If the record has some data, indicated by a '|' character
if (/\|/.test(rec)) {
// remove whitespace around the '|' separator
rec = rec.replace(/\s*\|\s*/g, '|');
// Get the text fields
var idx = rec.lastIndexOf('|');
var obj = {fields: rec.substring(0,idx).split(/\|/)};
// Get the coordinates
obj.coords = rec.substring(idx+1).split(':').map(function(v) {
return v.split(',');
});
// Add to accumulator
acc.push(obj);
}
return acc;
}, []);
return result;
}
console.log(parseText(text));
&#13;