getType() on Trimesh

Hi.

Debugger reports imported model as an instance of Trimesh, but asking getType() returns '6', not '4' as source states.

Model is built in C4D, exported as 3DS and then converted with Jme converter.

Trace or search for other usage of getType() to see how to properly use that method. Or just use instanceof…

@d_e_m:

the type value contains binary flags. so 6 would be 4 + 2 (that is TRIMESH + GEOMETRY). the test condition for geometry would be something like:


model.getType() & GEOMETRY != 0



or

model.getType() & GEOMETRY == GEOMETRY



for example here's the implementation of getType() for TerrainBlock:


public int getType() {
        return (SceneElement.GEOMETRY | SceneElement.TRIMESH | SceneElement.TERRAIN_BLOCK);
    }

have a vague recollection that getType maybe deprecated soon in favour of instanceof

yes, there was a discussion about that some time ago. you'll just have to search the board.

please not the difference between && and &



edit: I think braces are needed, too

sorry. i was in a rush :slight_smile:

theprism said:

have a vague recollection that getType maybe deprecated soon in favour of instanceof

Yes, instanceof is really much better than fiddling around with bits :P - and I still claim that it's faster

braces aren't needed unless you mean the braces of the if statement :stuck_out_tongue:

sfera said:

braces aren't needed unless you mean the braces of the if statement :P

that's not right: operator precedence

So

model.getType() & GEOMETRY == GEOMETRY

would evaluate

GEOMETRY == GEOMETRY

first then

model.getType() & true

which is rubbish :P

XD

Concerning the speed of the two methods, I would say it really depends on the access pattern more than on sheer speed. That is, if you use instanceof a lot more than the bit mask, it will end up being faster (because of cache issues), while if you only use it a few times, and not very often, I would predict the bit masking would be faster.



Any way, using the type as a masked field gives you absolute information about the actual type, while instanceof is only telling you if it is what you ask it.  :wink: There is no absolute better, so pick yours  :stuck_out_tongue:

duenez said:

Any way, using the type as a masked field gives you absolute information about the actual type, while instanceof is only telling you if it is what you ask it.


i see that exactly the other way around :)

I always thought any reflection calls are slow? I also read somewhere that instanceof calls are cheap, but down casting (which usually follows) is slow. Anyhow the profiler will be helpful determining if instanceof will be changed to getType() somewhere far in the future… for now I'm refactoring to the instanceof calls.

Just a thought, I haven't see bitwise comparison since early 1999 when I worked with C… some nice memories are emerging… :smiley:

Completely forgot about that construct… :smiley:

Instanceof is not a reflection call, it's an operator. Profiling with a profiler won't help much since the overhead of the profiler is more than the actual call (wether you use instanceof or getType()). Which leaves you with running things in a loop and timing them… but I think irrisor already did that showing that instanceof is a bit faster (due to method invokation overhead is my guess).

There is a test against the two here



http://www.jmonkeyengine.com/jmeforum/index.php?topic=4627.msg36707#msg36707

I think we should deprecate it and remove it for 1.0, shouldn't we?

definitely

In one computer: Gentoo, AMD64, 2GB RAM, sun-jdk-1.5.10

Total Instance Time 24137

Total Type Time 23414

:evil:



So it might be actually better in 1.6, but I don't have it available in this computer.

Be a man and compile from the sources.  :stuck_out_tongue:



I thought all you Gentoo people were "compile from the source" super-freaks? :wink: