def DropBlockToSurface():
    try:

        obj = rs.GetObjects('Select Objects',
                            rs.filter.curve | rs.filter.instance
                            | rs.filter.mesh | rs.filter.surface
                            | rs.filter.subd | rs.filter.light
                            | rs.filter.polysurface,
                            preselect=True)
        srf = rs.GetObject('Select Surface')

        if obj:
            if srf:

                rs.EnableRedraw(False)

                # Check if srf is a mesh, if so convert to Nurb
                isMesh = rs.IsMesh(srf)
                if isMesh == True:
                    srf = rs.MeshToNurb(srf)

                # For each object send test rays up and down in Z coord
                # Move each object to the ray test that hits a srf
                for i in obj:
                    bndBox = rs.BoundingBox(i)
                    pt1 = bndBox[0]
                    pt2 = bndBox[2]
                    crv = rs.AddLine(pt1, pt2)

                    if crv:
                        midcrv = rs.CurveMidPoint(crv)
                        rs.DeleteObject(crv)

                    ray_pt_up = rs.ShootRay(srf,
                                            midcrv, (0, 0, 1),
                                            reflections=1)
                    ray_pt_down = rs.ShootRay(srf,
                                              midcrv, (0, 0, -1),
                                              reflections=1)

                    if ray_pt_up:
                        vector = rs.VectorCreate(ray_pt_up[1], midcrv)
                        rs.MoveObject(i, vector)

                    if ray_pt_down:
                        vector = rs.VectorCreate(ray_pt_down[1], midcrv)
                        rs.MoveObject(i, vector)

                # deleate any created srf
                if isMesh == True:
                    rs.DeleteObject(srf)
                rs.EnableRedraw(True)

    except:
        print("Failed to execute")
        rs.EnableRedraw(True)
        return
def shoot_rays(room):
    """Performs the raytracing process using the room properties. Calculates
    all reflections, including geometry, times and energy values per segment.

    Parameters
    ----------
    room: object
        The room object to be analyzed.

    """
    # TODO: Figure out if it is possible to cut ray one reflection short.
    # TODO: how to get rid of deepcopy? it is super slow.
    # TODO: should propably use some generators, is that possible?

    directions = room.source.directions
    ref_srf = [room.surfaces[gk]['guid'] for gk in room.surfaces]
    ref_map = {room.surfaces[sk]['guid']: sk for sk in room.surfaces}

    room.ray_times = {dk: {} for dk in directions}
    room.ray_lengths = {dk: {} for dk in directions}
    room.ray_powers = {dk: {} for dk in directions}
    room.ray_lines = {dk: {} for dk in directions}

    for dk in directions:
        dir = directions[dk]
        src_ = room.source.xyz
        w = room.source.ray_power[dk]
        min_w = room.source.ray_minpower[dk]
        time = 0
        i = 0
        min_power = False
        while time < room.ctime and not min_power:
            i += 1
            ray = rs.ShootRay(ref_srf, src_, dir, 2)
            srf = rs.PointClosestObject(ray[0], ref_srf)[0]
            mp_list = []
            if i > 0:
                sk = ref_map[str(srf)]
                abs = room.materials[room.surfaces[sk]['material']].absorption
                for wk in w:
                    w[wk] *= (1 - abs[wk])
                    mp_list.append(w[wk] < min_w[wk])
            min_power = all(mp_list)
            l = distance_point_point(ray[0], ray[1])
            t = int((l / 343.0) * 1000)
            room.ray_times[dk][i] = t
            room.ray_lengths[dk][i] = l
            room.ray_powers[dk][i] = deepcopy(w)
            room.ray_lines[dk][i] = ((ray[0].X, ray[0].Y, ray[0].Z),
                                     (ray[1].X, ray[1].Y, ray[1].Z))
            dir = vector_from_points(ray[1], ray[2])
            src_ = ray[1]
            time += t
