Maths - Open fourum discussion

By: blue2v ( Jonathan Leonard )
file void sfRotation::getTransform(sfvec3f p1)  
2003-12-10 15:41

Why don't you let the definition of this function be this:

public void getTransform( sfvec3f p1 )
{
p1 = getTransform( p1 );
}

By: martinbaker ( Martin Baker )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-11 02:09

Hi blue2v,

The plan is to have Java, C# and C++ versions of the program which are as similar as possible, to make it as easier to keep them in step.

I'm not sure that these languages all support methods which are the same except for the return type? Also since these languages pass parameters by reference, setting the parameter inside the method doesn't have any effect, does it?

Have I understood your point?

Martin

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-12 13:44

Well, I didn't realize at the time I wrote the message that this library was written in 3 languages and that you wanted to keep the codebases identical (as nearly as possible). However, if Java has an equivalent to the C# keyword 'ref', then this is still possible.

define the funcs like this in C#:
public sfvec3f getTransform(sfvec3f p1)
{
// your code here
}

public void transform( ref sfvec3f p1 )
{
p1 = getTransform( p1 );
}

or in C++ as such:

sfvec3f getTransform( sfvec3f p1 )
{
// your code here
}

void transform( sfvec3f& p1 )
{
p1 = getTransform( p1 );
}

I would imagine that since C# is basically a clone of Java, it has a similar mechanism. Also, I would suspect this because it is a _true_ O-O language. Hope this helps. If you find out if this is possible in Java, I'd like to know. Thanks.

Jonathan

P.S. I didn't test this with an actual test case, only made sure that it compiled. I will do more tests and post the results here. It is strange that it compiled because objects *are* passed by ref by default in C#. I'm almost sure this will work in C++ though.

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-12 14:29

Ok, Martin, I did test this in C# and to my surprise, it works!! :-)

You have to use the 'ref' keyword when calling the function that accepts the ref param though. I've posted test code below if you want to try this for yourself. I'm sure this must be able to be done in Java too, but like i said before, I don't know Java all that well.

File #1 (Form1.cs):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
Class1 c = new Class1( 2 );

MyFunc( ref c ); // this calls the second definition below
System.Windows.Forms.MessageBox.Show( c.Val.ToString() ); // this outputs 777 * 2 or 1554

c = new Class1( 2 );
MyFunc( c ); // this calls the first definition below

System.Windows.Forms.MessageBox.Show( c.Val.ToString() ); // this outputs 2
}

private Class1 MyFunc( Class1 cls ) // 1st definition
{
Class1 retVal = new Class1( 777 * cls.Val );
return retVal;
}

private void MyFunc( ref Class1 cls ) // 2nd definition
{
cls = MyFunc( cls );
}
}
}


file #2 (Class1.cs):

using System;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
private int m_MyVal;

public Class1()
{
}

public Class1( int val )
{
m_MyVal = val;
}

public int Val
{
get{ return m_MyVal; }
set{ m_MyVal = value; }
}
}
}


Hope this helps! Jonathan

By: martinbaker ( Martin Baker )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-13 07:39

Jonathan,

I don't believe that java supports ref keyword or '*' or '&' modifiers or any equivalent. As far as I know built in types are always passed by value and classes are always passed by reference (equivalent to passing a pointer to the class, but java does not call it a pointer because you cant do arithmetic on it). I'm not an expert on the finer points of computer languages, but I'm keen to improve so I appreciate your comments.

This sounds restrictive, but I've not found much that I cant do and I find it much easier to debug programs as it reduces the chance that I might stomp over memory or slow things down by passing large class values on the stack.

I thought that I could use the following, its not as elegant as your suggestion, but at least it avoids the duplication of code that I had before.

public sfvec3f getTransform(sfvec3f p1){
sfvec3f value = new sfvec3f(p1); // copy constructor
transform(value);
return value;
}

public void transform(sfvec3f p1){
code to transform p1same as existing method
}

Martin

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-13 11:10

Martin,

That's an excellent compromise (especially given that you have to use Java). If you were sticking to either C++ or C#, my suggestion would work. It does sound restrictive that Java would be so dogmatic on that issue, and I bet that was the inspiration for the 'ref' keyword when C# was created.

