{"id":1478,"date":"2012-12-11T14:44:04","date_gmt":"2012-12-11T14:44:04","guid":{"rendered":"http:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/?page_id=1478"},"modified":"2012-12-19T01:01:06","modified_gmt":"2012-12-19T01:01:06","slug":"code-5-add-title","status":"publish","type":"page","link":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/code\/code-5-add-title\/","title":{"rendered":"EarthClouds"},"content":{"rendered":"<p><strong>MAIN CODE\u00a0<\/strong><\/p>\n<p>import rhinoscriptsyntax as rs<\/p>\n<p>import math<\/p>\n<p>class waveSource():<\/p>\n<p>def __init__(self, GUID, YVALUES):<\/p>\n<p><a href=\"http:\/\/self.id\">self.id<\/a> = GUID<\/p>\n<p>self.waveValues = YVALUES<\/p>\n<p>self.alpha = 0<\/p>\n<p>def explicitWaveVec(self,pt):<\/p>\n<p>if rs.IsPoint(<a href=\"http:\/\/self.id\">self.id<\/a>):<\/p>\n<p>self.pos = rs.PointCoordinates(<a href=\"http:\/\/self.id\">self.id<\/a>)<\/p>\n<p>else:<\/p>\n<p>t = rs.CurveClosestPoint(<a href=\"http:\/\/self.id\">self.id<\/a>,pt)<\/p>\n<p>self.pos = rs.EvaluateCurve(<a href=\"http:\/\/self.id\">self.id<\/a>,t)<\/p>\n<p>dist = rs.Distance(self.pos, pt)<\/p>\n<p>sine = self.waveValues[int(dist)]<\/p>\n<p>vec = rs.VectorCreate(pt,self.pos)<\/p>\n<p>vec = rs.VectorUnitize(vec)<\/p>\n<p>vec = rs.VectorScale(vec, sine)<\/p>\n<p>return vec<\/p>\n<p>def updateExplicit(self):<\/p>\n<p>#temp = self.waveValues[0]<\/p>\n<p>self.waveValues.pop(0)<\/p>\n<p>#print len(self.waveValues)<\/p>\n<p>class medium():<\/p>\n<p>def __init__(self,GUID, FRICTION):<\/p>\n<p><a href=\"http:\/\/self.id\">self.id<\/a> = GUID<\/p>\n<p>self.friction = FRICTION<\/p>\n<p>self.pts = rs.PointCloudPoints(<a href=\"http:\/\/self.id\">self.id<\/a>)<\/p>\n<p>def update(self,waveSources):<\/p>\n<p>newPts = []<\/p>\n<p>for pt in self.pts:<\/p>\n<p>sumVec = [0,0,0]<\/p>\n<p>for myWaveSource in waveSources:<\/p>\n<p>sumVec = rs.PointAdd(sumVec,myWaveSource.explicitWaveVec(pt))<\/p>\n<p>sumVec = rs.VectorScale(sumVec,1-self.friction)<\/p>\n<p>newPts.append(rs.PointAdd(pt,sumVec))<\/p>\n<p>#rs.DeleteObject(<a href=\"http:\/\/self.id\">self.id<\/a>)<\/p>\n<p><a href=\"http:\/\/self.id\">self.id<\/a> = rs.AddPointCloud(newPts)<\/p>\n<p>rs.AddLayer(&#8220;MyLayer####&#8221;)<\/p>\n<p>rs.ObjectLayer(<a href=\"http:\/\/self.id\">self.id<\/a>,&#8221;MyLayer####&#8221;)<\/p>\n<p>self.pts = newPts<\/p>\n<p>&nbsp;<\/p>\n<p>def Main():<\/p>\n<p>waveCrv = rs.GetObject(&#8220;select the curve for the curve in x-y plane&#8221;, rs.filter.curve)<\/p>\n<p>yValues = []<\/p>\n<p>rs.EnableRedraw(False)<\/p>\n<p>for i in range(500):<\/p>\n<p>temp = rs.AddLine([i,-100,0],[i,100,0])<\/p>\n<p>ccx = rs.CurveCurveIntersection(waveCrv, temp)<\/p>\n<p>if ccx:<\/p>\n<p>yValues.append(ccx[0][1][1])<\/p>\n<p>else:<\/p>\n<p>yValues.append(0)<\/p>\n<p>rs.DeleteObject(temp)<\/p>\n<p>sources = rs.GetObjects(&#8220;select the sources&#8221;, rs.filter.point+rs.filter.curve)<\/p>\n<p>ptClouds = rs.GetObjects(&#8220;select the mediums&#8221;, rs.filter.pointcloud)<\/p>\n<p>myWaveSources = []<\/p>\n<p>for source in sources:<\/p>\n<p>myWaveSources.append(waveSource(source, yValues))<\/p>\n<p>myMediums = []<\/p>\n<p>for ptCloud in ptClouds:<\/p>\n<p>name = rs.ObjectName(ptCloud)<\/p>\n<p>if name:<\/p>\n<p>myMediums.append( medium(ptCloud, float(name)) )<\/p>\n<p>else:<\/p>\n<p>myMediums.append( medium(ptCloud, 0) )<\/p>\n<p>for i in range(20):<\/p>\n<p>for myMedium in myMediums:<\/p>\n<p>myMedium.update(myWaveSources)<\/p>\n<p>for myWaveSource in myWaveSources:<\/p>\n<p>myWaveSource.updateExplicit()<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Main()<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>COLOR CODE<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>import rhinoscriptsyntax as rs<\/p>\n<p>import random<\/p>\n<p>&nbsp;<\/p>\n<p>class robot():<\/p>\n<p>def __init__(self, PATH, RADIOUS, TOTALITERATIONS):<\/p>\n<p>self.path = PATH<\/p>\n<p>self.radious = RADIOUS<\/p>\n<p>self.steps = TOTALITERATIONS<\/p>\n<p>self.pathPts = rs.DivideCurve(self.path, self.steps+1)<\/p>\n<p>self.pos = self.pathPts[0]<\/p>\n<p>self.folder = rs.BrowseForFolder()<\/p>\n<p>self.averageDistances = []<\/p>\n<p>def updateCamera(self, currentI):<\/p>\n<p>view = rs.CurrentView()<\/p>\n<p>rs.ViewCameraTarget( view, self.pathPts[currentI], self.pathPts[currentI+1] )<\/p>\n<p>self.pos = self.pathPts[currentI]<\/p>\n<p>def saveFrame(self, currentI):<\/p>\n<p>if currentI &lt; 100 and currentI &gt; 10:<\/p>\n<p>result = rs.CreatePreviewImage(self.folder + &#8220;<a href=\"file:\/\/\/\\\\robotView0\">\\\\robotView0<\/a>&#8221; + str(currentI) + &#8220;.jpg&#8221;)<\/p>\n<p>elif currentI&lt;10:<\/p>\n<p>result = rs.CreatePreviewImage(self.folder + &#8220;<a href=\"file:\/\/\/\\\\robotView00\">\\\\robotView00<\/a>&#8221; + str(currentI) + &#8220;.jpg&#8221;)<\/p>\n<p>else:<\/p>\n<p>result = rs.CreatePreviewImage(self.folder + &#8220;<a href=\"file:\/\/\/\\\\robotView\">\\\\robotView<\/a>&#8221; + str(currentI) + &#8220;.jpg&#8221;)<\/p>\n<p>def look(self, myEarthclouds):<\/p>\n<p>sumDistances = 0<\/p>\n<p>count = 0<\/p>\n<p>for myEarthcloud in myEarthclouds:<\/p>\n<p>dist = rs.Distance(self.pos, myEarthcloud.pos)<\/p>\n<p>if dist &lt;self.radious:<\/p>\n<p>#rs.ShowObject(myEarthcloud.id)<\/p>\n<p>currentDistance= myEarthcloud.colorByDistance2Robot(self)<\/p>\n<p>count = count +1<\/p>\n<p>else:<\/p>\n<p>rs.HideObject(myEarthcloud.id)<\/p>\n<p>rs.HideObjects(myEarthcloud.ptids)<\/p>\n<p>currentDistance= 0<\/p>\n<p>sumDistances = sumDistances + currentDistance<\/p>\n<p>if count &gt; 0 :<\/p>\n<p>avDistances = sumDistances\/count<\/p>\n<p>else:<\/p>\n<p>avDistances = 0<\/p>\n<p>self.averageDistances.append(avDistances)<\/p>\n<p>&nbsp;<\/p>\n<p>class earthcloud():<\/p>\n<p>def __init__(self, PTCLOUDGUID, POINTNEIGHBORHOODRADIOUS):<\/p>\n<p><a href=\"http:\/\/self.id\">self.id<\/a> = PTCLOUDGUID<\/p>\n<p>self.radious = POINTNEIGHBORHOODRADIOUS<\/p>\n<p>self.friction = random.random()<\/p>\n<p>self.pos = [0,0,0]<\/p>\n<p>self.pts = rs.PointCloudPoints(<a href=\"http:\/\/self.id\">self.id<\/a>)<\/p>\n<p>for pt in self.pts:<\/p>\n<p>self.pos = rs.PointAdd(self.pos, pt)<\/p>\n<p>self.pos = [self.pos[0]\/len(self.pts),self.pos[1]\/len(self.pts),self.pos[2]\/len(self.pts)]<\/p>\n<p>rs.HideObject(<a href=\"http:\/\/self.id\">self.id<\/a>)<\/p>\n<p>self.ptids = []<\/p>\n<p>for pt in self.pts:<\/p>\n<p>self.ptids.append(rs.AddPointCloud([pt]))<\/p>\n<p>rs.HideObject(self.ptids[-1])<\/p>\n<p>def colorByDistance2Robot(self, robot):<\/p>\n<p>min = 5000000000000<\/p>\n<p>max = 0<\/p>\n<p>allDist = []<\/p>\n<p>sumDistances = 0<\/p>\n<p>count = 0<\/p>\n<p>for i in range(len(self.pts)):<\/p>\n<p>pt = self.pts[i]<\/p>\n<p>dist = rs.Distance(pt, robot.pos)<\/p>\n<p>if dist&lt;min : min = dist<\/p>\n<p>if dist&gt;max : max = dist<\/p>\n<p>allDist.append(dist)<\/p>\n<p>if dist&lt;robot.radious:<\/p>\n<p>sumDistances = sumDistances + dist<\/p>\n<p>count +=1<\/p>\n<p>if count&gt;0:<\/p>\n<p>sumDistances = sumDistances\/count<\/p>\n<p>for i in range(len(self.pts)):<\/p>\n<p>#pt = self.pts[i]<\/p>\n<p>ptid = self.ptids[i]<\/p>\n<p>normalizedDistance = 1-(allDist[i]-min)\/(max-min)<\/p>\n<p>rs.ShowObject(self.ptids[i])<\/p>\n<p>rs.ObjectColor(self.ptids[i], [255*normalizedDistance,0,255-255*normalizedDistance])<\/p>\n<p>return rs.Distance(robot.pos, self.pos)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>def Main():<\/p>\n<p>crv = rs.GetObject(&#8220;select the path for the robot&#8221;, rs.filter.curve)<\/p>\n<p>radious = rs.GetReal(&#8220;plase type the radious of the robot&#8221;, 5)<\/p>\n<p>frameCount = rs.GetInteger(&#8220;please type the amount of frames&#8221;, 200)<\/p>\n<p>myRobot = robot(crv, radious, frameCount)<\/p>\n<p>ptClouds = rs.GetObjects(&#8220;select a bunch of point clouds&#8221;, rs.filter.pointcloud)<\/p>\n<p>myEarthclouds = []<\/p>\n<p>for ptCloud in ptClouds:<\/p>\n<p>myEarthclouds.append(earthcloud(ptCloud, 0.7))<\/p>\n<p>rs.HideObject(myRobot.path)<\/p>\n<p>for i in range(frameCount):<\/p>\n<p>myRobot.look(myEarthclouds)<\/p>\n<p>myRobot.updateCamera(i)<\/p>\n<p>myRobot.saveFrame(i)<\/p>\n<p>for myEarthcloud in myEarthclouds:<\/p>\n<p>rs.ShowObject(myEarthcloud.id)<\/p>\n<p>rs.DeleteObjects(myEarthcloud.ptids)<\/p>\n<p>&nbsp;<\/p>\n<p>pts4Distances = []<\/p>\n<p>for i in range(len(myRobot.averageDistances)):<\/p>\n<p>distanceValue = myRobot.averageDistances[i]<\/p>\n<p>pts4Distances.append([i,distanceValue,0])<\/p>\n<p>p = rs.AddPolyline(pts4Distances)<\/p>\n<p>rs.ObjectColor(p,[255,0,0])<\/p>\n<p>rs.ShowObject(myRobot.path)<\/p>\n<p>Main()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MAIN CODE\u00a0 import rhinoscriptsyntax as rs import math class waveSource(): def __init__(self, GUID, YVALUES): self.id = GUID self.waveValues = YVALUES self.alpha = 0 def explicitWaveVec(self,pt): if rs.IsPoint(self.id): self.pos = rs.PointCoordinates(self.id) else: t = rs.CurveClosestPoint(self.id,pt) self.pos = rs.EvaluateCurve(self.id,t) dist = rs.Distance(self.pos, pt) sine = self.waveValues[int(dist)] vec = rs.VectorCreate(pt,self.pos) vec = rs.VectorUnitize(vec) vec = rs.VectorScale(vec, sine) return [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":9,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1478","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/pages\/1478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/comments?post=1478"}],"version-history":[{"count":7,"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/pages\/1478\/revisions"}],"predecessor-version":[{"id":1734,"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/pages\/1478\/revisions\/1734"}],"up":[{"embeddable":true,"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/pages\/9"}],"wp:attachment":[{"href":"https:\/\/www.new-territories.com\/blog\/ncertainties\/col12\/wp-json\/wp\/v2\/media?parent=1478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}