Skip to content

Commit

Permalink
Add support for points and morphologies in scene loader (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
favreau committed Aug 2, 2017
1 parent f7ff9f0 commit 4db3ff0
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 36 deletions.
36 changes: 25 additions & 11 deletions brayns/io/MorphologyLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,24 @@ bool MorphologyLoader::_importMorphologyAsMesh(
}

bool MorphologyLoader::importMorphology(const servus::URI& uri,
const int morphologyIndex, Scene& scene)
const int morphologyIndex, Scene& scene,
const Matrix4f& transformation)
{
bool returnValue = true;
if (_geometryParameters.useMetaballs())
{
returnValue =
_importMorphologyAsMesh(uri, morphologyIndex, scene.getMaterials(),
Matrix4f(), scene.getTriangleMeshes(),
transformation, scene.getTriangleMeshes(),
scene.getWorldBounds());
}
float maxDistanceToSoma;
returnValue = returnValue &&
_importMorphology(uri, morphologyIndex, Matrix4f(), nullptr,
scene.getSpheres(), scene.getCylinders(),
scene.getCones(), scene.getWorldBounds(),
NO_OFFSET, maxDistanceToSoma);
returnValue =
returnValue &&
_importMorphology(uri, morphologyIndex, transformation, nullptr,
scene.getSpheres(), scene.getCylinders(),
scene.getCones(), scene.getWorldBounds(), NO_OFFSET,
maxDistanceToSoma);
return returnValue;
}

Expand Down Expand Up @@ -485,17 +487,24 @@ bool MorphologyLoader::importCircuit(const servus::URI& circuitConfig,
BRAYNS_ERROR << "Circuit does not contain any cells" << std::endl;
return false;
}
const Matrix4fs& transforms = circuit.getTransforms(gids);
const brain::URIs& uris = circuit.getMorphologyURIs(gids);

brain::GIDSet cr_gids;

// Load simulation information from compartment reports
std::unique_ptr<brion::CompartmentReport> compartmentReport = nullptr;
if (!report.empty())
compartmentReport.reset(new brion::CompartmentReport(
brion::URI(bc.getReportSource(report).getPath()), brion::MODE_READ,
gids));
try
{
compartmentReport.reset(new brion::CompartmentReport(
brion::URI(bc.getReportSource(report).getPath()),
brion::MODE_READ, gids));
}
catch (const std::exception& e)
{
BRAYNS_ERROR << e.what() << std::endl;
compartmentReport = nullptr;
}

const brion::CompartmentCounts& compartmentCounts =
compartmentReport ? compartmentReport->getCompartmentCounts()
Expand All @@ -504,19 +513,24 @@ bool MorphologyLoader::importCircuit(const servus::URI& circuitConfig,
compartmentReport ? compartmentReport->getOffsets()
: brion::SectionOffsets();
cr_gids = compartmentReport ? compartmentReport->getGIDs() : gids;
const Matrix4fs& transforms = circuit.getTransforms(cr_gids);

const auto circuitDensity = _geometryParameters.getCircuitDensity();
BRAYNS_INFO << "Loading "
<< static_cast<size_t>(cr_gids.size() * circuitDensity / 100)
<< " simulated cells" << std::endl;
brain::URIs cr_uris;
if (compartmentReport)
{
Progress progress("Loading URIs...", cr_gids.size());
for (const auto cr_gid : cr_gids)
{
++progress;
const auto it = std::find(gids.begin(), gids.end(), cr_gid);
const auto index = std::distance(gids.begin(), it);
cr_uris.push_back(uris[index]);
}
}
else
cr_uris = uris;

Expand Down
3 changes: 2 additions & 1 deletion brayns/io/MorphologyLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class MorphologyLoader
* @return True if the morphology is successfully loaded, false otherwise
*/
bool importMorphology(const servus::URI& uri, int morphologyIndex,
Scene& scene);
Scene& scene,
const Matrix4f& transformation = Matrix4f());

