| Path: | cross.rb |
| Last Update: | Fri May 09 22:26:50 +0200 2008 |
# File cross.rb, line 198 def angleBetween(v1,v2) # return the angle between 2 vectors # cos(rad) = x scalarproduct y / |v1|*|v2| begin return toDeg(Math::acos(scalarProduct(v1,v2)/vectLength(v1)/vectLength(v2))) rescue print "(fail: angle " print "v1=#{vectLength(v1)}, v2=#{vectLength(v2)}) " return 180.0 end end
# File cross.rb, line 120 def angleVector(vec1,ang) ang = ang*Math::PI/180.0 # rotation kring z-axeln return [ vec1[0]*Math::cos(ang)-vec1[1]*Math::sin(ang),vec1[0]*Math::sin(ang)+vec1[1]*Math::cos(ang),0.0] end
# File cross.rb, line 113 def angleVectorOld(vec1,ang) ang = ang*Math::PI/180.0 # fan det blir bara 2-d nu... return [ Math::sin(Math::acos(vec1[1])+ang.to_f),Math::cos(Math::acos(vec1[1])+ang.to_f),0.0] # a2 = sin(asin(a)+v) # b2=cos(acos(b)+v) end
from cartesian coordinate system to cylindrical coordinate system
# File cross.rb, line 54 def cart2cyl(x,y,z) begin x == 0 ? x = 1e-99:nil r = Math::sqrt(x**2 + y**2) #fi = Math::atan(y/x) fi = solidAtan(x,y) return r, fi, z rescue raise RuntimeError, "Error while executing cart2cyl" return 0.0, 0.0, 0.0 end end
from cartesian coordinate system to spherical coordinate system
# File cross.rb, line 25 def cart2spherical(x,y,z) #begin x == 0 ? x = 1e-99:nil r = Math::sqrt(x**2 + y**2 + z**2) #fi = Math::atan(y/x) fi = solidAtan(x,y) # according to BETA theta = Math::acos(z/Math::sqrt(x**2+y**2+z**2)) theta = Math::asin(z/Math::sqrt(x**2+y**2+z**2)) # puts "fi #{fi}, theta #{theta}" return r, fi, toDeg(theta) #rescue # raise RuntimeError, "Error while executing cart2spherical" # return 0, 0, 0 #end end
# File cross.rb, line 84 def crossNormal(v1,v2) cross=crossProduct(v1,v2) length=vectLength(cross) return [cross[0]/length,cross[1]/length,cross[2]/length] end
# File cross.rb, line 77 def crossProduct(v1,v2) if v1.size == 3 && v2.size ==3 return [v1[1].to_f*v2[2].to_f-v1[2].to_f*v2[1].to_f,-v1[0].to_f*v2[2].to_f+v1[2].to_f*v2[0].to_f,v1[0].to_f*v2[1].to_f-v1[1].to_f*v2[0].to_f] else puts "wrong size for cross product" end end
# File cross.rb, line 109 def crossSpecial(vec1,vec2,v) l=[vec2[0]-vec1[0],vec2[1]-vec1[1],vec2[2]-vec1[2]] return crossNormal(l,v) end
from cylindrical coordinate system to cartesian coordinate system
# File cross.rb, line 67 def cyl2cart(r,fi,z) begin x = r*Math::cos(toRad(fi)) y = r*Math::sin(toRad(fi)) return x, y, z rescue raise RuntimeError, "Error while executing cyl2cart" return 0.0, 0.0, 0.0 end end
# File cross.rb, line 89 def faceNormal(tmpface) p0x = tmpface.p0.pos[0][0] p0y = tmpface.p0.pos[0][1] p0z = tmpface.p0.pos[0][2] p1x = tmpface.p1.pos[0][0] p1y = tmpface.p1.pos[0][1] p1z = tmpface.p1.pos[0][2] p3x = tmpface.p3.pos[0][0] p3y = tmpface.p3.pos[0][1] p3z = tmpface.p3.pos[0][2] v0 = [ p1x - p0x, p1y - p0y, p1z - p0z ] v1 = [ p3x - p0x, p3y - p0y, p3z - p0z ] puts "p0x #{p0x}, p0y #{p0y}, p0z #{p0z}}" puts "p1x #{p1x}, p1y #{p1y}, p1z #{p1z}}" puts "p3x #{p3x}, p3y #{p3y}, p3z #{p3z}}" return crossNormal( v0, v1) end
find crossing of two lines starting at point pX with direction dX
# File cross.rb, line 254 def findCrossing(p1, d1, p2, d2) # first find out if they are parallell @p1, @d1, @p2, @d2 = p1, d1, p2, d2 # solve in z-plane # from Wxmaxima: p1 + s1*d1 = p2 + s2*d2 # s1=-(dx2*py2-dx2*py1+dy2*(px1-px2))/(dx1*dy2-dx2*dy1), # s2=-(dx1*py2-dx1*py1+dy1*(px1-px2))/(dx1*dy2-dx2*dy1) @s1= -(@d2[0]*@p2[1]-@d2[0]*@p1[1]+@d2[1]*(@p1[0]-@p2[0]))/(@d1[0]*@d2[1]-@d2[0]*@d1[1]) @s2= -(@d1[0]*@p2[1]-@d1[0]*@p1[1]+@d1[1]*(@p1[0]-@p2[0]))/(@d1[0]*@d2[1]-@d2[0]*@d1[1]) return [ @p1[0] + @s1*@d1[0], @p1[1] + @s1*@d1[1], @p1[2] + @s1*@d1[2] ] end
test if normalized vectors are parallell
# File cross.rb, line 236 def isParallell(v1, v2) @v1 = normalizeVector(v1) @v2 = normalizeVector(v2) if @v1[0] == @v2[0] && @v1[1] == @v2[1] && @v1[2] == @v2[2] return true elsif (@v1[0]*0.9 < @v2[0] && @v1[0]*1.1 > @v2[0]) && (@v1[1]*0.9 < @v2[1] && @v1[1]*1.1 > @v2[1]) && (@v1[2]*0.9 < @v2[2] && @v1[2]*1.1 > @v2[2]) return true else return false end end
# File cross.rb, line 209 def linearSpace(x1,y1,z1,x2,y2,z2,meshnum) if meshnum < 2 meshnum = 2 end # distance between points xrange = x2 -x1 yrange = y2 -y1 zrange = z2 -z1 # fill vectors with distances xpoints = Vector.elements( (0..(meshnum-1)).to_a )*(xrange.to_f/(meshnum-1).to_f) ypoints = Vector.elements( (0..(meshnum-1)).to_a )*(yrange.to_f/(meshnum-1).to_f) zpoints = Vector.elements( (0..(meshnum-1)).to_a )*(zrange.to_f/(meshnum-1).to_f) # adjust position of vectors so it starts at point @p0 ones = Array.new (0..meshnum-1).each do |i| ones[i] = 1.0 end x1pos = Vector.elements(ones)* x1.to_f y1pos = Vector.elements(ones)* y1.to_f z1pos = Vector.elements(ones)* z1.to_f xpoints += x1pos ypoints += y1pos zpoints += z1pos return xpoints, ypoints, zpoints end
normalize vector
# File cross.rb, line 248 def normalizeVector(v) @v = v @vlen = vectLength(@v) return [ @v[0]/@vlen, @v[1]/@vlen, @v[2]/@vlen ] end
# File cross.rb, line 144 def rotateX(p,ang,v) # rotate v around a z-axis located at p mov = Vector.elements(p) rot = Vector.elements(v) - mov rot = Matrix[[1.0,0.0,0.0], [0.0,Math::cos(toRad(ang)), Math::sin(toRad(ang))], [0.0,-Math::sin(toRad(ang)),Math::cos(toRad(ang))]]*rot return rot + mov end
# File cross.rb, line 153 def rotateXinv(p,ang,v) # rotate v around a z-axis located at p mov = Vector.elements(p) rot = Vector.elements(v) - mov rot = Matrix[[1.0,0.0,0.0], [0.0,Math::cos(toRad(ang)), -Math::sin(toRad(ang))], [0.0,Math::sin(toRad(ang)),Math::cos(toRad(ang))]]*rot return rot + mov end
# File cross.rb, line 162 def rotateY(p,ang,v) # rotate v around a z-axis located at p mov = Vector.elements(p) rot = Vector.elements(v) - mov rot = Matrix[[Math::cos(toRad(ang)),0.0, -Math::sin(toRad(ang))], [0.0,1.0,0.0], [Math::sin(toRad(ang)),0.0,Math::cos(toRad(ang))]]*rot return rot + mov end
# File cross.rb, line 171 def rotateYinv(p,ang,v) # rotate v around a z-axis located at p mov = Vector.elements(p) rot = Vector.elements(v) - mov rot = Matrix[[Math::cos(toRad(ang)),0.0, Math::sin(toRad(ang))], [0.0,1.0,0.0], [-Math::sin(toRad(ang)),0.0,Math::cos(toRad(ang))]]*rot return rot + mov end
# File cross.rb, line 180 def rotateZ(p,ang,v) # rotate v around a z-axis located at p mov = Vector.elements(p) rot = Vector.elements(v) - mov rot = Matrix[[Math::cos(toRad(ang)), Math::sin(toRad(ang)),0.0], [-Math::sin(toRad(ang)),Math::cos(toRad(ang)),0.0], [0.0,0.0,1.0]]*rot return rot + mov end
# File cross.rb, line 189 def rotateZinv(p,ang,v) # rotate v around a z-axis located at p mov = Vector.elements(p) rot = Vector.elements(v) - mov rot = Matrix[[Math::cos(toRad(ang)), -Math::sin(toRad(ang)),0.0], [Math::sin(toRad(ang)),Math::cos(toRad(ang)),0.0], [0.0,0.0,1.0]]*rot return rot + mov end
# File cross.rb, line 125 def scalarProd(vec1,vec2) return vec1[0].to_f*vec2[0].to_f+vec1[1].to_f*vec2[1].to_f+vec1[2].to_f*vec2[2].to_f end
# File cross.rb, line 128 def scalarProduct(vec1,vec2) return vec1[0].to_f*vec2[0].to_f+vec1[1].to_f*vec2[1].to_f+vec1[2].to_f*vec2[2].to_f end
from spherical coordinate system to cartesian coordinate system
# File cross.rb, line 41 def spherical2cart(r, fi, theta) begin # It's not obvious which definition that is right here. x = r*Math::cos(toRad(theta))*Math::cos(toRad(fi)) y = r*Math::cos(toRad(theta))*Math::sin(toRad(fi)) z = r*Math::sin(toRad(theta)) return x, y, z rescue raise RuntimeError, "Error while executing spherical2cart" return 0.0, 0.0, 0.0 end end
# File cross.rb, line 106 def vectLength(v) return Math::sqrt(v[0].to_f**2+v[1]**2+v[2].to_f**2) end