Ray Tracer  2020
Public Member Functions | Private Member Functions | Private Attributes | List of all members
SceneReader Class Reference

Simple parser for Scene description files. More...

#include <SceneReader.h>

Inheritance diagram for SceneReader:
NonCopyable

Public Member Functions

 SceneReader (Scene *scene)
 SceneReader constructor. More...
 
 ~SceneReader ()
 SceneReader destructor. More...
 
void read (const std::string &filename)
 Read Scene data from a file. More...
 

Private Member Functions

void parseTokenBlock (std::queue< std::string > &tokenBlock)
 Parse a block of tokens. More...
 
Colour parseColour (std::queue< std::string > &tokenBlock)
 Read Colour information from a block of tokens. More...
 
double parseNumber (std::queue< std::string > &tokenBlock)
 Read a number information from a block of tokens. More...
 
void parseSceneBlock (std::queue< std::string > &tokenBlock)
 Parse a block of tokens representing a Scene. More...
 
void parseCameraBlock (std::queue< std::string > &tokenBlock)
 Parse a block of tokens representing a Scene. More...
 
void parseLightBlock (std::queue< std::string > &tokenBlock)
 Parse a block of tokens representing a Camera. More...
 
std::shared_ptr< ObjectparseObjectBlock (std::queue< std::string > &tokenBlock)
 Parse a block of tokens representing a LightSource. More...
 
void parseMaterialBlock (std::queue< std::string > &tokenBlock)
 Parse a block of tokens representing a Material. More...
 
- Private Member Functions inherited from NonCopyable
 NonCopyable ()
 NonCopyable default constructor. More...
 
 ~NonCopyable ()
 NonCopyable destructor. More...
 

Private Attributes

Scenescene_
 The Scene which information is read to. More...
 
int startLine_
 The first line of the current block being parsed, for error reporting. More...
 
std::map< std::string, Materialmaterials_
 A dictionary of Material types that have been read, and which can be used for subsequent Object properties. More...
 

Detailed Description

Simple parser for Scene description files.

This class implements a simple parser for Scene description files. A SceneReader is linked to a Scene object, and can then read one or more text files describing Scene properties, Cameras, LightSources, and Objects. These files are defined in blocks, and are case-insensitive. Whitespace is contracted, so new lines, spaces, and tabs are all just token separators. Comments are introduced with #, and continue to the end of the line.

There are five main types of block:

Scene Blocks

Example:

 Scene
   renderSize 800 600
   backgroundColour 0.5 0.5 0.5
   filename output.png
   rayDepth 5
 End

Allowed elements within a Scene block are:

Camera Blocks

Example:

Camera PinholeCamera 1.5
  Rotate X -15
  Translate 0 -8 -15
End

A Camera block starts with a line giving the type of Camera and a parameter for that Camera if one can be provided. In the example, a PinholeCamera is created with a focalLength of 1.5 Allowed elements within a Scene block are:

Light Blocks

Examples:

Light PointLight
  Location 1 2 3
  Colour 20 30 40
End

Light SpotLight
  Location 0 -5 0
  Colour 10 10 10
  Direction 0 1 0
  Angle 30
End

A Light block starts with a line giving the type of Light.

Allowed elements within a Light block are:

Not all light source types use all parameters, and any surplus elements are ignored:

Material Blocks

Example:

Material Gold
  Colour 1 0.88 0.25
  Specular 1 1 1 100
  Mirror 1 0.88 0.25
End

A Material block starts with a line giving the name of a Material (in this example, 'Gold'). Allowed elements within a Material block are:

Object Blocks

Example:

Object Sphere
  Material Gold
  Specular 0.7 0.7 0.7 75 
  Scale3 1 2 3
  Rotate Y 45
  Translate 0 0 3

An Object block starts with a line giving the type of Object to create (a Sphere in this case). Allowed elements within an Object block are:

Constructor & Destructor Documentation

◆ SceneReader()

SceneReader::SceneReader ( Scene scene)

