#include <stdio.h>
#include <iostream>
#undef drand48
#undef srand48
#include <Field3D/Field.h>
#include <Field3D/Field3DFile.h>
#include <Field3D/InitIO.h>
#include <Field3D/DenseField.h>
#include <Field3D/SparseField.h>
#include <Field3D/MACField.h>
#include "f3d_io.h"
using namespace HDK_Sample;
void
{
for (
int y = 0;
y < 4;
y++)
for (
int x = 0;
x < 4;
x++)
}
void
{
for (
int y = 0;
y < 4;
y++)
for (
int x = 0;
x < 4;
x++)
}
template <typename FIELD_PTR>
void
bool ismacfield, int axis, int res)
{
Field3D::FieldMapping *raw_mapping = field->mapping().get();
Field3D::MatrixFieldMapping *mtx_mapping = dynamic_cast<Field3D::MatrixFieldMapping *>(raw_mapping);
if (dynamic_cast<Field3D::NullFieldMapping *>(raw_mapping))
{
xform4.
scale(0.5, 0.5, 0.5);
}
else if (mtx_mapping)
{
f3d_matrixConvertToUT(f3dmtx, xform4);
tohoudini.
scale(0.5, 0.5, 0.5);
xform4 = tohoudini * xform4;
}
else
{
std::cerr << "Warning: Unknown mapping type found." << std::endl;
}
if (ismacfield)
{
double scalefactor[3] = { 1, 1, 1 };
scalefactor[axis] *= ((double)res) / ((double)(res+1));
tomac.
scale(scalefactor[0], scalefactor[1], scalefactor[2]);
xform4 = tomac * xform4;
}
}
template <typename FIELD_PTR>
void
f3d_getTransformFromVolume(
const GEO_PrimVolume *vol, FIELD_PTR field)
{
fromhoudini.
scale(2, 2, 2);
xform4 = fromhoudini * xform4;
f3d_matrixConvertToF3D(xform4, f3dmtx);
Field3D::MatrixFieldMapping::Ptr mapping(new Field3D::MatrixFieldMapping);
mapping->setLocalToWorld(f3dmtx);
field->setMapping(mapping);
#if 0
{
mapping->localToWorld(wpt, vpt);
cerr << "Mapping to world " << vpt << endl;
}{
mapping->localToWorld(wpt, vpt);
cerr << "Mapping to world " << vpt << endl;
}{
mapping->localToWorld(wpt, vpt);
cerr << "Mapping to world " << vpt << endl;
}{
mapping->localToWorld(wpt, vpt);
cerr << "Mapping to world " << vpt << endl;
}
#endif
}
template <typename FIELD_PTR>
void
{
database = field->dataWindow().min;
{
v = field->value(vit.
x() + database.x,
}
}
template <typename FIELD_PTR>
void
{
database = field->dataWindow().min;
{
v = field->fastValue(vit.
x() + database.x,
}
}
template <typename FIELD>
void
{
database = field->dataWindow().min;
if (field->blockSize() != 16)
{
f3d_loadDenseField(dst, field);
return;
}
if (database.x & 15 ||
database.y & 15 ||
database.z & 15)
{
f3d_loadDenseField(dst, field);
return;
}
typename FIELD::block_iterator bi = field->blockBegin();
bool isfp16 =
sizeof(field->getBlockEmptyValue(0, 0, 0)) ==
sizeof(
fpreal16);
for (; bi != field->blockEnd(); ++bi)
{
tile = dst->
getTile(bi.x, bi.y, bi.z);
if (!field->blockIsAllocated(bi.x, bi.y, bi.z))
{
v = field->getBlockEmptyValue(bi.x, bi.y, bi.z);
}
else
{
bmin = bi.blockBoundingBox().min;
bmax = bi.blockBoundingBox().max;
for (
int z = 0;
z < bsize.z;
z++)
{
for (
int y = 0;
y < bsize.y;
y++)
{
for (
int x = 0;
x < bsize.x;
x++)
{
v = field->fastValue(
x+bmin.x,
y+bmin.y,
z+bmin.z);
}
}
}
if (isfp16)
}
}
}
template <typename FIELD_PTR>
void
{
database = field->dataWindow().min;
for (int i = 0; i < 3; i++)
{
}
for (; !vit[0].
atEnd(); )
{
v = field->value(vit[0].
x() + database.x,
vit[0].
z() + database.z);
}
}
template <typename FIELD_PTR>
void
{
database = field->dataWindow().min;
for (int i = 0; i < 3; i++)
{
}
for (; !vit[0].
atEnd(); )
{
v = field->fastValue(vit[0].
x() + database.x,
vit[0].
z() + database.z);
}
}
template <typename FIELD_PTR>
void
{
for (int i = 0; i < 3; i++)
{
database = field->dataWindow().min;
{
if (i == 0)
v = field->u(vit.
x() + database.x,
else if (i == 1)
v = field->v(vit.
x() + database.x,
else
v = field->w(vit.
x() + database.x,
}
}
}
{
}
{
}
f3d_createAttribute(
GEO_Detail *gdp,
const char *name,
int value)
{
}
{
}
{
}
void
{
}
void
{
h.set(offset, 0, value.x);
h.set(offset, 1, value.x);
h.set(offset, 2, value.x);
}
void
{
}
void
{
h.set(offset, 0, value.x);
h.set(offset, 1, value.x);
h.set(offset, 2, value.x);
}
void
{
h.set(offset, value.c_str());
}
template <typename T>
void
const char *name, const T &value)
{
if (sname.startsWith("_houdini_."))
{
return;
}
aref = f3d_createAttribute(gdp, name, value);
}
template <typename T>
void
const T &metadata)
{
typename T::const_iterator i;
for (i = metadata.begin(); i != metadata.end(); ++i)
{
f3d_singleMetadataToPrimitive(gdp, vol, i->first.c_str(), i->second);
}
}
template <typename T>
void
f3d_vectorMetadataListToPrimitive(int vecidx,
const T &metadata)
{
typename T::const_iterator i;
for (i = metadata.begin(); i != metadata.end(); ++i)
{
if (vecidx == 0)
f3d_singleMetadataToPrimitive(gdp, vol, i->first.c_str(), i->second.x);
else if (vecidx == 1)
f3d_singleMetadataToPrimitive(gdp, vol, i->first.c_str(), i->second.y);
else
f3d_singleMetadataToPrimitive(gdp, vol, i->first.c_str(), i->second.z);
}
}
template <typename FIELD_PTR>
void
FIELD_PTR field)
{
f3d_metadataListToPrimitive(gdp, vol, field->metadata().vecFloatMetadata());
f3d_metadataListToPrimitive(gdp, vol, field->metadata().floatMetadata());
f3d_metadataListToPrimitive(gdp, vol, field->metadata().vecIntMetadata());
f3d_metadataListToPrimitive(gdp, vol, field->metadata().intMetadata());
f3d_metadataListToPrimitive(gdp, vol, field->metadata().strMetadata());
}
template <typename FIELD_PTR>
void
FIELD_PTR field)
{
f3d_vectorMetadataListToPrimitive(
vecidx, gdp, vol, field->metadata().vecFloatMetadata());
f3d_metadataListToPrimitive(
gdp, vol, field->metadata().floatMetadata());
f3d_vectorMetadataListToPrimitive(
vecidx, gdp, vol, field->metadata().vecIntMetadata());
f3d_metadataListToPrimitive(
gdp, vol, field->metadata().intMetadata());
f3d_metadataListToPrimitive(
gdp, vol, field->metadata().strMetadata());
}
template <typename T>
void
{
typename Field3D::Field<T>::Vec scalarfields;
scalarfields = infile.readScalarLayers<
T>();
typename Field3D::Field<T>::Vec::const_iterator i = scalarfields.begin();
for (; i != scalarfields.end(); ++i)
{
int rx, ry, rz;
rawres = (**i).dataResolution();
database = (**i).dataWindow().min;
rx = rawres.x;
ry = rawres.y;
rz = rawres.z;
{
name += ".";
}
f3d_metadataToPrimitive(gdp, vol, (*i));
float taperx = (*i)->metadata().floatMetadata("_houdini_.taperx", 1.0);
float tapery = (*i)->metadata().floatMetadata("_houdini_.tapery", 1.0);
int primnum = (*i)->metadata().intMetadata("_houdini_.primnum", -1);
if (primnum == -1)
f3d_getTransformFromField((*i), vol, false, 0, 0);
{
handle->setCompressionOptions(options);
}
handle->size(rx, ry, rz);
typename Field3D::DenseField<T>::Ptr dense_field = Field3D::field_dynamic_cast< Field3D::DenseField<T> > (*i);
typename Field3D::SparseField<T>::Ptr sparse_field = Field3D::field_dynamic_cast< Field3D::SparseField<T> > (*i);
if (dense_field)
{
f3d_loadDenseField(&*handle, dense_field);
}
else if (sparse_field)
{
f3d_loadSparseField(&*handle, sparse_field.
get());
}
else
{
f3d_loadField(&*handle, *i);
}
}
typename Field3D::Field< FIELD3D_VEC3_T<T> >::Vec vectorfields;
vectorfields = infile.readVectorLayers<
T>();
for (typename Field3D::Field< FIELD3D_VEC3_T<T> >::Vec::const_iterator i = vectorfields.begin(); i != vectorfields.end(); ++i)
{
int rx, ry, rz;
rawres = (**i).dataResolution();
database = (**i).dataWindow().min;
rx = rawres.x;
ry = rawres.y;
rz = rawres.z;
{
name += ".";
}
typename Field3D::DenseField< FIELD3D_VEC3_T<T> >::Ptr dense_field = Field3D::field_dynamic_cast< Field3D::DenseField< FIELD3D_VEC3_T<T> > > (*i);
typename Field3D::SparseField< FIELD3D_VEC3_T<T> >::Ptr sparse_field = Field3D::field_dynamic_cast< Field3D::SparseField< FIELD3D_VEC3_T<T> > > (*i);
typename Field3D::MACField< FIELD3D_VEC3_T<T> >::Ptr mac_field = Field3D::field_dynamic_cast< Field3D::MACField< FIELD3D_VEC3_T<T> > > (*i);
for (
int j = 0;
j < 3;
j++)
{
f3d_vectorMetadataToPrimitive(
j, gdp, vol[
j], (*i));
float taperx = (*i)->metadata().floatMetadata("_houdini_.taperx", 1.0);
float tapery = (*i)->metadata().floatMetadata("_houdini_.tapery", 1.0);
int primnum = (*i)->metadata().intMetadata("_houdini_.primnum", -1);
if (primnum == -1)
primsortlist.
append(primnum + (j * 0.3));
char ext[2] = "x";
int res[3];
nameext = name;
nameext += ".";
nameext += ext;
name_gah.set(vol[j]->getMapOffset(), nameext);
res[0] = rx;
res[1] = ry;
res[2] = rz;
f3d_getTransformFromField((*i), vol[j], mac_field != 0, j, res[j]);
{
handle[
j]->setCompressionOptions(options);
}
if (mac_field)
handle[
j]->size(res[0], res[1], res[2]);
}
if (dense_field)
{
f3d_loadDenseField(vox, dense_field);
}
else if (sparse_field)
{
f3d_loadDenseField(vox, sparse_field);
}
else if (mac_field)
{
f3d_loadMACField(vox, mac_field);
}
else
{
f3d_loadField(vox, *i);
}
}
}
template <typename FIELD_PTR>
void
{
{
if (!strcmp(atr->
getName(),
"name"))
continue;
{
if (str.isstring())
{
field->metadata().setStrMetadata(aname, avalue);
}
}
{
{
if (vol2 && vol3)
{
mv.x =
h.get(prim_offset);
field->metadata().setVecFloatMetadata(aname, mv);
}
else
field->metadata().setFloatMetadata(aname,
}
else
{
mv.x =
h.get(prim_offset, 0);
mv.y =
h.get(prim_offset, 1);
mv.z =
h.get(prim_offset, 2);
field->metadata().setVecFloatMetadata(aname, mv);
}
}
{
{
if (vol2 && vol3)
{
mv.x =
h.get(prim_offset);
field->metadata().setVecIntMetadata(aname, mv);
}
else
field->metadata().setIntMetadata(aname,
}
else
{
mv.x =
h.get(prim_offset, 0);
mv.y =
h.get(prim_offset, 1);
mv.z =
h.get(prim_offset, 2);
field->metadata().setVecIntMetadata(aname, mv);
}
}
}
}
template <typename T, typename FIELD_PTR>
void
{
int resx, resy, resz;
if (name_gah.isValid())
vol->
getRes(resx, resy, resz);
scalarfield->clear(0.0
f);
{
scalarfield->name = (const char *) namewithoutextension;
}
else
{
scalarfield->name = (const char *) name;
scalarfield->attribute = (const char *) name;
}
f3d_primitiveToMetadata(gdp, vol, 0, 0, scalarfield);
f3d_getTransformFromVolume(vol, scalarfield);
scalarfield->metadata().setIntMetadata(
"_houdini_.primnum", vol->
getMapIndex());
scalarfield->metadata().setFloatMetadata(
"_houdini_.taperx", vol->
getTaperX());
scalarfield->metadata().setFloatMetadata(
"_houdini_.tapery", vol->
getTaperY());
typename Field3D::DenseField<T>::Ptr densefield = Field3D::field_dynamic_cast< Field3D::DenseField<T> > (scalarfield);
typename Field3D::SparseField<T>::Ptr sparsefield = Field3D::field_dynamic_cast< Field3D::SparseField<T> > (scalarfield);
if (densefield)
{
{
densefield->fastLValue(vit.
x(), vit.
y(), vit.
z()) = vit.
getValue();
}
}
else if (sparsefield)
{
sparsefield->setBlockOrder(4);
typename Field3D::SparseField<T>::block_iterator bi = sparsefield->blockBegin();
for (; bi != sparsefield->blockEnd(); ++bi)
{
tile = handle->getTile(bi.x, bi.y, bi.z);
{
sparsefield->setBlockEmptyValue(bi.x, bi.y, bi.z, v);
}
else
{
bmin = bi.blockBoundingBox().min;
bmax = bi.blockBoundingBox().max;
for (
int z = 0;
z < bsize.z;
z++)
{
for (
int y = 0;
y < bsize.y;
y++)
{
for (
int x = 0;
x < bsize.x;
x++)
{
sparsefield->fastLValue(x+bmin.x, y+bmin.y,
z+bmin.z) =
v;
}
}
}
}
}
}
else
{
{
scalarfield->lvalue(vit.
x(), vit.
y(), vit.
z()) = vit.
getValue();
}
}
out.writeScalarLayer<
T>(scalarfield);
}
template <typename T, typename FIELD_PTR>
void
f3d_SaveVectorField(Field3D::Field3DOutputFile &out, FIELD_PTR vectorfield,
const GEO_Detail *gdp,
const GEO_PrimVolume *vol[3])
{
int resx, resy, resz;
int i;
if (name_gah.isValid())
{
}
vol[0]->
getRes(resx, resy, resz);
zero.x = 0.0;
zero.y = 0.0;
zero.z = 0.0;
vectorfield->
clear(zero);
{
vectorfield->name = (const char *) namewithoutextension;
}
else
{
vectorfield->name = (const char *) name;
vectorfield->attribute = (const char *) name;
}
f3d_primitiveToMetadata(gdp, vol[0], vol[1], vol[2], vectorfield);
f3d_getTransformFromVolume(vol[0], vectorfield);
vectorfield->metadata().setIntMetadata("_houdini_.primnum", vol[0]->getMapIndex());
vectorfield->metadata().setFloatMetadata("_houdini_.taperx", vol[0]->getTaperX());
vectorfield->metadata().setFloatMetadata("_houdini_.tapery", vol[0]->getTaperY());
typename Field3D::DenseField<FIELD3D_VEC3_T<T> >::Ptr densefield = Field3D::field_dynamic_cast< Field3D::DenseField<FIELD3D_VEC3_T<T> > > (vectorfield);
typename Field3D::SparseField<FIELD3D_VEC3_T<T> >::Ptr sparsefield = Field3D::field_dynamic_cast< Field3D::SparseField<FIELD3D_VEC3_T<T> > > (vectorfield);
if (densefield)
{
for (i = 0; i < 3; i++)
{
}
for (; !vit[0].
atEnd(); )
{
densefield->fastLValue(vit[0].
x(), vit[0].
y(), vit[0].
z()) =
value;
for (i = 0; i < 3; i++)
vit[i].advance();
}
}
else if (sparsefield)
{
sparsefield->setBlockOrder(4);
typename Field3D::SparseField<FIELD3D_VEC3_T<T> >::block_iterator bi = sparsefield->blockBegin();
for (; bi != sparsefield->blockEnd(); ++bi)
{
for (i = 0; i < 3; i++)
tile[i] = handle[i]->getTile(bi.x, bi.y, bi.z);
if (tile[0]->isConstant() &&
{
v.x = (*tile[0])(0, 0, 0);
v.y = (*tile[1])(0, 0, 0);
v.z = (*tile[2])(0, 0, 0);
sparsefield->setBlockEmptyValue(bi.x, bi.y, bi.z, v);
}
else
{
bmin = bi.blockBoundingBox().min;
bmax = bi.blockBoundingBox().max;
for (
int z = 0;
z < bsize.z;
z++)
{
for (
int y = 0;
y < bsize.y;
y++)
{
for (
int x = 0;
x < bsize.x;
x++)
{
v.x = (*tile[0])(
x,
y,
z);
v.y = (*tile[1])(
x,
y,
z);
v.z = (*tile[2])(
x,
y,
z);
sparsefield->fastLValue(
x+bmin.x,
y+bmin.y,
z+bmin.z) =
v;
}
}
}
}
}
}
else
{
for (i = 0; i < 3; i++)
{
}
for (; !vit[0].
atEnd(); )
{
vectorfield->lvalue(vit[0].
x(), vit[0].
y(), vit[0].
z()) =
value;
for (i = 0; i < 3; i++)
vit[i].advance();
}
}
out.writeVectorLayer<
T>(vectorfield);
}
bool
f3d_SaveCollated(Field3D::Field3DOutputFile &out,
F3D_BitDepth bitdepth,
F3D_GridType gridtype,
{
int resx, resy, resz;
int oresx, oresy, oresz;
vol[0]->
getRes(resx, resy, resz);
vol[1]->
getRes(oresx, oresy, oresz);
if (resx != oresx || resy != oresy || resz != oresz)
{
return false;
}
vol[2]->
getRes(oresx, oresy, oresz);
if (resx != oresx || resy != oresy || resz != oresz)
{
return false;
}
F3D_BitDepth desireddepth;
if (bitdepth == F3D_BITDEPTH_AUTO)
{
desireddepth = F3D_BITDEPTH_FLOAT;
desireddepth = F3D_BITDEPTH_HALF;
}
else
desireddepth = bitdepth;
if (desireddepth == F3D_BITDEPTH_HALF)
{
if (gridtype == F3D_GRIDTYPE_DENSE)
{
Field3D::DenseField< FIELD3D_VEC3_T<Field3D::half> >::Ptr vectorfield(new Field3D::DenseField< FIELD3D_VEC3_T<Field3D::half> >);
f3d_SaveVectorField<Field3D::half>(out, vectorfield, gdp, vol);
}
else if (gridtype == F3D_GRIDTYPE_SPARSE)
{
Field3D::SparseField< FIELD3D_VEC3_T<Field3D::half> >::Ptr vectorfield(new Field3D::SparseField< FIELD3D_VEC3_T<Field3D::half> >);
f3d_SaveVectorField<Field3D::half>(out, vectorfield, gdp, vol);
}
}
else if (desireddepth == F3D_BITDEPTH_FLOAT)
{
if (gridtype == F3D_GRIDTYPE_DENSE)
{
Field3D::DenseField<FIELD3D_VEC3_T<float> >::Ptr vectorfield(new Field3D::DenseField<FIELD3D_VEC3_T<float> >);
f3d_SaveVectorField<float>(out, vectorfield, gdp, vol);
}
else if (gridtype == F3D_GRIDTYPE_SPARSE)
{
Field3D::SparseField<FIELD3D_VEC3_T<float> >::Ptr vectorfield(new Field3D::SparseField<FIELD3D_VEC3_T<float> >);
f3d_SaveVectorField<float>(out, vectorfield, gdp, vol);
}
}
else if (desireddepth == F3D_BITDEPTH_DOUBLE)
{
if (gridtype == F3D_GRIDTYPE_DENSE)
{
Field3D::DenseField<FIELD3D_VEC3_T<double> >::Ptr vectorfield(new Field3D::DenseField<FIELD3D_VEC3_T<double> >);
f3d_SaveVectorField<double>(out, vectorfield, gdp, vol);
}
else if (gridtype == F3D_GRIDTYPE_SPARSE)
{
Field3D::SparseField<FIELD3D_VEC3_T<double> >::Ptr vectorfield(new Field3D::SparseField<FIELD3D_VEC3_T<double> >);
f3d_SaveVectorField<double>(out, vectorfield, gdp, vol);
}
}
return true;
}
namespace HDK_Sample {
{
Field3D::Field3DInputFile infile;
if (!infile.open(fname))
{
std::cerr << "Error: Failed to open " << fname << " as a Field3D file.\n";
return false;
}
f3d_LoadFields<Field3D::half>(gdp, infile, primsortlist);
f3d_LoadFields<float>(gdp, infile, primsortlist);
f3d_LoadFields<double>(gdp, infile, primsortlist);
if (primsortlist.
entries() == gdp->getNumPrimitives())
{
static_cast<GU_Detail *
>(gdp)->sortPrimitiveList(primsortlist.
array());
}
return true;
}
f3d_fileSave(
const GEO_Detail *gdp,
const char *fname,
F3D_BitDepth bitdepth,
F3D_GridType gridtype,
bool collatevector)
{
Field3D::Field3DOutputFile out;
out.create(fname);
if (collatevector && name_gah.isValid())
{
{
continue;
const char *name = name_gah.get(prim_off);
name_lut[name] = prim_off;
}
{
continue;
if (name.fileExtension() && !strcmp(name.fileExtension(), ".x"))
{
othername += ".y";
auto it = name_lut.find(othername);
if (it != name_lut.end())
ynum = it->second;
othername += ".z";
it = name_lut.find(othername);
if (it != name_lut.end())
znum = it->second;
{
if (f3d_SaveCollated(out, bitdepth, gridtype,
gdp, xnum, ynum, znum))
{
processed.insert(xnum);
processed.insert(ynum);
processed.insert(znum);
}
}
}
}
}
{
if (processed.find(prim_off) != processed.end())
continue;
processed.insert(prim_off);
{
F3D_BitDepth desireddepth;
if (bitdepth == F3D_BITDEPTH_AUTO)
{
desireddepth = F3D_BITDEPTH_FLOAT;
desireddepth = F3D_BITDEPTH_HALF;
}
else
desireddepth = bitdepth;
if (desireddepth == F3D_BITDEPTH_HALF)
{
if (gridtype == F3D_GRIDTYPE_DENSE)
{
Field3D::DenseField<Field3D::half>::Ptr scalarfield(new Field3D::DenseField<Field3D::half>);
f3d_SaveField<Field3D::half>(out, scalarfield, gdp, vol);
}
else if (gridtype == F3D_GRIDTYPE_SPARSE)
{
Field3D::SparseField<Field3D::half>::Ptr scalarfield(new Field3D::SparseField<Field3D::half>);
f3d_SaveField<Field3D::half>(out, scalarfield, gdp, vol);
}
}
else if (desireddepth == F3D_BITDEPTH_FLOAT)
{
if (gridtype == F3D_GRIDTYPE_DENSE)
{
Field3D::DenseField<float>::Ptr scalarfield(new Field3D::DenseField<float>);
f3d_SaveField<float>(out, scalarfield, gdp, vol);
}
else if (gridtype == F3D_GRIDTYPE_SPARSE)
{
Field3D::SparseField<float>::Ptr scalarfield(new Field3D::SparseField<float>);
f3d_SaveField<float>(out, scalarfield, gdp, vol);
}
}
else if (desireddepth == F3D_BITDEPTH_DOUBLE)
{
if (gridtype == F3D_GRIDTYPE_DENSE)
{
Field3D::DenseField<double>::Ptr scalarfield(new Field3D::DenseField<double>);
f3d_SaveField<double>(out, scalarfield, gdp, vol);
}
else if (gridtype == F3D_GRIDTYPE_SPARSE)
{
Field3D::SparseField<double>::Ptr scalarfield(new Field3D::SparseField<double>);
f3d_SaveField<double>(out, scalarfield, gdp, vol);
}
}
}
}
return true;
}
}