Raytracing: Added function to write shapes as luxrender strings

This commit is contained in:
Yorik van Havre
2013-09-22 19:30:07 -03:00
parent 1c51aea58a
commit 382a12f55f
3 changed files with 104 additions and 2 deletions

View File

@@ -29,6 +29,7 @@
#include <Base/Console.h>
#include "PovTools.h"
#include "LuxTools.h"
// automatically generated.....
#include "FreeCADpov.h"
@@ -92,6 +93,32 @@ getPartAsPovray(PyObject *self, PyObject *args)
return Py::new_reference_to(Py::String(out.str()));
}
/// get part as lux string
static PyObject *
getPartAsLux(PyObject *self, PyObject *args)
{
float r=0.5,g=0.5,b=0.5;
PyObject *ShapeObject;
const char *PartName;
if (! PyArg_ParseTuple(args, "sO!|fff",&PartName,
&(Part::TopoShapePy::Type), &ShapeObject,&r,&g,&b))
return NULL;
std::stringstream out;
TopoDS_Shape &aShape = static_cast<Part::TopoShapePy *>(ShapeObject)->getTopoShapePtr()->_Shape;
// write a material entry
// This must not be done in PovTools::writeShape!
out << "MakeNamedMaterial \"FreeCADMaterial_" << PartName << "\"" << endl;
out << " \"color Kd\" [" << r << " " << g << " " << b << "]" << endl;
out << " \"float sigma\" [0.000000000000000]" << endl;
out << " \"string type\" [\"matte\"]" << endl << endl;
LuxTools::writeShape(out,PartName,aShape,(float)0.1);
return Py::new_reference_to(Py::String(out.str()));
}
/// write part file
static PyObject *
writePartFile(PyObject *self, PyObject *args)
@@ -218,6 +245,7 @@ struct PyMethodDef Raytracing_methods[] = {
{"writePartFile", writePartFile , 1},
{"writePartFileCSV", writePartFileCSV, 1},
{"getPartAsPovray", getPartAsPovray , 1},
{"getPartAsLux", getPartAsLux , 1},
{"writeDataFile", writeDataFile , 1},
{"writeCameraFile", writeCameraFile , 1},
{"copyResource", copyResource , 1},

View File

@@ -63,3 +63,75 @@ std::string LuxTools::getCamera(const CamDef& Cam)
return out.str();
}
void LuxTools::writeShape(std::ostream &out, const char *PartName, const TopoDS_Shape& Shape, float fMeshDeviation)
{
Base::Console().Log("Meshing with Deviation: %f\n",fMeshDeviation);
TopExp_Explorer ex;
BRepMesh_IncrementalMesh MESH(Shape,fMeshDeviation);
// counting faces and start sequencer
int l = 1;
for (ex.Init(Shape, TopAbs_FACE); ex.More(); ex.Next(),l++) {}
Base::SequencerLauncher seq("Writing file", l);
// write object
out << "AttributeBegin # \"" << PartName << "\"" << endl;
out << "Transform [1.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 1.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 1.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 1.000000000000000]" << endl;
out << "NamedMaterial \"FreeCADMaterial_" << PartName << "\"" << endl;
out << "Shape \"mesh\"" << endl;
// gather vertices, normals and face indices
std::stringstream triindices;
std::stringstream N;
std::stringstream P;
l = 1;
int vi = 0;
for (ex.Init(Shape, TopAbs_FACE); ex.More(); ex.Next(),l++) {
// get the shape and mesh it
const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
// this block mesh the face and transfers it in a C array of vertices and face indexes
Standard_Integer nbNodesInFace,nbTriInFace;
gp_Vec* vertices=0;
gp_Vec* vertexnormals=0;
long* cons=0;
PovTools::transferToArray(aFace,&vertices,&vertexnormals,&cons,nbNodesInFace,nbTriInFace);
if (!vertices) break;
// writing vertices
for (int i=0; i < nbNodesInFace; i++) {
P << vertices[i].X() << " " << vertices[i].Z() << " " << vertices[i].Y() << " ";
}
// writing per vertex normals
for (int j=0; j < nbNodesInFace; j++) {
N << vertexnormals[j].X() << " " << vertexnormals[j].Z() << " " << vertexnormals[j].Y() << " ";
}
// writing triangle indices
for (int k=0; k < nbTriInFace; k++) {
triindices << cons[3*k]+vi << " " << cons[3*k+2]+vi << " " << cons[3*k+1]+vi << " ";
}
vi = vi + nbNodesInFace;
delete [] vertexnormals;
delete [] vertices;
delete [] cons;
seq.next();
} // end of face loop
// write mesh data
out << " \"integer triindices\" [" << triindices.str() << "]" << endl;
out << " \"point P\" [" << P.str() << "]" << endl;
out << " \"normal N\" [" << N.str() << "]" << endl;
out << " \"bool generatetangents\" [\"false\"]" << endl;
out << " \"string name\" [\"" << PartName << "\"]" << endl;
out << "AttributeEnd # \"\"" << endl;
}

View File

@@ -37,9 +37,11 @@ namespace Raytracing
class AppRaytracingExport LuxTools
{
public:
/// returns the given camera position as povray defines in a file
/// returns the given camera position as luxray defines
static std::string getCamera(const CamDef& Cam);
/// returns the given shape as luxrender material + shape data
static void writeShape(std::ostream &out, const char *PartName, const TopoDS_Shape& Shape, float fMeshDeviation=0.1);
};
} // namespace Raytracing
#endif // _PovTools_h_
#endif // _LuxTools_h_