00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include <iostream>
00031 #include <CMD/CMD_Args.h>
00032 #include <UT/UT_Assert.h>
00033 #include <GEO/GEO_AttributeHandle.h>
00034 #include <GU/GU_Detail.h>
00035 #include <GU/GU_PrimVolume.h>
00036
00037 static void
00038 usage(const char *program)
00039 {
00040 cerr << "Usage: " << program << " sourcefile dstfile\n";
00041 cerr << "The extension of the source/dest will be used to determine" << endl;
00042 cerr << "how the conversion is done. Supported extensions are .voxel" << endl;
00043 cerr << "and .bgeo" << endl;
00044 }
00045
00046
00047 bool
00048 voxelLoad(UT_IStream &is, GU_Detail *gdp)
00049 {
00050
00051 if (!is.checkToken("VOXELS"))
00052 return false;
00053
00054 GEO_AttributeHandle name_gah;
00055 int def = -1;
00056
00057 gdp->addPrimAttrib("name", sizeof(int), GB_ATTRIB_INDEX, &def);
00058 name_gah = gdp->getPrimAttribute("name");
00059
00060 while (is.checkToken("VOLUME"))
00061 {
00062 UT_String name;
00063 UT_WorkBuffer buf;
00064
00065 is.getWord(buf);
00066 name.harden(buf.buffer());
00067
00068 int rx, ry, rz;
00069
00070 is.read(&rx); is.read(&ry); is.read(&rz);
00071
00072
00073 float tx, ty, tz, sx, sy, sz;
00074
00075 is.read(&tx); is.read(&ty); is.read(&tz);
00076 is.read(&sx); is.read(&sy); is.read(&sz);
00077
00078 GU_PrimVolume *vol;
00079
00080 vol = (GU_PrimVolume *)GU_PrimVolume::build(gdp);
00081
00082
00083 name_gah.setElement(vol);
00084 name_gah.setString(name);
00085
00086
00087 vol->getVertex().getPt()->getPos() = UT_Vector3(tx, ty, tz);
00088
00089 UT_Matrix3 xform;
00090
00091
00092
00093 xform.identity();
00094 xform.scale(sx/2, sy/2, sz/2);
00095
00096 vol->setTransform(xform);
00097
00098 UT_VoxelArrayWriteHandleF handle = vol->getVoxelWriteHandle();
00099
00100
00101 handle->size(rx, ry, rz);
00102
00103 if (!is.checkToken("{"))
00104 return false;
00105
00106 for (int z = 0; z < rz; z++)
00107 {
00108 for (int y = 0; y < ry; y++)
00109 {
00110 for (int x = 0; x < rx; x++)
00111 {
00112 float v;
00113
00114 is.read(&v);
00115
00116 handle->setValue(x, y, z, v);
00117 }
00118 }
00119 }
00120
00121 if (!is.checkToken("}"))
00122 return false;
00123
00124
00125 }
00126
00127
00128 return true;
00129 }
00130
00131 bool
00132 voxelLoad(const char *fname, GU_Detail *gdp)
00133 {
00134
00135
00136 UT_IFStream is(fname, UT_ISTREAM_ASCII);
00137
00138 return voxelLoad(is, gdp);
00139 }
00140
00141 bool
00142 voxelSave(ostream &os, const GU_Detail *gdp)
00143 {
00144
00145 os << "VOXELS" << endl;
00146
00147
00148 const GEO_Primitive *prim;
00149 GEO_AttributeHandle name_gah;
00150 UT_String name;
00151 UT_WorkBuffer buf;
00152
00153 name_gah = gdp->getPrimAttribute("name");
00154 FOR_ALL_PRIMITIVES(gdp, prim)
00155 {
00156 if (prim->getPrimitiveId() == GEOPRIMVOLUME)
00157 {
00158
00159 buf.sprintf("volume_%d", prim->getNum());
00160 name.harden(buf.buffer());
00161
00162
00163 if (name_gah.isAttributeValid())
00164 {
00165 name_gah.setElement(prim);
00166 name_gah.getString(name);
00167 }
00168
00169 os << "VOLUME " << name << endl;
00170 const GEO_PrimVolume *vol = (GEO_PrimVolume *) prim;
00171
00172 int resx, resy, resz;
00173
00174
00175 vol->getRes(resx, resy, resz);
00176 os << resx << " " << resy << " " << resz << endl;
00177
00178
00179
00180
00181
00182 UT_Vector3 p1, p2;
00183
00184 os << vol->getVertex().getPt()->getPos().x() << " "
00185 << vol->getVertex().getPt()->getPos().y() << " "
00186 << vol->getVertex().getPt()->getPos().z() << endl;
00187
00188 vol->indexToPos(0, 0, 0, p1);
00189 vol->indexToPos(1, 0, 0, p2);
00190 os << resx * (p1 - p2).length() << " ";
00191 vol->indexToPos(0, 1, 0, p2);
00192 os << resy * (p1 - p2).length() << " ";
00193 vol->indexToPos(0, 0, 1, p2);
00194 os << resz * (p1 - p2).length() << endl;
00195
00196 UT_VoxelArrayReadHandleF handle = vol->getVoxelHandle();
00197
00198
00199 os << "{" << endl;
00200 for (int z = 0; z < resz; z++)
00201 {
00202 for (int y = 0; y < resy; y++)
00203 {
00204 os << " ";
00205 for (int x = 0; x < resx; x++)
00206 {
00207 os << (*handle)(x, y, z) << " ";
00208 }
00209 os << endl;
00210 }
00211 }
00212 os << "}" << endl;
00213 os << endl;
00214 }
00215 }
00216
00217 return true;
00218 }
00219
00220 bool
00221 voxelSave(const char *fname, const GU_Detail *gdp)
00222 {
00223 ofstream os(fname);
00224
00225
00226
00227
00228 os.precision(SYS_FLT_DIG);
00229
00230 return voxelSave(os, gdp);
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 int
00248 main(int argc, char *argv[])
00249 {
00250 CMD_Args args;
00251 GU_Detail gdp;
00252
00253 args.initialize(argc, argv);
00254
00255 if (args.argc() != 3)
00256 {
00257 usage(argv[0]);
00258 return 1;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267 UT_String inputname, outputname;
00268
00269 inputname.harden(argv[1]);
00270 outputname.harden(argv[2]);
00271
00272 if (!strcmp(inputname.fileExtension(), ".voxel"))
00273 {
00274
00275 voxelLoad(inputname, &gdp);
00276
00277
00278 gdp.save((const char *) outputname, 0, 0);
00279 }
00280 else
00281 {
00282
00283 gdp.load((const char *) inputname, 0);
00284
00285 voxelSave(outputname, &gdp);
00286 }
00287 return 0;
00288 }