Exception: Comparison method violates its general contract

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Exception: Comparison method violates its general contract

runiter
During picking I get the following exception in some of my 3D models:

java.lang.IllegalArgumentException: Comparison method violates its general contract!
        at java.util.TimSort.mergeHi(TimSort.java:899)
        at java.util.TimSort.mergeAt(TimSort.java:516)
        at java.util.TimSort.mergeForceCollapse(TimSort.java:457)
        at java.util.TimSort.sort(TimSort.java:254)
        at java.util.Arrays.sort(Arrays.java:1512)
        at java.util.ArrayList.sort(ArrayList.java:1454)
        at java.util.Collections.sort(Collections.java:175)
        at com.ardor3d.intersection.PickResults.getPickData(PickResults.java:73)

I tracked it to the following file:

https://github.com/gouessej/Ardor3D/blob/master/ardor3d-core/src/main/java/com/ardor3d/intersection/PickResults.java

In particular the following code:

    private static class DistanceComparator implements Comparator<PickData> {
        @Override
        public int compare(final PickData o1, final PickData o2) {
            if (o1.getIntersectionRecord().getClosestDistance() <= o2.getIntersectionRecord().getClosestDistance()) {
                return -1;
            }
            return 1;
        }
    }

Shouldn't the above code return zero when o1 and o2 distances are equal?
Saeid Nourian, Ph.D. Eng. | Graphing Calculator 3D
Reply | Threaded
Open this post in threaded view
|

Re: Exception: Comparison method violates its general contract

gouessej
Administrator
Hi

Yes, you're right. Maybe I need to take into account an epsilon but your suggestion is fine to me. I can fix this bug and produce a new build before the end of the week.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Exception: Comparison method violates its general contract

runiter
Great thanks, I shall wait for the new build.

I don't think you need to take into account epsilon in this particular case. I think a simple == might be more proper in a compare method. It's probably okay [and in fact desirable] that in most situations the two distances are not exactly equal.
Saeid Nourian, Ph.D. Eng. | Graphing Calculator 3D
Reply | Threaded
Open this post in threaded view
|

Re: Exception: Comparison method violates its general contract

gouessej
Administrator
I've updated the source code. There is no new build yet because I have to test another bug fix (for another bug) very soon.
Julien Gouesse | Personal blog | Website