/** * Fills solid arrays with data about faces of an object generated whose status * is as required * * @param object3d solid object used to fill the arrays * @param vertices vertices array to be filled * @param indices indices array to be filled * @param colors colors array to be filled * @param faceStatus1 a status expected for the faces used to to fill the data arrays * @param faceStatus2 a status expected for the faces used to to fill the data arrays */ private void groupObjectComponents(Object3D obj, List <Vertex> vertices, List <int> indices, List <Color3f> colors, int faceStatus1, int faceStatus2) { Face face; //for each face.. for (int i = 0; i < obj.getNumFaces(); i++) { face = obj.getFace(i); //if the face status fits with the desired status... if (face.getStatus() == faceStatus1 || face.getStatus() == faceStatus2) { //adds the face elements into the arrays Vertex[] faceVerts = { face.v1, face.v2, face.v3 }; for (int j = 0; j < faceVerts.Length; j++) { if (vertices.Contains(faceVerts[j])) { indices.Add(vertices.IndexOf(faceVerts[j])); } else { indices.Add(vertices.Count); vertices.Add(faceVerts[j]); colors.Add(faceVerts[j].getColor()); } } } } }
//-------------------------FACES_SPLITTING_METHODS------------------------------// /** * Split faces so that none face is intercepted by a face of other object * * @param object the other object 3d used to make the split */ public void splitFaces(Object3D obj) { Line line; Face face1, face2; Segment[] segments; Segment segment1; Segment segment2; double distFace1Vert1, distFace1Vert2, distFace1Vert3, distFace2Vert1, distFace2Vert2, distFace2Vert3; int signFace1Vert1, signFace1Vert2, signFace1Vert3, signFace2Vert1, signFace2Vert2, signFace2Vert3; int numFacesBefore = getNumFaces(); int numFacesStart = getNumFaces(); int facesIgnored = 0; //if the objects bounds overlap... if (getBound().overlap(obj.getBound())) { //for each object1 face... for (int i = 0; i < getNumFaces(); i++) { //if object1 face bound and object2 bound overlap ... face1 = getFace(i); if (face1.getBound().overlap(obj.getBound())) { //for each object2 face... for (int j = 0; j < obj.getNumFaces(); j++) { //if object1 face bound and object2 face bound overlap... face2 = obj.getFace(j); if (face1.getBound().overlap(face2.getBound())) { //PART I - DO TWO POLIGONS INTERSECT? //POSSIBLE RESULTS: INTERSECT, NOT_INTERSECT, COPLANAR //distance from the face1 vertices to the face2 plane distFace1Vert1 = computeDistance(face1.v1, face2); distFace1Vert2 = computeDistance(face1.v2, face2); distFace1Vert3 = computeDistance(face1.v3, face2); //distances signs from the face1 vertices to the face2 plane signFace1Vert1 = (distFace1Vert1 > TOL ? 1 : (distFace1Vert1 < -TOL ? -1 : 0)); signFace1Vert2 = (distFace1Vert2 > TOL ? 1 : (distFace1Vert2 < -TOL ? -1 : 0)); signFace1Vert3 = (distFace1Vert3 > TOL ? 1 : (distFace1Vert3 < -TOL ? -1 : 0)); //if all the signs are zero, the planes are coplanar //if all the signs are positive or negative, the planes do not intersect //if the signs are not equal... if (!(signFace1Vert1 == signFace1Vert2 && signFace1Vert2 == signFace1Vert3)) { //distance from the face2 vertices to the face1 plane distFace2Vert1 = computeDistance(face2.v1, face1); distFace2Vert2 = computeDistance(face2.v2, face1); distFace2Vert3 = computeDistance(face2.v3, face1); //distances signs from the face2 vertices to the face1 plane signFace2Vert1 = (distFace2Vert1 > TOL ? 1 : (distFace2Vert1 < -TOL ? -1 : 0)); signFace2Vert2 = (distFace2Vert2 > TOL ? 1 : (distFace2Vert2 < -TOL ? -1 : 0)); signFace2Vert3 = (distFace2Vert3 > TOL ? 1 : (distFace2Vert3 < -TOL ? -1 : 0)); //if the signs are not equal... if (!(signFace2Vert1 == signFace2Vert2 && signFace2Vert2 == signFace2Vert3)) { line = new Line(face1, face2); //intersection of the face1 and the plane of face2 segment1 = new Segment(line, face1, signFace1Vert1, signFace1Vert2, signFace1Vert3); //intersection of the face2 and the plane of face1 segment2 = new Segment(line, face2, signFace2Vert1, signFace2Vert2, signFace2Vert3); //if the two segments intersect... if (segment1.intersect(segment2)) { //PART II - SUBDIVIDING NON-COPLANAR POLYGONS int lastNumFaces = getNumFaces(); this.splitFace(i, segment1, segment2); //prevent from infinite loop (with a loss of faces...) //if(numFacesStart*20<getNumFaces()) //{ // System.out.println("possible infinite loop situation: terminating faces split"); // return; //} //if the face in the position isn't the same, there was a break if (face1 != getFace(i)) { //if the generated solid is equal the origin... if (face1.equals(getFace(getNumFaces() - 1))) { //return it to its position and jump it if (i != (getNumFaces() - 1)) { faces.RemoveAt(getNumFaces() - 1); faces.Insert(i, face1); } else { continue; } } //else: test next face else { i--; break; } } } } } } } } } } }
/** * Classifies the face based on the ray trace technique * * @param object object3d used to compute the face status */ public void rayTraceClassify(Object3D obj) { //creating a ray starting starting at the face baricenter going to the normal direction Point3d p0 = new Point3d(); p0.X = (v1.X + v2.X + v3.X) / 3.0f; p0.Y = (v1.Y + v2.Y + v3.Y) / 3.0f; p0.Z = (v1.Z + v2.Z + v3.Z) / 3.0f; Line ray = new Line(getNormal(), p0); bool success; double dotProduct, distance; Point3d?intersectionPoint; Face closestFace = null; double closestDistance; do { success = true; closestDistance = Double.MaxValue; //for each face from the other solid... for (int i = 0; i < obj.getNumFaces(); i++) { Face face = obj.getFace(i); //dotProduct = face.getNormal().dot(ray.getDirection()); dotProduct = Vector3d.Dot(face.getNormal(), ray.getDirection()); intersectionPoint = ray.computePlaneIntersection(face.getNormal(), face.v1.getPosition()); //if ray intersects the plane... if (intersectionPoint != null) { distance = ray.computePointToPointDistance(intersectionPoint.Value); //if ray lies in plane... if (Math.Abs(distance) < TOL && Math.Abs(dotProduct) < TOL) { //disturb the ray in order to not lie into another plane ray.perturbDirection(); success = false; break; } //if ray starts in plane... if (Math.Abs(distance) < TOL && Math.Abs(dotProduct) > TOL) { //if ray intersects the face... if (face.hasPoint(intersectionPoint.Value)) { //faces coincide closestFace = face; closestDistance = 0; break; } } //if ray intersects plane... else if (Math.Abs(dotProduct) > TOL && distance > TOL) { if (distance < closestDistance) { //if ray intersects the face; if (face.hasPoint(intersectionPoint.Value)) { //this face is the closest face untill now closestDistance = distance; closestFace = face; } } } } } } while (success == false); //none face found: outside face if (closestFace == null) { status = OUTSIDE; } //face found: test dot product else { //dotProduct = closestFace.getNormal().dot(ray.getDirection()); dotProduct = Vector3d.Dot(closestFace.getNormal(), ray.getDirection()); //distance = 0: coplanar faces if (Math.Abs(closestDistance) < TOL) { if (dotProduct > TOL) { status = SAME; } else if (dotProduct < -TOL) { status = OPPOSITE; } } //dot product > 0 (same direction): inside face else if (dotProduct > TOL) { status = INSIDE; } //dot product < 0 (opposite direction): outside face else if (dotProduct < -TOL) { status = OUTSIDE; } } }
/** * Classifies the face based on the ray trace technique * * @param object object3d used to compute the face status */ public void rayTraceClassify(Object3D obj) { //creating a ray starting starting at the face baricenter going to the normal direction Point3d p0 = new Point3d(); p0.x = (v1.x + v2.x + v3.x) / 3d; p0.y = (v1.y + v2.y + v3.y) / 3d; p0.z = (v1.z + v2.z + v3.z) / 3d; Line ray = new Line(getNormal(), p0); bool success; double dotProduct, distance; Point3d intersectionPoint; Face closestFace = null; double closestDistance; do { success = true; closestDistance = Double.MaxValue; //for each face from the other solid... for (int i = 0; i < obj.getNumFaces(); i++) { Face face = obj.getFace(i); dotProduct = face.getNormal().dot(ray.getDirection()); intersectionPoint = ray.computePlaneIntersection(face.getNormal(), face.v1.getPosition()); //if ray intersects the plane... if (intersectionPoint != null) { distance = ray.computePointToPointDistance(intersectionPoint); //if ray lies in plane... if (Math.Abs(distance) < TOL && Math.Abs(dotProduct) < TOL) { //disturb the ray in order to not lie into another plane ray.perturbDirection(); success = false; break; } //if ray starts in plane... if (Math.Abs(distance) < TOL && Math.Abs(dotProduct) > TOL) { //if ray intersects the face... if (face.hasPoint(intersectionPoint)) { //faces coincide closestFace = face; closestDistance = 0; break; } } //if ray intersects plane... else if (Math.Abs(dotProduct) > TOL && distance > TOL) { if (distance < closestDistance) { //if ray intersects the face; if (face.hasPoint(intersectionPoint)) { //this face is the closest face untill now closestDistance = distance; closestFace = face; } } } } } } while(success == false); //none face found: outside face if (closestFace == null) { status = OUTSIDE; } //face found: test dot product else { dotProduct = closestFace.getNormal().dot(ray.getDirection()); //distance = 0: coplanar faces if (Math.Abs(closestDistance) < TOL) { if (dotProduct > TOL) { status = SAME; } else if (dotProduct < -TOL) { status = OPPOSITE; } } //dot product > 0 (same direction): inside face else if (dotProduct > TOL) { status = INSIDE; } //dot product < 0 (opposite direction): outside face else if (dotProduct < -TOL) { status = OUTSIDE; } } }
/** * Fills solid arrays with data about faces of an object generated whose status * is as required * * @param object3d solid object used to fill the arrays * @param vertices vertices array to be filled * @param indices indices array to be filled * @param colors colors array to be filled * @param faceStatus1 a status expected for the faces used to to fill the data arrays * @param faceStatus2 a status expected for the faces used to to fill the data arrays */ private void groupObjectComponents(Object3D obj, List<Vertex> vertices, List<int> indices, List<Color3f> colors, int faceStatus1, int faceStatus2) { Face face; //for each face.. for (int i = 0; i < obj.getNumFaces(); i++) { face = obj.getFace(i); //if the face status fits with the desired status... if (face.getStatus() == faceStatus1 || face.getStatus() == faceStatus2) { //adds the face elements into the arrays Vertex[] faceVerts = { face.v1, face.v2, face.v3 }; for (int j = 0; j < faceVerts.Length; j++) { if (vertices.Contains(faceVerts[j])) { indices.Add(vertices.IndexOf(faceVerts[j])); } else { indices.Add(vertices.Count); vertices.Add(faceVerts[j]); colors.Add(faceVerts[j].getColor()); } } } } }
//-------------------------FACES_SPLITTING_METHODS------------------------------// /** * Split faces so that none face is intercepted by a face of other object * * @param object the other object 3d used to make the split */ public void splitFaces(Object3D obj) { Line line; Face face1, face2; Segment[] segments; Segment segment1; Segment segment2; double distFace1Vert1, distFace1Vert2, distFace1Vert3, distFace2Vert1, distFace2Vert2, distFace2Vert3; int signFace1Vert1, signFace1Vert2, signFace1Vert3, signFace2Vert1, signFace2Vert2, signFace2Vert3; int numFacesBefore = getNumFaces(); int numFacesStart = getNumFaces(); int facesIgnored = 0; //if the objects bounds overlap... if (getBound().overlap(obj.getBound())) { //for each object1 face... for (int i = 0; i < getNumFaces(); i++) { //if object1 face bound and object2 bound overlap ... face1 = getFace(i); if (face1.getBound().overlap(obj.getBound())) { //for each object2 face... for (int j = 0; j < obj.getNumFaces(); j++) { //if object1 face bound and object2 face bound overlap... face2 = obj.getFace(j); if (face1.getBound().overlap(face2.getBound())) { //PART I - DO TWO POLIGONS INTERSECT? //POSSIBLE RESULTS: INTERSECT, NOT_INTERSECT, COPLANAR //distance from the face1 vertices to the face2 plane distFace1Vert1 = computeDistance(face1.v1, face2); distFace1Vert2 = computeDistance(face1.v2, face2); distFace1Vert3 = computeDistance(face1.v3, face2); //distances signs from the face1 vertices to the face2 plane signFace1Vert1 = (distFace1Vert1 > TOL ? 1 : (distFace1Vert1 < -TOL ? -1 : 0)); signFace1Vert2 = (distFace1Vert2 > TOL ? 1 : (distFace1Vert2 < -TOL ? -1 : 0)); signFace1Vert3 = (distFace1Vert3 > TOL ? 1 : (distFace1Vert3 < -TOL ? -1 : 0)); //if all the signs are zero, the planes are coplanar //if all the signs are positive or negative, the planes do not intersect //if the signs are not equal... if (!(signFace1Vert1 == signFace1Vert2 && signFace1Vert2 == signFace1Vert3)) { //distance from the face2 vertices to the face1 plane distFace2Vert1 = computeDistance(face2.v1, face1); distFace2Vert2 = computeDistance(face2.v2, face1); distFace2Vert3 = computeDistance(face2.v3, face1); //distances signs from the face2 vertices to the face1 plane signFace2Vert1 = (distFace2Vert1 > TOL ? 1 : (distFace2Vert1 < -TOL ? -1 : 0)); signFace2Vert2 = (distFace2Vert2 > TOL ? 1 : (distFace2Vert2 < -TOL ? -1 : 0)); signFace2Vert3 = (distFace2Vert3 > TOL ? 1 : (distFace2Vert3 < -TOL ? -1 : 0)); //if the signs are not equal... if (!(signFace2Vert1 == signFace2Vert2 && signFace2Vert2 == signFace2Vert3)) { line = new Line(face1, face2); //intersection of the face1 and the plane of face2 segment1 = new Segment(line, face1, signFace1Vert1, signFace1Vert2, signFace1Vert3); //intersection of the face2 and the plane of face1 segment2 = new Segment(line, face2, signFace2Vert1, signFace2Vert2, signFace2Vert3); //if the two segments intersect... if (segment1.intersect(segment2)) { //PART II - SUBDIVIDING NON-COPLANAR POLYGONS int lastNumFaces = getNumFaces(); this.splitFace(i, segment1, segment2); //prevent from infinite loop (with a loss of faces...) //if(numFacesStart*20<getNumFaces()) //{ // System.out.println("possible infinite loop situation: terminating faces split"); // return; //} //if the face in the position isn't the same, there was a break if (face1 != getFace(i)) { //if the generated solid is equal the origin... if (face1.equals(getFace(getNumFaces() - 1))) { //return it to its position and jump it if (i != (getNumFaces() - 1)) { faces.RemoveAt(getNumFaces() - 1); faces.Insert(i, face1); } else { continue; } } //else: test next face else { i--; break; } } } } } } } } } } }