SceneReader constructor.

Parameters
sceneA pointer to the Scene which data will be read into.

◆ ~SceneReader()

SceneReader::~SceneReader ( )

SceneReader destructor.

Member Function Documentation

◆ parseCameraBlock()

void SceneReader::parseCameraBlock ( std::queue< std::string > &  tokenBlock)
private

Parse a block of tokens representing a Scene.

This method reads Scene information from a block of tokens. The format for Scene blocks is described above, and any errors in parsing the block will terminate the program.

Parameters
tokenBlockA sequence of tokens to be interpreted.

◆ parseColour()

Colour SceneReader::parseColour ( std::queue< std::string > &  tokenBlock)
private

Read Colour information from a block of tokens.

This tries to interpret the next three items in a block of tokens as a Colour. Three tokens are taken from the block and interpreted as red, green, and blue values for the Colour.

If there is a problem in this process, the program is terminated.

Parameters
tokenBlockA sequence of tokens to read the Colour from.
Returns
The Colour read from the block of tokens..

◆ parseLightBlock()

void SceneReader::parseLightBlock ( std::queue< std::string > &  tokenBlock)
private

Parse a block of tokens representing a Camera.

This method reads Camera information from a block of tokens. The format for Camera blocks is described above, and any errors in parsing the block will terminate the program.

Parameters
tokenBlockA sequence of tokens to be interpreted.

◆ parseMaterialBlock()

void SceneReader::parseMaterialBlock ( std::queue< std::string > &  tokenBlock)
private

Parse a block of tokens representing a Material.

This method reads Material information from a block of tokens. The format for Material blocks is described above, and any errors in parsing the block will terminate the program.

Parameters
tokenBlockA sequence of tokens to be interpreted.

◆ parseNumber()

double SceneReader::parseNumber ( std::queue< std::string > &  tokenBlock)
private

Read a number information from a block of tokens.

This tries to interpret the next item in a block of tokens as a number. The token is removed from the block.

If there is a problem in this process, the program is terminated.

Parameters
tokenBlockA sequence of tokens to read the Colour from.
Returns
The Colour read from the block of tokens..

◆ parseObjectBlock()

std::shared_ptr< Object > SceneReader::parseObjectBlock ( std::queue< std::string > &  tokenBlock)
private

Parse a block of tokens representing a LightSource.

This method reads LightSource information from a block of tokens. The format for LightSource blocks is described above, and any errors in parsing the block will terminate the program.

Parameters
tokenBlockA sequence of tokens to be interpreted.

◆ parseSceneBlock()

void SceneReader::parseSceneBlock ( std::queue< std::string > &  tokenBlock)
private

Parse a block of tokens representing a Scene.

This method reads Scene information from a block of tokens. The format for Scene blocks is described above, and any errors in parsing the block will terminate the program.

Parameters
tokenBlockA sequence of tokens to be interpreted.

◆ parseTokenBlock()

void SceneReader::parseTokenBlock ( std::queue< std::string > &  tokenBlock)
private

Parse a block of tokens.

When reading a file, it is separated into a squence of tokens (words and numbers) separated by whitespace. These are formed into blocks, each ending with the token 'End'. This method determines how to interpret each block.

Parameters
tokenBlockA sequence of tokens to be interpreted.

◆ read()

void SceneReader::read ( const std::string &  filename)

Read Scene data from a file.

This method reads data from a file in the format discussed above. It adds information to the Scene linked to this SceneReader, and so multiple files can be combined into one Scene.

If an error is encountered parsing the file, the program is terminated.

Parameters
filenameThe name of the file to read.

Member Data Documentation

◆ materials_

std::map<std::string, Material> SceneReader::materials_
private

A dictionary of Material types that have been read, and which can be used for subsequent Object properties.

◆ scene_

Scene* SceneReader::scene_
private

The Scene which information is read to.

◆ startLine_

int SceneReader::startLine_
private

The first line of the current block being parsed, for error reporting.


The documentation for this class was generated from the following files: