def test_create_mesh_with_API(self):
        # 1.- Create a VCD instance
        vcd = core.VCD()

        # Mesh sample representation
        #
        # P0 L0   P1   L4   P4
        # *--------*--------*
        # |        |        |
        # |L3  A0  |L1  A1  |L5
        # |        |        |
        # *--------*--------*
        # P3  L2   P2  L6   P5
        #
        # V0  [A0,A1]

        mesh1 = types.mesh("parkslot1")

        # Vertex
        # P0
        p0 = types.point3d("Vertex", (25, 25, 0))
        p0.add_attribute(types.text("T Shape", "PM_line_ending_type"))
        p0_id = mesh1.add_vertex(p0)
        # P1
        p1 = types.point3d("Vertex", (26, 25, 0))
        p1.add_attribute(types.text("I Shape", "PM_line_ending_type"))
        mesh1.add_vertex(p1)
        # P2
        p2 = types.point3d("Vertex", (26, 26, 0))
        p2.add_attribute(types.text("U Shape", "PM_line_ending_type"))
        mesh1.add_vertex(p2)
        # P3
        p3 = types.point3d("Vertex", (25, 26, 0))
        p3.add_attribute(types.text("C Shape", "PM_line_ending_type"))
        mesh1.add_vertex(p3)
        # P4
        p4 = types.point3d("Vertex", (27, 25, 0))
        p4.add_attribute(types.text("T Shape", "PM_line_ending_type"))
        mesh1.add_vertex(p4)
        # P5
        p5 = types.point3d("Vertex", (27, 26, 0))
        p5.add_attribute(types.text("I Shape", "PM_line_ending_type"))
        mesh1.add_vertex(p5)

        # Edges
        # L0
        l0 = types.lineReference("Edge", [0, 1], types.ObjectDataType.point3d)
        l0.add_attribute(types.text("Single Solid", "PM_line_marking_type"))
        l0.add_attribute(
            types.text("White", "PM_lPM_line_colourine_marking_typ"))
        l0_id = mesh1.add_edge(l0)
        # L1
        l1 = types.lineReference("Edge", [1, 2], types.ObjectDataType.point3d)
        l1.add_attribute(types.text("Double Solid", "PM_line_marking_type"))
        l1.add_attribute(
            types.text("Blue", "PM_lPM_line_colourine_marking_type"))
        l1_id = mesh1.add_edge(l1)
        # L2
        l2 = types.lineReference("Edge", [2, 3], types.ObjectDataType.point3d)
        l2.add_attribute(types.text("Dashed", "PM_line_marking_type"))
        l2.add_attribute(
            types.text("Yellow", "PM_lPM_line_colourine_marking_type"))
        l2_id = mesh1.add_edge(l2)
        # L3
        l3 = types.lineReference("Edge", [3, 0], types.ObjectDataType.point3d)
        l3.add_attribute(types.text("Cross", "PM_line_marking_type"))
        l3.add_attribute(
            types.text("Green", "PM_lPM_line_colourine_marking_type"))
        mesh1.add_edge(l3)
        # L4
        l4 = types.lineReference("Edge", [1, 4], types.ObjectDataType.point3d)
        l4.add_attribute(types.text("Single Solid", "PM_line_marking_type"))
        l4.add_attribute(
            types.text("White", "PM_lPM_line_colourine_marking_type"))
        mesh1.add_edge(l4)
        # L5
        l5 = types.lineReference("Edge", [4, 5], types.ObjectDataType.point3d)
        l5.add_attribute(types.text("Double Solid", "PM_line_marking_type"))
        l5.add_attribute(
            types.text("Blue", "PM_lPM_line_colourine_marking_type"))
        mesh1.add_edge(l5)
        # L6
        l6 = types.lineReference("Edge", [5, 2], types.ObjectDataType.point3d)
        l6.add_attribute(types.text("Dashed", "PM_line_marking_type"))
        l6.add_attribute(
            types.text("Yellow", "PM_lPM_line_colourine_marking_type"))
        mesh1.add_edge(l6)

        # Areas
        # A0
        a0 = types.areaReference("Slot", [0, 1, 2, 3],
                                 types.ObjectDataType.line_reference)
        a0.add_attribute(types.text("Fishbone", "PM_park_slot_type"))
        a0.add_attribute(types.text("Empty", "PM_park_slot_content"))
        mesh1.add_area(a0)
        a1 = types.areaReference("Slot", [4, 5, 6, 1],
                                 types.ObjectDataType.line_reference)
        a1.add_attribute(types.text("Fishbone", "PM_park_slot_type"))
        a1.add_attribute(types.text("Empty", "PM_park_slot_content"))
        mesh1.add_area(a1)

        mesh_id = vcd.add_object("mesh1", "mesh")
        vcd.add_object_data(mesh_id, mesh1)

        string_mesh = mesh1.get_mesh_geometry_as_string()

        self.assertEqual(
            string_mesh,
            "[[[25,25,0],[26,25,0],[26,26,0],[25,26,0],[27,25,0],[27,26,0]],"
            "[[0,1],[1,2],[2,3],[3,0],[1,4],[4,5],[5,2]],[[0,1,2,3],[4,5,6,1]]]"
        )

        if not os.path.isfile('./etc/test_mesh.json'):
            vcd.save('./etc/test_mesh.json', True)