Beispiel #3
0
    def RunScript(self, Vi, NmbrOfRay, NmbrOfRef, Tolerance):

        __author__ = "theomarchal"
        self.Params.Input[
            0].Description = "Visualize (from esquissons main engine)"
        self.Params.Input[
            1].Description = "Definition/number of ray (1-100)<default=10>"
        self.Params.Input[
            2].Description = "Level of reflexion/number of bounces(1-10)<default=2>"
        self.Params.Input[
            3].Description = "Ray Distance to listener tolerance (0.1-1.0)<default=0.5>"
        self.Params.Output[0].Description = "Reflexion Rays"
        self.Name = "Visualize Reflections"
        self.NickName = "Visualize Rays"
        self.Message = "EsquisSons V3"
        self.Category = "EsquisSons"
        self.SubCategory = "2/ Visualization"
        Def = NmbrOfRay
        Level = NmbrOfRef
        Dist = Tolerance
        import rhinoscriptsyntax as rs
        import Grasshopper.Kernel as gh

        rem = gh.GH_RuntimeMessageLevel.Remark
        ero = gh.GH_RuntimeMessageLevel.Error
        war = gh.GH_RuntimeMessageLevel.Warning

        if Def == None:
            Def = 10
        if Level == None:
            Level = 2
        if Dist == None:
            Dist = 0.5
        try:
            Ge = Vi[0]
        except:
            self.AddRuntimeMessage(
                ero, 'A "Vi" ouptut from main engine must be connected !')
            raise Exception('noInput')
        Lis = Vi[1]
        Src = Vi[2]
        udiv = Def
        vdiv = Def
        Pt_L = Lis[0][0]
        Pt_R = Lis[0][2]
        srf = []
        FirstRay = []
        SecondRay = []
        First_RefPoint = []
        drawray = []
        for i in Ge:
            srf.append(i[0])
        for i in Src:
            sph = (rs.AddSphere(i[1], i[2]))
            Src_pt = (i[1])
            u = rs.SurfaceDomain(sph, 0)
            v = rs.SurfaceDomain(sph, 1)
            pts = []
            for i in range(0, udiv + 1, 1):
                for j in range(0, vdiv + 1, 1):
                    pt = (i / udiv, j / vdiv, 0)
                    sphP = rs.SurfaceParameter(sph, pt)
                    newpt = rs.EvaluateSurface(sph, sphP[0], sphP[1])
                    pts.append(rs.AddPoint(newpt))
            Dir = []
            for p in pts:
                Dir.append(rs.VectorCreate(p, Src_pt))
            Reflexion = []
            for d in Dir:
                Reflexion.append(rs.ShootRay(srf, Src_pt, d,
                                             reflections=Level))
            SourceRay = []
            for v in Reflexion:
                Cl_Pt = []
                Ray_v = []
                try:
                    Ray_v.append(rs.AddPolyline(v))
                except:
                    pass
                for u in Ray_v:
                    pt_on = rs.CurveClosestPoint(u, Pt_L)
                    cl = rs.EvaluateCurve(u, pt_on)
                    Dicl = (rs.Distance(Pt_L, cl))
                    if Dicl <= ((Lis[0])[3]) * Dist:
                        try:
                            drawray.append(u)
                        except:
                            pass
        if len(drawray) == 0:
            self.AddRuntimeMessage(
                war,
                'No ray, please be sure Geometries are connected to main engine and placed to create reflexions'
            )
            self.AddRuntimeMessage(war,
                                   'Change parameters to generate more rays')
            Rays = None
        else:
            Rays = drawray
        return Rays
