PointTree* loadPointSet(const char* pointFileName)
	{
	std::cout<<"Loading points from "<<pointFileName<<"..."<<std::flush;
	std::vector<Point> points;
	Misc::File pointFile(pointFileName,"rt");
	while(!pointFile.eof())
		{
		char line[256];
		pointFile.gets(line,sizeof(line));
		float p[3];
		if(sscanf(line,"%f %f %f",&p[0],&p[1],&p[2])==3)
			points.push_back(Point(p));
		}
	std::cout<<" done"<<std::endl;
	
	std::cout<<"Creating kd-tree of "<<points.size()<<" points..."<<std::flush;
	PointTree* pointTree=new PointTree(points.size());
	Point* ptps=pointTree->accessPoints();
	for(size_t i=0;i<points.size();++i)
		ptps[i]=points[i];
	pointTree->releasePoints(4);
	std::cout<<" done"<<std::endl;
	
	return pointTree;
	}
void FormattedPointSetReaderNode::readPoints(CoordinateNode* coordNode,ColorNode* colorNode) const
	{
	CoordinateNode::PointList& points=coordNode->getPoints();
	ColorNode::ColorList* colors=colorNode!=0?&colorNode->getColors():0;
	const EllipsoidNode* e=dynamic_cast<const EllipsoidNode*>(ellipsoid.getPointer());
	const ColorInterpolatorNode* c=dynamic_cast<const ColorInterpolatorNode*>(colorMap.getPointer());
	
	/* Open the input file: */
	Misc::File pointFile(url.c_str(),"rt");
	
	/* Skip the header lines: */
	char line[256];
	for(int i=0;i<numHeaderLines;++i)
		pointFile.gets(line,sizeof(line));
	
	/* Read all lines in the point file: */
	while(!pointFile.eof())
		{
		/* Read the next line from the file: */
		pointFile.gets(line,sizeof(line));
		
		/* Extract the relevant information: */
		double values[4];
		for(int i=0;i<4;++i)
			if(columnIndices[i]>=0)
				{
				/* Add a temporary separator into the string and extract the value: */
				char savedChar=line[columnStarts[columnIndices[i]]+columnWidths[columnIndices[i]]];
				line[columnStarts[columnIndices[i]]+columnWidths[columnIndices[i]]]='\0';
				values[i]=atof(line+columnStarts[columnIndices[i]]);
				line[columnStarts[columnIndices[i]]+columnWidths[columnIndices[i]]]=savedChar;
				}
		
		if(e!=0)
			{
			/* Convert the point to Cartesian coordinates and store it: */
			for(int i=0;i<2;++i)
				values[i]=Math::rad(values[i]);
			values[2]*=1000.0;
			points.push_back(e->sphericalToCartesian(values));
			}
		else
			{
			/* Store the point in the given coordinates: */
			points.push_back(CoordinateNode::Point(values));
			}
		
		if(columnIndices[3]>=0&&c!=0)
			{
			/* Convert the point value to a color and store it: */
			colors->push_back(c->interpolate(float(values[3])));
			}
		}
	}
