Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Do we really need templates to work with primitives?  (Read 1410 times)
0 Members and 1 Guest are viewing this topic.
Offline zingbat

Senior Member




Java games rock!


« Posted 2007-03-05 22:53:56 »

I was thinking about this for an api about Vector i restarted to work on recently. Do we really need templates that would support primitive data types?

It would be nice if we could do this.

public class Tuple<primitive T> {
T x, y, z, w;
...
}

But theres an alternative that may work just as well.

We can enrich the tuple class with type coercion methods:

public interface Tuple
{
int length();
void setLength(int i);
...
int getInt(int i);
int getDouble(int i);
...
void setInt(int i, int val);
void setDouble(int i, double val);
...
void parse(String s)
void parse(String s, int dim)
...
...etc for other primitive types
}

Internally the vector may be implemented in any way. This interface would work no matter what it's implementation was.

Any ideas, sugestions?
 
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2007-03-06 03:58:09 »

ByteBuffer...
 get()
 getInt()
 getLong
 getFloat()
 getDouble()

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 742
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2007-03-06 11:28:26 »

Quote
ByteBuffer...
 get()
 getShort()
 getInt()
 getLong
 getFloat()
 getDouble()

Those methods are not exactly performant.

I usually work around issues like this by writing a little code-generator that spits out the structure I want for all primitives.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2007-03-06 13:35:25 »

Then use the appropriate buffer type.

FloatBuffer
DoubleBuffer
IntBuffer
etc...

Those would be fast enough.

Offline zingbat

Senior Member




Java games rock!


« Reply #4 - Posted 2007-03-06 17:57:47 »

I usually work around issues like this by writing a little code-generator that spits out the structure I want for all primitives.

I did a python script once that would process Java source decorated with special tags to simulate C++ templates and that worked best for performance. The problem however is that it's hard to maintain code this way and integrate it with Eclipse.

I thought about having a hierarchy of Tuples and classes that extend it for all vectors no mater their dimension or representation. So we would have a Tuple interface then other interfaces that extend it like Vector, Point, Quaternion, but unlike javax.vecmath where a Tuple2f is the base class of Vector2f but not a Vector3f or Vector4d in my idea a Tuple would be a base class for every Vector class no what their representation or dimension is.

interface Tuple
{
int getSize();
Tuple setSize(int sz);

Class getDataType(); // the data type used to implement the tupe, ex. Integer.TYPE

<type> get<type>(int i);
<type> get<type>X(); // faster
<type> get<type>Y();
<type> get<type>Z();
<type> get<type>W();

Tuple set(int i, <type> val);
Tuple setX(<type> val); // faster
Tuple setY(<type> val);
Tuple setZ(<type> val);
Tuple setW(<type> val);

boolean equals(Tuple u2);
boolean equals(Tuple u2, float delta); // ignored by integer representations

long hashCode();
String toString();
...
}

Implementing the add operation.

Vector v1 = new FloatVector4(); // FloatVector4 implements Vector interface
Vector v2 = new FloatBufferVector(4); // FloatBufferVector implements Vector interface also
import static LinearAlgebra.*;
Vector v = add(v1,v2);
Point p1 = addVector(v1,v2); // wrong: can't cast vector to point
Point p1 = new FloatPoint4();
Point p = addPoint(p1,v);
Point p = addPoint(v1,v2); // wrong: can only add a vector to a point (not two vectors) to obtain a point

How would the add function be implemented in so that using multiple vector dimensions and representations would not conflict?

My idea is to use type conversions before or after doing calculations:

public class FloatVector4 implements Vector {
float x, y, w, z;
public double getDoubleX() { return x; }
public double getDoubleY() { return y; }
public double getDoubleZ() { return z; }
public double getDoubleW() { return w; }
}

public class LinearAlgebra { // only deals with interfaces
public Vector addVector(Vector v1, Vector v2) {
// since we don't know at this point how v2 is represented we simply use doubles and let their values
// be casted when using get/set methods with any lost of data being ignored
double x1 = v1.getDoubleX();
double y1 = v1.getDoubleY();
double z1 = v1.getDoubleZ();
double w1 = v1.getDoubleW();

double x2 = v2.getDoubleX();
double y2 = v2.getDoubleY();
double z2 = v2.getDoubleZ();
double w2 = v2.getDoubleW();

v1.setX(x1+x2);
v1.setY(y1+z2);
v1.setZ(z1+z2);
v1.setW(w1+w2);

return this;
}
}

The only performance issue is the type casts, but since all Math functions work with double arguments how much would this be a problem?
 
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (11 views)
2014-07-24 01:59:36

Riven (9 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (49 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!