Example #1
0
def test_booleans():

    # ==============================================================================
    # Make a box and a sphere
    # ==============================================================================

    box = Box.from_width_height_depth(2, 2, 2)
    box = Mesh.from_shape(box)
    box.quads_to_triangles()

    A = box.to_vertices_and_faces()

    sphere = Sphere(Point(1, 1, 1), 1)
    sphere = Mesh.from_shape(sphere, u=30, v=30)
    sphere.quads_to_triangles()

    B = sphere.to_vertices_and_faces()

    # ==============================================================================
    # Remesh the sphere
    # ==============================================================================

    B = remesh(B, 0.3, 10)

    # ==============================================================================
    # Compute the boolean mesh
    # ==============================================================================

    V, F = boolean_union(A, B)

    mesh = Mesh.from_vertices_and_faces(V, F)
Example #2
0
    def create(self, link=None):
        """Recursive function that triggers the drawing of the robot geometry.

        This method delegates the geometry drawing to the :meth:`draw_geometry`
        method. It transforms the geometry based on the saved initial
        transformation from the robot model.

        Parameters
        ----------
        link : :class:`compas.robots.Link`, optional
            Link instance to create. Defaults to the robot model's root.

        Returns
        -------
        None
        """
        if link is None:
            link = self.robot.root

        for item in itertools.chain(link.visual, link.collision):
            # NOTE: Currently, shapes assign their meshes to an
            # attribute called `geometry`, but this will change soon to `meshes`.
            # This code handles the situation in a forward-compatible
            # manner. Eventually, this can be simplified to use only `meshes` attr
            if hasattr(item.geometry.shape, 'meshes'):
                meshes = item.geometry.shape.meshes
            else:
                meshes = item.geometry.shape.geometry

            if isinstance(meshes, Shape):
                meshes = [Mesh.from_shape(meshes)]

            if meshes:
                # Coerce meshes into an iteratable (a tuple if not natively iterable)
                if not hasattr(meshes, '__iter__'):
                    meshes = (meshes, )

                is_visual = hasattr(item, 'get_color')
                color = item.get_color() if is_visual else None

                native_geometry = []
                for i, mesh in enumerate(meshes):
                    # create native geometry
                    mesh_type = 'visual' if is_visual else 'collision'
                    mesh_name = '{}.{}.{}.{}'.format(self.robot.name,
                                                     mesh_type, link.name, i)
                    native_mesh = self.draw_geometry(mesh,
                                                     name=mesh_name,
                                                     color=color)
                    # transform native geometry based on saved init transform
                    self.transform(native_mesh, item.init_transformation)
                    # append to list
                    native_geometry.append(native_mesh)

                item.native_geometry = native_geometry
                item.current_transformation = Transformation()

        for child_joint in link.joints:
            self.create(child_joint.child_link)
 def grasshopper_draw(self):
     visualisations = []
     for woodLayer in self.timberboards:
         for board in woodLayer:
             my_box = board.box
             mesh_box = Mesh.from_shape(my_box)
             artist = MeshArtist(mesh_box)
             box_visualisation = artist.draw_mesh()
             visualisations.append(box_visualisation)
     return visualisations
    def grasshopper_draw():
        visualisations = []
        for brd in self.elements():
            board = brd[1]

            # visualise all the boards
            my_box = box_update(board)[1]
            mesh_box = Mesh.from_shape(my_box)
            artist = MeshArtist(mesh_box)
            box_visualisation = artist.draw_mesh()
            visualisations.append(box_visualisation)

        return visualisations
    def mesh(self):
        """Mesh of the element."""
        if not self._source:
            return None

        if self._mesh:
            return self._mesh

        if isinstance(self._source, Mesh):
            self._mesh = self._source
        else:
            self._mesh = Mesh.from_shape(self._source)

        return self._mesh
def grasshopper_draw(system):
    def box_update(elmnt):
        elmnt.board_frame = Frame(elmnt.centre_point, elmnt.length_vector, elmnt.width_vector)
        elmnt.box = Box(elmnt.board_frame, elmnt.length, elmnt.width, elmnt.height)
        return elmnt.board_frame, elmnt.box

    visualisations = []
    for brd in system.elements():
        board = brd[1]
        # visualise all the boards
        my_box = box_update(board)[1]
        mesh_box = Mesh.from_shape(my_box)
        artist = MeshArtist(mesh_box)
        box_visualisation = artist.draw_mesh()
        visualisations.append(box_visualisation)

    return visualisations
    def from_shape(cls, shape, frame):
        """Construct an element from a shape primitive.

        Parameters
        ----------
        shape : :class:`compas.geometry.Shape`
            Shape primitive describing the element.
        frame : :class:`Frame`
            Origin frame of the element.

        Returns
        -------
        :class:`Element`
            New instance of element.
        """
        element = cls(frame)
        element._source = shape
        element._mesh = Mesh.from_shape(element._source)
        return element
Example #8
0
    def _get_item_meshes(item):
        # NOTE: Currently, shapes assign their meshes to an
        # attribute called `geometry`, but this will change soon to `meshes`.
        # This code handles the situation in a forward-compatible
        # manner. Eventually, this can be simplified to use only `meshes` attr
        if hasattr(item.geometry.shape, 'meshes'):
            meshes = item.geometry.shape.meshes
        else:
            meshes = item.geometry.shape.geometry

        if isinstance(meshes, Shape):
            meshes = [Mesh.from_shape(meshes)]

        if meshes:
            # Coerce meshes into an iterable (a tuple if not natively iterable)
            if not hasattr(meshes, '__iter__'):
                meshes = (meshes, )

        return meshes
from compas.geometry import Box, Translation
from compas.geometry import dot_vectors
from compas.datastructures import Mesh

from compas_view2.app import App

box = Box.from_width_height_depth(1, 1, 1)
cage = Mesh.from_shape(box)
cage.update_default_edge_attributes({'crease': 0})

top = sorted(
    cage.faces(),
    key=lambda face: dot_vectors(cage.face_normal(face), [0, 0, 1]))[-1]
bottom = sorted(
    cage.faces(),
    key=lambda face: dot_vectors(cage.face_normal(face), [0, 0, -1]))[-1]

c0 = cage.copy()
c1 = c0.transformed(Translation.from_vector([1.5, 0, 0]))
c2 = c1.transformed(Translation.from_vector([1.5, 0, 0]))
c3 = c2.transformed(Translation.from_vector([1.5, 0, 0]))
c4 = c3.transformed(Translation.from_vector([1.5, 0, 0]))
c5 = c4.transformed(Translation.from_vector([1.5, 0, 0]))

c1.edges_attribute('crease', 1, keys=list(cage.face_halfedges(top)))
c1.edges_attribute('crease', 1, keys=list(cage.face_halfedges(bottom)))

c2.edges_attribute('crease', 2, keys=list(cage.face_halfedges(top)))
c2.edges_attribute('crease', 2, keys=list(cage.face_halfedges(bottom)))

c3.edges_attribute('crease', 3, keys=list(cage.face_halfedges(top)))
Example #10
0
 def meshes(self):
     return [Mesh.from_shape(self)]
from compas.datastructures import Mesh

# Box in the world coordinate system
frame = Frame([1, 0, 0], [-0.45, 0.1, 0.3], [1, 0, 0])
width, length, height = 1, 1, 1
box = Box(frame, width, length, height)

# Frame F representing a coordinate system
F = Frame([2, 2, 2], [0.978, 0.010, -0.210], [0.090, 0.882, 0.463])

# Get transformation between frames and apply transformation on box.
T = Transformation.from_frame_to_frame(Frame.worldXY(), F)
box_transformed = box.transformed(T)

# make Mesh from box
mesh_transformed = Mesh.from_shape(box_transformed)

# create Projection
P = Projection.from_plane_and_direction(Plane((0, 0, 0), (0, 0, 1)), (3, 5, 5))
P = Projection.from_plane(Plane((0, 0, 0), (0, 0, 1)))
P = Projection.from_plane_and_point(Plane((0, 0, 0), (0, 0, 1)), (3, 5, 5))

# apply transformation on mesh
mesh_projected = mesh_transformed.transformed(P)

# create artists
artist1 = BoxArtist(box_transformed)
artist2 = MeshArtist(mesh_projected)

# draw
artist1.draw()
# Rhino
from compas.datastructures import Mesh
from compas.geometry import *
from compas.robots import Joint, RobotModel
from compas_fab.rhino import RobotArtist
from compas_fab.robots import Configuration

# create cylinder in yz plane
radius, length = 0.3, 5
cylinder = Cylinder(Circle(Plane([0, 0, 0], [1, 0, 0]), radius), length)
cylinder.transform(Translation([length / 2., 0, 0]))
mesh = Mesh.from_shape(cylinder)

# create robot
robot = RobotModel("robot", links=[], joints=[])
link0 = robot.add_link("world")

# Add all other links to robot
for count in range(1, 8):
    # add new link to robot
    robot.add_link("link" + str(count), visual_mesh=mesh.copy(), visual_color=(count * 0.72 % 1.0, count * 0.23 % 1.0 , 0.6))

    # add the joint between the last two links
    axis = (0, 0, 1)
    origin = Frame((length , 0, 0), (1, 0, 0), (0, 1, 0))
    robot.add_joint("joint" + str(count), Joint.CONTINUOUS, robot.links[-2] , robot.links[-1], origin, axis)

artist = RobotArtist(robot)
artist.clear_layer()

joint_names = robot.get_configurable_joint_names()
Example #13
0
    Ry = Rotation.from_axis_and_angle(Vector(0, 1, 0), radians(ry),
                                      plane.point)
    Rz = Rotation.from_axis_and_angle(Vector(0, 0, 1), radians(rz),
                                      plane.point)
    plane.transform(Rz * Ry)
    return plane


# ==============================================================================
# Input
# ==============================================================================

frame = Frame.worldXY()
box = Box(frame, 10, 3, 5)

mesh = Mesh.from_shape(box)

# ==============================================================================
# Parts
# ==============================================================================

red = []
blue = []

plane = cutting_plane(mesh, ry=0, rz=0)
A, B = mesh.cut(plane)

for _ in range(0, 7):
    plane = cutting_plane(A, ry=-40, rz=0)
    A, b = A.cut(plane)
    red.append(b)
import time

from compas.datastructures import Mesh
from compas.geometry import Box, Translation

from compas_fab.backends import RosClient
from compas_fab.robots import CollisionMesh
from compas_fab.robots import PlanningScene

with RosClient('localhost') as client:
    robot = client.load_robot()
    scene = PlanningScene(robot)

    brick = Box.from_width_height_depth(0.016, 0.012, 0.031)
    brick.transform(Translation.from_vector([0, 0, brick.zsize / 2.]))

    for i in range(5):
        mesh = Mesh.from_shape(brick)
        cm = CollisionMesh(mesh, 'brick_wall')
        cm.frame.point.y += 0.5
        cm.frame.point.z += brick.zsize * i

        scene.append_collision_mesh(cm)

    # sleep a bit before terminating the client
    time.sleep(1)
Example #15
0
def sphere():
    sphere = Sphere([0, 0, 0], 1.0)
    mesh = Mesh.from_shape(sphere, u=16, v=16)
    return mesh
Example #16
0
def box():
    box = Box.from_width_height_depth(2, 2, 2)
    return Mesh.from_shape(box)
from compas.datastructures import Mesh
from compas.geometry import Box
from compas.geometry import Frame
from compas.geometry import boolean_union_mesh_mesh

b1 = Box(Frame([+4, +4, 0], [1, 0, 0], [0, 1, 0]), 10, 10, 10)
b2 = Box(Frame([-4, -4, 0], [1, 0, 0], [0, 1, 0]), 10, 10, 10)

A = Mesh.from_shape(b1)
B = Mesh.from_shape(b2)

A.quads_to_triangles()
B.quads_to_triangles()

A = A.to_vertices_and_faces()
B = B.to_vertices_and_faces()

# Use best boolean union available depending on context
V, F = boolean_union_mesh_mesh(A, B)

mesh = Mesh.from_vertices_and_faces(V, F)
print(mesh.summary())
# Create a Box with that frame
width, length, height = 1, 1, 1
box = Box(frame, width, length, height)

box_frame_transformed = frame1.to_world_coordinates(box.frame)
box_transformed = Box(box_frame_transformed, width, length, height)

# Create a Projection (can be orthogonal, parallel or perspective)
point = [0, 0, 0]
normal = [0, 0, -1]
plane = Plane(point, normal)
direction = [2, -2, 1]
P = Projection.from_plane_and_direction(plane, direction)

# Create a Mesh from the Box
mesh = Mesh.from_shape(box)
tmesh = Mesh.from_shape(box_transformed)

# Apply the Projection onto the mesh
mesh_projected = tmesh.transformed(P)

# clear layer
artist = BoxArtist(box, layer="Default")
artist.clear_layer()

# defining Artists
artist1 = BoxArtist(box)
artist2 = BoxArtist(box_transformed)
artist3 = MeshArtist(mesh_projected)
artist4 = FrameArtist(frame, scale=1.0)
artist5 = FrameArtist(frame1, scale=1.0)
Example #19
0
 def shape(self, shape):
     self._shape = shape
     self._mesh = Mesh.from_shape(shape)
