def remove_overlapping(models):
    keep_models = []
    for i in progress.bar(range(len(models)), label='Removing Overlapping...'):
        m1 = models.pop()
        overlapping = False

        for m2 in models:

            minpt1, maxpt1 = scene.sirikata_bounds(m1.boundsInfo)
            minpt1 *= m1.scale
            minpt1 += numpy.array([m1.x, m1.y, m1.z], dtype=numpy.float32)
            maxpt1 *= m1.scale
            maxpt1 += numpy.array([m1.x, m1.y, m1.z], dtype=numpy.float32)

            minpt2, maxpt2 = scene.sirikata_bounds(m2.boundsInfo)
            minpt2 *= m2.scale
            minpt2 += numpy.array([m2.x, m2.y, m2.z], dtype=numpy.float32)
            maxpt2 *= m2.scale
            maxpt2 += numpy.array([m2.x, m2.y, m2.z], dtype=numpy.float32)

            overlapping = overlapping or overlaps((minpt1, maxpt1),
                                                  (minpt2, maxpt2))

        if not overlapping:
            keep_models.append(m1)

    return keep_models
def remove_overlapping(models):
    keep_models = []
    for i in progress.bar(range(len(models)), label='Removing Overlapping...'):
        m1 = models.pop()
        overlapping = False
        
        for m2 in models:
            
            minpt1, maxpt1 = scene.sirikata_bounds(m1.boundsInfo)
            minpt1 *= m1.scale
            minpt1 += numpy.array([m1.x, m1.y, m1.z], dtype=numpy.float32)
            maxpt1 *= m1.scale
            maxpt1 += numpy.array([m1.x, m1.y, m1.z], dtype=numpy.float32)
            
            minpt2, maxpt2 = scene.sirikata_bounds(m2.boundsInfo)
            minpt2 *= m2.scale
            minpt2 += numpy.array([m2.x, m2.y, m2.z], dtype=numpy.float32)
            maxpt2 *= m2.scale
            maxpt2 += numpy.array([m2.x, m2.y, m2.z], dtype=numpy.float32)
            
            overlapping = overlapping or overlaps((minpt1, maxpt1), (minpt2, maxpt2))
            
        if not overlapping:
            keep_models.append(m1)
            
    return keep_models
def generate_flying(models, terrain, map, json_out):
    terrain_bounds = scene.sirikata_bounds(terrain.boundsInfo)
    minpt, maxpt = terrain_bounds
    minpt *= terrain.scale
    maxpt *= terrain.scale
    height_max = (maxpt[2] - minpt[2]) * 1.20

    flying_models = models['flying']
    centers = map.centers.values()
    random.shuffle(centers)
    centers = centers[:len(flying_models)]
    for center, flying_model in progress.bar(
            zip(centers,
                flying_models), label='Generating flying objects... '):
        center_pt = numpy.array(
            [center.x, center.y, center.elevation * Z_SCALE],
            dtype=numpy.float32)
        center_pt = scene.mapgen_coords_to_sirikata(center_pt, terrain)

        rand_height = random.uniform(center_pt[2], height_max) * 1.10

        m = scene.SceneModel(flying_model['full_path'],
                             x=float(center_pt[0]),
                             y=float(center_pt[1]),
                             z=rand_height,
                             scale=random.uniform(1.0, 8.0),
                             model_type='flying')
        json_out.append(m.to_json())
    print 'Generated (%d) flying objects' % len(flying_models)
def generate_flying(models, terrain, map, json_out):
    terrain_bounds = scene.sirikata_bounds(terrain.boundsInfo)
    minpt, maxpt = terrain_bounds
    minpt *= terrain.scale
    maxpt *= terrain.scale
    height_max = (maxpt[2] - minpt[2]) * 1.20
    
    flying_models = models['flying']
    centers = map.centers.values()
    random.shuffle(centers)
    centers = centers[:len(flying_models)]
    for center, flying_model in progress.bar(zip(centers, flying_models), label='Generating flying objects... '):
        center_pt = numpy.array([center.x, center.y, center.elevation * Z_SCALE], dtype=numpy.float32)
        center_pt = scene.mapgen_coords_to_sirikata(center_pt, terrain)

        rand_height = random.uniform(center_pt[2], height_max) * 1.10
        
        m = scene.SceneModel(flying_model['full_path'],
                       x=float(center_pt[0]),
                       y=float(center_pt[1]),
                       z=rand_height,
                       scale=random.uniform(1.0, 8.0),
                       model_type='flying')
        json_out.append(m.to_json())
    print 'Generated (%d) flying objects' % len(flying_models)
def generate_roads(models, terrain, map, json_out):
    numroads = 0
    for center in progress.bar(map.centers.values(),
                               label='Generating roads... '):
        road_edges = [
            e for e in center.edges
            if e.is_road and e.corner0 is not None and e.corner1 is not None
        ]
        if len(road_edges) != 2:
            continue

        e1, e2 = road_edges
        e1_0 = numpy.array(
            [e1.corner0.x, e1.corner0.y, e1.corner0.elevation * Z_SCALE],
            dtype=numpy.float32)
        e1_1 = numpy.array(
            [e1.corner1.x, e1.corner1.y, e1.corner1.elevation * Z_SCALE],
            dtype=numpy.float32)
        e2_0 = numpy.array(
            [e2.corner0.x, e2.corner0.y, e2.corner0.elevation * Z_SCALE],
            dtype=numpy.float32)
        e2_1 = numpy.array(
            [e2.corner1.x, e2.corner1.y, e2.corner1.elevation * Z_SCALE],
            dtype=numpy.float32)

        region_center = numpy.array(
            [center.x, center.y, center.elevation * Z_SCALE])

        for end1, edge1, edge2 in [(region_center, e1_0, e1_1),
                                   (region_center, e2_0, e2_1)]:
            end2 = v3mid(edge1, edge2)

            midpt = v3mid(end1, end2)
            midpt = scene.mapgen_coords_to_sirikata(midpt, terrain)

            kata_pt1 = scene.mapgen_coords_to_sirikata(end1, terrain)
            kata_pt2 = scene.mapgen_coords_to_sirikata(end2, terrain)

            scale = v3dist(kata_pt1, kata_pt2) / 2

            m = scene.SceneModel(ROAD_PATH,
                                 x=float(midpt[0]),
                                 y=float(midpt[1]),
                                 z=float(midpt[2]),
                                 scale=scale,
                                 model_type='road')

            kataboundmin, kataboundmax = scene.sirikata_bounds(m.boundsInfo)
            scenemin = kataboundmin * scale + midpt
            scenemax = kataboundmax * scale + midpt
            xmid = (scenemax[0] - scenemin[0]) / 2.0 + scenemin[0]
            road_edge1 = numpy.array([xmid, scenemin[1], scenemin[2]],
                                     dtype=numpy.float32)
            road_edge2 = numpy.array([xmid, scenemax[1], scenemin[2]],
                                     dtype=numpy.float32)

            midv3 = Vec3(midpt[0], midpt[1], midpt[2])
            src = Vec3(road_edge2[0], road_edge2[1], road_edge2[2])
            src -= midv3
            src_copy = Vec3(src)
            target = Vec3(kata_pt1[0], kata_pt1[1], kata_pt1[2])
            target -= midv3
            cross = src.cross(target)
            w = math.sqrt(
                src.lengthSquared() * target.lengthSquared()) + src.dot(target)
            q = Quat(w, cross)
            q.normalize()

            orig_up = q.getUp()
            orig_up.normalize()

            edge1_kata = scene.mapgen_coords_to_sirikata(edge1, terrain)
            edge2_kata = scene.mapgen_coords_to_sirikata(edge2, terrain)
            new_up = normal_vector(kata_pt1, edge1_kata, edge2_kata)
            new_up = Vec3(new_up[0], new_up[1], new_up[2])
            rotate_about = orig_up.cross(new_up)
            rotate_about.normalize()
            angle_between = orig_up.angleDeg(new_up)
            r = Quat()
            r.setFromAxisAngle(angle_between, rotate_about)
            r.normalize()
            q *= r
            q.normalize()

            m.orient_x = q.getI()
            m.orient_y = q.getJ()
            m.orient_z = q.getK()
            m.orient_w = q.getR()

            numroads += 1
            json_out.append(m.to_json())

    print 'Generated (%d) road objects' % numroads
