def open_data(filename):
    lines = open(filename, 'r').read()
    solids = re.split("--- SOLID \[\d*\]---", lines)
    return [
        Solid([
            Face(*[
                Point(*[float(p) for p in point.split(";")])
                for point in face.split("\n") if len(point) != 0
            ]) for face in solid.split("\n\n") if face != ""
        ]) for solid in solids if solid != ""
    ]
def distance(e: Edge, p: Point) -> float:
    v = Vector(e.p0, e.p1)
    w = Vector(e.p0, p)

    c1 = dot(w, v)
    c2 = dot(v, v)
    if c1 <= 0:
        return distance(p, e.p0)
    if c2 <= c1:
        return distance(p, e.p1)

    b = c1 / c2

    pb = Point(e.p0.x + b * v.x, e.p0.y + b * v.y, e.p0.z + b * v.z)

    return distance(p, pb)
Ejemplo n.º 3
0
def cross(v1: Vector, v2: Vector) -> Vector:
    return Vector(Point(v1.z * v2.y, v1.x * v2.z, v1.y * v2.x),
                  Point(v1.y * v2.z, v1.z * v2.x, v1.x * v2.y))
Ejemplo n.º 4
0
 def __truediv__(self, other):
     if isinstance(other, (int, float)):
         return Vector(Point(self.x / other, self.y / other,
                             self.z / other))
Ejemplo n.º 5
0
 def __mul__(self, other):
     if isinstance(other, (int, float)):
         return Vector(Point(self.x * other, self.y * other,
                             self.z * other))
Ejemplo n.º 6
0
 def __sub__(self, other: 'Vector'):
     return Vector(
         Point(self.x - other.x, self.y - other.y, self.z - other.z))
Ejemplo n.º 7
0
 def __add__(self, other: 'Vector'):
     return Vector(
         Point(self.x + other.x, self.y + other.y, self.z + other.z))
        distance(ae3, be3)
    ]
    return min(distances)


@multimethod
def distance(a: Edge, b: Face) -> float:
    eb1, eb2, eb3 = b.edges
    return min(distance(a, eb1), distance(a, eb2), distance(a, eb3),
               distance(a, b.p0), distance(a, b.p1), distance(a, b.p2))


@multimethod
def distance(a: Solid, b: Solid) -> float:
    distances = [
        distance(f1, f2) for f1, f2 in itertools.product(a.faces, b.faces)
    ]
    return min(distances)


filename = './computer_graphics_algorithm/solid_data.txt'
solids = open_data(filename)

print(
    "faces: ",
    distance(
        Face(Point(1, 0, 0), Point(0, 1, 0), Point(0, 0, 0)),
        Face(Point(100, 0, 0.13), Point(0, 100, 0.13), Point(-100, -100,
                                                             0.13))))

print("solids: ", distance(solids[0], solids[1]))