Glad I could help.

Jonathan

BTW, what actually led me to find this code was a google search for 'quaternion c#' I think. I am looking for a trackball implementation for C# that will do the 3d rotations you expect in a 3d app. I haven't read all of your code, so do you think it is possible for a clean cut from your project to provide what I'm looking for? I don't know about licensing issues or what not, but maybe you could put some of the general use things in a lib and release under the GPL-lesser.

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-14 19:58

Martin,

About taking some of you code out to put in my project--it doesn't seem like that will be necessary. I have a very near perfect implementation of a virtual trackball. I ported the common SGI port to C-sharp while incorporating a couple of ideas from the VirtualTrackball project I found on codeproject.com. I do have a sizable debugging session in front of me, however (at least it looks that way from the outset) due to the fact that I'm not that familiar with all the 3d mathematics behind this.

If you are interested in this code, I can post it. It takes advantage of the C-sharp delegate system to produce a very coherent trackball. With just two lines of code in the consumer of this class, you can be up and 3d trackballing. (This assumes you're using Direct3D, with openGL, it would take a conversion from the DirectX.Matrix object to the glMatrix or equivalent).

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-14 21:53

Martin,

BTW, I just couldn't let myself use all of your defined data types when the framework (Microsoft.Direct3D) provides all of these to begin with. So, that did make a clean cut from your code seem like more effort than it was worth.

Thanks again for the reference code though. :-)

Jonathan

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-14 22:03

Martin,

OK. I've gotten my module debugged. It makes using a trackball with Direct3D very easy. If you wanted to use it with OpenGL, it would take very few modifications. Like I said before, just a conversion from a Direct3D.Matrix to some equivalent glRotate calls.

I'm going to be posting the code under the 'Patches' section.

Best Regards,
Jonathan

By: martinbaker ( Martin Baker )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-15 01:27

Jonathan,

Yes I would like to add a VirtualTrackball but the code would have to be GNU General Public License, I think that the projects on codeproject.com use a different licence and I don't really want the complication of mixing licences. Thanks for the information though.

Martin

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-15 09:44

Martin,

I wrote this code from scratch. No code was taken from codeproject, only the idea to let the class actually handle the mouse messages (which certainly isn't patentable or unique especially given that I used a system which is only possible in C#, that is the delegate system). The C# class I wrote is even more concise and elegant than the codeproject.com solution. If you are interested, take a look at the Patches section of this website.

Jonathan

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-15 09:46

BTW, both the SGI and the codeproject implementations were in C++. My code is in C#. There shouldn't be any license issues there.

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-15 10:07

From the codeproject.com website (http://www.codeproject.com/info/faq.asp):

Can I use code snippets and programs in my own work?
Yes. Some authors may have specific restrictions on using code in commercial apps such as providing credit in documentation or sending them an email first, but all code can be used for free.

This particular author (Allessandro Falappa) has but no specific restrictions on it. And like I said before, it would be a far stretch for anyone to say that my work is even derived of his. The only resemblance is the existence of OnMouseMove, OnMouseDown, OnMouseUp, OnKeyDown, and OnKeyUp and the contents of these funcs are considerable different. And these functions are automatically registered for using delegates which isn't even possible in MFC.

If you want to compare, here are the two works I used as reference for this class:

http://www.codeproject.com/opengl/virtualtrackball.asp?target=virtual%7Ctrackball

http://www.lifl.fr/~triquet/tools/trackball.cpp

The second one does say to put the Copyright (C) Silicon Graphics on the file, but I actually have a problem yielding the copyright to them. As far as I know, a document can have only one copyright, and there's more original stuff in there than there is adapted code. What are your thoughts on that? There's only two of their funcs in my current version of the code, and they have been translated to C#.

By: blue2v ( Jonathan Leonard )
file RE: void sfRotation::getTransform(sfvec3f p1)  
2003-12-15 10:10

maybe I could say something like:

portions of this code were adapted from Silicon Graphics copyrighted material.

??


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.

 

cover us uk de jp fr ca Quaternions and Rotation Sequences.

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.