Example #20
0
def box():
    box = Box.from_corner_corner_height([0, 0, 0], [1, 1, 0], 1.0)
    mesh = Mesh.from_shape(box)
    return mesh
from compas.geometry import Translation
from compas.datastructures import Mesh
from compas.datastructures import mesh_quads_to_triangles

from compas_blender.artists import MeshArtist

import compas_libigl as igl

# ==============================================================================
# Input Geometry
# ==============================================================================

a = Box.from_width_height_depth(5.0, 3.0, 1.0)
b = Box.from_width_height_depth(1.0, 5.0, 3.0)

a = Mesh.from_shape(a)
b = Mesh.from_shape(b)

mesh_quads_to_triangles(a)
mesh_quads_to_triangles(b)

# ==============================================================================
# Booleans
# ==============================================================================

A = a.to_vertices_and_faces()
B = b.to_vertices_and_faces()

C = igl.mesh_union(A, B)
c_union = Mesh.from_vertices_and_faces(*C)
Example #22
0
from compas.datastructures import Mesh
from compas.utilities import hex_to_rgb

from compas_rhino.artists import PointArtist, PolylineArtist, MeshArtist

# @me add restart option to init
from compas.rpc import Proxy

proxy = Proxy()

# ==============================================================================
# Make a box and a sphere
# ==============================================================================

box = Box.from_width_height_depth(2, 2, 2)
box = Mesh.from_shape(box)
box.quads_to_triangles()

A = box.to_vertices_and_faces()

sphere = Sphere(Point(1, 1, 1), 1)
sphere = Mesh.from_shape(sphere, u=30, v=30)
sphere.quads_to_triangles()

B = sphere.to_vertices_and_faces()

# ==============================================================================
# Remesh the sphere
# ==============================================================================

proxy.package = 'compas_cgal.meshing'
from compas_fab.rhino import RobotArtist
from compas_fab.robots import Configuration

# create cylinder in yz plane
radius, length = 0.3, 5
cylinder = Cylinder(Circle(Plane([0, 0, 0], [1, 0, 0]), radius), length)
cylinder.transform(Translation([length / 2., 0, 0]))

# create robot
robot = RobotModel("robot", links=[], joints=[])

# add first link to robot
link0 = robot.add_link("world")

# add second link to robot
mesh = Mesh.from_shape(cylinder)
link1 = robot.add_link("link1", visual_mesh=mesh, visual_color=(0.2, 0.5, 0.6))

# add the joint between the links
axis = (0, 0, 1)
origin = Frame.worldXY()
robot.add_joint("joint1", Joint.CONTINUOUS, link0, link1, origin, axis)

# add another link
mesh = Mesh.from_shape(cylinder) # create a copy!
link2 = robot.add_link("link2", visual_mesh=mesh, visual_color=(0.5, 0.6, 0.2))

# add another joint to 'glue' the link to the chain
origin = Frame((length, 0, 0), (1, 0, 0), (0, 1, 0))
robot.add_joint("joint2", Joint.CONTINUOUS, link1, link2, origin, axis)
Example #24
0
print(F)
"""
CREATE BOX AND PROJECTION
"""
width, length, height = 1, 1, 8
box = Box(F, width, length, height)

#create projection
projection_plane = Plane([0, 0, 0], [0, 0, 1])  #world XY plane
P = Projection.from_plane(projection_plane)  #orthogonal projection

print("Projection:\n", P)
"""
CREATE PROJECTED MESH
"""
mesh_01 = Mesh.from_shape(box)
projected_mesh = mesh_01

try:
    projected_mesh.transform(P)
except:
    print("Something went wrong with the projection")
"""
ARTISTS
"""
artist_01 = BoxArtist(box, layer='box')
artist_02 = MeshArtist(mesh_01, layer='box::projection edges')

artist_01.clear_layer()
artist_02.clear_layer()
Example #25
0
from compas.geometry import Translation
from compas.datastructures import Mesh
from compas.datastructures import mesh_quads_to_triangles
from compas.datastructures import mesh_subdivide_quad
from compas_viewers.multimeshviewer import MultiMeshViewer
from compas_viewers.multimeshviewer import MeshObject
import compas_libigl as igl

# ==============================================================================
# Input Geometry
# ==============================================================================

# create a box mesh around the center of the world

box = Box.from_width_height_depth(5.0, 3.0, 1.0)
a = Mesh.from_shape(box)
mesh_quads_to_triangles(a)

# create a box mesh around the center of the world

