Advertisement

slice a triangle with plane and return 3 new triangles

Started by March 13, 2016 12:05 PM
0 comments, last by _WeirdCat_ 8 years, 11 months ago

If i manage to cut that and return only something that is lets say behind cutting plane, its fairly easy, but i need to extract all triangles that are made up after cut, i can't find any solution for that.

someone can ask what the hell 3 triangles out of one triangle wtf? when you cut a triangle with a plane, it will give you one triangle and a quad (and quad = 2 tris)

nvm i ll find which vertex is the only one on one side and then create rest

function ain't finished but this idea is implemented



/*
 * Won't work on model that ain't fully made out of pure triangles
 * Splits solid by a plane, note that initially this function is written for solid made out of pure triangles
 * because it creates new set of triangles for each split (always 2 additional tris per triangle split)
 */
void SplitByPlane(t3dpoint<T> nn, T d)
{

//First check how many triangles we need to split (this will give us new number of tris since each tri split gives 2 additional triangles)
//(in ex. on one side of a split plane is one vertex, we shrink sides connected to this vert, then on other side we have 2 vertices
//(along with 2 collision points that will form a quad which is simply two tris)

int vcount = header.LENGTH;
int fcount = FaceLength;

for (int i=0; i < FaceLength; i++)
{
int index = VBO_BE[i].INDEX_START;
int first_size = classifyapointagainstaplane(AOS[index].v, nn, d);
if (first_size == isOnPlane) continue; //do not split
for (int n=1; n < VBO_BE[i].length; n++)
	if (first_size != classifyapointagainstaplane(AOS[index+n].v, nn, d)) //we have a split
	{
		vcount = vcount + 2 * 3;
		fcount = fcount + 2;
		break;
	}
}

TTGLVertex<T,float,float> * tAOS = new TTGLVertex<T,float,float>[vcount]; //new vert array

for (int i=0; i < header.LENGTH; i++) tAOS[i] = AOS[i]; //copy vertice structure;

//loop again through faces and find if one is split then fix that face along with adding two new triangles

for (int i=0; i < FaceLength; i++)
{
int index = VBO_BE[i].INDEX_START;
int first_size = classifyapointagainstaplane(AOS[index].v, nn, d);
if (first_size == isOnPlane) continue; //do not split
bool cut = false;
for (int n=1; n < VBO_BE[i].length; n++)
	if (first_size != classifyapointagainstaplane(AOS[index+n].v, nn, d)) //there is a cut
		cut = true;


if (!cut) continue;

TTriSplitInfo vsplit_nfo[3];
for (int n=0; n < VBO_BE[i].length; n++)
{
	vsplit_nfo[n].side 			= classifyapointagainstaplane(AOS[index+n].v, nn, d);
	vsplit_nfo[n].vertex_index  = index + n;
}
//now we need to find vertex that is 'alone' on one of the plane sides
int back_sides 	= 0;
int front_sides = 0;
int last_backside;
int last_frontside;

for (int p=0; p < 3; p++)
{
  if (vsplit_nfo[p].side == isBack)
	 { back_sides 	= back_sides 	+ 1; last_backside 	= p; }
else { front_sides	= front_sides 	+ 1; last_frontside = p; }
}
int apex_index;
if (back_sides == 1) apex_index = last_backside;
				else apex_index = last_frontside;

vec3 vA[3];
if (apex_index == 0)
{
vA[0] = AOS[apex_index].v;
vA[1] = AOS[apex_index+1].v;
vA[2] = AOS[apex_index+2].v;
}


if (apex_index == 1)
{
vA[0] = AOS[apex_index].v;
vA[1] = AOS[apex_index+1].v;
vA[2] = AOS[apex_index-1].v;
}

if (apex_index == 2)
{
vA[0] = AOS[apex_index].v;
vA[1] = AOS[apex_index-2].v;
vA[2] = AOS[apex_index-1].v;
}
/*
 * vA is our original triangle but apex (side alone vertex) is a first vert in triangle
 * now we can easily determine 3 tris.
 *
 * First tri:
 * Apex > collision point with +1 > collision point with +2
 *
 * Second tri:
 * +1 vert > collision with apex (save to Kc) > +2 vert
 *
 * Thrid tri:
 * +2 vert > collision with apex > Kc
 */

vec3 v[9];

vec3 Kc = vA[1]; SegmentPlaneIntersection(normal, dst, vA[0], vA[1], Kc);
vec3 Kp = vA[2]; SegmentPlaneIntersection(normal, dst, vA[0], vA[2], Kp);
v[0] = vA[0];
v[1] = Kc;
v[2] = Kp;

v[3] = vA[1];
v[4] = Kc;
v[5] = vA[2];

v[6] = vA[2];
v[7] = Kp;
v[8] = Kc;

//fill newly created vertex data to the vertex array
}

	//Clear whole model arrays
	DeInitialize();

	//Now make new ones (since each face has 3 verts we can easily recreate everything [when we have vert array])

}

This topic is closed to new replies.

Advertisement