マイペースなプログラミング日記

DTMやプログラミングにお熱なd-kamiがマイペースに書くブログ

mqoを読み込んでみる その2

mqoファイルを読み込んでみて、とりあえずObject要素のvertexとface読み込めればいいのかなと思い、まずはデータを格納するクラスを作成した。まずは読み込んだテキストを1行ずつ取り出せるクラス作成。あれ?Listでいいような...と思ったが...まぁいいや

package net.d_kami.mqo;

public class MQOLines{
    private String[] lines;
    private int current;
    
    public MQOLines(String[] lines){
        this.lines = lines;
        current = 0;
    }
    
    public String next(){
        if(current >= lines.length){
            return null;
        }
        
        return lines[current++];
    }
    
    public int current(){
        return current;
    }
}

次はvertex要素を格納するクラス。これは構造体じゃないか

package net.d_kami.mqo;

public class MQOVertex{
    public final float x;
    public final float y;
    public final float z;
    
    public MQOVertex(float x, float y, float z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

次にface要素を格納するクラス。faceは3つもしくは4つのindexを持ってるので、どちらにも対応できるようにしておく

package net.d_kami.mqo;

public class MQOFace{
    private int[] indices;
    private int next;
    
    public MQOFace(int index0, int index1, int index2){
        indices = new int[3];
        indices[0] = index0;
        indices[1] = index1;
        indices[2] = index2;
    }

    public MQOFace(int index0, int index1, int index2, int index3){
        indices = new int[4];
        indices[0] = index0;
        indices[1] = index1;
        indices[2] = index2;
        indices[3] = index3;
    }

    public int getIndex(int index){
        return indices[index];
    }
    
    public int getSize(){
        return indices.length;
    }
}

次はObject要素だが、vertexとfaceだけ持たせておく

package net.d_kami.mqo;

import java.util.List;
import java.util.ArrayList;

public class MQOObject{
    private List<MQOVertex> vertices;
    private List<MQOFace> faces;
    
    public MQOObject(){
        vertices = new ArrayList<MQOVertex>();
        faces = new ArrayList<MQOFace>();
    }
    
    public void addVertex(float x, float y, float z){
        vertices.add(new MQOVertex(x, y, z));
    }
    
    public void addFace(String[] elements){
        if(elements.length == 3){
            addFace(Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), Integer.parseInt(elements[2]));
        }else if(elements.length == 4){
            addFace(Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), Integer.parseInt(elements[2]), Integer.parseInt(elements[3]));
        }
    }
    
    public void addFace(int index0, int index1, int index2){
        faces.add(new MQOFace(index0, index1, index2));
    }
    
    public void addFace(int index0, int index1, int index2, int index3){
        faces.add(new MQOFace(index0, index1, index2, index3));
    }
    
    public MQOVertex getVertex(int index){
        return vertices.get(index);
    }
    
    public MQOFace getFace(int index){
        return faces.get(index);
    }
    
    public List<MQOVertex> getVertices(){
        return vertices;
    }
    
    public List<MQOFace> getFaces(){
        return faces;
    }
}

最後にObject要素の集合

package net.d_kami.mqo;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class MQOContainer implements Iterable<MQOObject>{
    private List<MQOObject> objects;
    
    public MQOContainer(){
        objects = new ArrayList<MQOObject>();
    }
    
    public void addObject(MQOObject object){
        objects.add(object);
    }
    
    public Iterator<MQOObject> iterator(){
        return objects.iterator();
    }
}

次のブログのエントリにはパーサを載せたい