Example #1
0
def addVectors(a, b):  # `a` and `b` must have the same lenght!
    from vectormath import Vector3 as Vector3
    return Vector3([a[i] + b[i] for i in range(len(a))])
import pyfabrik
from vectormath import Vector3
import numpy as np
import cv2

# Create a black image
img = np.zeros((500, 500, 3), np.uint8)

initial_joint_positions = [
    Vector3(0, 0, 0),
    Vector3(5, 2, 0),
    Vector3(10, 2, 0),
    Vector3(15, 0, 0)
]
tolerance = 0.01

# Initialize the Fabrik class (Fabrik, Fabrik2D or Fabrik3D)
fab = pyfabrik.Fabrik3D(initial_joint_positions, tolerance)

currentPosition = Vector3(15, 0, 0)
goalPosition = Vector3(10, 5, 0)

newGoalPosition = currentPosition
dx = (goalPosition[0] - currentPosition[0]) / 100
dy = (goalPosition[1] - currentPosition[1]) / 100
dz = (goalPosition[2] - currentPosition[2]) / 100

img_array = []

for i in range(1, 101):
    img = np.zeros((500, 500, 3), np.uint8)
Example #3
0
def getAtomsAtEquilibriumPositions():
    import numpy as np
    from math import sqrt, cos, sin, pi
    import vectormath as vmath
    from vectormath import Vector3 as Vector3
    # BCC:
    # A1 = Atom("Co", Vector3(1.0, 0.0, 0.0) )
    # A2 = Atom("Co", Vector3(0.0, 1.0, 0.0) )
    # A3 = Atom("Co", Vector3(0.0, 0.0, 1.0) )
    # A4 = Atom("Co", Vector3(0.0, 0.0, 0.0) )
    # A5 = Atom("Co", Vector3(1.0, 1.0, 0.0) )
    # A6 = Atom("Co", Vector3(1.0, 0.0, 1.0) )
    # A7 = Atom("Co", Vector3(0.0, 1.0, 1.0) )
    # A8 = Atom("Co", Vector3(1.0, 1.0, 1.0) )





    a = sqrt(2.0)
    n = 3 # for a cuboid of n^3 atoms
    ex = Vector3(1, 0, 0)
    ey = Vector3(0, 1, 0)
    ez = Vector3(0, 0, 1)
    a1 = (a / 2) * (ex + ey)
    a2 = (a / 2) * (ey + ez)
    a3 = (a / 2) * (ex + ez)

    lAtoms = []
    for i in range(n):
        for j in range(n):
            for k in range(n):
                vx = i * a1
                vy = j * a2
                vz = k * a3
                lAtoms.append( Atom("H", vx + vy + vz, False, -1 ) )





    # a = 0.978351 #0.982
    # nMax = 5 #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    #
    # lAtoms = []
    # for i in range(nMax):
    #     for j in range(nMax):
    #         # k = (i * nMax) + j
    #         x = i * a
    #         y = j * a
    #         lAtoms.append( Atom("H", Vector3(x, y, 0.0), False, -1 ) )
    #



    #
    # A0 = Atom("Co", Vector3(0, 0, 0), False, -1 )
    #
    # # dist = a
    # A1 = Atom("Co", Vector3(a, 0,  0), False, -1 )
    # A2 = Atom("Co", Vector3(-a, 0, 0), False, -1 )
    # A3 = Atom("Co", Vector3(0, a,  0), False, -1 )
    # A4 = Atom("Co", Vector3(0, -a, 0), False, -1 )
    #
    # # dist = a*sqrt(2)
    # lx = a
    # ly = a
    # A5 = Atom("Co", Vector3(lx,  ly, 0), False, -1 )
    # A6 = Atom("Co", Vector3(lx, -ly, 0), False, -1 )
    # A7 = Atom("Co", Vector3(-lx, ly, 0), False, -1 )
    # A8 = Atom("Co", Vector3(-lx, -ly,0), False, -1 )
    #
    # # dist = 2a
    # l = 2 * a
    # A9  = Atom("Co", Vector3( l, 0, 0), False, -1 )  #1
    # A10 = Atom("Co", Vector3(-l, 0, 0), True,   9 )  #1
    # A11 = Atom("Co", Vector3(0,  l, 0), False, -1 )  #2
    # A12 = Atom("Co", Vector3(0, -l, 0), True,  11 )  #2
    #
    # # dist = a*sqrt(5)
    # lx = a
    # ly = 2 * a
    # A13 = Atom("Co", Vector3(lx,  ly, 0), False, -1 )  #3
    # A14 = Atom("Co", Vector3(lx, -ly, 0), True,  13 )  #3
    # A15 = Atom("Co", Vector3(-lx, ly, 0), False, -1 )  #4
    # A16 = Atom("Co", Vector3(-lx, -ly,0), True,  13 )  #4
    #
    # lx = 2 * a
    # ly = a
    # A17 = Atom("Co", Vector3(lx,  ly, 0), False, -1 )  #5
    # A18 = Atom("Co", Vector3(lx, -ly, 0), False, -1 )  #6
    # A19 = Atom("Co", Vector3(-lx, ly, 0), True,  18 )  #5
    # A20 = Atom("Co", Vector3(-lx, -ly,0), True,  17 )  #6
    #
    # lx = 2 * a
    # ly = 2 * a
    # A21 = Atom("Co", Vector3(lx,  ly, 0), False, -1 )  #7
    # A22 = Atom("Co", Vector3(lx, -ly, 0), True,  21 )  #7
    # A23 = Atom("Co", Vector3(-lx, ly, 0), True,  21 )  #7
    # A24 = Atom("Co", Vector3(-lx, -ly,0), True,  21 )  #7
    #
    #
    # lAtoms = [A0, A1,A2,A3,A4,  A5,A6,A7,A8,A9,A10,\
    #           A11,A12,A13,A14,A15,A16,A17,A18,A19,A20,\
    #           A21,A22,A23,A24]
    #
    #
    #
    # # lAtoms = [A1,A2,A3,A4,A5,A6,A7,A8]
    # # lAtoms = [A1,A2,A3]
    return lAtoms, a1, a2, a3
Example #4
0
def facing(quat) -> Vector3:
    """Given a Unit Quaternion, return the Unit Vector of its direction."""
    return rotate_vector(Vector3(0, 1, 0), quat)
Example #5
0
def break_rotor(q: quaternion) -> Tuple[float, Vector3]:
    """Given a Unit Quaternion, break it into an angle and a Vector3."""
    theta, v = 2 * np.arccos(q.w), []
    axis = Vector3(*v)
    return theta, axis
Example #6
0
# Library imports
import argparse
import numpy as np
import vectormath as vm
from vectormath import Vector3
from pprint import pprint
from PIL import Image
import time

# Project imports
import raytracer as rt

m1 = rt.Material(diffuse=Vector3(0.7, 0.2, 0.5),
                 ambient=Vector3(0.3, 0.3, 0.3),
                 specular=Vector3(0.85, 0.85, 0.85),
                 reflective=Vector3(0.5, 0.2, 0.1),
                 phong_power=200)

m2 = rt.Material(diffuse=Vector3(0.5, 0.7, 0.4),
                 ambient=Vector3(0.3, 0.3, 0.3),
                 specular=Vector3(0.55, 0.55, 0.55),
                 reflective=Vector3(0.1, 0.3, 0.2),
                 phong_power=32)

m3 = rt.Material(diffuse=Vector3(0.1, 0.3, 0.7),
                 ambient=Vector3(0.3, 0.3, 0.3),
                 specular=Vector3(0.75, 0.75, 0.75),
                 reflective=Vector3(0.1, 0.2, 0.4),
                 phong_power=148)

m4 = rt.Material(diffuse=Vector3(0.4, 0.1, 0.6),
Example #7
0
from math import degrees, radians
from typing import Tuple, Type, Union

from numba import jit
import numpy as np
from quaternion import quaternion
from vectormath import Vector3

NumpyVector: Type = Union[np.ndarray, Tuple[float, float, float]]
Quat: Type = Union[quaternion, Tuple[float, float, float, float]]

EAST = Vector3(1, 0, 0)
WEST = Vector3(-1, 0, 0)

NORTH = Vector3(0, 1, 0)
SOUTH = Vector3(0, -1, 0)

ZENITH = Vector3(0, 0, 1)
NADIR = Vector3(0, 0, -1)

###===---
# COORDINATE TRANSFORMATIONS
###===---


@jit(nopython=True)
def to_spherical(x: float, y: float, z: float) -> Tuple[float, float, float]:
    """Convert three-dimensional Cartesian Coordinates to Spherical."""
    rho = np.sqrt(np.sum(np.square(np.array((x, y, z)))))
    theta = 90 - degrees(np.arccos(z / rho)) if rho else 0
    phi = (0 if theta == 90 or theta == -90 else
Example #8
0
 def velocity(self, value: np.ndarray):
     self._vel: Vector3 = Vector3(value)
Example #9
0
from raymarch import *
from raymarch.shapes import Sphere
from vectormath import Vector3

# create some spheres
shapes = [Sphere(Vector3(0,0,0), 1), Sphere(Vector3(1,0,0), 0.5, [255, 100, 100]), Sphere(Vector3(-1,0,0), 0.5, [100,100,255])]

# create the camera
camera = Camera(1080, 720, 90, Vector3(0, 3, 0), Vector3(0, -1, 0))

# create the scene with a black background
# if you want, you can specify a special norm by specifing p_norm=1 (or any other value > 0)
scene = Scene(camera, shapes, [0,0,0])

# set the number of threads you want to spawn
#scene.poolsize = 4

# draw the scene
scene.draw()
Example #10
0
 def velocity(self) -> Vector3:
     """Go into the relevant Space structure and retrieve the Velocity that
         is assigned to this FoR, and wrap it in a Vector3.
     """
     return Vector3(self.domain.array_velocity[self.index])
Example #11
0
 def position(self, value: np.ndarray):
     self._pos: Vector3 = Vector3(value)
Example #12
0
 def position(self) -> Vector3:
     """Go into the relevant Space structure and retrieve the Position that
         is assigned to this FoR, and wrap it in a Vector3.
     """
     return Vector3(self.domain.array_position[self.index])
Example #13
0
def simplest_scene():
    cam = default_camera()
    lights = [rt.Light(cam.position, Vector3(.9, .9, .9))]
    spheres = [rt.Sphere(Vector3(0.0, 0.0, 0.0), .5, m1)]

    return rt.Scene(cam, lights, spheres, background=Vector3(0.3, 0.3, 0.3))
Example #14
0
def default_camera():
    return rt.Camera(position=Vector3(0., 0., 6.),
                     look_at=Vector3(0., 0., 0.),
                     up=Vector3(0.0, 1.0, 0.0),
                     right=Vector3(1.0, 0.0, 0.0))
# moves are performed... more info about how to do the last part.

# select a starting configuration:
#e, X = Emin, lAtomsEquilibrium
# e, X = Emin, [lAtomsEquilibrium[i].position for i in range(len(lAtomsEquilibrium))]

import copy

# structEq
# convert 'structX' to a simple array of coordinates `X`:
# Xeq = [list(structEq[i].coords) for i in range(structEq.num_sites)]
Xeq = [list(structEq[i].frac_coords) for i in range(structEq.num_sites)]

from vectormath import Vector3 as Vector3
Xeq = [
    Vector3(list(structEq[i].frac_coords)) for i in range(structEq.num_sites)
]
# Xeq

X = copy.deepcopy(Xeq)
# X

# structEq[0]
# structEq[0].coords
# structEq[0].frac_coords

e = Emin
X = copy.deepcopy(Xeq)

# Main Reverse Energy Partitioning loop:
#L = 0.005 #0.05 #0.1 #0.01  #0.01 #0.2 #0.5 # 0.1#  ~amplitud of random walk
Example #16
0
def plot_spherical(
    ax: Axes3D,
    x: float,
    y: float,
    z: float,
    *,
    arcs_primary: bool = False,
    label_primary: bool = False,
    arcs_secondary: bool = False,
    cartesian_trace: bool = False,
    angle_square: bool = False,
    mark_final: bool = True,
):
    rho, theta, phi = to_spherical(x, y, z)

    color_rho = "red"
    color_theta = "green"
    color_phi = "blue"
    grey = "#777777"
    seg = 15
    w = 0.1

    def mark(x_, y_, z_, label: str = None):
        ax.text(
            x_,
            y_,
            z_,
            (label or "  {}").format(point(x_, y_, z_)),
        )

    def plot(*points: Tuple[float, float, float], **kw):
        points = np.array(points)
        return ax.plot(points[..., 0], points[..., 1], points[..., 2], **kw)

    arc_theta = lambda d=1: starmap(from_spherical, (
        (rho * d, (theta / seg * i), phi) for i in range(seg + 1)))
    arc_phi = lambda d=1: starmap(from_spherical, (
        (rho * d, 0, (phi / seg * i)) for i in range(seg + 1)))

    # Y-axis Line.
    if arcs_primary:
        plot((0, rho, 0), (0, 0, 0), c=grey)
    elif arcs_secondary:
        plot((0, rho * 0.5, 0), (0, 0, 0), c=grey)

    # Coordinate Triangle.
    if cartesian_trace:
        plot(
            (x, y, z),  # Endpoint.
            (x, y, 0),  # Below Endpoint.
            (x, 0, 0),  # Endpoint on X-Axis.
            (0, 0, 0),  # Origin.
            c=grey,
        )

        # Right Angle.
        if angle_square:
            angle_off = min(abs(x * w), abs(y * w), abs(z * w))
            plot(
                (x, y, angle_off),
                (x, y - angle_off, angle_off),
                (x, y - angle_off, 0),
                c=grey,
            )

        # mark(x, 0, 0)  # Endpoint on X-Axis.
        # mark(x, y, 0)  # Below Endpoint.

    # SECONDARY Labels.
    if arcs_secondary:
        ax.text(0, rho * 0.52, 0, "ρ", c="black")
        ax.text(*from_spherical(rho * 0.52, (theta / 2), phi), "θ", c="black")
        ax.text(*from_spherical(rho * 0.52, 0, (phi / 2)), "φ", c="black")
        plot(*arc_theta(0.5), c=grey)  # GREY Theta Arc.
        plot(*arc_phi(0.5), c=grey)  # GREY Phi Arc.

    if arcs_primary:
        plot(*arc_theta(), c=color_theta)  # Theta Arc.
        plot(*arc_phi(), c=color_phi)  # Phi Arc.
        plot((0, 0, 0), (x, y, z), c=color_rho)
        plot(
            (0, 0, 0),  # Origin.
            Vector3(x, y, 0).normalize() * rho,  # Point where Theta=0.
            c=grey,
        )

    # PRIMARY Labels.
    if label_primary:
        ax.text(
            x,
            y,
            z,
            f"ρ ({np.round(rho, 2)})",
            c=color_rho,
            fontsize=12,
            horizontalalignment="right",
            verticalalignment="bottom",
        )
        ax.text(
            *from_spherical(rho, (theta / 2), phi),
            f"θ ({np.round(theta, 2)}°)",
            c=color_theta,
            fontsize=12,
            horizontalalignment="left",
            verticalalignment="bottom",
        )
        ax.text(
            *from_spherical(rho, 0, (phi / 2)),
            f"φ ({np.round(phi, 2)}°)",
            c=color_phi,
            fontsize=12,
            horizontalalignment="left",
            verticalalignment="bottom",
        )

    if mark_final:
        mark(x, y, z, f"{{}}\n{point(rho, theta, phi)}")  # Endpoint.
Example #17
0
def test_value_error_raised_when_joints_overlap():
    poss = [Vector3(0, 0, 0), Vector3(10, 0, 0), Vector3(10, 0, 0)]
    with pytest.raises(ValueError) as exinfo:
        fab = Fabrik3D(poss, 0.01)
    assert str(exinfo.value) == "link lengths must be > 0"
Example #18
0
def test_value_error_raised_when_tolerance_isnt_positive(tolerance):
    poss = [Vector3(0, 0, 0), Vector3(10, 0, 0), Vector3(10, 0, 0)]
    with pytest.raises(ValueError) as exinfo:
        fab = Fabrik3D(poss, tolerance)
    assert str(exinfo.value) == "tolerance must be > 0"