/*
* Copyright (C) 1998 Ralf Wiebicke
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package de.rw7;
import java.io.IOException;
import java.io.InputStream;
import java.io.StreamTokenizer;
class Line
{
int A, B;
Line(int A, int B)
{
this.A = A;
this.B = B;
}
//public boolean equals(Object o){Line l=(Line)o;return((l.A==A)&&(l.B==B));}
boolean validate(int n)
{
if ((0 <= A) && (A < n) && (0 <= B) && (B < n))
return (true);
System.out.println("Line out of Bounds");
return (false);
}
} // Line
class Surf
{
int P[];
Surf(int A, int B, int C)
{
P = new int[3];
P[0] = A;
P[1] = B;
P[2] = C;
}
Surf(int A, int B, int C, int D)
{
P = new int[4];
P[0] = A;
P[1] = B;
P[2] = C;
P[3] = D;
}
Surf(int P[], int nP)
{
this.P = new int[nP];
for (int i = 0; i < nP; i++)
this.P[i] = P[i];
}
//public boolean equals(Object o){Surf s=(Surf)o;return(P.equals(s.P));}
boolean validate(int n)
{
for (int i = 0; i < P.length; i++)
if ((0 > P[i]) || (P[i] >= n))
{
System.out.println("Surf out of Bounds");
return (false);
};
return (true);
}
} // Surf
/**
Kapselung des Vektor-Objekts fuer Monitor3D
immutable
*/
public class Model
{
private static Spot PyramideSpots[] =
{
new Spot(-80, -80, -80),
new Spot(+80, -80, -80),
new Spot(+80, -80, +80),
new Spot(-80, -80, +80),
new Spot(-80, 0, -80),
new Spot(+80, 0, -80),
new Spot(+80, 0, +80),
new Spot(-80, 0, +80),
new Spot(0, 90, 0),
};
private static Surf PyramideSurfs[] =
{
new Surf(0, 1, 5, 4),
new Surf(1, 2, 6, 5),
new Surf(2, 3, 7, 6),
new Surf(3, 0, 4, 7),
new Surf(4, 5, 8),
new Surf(5, 6, 8),
new Surf(6, 7, 8),
new Surf(7, 4, 8),
new Surf(3, 2, 1, 0),
};
Spot Spots[] = null;
Line Lines[] = null;
Surf Surfs[] = null;
Model() // Pyramide
{
Spots = PyramideSpots;
Surfs = PyramideSurfs;
computeLinesFromSurfs();
optimize();
}
Model(Model m) // Lines berechnen
{
Spots = m.Spots;
Surfs = m.Surfs;
computeLinesFromSurfs();
optimize();
}
Model(InputStream is) throws java.io.IOException // Datei importieren
{
Spots = new Spot[5000];
int NSpot = 0;
Lines = new Line[5000];
int NLine = 0;
Surfs = new Surf[5000];
int NSurf = 0;
int P[] = new int[40];
int nP;
System.out.println("Start loading...");
StreamTokenizer st = new StreamTokenizer(is);
st.eolIsSignificant(true);
st.commentChar('#');
scan : while (true)
{
switch (st.nextToken())
{
default :
break scan;
case StreamTokenizer.TT_EOL :
break;
case StreamTokenizer.TT_WORD :
if ("v".equals(st.sval))
{
double x = 0, y = 0, z = 0;
if (st.nextToken() == StreamTokenizer.TT_NUMBER)
{
x = st.nval;
if (st.nextToken() == StreamTokenizer.TT_NUMBER)
{
y = st.nval;
if (st.nextToken() == StreamTokenizer.TT_NUMBER)
z = st.nval;
}
}
Spots[NSpot++] = new Spot((float) x, (float) y, (float) z);
while (st.ttype != StreamTokenizer.TT_EOL
&& st.ttype != StreamTokenizer.TT_EOF)
st.nextToken();
}
else if ("l".equals(st.sval))
{
int start = -1;
int prev = -1;
int n = -1;
while (true)
if (st.nextToken() == StreamTokenizer.TT_NUMBER)
{
n = (int) st.nval;
if (prev >= 0)
Lines[NLine++] = new Line(prev - 1, n - 1);
if (start < 0)
start = n;
prev = n;
}
else if (st.ttype == '/')
st.nextToken();
else
break;
if (start >= 0)
Lines[NLine++] = new Line(start - 1, prev - 1);
if (st.ttype != StreamTokenizer.TT_EOL)
break scan;
}
else if ("f".equals(st.sval) || "fo".equals(st.sval))
{
nP = 0;
while (true)
if (st.nextToken() == StreamTokenizer.TT_NUMBER)
P[nP++] = (int) (st.nval - 1);
else if (st.ttype == '/')
st.nextToken();
else
break;
Surfs[NSurf++] = new Surf(P, nP);
if (st.ttype != StreamTokenizer.TT_EOL)
break scan;
}
else
{
while (st.nextToken() != StreamTokenizer.TT_EOL
&& st.ttype != StreamTokenizer.TT_EOF);
}
}
}
is.close();
if (st.ttype != StreamTokenizer.TT_EOF)
throw new IOException(st.toString());
optimize();
}
private void computeLinesFromSurfs()
{
int X[];
Lines = new Line[50 * Surfs.length];
int NLine = 0;
for (int i = 0; i < Surfs.length; i++)
{
X = Surfs[i].P;
for (int j = 0; j < X.length; j++)
Lines[NLine++] = new Line(X[j], X[(j + 1) % X.length]);
};
}
private void optimizeSpots()
{
System.out.print("S");
System.out.flush();
int i;
for (i = Spots.length - 1; i >= 0; i--)
if (Spots[i] != null)
break;
i++;
if (i != Spots.length)
{
Spot B[] = new Spot[i];
for (int j = 0; j < i; j++)
B[j] = Spots[j];
Spots = B;
};
System.out.print("s");
System.out.flush();
}
private void optimizeLines()
{
System.out.print("L");
System.out.flush();
int Max = Spots.length;
for (int i = 0; i < Lines.length; i++)
if ((Lines[i] != null) && (!Lines[i].validate(Max)))
Lines[i] = null;
int NObject = 0;
for (int i = 0; i < Lines.length; i++)
if (Lines[i] != null)
{
for (int j = 0; j < i; j++)
if ((Lines[j] != null) && (Lines[i].equals(Lines[j])))
{
Lines[i] = null;
break;
};
if (Lines[i] != null)
NObject++;
}
Line B[] = new Line[NObject];
int j = 0;
for (int i = 0; i < Lines.length; i++)
if (Lines[i] != null)
B[j++] = Lines[i];
if (NObject != j)
System.out.println("Model3D.optimize");
Lines = B;
System.out.print("l");
System.out.flush();
}
private void optimizeSurfs()
{
System.out.print("F");
System.out.flush();
int Max = Spots.length;
for (int i = 0; i < Surfs.length; i++)
if ((Surfs[i] != null) && (!Surfs[i].validate(Max)))
Surfs[i] = null;
int NObject = 0;
for (int i = 0; i < Surfs.length; i++)
if (Surfs[i] != null)
{
for (int j = 0; j < i; j++)
if ((Surfs[j] != null) && (Surfs[i].equals(Surfs[j])))
{
Surfs[i] = null;
break;
};
if (Surfs[i] != null)
NObject++;
}
Surf B[] = new Surf[NObject];
int j = 0;
for (int i = 0; i < Surfs.length; i++)
if (Surfs[i] != null)
B[j++] = Surfs[i];
if (NObject != j)
System.out.println("Model3D.optimize");
Surfs = B;
System.out.print("f");
System.out.flush();
}
private void optimize()
{
System.out.print("Optimierung...");
System.out.flush();
optimizeSpots();
optimizeLines();
optimizeSurfs();
System.out.println("ready");
}
} // class Model