Trouble with an algorithm to find edges
I will try to be as thorough as possible in describing the problem here, but the task at hand is a peculiar one so I may miss something. let me know if it is unclear.
So, I am working on a CFD code. I am still new to programming, so the part I have been given to work on is relatively easy: Given a field of points, I am supposed to find out which correspond to the object in the center of the field. For clarity, here's what that looks like in an x-y plot: Example.
Now, the program I am working with spits out these points as a series of triangles. The points in the field are numbered from 1 to N, and each triangle is presented as a list of three of these points. I first divide these triangles into three edges apiece. To find out where the object in the center lies, in this case a wing, I need to find the edges that don't repeat, as the only places where triangles aren't touching each other is the surface of the wing.
The first code I wrote to do this worked just fine, but it was naive and slow: it compared every single edge to every other edge to find matches. That's fine on a small scale, but ultimately I'll be working with data that has hundreds of thousands of points. So, I've been working on an algorithm to make things quicker.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int main()
{
//I've gotten rid of the code that reads the file and converts that data
//into edges, to make this easier to read. I know that part works fine.
//E = Number of triangles
//N = Number of points
vector<int> Newalg (9*E,0); //3 edges per triangle * 3 spaces per edge (two for endpoints, one to mark duplicates)
vector<int> Newcheck (N,0);
int temp1 = 0;
int temp2 = 0;
//write all edges to newalg:
for (int i=0;i<3*E;i++)
{
Newalg[3*i] = Edges[2*i];
Newalg[3*i+1] = Edges[2*i+1];
temp1 = Edges[2*i] - 1;
temp2 = Edges[2*i+1] - 1;
Newcheck[temp1] += 1;
Newcheck[temp2] += 1;
};
//find max instances of any one point
int inst = 0;
for (int i=0;i<N;i++)
{
if (Newcheck[i] > inst)
{
inst = Newcheck[i];
};
};
vector<int> Instances (inst*N,0); //This allows one to compare edges only with those edges in which the same point apppears
temp1 = 0;
temp2 = 0;
for (int i=0;i<N;i++)
{
Newcheck[i] = 0; //resets vector for use in next loop
};
int temp3 = 0; //placeholder value for stacking vectors
for (int i=0;i<3*E;i++)
{
temp1 = Newalg[3*i];
temp2 = Newalg[3*i+1];
//check against all other cases found thus far
for (int j=0;j<inst;j++)
{
if (Instances[j] == 0)
{
continue;
};
temp3 = Instances[inst*(temp1-1)+j] - 1;
if ((Newalg[3*temp3] == Newalg[3*i]) && (Newalg[3*temp3+1] == Newalg[3*i+1]))
{
Newalg[3*i+2] = 1;
Newalg[3*temp3+2] = 1;
};
temp3 = Instances[inst*(temp2-1)+j] - 1;
if ((Newalg[3*temp3] == Newalg[3*i]) && (Newalg[3*temp3+1] == Newalg[3*i+1]))
{
Newalg[3*i+2] = 1;
Newalg[3*temp3+2] = 1;
};
};
//first endpoint write
Newcheck[temp1-1] += 1; //-1 relates to fact that vector starts at 0
temp3 = Newcheck[temp1-1];
Instances[inst*(temp1-1)+(temp3-1)] = i+1;
//second endpoint write
Newcheck[temp2-1] += 1;
temp3 = Newcheck[temp2-1];
Instances[inst*(temp2-1)+(temp3-1)] = i+1;
};
//eliminate all rows with "1" in third column
temp3 = 0;
for (int i=0;i<3*E;i++)
{
if (Newalg[3*i+2] > 0)
{
Newalg[3*i] = 0;
Newalg[3*i+1] = 0;
Newalg[3*i+2] = 0;
temp3++;
};
};
cout<<"Eliminates "<<temp3<<" edges"<<endl;
};
Basically, it's supposed to find all the edges in which an endpoint appears, and compare them to one another to find matches instead of trying against every single edge. The most times any endpoint appears in these edges is 20 for the data set I'm working with right now, which is much smaller than the ~21000 total edges.
I know from the naive original program that I should get 80 edges on the wing. This code produces 20658. I've spent probably 50 hours trying to figure out where the problem is, and haven't had any luck. If you can spot my error, I will be much obliged, as this is starting to gnaw on my sanity.