Created
September 18, 2013 14:54
-
-
Save yasushisakai/6610330 to your computer and use it in GitHub Desktop.
adding the reflection method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import rhinoscriptsyntax as rs | |
import Rhino.Geometry.Vector3d as vec3d | |
def VectorReflection(_vector,_normal): | |
''' | |
反射ベクトルを求める | |
''' | |
n = rs.VectorUnitize(_normal) | |
dot = rs.VectorDotProduct(n,_vector) | |
return -2 * dot * n +_vector | |
def MeshReflection(_mesh,_line): | |
intersections = rs.CurveMeshIntersection(_line,_mesh,True) | |
if intersections is None: return None | |
sp = rs.CurveStartPoint(_line) | |
dist = 99999999999999.99 | |
for i in intersections: | |
tempDist = rs.Distance(sp,i[0]) | |
if tempDist < dist : | |
dist = tempDist | |
nearest = i | |
meshNormals = rs.MeshFaceNormals(_mesh) | |
mn = meshNormals[nearest[1]] | |
vec = rs.CurveEndPoint(_line) - sp | |
return nearest[0],VectorReflection (vec,mn) | |
def MeshBounce(_mesh,_line,_length=None): | |
mr = MeshReflection(_mesh,line) | |
if mr is None : return None | |
sp = mr[0] | |
if _length is None : | |
ep = sp + mr[1] | |
else: | |
d = rs.VectorScale(rs.VectorUnitize(mr[1]),_length) | |
ep = sp + d | |
return rs.AddLine(sp,ep) | |
if __name__ == '__main__': | |
line = rs.GetObject('line') | |
mesh = rs.GetObject('mesh') | |
bounce = MeshBounce(mesh,line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment