Philip-Trettner / GlmSharp

Open-source semi-generated GLM-flavored math library for .NET/C#
MIT License
47 stars 18 forks source link

Instance methods #7

Open ChargeProduction opened 7 years ago

ChargeProduction commented 7 years ago

Sometimes a simple position struct has to be updated. There is actually no need to allocate a new object and let the old one be garbage on the heap. Also, this method of using memory is much much faster than creating a new instance from , for example, two multiplied struct.

I would aprreciate instance methods like:

// Somewhere in vec3.cs

public void Load(vec3 vec){
    this.x = vec.x;
    this.y = vec.y;    
    this.z = vec.z;
}

public void Mult(vec3 vec){
    this.x *= vec.x;
    this.y *= vec.y;    
    this.z *= vec.z;
}

An example how I would use it with matrices:

// In Camera.cs

public void Update(){
    viewMat.Load(dmat4.Identity);
    viewMat.Translate(position);
    viewMat.Mul(rotation.ToMat4);
    ...
}
Philip-Trettner commented 7 years ago

I really dislike this for syntactical reasons but I agree that it has performance benefits.

How about this idea: All those methods are implemented as Extension Methods inside a new namespace (e.g. GlmSharp.PerformanceExtensions). They will not clutter the interface unless you explicitly state using GlmSharp.PerformanceExtensions; in the beginning.

Does that work?

ChargeProduction commented 7 years ago

I`m not sure if its works.

You have to use the this keyword in your extension methods. It will copy the struct so you are not able to edit the fields =/ Thats what I experienced.

It works with the ref keyword. But then Its not an extension. Also you have to make sure you never assign an other struct to theref` parameter before the assigning values. I can explain it in detail if needed.

Philip-Trettner commented 7 years ago

Right that makes sense.

Hm I have to think about it a bit more because I really don't like to clutter the interface unnecessarily.

ChargeProduction commented 7 years ago

I just have tried some approaches. But there is just no way to create an extension with this ref or some kind of other access...