Beispiel #4
0
def ProjectCurvesToTIN():
    try:
        crvs = rs.GetObjects(message="Select curves to project",
                             filter=4,
                             group=True,
                             preselect=False,
                             select=False,
                             objects=None,
                             minimum_count=1,
                             maximum_count=0,
                             custom_filter=None)
        if not crvs:
            return
        obj = rs.GetObject("Select the TIN to project onto", 8 | 16 | 32)
        if not obj:
            return
        isMesh = rs.IsMesh(obj)
        zUpList = []
        zDownList = []

        rs.EnableRedraw(False)

        # Convert Mesh to Nurbs for ShootRay compatibility
        if isMesh == True:
            srf = rs.MeshToNurb(obj)
        if isMesh == False:
            srf = obj

        # Shoot ray from each grip point and move grips to reflection point
        for crv in crvs:
            rs.EnableObjectGrips(crv)
            grips = rs.ObjectGripLocations(crv)
            for grip in grips:

                zUp = rs.ShootRay(srf, grip, (0, 0, 1), 1)
                # if zUp != None:
                if zUp == None:
                    zUpList.append(False)
                else:
                    zUpList.append(zUp[1])

                zDown = rs.ShootRay(srf, grip, (0, 0, -1), 1)
                # if zDown != None:
                if zDown == None:
                    zDownList.append(False)
                else:
                    zDownList.append(zDown[1])

            rs.CopyObject(crv)  # Copy Existing curve

            # Find the right list to iterate over and insert existing points for any falses
            if all(x is False for x in zUpList):
                falseindex = [i for i, val in enumerate(zDownList) if not val]
                for i in falseindex:
                    # Replace False with existing grip location and closest Z value
                    closestPt = rs.BrepClosestPoint(srf, grips[i])
                    zDownList[i] = (grips[i].X, grips[i].Y, closestPt[0].Z)
                rs.ObjectGripLocations(crv, zDownList)
            else:
                falseindex = [i for i, val in enumerate(zUpList) if not val]
                for i in falseindex:
                    # Replace False with existing grip location and closest Z value
                    closestPt = rs.BrepClosestPoint(srf, grips[i])
                    zUpList[i] = (grips[i].X, grips[i].Y, closestPt[0].Z)
                rs.ObjectGripLocations(crv, zUpList)

            del zDownList[:]
            del zUpList[:]
            rs.EnableObjectGrips(crv, False)

        if isMesh == True:
            rs.DeleteObject(srf)

        rs.EnableRedraw(True)

    except:
        rs.EnableObjectGrips(crv, False)
        rs.DeleteObject(crv)
        rs.EnableRedraw(True)
        print("Failed to project curves")
        return
