NCF参数化建筑论坛

标题: processing 犀牛化的类 [打印本页]

作者: panhao1    时间: 2010-7-8 22:44
标题: processing 犀牛化的类
本帖最后由 panhao1 于 2010-7-8 22:48 编辑 矩阵和3d点 很有用的类 用processing的时候可以作为文件导进去 3d点就是3d Vector 矩阵大家自己去改方法名 class Vector3d { Vector3d() { x = y = z = 0; }; Vector3d( float xx, float yy, float zz ) { x = xx; y = yy; z = zz; }; Vector3d( Vector3d v ) { x = v.x; y = v.y; z = v.z; }; void set( float xx, float yy, float zz ) { x = xx; y = yy; z = zz; }; void set( Vector3d v ) { x = v.x; y = v.y; z = v.z; }; void reset() { x = 0; y = 0; z = 0; }; Vector3d copy() { return new Vector3d( x, y, z ); }; void add( Vector3d v ) { x += v.x; y += v.y; z += v.z; } void sub( Vector3d v ) { x -= v.x; y -= v.y; z -= v.z; } Vector3 subtract( Vector3d v ) { Vector3d tmp = new Vector3d(); tmp.x = x - v.x; tmp.y = y - v.y; tmp.z = z - v.z; return tmp; } void mul( Vector3d v ) { x *= v.x; y *= v.y; z *= v.z; } void div( Vector3d v ) { x /= v.x; y /= v.y; z /= v.z; } // // Scalar // void mul( float s ) { x *= s; y *= s; z *= s; } void div( float s ) { x /= s; y /= s; z /= s; } float dot( Vector3d v ) { return ( x*v.x + y*v.y + z*v.z ); } public Vector3d cross( Vector3d v ) { float crossX = y * v.z - v.y * z; float crossY = z * v.x - v.z * x; float crossZ = x * v.y - v.x * y; return( new Vector3d(crossX, crossY, crossZ) ); } public float length() { return (float)Math.sqrt( (x*x) + (y*y) + (z*z) ); } public float lengthSqr() { return ( (x*x) + (y*y) + (z*z) ); } public float lengthXY() { return (float)Math.sqrt( (x*x) + (y*y) ); } public void normalize() { float m = length(); if (m > 0) { div(m); } } boolean SetCatmullRom( Vector3d V1, Vector3d V2, Vector3d V3, Vector3d V4, float s ) { float ss, sss, a, b, c, d; ss = s * s; sss = s * ss; a = -0.5f * sss + ss - 0.5f * s; b = 1.5f * sss - 2.5f * ss + 1.0f; c = -1.5f * sss + 2.0f * ss + 0.5f * s; d = 0.5f * sss - 0.5f * ss; x = a * V1.x + b * V2.x + c * V3.x + d * V4.x; y = a * V1.y + b * V2.y + c * V3.y + d * V4.y; z = a * V1.z + b * V2.z + c * V3.z + d * V4.z; return true; } boolean SetHermite( Vector3d V1, Vector3d T1, Vector3d V2, Vector3d T2, float s ) { float ss, sss, a, b, c, d; ss = s * s; sss = s * ss; a = 2.0f * sss - 3.0f * ss + 1.0f; b = -2.0f * sss + 3.0f * ss; c = sss - 2.0f * ss + s; d = sss - ss; x = a * V1.x + b * V2.x + c * T1.x + d * T2.x; y = a * V1.y + b * V2.y + c * T1.y + d * T2.y; z = a * V1.z + b * V2.z + c * T1.z + d * T2.z; return true; } boolean SetBaryCentric( Vector3d V1, Vector3d V2, Vector3d V3, float f, float g ) { x = V1.x + f * (V2.x - V1.x) + g * (V3.x - V1.x); y = V1.y + f * (V2.y - V1.y) + g * (V3.y - V1.y); z = V1.z + f * (V2.z - V1.z) + g * (V3.z - V1.z); return true; } Vector3 lerp( Vector3d V1, float s ) { return new Vector3d( x + s * (V1.x - x), y + s * (V1.y - y), z + s * (V1.z - z) ); } Vector3d transform( Matrix m ) { float xx = ( x*m._M[0] + y*m._M[4] + z*m._M[8] + m._M[12] ); float yy = ( x*m._M[1] + y*m._M[5] + z*m._M[9] + m._M[13] ); float zz = ( x*m._M[2] + y*m._M[6] + z*m._M[10] + m._M[14] ); return new Vector3d( xx, yy, zz ); } /********************************************************************** // Static methods **********************************************************************/ public static Vector3d VectorAdd( Vector3d a, Vector3d b ) { return new Vector3d( a.x+b.x, a.y+b.y, a.z+b.z ); } public static Vector3d VectorSub( Vector3d a, Vector3d b ) { return new Vector3d( a.x-b.x, a.y-b.y, a.z-b.z ); } public static Vector3d VectorMul( Vector3d a, Vector3d b ) { return new Vector3d( a.x*b.x, a.y*b.y, a.z*b.z ); } public static float VectorDotProduct( Vector3d v1, Vector3d v2 ) { return ( v1.x*v2.x + v1.y*v2.y + v1.z*v2.z ); } public static Vector3d VectorCrossProduct( Vector3d a, Vector3d b ) { float crossX = a.y * b.z - b.y * a.z; float crossY = a.z * b.x - b.z * a.x; float crossZ = a.x * b.y - b.x * a.y; return( new Vector3d(crossX, crossY, crossZ) ); } public static float DistanceTo( Vector3d v1, Vector3d v2 ) { float dx = v1.x - v2.x; float dy = v1.y - v2.y; float dz = v1.z - v2.z; return (float)Math.sqrt(dx*dx + dy*dy + dz*dz); } public static float Angle2Vec( Vector3d v1, Vector3d v2 ) { float dot = v1.dot( v2 ); float theta = (float) Math.acos(dot / (v1.length() * v2.length())); return theta; } public static VectorZero() { return new Vector3d( 0, 0, 0 ); } float x, y, z; };
作者: panhao1    时间: 2010-7-8 22:51
//import processing.*; /*这个是矩阵 多用于OpenGL,我们大概装逼的话会用到,我建议大家直接用SU脚本,我们需要的功能和OGL能差不多,大家也熟悉*/ import java.nio.*; class Matrix { Matrix() { _M = new float[16]; identity(); }; Matrix( Matrix m ) { copy( m ); }; public void copy( Matrix m ) { _M[0] = m._M[0]; _M[4] = m._M[4]; _M[8] = m._M[8]; _M[12] = m._M[12]; _M[1] = m._M[1]; _M[5] = m._M[5]; _M[9] = m._M[9]; _M[13] = m._M[13]; _M[2] = m._M[2]; _M[6] = m._M[6]; _M[10] = m._M[10]; _M[14] = m._M[14]; _M[3] = m._M[3]; _M[7] = m._M[7]; _M[11] = m._M[11]; _M[15] = m._M[15]; } public FloatBuffer getFloatBuffer() { FloatBuffer fb = FloatBuffer.wrap( _M ); return fb; } public void identity() { _M[0] = 1; _M[4] = 0; _M[8] = 0; _M[12] = 0; _M[1] = 0; _M[5] = 1; _M[9] = 0; _M[13] = 0; _M[2] = 0; _M[6] = 0; _M[10] = 1; _M[14] = 0; _M[3] = 0; _M[7] = 0; _M[11] = 0; _M[15] = 1; } public void scale( float s ) { _M[0] *= s; _M[4] = 0; _M[8] = 0; _M[12] = 0; _M[1] = 0; _M[5] *= s; _M[9] = 0; _M[13] = 0; _M[2] = 0; _M[6] = 0; _M[10] *= s; _M[14] = 0; _M[3] = 0; _M[7] = 0; _M[11] = 0; _M[15] = 1; } Matrix translate( float X, float Y, float Z ) { Matrix m = new Matrix(); m.identity(); m._M[12] = X; m._M[13] = Y; m._M[14] = Z; return m; } void translate2( float X, float Y, float Z ) { _M[12] = X; _M[13] = Y; _M[14] = Z; } public void rotateX( float a ) { //identity(); float ca = (float)Math.cos( a ); float sa = (float)Math.sin( a ); _M[0] = 1; _M[4] = 0; _M[8] = 0; _M[12] = 0; _M[1] = 0; _M[5] = ca; _M[9] = sa; _M[13] = 0; _M[2] = 0; _M[6] = -sa; _M[10] = ca; _M[14] = 0; _M[3] = 0; _M[7] = 0; _M[11] = 0; _M[15] = 1; } public void rotateY( float a ) { //identity(); float ca = (float)Math.cos( a ); float sa = (float)Math.sin( a ); _M[0] = ca; _M[4] = 0; _M[8] = sa; _M[12] = 0; _M[1] = 0; _M[5] = 1; _M[9] = 0; _M[13] = 0; _M[2] = -sa; _M[6] = 0; _M[10] = ca; _M[14] = 0; _M[3] = 0; _M[7] = 0; _M[11] = 0; _M[15] = 1; } public void rotateZ( float a ) { //identity(); float ca = (float)Math.cos( a ); float sa = (float)Math.sin( a ); _M[0] = ca; _M[4] = -sa; _M[8] = 0; _M[12] = 0; _M[1] = sa; _M[5] = ca; _M[9] = 0; _M[13] = 0; _M[2] = 0; _M[6] = 0; _M[10] = 0; _M[14] = 0; _M[3] = 0; _M[7] = 0; _M[11] = 0; _M[15] = 1; } ////////////////////////////////////////////////////////////////////////////////////////////// // compute matrix based on 3 axis angles ////////////////////////////////////////////////////////////////////////////////////////////// Matrix rotate( float Yaw, float Pitch, float Roll ) { Matrix m = new Matrix(); float sinY, cosY, sinP, cosP, sinR, cosR; float ux, uy, uz, vx, vy, vz, nx, ny, nz; sinY = (float)Math.sin(Yaw); cosY = (float)Math.cos(Yaw); sinP = (float)Math.sin(Pitch); cosP = (float)Math.cos(Pitch); sinR = (float)Math.sin(Roll); cosR = (float)Math.cos(Roll); ux = cosY * cosR + sinY * sinP * sinR; uy = sinR * cosP; uz = cosY * sinP * sinR - sinY * cosR; vx = sinY * sinP * cosR - cosY * sinR; vy = cosR * cosP; vz = sinR * sinY + cosR * cosY * sinP; nx = cosP * sinY; ny = -sinP; nz = cosP * cosY; m._M[0] = ux; m._M[1] = uy; m._M[2] = uz; m._M[3] = 0.0f; m._M[4] = vx; m._M[5] = vy; m._M[6] = vz; m._M[7] = 0.0f; m._M[8] = nx; m._M[9] = ny; m._M[10] = nz; m._M[11] = 0.0f; m._M[12] = 0.0f; m._M[13] = 0.0f; m._M[14] = 0.0f; m._M[15] = 1.0f; return m; } public void add( Matrix m ) { _M[0] += m._M[0]; _M[4] += m._M[1]; _M[8] += m._M[2]; _M[12] += m._M[3]; _M[1] += m._M[4]; _M[5] += m._M[5]; _M[9] += m._M[6]; _M[13] += m._M[7]; _M[2] += m._M[8]; _M[6] += m._M[9]; _M[10] += m._M[10]; _M[14] += m._M[11]; _M[3] += m._M[12]; _M[7] += m._M[13]; _M[11] += m._M[14]; _M[15] += m._M[15]; } ////////////////////////////////////////////////////////////////////////////////////////////// // row for column concatenation ////////////////////////////////////////////////////////////////////////////////////////////// Matrix mul( Matrix m ) { Matrix mat = new Matrix(); mat._M[0] = _M[0] * m._M[0] + _M[4] * m._M[1] + _M[8] * m._M[2] + _M[12] * m._M[3]; mat._M[1] = _M[0] * m._M[4] + _M[4] * m._M[5] + _M[8] * m._M[6] + _M[12] * m._M[7]; mat._M[2] = _M[0] * m._M[8] + _M[4] * m._M[9] + _M[8] * m._M[10] + _M[12] * m._M[11]; mat._M[3] = _M[0] * m._M[12] + _M[4] * m._M[13] + _M[8] * m._M[15] + _M[12] * m._M[15]; mat._M[4] = _M[1] * m._M[0] + _M[5] * m._M[1] + _M[9] * m._M[2] + _M[13] * m._M[3]; mat._M[5] = _M[1] * m._M[4] + _M[5] * m._M[5] + _M[9] * m._M[6] + _M[13] * m._M[7]; mat._M[6] = _M[1] * m._M[8] + _M[5] * m._M[9] + _M[9] * m._M[10] + _M[13] * m._M[11]; mat._M[7] = _M[1] * m._M[12] + _M[5] * m._M[13] + _M[9] * m._M[15] + _M[13] * m._M[15]; mat._M[8] = _M[2] * m._M[0] + _M[6] * m._M[1] + _M[10] * m._M[2] + _M[14] * m._M[3]; mat._M[9] = _M[2] * m._M[4] + _M[6] * m._M[5] + _M[10] * m._M[6] + _M[14] * m._M[7]; mat._M[10] = _M[2] * m._M[8] + _M[6] * m._M[9] + _M[10] * m._M[10] + _M[14] * m._M[11]; mat._M[11] = _M[2] * m._M[12] + _M[6] * m._M[13] + _M[10] * m._M[15] + _M[14] * m._M[15]; mat._M[12] = _M[3] * m._M[0] + _M[7] * m._M[1] + _M[11] * m._M[2] + _M[15] * m._M[3]; mat._M[13] = _M[3] * m._M[4] + _M[7] * m._M[5] + _M[11] * m._M[6] + _M[15] * m._M[7]; mat._M[14] = _M[3] * m._M[8] + _M[7] * m._M[9] + _M[11] * m._M[10] + _M[15] * m._M[11]; mat._M[15] = _M[3] * m._M[12] + _M[7] * m._M[13] + _M[11] * m._M[15] + _M[15] * m._M[15]; /* mat._M[0] = _M[0] * m._M[0] + _M[4] * m._M[1] + _M[8] * m._M[2] + _M[12] * m._M[3]; mat._M[4] = _M[0] * m._M[4] + _M[4] * m._M[5] + _M[8] * m._M[6] + _M[12] * m._M[7]; mat._M[8] = _M[0] * m._M[8] + _M[4] * m._M[9] + _M[8] * m._M[10] + _M[12] * m._M[11]; mat._M[12] = _M[0] * m._M[12] + _M[4] * m._M[13] + _M[8] * m._M[15] + _M[12] * m._M[15]; mat._M[1] = _M[1] * m._M[0] + _M[5] * m._M[1] + _M[9] * m._M[2] + _M[13] * m._M[3]; mat._M[5] = _M[1] * m._M[4] + _M[5] * m._M[5] + _M[9] * m._M[6] + _M[13] * m._M[7]; mat._M[9] = _M[1] * m._M[8] + _M[5] * m._M[9] + _M[9] * m._M[10] + _M[13] * m._M[11]; mat._M[13] = _M[1] * m._M[12] + _M[5] * m._M[13] + _M[9] * m._M[15] + _M[13] * m._M[15]; mat._M[2] = _M[2] * m._M[0] + _M[6] * m._M[1] + _M[10] * m._M[2] + _M[14] * m._M[3]; mat._M[6] = _M[2] * m._M[4] + _M[6] * m._M[5] + _M[10] * m._M[6] + _M[14] * m._M[7]; mat._M[10] = _M[2] * m._M[8] + _M[6] * m._M[9] + _M[10] * m._M[10] + _M[14] * m._M[11]; mat._M[14] = _M[2] * m._M[12] + _M[6] * m._M[13] + _M[10] * m._M[15] + _M[14] * m._M[15]; mat._M[3] = _M[3] * m._M[0] + _M[7] * m._M[1] + _M[11] * m._M[2] + _M[15] * m._M[3]; mat._M[7] = _M[3] * m._M[4] + _M[7] * m._M[5] + _M[11] * m._M[6] + _M[15] * m._M[7]; mat._M[11] = _M[3] * m._M[8] + _M[7] * m._M[9] + _M[11] * m._M[10] + _M[15] * m._M[11]; mat._M[15] = _M[3] * m._M[12] + _M[7] * m._M[13] + _M[11] * m._M[15] + _M[15] * m._M[15]; */ return mat; } ////////////////////////////////////////////////////////////////////////////////////////////// // column for row concatenation ////////////////////////////////////////////////////////////////////////////////////////////// Matrix mul2( Matrix m ) { Matrix mat = new Matrix(); mat._M[0] = _M[0] * m._M[0] + _M[1] * m._M[4] + _M[2] * m._M[8] + _M[3] * m._M[12]; mat._M[1] = _M[0] * m._M[1] + _M[1] * m._M[5] + _M[2] * m._M[9] + _M[3] * m._M[13]; mat._M[2] = _M[0] * m._M[2] + _M[1] * m._M[6] + _M[2] * m._M[10] + _M[3] * m._M[14]; mat._M[3] = _M[0] * m._M[3] + _M[1] * m._M[7] + _M[2] * m._M[11] + _M[3] * m._M[15]; mat._M[4] = _M[4] * m._M[0] + _M[5] * m._M[4] + _M[6] * m._M[8] + _M[7] * m._M[12]; mat._M[5] = _M[4] * m._M[1] + _M[5] * m._M[5] + _M[6] * m._M[9] + _M[7] * m._M[13]; mat._M[6] = _M[4] * m._M[2] + _M[5] * m._M[6] + _M[6] * m._M[10] + _M[7] * m._M[14]; mat._M[7] = _M[4] * m._M[3] + _M[5] * m._M[7] + _M[6] * m._M[11] + _M[7] * m._M[15]; mat._M[8 ] = _M[8] * m._M[0] + _M[9] * m._M[4] + _M[10] * m._M[8] + _M[11] * m._M[12]; mat._M[9 ] = _M[8] * m._M[1] + _M[9] * m._M[5] + _M[10] * m._M[9] + _M[11] * m._M[13]; mat._M[10] = _M[8] * m._M[2] + _M[9] * m._M[6] + _M[10] * m._M[10] + _M[11] * m._M[14]; mat._M[11] = _M[8] * m._M[3] + _M[9] * m._M[7] + _M[10] * m._M[11] + _M[11] * m._M[15]; mat._M[12] = _M[12] * m._M[0] + _M[13] * m._M[4] + _M[14] * m._M[8] + _M[15] * m._M[12]; mat._M[13] = _M[12] * m._M[1] + _M[13] * m._M[5] + _M[14] * m._M[9] + _M[15] * m._M[13]; mat._M[14] = _M[12] * m._M[2] + _M[13] * m._M[6] + _M[14] * m._M[10] + _M[15] * m._M[14]; mat._M[15] = _M[12] * m._M[3] + _M[13] * m._M[7] + _M[14] * m._M[11] + _M[15] * m._M[15]; return mat; } public void mul( float s ) { _M[0] *= s; _M[4] *= s; _M[8] *= s; _M[12] *= s; _M[1] *= s; _M[5] *= s; _M[9] *= s; _M[13] *= s; _M[2] *= s; _M[6] *= s; _M[10] *= s; _M[14] *= s; _M[3] *= s; _M[7] *= s; _M[11] *= s; _M[15] *= s; } public void transpose() { Matrix m = new Matrix(); m.copy( this ); _M[0] = m._M[0]; _M[4] = m._M[1]; _M[8] = m._M[2]; _M[12] = m._M[3]; _M[1] = m._M[4]; _M[5] = m._M[5]; _M[9] = m._M[6]; _M[13] = m._M[7]; _M[2] = m._M[8]; _M[6] = m._M[9]; _M[10] = m._M[10]; _M[14] = m._M[11]; _M[3] = m._M[12]; _M[7] = m._M[13]; _M[11] = m._M[14]; _M[15] = m._M[15]; } public Vector3 transform( Vector3 v ) { float xx = ( v.x*_M[0] + v.y*_M[4] + v.z*_M[8] + _M[12] ); float yy = ( v.x*_M[1] + v.y*_M[5] + v.z*_M[9] + _M[13] ); float zz = ( v.x*_M[2] + v.y*_M[6] + v.z*_M[10] + _M[14] ); return new Vector3( xx, yy, zz ); } /* public void debug( PApplet p ) { p.println( _M[ 0] + ", " + _M[ 1] + ", " + _M[ 2] + ", " + _M[ 3] ); p.println( _M[ 4] + ", " + _M[ 5] + ", " + _M[ 6] + ", " + _M[ 7] ); p.println( _M[ 8] + ", " + _M[ 9] + ", " + _M[10] + ", " + _M[11] ); p.println( _M[12] + ", " + _M[13] + ", " + _M[14] + ", " + _M[15] ); }*/ public static Vector3 transform( Vector3 v, float[] m ) { float xx = ( v.x*m[0] + v.y*m[4] + v.z*m[8] + m[12] ); float yy = ( v.x*m[1] + v.y*m[5] + v.z*m[9] + m[13] ); float zz = ( v.x*m[2] + v.y*m[6] + v.z*m[10] + m[14] ); return new Vector3( xx, yy, zz ); } float[] _M; };
作者: 董羽天    时间: 2010-7-9 17:05
学习了 O(∩_∩)O谢谢
作者: wonderful    时间: 2010-7-10 11:11
上面的程序真是复杂啊,是人写的吗?
作者: divedragon    时间: 2010-7-10 13:08
谢谢楼主分享 学习了
作者: f(x)    时间: 2010-7-10 19:28
潘浩兄弟,你发的东西相信只有你一个可以看懂,表示压力很大。
作者: panhao1    时间: 2010-7-10 23:39
4# wonderful 这还是基本的 用的比较多的一个类 这是我找的比较全的一个范本 建筑学的写程序必用的 相当于rs里面的vector静态方法 加上 gh里面的On3dPoint 和 OndVector 或是common 里面的 point3d 或vector3d 我一般写程序都会比这长几十倍的样子 大的代码会接近万行
作者: dongjuanhome    时间: 2010-7-11 10:17
写错一点岂不是很难查错吗,辛苦啦
作者: kay__lc    时间: 2010-7-11 14:42
这个这个,貌似很复杂
作者: wonderful    时间: 2010-9-2 13:01
学习中,感谢
作者: seifer0201    时间: 2010-9-2 19:20
这个是要导进library里的吧
作者: ygm    时间: 2010-9-6 10:46
强 看的我头疼
作者: Lemmonade    时间: 2010-9-15 15:20
好长的说,但是尽量学习中
作者: qujing    时间: 2010-9-18 19:11
哈哈,还没这个能力写啊,羡慕中
作者: lliiuushuai    时间: 2010-10-22 10:38
好复杂阿。。。
作者: suzhou    时间: 2010-11-12 10:05
public Vector3 transform( Vector3 v )这个句子有错误!

登录/注册后可看大图

作者: xielanmin    时间: 2011-3-17 09:55
天书 收藏着 以后肯定用得上~~~
作者: darklight    时间: 2011-3-18 22:41
学习啦。。。。。
作者: archizool    时间: 2011-3-20 22:36
panhao果然猛
作者: Raymark    时间: 2011-3-20 23:24
不懂,,, 只能说发这个帖子的人很猛。。。
作者: ja126    时间: 2011-8-31 02:10
感謝 先拿回去實驗了
作者: leo48    时间: 2011-9-12 19:22
菜鸟的我还是看不懂这些,慢慢来吧
作者: 熊熊    时间: 2011-11-30 12:27
这个还真是长啊…………努力学习中
作者: sheep2086    时间: 2011-12-26 10:55
复杂化or简化?
作者: thinksong    时间: 2011-12-28 13:55
我下载了,就是一直没有用,不怎么会,
作者: s.k.    时间: 2012-1-13 14:21
神奇代码,表示看不懂,潘老大神人啊,代码天才
作者: kzseL    时间: 2012-1-20 11:02
强势学习中~楼主~我新入学的processing~能教教我该怎么学么?
作者: 木叶苍蓝猛兽    时间: 2012-2-3 21:18
我勒个去 这么长的代码。。。。先收着。。
作者: porther    时间: 2012-4-22 14:22
直接调用的写好的类啊,不错
作者: bxsqrym    时间: 2012-4-25 11:38
掌握四个软件,而且是高难度的。真不容易
作者: 熊猫无敌    时间: 2013-3-10 20:45
发上来的编码好乱的




欢迎光临 NCF参数化建筑论坛 (http://www.ncf-china.com/) Powered by Discuz! X3.2