box = Box.from_width_height_depth(1.0, 5.0, 3.0)
b = Mesh.from_shape(box)
mesh_quads_to_triangles(b)

# ==============================================================================
# Booleans
# ==============================================================================

# convert meshes to data

VA, FA = a.to_vertices_and_faces()
Example #26
0
from compas.geometry import Transformation
from compas.geometry import Box
from compas.datastructures import Mesh
from compas_rhino.artists import FrameArtist
from compas_rhino.artists import MeshArtist

# Box in the world coordinate system
frame = Frame([1, 0, 0], [-0.45, 0.1, 0.3], [1, 0, 0])
width, length, height = 1, 1, 1
box = Box(frame, width, length, height)

# Frame F representing a coordinate system
F = Frame([2, 2, 2], [0.978, 0.010, -0.210], [0.090, 0.882, 0.463])

# Get transformation between frames and apply transformation on box.
T = Transformation.from_frame_to_frame(Frame.worldXY(), F)
box_transformed = box.transformed(T)
print("Box frame transformed", box_transformed.frame)

# create artists
artist1 = FrameArtist(Frame.worldXY())
artist2 = MeshArtist(Mesh.from_shape(box))
artist3 = FrameArtist(F)
artist4 = MeshArtist(Mesh.from_shape(box_transformed))

# draw
artist1.draw()
artist2.draw_faces()
artist3.draw()
artist4.draw_faces()
Example #27
0
from compas.utilities import pairwise
from compas.utilities import linspace
from compas.utilities import remap_values

import compas_rhino
from compas_rhino.artists import FrameArtist, LineArtist, CylinderArtist
from compas_rhino.artists import RobotModelArtist

robot = RobotModel('tom')

# ==============================================================================
# Links
# ==============================================================================

cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.5), 0.02)
mesh = Mesh.from_shape(cylinder, u=32)
base = robot.add_link('base',
                      visual_meshes=[mesh],
                      visual_color=(0.1, 0.1, 0.1))

cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.2), 0.5)
cylinder.transform(Translation.from_vector([0, 0, 0.25]))
mesh = Mesh.from_shape(cylinder, u=24)
shoulder = robot.add_link('shoulder',
                          visual_meshes=[mesh],
                          visual_color=(0, 0, 1.0))

cylinder = Cylinder(Circle(Plane(Point(0, 0, 0), Vector(0, 0, 1)), 0.08), 1.0)
cylinder.transform(Translation.from_vector([0, 0, 0.5]))
mesh = Mesh.from_shape(cylinder)
arm = robot.add_link('arm', visual_meshes=[mesh], visual_color=(0.0, 1.0, 1.0))
from compas_viewers.multimeshviewer import MultiMeshViewer
from compas_viewers.multimeshviewer import MeshObject

import compas_libigl as igl

# ==============================================================================
# Input Geometry
# ==============================================================================

R = 1.4

point = [0.0, 0.0, 0.0]
cube = Box.from_width_height_depth(2 * R, 2 * R, 2 * R)
sphere = Sphere(point, R * 1.25)

a = Mesh.from_shape(cube)
b = Mesh.from_shape(sphere, u=30, v=30)

mesh_quads_to_triangles(a)
mesh_quads_to_triangles(b)

A = a.to_vertices_and_faces()
B = b.to_vertices_and_faces()

# ==============================================================================
# Booleans
# ==============================================================================

D = igl.mesh_intersection(A, B)

for n in ([1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]):
Example #29
0
import compas_rhino
from compas_rhino.artists import BoxArtist, MeshArtist

# ==============================================================================
# Paths
# ==============================================================================

HERE = os.path.dirname(__file__)
FILE_O = os.path.join(HERE, 'data', 'block.json')

# ==============================================================================
# Block and Blank
# ==============================================================================

box = Box.from_width_height_depth(200, 200, 200)
block = Mesh.from_shape(box)

vertex = next(
    block.vertices_where({
        'x': (float('-inf'), 0),
        'y': (0, float('inf')),
        'z': (0, float('inf'))
    }))
block.vertex_attribute(vertex, 'z', 120)

vertex = next(
    block.vertices_where({
        'x': (0, float('inf')),
        'y': (0, float('inf')),
        'z': (0, float('inf'))
    }))
Example #30
0
def test_astar_shortest_path_mesh():
    mesh = Mesh.from_shape(Box(Frame.worldXY(), 1, 1, 1))
    a, b = mesh.get_any_vertices(2)
    path = astar_shortest_path(mesh, a, b)
    assert path is not None