summary refs log tree commit diff
path: root/src/Models.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Models.cpp')
-rw-r--r--src/Models.cpp116
1 files changed, 26 insertions, 90 deletions
diff --git a/src/Models.cpp b/src/Models.cpp
index a414008..acbbaa8 100644
--- a/src/Models.cpp
+++ b/src/Models.cpp
@@ -117,99 +117,35 @@ void Model::draw(float r, float g, float b)
 	glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
 }
 
-int Model::LineCheck(XYZ p1,XYZ p2, XYZ *p)
+int Model::LineCheck(XYZ p1, XYZ p2, XYZ *p)
 {
-  	int j;
-	float distance;
-	float olddistance=9999999.0;
-	int intersecting=0;
-	int firstintersecting=-1;
-	XYZ point;
-	if (segmentIntersectsSphere(p1, p2, boundingspherecenter, boundingsphereradius))
-	for (j=0;j<TriangleNum;j++){
-		intersecting=LineFacetd(p1,p2,vertex[Triangles[j].vertex[0]],vertex[Triangles[j].vertex[1]],vertex[Triangles[j].vertex[2]],normals[j],&point);
-		if (intersecting == 0) continue;
-		distance=(point.x-p1.x)*(point.x-p1.x)+(point.y-p1.y)*(point.y-p1.y)+(point.z-p1.z)*(point.z-p1.z);
-		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
+	int result = -1;
+	if (segmentIntersectsSphere(p1, p2, boundingspherecenter,
+	                            boundingsphereradius)) {
+		float olddistance = 9999999.0;
+		for (int j = 0; j < TriangleNum; ++j) {
+			XYZ point;
+			if (!LineFacetd(p1, p2,
+			                vertex[Triangles[j].vertex[0]],
+			                vertex[Triangles[j].vertex[1]],
+			                vertex[Triangles[j].vertex[2]],
+			                normals[j], &point))
+				continue;
+			float distance = sqrlen(point - p1);
+			if (distance < olddistance || result == -1) {
+				olddistance = distance;
+				result = j;
+				*p = point;
+			}
+		}
 	}
-	return firstintersecting;
+	return result;
 }
 
-int Model::LineCheck2(XYZ p1,XYZ p2, XYZ *p, XYZ move, float rotate)
+int Model::LineCheck2(XYZ p1, XYZ p2, XYZ *p, XYZ move, float deg_y)
 {
-  	int j;
-	float distance;
-	float olddistance=9999999.0;
-	int intersecting=0;
-	int firstintersecting=-1;
-	XYZ point;
-	p1=p1-move;
-	p2=p2-move;
-	if(rotate)p1=DoRotation(p1,0,-rotate,0);
-	if(rotate)p2=DoRotation(p2,0,-rotate,0);
-	if (segmentIntersectsSphere(p1, p2, boundingspherecenter, boundingsphereradius))
-	for (j=0;j<TriangleNum;j++){
-		intersecting=LineFacetd(p1,p2,vertex[Triangles[j].vertex[0]],vertex[Triangles[j].vertex[1]],vertex[Triangles[j].vertex[2]],normals[j],&point);
-		if (intersecting == 0) continue;
-		distance=(point.x-p1.x)*(point.x-p1.x)+(point.y-p1.y)*(point.y-p1.y)+(point.z-p1.z)*(point.z-p1.z);
-		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
-	}
-
-	if(rotate)*p=DoRotation(*p,0,rotate,0);
-	*p=*p+move;
-	return firstintersecting;
-}
-
-int Model::LineCheck2(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
-{
-  	int j;
-	float distance;
-	float olddistance=9999999.0;
-	int intersecting=0;
-	int firstintersecting=-1;
-	XYZ point;
-	*p1=*p1-*move;
-	*p2=*p2-*move;
-	if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0);
-	if(*rotate)*p2=DoRotation(*p2,0,-*rotate,0);
-	if (segmentIntersectsSphere(*p1, *p2, boundingspherecenter, boundingsphereradius))
-	for (j=0;j<TriangleNum;j++){
-		intersecting = LineFacetd(*p1, *p2,
-			vertex[Triangles[j].vertex[0]],
-			vertex[Triangles[j].vertex[1]],
-			vertex[Triangles[j].vertex[2]],
-			normals[j], &point);
-		if (intersecting == 0) continue;
-		distance=(point.x-p1->x)*(point.x-p1->x)+(point.y-p1->y)*(point.y-p1->y)+(point.z-p1->z)*(point.z-p1->z);
-		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
-	}
-
-	if(*rotate)*p=DoRotation(*p,0,*rotate,0);
-	*p=*p+*move;
-	return firstintersecting;
-}
-
-int Model::LineCheck3(XYZ p1,XYZ p2, XYZ *p, XYZ move, float rotate, float *d)
-{
-  	int j;
-	float distance;
-	float olddistance=9999999.0;
-	int intersecting=0;
-	int firstintersecting=-1;
-	XYZ point;
-	p1=p1-move;
-	p2=p2-move;
-	p1=DoRotation(p1,0,-rotate,0);
-	p2=DoRotation(p2,0,-rotate,0);
-	if (segmentIntersectsSphere(p1, p2, boundingspherecenter, boundingsphereradius))
-	for (j=0;j<TriangleNum;j++){
-		intersecting=LineFacetd(p1,p2,vertex[Triangles[j].vertex[0]],vertex[Triangles[j].vertex[1]],vertex[Triangles[j].vertex[2]],normals[j],&point);
-		if (intersecting == 0) continue;
-		distance=(point.x-p1.x)*(point.x-p1.x)+(point.y-p1.y)*(point.y-p1.y)+(point.z-p1.z)*(point.z-p1.z);
-		if((distance<olddistance||firstintersecting==-1)&&intersecting){olddistance=distance; firstintersecting=j; *p=point;}
-	}
-	*d=intersecting;
-	*p=DoRotation(*p,0,rotate,0);
-	*p=*p+move;
-	return firstintersecting;
+	int result = this->LineCheck(rotate(p1 - move, 0, -deg_y, 0),
+		rotate(p2 - move, 0, -deg_y, 0), p);
+	*p = rotate(*p, 0, deg_y, 0) + move;
+	return result;
 }