/** Imports morphology from a circuit for the given target name
*
Expand Down
70 changes: 53 additions & 17 deletions brayns/io/SceneLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <brayns/common/log.h>
#include <brayns/common/scene/Scene.h>
#include <fstream>
#include <servus/uri.h>

namespace brayns
{
Expand Down Expand Up @@ -56,18 +57,24 @@ bool SceneLoader::_parsePositions(const std::string& filename)
switch (lineData.size())
{
case 5:
case 6:
{
Node node;
node.position = Vector3f(boost::lexical_cast<float>(lineData[0]),
boost::lexical_cast<float>(lineData[1]),
boost::lexical_cast<float>(lineData[2]));
node.materialId = boost::lexical_cast<uint16_t>(lineData[3]);
node.filename = lineData[4];
node.fileType = static_cast<FileType>(
boost::lexical_cast<uint16_t>(lineData[4]));
if (lineData.size() == 6)
node.filename = lineData[5];
_nodes.push_back(node);
break;
}
default:
BRAYNS_ERROR << "Invalid line: " << line << std::endl;
BRAYNS_ERROR << "Invalid line, 5 to 6 values were expected, only "
<< lineData.size() << " were found" << std::endl;
BRAYNS_ERROR << line << std::endl;
validParsing = false;
break;
}
Expand All @@ -77,34 +84,63 @@ bool SceneLoader::_parsePositions(const std::string& filename)
return true;
}

void SceneLoader::_importMeshes(Scene& scene, MeshLoader& meshLoader)
void SceneLoader::_importMorphology(Scene& scene, const Node& node,
const Matrix4f& transformation)
{
MorphologyLoader morphologyLoader(_geometryParameters);
const servus::URI uri(node.filename);
if (!morphologyLoader.importMorphology(uri, NB_SYSTEM_MATERIALS +
node.materialId,
scene, transformation))
BRAYNS_ERROR << "Failed to load " << node.filename << std::endl;
}

void SceneLoader::_importMesh(Scene& scene, MeshLoader& loader,
const Node& node, const Matrix4f& transformation)
{
if (!loader.importMeshFromFile(node.filename, scene,
_geometryParameters.getGeometryQuality(),
transformation,
NB_SYSTEM_MATERIALS + node.materialId))
BRAYNS_ERROR << "Failed to load " << node.filename << std::endl;
}

bool SceneLoader::_processNodes(Scene& scene, MeshLoader& meshLoader)
{
// Load mesh at specified positions
uint32_t count = 0;
Progress progress("Loading scene...", _nodes.size());
for (const auto& node : _nodes)
{
++progress;
Matrix4f matrix;
matrix.setTranslation(node.position);
if (!meshLoader.importMeshFromFile(
node.filename, scene, _geometryParameters.getGeometryQuality(),
matrix, NB_SYSTEM_MATERIALS + node.materialId))
Matrix4f transformation;
transformation.setTranslation(node.position);
switch (node.fileType)
{
BRAYNS_DEBUG << "Failed to load " << node.filename << std::endl;
case FileType::point:
scene.getSpheres()[node.materialId].push_back(SpherePtr(
new Sphere(node.position,
_geometryParameters.getRadiusMultiplier())));
scene.getWorldBounds().merge(node.position);
break;
case FileType::morphology:
_importMorphology(scene, node, transformation);
break;
case FileType::mesh:
_importMesh(scene, meshLoader, node, transformation);
break;
default:
BRAYNS_ERROR << "Unknown file type: "
<< static_cast<size_t>(node.fileType) << std::endl;
return false;
}
++count;
}
return true;
}

bool SceneLoader::importFromFile(const std::string& filename, Scene& scene,
MeshLoader& meshLoader)
{
if (_parsePositions(filename))
_importMeshes(scene, meshLoader);
else
return false;

return true;
return _processNodes(scene, meshLoader);
return false;
}
}
31 changes: 24 additions & 7 deletions brayns/io/SceneLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <brayns/common/types.h>
#include <brayns/io/MeshLoader.h>
#include <brayns/io/MorphologyLoader.h>

namespace brayns
{
Expand All @@ -36,30 +37,46 @@ class SceneLoader

/** Imports a file containing the positions and the filename of a list
* of meshes. All loaded meshes are positioned at the corresponding
* coordinates. The file is ASCII and has the following format for every
* line:
* x,y,z,filename
* coordinates. The file is a space separate ASCII and has the following
* format for every line:
*
* @param filename name of the file containing the positions and meshes
* x y z type filename
*
* type is the kind of geometry that will be loaded (point, morphology or
* mesh)
*
* @param filename name of the file containing the positions and geometry
* filenames
* @param Scene holding the meshes
* @param meshLoader Loader used to load a meshes
* @param Scene holding the geometry
* @param meshLoader Loader used to load meshes
* @return true if the file was successfully imported. False otherwise.
*/
bool importFromFile(const std::string& filename, Scene& scene,
MeshLoader& meshLoader);

private:
enum class FileType
{
point = 0,
morphology = 1,
mesh = 2
};

struct Node
{
Vector3f position;
uint16_t materialId;
FileType fileType;
std::string filename;
};
typedef std::vector<Node> Nodes;

bool _parsePositions(const std::string& filename);
void _importMeshes(Scene& scene, MeshLoader& meshLoader);
void _importMorphology(Scene& scene, const Node& node,
const Matrix4f& transformation);
void _importMesh(Scene& scene, MeshLoader& loader, const Node& node,
const Matrix4f& transformation);
bool _processNodes(Scene& scene, MeshLoader& loader);

const GeometryParameters& _geometryParameters;

Expand Down

0 comments on commit 4db3ff0

Please sign in to comment.