Beispiel #5
0
 def rev(Ge, Lis, Src, div):
     udiv = div
     vdiv = div
     Pt_L = Lis[0][0]
     srf = []
     FirstRay = []
     SecondRay = []
     First_RefPoint = []
     drawray = []
     Reverb = []
     for i in Ge:
         srf.append(i[0])
     sph = (rs.AddSphere(Src[1], Src[2]))
     Src_pt = (Src[1])
     u = rs.SurfaceDomain(sph, 0)
     v = rs.SurfaceDomain(sph, 1)
     pts = []
     for i in range(0, udiv + 1, 1):
         for j in range(0, vdiv + 1, 1):
             pt = (i / udiv, j / vdiv, 0)
             sphP = rs.SurfaceParameter(sph, pt)
             newpt = rs.EvaluateSurface(sph, sphP[0], sphP[1])
             pts.append(rs.AddPoint(newpt))
     Dir = []
     for p in pts:
         Dir.append(rs.VectorCreate(p, Src_pt))
     Reflexion = []
     for d in Dir:
         Reflexion.append(rs.ShootRay(srf, Src_pt, d, reflections=4))
     Project = []
     for v in Reflexion:
         Cl_Pt = []
         Ray_v = []
         try:
             Project.append(v[1])
             Ray_v.append(rs.AddPolyline(v))
         except:
             pass
         for u in Ray_v:
             pt_on = rs.CurveClosestPoint(u, Pt_L)
             cl = rs.EvaluateCurve(u, pt_on)
             Dicl = (rs.Distance(Pt_L, cl))
             if Dicl <= ((Lis[0])[3]):
                 try:
                     First_RefPoint = rs.CurveClosestPoint(u, v[1])
                     Second_RefPoint = rs.CurveClosestPoint(u, v[2])
                     endc = ((rs.CurveClosestPoint(
                         u, (rs.CurveEndPoint(u)))))
                     if pt_on > Second_RefPoint:
                         SecondRay.append(pt_on / endc *
                                          (rs.CurveLength(u)))
                         drawray.append(u)
                     elif pt_on > First_RefPoint:
                         FirstRay.append(pt_on / endc *
                                         (rs.CurveLength(u)))
                 except:
                     pass
     box = rs.AddBox(rs.BoundingBox(Project))
     boxarea = round((rs.SurfaceArea(box)[0]), 2)
     Cube = []
     Cube.append(box)
     surfacetorev = []
     for s in srf:
         ptons = []
         for p in Project:
             if rs.Distance((rs.BrepClosestPoint(s, p)[0]), p) < 0.1:
                 ptons.append(p)
         if len(ptons) > 0:
             surfacetorev.append(s)
     surfaceab = []
     for x in Ge:
         if x[0] in surfacetorev:
             surfaceab.append(x[2])
     SrfandAb = [(surfacetorev[i], surfaceab[i])
                 for i in range(0, len(surfacetorev))]
     bbox = box
     box = round(((rs.SurfaceVolume(box))[0]), 1)
     srfvol = []
     srfvolex = []
     absvol = []
     srfarea = []
     srfrev = []
     areaabs = []
     surfacecenter = []
     absidx = []
     absvoltot = []
     for i in SrfandAb:
         if rs.SurfaceVolume(i[0]) > 0:
             srfvol.append(i[0])
             absvol.append(i[1])
         else:
             srfarea.append((rs.SurfaceArea(i[0]))[0])
             absvoltot.append(i[1])
     srfvolex = rs.ExplodePolysurfaces(srfvol)
     for i in srfvolex:
         ptonsrf = []
         usefulsrf = []
         for p in Project:
             if rs.Distance((rs.BrepClosestPoint(i, p)[0]), p) < 0.01:
                 ptonsrf.append(p)
                 usefulsrf.append(i)
         if len(ptonsrf) > 0:
             srfarea.append(rs.SurfaceArea(i)[0])
             srfrev.append(i)
     for i in srfrev:
         surfacecenter.append(rs.SurfaceAreaCentroid(i)[0])
     for b in srfvol:
         for i in surfacecenter:
             if rs.Distance((rs.BrepClosestPoint(b, i)[0]), i) < 0.01:
                 absidx.append(srfvol.index(b))
     for i in absidx:
         absvoltot.append(absvol[i])
     try:
         areaabs = [
             srfarea[i] * (absvoltot[i])
             for i in range(0, len(absvoltot))
         ]
     except:
         raise Exception(
             'One source must be too deep inside a geometry, try to get it out or to move it a little bit !'
         )
     Builtareaabs = 0
     for i in areaabs:
         Builtareaabs += i
     BuiltArea = 0
     for i in srfarea:
         BuiltArea += i
     BuiltArea = round(BuiltArea, 2)
     EmptyArea = 2 * (round(boxarea - BuiltArea, 2))
     if EmptyArea < 0:
         EmptyArea = 0
     TR = 1000 * (0.16 * box) / (Builtareaabs + (EmptyArea * 1))
     FRValue = 0
     for f in FirstRay:
         FV = ((((Lis[0])[3]) * 15) / f)
         FRValue += FV
     if FRValue >= 125:
         FRValue = 125
     SRValue = 0
     for s in SecondRay:
         SV = ((((Lis[0])[3]) * 20) / s)
         SRValue += SV
     if SRValue > 125:
         SRValue = 125
     Reverb.append(round(FRValue))
     Reverb.append(round(SRValue))
     Reverb.append(round(TR, 2))
     return Reverb