def generate_roads(models, terrain, map, json_out):
    numroads = 0
    for center in progress.bar(map.centers.values(), label='Generating roads... '):
        road_edges = [e for e in center.edges if e.is_road and e.corner0 is not None and e.corner1 is not None]
        if len(road_edges) != 2:
            continue
        
        e1, e2 = road_edges
        e1_0 = numpy.array([e1.corner0.x, e1.corner0.y, e1.corner0.elevation * Z_SCALE], dtype=numpy.float32)
        e1_1 = numpy.array([e1.corner1.x, e1.corner1.y, e1.corner1.elevation * Z_SCALE], dtype=numpy.float32)
        e2_0 = numpy.array([e2.corner0.x, e2.corner0.y, e2.corner0.elevation * Z_SCALE], dtype=numpy.float32)
        e2_1 = numpy.array([e2.corner1.x, e2.corner1.y, e2.corner1.elevation * Z_SCALE], dtype=numpy.float32)
        
        region_center = numpy.array([center.x, center.y, center.elevation * Z_SCALE])
        
        for end1, edge1, edge2 in [(region_center, e1_0, e1_1), (region_center, e2_0, e2_1)]:
            end2 = v3mid(edge1, edge2)
            
            midpt = v3mid(end1, end2)
            midpt = scene.mapgen_coords_to_sirikata(midpt, terrain)
            
            kata_pt1 = scene.mapgen_coords_to_sirikata(end1, terrain)
            kata_pt2 = scene.mapgen_coords_to_sirikata(end2, terrain)
            
            scale = v3dist(kata_pt1, kata_pt2) / 2
            
            m = scene.SceneModel(ROAD_PATH,
                           x=float(midpt[0]),
                           y=float(midpt[1]),
                           z=float(midpt[2]),
                           scale=scale,
                           model_type='road')
            
            kataboundmin, kataboundmax = scene.sirikata_bounds(m.boundsInfo)
            scenemin = kataboundmin * scale + midpt
            scenemax = kataboundmax * scale + midpt
            xmid = (scenemax[0] - scenemin[0]) / 2.0 + scenemin[0]
            road_edge1 = numpy.array([xmid, scenemin[1], scenemin[2]], dtype=numpy.float32)
            road_edge2 = numpy.array([xmid, scenemax[1], scenemin[2]], dtype=numpy.float32)
            
            midv3 = Vec3(midpt[0], midpt[1], midpt[2])
            src = Vec3(road_edge2[0], road_edge2[1], road_edge2[2])
            src -= midv3
            src_copy = Vec3(src)
            target = Vec3(kata_pt1[0], kata_pt1[1], kata_pt1[2])
            target -= midv3
            cross = src.cross(target)
            w = math.sqrt(src.lengthSquared() * target.lengthSquared()) + src.dot(target)
            q = Quat(w, cross)
            q.normalize()
            
            orig_up = q.getUp()
            orig_up.normalize()
            
            edge1_kata = scene.mapgen_coords_to_sirikata(edge1, terrain)
            edge2_kata = scene.mapgen_coords_to_sirikata(edge2, terrain)
            new_up = normal_vector(kata_pt1, edge1_kata, edge2_kata)
            new_up = Vec3(new_up[0], new_up[1], new_up[2])
            rotate_about = orig_up.cross(new_up)
            rotate_about.normalize()
            angle_between = orig_up.angleDeg(new_up)
            r = Quat()
            r.setFromAxisAngle(angle_between, rotate_about)
            r.normalize()
            q *= r
            q.normalize()
            
            m.orient_x = q.getI()
            m.orient_y = q.getJ()
            m.orient_z = q.getK()
            m.orient_w = q.getR()
            
            numroads += 1
            json_out.append(m.to_json())
    
    print 'Generated (%d) road objects' % numroads