00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __FRZ_OBJ3D_HPP__
00025 #define __FRZ_OBJ3D_HPP__
00026
00027 #include <iostream>
00028 #include <vector>
00029 #include <cmath>
00030 #include <cstdlib>
00031
00032 #include <stdint.h>
00033
00034 namespace Frz {
00035
00044 class Object3d {
00045 public:
00048 struct Vertex {
00050 float x;
00052 float y;
00054 float z;
00055
00062 Vertex(float _x=0.f, float _y=0.f, float _z=0.f) : x(_x), y(_y), z(_z) {}
00063
00070 Vertex operator- (const Vertex &a) const {
00071 return Vertex(x-a.x, y-a.y, z-a.z);
00072 }
00073
00080 Vertex operator* (const Vertex &b) const {
00081 return Vertex(y*b.z-z*b.y, z*b.x-x*b.z, x*b.y-y*b.x);
00082 }
00083
00090 Vertex operator* (const float a) const {
00091 return Vertex(x*a, y*a, z*a);
00092 }
00093
00101 bool operator== (const Vertex &a) const {
00102 const double precision = 1e-5f;
00103 bool ret = fabs((a.x - x)) < precision &&
00104 fabs((a.y - y)) < precision &&
00105 fabs((a.z - z)) < precision;
00106
00107 return ret;
00108 }
00109
00114 void write(std::ostream &out) {
00115 out.write((char*)&x, 12);
00116 }
00117
00122 void read(std::istream &in) {
00123 in.read((char*)&x, 12);
00124 }
00125
00133 friend std::ostream &operator<< (std::ostream &out, const Vertex &v) {
00134 return out << v.x << " " << v.y << " " << v.z;
00135 }
00136
00144 friend std::istream &operator>> (std::istream &in, Vertex &v) {
00145 char buf[80], *p;
00146 in >> buf;
00147 p = buf;
00148 v.x = strtod(p, &p); p++;
00149 v.y = strtod(p, &p); p++;
00150 v.z = strtod(p, &p);
00151 return in;
00152 }
00153 };
00154
00161 struct Poly {
00162 union {
00163 struct {
00164 int a;
00165 int b;
00166 int c;
00167 int d;
00168 };
00169 int v[4];
00170 };
00171 uint32_t color;
00172
00174 bool triangle;
00175
00184 Poly(int _a=-1, int _b=-1, int _c=-1, int _d=-1, int _color=0xffffff);
00185
00192 bool operator== (const Poly &p) {
00193 if (d==-1)
00194 return (a==p.a && b==p.b && c==p.c)
00195 || (b==p.a && c==p.b && a==p.c)
00196 || (c==p.a && a==p.b && b==p.c);
00197 else
00198 return (a==p.a && b==p.b && c==p.c && d==p.d)
00199 || (b==p.a && c==p.b && d==p.c && a==p.d)
00200 || (c==p.a && d==p.b && a==p.c && b==p.d)
00201 || (d==p.a && a==p.b && b==p.c && c==p.d);
00202 }
00203
00208 void write(std::ostream &out) {
00209 out.write((char*)&a, 20);
00210 }
00211
00216 void read(std::istream &in) {
00217 in.read((char*)&a, 20);
00218 triangle = (d == -1);
00219 }
00220
00228 friend std::ostream &operator<< (std::ostream &out, const Poly &q) {
00229 if (q.triangle)
00230 return out << "3 " << q.a << " " << q.b << " " << q.c;
00231 else
00232 return out << "4 " << q.a << " " << q.b << " " << q.c << " " << q.d;
00233 }
00234 };
00235
00236 protected:
00238 std::vector<Vertex> vertices;
00240 std::vector<Poly> polys;
00241
00242 public:
00244 Object3d() : vertices(), polys() {}
00246 Object3d(const Object3d &obj, float zoom=1.0f);
00253 Object3d(const std::string &filename, float zoom=100.0f,
00254 uint32_t color=0xffffff);
00256 virtual ~Object3d() {}
00257
00259 int vertexCount() const { return vertices.size(); }
00261 int polyCount() const { return polys.size(); }
00263 const Vertex &vert(int i) const { return vertices[i]; }
00265 const Poly &poly(int i) const { return polys[i]; }
00266
00273 int addVertex(const Vertex &v);
00274
00283 int addVertex(float x, float y, float z);
00284
00295 int addPoly(int a, int b, int c, int d=-1, uint32_t color=0xffffff);
00296
00303 void translate(float x, float y, float z);
00304
00311 void merge(const Object3d &obj);
00312
00321 void importBin(const std::string &filename, float zoom=1.0f);
00322
00331 void importOff(const std::string &filename, float zoom=1.0f,
00332 uint32_t color=0xffffff);
00333
00341 void exportBin(const std::string &filename);
00342
00349 void exportOff(const std::string &filename);
00350 };
00351
00352 };
00353
00354 #endif