bool RenderableGalaxy::initialize() {
    // Aspect is currently hardcoded to cubic voxels.
    _aspect = static_cast<glm::vec3>(_volumeDimensions);
    _aspect = _aspect / std::max(std::max(_aspect.x, _aspect.y), _aspect.z);

    RawVolumeReader<glm::tvec4<GLfloat>> reader(_volumeFilename, _volumeDimensions);
    _volume = reader.read();
    
    _texture = std::make_unique<ghoul::opengl::Texture>(
        _volumeDimensions,
        ghoul::opengl::Texture::Format::RGBA,
        GL_RGBA32F,
        GL_FLOAT,
        ghoul::opengl::Texture::FilterMode::Linear,
        ghoul::opengl::Texture::WrappingMode::Clamp);
   
    _texture->setPixelData(reinterpret_cast<char*>(_volume->data()), ghoul::opengl::Texture::TakeOwnership::No);
    _texture->setDimensions(_volume->dimensions());
    _texture->uploadTexture();

    _raycaster = std::make_unique<GalaxyRaycaster>(*_texture);
    _raycaster->initialize();

    OsEng.renderEngine().raycasterManager().attachRaycaster(*_raycaster.get());

    std::function<void(bool)> onChange = [&](bool enabled) {
        if (enabled) {
            OsEng.renderEngine().raycasterManager().attachRaycaster(*_raycaster.get());
        }
        else {
            OsEng.renderEngine().raycasterManager().detachRaycaster(*_raycaster.get());
        }
    };

    onEnabledChange(onChange);

    addProperty(_stepSize);
    addProperty(_pointStepSize);
    addProperty(_translation);
    addProperty(_rotation);
    addProperty(_enabledPointsRatio);
    
    // initialize points.
    std::ifstream pointFile(_pointsFilename, std::ios::in | std::ios::binary);

    std::vector<glm::vec3> pointPositions;
    std::vector<glm::vec3> pointColors;

    int64_t nPoints;
    pointFile.seekg(0, std::ios::beg); // read heder.
    pointFile.read(reinterpret_cast<char*>(&nPoints), sizeof(int64_t));

    _nPoints = static_cast<size_t>(nPoints);

    size_t nFloats = _nPoints * 7;

    float* pointData = new float[nFloats];
    pointFile.seekg(sizeof(int64_t), std::ios::beg); // read past heder.
    pointFile.read(reinterpret_cast<char*>(pointData), nFloats * sizeof(float));
    pointFile.close();

    float maxdist = 0;
    

    float x, y, z, r, g, b, a;
    for (size_t i = 0; i < _nPoints; ++i) {
        float x = pointData[i * 7 + 0];
        float y = pointData[i * 7 + 1];
        float z = pointData[i * 7 + 2];
        float r = pointData[i * 7 + 3];
        float g = pointData[i * 7 + 4];
        float b = pointData[i * 7 + 5];
        maxdist = std::max(maxdist, glm::length(glm::vec3(x, y, z)));
        //float a = pointData[i * 7 + 6];  alpha is not used.
                               
        pointPositions.push_back(glm::vec3(x, y, z));
        pointColors.push_back(glm::vec3(r, g, b));        
    }

    std::cout << maxdist << std::endl;

    delete[] pointData;

    glGenVertexArrays(1, &_pointsVao);
    glGenBuffers(1, &_positionVbo);
    glGenBuffers(1, &_colorVbo);

    glBindVertexArray(_pointsVao);
    glBindBuffer(GL_ARRAY_BUFFER, _positionVbo);
    glBufferData(GL_ARRAY_BUFFER,
        pointPositions.size()*sizeof(glm::vec3),
        pointPositions.data(),
        GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, _colorVbo);
    glBufferData(GL_ARRAY_BUFFER,
        pointColors.size()*sizeof(glm::vec3),
        pointColors.data(),
        GL_STATIC_DRAW);


    RenderEngine& renderEngine = OsEng.renderEngine();
    _pointsProgram = renderEngine.buildRenderProgram("Galaxy points",
        "${MODULE_GALAXY}/shaders/points.vs",
        "${MODULE_GALAXY}/shaders/points.fs",
        ghoul::Dictionary(),
        RenderEngine::RenderProgramType::Post);

    _pointsProgram->setIgnoreUniformLocationError(ghoul::opengl::ProgramObject::IgnoreError::Yes);

    GLint positionAttrib = _pointsProgram->attributeLocation("inPosition");
    GLint colorAttrib = _pointsProgram->attributeLocation("inColor");

    glBindBuffer(GL_ARRAY_BUFFER, _positionVbo);
    glEnableVertexAttribArray(positionAttrib);    
    glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, _colorVbo);
    glEnableVertexAttribArray(colorAttrib);
    glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
        
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    return true;
}
void loadPointSet(const char* lidarFileName,unsigned int memCacheSize,const char* pointFileName,Scalar filterRadius,int numLobes,const Vector& pointOffset)
	{
	/* Create a color and coordinate node to receive points: */
	SceneGraph::ColorNode* color=new SceneGraph::ColorNode;
	SceneGraph::CoordinateNode* coord=new SceneGraph::CoordinateNode;
	
	/* Create the point set node: */
	SceneGraph::PointSetNode* pointSet=new SceneGraph::PointSetNode;
	pointSet->color.setValue(color);
	pointSet->coord.setValue(coord);
	pointSet->pointSize.setValue(5);
	pointSet->update();
	SceneGraph::ShapeNode* shape=new SceneGraph::ShapeNode;
	shape->geometry.setValue(pointSet);
	shape->update();
	getSceneGraphRoot().children.appendValue(shape);
	getSceneGraphRoot().update();
	
	/* Create a temporary LiDAR processing octree to calculate point elevations: */
	LidarProcessOctree lpo(lidarFileName,size_t(memCacheSize)*size_t(1024*1024));
	
	/* Open the point file: */
	IO::AutoFile pointFile(IO::openFile(pointFileName));
	IO::ValueSource pointSource(*pointFile);
	pointSource.setWhitespace("");
	pointSource.setPunctuation(",\n");
	pointSource.setQuotes("\"");
	
	/* Read the header line: */
	int valueCol=-1;
	int posCol[2]={-1,-1};
	for(int col=0;!pointSource.eof()&&pointSource.peekc()!='\n';++col)
		{
		std::string columnHeader=pointSource.readString();
		if(columnHeader=="DamageID1")
			valueCol=col;
		else if(columnHeader=="POINT_X")
			posCol[0]=col;
		else if(columnHeader=="POINT_Y")
			posCol[1]=col;
		if(pointSource.peekc()==',')
			pointSource.skipString();
		}
	
	/* Go to the next line: */
	pointSource.skipString();
	
	/* Read all points: */
	while(!pointSource.eof())
		{
		/* Read the current line: */
		double value=0.0;
		double pos[2]={0.0,0.0};
		for(int col=0;!pointSource.eof()&&pointSource.peekc()!='\n';++col)
			{
			if(col==valueCol)
				value=pointSource.readNumber();
			else if(col==posCol[0])
				pos[0]=pointSource.readNumber();
			else if(col==posCol[1])
				pos[1]=pointSource.readNumber();
			else
				pointSource.skipString();
			if(pointSource.peekc()==',')
				pointSource.skipString();
			}
		
		/* Calculate the current point's elevation: */
		LidarRadialElevationSampler les(pos,filterRadius,numLobes);
		lpo.processPointsInBox(les.getBox(),les);
		if(les.getAbsWeightSum()>0.0)
			{
			/* Add the point to the coordinate node: */
			SceneGraph::Point p;
			for(int i=0;i<2;++i)
				p[i]=SceneGraph::Scalar(pos[i]-double(pointOffset[i]));
			p[2]=SceneGraph::Scalar(les.getValue()-double(pointOffset[2]));
			coord->point.appendValue(p);
			
			if(value<2.5)
				color->color.appendValue(SceneGraph::Color(1.0f,0.0f,0.0f));
			else if(value<3.5)
				color->color.appendValue(SceneGraph::Color(1.0f,0.5f,0.0f));
			else if(value<4.5)
				color->color.appendValue(SceneGraph::Color(0.5f,1.0f,0.0f));
			else if(value<5.5)
				color->color.appendValue(SceneGraph::Color(0.0f,1.0f,0.0f));
			else
				color->color.appendValue(SceneGraph::Color(0.0f,1.0f,0.0f));
			}
		
		/* Go to the next line: */
		pointSource.skipString();
		}
	
	color->update();
	coord->update();
	}