Ejemplo n.º 2
0
            category_token = instance['category_token']
            category = nusc.get('category', category_token)
            attribute_token = annotation['attribute_tokens']

            if len(attribute_token) > 0:
                attribute = nusc.get('attribute', attribute_token[0])
                vcd.add_object_data(objects_uids[instance['token']],
                                    types.text('attribute', attribute['name']),
                                    (frame_num, frame_num))

            visibility_token = annotation['visibility_token']
            visibility = nusc.get('visibility', visibility_token)

            vcd.add_object_data(
                objects_uids[instance['token']],
                types.point3d('translation', tuple(annotation['translation'])),
                (frame_num, frame_num))
            vcd.add_object_data(objects_uids[instance['token']],
                                types.vec('size', tuple(annotation['size'])),
                                (frame_num, frame_num))
            vcd.add_object_data(
                objects_uids[instance['token']],
                types.vec('rotation', tuple(annotation['rotation'])),
                (frame_num, frame_num))

            vcd.add_object_data(
                objects_uids[instance['token']],
                types.num('num_lidar_pts', annotation['num_lidar_pts']),
                (frame_num, frame_num))
            vcd.add_object_data(
                objects_uids[instance['token']],
Ejemplo n.º 3
0
    def __copy_elements(self, vcd_430, root, frame_num=None):
        if 'objects' in root:
            for object in root['objects']:
                uid = str(object['uid'])  # Let's convert to string here
                name = object['name']
                ontologyUID = None
                if 'ontologyUID' in object:
                    ontologyUID = str(object['ontologyUID'])  # Let's convert to string here
                typeSemantic = object.get('type', '')  # In VCD 4.3.0 type is required, but it VCD 3.3.0 seems to be not

                if not vcd_430.has(core.ElementType.object, uid):
                    vcd_430.add_object(name, typeSemantic, frame_num, uid, ontologyUID)

                if 'objectDataContainer' in object:
                    objectDataContainer = object['objectDataContainer']
                    for key, value in objectDataContainer.items():
                        for object_data in value:
                            inStream = None
                            if 'inStream' in object_data:
                                inStream = object_data['inStream']
                            if 'val' in object_data:
                                val = object_data['val']
                            currentObjectData = None

                            # Create main object_data body
                            # NOTE: in the following calls, I am using direct access to dictionary for required fields, e.g.
                            # object_data['name'], etc.
                            # For optional fields, I am using get() function, e.g. object_data.get('mode') which defaults to
                            # None
                            if key == 'num':
                                if len(val) == 1:
                                    # Single value, this is a num
                                    currentObjectData = types.num(object_data['name'], val[0], inStream)
                                else:
                                    # Multiple values, this is a vec
                                    currentObjectData = types.vec(object_data['name'], val, inStream)
                            elif key == 'bool':
                                currentObjectData = types.boolean(object_data['name'], val, inStream)
                            elif key == 'text':
                                currentObjectData = types.text(object_data['name'], val, inStream)
                            elif key == 'image':
                                currentObjectData = types.image(
                                    object_data['name'], val,
                                    object_data['mimeType'], object_data['encoding'],
                                    inStream
                                )
                            elif key == 'binary':
                                currentObjectData = types.binary(
                                    object_data['name'], val,
                                    object_data['dataType'], object_data['encoding'],
                                    inStream
                                )
                            elif key == 'vec':
                                currentObjectData = types.vec(object_data['name'], val, inStream)
                            elif key == 'bbox':
                                currentObjectData = types.bbox(object_data['name'], val, inStream)
                            elif key == 'cuboid':
                                currentObjectData = types.cuboid(object_data['name'], val, inStream)
                            elif key == 'mat':
                                currentObjectData = types.mat(
                                    object_data['name'], val,
                                    object_data['channels'], object_data['width'], object_data['height'],
                                    object_data['dataType'],
                                    inStream
                                )
                            elif key == 'point2D':
                                currentObjectData = types.point2d(object_data['name'], val, object_data.get('id'), inStream)
                            elif key == 'point3D':
                                currentObjectData = types.point3d(object_data['name'], val, object_data.get('id'), inStream)
                            elif key == "poly2D":
                                mode_int = object_data['mode']
                                currentObjectData = types.poly2d(
                                    object_data['name'], val, types.Poly2DType(mode_int), object_data['closed'], inStream
                                )
                            elif key == "poly3D":
                                currentObjectData = types.poly3d(object_data['name'], val, object_data['closed'], inStream)
                            elif key == "mesh":
                                currentObjectData = types.mesh(object_data['name'])
                                if 'point3D' in object_data:
                                    for p3d_330 in object_data['point3D']:
                                        # Create a types.point3d object and add it to the mesh
                                        id = p3d_330['id']
                                        name = p3d_330['name']
                                        val = p3d_330['val']

                                        p3d_430 = types.point3d(name, val)
                                        self.__add_attributes(p3d_330, p3d_430)
                                        currentObjectData.add_vertex(p3d_430, id)

                                if 'lineReference' in object_data:
                                    for lref_330 in object_data['lineReference']:
                                        # Create a types.line_reference object and add it to the mesh
                                        id = lref_330['id']
                                        name = lref_330['name']
                                        referenceType = lref_330['referenceType']
                                        assert(referenceType == "point3D")
                                        val = lref_330.get('val')  # defaults to None, needed for the constructor

                                        lref_430 = types.lineReference(name, val, types.ObjectDataType.point3d)
                                        self.__add_attributes(lref_330, lref_430)
                                        currentObjectData.add_edge(lref_430, id)

                                if 'areaReference' in object_data:
                                    for aref_330 in object_data['areaReference']:
                                        # Create a types.area_reference object and add it to the mesh
                                        id = aref_330['id']
                                        name = aref_330['name']
                                        referenceType = aref_330['referenceType']
                                        assert (referenceType == "point3D" or referenceType == "lineReference")
                                        val = aref_330.get('val')  # defaults to None, needed for the constructor

                                        if referenceType == "point3D":
                                            aref_430 = types.areaReference(name, val, types.ObjectDataType.point3d)
                                        else:
                                            aref_430 = types.areaReference(name, val, types.ObjectDataType.line_reference)
                                        self.__add_attributes(aref_330, aref_430)
                                        currentObjectData.add_area(aref_430, id)

                            # Add any attributes
                            self.__add_attributes(object_data, currentObjectData)

                            # Add the object_data to the object
                            vcd_430.add_object_data(uid, currentObjectData, frame_num)

        if 'actions' in root:
            for action in root['actions']:
                uid = str(action['uid'])
                name = action['name']
                ontologyUID = None
                if 'ontologyUID' in action:
                    ontologyUID = str(action['ontologyUID'])
                typeSemantic = action.get('type', '')  # required in VCD 4.0, not in VCD 3.3.0
                vcd_430.add_action(name, typeSemantic, frame_num, uid, ontologyUID)

        if 'events' in root:
            for event in root['events']:
                uid = str(event['uid'])
                name = event['name']
                ontologyUID = None
                if 'ontologyUID' in event:
                    ontologyUID = str(event['ontologyUID'])
                typeSemantic = event.get('type', '')
                vcd_430.add_event(name, typeSemantic, frame_num, uid, ontologyUID)

        if 'contexts' in root:
            for context in root['contexts']:
                uid = str(context['uid'])
                name = context['name']
                ontologyUID = None
                if 'ontologyUID' in context:
                    ontologyUID = str(context['ontologyUID'])
                typeSemantic = context.get('type', '')
                vcd_430.add_context(name, typeSemantic, frame_num, uid, ontologyUID)

        if 'relations' in root:
            for relation in root['relations']:
                uid = str(relation['uid'])
                name = relation['name']
                ontologyUID = None
                if 'ontologyUID' in relation:
                    ontologyUID = str(relation['ontologyUID'])
                predicate = relation.get('predicate', '')
                rdf_objects = relation.get('rdf_objects', None)
                rdf_subjects = relation.get('rdf_subjects', None)

                vcd_430.add_relation(name, predicate, frame_value=frame_num, uid=uid, ont_uid=ontologyUID)
                relation = vcd_430.get_element(core.ElementType.relation, uid)
                if not 'rdf_objects' in relation or len(relation['rdf_objects']) == 0:  # just add once
                    for rdf_object in rdf_objects:
                        element_type = None
                        rdf_object_type_str = rdf_object['type']
                        if rdf_object_type_str == "Object":
                            element_type = core.ElementType.object
                        elif rdf_object_type_str == "Action":
                            element_type = core.ElementType.action
                        elif rdf_object_type_str == "Event":
                            element_type = core.ElementType.event
                        elif rdf_object_type_str == "Context":
                            element_type = core.ElementType.context
                        else:
                            warnings.warn("ERROR: Unrecognized Element type. Must be Object, Action, Event or Context.")

                        vcd_430.add_rdf(uid, core.RDF.object, str(rdf_object['uid']), element_type)

                if not 'rdf_subjects' in relation or len(relation['rdf_subjects']) == 0:  # just add once
                    for rdf_subject in rdf_subjects:
                        element_type = None
                        rdf_object_type_str = rdf_subject['type']
                        if rdf_object_type_str == "Object":
                            element_type = core.ElementType.object
                        elif rdf_object_type_str == "Action":
                            element_type = core.ElementType.action
                        elif rdf_object_type_str == "Event":
                            element_type = core.ElementType.event
                        elif rdf_object_type_str == "Context":
                            element_type = core.ElementType.context
                        else:
                            warnings.warn("ERROR: Unrecognized Element type. Must be Object, Action, Event or Context.")

                        vcd_430.add_rdf(uid, core.RDF.subject, str(rdf_subject['uid']), element_type)