Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.finalspace.engine.geometry;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- public class Mesh {
- private List<Vertex> vertices;
- private List<Face> faces;
- private List<Integer> indices;
- public Mesh() {
- vertices = new ArrayList<Vertex>();
- faces = new ArrayList<Face>();
- indices = new ArrayList<Integer>();
- }
- private class VertexIndex {
- private final Vertex vertex;
- private final Integer index;
- public VertexIndex(final Vertex vertex, final Integer index) {
- this.vertex = vertex;
- this.index = index;
- }
- public Vertex getVertex() {
- return vertex;
- }
- public Integer getIndex() {
- return index;
- }
- @Override
- public String toString() {
- return index.toString() + " - " + vertex.toString();
- }
- }
- private final int searchVertexIndexCacheCapacity = 100;
- private VertexIndex[] searchVertexIndexCache = new VertexIndex[searchVertexIndexCacheCapacity];
- private int searchVertexIndexCacheCurrentIndex = 0;
- private void writeToCache(final Vertex v, final int idx) {
- searchVertexIndexCacheCurrentIndex++;
- if (searchVertexIndexCacheCurrentIndex < searchVertexIndexCacheCapacity) {
- // We have free cache slots free, add it to it
- } else {
- // Cache is full, restart it
- searchVertexIndexCacheCurrentIndex = 1;
- }
- searchVertexIndexCache[searchVertexIndexCacheCurrentIndex - 1] = new VertexIndex(
- v, idx);
- }
- private int getVertexIndex(final Vertex v) {
- // // First look into the cache
- // for (int i = 0; i < searchVertexIndexCacheCapacity; i++) {
- // if (searchVertexIndexCache[i] != null) {
- // if (searchVertexIndexCache[i].getVertex().equals(v)) {
- // return searchVertexIndexCache[i].getIndex();
- // }
- // }
- // }
- // Not found, we have to search in our vertices list
- int i = 0;
- for (Vertex x : vertices) {
- if (x.equals(v)) {
- // writeToCache(x, i);
- return i;
- }
- i++;
- }
- // Not found
- return -1;
- }
- public int addVertex(final Vertex v, boolean ignoreSearch) {
- // Find duplicated vertex and returns index of it
- int idx = !ignoreSearch ? getVertexIndex(v) : -1;
- if (idx == -1) {
- int newindex = vertices.size();
- // writeToCache(v, newindex);
- vertices.add(v);
- return newindex;
- } else {
- return idx;
- }
- }
- public void addFace(final Face face) {
- faces.add(face);
- }
- public void addIndex(final Integer index) {
- indices.add(index);
- }
- public Collection<Vertex> getVertices() {
- return vertices;
- }
- public Collection<Face> getFaces() {
- return faces;
- }
- public Collection<Integer> getIndices() {
- return indices;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement