Maths - 3d multivector Code - C#

 related classes

/*Title:      mjbWorld
Copyright (c) 1998-2003 Martin John BakerThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.For information about the GNU General Public License see http://www.gnu.org/To discuss this program http://sourceforge.net/forum/forum.php?forum_id=122133 also see website https://www.euclideanspace.com/ */namespace mjbModel { using System; using System.ComponentModel; using System.IO; using System.Collections; using System.Reflection; using System.Text; using System.Data; using System.Data.SqlClient; using System.Xml; /// <summary> /// a class to represent a 3d multivector (clifford / geometric algebra) as described here: /// https://www.euclideanspace.com/maths/algebra/clifford/index.htm /// </summary> class sfmulti3d : property {/// <summary> /// scalar /// </summary> public double e; /// <summary> /// vector ex = e1,ey = e2 ,ez = e3 /// </summary> public double ex,ey,ez; /// <summary> /// bivector exy = e1 ^ e2 ,ezx = e3 ^ e1 ,eyz = e2 ^ e3 /// </summary> public double exy,ezx,eyz; /// <summary> /// trivector exyz = e1 ^ e2 ^ e3 /// </summary> public double exyz;/// <summary> /// null constructor /// creates class with all zero elements /// </summary> public sfmulti3d() { }/// <summary> /// copy constructor /// creates class with elements set to same value as other class /// </summary> /// <param name="other">other class to copy</param> public sfmulti3d(sfmulti3d other) { // copy constructor e = other.e; ex = other.ex; ey = other.ey; ez = other.ez; exy = other.exy; ezx = other.ezx; eyz = other.eyz; exyz = other.exyz; }/// <summary> /// generates an array /// </summary> /// <returns>array containing equivelant matrix</returns> public double[] toMatrix(){ // under construction double[] matrix = new double[16]; try { matrix[0] = 0; matrix[1] = 0; matrix[2] = 0; matrix[3] = 0; matrix[4] = 0; matrix[5] = 0; matrix[6] = 0; matrix[7] = 0; matrix[8] = 0; matrix[9] = 0; matrix[10] = 0; matrix[11] = 0; matrix[12] = 0; matrix[13] = 0; matrix[14] = 0; matrix[15] = 0; } catch (Exception e1) { Console.WriteLine("sfmulti3d.instring {0}",e1); } return matrix; }/// <summary> /// returns a string containing the name of this type /// cant use static method below when we need to override property.vrmlType() method /// </summary> /// <returns>name of this type "sfmulti3d"</returns> public override String vrmlType(){ return "sfmulti3d"; }/// <summary> /// a static method to get a string containing the name of this type /// </summary> /// <returns>name of this type "sfmulti3d"</returns> public static String vrmlType_s(){ return "sfmulti3d"; }/// <summary> /// override clone method /// </summary> /// <returns>clone of this class</returns> public override property clone() { return new sfmulti3d(this); }/// <summary> /// add other multivector to this one /// other is not altered /// for theory see: /// https://www.euclideanspace.com/maths/algebra/clifford/arithmetic/index.htm /// </summary> /// <param name="other">the multivector to be added to this, other is not altered</param> public void add(sfmulti3d other) { e += other.e; ex += other.ex; ey += other.ey; ez += other.ez; exy += other.exy; ezx += other.ezx; eyz += other.eyz; exyz += other.exyz; }/// <summary> /// subtract other multivector from this one /// other is not altered /// for theory see: /// https://www.euclideanspace.com/maths/algebra/clifford/arithmetic/index.htm /// </summary> /// <param name="other">the multivector to be subtracted from this, other is not altered</param> public void sub(sfmulti3d other) { e -= other.e; ex -= other.ex; ey -= other.ey; ez -= other.ez; exy -= other.exy; ezx -= other.ezx; eyz -= other.eyz; exyz -= other.exyz; }/// <summary> /// multiply another multivector with this one /// other is not altered /// for theory see: /// https://www.euclideanspace.com/maths/algebra/clifford/arithmetic/index.htm /// </summary> /// <param name="other">the multivector to be multiplied by this, other is not altered</param> public void mult(sfmulti3d other) { sfmulti3d tmp = new sfmulti3d(this); mult(tmp,other); }/// <summary> /// set this multivector to a * b /// a and b are not altered /// for theory see: /// https://www.euclideanspace.com/maths/algebra/clifford/arithmetic/index.htm /// </summary> /// <param name="a">the first multivector to be multiplied, it is not altered</param> /// <param name="b">the second multivector to be multiplied, it is not altered</param> public void mult(sfmulti3d a,sfmulti3d b) { e = a.e * b.e + a.ex * b.ex + a.ey * b.ey + a.ez * b.ez - a.exy * b.exy - a.ezx * b.ezx - a.eyz * b.eyz - a.exyz * b.exyz; ex = a.ex * b.e + a.e * b.ex - a.exy * b.ey + a.ezx * b.ez + a.ey * b.exy - a.ez * b.ezx - a.exyz * b.eyz - a.eyz * b.exyz; ey = a.ey * b.e + a.exy * b.ex + a.e * b.ey - a.eyz * b.ez - a.ex * b.exy - a.exyz * b.ezx + a.ez * b.eyz - a.ezx * b.exyz; ez = a.ez * b.e - a.ezx * b.ex + a.eyz * b.ey + a.e * b.ez - a.exyz * b.exy + a.ex * b.ezx - a.ey * b.eyz - a.exy * b.exyz; exy = a.exy * b.e + a.ey * b.ex - a.ex * b.ey + a.exyz * b.ez + a.e * b.exy + a.eyz * b.ezx - a.ezx * b.eyz + a.ez * b.exyz; ezx = a.ezx * b.e - a.ez * b.ex + a.exyz * b.ey + a.ex * b.ez - a.eyz * b.exy + a.e * b.ezx + a.exy * b.eyz + a.ey * b.exyz; eyz = a.eyz * b.e + a.exyz * b.ex + a.ez * b.ey - a.ey * b.ez + a.ezx * b.exy - a.exy * b.ezx + a.e * b.eyz + a.ex * b.exyz; exyz =a.exyz * b.e + a.eyz * b.ex + a.ezx * b.ey + a.exy * b.ez + a.ez * b.exy + a.ey * b.ezx + a.ex * b.eyz + a.e * b.exyz; }/// <summary> /// calculate the multiplative inverse (1/a) /// under construction /// </summary> public void invert() { // under construction }/// <summary> /// transform a vector /// </summary> /// <param name="v">a vector which is transformed by this multivector</param> public void transform(sfvec3f v){ // under construction }/// <summary> /// used for generation of source code /// </summary> /// <param name="f">filter holds read and write utilities</param> /// <param name="mode">0=procedure defn 1=procedure call</param> /// <param name="maxInstances">how many instances of this class</param> public void writeJava(filter f,int mode,int maxInstances){ try { // String name = generateUniqueName(); if (mode != 0) return; // no procedure defn required f.status(GetType().Name); f.writeln("// code for "+GetType().Name,0); f.writeln("double []d = {",2); f.writeln(""+e+",",2); f.writeln(""+ex+","+ey+","+ez+",",2); f.writeln(""+exy+","+ezx+","+eyz+",",2); f.writeln(""+exyz,2); f.writeln("};",2); } catch (Exception e1) { Console.WriteLine("sfmulti3d.writeJava {0}",e1); } return; }/// <summary> /// output as a string /// </summary> /// <param name="mode">mode mode values /// 0 - output modified values /// 1 - output original values /// 2 - output attribute /// 3 - output attribute in brackets /// 4 - output with f prefix</param> /// <returns>string representing this class</returns> public override String outstring(int mode) { return "\n"+e+"\n"+ ex+","+ey+","+ez+","+"\n"+ exy+","+ezx+","+eyz+","+"\n"+ exyz; }/// <summary> /// write to file /// </summary> /// <param name="f">filter = information about output</param> /// <param name="mode">mode values /// 0 - output VRML97 modified values /// 1 - output VRML97 original values /// 2 - output xml (x3d) /// 3 - output attribute in brackets /// 4 - output with f prefix</param> /// <param name="indent">indent indent applied to new lines</param> public override void write(filter f,int mode,int indent){ f.write(outstring(mode)); }/// <summary> /// used by mfparam.vrml2par /// </summary> /// <param name="f"></param> /// <param name="sfp"></param> /// <param name="n"></param> /// <param name="mode"></param> /// <returns></returns> public override bool instring(filter f,sfparam sfp,nodeBean n,int mode) { return false; }/// <summary> /// get a class that can edit this /// </summary> /// <returns></returns> public static Type getEditClass(){ //return typeof(sfmulti3dEditor); return null; }} } //namespace mjbModel

metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

flag flag flag flag flag flag Clifford Algebra to Geometric Calculus: A Unified Language for Mathematics and Physics (Fundamental Theories of Physics). This book is intended for mathematicians and physicists rather than programmers, it is very theoretical. It covers the algebra and calculus of multivectors of any dimension and is not specific to 3D modelling.

 

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.