Beispiel #5
0
PointSet::PointSet(const char* pointFileName,double flatteningFactor,double scaleFactor)
	{
	/* Open the point file: */
	Misc::File pointFile(pointFileName,"rt");
	
	/* Read the header line from the point file: */
	int latIndex=-1;
	int lngIndex=-1;
	int radiusIndex=-1;
	enum RadiusMode
		{
		RADIUS,DEPTH,NEGDEPTH
		} radiusMode=RADIUS;
	int index=0;
	while(true)
		{
		char valueBuffer[40];
		int terminator=getNextValue(pointFile,valueBuffer,sizeof(valueBuffer));
		if(terminator=='\n')
			break;
		else if(terminator==EOF)
			Misc::throwStdErr("PointSet::PointSet: Early end of file in input file \"%s\"",pointFileName);
		else if(strcasecmp(valueBuffer,"Latitude")==0||strcasecmp(valueBuffer,"Lat")==0)
			latIndex=index;
		else if(strcasecmp(valueBuffer,"Longitude")==0||strcasecmp(valueBuffer,"Long")==0||strcasecmp(valueBuffer,"Lon")==0)
			lngIndex=index;
		else if(strcasecmp(valueBuffer,"Radius")==0)
			{
			radiusIndex=index;
			radiusMode=RADIUS;
			}
		else if(strcasecmp(valueBuffer,"Depth")==0)
			{
			radiusIndex=index;
			radiusMode=DEPTH;
			}
		else if(strcasecmp(valueBuffer,"Negative Depth")==0||strcasecmp(valueBuffer,"Neg Depth")==0||strcasecmp(valueBuffer,"NegDepth")==0)
			{
			radiusIndex=index;
			radiusMode=NEGDEPTH;
			}
		++index;
		}
	
	/* Check if all required portions have been detected: */
	if(latIndex<0||lngIndex<0||radiusIndex<0)
		Misc::throwStdErr("PointSet::PointSet: Missing point components in input file \"%s\"",pointFileName);
	
	/* Read all point positions from the point file: */
	bool finished=false;
	while(!finished)
		{
		/* Read the next line from the input file: */
		int index=0;
		float sphericalCoordinates[3]={0.0f,0.0f,0.0f}; // Initialization just to shut up gcc
		int parsedComponentsMask=0x0;
		while(true)
			{
			char valueBuffer[40];
			int terminator=getNextValue(pointFile,valueBuffer,sizeof(valueBuffer));
			if(terminator=='\n')
				break;
			else if(terminator==EOF)
				{
				finished=true;
				break;
				}
			else if(index==latIndex)
				{
				sphericalCoordinates[0]=Math::rad(float(atof(valueBuffer)));
				parsedComponentsMask|=0x1;
				}
			else if(index==lngIndex)
				{
				sphericalCoordinates[1]=Math::rad(float(atof(valueBuffer)));
				parsedComponentsMask|=0x2;
				}
			else if(index==radiusIndex)
				{
				sphericalCoordinates[2]=float(atof(valueBuffer));
				parsedComponentsMask|=0x4;
				}
			++index;
			}
		
		/* Check if a complete set of coordinates has been parsed: */
		if(parsedComponentsMask==0x7&&!isnan(sphericalCoordinates[2]))
			{
			/* Convert the read spherical coordinates to Cartesian coordinates: */
			Vertex p;
			p.position=Vertex::Position(0,0,0); // To shut up gcc
			switch(radiusMode)
				{
				case RADIUS:
					calcRadiusPos(sphericalCoordinates[0],sphericalCoordinates[1],sphericalCoordinates[2]*1000.0f,scaleFactor,p.position.getXyzw());
					break;
				
				case DEPTH:
					calcDepthPos(sphericalCoordinates[0],sphericalCoordinates[1],sphericalCoordinates[2]*1000.0f,flatteningFactor,scaleFactor,p.position.getXyzw());
					break;
				
				case NEGDEPTH:
					calcDepthPos(sphericalCoordinates[0],sphericalCoordinates[1],-sphericalCoordinates[2]*1000.0f,flatteningFactor,scaleFactor,p.position.getXyzw());
					break;
				}
			
			/* Append the point to the point set: */
			points.push_back(p);
			}
		}
	std::cout<<points.size()<<" points parsed from "<<pointFileName<<std::endl;
	}