Text archives Help
- From: cgribble@sci.utah.edu
- To: manta@sci.utah.edu
- Subject: [MANTA] r1204 - in trunk: Core/Util Engine/Renderers
- Date: Thu, 5 Oct 2006 09:22:45 -0600 (MDT)
Author: cgribble
Date: Thu Oct 5 09:22:44 2006
New Revision: 1204
Modified:
trunk/Core/Util/RayTree.cc
trunk/Core/Util/RayTree.h
trunk/Engine/Renderers/Raydumper.cc
trunk/Engine/Renderers/Raydumper.h
Log:
Added endian-aware capabilities to RayTree read functions. Data is written in
the platforms native endianness, and if it doesn't match on the machine that
later reads the data, the bytes are swapped. Added a magic flag at the top of
each data file to determine byte-order. Changed data values that used size_t
to
use unsigned int instead to avoid 32/64-bit issues; this should probably be
corrected in a more intelligent matter.
Modified: trunk/Core/Util/RayTree.cc
==============================================================================
--- trunk/Core/Util/RayTree.cc (original)
+++ trunk/Core/Util/RayTree.cc Thu Oct 5 09:22:44 2006
@@ -4,9 +4,24 @@
#include <cstdio>
#include <cstdlib>
+#define MAGIC 1234
+#define byteswap(x) swapbytes(reinterpret_cast<unsigned char*>(&x),
sizeof(x))
+
using namespace Manta;
-// TODO: replace the fwrite, fread with EndianSafe versions
+// TODO: Prefer not to use unsigned int for num_{trees,children}, but size_t
+// causes issues between 32- and 64-bit systems
+
+inline void swapbytes(unsigned char* b, int n)
+{
+ register unsigned int i=0;
+ register unsigned int j=n - 1;
+ while (i<j) {
+ std::swap(b[i], b[j]);
+ ++i;
+ --j;
+ }
+}
RayForest::~RayForest()
{
@@ -25,12 +40,15 @@
// printf("RayForest::writeToFile() -- %d Children\n", int(trees.size()));
// write out the number of trees then
// call writeToFile recursively
- size_t num_trees = trees.size();
- size_t result = fwrite((const void*)&num_trees, sizeof(size_t), (size_t)1,
output);
+ unsigned int magic = MAGIC;
+ size_t result = fwrite((const void*)&magic, sizeof(unsigned int),
(size_t)1, output);
if (result != 1)
- {
- return false;
- }
+ return false;
+
+ unsigned int num_trees = trees.size();
+ result = fwrite((const void*)&num_trees, sizeof(unsigned int), (size_t)1,
output);
+ if (result != 1)
+ return false;
for (size_t i = 0; i < num_trees; i++ )
{
@@ -43,16 +61,35 @@
bool RayForest::readFromFile(FILE* input)
{
- size_t num_trees = 0;
- size_t result = fread((void*)&num_trees, sizeof(size_t), (size_t)1, input);
+ bool swapit = false;
+ unsigned int magic;
+ size_t result = fread((void*)&magic, sizeof(int), (size_t)1, input);
if (result != 1)
return false;
+ else if (magic != MAGIC) {
+ swapit = true;
+ // printf("RayForest::readFromFile() -- swapping bytes\n");
+ byteswap(magic);
+ if (magic != MAGIC) {
+ // printf("RayForest::readFromFile() -- swapping bytes didn't
work!\n");
+ return false;
+ }
+ }
+
+ // printf("RayForest::readFromFile() -- magic = %d, swapit = %s\n", magic,
(swapit?"true":"false"));
+
+ unsigned int num_trees = 0;
+ result = fread((void*)&num_trees, sizeof(unsigned int), (size_t)1, input);
+ if (result != 1)
+ return false;
+ else if (swapit)
+ byteswap(num_trees);
trees.resize(num_trees);
for (size_t i = 0; i < num_trees; i++ )
{
trees[i] = new RayTree;
- if (!trees[i]->readFromFile(input))
+ if (!trees[i]->readFromFile(input, swapit))
return false;
}
return true;
@@ -71,8 +108,8 @@
node.writeToFile(output);
// write out the number of children then
// call writeToFile recursively
- size_t num_children = children.size();
- size_t result = fwrite((const void*)&num_children, sizeof(size_t),
(size_t)1, output);
+ unsigned int num_children = children.size();
+ size_t result = fwrite((const void*)&num_children, sizeof(unsigned int),
(size_t)1, output);
if (result != 1)
{
fprintf(stderr, "Failed to write number of children\n");
@@ -89,24 +126,24 @@
return true;
}
-bool RayTree::readFromFile(FILE* input)
+bool RayTree::readFromFile(FILE* input, bool swapit)
{
- node.readFromFile(input);
+ node.readFromFile(input, swapit);
- size_t num_children = 0;
- size_t result = fread((void*)&num_children, sizeof(size_t), (size_t)1,
input);
- if (result != 1)
- {
- fprintf(stderr, "Failed to read number of children\n");
- return false;
- }
+ unsigned int num_children = 0;
+ size_t result = fread((void*)&num_children, sizeof(unsigned int),
(size_t)1, input);
+ if (result != 1) {
+ fprintf(stderr, "Failed to read number of children\n");
+ return false;
+ } else if (swapit) {
+ byteswap(result);
+ }
children.resize(num_children);
-
for (size_t i = 0; i < num_children; i++ )
{
children[i] = new RayTree;
- if (!children[i]->readFromFile(input))
+ if (!children[i]->readFromFile(input, swapit))
return false;
}
return true;
@@ -132,15 +169,25 @@
return true;
}
-bool RayInfo::readFromFile(FILE* input)
+bool RayInfo::readFromFile(FILE* input, bool swapit)
{
size_t result = fread((void*)origin, (size_t)4, (size_t)RayInfo::Num4Byte,
input);
if (result != Num4Byte)
return false;
+ else if (swapit) {
+ int* buffer = reinterpret_cast<int*>(origin);
+ for (int i = 0; i < RayInfo::Num4Byte; ++i)
+ byteswap(buffer[i]);
+ }
result = fread((void*)&ray_id, sizeof(long long int),
(size_t)RayInfo::Num8Byte, input);
if (result != Num8Byte)
return false;
+ else if (swapit) {
+ long long int* buffer = reinterpret_cast<long long int*>(&ray_id);
+ for (int i = 0; i < RayInfo::Num8Byte; ++i)
+ byteswap(buffer[i]);
+ }
return true;
}
Modified: trunk/Core/Util/RayTree.h
==============================================================================
--- trunk/Core/Util/RayTree.h (original)
+++ trunk/Core/Util/RayTree.h Thu Oct 5 09:22:44 2006
@@ -39,7 +39,7 @@
static const int Num8Byte = 2;
bool writeToFile(FILE* output) const;
- bool readFromFile(FILE* input);
+ bool readFromFile(FILE* input, bool swapit);
void setOrigin(float x, float y, float z)
{
@@ -110,7 +110,7 @@
void addChild(RayTree* t);
bool writeToFile(FILE* output) const;
- bool readFromFile(FILE* input);
+ bool readFromFile(FILE* input, bool swapit);
RayInfo node;
std::vector<RayTree*> children;
Modified: trunk/Engine/Renderers/Raydumper.cc
==============================================================================
--- trunk/Engine/Renderers/Raydumper.cc (original)
+++ trunk/Engine/Renderers/Raydumper.cc Thu Oct 5 09:22:44 2006
@@ -7,6 +7,8 @@
#include <Engine/Renderers/Raydumper.h>
#include <Interface/Context.h>
+#include <cfloat>
+
using namespace Manta;
using namespace SCIRun;
@@ -130,22 +132,30 @@
return shadows->getSpecs();
}
-void Raydumper::logRays(const RayPacket& rays, RayInfo::RayType type)
+void Raydumper::logRays(RayPacket& rays, RayInfo::RayType type)
{
int depth=rays.getDepth();
for (int i = rays.begin(); i < rays.end(); ++i) {
Vector o = rays.getOrigin(i);
Vector d = rays.getDirection(i);
+ float tmin = FLT_MAX;
+ if (rays.wasHit(i))
+ tmin = rays.getMinT(i);
+
+ RayInfo node;
+ node.setOrigin(o.x(), o.y(), o.z());
+ node.setDirection(d.x(), d.y(), d.z());
+ node.setTime(-1.f);
+ node.setHit(tmin);
+ node.setObjectID(-1);
+ node.setMaterialID(-1);
+ node.setSurfaceParams(-1.f, -1.f);
+ node.setDepth(depth);
+ node.setType(type);
+ node.setRayID(ray_id++);
+ node.setParentID(-1);
- RayInfo ray;
- ray.setRayID(ray_id++);
- // ray.setParentID(parent_id);
- ray.setDepth(depth);
- ray.setType(type);
- ray.setOrigin(o.x(), o.y(), o.z());
- ray.setDirection(d.x(), d.y(), d.z());
-
- forest.addChild(new RayTree(ray));
+ forest.addChild(new RayTree(node));
}
}
Modified: trunk/Engine/Renderers/Raydumper.h
==============================================================================
--- trunk/Engine/Renderers/Raydumper.h (original)
+++ trunk/Engine/Renderers/Raydumper.h Thu Oct 5 09:22:44 2006
@@ -43,7 +43,7 @@
Raydumper(const Raydumper&);
Raydumper& operator=(const Raydumper&);
- void logRays(const RayPacket& rays, RayInfo::RayType type);
+ void logRays(RayPacket& rays, RayInfo::RayType type);
void quit(int, int);
string renderer_string;
- [MANTA] r1204 - in trunk: Core/Util Engine/Renderers, cgribble, 10/05/2006
Archive powered by MHonArc 2.6.16.