1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.jme3.renderer.queue;
34
35import com.jme3.renderer.Camera;
36import com.jme3.scene.Geometry;
37import com.jme3.util.SortUtil;
38
39/**
40 * This class is a special purpose list of {@link Geometry} objects for render
41 * queuing.
42 *
43 * @author Jack Lindamood
44 * @author Three Rings - better sorting alg.
45 * @author Kirill Vainer
46 */
47public class GeometryList {
48
49    private static final int DEFAULT_SIZE = 32;
50
51    private Geometry[] geometries;
52    private Geometry[] geometries2;
53    private int size;
54    private GeometryComparator comparator;
55
56    /**
57     * Initializes the GeometryList to use the given {@link GeometryComparator}
58     * to use for comparing geometries.
59     *
60     * @param comparator The comparator to use.
61     */
62    public GeometryList(GeometryComparator comparator) {
63        size = 0;
64        geometries = new Geometry[DEFAULT_SIZE];
65        geometries2 = new Geometry[DEFAULT_SIZE];
66        this.comparator = comparator;
67    }
68
69    /**
70     * Set the camera that will be set on the geometry comparators
71     * via {@link GeometryComparator#setCamera(com.jme3.renderer.Camera)}.
72     *
73     * @param cam Camera to use for sorting.
74     */
75    public void setCamera(Camera cam){
76        this.comparator.setCamera(cam);
77    }
78
79    /**
80     * Returns the number of elements in this GeometryList.
81     *
82     * @return Number of elements in the list
83     */
84    public int size(){
85        return size;
86    }
87
88    /**
89     * Returns the element at the given index.
90     *
91     * @param index The index to lookup
92     * @return Geometry at the index
93     */
94    public Geometry get(int index){
95        return geometries[index];
96    }
97
98    /**
99     * Adds a geometry to the list.
100     * List size is doubled if there is no room.
101     *
102     * @param g
103     *            The geometry to add.
104     */
105    public void add(Geometry g) {
106        if (size == geometries.length) {
107            Geometry[] temp = new Geometry[size * 2];
108            System.arraycopy(geometries, 0, temp, 0, size);
109            geometries = temp; // original list replaced by double-size list
110
111            geometries2 = new Geometry[size * 2];
112        }
113        geometries[size++] = g;
114    }
115
116    /**
117     * Resets list size to 0.
118     */
119    public void clear() {
120        for (int i = 0; i < size; i++){
121            geometries[i] = null;
122        }
123
124        size = 0;
125    }
126
127    /**
128     * Sorts the elements in the list according to their Comparator.
129     */
130    public void sort() {
131        if (size > 1) {
132            // sort the spatial list using the comparator
133
134//            SortUtil.qsort(geometries, 0, size, comparator);
135//            Arrays.sort(geometries, 0, size, comparator);
136
137            System.arraycopy(geometries, 0, geometries2, 0, size);
138            SortUtil.msort(geometries2, geometries, 0, size-1, comparator);
139
140
141        }
142    }
143}